blob: 6c76dbc7c1651827237cc83efab2941103db87af [file] [log] [blame]
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00001/*
2 * Copyright (c) 2015 Eugene Syromyatnikov <evgsyr@gmail.com>
Dmitry V. Levin6a641fc2016-01-04 23:44:20 +00003 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
Dmitry V. Levin38a34c92015-12-17 17:56:48 +00004 * 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 Syromyatnikov7a036052015-08-04 15:52:55 +030029/**
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. Levin0c8853c2016-01-02 13:28:43 +000035#include "tests.h"
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030036#include <sched.h>
37#include <stdio.h>
38#include <time.h>
39#include <unistd.h>
40
Dmitry V. Levin4bbed302015-12-06 05:28:11 +000041#include <sys/syscall.h>
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030042#include <sys/times.h>
43#include <sys/wait.h>
44
45enum {
46 NUM_USER_ITERS = 1000000,
Dmitry V. Levinfc0a22d2015-12-06 04:51:57 +000047 PARENT_CPUTIME_LIMIT_NSEC = 200000000,
48 CHILD_CPUTIME_LIMIT_NSEC = 300000000
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030049};
50
51int
52main (void)
53{
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030054 struct timespec ts;
Dmitry V. Levinb6edea52016-01-12 04:27:22 +000055 volatile int dummy = 0;
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030056 int i;
57
Dmitry V. Levinfc0a22d2015-12-06 04:51:57 +000058 pid_t pid = fork();
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030059 if (pid < 0)
Dmitry V. Levin6a641fc2016-01-04 23:44:20 +000060 perror_msg_and_fail("fork");
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030061
Dmitry V. Levinfc0a22d2015-12-06 04:51:57 +000062 const long cputime_limit =
63 pid ? PARENT_CPUTIME_LIMIT_NSEC : CHILD_CPUTIME_LIMIT_NSEC;
64
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030065 /* Enjoying my user time */
66 while (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
Dmitry V. Levinfc0a22d2015-12-06 04:51:57 +000067 if (ts.tv_sec || ts.tv_nsec >= cputime_limit)
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030068 break;
69
Dmitry V. Levinfc0a22d2015-12-06 04:51:57 +000070 for (i = 0; i < NUM_USER_ITERS; ++i)
71 ++dummy;
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030072 }
73
74 /* Enjoying my system time */
75 while (clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &ts) == 0) {
Dmitry V. Levinfc0a22d2015-12-06 04:51:57 +000076 if (ts.tv_sec || ts.tv_nsec >= cputime_limit * 2)
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +030077 break;
78
79 sched_yield();
80 }
81
82 if (pid == 0) {
83 return 0;
84 } else {
85 wait(NULL);
86 }
87
Dmitry V. Levinfc0a22d2015-12-06 04:51:57 +000088 struct tms tbuf;
Dmitry V. Levin4bbed302015-12-06 05:28:11 +000089 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. Levinb6b38fa2015-12-15 15:26:29 +000096#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. Levin6a641fc2016-01-04 23:44:20 +0000107 perror_msg_and_skip("times");
Dmitry V. Levinb6b38fa2015-12-15 15:26:29 +0000108 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. Levinfc0a22d2015-12-06 04:51:57 +0000119 clock_t res = times(&tbuf);
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +0300120
Dmitry V. Levin4bbed302015-12-06 05:28:11 +0000121 if ((clock_t) -1 == res)
Dmitry V. Levin6a641fc2016-01-04 23:44:20 +0000122 perror_msg_and_skip("times");
Dmitry V. Levin4bbed302015-12-06 05:28:11 +0000123 if (sizeof(res) < sizeof(unsigned long long))
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +0300124 llres = (unsigned long) res;
125 else
126 llres = res;
Dmitry V. Levin4bbed302015-12-06 05:28:11 +0000127#endif
Eugene Syromyatnikov7a036052015-08-04 15:52:55 +0300128
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}