]> rtime.felk.cvut.cz Git - frescor/fwp.git/commitdiff
schedlat.c added
authorMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 6 Feb 2008 15:48:04 +0000 (16:48 +0100)
committerMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 6 Feb 2008 15:48:04 +0000 (16:48 +0100)
wme_test/schedlat.c [new file with mode: 0644]

diff --git a/wme_test/schedlat.c b/wme_test/schedlat.c
new file mode 100644 (file)
index 0000000..f3a2237
--- /dev/null
@@ -0,0 +1,82 @@
+#include <stdio.h>
+#include <signal.h>
+#include <stdint.h>
+#include <time.h>
+#include <unistd.h>
+#include <stdlib.h>
+#include <sched.h>
+#include <string.h>
+#include "common.h"
+
+static inline uint64_t rdtsc(void) {
+       uint32_t hi, lo;
+       asm volatile ("rdtsc" : "=a" (lo), "=d" (hi));
+       return ((uint64_t)hi << 32) | lo;
+}
+
+void int_handler(int sig)
+{
+       exit(0);
+}
+
+static double cpu_mhz;
+
+void find_cpu_clock()
+{
+       FILE *f;
+       char line[100];
+       f = fopen("/proc/cpuinfo", "r");
+       if (!f) {
+               perror("/proc/cpuinfo");
+               exit(1);
+       }
+       while (fgets(line, sizeof(line), f)) {
+               if (strncmp(line, "cpu MHz", 7) == 0) {
+                       double mhz;
+                       int ret = sscanf(line, "cpu MHz : %lf", &mhz);
+                       if (ret != 1) {
+                               fprintf(stderr, "Can't read cpu Mhz\n");
+                               exit(1);
+                       }
+                       cpu_mhz = mhz;
+                       printf("CPU frequency: %.0lf MHz\n", cpu_mhz);
+                       break;
+               }
+       }
+       fclose(f);
+}
+
+#define CLOCK_TO_SEC(clock) ((double)(clock)/(cpu_mhz*1000.0*1000.0))
+
+void measure()
+{
+       struct timespec ts = { .tv_sec = 0, .tv_nsec = 10*1000*1000 };
+       /* uint64_t start, end; */
+       long long duration;
+       long long difference, max_difference = 0;
+       struct timespec start, end;
+       while (1) {
+               clock_gettime(CLOCK_MONOTONIC, &start);
+               nanosleep(&ts, NULL);
+               clock_gettime(CLOCK_MONOTONIC, &end);
+               duration = timespec_sub_usec(&end, &start);
+               difference = duration - ts.tv_nsec/1000;
+               if (difference > max_difference) {
+                       max_difference = difference;
+                       printf("%lld usec\n", max_difference);
+               }
+       }
+}
+
+int main(int argc, char *argv[])
+{
+       struct sigaction act = {
+               .sa_handler = int_handler
+       };
+
+       find_cpu_clock();
+       sigaction(SIGINT, &act, NULL);
+       set_rt_prio(50);
+       measure();
+       return 0;
+}