]> rtime.felk.cvut.cz Git - frescor/fwp.git/blob - wme_test/schedlat.c
Merge branch 'master' of molnam1@rtime.felk.cvut.cz:/var/git/frescor
[frescor/fwp.git] / wme_test / schedlat.c
1 #include <stdio.h>
2 #include <signal.h>
3 #include <stdint.h>
4 #include <time.h>
5 #include <unistd.h>
6 #include <stdlib.h>
7 #include <sched.h>
8 #include <string.h>
9 #include "common.h"
10
11 static inline uint64_t rdtsc(void) {
12         uint32_t hi, lo;
13         asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
14         return ((uint64_t)hi << 32) | lo;
15 }
16
17 void int_handler(int sig)
18 {
19         exit(0);
20 }
21
22 static double cpu_mhz;
23
24 void find_cpu_clock()
25 {
26         FILE *f;
27         char line[100];
28         f = fopen("/proc/cpuinfo", "r");
29         if (!f) {
30                 perror("/proc/cpuinfo");
31                 exit(1);
32         }
33         while (fgets(line, sizeof(line), f)) {
34                 if (strncmp(line, "cpu MHz", 7) == 0) {
35                         double mhz;
36                         int ret = sscanf(line, "cpu MHz : %lf", &mhz);
37                         if (ret != 1) {
38                                 fprintf(stderr, "Can't read cpu Mhz\n");
39                                 exit(1);
40                         }
41                         cpu_mhz = mhz;
42                         printf("CPU frequency: %.0lf MHz\n", cpu_mhz);
43                         break;
44                 }
45         }
46         fclose(f);
47 }
48
49 #define CLOCK_TO_SEC(clock) ((double)(clock)/(cpu_mhz*1000.0*1000.0))
50
51 void measure()
52 {
53         struct timespec ts = { .tv_sec = 0, .tv_nsec = 10*1000*1000 };
54         /* uint64_t start, end; */
55         long long duration;
56         long long difference, max_difference = 0;
57         struct timespec start, end;
58         while (1) {
59                 clock_gettime(CLOCK_MONOTONIC, &start);
60                 nanosleep(&ts, NULL);
61                 clock_gettime(CLOCK_MONOTONIC, &end);
62                 duration = timespec_sub_usec(&end, &start);
63                 difference = duration - ts.tv_nsec/1000;
64                 if (difference > max_difference) {
65                         max_difference = difference;
66                         printf("%lld usec\n", max_difference);
67                 }
68         }
69 }
70
71 int main(int argc, char *argv[])
72 {
73         struct sigaction act = {
74                 .sa_handler = int_handler
75         };
76
77         find_cpu_clock();
78         sigaction(SIGINT, &act, NULL);
79         set_rt_prio(50);
80         measure();
81         return 0;
82 }