Logo AND Algorithmique Numérique Distribuée

Public GIT Repository
Add new entry in Release_Notes.
[simgrid.git] / teshsuite / smpi / timers / timers.c
1 /* Copyright (c) 2016-2023. The SimGrid Team. All rights reserved.          */
2
3 /* This program is free software; you can redistribute it and/or modify it
4  * under the terms of the license (GNU LGPL) which comes with this package. */
5
6 #include "mpi.h"
7 #include <stdio.h>
8 #include <stdlib.h>
9
10 #include <unistd.h>
11 #include <sys/time.h>
12 #if _POSIX_TIMERS > 0
13 #include <time.h>
14 #endif
15
16 // Test if we correctly intercept gettimeofday and clock_gettime, and sleeps
17
18 int main(int argc, char* argv[])
19 {
20   MPI_Init(&argc, &argv);
21
22   // gettimeofday - First test that two consecutive calls will return the same
23   // time, as computing power is set to -1
24
25   struct timeval tv1;
26   struct timeval tv2;
27   gettimeofday(&tv1, NULL);
28   gettimeofday(&tv2, NULL);
29   if ((tv1.tv_sec != tv2.tv_sec) || (tv1.tv_usec != tv2.tv_usec))
30     printf("Error, two consecutive calls to gettimeofday did not return same time (with cpu_threshold set to 0)\n");
31
32   // sleep one 1 second
33   gettimeofday(&tv1, NULL);
34   sleep(1);
35   gettimeofday(&tv2, NULL);
36   long res = (tv2.tv_sec * 1000000 + tv2.tv_usec) - (tv1.tv_sec * 1000000 + tv1.tv_usec);
37   if (res < 999998 || res > 1000002)
38     printf("Error, sleep(1) did not exactly slept 1s\n");
39
40   // usleep 100 us
41   gettimeofday(&tv1, NULL);
42   usleep(100);
43   gettimeofday(&tv2, NULL);
44   res = (tv2.tv_sec * 1000000 + tv2.tv_usec) - (tv1.tv_sec * 1000000 + tv1.tv_usec);
45   if (res < 98 || res > 102)
46     printf("Error, usleep did not really sleep 100us, but %ld\n", res);
47
48
49   // clock_gettime, only if available
50 #if _POSIX_TIMERS > 0
51   struct timespec tp1;
52   struct timespec tp2;
53   struct timespec tpsleep;
54   clock_gettime(CLOCK_REALTIME, &tp1);
55   clock_gettime(CLOCK_REALTIME, &tp2);
56   if (tp1.tv_sec != tp2.tv_sec || tp1.tv_nsec != tp2.tv_nsec)
57     printf("Error, two consecutive calls to clock_gettime did not return same time (with running power to 0)\n");
58
59   // nanosleep for 100ns
60   clock_gettime(CLOCK_REALTIME, &tp1);
61   tpsleep.tv_sec  = 0;
62   tpsleep.tv_nsec = 100;
63   nanosleep(&tpsleep, NULL);
64   clock_gettime(CLOCK_REALTIME, &tp2);
65   res = (tp2.tv_sec * 1000000000 + tp2.tv_nsec) - (tp1.tv_sec * 1000000000 + tp1.tv_nsec);
66   if (res < 98 || res > 102)
67     printf("Error, nanosleep did not really sleep 100ns, but %ld\n", res);
68 #endif
69
70   MPI_Finalize();
71   return 0;
72 }