Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <pthread.h>
- #include <stdlib.h>
- #include <string.h>
- #include <signal.h>
- volatile uint64_t glob1;
- volatile uint64_t glob2;
- volatile int stop;
- #define HIST_SIZE 100
- void *
- ThreadWriter(void *x)
- {
- uint32_t i = 0;
- uint32_t delay_dist[HIST_SIZE] = {0};
- uint32_t h, l, tsc1, tsc2;
- volatile uint32_t delay = 0;
- __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h));
- tsc1 = ((uint64_t)l) | (((uint64_t)h) << 32);
- while(!stop)
- {
- glob1 = i;
- while(glob2 < glob1){};
- __asm__ __volatile__("rdtsc" : "=a" (l), "=d" (h));
- tsc2 = ((uint64_t)l) | (((uint64_t)h) << 32);
- delay = (tsc2 - tsc1)/10;
- tsc1 = tsc2;
- if (delay < HIST_SIZE)
- delay_dist[delay]++;
- i++;
- }
- printf("Ping-pong distribution by amount of cycles:\n");
- for(int d = 0; d < HIST_SIZE; d++)
- printf("%3d: %d\n", 10*d, delay_dist[d]);
- return x;
- }
- void *
- ThreadReader(void *x)
- {
- volatile uint32_t my_glob1 = glob1;
- while(!stop)
- {
- if (my_glob1 != glob1)
- {
- glob2 = glob1;
- my_glob1 = glob1;
- }
- }
- return x;
- }
- void
- inthandler(int sig)
- {
- stop = 1;
- glob2++;
- }
- #define THR 2
- int main()
- {
- pthread_t t[THR];
- int i;
- struct sigaction act;
- act.sa_handler = inthandler;
- sigaction(SIGINT, &act, NULL);
- pthread_create(&(t[0]), NULL, ThreadWriter, NULL);
- pthread_create(&(t[1]), NULL, ThreadReader, NULL);
- for (i = 0; i < THR; i++)
- pthread_join(t[i], NULL);
- return 0;
- }
Add Comment
Please, Sign In to add comment