Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Eugene Syromyatnikov <evgsyr@gmail.com> |
Dmitry V. Levin | 6a641fc | 2016-01-04 23:44:20 +0000 | [diff] [blame] | 3 | * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org> |
Dmitry V. Levin | 38a34c9 | 2015-12-17 17:56:48 +0000 | [diff] [blame] | 4 | * All rights reserved. |
| 5 | * |
| 6 | * Redistribution and use in source and binary forms, with or without |
| 7 | * modification, are permitted provided that the following conditions |
| 8 | * are met: |
| 9 | * 1. Redistributions of source code must retain the above copyright |
| 10 | * notice, this list of conditions and the following disclaimer. |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in the |
| 13 | * documentation and/or other materials provided with the distribution. |
| 14 | * 3. The name of the author may not be used to endorse or promote products |
| 15 | * derived from this software without specific prior written permission. |
| 16 | * |
| 17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
| 18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
| 19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
| 20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
| 21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
| 26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | */ |
| 28 | |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 29 | /** |
| 30 | * @file |
| 31 | * This test burns some CPU cycles in user space and kernel space in order to |
| 32 | * get some non-zero values returned by times(2). |
| 33 | */ |
| 34 | |
Dmitry V. Levin | 0c8853c | 2016-01-02 13:28:43 +0000 | [diff] [blame] | 35 | #include "tests.h" |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 36 | #include <sched.h> |
| 37 | #include <stdio.h> |
| 38 | #include <time.h> |
| 39 | #include <unistd.h> |
| 40 | |
Dmitry V. Levin | 4bbed30 | 2015-12-06 05:28:11 +0000 | [diff] [blame] | 41 | #include <sys/syscall.h> |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 42 | #include <sys/times.h> |
| 43 | #include <sys/wait.h> |
| 44 | |
| 45 | enum { |
| 46 | NUM_USER_ITERS = 1000000, |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 47 | PARENT_CPUTIME_LIMIT_NSEC = 200000000, |
| 48 | CHILD_CPUTIME_LIMIT_NSEC = 300000000 |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 49 | }; |
| 50 | |
| 51 | int |
| 52 | main (void) |
| 53 | { |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 54 | struct timespec ts; |
Dmitry V. Levin | b6edea5 | 2016-01-12 04:27:22 +0000 | [diff] [blame] | 55 | volatile int dummy = 0; |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 56 | int i; |
| 57 | |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 58 | pid_t pid = fork(); |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 59 | if (pid < 0) |
Dmitry V. Levin | 6a641fc | 2016-01-04 23:44:20 +0000 | [diff] [blame] | 60 | perror_msg_and_fail("fork"); |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 61 | |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 62 | const long cputime_limit = |
| 63 | pid ? PARENT_CPUTIME_LIMIT_NSEC : CHILD_CPUTIME_LIMIT_NSEC; |
| 64 | |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 65 | /* Enjoying my user time */ |
| 66 | while (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) { |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 67 | if (ts.tv_sec || ts.tv_nsec >= cputime_limit) |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 68 | break; |
| 69 | |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 70 | for (i = 0; i < NUM_USER_ITERS; ++i) |
| 71 | ++dummy; |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 72 | } |
| 73 | |
| 74 | /* Enjoying my system time */ |
| 75 | while (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) { |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 76 | if (ts.tv_sec || ts.tv_nsec >= cputime_limit * 2) |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 77 | break; |
| 78 | |
| 79 | sched_yield(); |
| 80 | } |
| 81 | |
| 82 | if (pid == 0) { |
| 83 | return 0; |
| 84 | } else { |
| 85 | wait(NULL); |
| 86 | } |
| 87 | |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 88 | struct tms tbuf; |
Dmitry V. Levin | 4bbed30 | 2015-12-06 05:28:11 +0000 | [diff] [blame] | 89 | unsigned long long llres; |
| 90 | |
| 91 | /* |
| 92 | * On systems where user's and kernel's long types are the same, |
| 93 | * prefer direct times syscall over libc's times function because |
| 94 | * the latter is more prone to return value truncation. |
| 95 | */ |
Dmitry V. Levin | b6b38fa | 2015-12-15 15:26:29 +0000 | [diff] [blame] | 96 | #undef USE_LIBC_SYSCALL |
| 97 | #if defined __NR_times && \ |
| 98 | !defined(LINUX_MIPSN32) && \ |
| 99 | !(defined __x86_64__ && defined __ILP32__) |
| 100 | # define USE_LIBC_SYSCALL 1 |
| 101 | #endif |
| 102 | |
| 103 | #if defined USE_LIBC_SYSCALL |
| 104 | long res = syscall(__NR_times, &tbuf); |
| 105 | |
| 106 | if (-1L == res) |
Dmitry V. Levin | 6a641fc | 2016-01-04 23:44:20 +0000 | [diff] [blame] | 107 | perror_msg_and_skip("times"); |
Dmitry V. Levin | b6b38fa | 2015-12-15 15:26:29 +0000 | [diff] [blame] | 108 | else |
| 109 | llres = (unsigned long) res; |
| 110 | #elif defined __NR_times && defined __x86_64__ && defined __ILP32__ |
| 111 | register long arg asm("rdi") = (long) &tbuf; |
| 112 | asm volatile("syscall\n\t" |
| 113 | : "=a"(llres) |
| 114 | : "0"(__NR_times), "r"(arg) |
| 115 | : "memory", "cc", "r11", "cx"); |
| 116 | if (llres > 0xfffffffffffff000) |
| 117 | return 77; |
| 118 | #else |
Dmitry V. Levin | fc0a22d | 2015-12-06 04:51:57 +0000 | [diff] [blame] | 119 | clock_t res = times(&tbuf); |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 120 | |
Dmitry V. Levin | 4bbed30 | 2015-12-06 05:28:11 +0000 | [diff] [blame] | 121 | if ((clock_t) -1 == res) |
Dmitry V. Levin | 6a641fc | 2016-01-04 23:44:20 +0000 | [diff] [blame] | 122 | perror_msg_and_skip("times"); |
Dmitry V. Levin | 4bbed30 | 2015-12-06 05:28:11 +0000 | [diff] [blame] | 123 | if (sizeof(res) < sizeof(unsigned long long)) |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 124 | llres = (unsigned long) res; |
| 125 | else |
| 126 | llres = res; |
Dmitry V. Levin | 4bbed30 | 2015-12-06 05:28:11 +0000 | [diff] [blame] | 127 | #endif |
Eugene Syromyatnikov | 7a03605 | 2015-08-04 15:52:55 +0300 | [diff] [blame] | 128 | |
| 129 | printf("times({tms_utime=%llu, tms_stime=%llu, ", |
| 130 | (unsigned long long) tbuf.tms_utime, |
| 131 | (unsigned long long) tbuf.tms_stime); |
| 132 | printf("tms_cutime=%llu, tms_cstime=%llu}) = %llu\n", |
| 133 | (unsigned long long) tbuf.tms_cutime, |
| 134 | (unsigned long long) tbuf.tms_cstime, |
| 135 | llres); |
| 136 | puts("+++ exited with 0 +++"); |
| 137 | |
| 138 | return 0; |
| 139 | } |