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 56char *TCID = "getrusage04"; 57int 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 66static long BIAS_MAX; 67 68static int opt_factor; 69static char *factor_str; 70static long factor_nr = 1; 71 72option_t child_options[] = { 73 {"m:", &opt_factor, &factor_str}, 74 {NULL, NULL, NULL} 75}; 76 77static void fusage(void); 78static void busyloop(long wait); 79static void setup(void); 80static void cleanup(void); 81 82int 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:%12lldus; stime:%12lldus", 108 (long long)usage.ru_utime.tv_usec, 109 (long long)usage.ru_stime.tv_usec); 110 ulast = usage.ru_utime.tv_usec; 111 slast = usage.ru_stime.tv_usec; 112 113 while (i < RECORD_MAX) { 114 SAFE_GETRUSAGE(cleanup, RUSAGE_THREAD, &usage); 115 udelta = usage.ru_utime.tv_usec - ulast; 116 sdelta = usage.ru_stime.tv_usec - slast; 117 if (udelta > 0 || sdelta > 0) { 118 i++; 119 tst_resm(TINFO, "utime:%12lldus; stime:%12lldus", 120 (long long)usage.ru_utime.tv_usec, 121 (long long)usage.ru_stime.tv_usec); 122 if ((long)udelta > 1000 + (BIAS_MAX * factor_nr)) { 123 sprintf(msg_string, 124 "utime increased > %ldus:", 125 1000 + BIAS_MAX * factor_nr); 126 tst_brkm(TFAIL, cleanup, msg_string, 127 " delta = %luus", udelta); 128 } 129 if ((long)sdelta > 1000 + (BIAS_MAX * factor_nr)) { 130 sprintf(msg_string, 131 "stime increased > %ldus:", 132 1000 + BIAS_MAX * factor_nr); 133 tst_brkm(TFAIL, cleanup, msg_string, 134 " delta = %luus", sdelta); 135 } 136 } 137 ulast = usage.ru_utime.tv_usec; 138 slast = usage.ru_stime.tv_usec; 139 busyloop(100000); 140 } 141 } 142 143 tst_resm(TPASS, "Test Passed"); 144 145 cleanup(); 146 tst_exit(); 147} 148 149static void fusage(void) 150{ 151 printf(" -m n use n as multiply factor for max [us]time " 152 "increment (1000+(1000*n)us),\n default value is 1\n"); 153} 154 155static void busyloop(long wait) 156{ 157 while (wait--) ; 158} 159 160/* 161 * The resolution of getrusage timers currently depends on CONFIG_HZ settings, 162 * as they are measured in jiffies. 163 * 164 * The problem is that there is no reasonable API to get either getrusage 165 * timers resolution or duration of jiffie. 166 * 167 * Here we use clock_getres() with linux specific CLOCK_REALTIME_COARSE (added 168 * in 2.6.32) which is also based on jiffies. This timer has the same 169 * granularity as getrusage but it's not guaranteed and it may change in the 170 * future. 171 * 172 * The default value for resolution was choosen to be 4ms as it corresponds to 173 * CONFIG_HZ=250 which seems to be default value. 174 */ 175static unsigned long guess_timer_resolution(void) 176{ 177 struct timespec res; 178 179 if (clock_getres(CLOCK_REALTIME_COARSE, &res)) { 180 tst_resm(TINFO, 181 "CLOCK_REALTIME_COARSE not supported, using 4000 us"); 182 return 4000; 183 } 184 185 if (res.tv_nsec < 1000000 || res.tv_nsec > 10000000) { 186 tst_resm(TINFO, "Unexpected CLOCK_REALTIME_COARSE resolution," 187 " using 4000 us"); 188 return 4000; 189 } 190 191 tst_resm(TINFO, "Expected timers granularity is %li us", 192 res.tv_nsec / 1000); 193 194 return res.tv_nsec / 1000; 195} 196 197static void setup(void) 198{ 199 tst_sig(NOFORK, DEF_HANDLER, cleanup); 200 201 if (tst_is_virt(VIRT_XEN) || tst_is_virt(VIRT_KVM) || tst_is_virt(VIRT_HYPERV)) 202 tst_brkm(TCONF, NULL, "This testcase is not supported on this" 203 " virtual machine."); 204 205 BIAS_MAX = guess_timer_resolution(); 206 207 TEST_PAUSE; 208} 209 210static void cleanup(void) 211{ 212} 213