Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add a test for SMPI versions of gettimeofday, clock_gettime and nano/u/sleep
[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
12 //Test if we correctly intercept gettimeofday and clock_gettime, and sleeps
13
14 int main( int argc, char *argv[] )
15 {
16     MPI_Init( &argc, &argv );
17
18     //gettimeofday - First test that two consecutive calls will return the same
19     //time, as computing power is set to -1
20
21     struct timeval tv1, 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, tp2, tpsleep;
47     clock_gettime (CLOCK_REALTIME, &tp1);
48     clock_gettime (CLOCK_REALTIME, &tp2);
49     if ((tp1.tv_sec != tp2.tv_sec) || (tp1.tv_nsec != tp2.tv_nsec))
50       printf("Error, two consecutive calls to gettimeofday did not return same time (with running power to 0)\n");
51
52     //nanosleep for 100ns
53     clock_gettime (CLOCK_REALTIME, &tp1);
54     tpsleep.tv_sec=0;
55     tpsleep.tv_nsec=100;
56     nanosleep(&tpsleep, NULL);
57     clock_gettime (CLOCK_REALTIME, &tp2);
58     res = ((tp2.tv_sec * 1000000000 + tp2.tv_nsec)) - ((tp1.tv_sec * 1000000000 + tp1.tv_nsec));
59     if (res <98 || res > 102)
60       printf("Error, nanosleep did not really sleep 100ns, but %ld\n", res);
61
62 #endif
63
64     MPI_Finalize();
65     return 0;
66 }