Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Merge branch 'master' of git+ssh://scm.gforge.inria.fr//gitroot/simgrid/simgrid
[simgrid.git] / teshsuite / smpi / timers / timers.c
1 #include "mpi.h"
2 #include <stdio.h>
3 #include <stdlib.h>
4
5 #include <unistd.h>
6 #include <sys/time.h>
7 #if _POSIX_TIMERS > 0
8 #include <time.h>
9 #endif
10
11 // Test if we correctly intercept gettimeofday and clock_gettime, and sleeps
12
13 int main(int argc, char* argv[])
14 {
15   MPI_Init(&argc, &argv);
16
17   // gettimeofday - First test that two consecutive calls will return the same
18   // time, as computing power is set to -1
19
20   struct timeval tv1;
21   struct timeval tv2;
22   gettimeofday(&tv1, NULL);
23   gettimeofday(&tv2, NULL);
24   if ((tv1.tv_sec != tv2.tv_sec) || (tv1.tv_usec != tv2.tv_usec))
25     printf("Error, two consecutive calls to gettimeofday did not return same time (with cpu_threshold set to 0)\n");
26
27   // sleep one 1 second
28   gettimeofday(&tv1, NULL);
29   sleep(1);
30   gettimeofday(&tv2, NULL);
31   long res = ((tv2.tv_sec * 1000000 + tv2.tv_usec)) - ((tv1.tv_sec * 1000000 + tv1.tv_usec));
32   if (res < 999998 || res > 1000002)
33     printf("Error, sleep(1) did not exactly slept 1s\n");
34
35   // usleep 100 us
36   gettimeofday(&tv1, NULL);
37   usleep(100);
38   gettimeofday(&tv2, NULL);
39   res = ((tv2.tv_sec * 1000000 + tv2.tv_usec)) - ((tv1.tv_sec * 1000000 + tv1.tv_usec));
40   if (res < 98 || res > 102)
41     printf("Error, usleep did not really sleep 100us, but %ld\n", res);
42
43
44   // clock_gettime, only if available
45 #if _POSIX_TIMERS > 0
46   struct timespec tp1;
47   struct timespec tp2;
48   struct timespec tpsleep;
49   clock_gettime(CLOCK_REALTIME, &tp1);
50   clock_gettime(CLOCK_REALTIME, &tp2);
51   if ((tp1.tv_sec != tp2.tv_sec) || (tp1.tv_nsec != tp2.tv_nsec))
52     printf("Error, two consecutive calls to gettimeofday did not return same time (with running power to 0)\n");
53
54   // nanosleep for 100ns
55   clock_gettime(CLOCK_REALTIME, &tp1);
56   tpsleep.tv_sec  = 0;
57   tpsleep.tv_nsec = 100;
58   nanosleep(&tpsleep, NULL);
59   clock_gettime(CLOCK_REALTIME, &tp2);
60   res = ((tp2.tv_sec * 1000000000 + tp2.tv_nsec)) - ((tp1.tv_sec * 1000000000 + tp1.tv_nsec));
61   if (res < 98 || res > 102)
62     printf("Error, nanosleep did not really sleep 100ns, but %ld\n", res);
63 #endif
64
65   MPI_Finalize();
66   return 0;
67 }