Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 1 | /** |
| 2 | * @file |
| 3 | * This test burns some CPU cycles in user space and kernel space in order to |
| 4 | * get some non-zero values returned by times(2). |
| 5 | */ |
| 6 | |
| 7 | #ifdef HAVE_CONFIG_H |
| 8 | # include "config.h" |
| 9 | #endif |
| 10 | |
| 11 | #include <sched.h> |
| 12 | #include <stdio.h> |
| 13 | #include <time.h> |
| 14 | #include <unistd.h> |
| 15 | |
Dmitry V. Levin | 4bbed30 | 2015-12-06 05:28:11 +0000 | [diff] [blame^] | 16 | #include <sys/syscall.h> |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 17 | #include <sys/times.h> |
| 18 | #include <sys/wait.h> |
| 19 | |
| 20 | enum { |
| 21 | NUM_USER_ITERS = 1000000, |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 22 | PARENT_CPUTIME_LIMIT_NSEC = 200000000, |
| 23 | CHILD_CPUTIME_LIMIT_NSEC = 300000000 |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 24 | }; |
| 25 | |
| 26 | int |
| 27 | main (void) |
| 28 | { |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 29 | struct timespec ts; |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 30 | volatile int dummy; |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 31 | int i; |
| 32 | |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 33 | pid_t pid = fork(); |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 34 | |
| 35 | if (pid < 0) |
| 36 | return 77; |
| 37 | |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 38 | const long cputime_limit = |
| 39 | pid ? PARENT_CPUTIME_LIMIT_NSEC : CHILD_CPUTIME_LIMIT_NSEC; |
| 40 | |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 41 | /* Enjoying my user time */ |
| 42 | while (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) { |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 43 | if (ts.tv_sec || ts.tv_nsec >= cputime_limit) |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 44 | break; |
| 45 | |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 46 | for (i = 0; i < NUM_USER_ITERS; ++i) |
| 47 | ++dummy; |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | /* Enjoying my system time */ |
| 51 | while (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) { |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 52 | if (ts.tv_sec || ts.tv_nsec >= cputime_limit * 2) |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 53 | break; |
| 54 | |
| 55 | sched_yield(); |
| 56 | } |
| 57 | |
| 58 | if (pid == 0) { |
| 59 | return 0; |
| 60 | } else { |
| 61 | wait(NULL); |
| 62 | } |
| 63 | |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 64 | struct tms tbuf; |
Dmitry V. Levin | 4bbed30 | 2015-12-06 05:28:11 +0000 | [diff] [blame^] | 65 | unsigned long long llres; |
| 66 | |
| 67 | /* |
| 68 | * On systems where user's and kernel's long types are the same, |
| 69 | * prefer direct times syscall over libc's times function because |
| 70 | * the latter is more prone to return value truncation. |
| 71 | */ |
| 72 | #if !defined __NR_times \ |
| 73 | || defined LINUX_MIPSN32 \ |
| 74 | || defined __x86_64__ && defined __ILP32__ |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 75 | clock_t res = times(&tbuf); |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 76 | |
Dmitry V. Levin | 4bbed30 | 2015-12-06 05:28:11 +0000 | [diff] [blame^] | 77 | if ((clock_t) -1 == res) |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 78 | return 77; |
Dmitry V. Levin | 4bbed30 | 2015-12-06 05:28:11 +0000 | [diff] [blame^] | 79 | if (sizeof(res) < sizeof(unsigned long long)) |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 80 | llres = (unsigned long) res; |
| 81 | else |
| 82 | llres = res; |
Dmitry V. Levin | 4bbed30 | 2015-12-06 05:28:11 +0000 | [diff] [blame^] | 83 | #else |
| 84 | long res = syscall(__NR_times, &tbuf); |
| 85 | |
| 86 | if (-1L == res) |
| 87 | return 77; |
| 88 | else |
| 89 | llres = (unsigned long) res; |
| 90 | #endif |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 91 | |
| 92 | printf("times({tms_utime=%llu, tms_stime=%llu, ", |
| 93 | (unsigned long long) tbuf.tms_utime, |
| 94 | (unsigned long long) tbuf.tms_stime); |
| 95 | printf("tms_cutime=%llu, tms_cstime=%llu}) = %llu\n", |
| 96 | (unsigned long long) tbuf.tms_cutime, |
| 97 | (unsigned long long) tbuf.tms_cstime, |
| 98 | llres); |
| 99 | puts("+++ exited with 0 +++"); |
| 100 | |
| 101 | return 0; |
| 102 | } |