utils.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/time.h>
  4. #include <malloc.h>
  5. #include <sched.h>
  6. #include "utils.h"
  7. /**
  8. * Calculate a duration
  9. *
  10. * @param start start of the computing or null if
  11. * this is the first time this function is inoked
  12. *
  13. * @return current time
  14. */
  15. struct timeval* showTime(struct timeval* start)
  16. {
  17. struct timeval* stop;
  18. long tv_sec;
  19. long tv_usec;
  20. stop = (struct timeval*)malloc(sizeof(struct timeval));
  21. gettimeofday(stop, 0);
  22. if (!start) return stop;
  23. tv_sec = (start->tv_usec > stop->tv_usec) ? stop->tv_sec - start->tv_sec - 1 : stop->tv_sec - start->tv_sec;
  24. tv_usec = (start->tv_usec > stop->tv_usec) ? 1000000 + stop->tv_usec - start->tv_usec : stop->tv_usec - start->tv_usec;
  25. fprintf(stderr, "%lus %lums %luµs\n", tv_sec, tv_usec/1000, tv_sec % 1000);
  26. return stop;
  27. }
  28. /**
  29. * Switch to real-time mode
  30. */
  31. void scheduler_realtime()
  32. {
  33. struct sched_param p;
  34. p.__sched_priority = sched_get_priority_max(SCHED_RR);
  35. if( sched_setscheduler( 0, SCHED_RR, &p ) == -1 ) {
  36. perror("Failed to switch to realtime scheduler.");
  37. }
  38. }
  39. /**
  40. * Exit from real-time mode
  41. */
  42. void scheduler_standard()
  43. {
  44. struct sched_param p;
  45. p.__sched_priority = 0;
  46. if( sched_setscheduler( 0, SCHED_OTHER, &p ) == -1 ) {
  47. perror("Failed to switch to normal scheduler.");
  48. }
  49. }