1 /* Copyright (c) 2016-2022. The SimGrid Team. All rights reserved. */
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. */
16 // Test if we correctly intercept gettimeofday and clock_gettime, and sleeps
18 int main(int argc, char* argv[])
20 MPI_Init(&argc, &argv);
22 // gettimeofday - First test that two consecutive calls will return the same
23 // time, as computing power is set to -1
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");
33 gettimeofday(&tv1, NULL);
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");
41 gettimeofday(&tv1, NULL);
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);
49 // clock_gettime, only if available
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");
59 // nanosleep for 100ns
60 clock_gettime(CLOCK_REALTIME, &tp1);
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);