1 /*
2 * getrusage04 - accuracy of getrusage() with RUSAGE_THREAD
3 *
4 * This program is used for testing the following upstream commit:
5 * 761b1d26df542fd5eb348837351e4d2f3bc7bffe.
6 *
7 * getrusage() returns cpu resource usage with accuracy of 10ms
8 * when RUSAGE_THREAD is specified to the argument who. Meanwhile,
9 * accuracy is 1ms when RUSAGE_SELF is specified. This bad accuracy
10 * of getrusage() caused a big impact on some application which is
11 * critical to accuracy of cpu usage. The upstream fix removed
12 * casts to clock_t in task_u/stime(), to keep granularity of
13 * cputime_t over the calculation.
14 *
15 * Note that the accuracy on powerpc and s390x systems is always
16 * 10ms when either RUSAGE_THREAD or RUSAGE_SELF specified, so
17 * this test won't be executed on those platforms.
18 *
19 * Copyright (C) 2011 Red Hat, Inc.
20 * Copyright (C) 2013 Cyril Hrubis <chrubis@suse.cz>
21 *
22 * This program is free software; you can redistribute it and/or
23 * modify it under the terms of version 2 of the GNU General Public
24 * License as published by the Free Software Foundation.
25 *
26 * This program is distributed in the hope that it would be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
29 *
30 * Further, this software is distributed without any warranty that it
31 * is free of the rightful claim of any third person regarding
32 * infringement or the like. Any license provided herein, whether
33 * implied or otherwise, applies only to this software file. Patent
34 * licenses, if any, provided herein do not apply to combinations of
35 * this program with other software, or any other product whatsoever.
36 *
37 * You should have received a copy of the GNU General Public License
38 * along with this program; if not, write the Free Software
39 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
40 * 02110-1301, USA.
41 */
42
43 #define _GNU_SOURCE
44 #include <sys/types.h>
45 #include <sys/resource.h>
46 #include <sys/time.h>
47 #include <errno.h>
48 #include <stdio.h>
49 #include <stdlib.h>
50 #include <time.h>
51
52 #include "test.h"
53 #include "safe_macros.h"
54 #include "lapi/posix_clocks.h"
55
56 char *TCID = "getrusage04";
57 int TST_TOTAL = 1;
58
59 #define RECORD_MAX 20
60 #define FACTOR_MAX 10
61
62 #ifndef RUSAGE_THREAD
63 #define RUSAGE_THREAD 1
64 #endif
65
66 static long BIAS_MAX;
67
68 static int opt_factor;
69 static char *factor_str;
70 static long factor_nr = 1;
71
72 option_t child_options[] = {
73 {"m:", &opt_factor, &factor_str},
74 {NULL, NULL, NULL}
75 };
76
77 static void fusage(void);
78 static void busyloop(long wait);
79 static void setup(void);
80 static void cleanup(void);
81
main(int argc,char * argv[])82 int main(int argc, char *argv[])
83 {
84 struct rusage usage;
85 unsigned long ulast, udelta, slast, sdelta;
86 int i, lc;
87 char msg_string[BUFSIZ];
88
89 tst_parse_opts(argc, argv, child_options, fusage);
90
91 #if (__powerpc__) || (__powerpc64__) || (__s390__) || (__s390x__)
92 tst_brkm(TCONF, NULL, "This test is not designed for current system");
93 #endif
94
95 setup();
96
97 if (opt_factor)
98 factor_nr = SAFE_STRTOL(cleanup, factor_str, 0, FACTOR_MAX);
99
100 tst_resm(TINFO, "Using %ld as multiply factor for max [us]time "
101 "increment (1000+%ldus)!", factor_nr, BIAS_MAX * factor_nr);
102
103 for (lc = 0; TEST_LOOPING(lc); lc++) {
104 tst_count = 0;
105 i = 0;
106 SAFE_GETRUSAGE(cleanup, RUSAGE_THREAD, &usage);
107 tst_resm(TINFO, "utime:%12luus; stime:%12luus",
108 usage.ru_utime.tv_usec, usage.ru_stime.tv_usec);
109 ulast = usage.ru_utime.tv_usec;
110 slast = usage.ru_stime.tv_usec;
111
112 while (i < RECORD_MAX) {
113 SAFE_GETRUSAGE(cleanup, RUSAGE_THREAD, &usage);
114 udelta = usage.ru_utime.tv_usec - ulast;
115 sdelta = usage.ru_stime.tv_usec - slast;
116 if (udelta > 0 || sdelta > 0) {
117 i++;
118 tst_resm(TINFO, "utime:%12luus; stime:%12luus",
119 usage.ru_utime.tv_usec,
120 usage.ru_stime.tv_usec);
121 if (udelta > 1000 + (BIAS_MAX * factor_nr)) {
122 sprintf(msg_string,
123 "utime increased > %ldus:",
124 1000 + BIAS_MAX * factor_nr);
125 tst_brkm(TFAIL, cleanup, msg_string,
126 " delta = %luus", udelta);
127 }
128 if (sdelta > 1000 + (BIAS_MAX * factor_nr)) {
129 sprintf(msg_string,
130 "stime increased > %ldus:",
131 1000 + BIAS_MAX * factor_nr);
132 tst_brkm(TFAIL, cleanup, msg_string,
133 " delta = %luus", sdelta);
134 }
135 }
136 ulast = usage.ru_utime.tv_usec;
137 slast = usage.ru_stime.tv_usec;
138 busyloop(100000);
139 }
140 }
141
142 tst_resm(TPASS, "Test Passed");
143
144 cleanup();
145 tst_exit();
146 }
147
fusage(void)148 static void fusage(void)
149 {
150 printf(" -m n use n as multiply factor for max [us]time "
151 "increment (1000+(1000*n)us),\n default value is 1\n");
152 }
153
busyloop(long wait)154 static void busyloop(long wait)
155 {
156 while (wait--) ;
157 }
158
159 /*
160 * The resolution of getrusage timers currently depends on CONFIG_HZ settings,
161 * as they are measured in jiffies.
162 *
163 * The problem is that there is no reasonable API to get either getrusage
164 * timers resolution or duration of jiffie.
165 *
166 * Here we use clock_getres() with linux specific CLOCK_REALTIME_COARSE (added
167 * in 2.6.32) which is also based on jiffies. This timer has the same
168 * granularity as getrusage but it's not guaranteed and it may change in the
169 * future.
170 *
171 * The default value for resolution was choosen to be 4ms as it corresponds to
172 * CONFIG_HZ=250 which seems to be default value.
173 */
guess_timer_resolution(void)174 static unsigned long guess_timer_resolution(void)
175 {
176 struct timespec res;
177
178 if (clock_getres(CLOCK_REALTIME_COARSE, &res)) {
179 tst_resm(TINFO,
180 "CLOCK_REALTIME_COARSE not supported, using 4000 us");
181 return 4000;
182 }
183
184 if (res.tv_nsec < 1000000 || res.tv_nsec > 10000000) {
185 tst_resm(TINFO, "Unexpected CLOCK_REALTIME_COARSE resolution,"
186 " using 4000 us");
187 return 4000;
188 }
189
190 tst_resm(TINFO, "Expected timers granularity is %li us",
191 res.tv_nsec / 1000);
192
193 return res.tv_nsec / 1000;
194 }
195
setup(void)196 static void setup(void)
197 {
198 tst_sig(NOFORK, DEF_HANDLER, cleanup);
199
200 if (tst_is_virt(VIRT_XEN) || tst_is_virt(VIRT_KVM))
201 tst_brkm(TCONF, NULL, "This testcase is not supported on this"
202 " virtual machine.");
203
204 BIAS_MAX = guess_timer_resolution();
205
206 TEST_PAUSE;
207 }
208
cleanup(void)209 static void cleanup(void)
210 {
211 }
212