Advertisement
Guest User

Untitled

a guest
Mar 31st, 2020
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. #define _GNU_SOURCE
  2. #include <sched.h>
  3. #include <stdio.h>
  4. #include <iostream>
  5. #include <chrono>
  6. #include <cmath>
  7. #include <unistd.h>
  8. void setSchedulingPolicy (int policy, int priority)
  9. {
  10. sched_param sched;
  11. sched_getparam(0, &sched);
  12. sched.sched_priority = priority;
  13. if (sched_setscheduler(0, policy, &sched)) {
  14. perror("sched_setscheduler");
  15. exit(EXIT_FAILURE);
  16. }
  17. }
  18.  
  19. void workload_1ms (void)
  20. {
  21. int repeat = 100000; // tune this for the right amount of workload
  22. for (int i = 0; i <= repeat; i++)
  23. {
  24. int count[1000]={};
  25. for(int j=0;j<1000;j++)
  26. {
  27. count[j]=j;
  28. }
  29. //for(int k=0;k<99;k++)
  30. {
  31. for(int p=0;p<99;p++)
  32. {
  33. if(count[p+1]>count[p])
  34. {
  35. int tmp=count[p];
  36. count[p]=count[p+1];
  37. count[p+1]=count[p];
  38. }
  39. }
  40. }
  41. }
  42. }
  43.  
  44. void pinCPU (int cpu_number)
  45. {
  46. cpu_set_t mask;
  47. CPU_ZERO(&mask);
  48.  
  49. CPU_SET(cpu_number, &mask);
  50.  
  51. if (sched_setaffinity(0, sizeof(cpu_set_t), &mask) == -1)
  52. {
  53. perror("sched_setaffinity");
  54. exit(EXIT_FAILURE);
  55. }
  56. }
  57.  
  58. int main (void)
  59. {
  60. pinCPU (0);
  61. int period = 7000; // unit: microsecond
  62. int delta;
  63. setSchedulingPolicy (SCHED_FIFO, 98);
  64. while (1)
  65. {
  66. std::chrono::system_clock::time_point startTime = std::chrono::system_clock::now();
  67. for (int j = 0; j < 4; j++)
  68. {
  69. workload_1ms ();
  70. }
  71. std::chrono::system_clock::time_point endTime = std::chrono::system_clock::now();
  72. delta = std::chrono::duration_cast<std::chrono::microseconds>(endTime - startTime).count();
  73. if (delta > period)
  74. {
  75. continue;
  76. }
  77. else
  78. {
  79. usleep (period-delta);
  80. }
  81. }
  82. return 0;
  83. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement