Guest User

Untitled

a guest
Apr 19th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.43 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <pthread.h>
  3. #include <stdlib.h>
  4. #include <string.h>
  5. #include <signal.h>
  6.  
  7. volatile uint64_t glob1;
  8. volatile uint64_t glob2;
  9. volatile int stop;
  10.  
  11. #define HIST_SIZE 100
  12. void *
  13. ThreadWriter(void *x)
  14. {
  15. uint32_t i = 0;
  16. uint32_t delay_dist[HIST_SIZE] = {0};
  17. uint32_t h, l, tsc1, tsc2;
  18. volatile uint32_t delay = 0;
  19.  
  20. __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h));
  21. tsc1 = ((uint64_t)l) | (((uint64_t)h) << 32);
  22.  
  23. while(!stop)
  24. {
  25. glob1 = i;
  26.  
  27. while(glob2 < glob1){};
  28.  
  29. __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h));
  30. tsc2 = ((uint64_t)l) | (((uint64_t)h) << 32);
  31. delay = (tsc2 - tsc1)/10;
  32. tsc1 = tsc2;
  33.  
  34. if (delay < HIST_SIZE)
  35. delay_dist[delay]++;
  36.  
  37. i++;
  38. }
  39.  
  40. printf("Ping-pong distribution by amount of cycles:\n");
  41. for(int d = 0; d < HIST_SIZE; d++)
  42. printf("%3d: %d\n", 10*d, delay_dist[d]);
  43.  
  44. return x;
  45. }
  46.  
  47. void *
  48. ThreadReader(void *x)
  49. {
  50. volatile uint32_t my_glob1 = glob1;
  51.  
  52. while(!stop)
  53. {
  54. if (my_glob1 != glob1)
  55. {
  56. glob2 = glob1;
  57. my_glob1 = glob1;
  58. }
  59. }
  60.  
  61. return x;
  62. }
  63.  
  64. void
  65. inthandler(int sig)
  66. {
  67. stop = 1;
  68. glob2++;
  69. }
  70.  
  71. #define THR 2
  72.  
  73. int main()
  74. {
  75. pthread_t t[THR];
  76. int i;
  77. struct sigaction act;
  78.  
  79. act.sa_handler = inthandler;
  80. sigaction(SIGINT, &act, NULL);
  81.  
  82. pthread_create(&(t[0]), NULL, ThreadWriter, NULL);
  83. pthread_create(&(t[1]), NULL, ThreadReader, NULL);
  84.  
  85. for (i = 0; i < THR; i++)
  86. pthread_join(t[i], NULL);
  87.  
  88. return 0;
  89. }
Add Comment
Please, Sign In to add comment