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