plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * |
| 3 | * Copyright (c) International Business Machines Corp., 2001 |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See |
| 13 | * the GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
Wanlong Gao | 4548c6c | 2012-10-19 18:03:36 +0800 | [diff] [blame] | 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
| 20 | /* |
| 21 | * NAME |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 22 | * times03.c |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 23 | * |
| 24 | * DESCRIPTION |
| 25 | * Testcase to check the basic functionality of the times() system call. |
| 26 | * |
| 27 | * ALGORITHM |
| 28 | * This testcase checks the values that times(2) system call returns. |
| 29 | * Start a process, and spend some CPU time by performing a spin in |
| 30 | * a for-loop. Then use the times() system call, to determine the |
| 31 | * cpu time/sleep time, and other statistics. |
| 32 | * |
| 33 | * USAGE: <for command-line> |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 34 | * times03 [-c n] [-f] [-P x] [-t] |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 35 | * where, -c n : Run n copies concurrently. |
| 36 | * -f : Turn off functionality Testing. |
| 37 | * -P x : Pause for x seconds between iterations. |
| 38 | * -t : Turn on syscall timing. |
| 39 | * |
| 40 | * History |
| 41 | * 07/2001 John George |
| 42 | * -Ported |
| 43 | * |
| 44 | * Restrictions |
| 45 | * NONE |
| 46 | */ |
| 47 | |
| 48 | #include <sys/types.h> |
| 49 | #include <sys/times.h> |
| 50 | #include <errno.h> |
Steven Jackson | 249f405 | 2016-12-13 16:16:00 +0000 | [diff] [blame^] | 51 | #include <sys/wait.h> |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 52 | #include <time.h> |
Garrett Cooper | e8530df | 2010-12-21 11:37:57 -0800 | [diff] [blame] | 53 | #include "test.h" |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 54 | #include <signal.h> |
subrata_modak | 923b23f | 2009-11-02 13:57:16 +0000 | [diff] [blame] | 55 | #include <stdint.h> |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 56 | |
nstraz | fa31d55 | 2002-05-14 16:50:06 +0000 | [diff] [blame] | 57 | char *TCID = "times03"; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 58 | int TST_TOTAL = 1; |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 59 | |
subrata_modak | 0c2e2e2 | 2008-01-28 11:33:15 +0000 | [diff] [blame] | 60 | volatile int timeout; /* Did we timeout in alarm() ? */ |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 61 | |
| 62 | void work(void); |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 63 | void sighandler(int signal, siginfo_t * info, void *uc); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 64 | |
| 65 | void setup(void); |
| 66 | void cleanup(void); |
| 67 | |
plars | 74948ad | 2002-11-14 16:16:14 +0000 | [diff] [blame] | 68 | int main(int argc, char **argv) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 69 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 70 | struct tms buf1, buf2; |
| 71 | time_t start_time, end_time; |
Garrett Cooper | f1d2a63 | 2010-12-19 08:23:06 -0800 | [diff] [blame] | 72 | int pid2, status; |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 73 | struct sigaction sa; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 74 | |
Cyril Hrubis | d6d11d0 | 2015-03-09 17:35:43 +0100 | [diff] [blame] | 75 | tst_parse_opts(argc, argv, NULL, NULL); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 76 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 77 | setup(); |
| 78 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 79 | /* |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 80 | * We spend time in userspace using the following mechanism : |
| 81 | * Setup an alarm() for 3 secs and do some simple loop operations |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 82 | * until we get the signal. This makes the test independent of |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 83 | * processor speed. |
| 84 | */ |
| 85 | sa.sa_sigaction = sighandler; |
| 86 | sigemptyset(&sa.sa_mask); |
| 87 | sa.sa_flags = SA_SIGINFO; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 88 | |
Garrett Cooper | f1d2a63 | 2010-12-19 08:23:06 -0800 | [diff] [blame] | 89 | if (sigaction(SIGALRM, &sa, NULL) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 90 | tst_brkm(TBROK | TERRNO, cleanup, "Sigaction failed !\n"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 91 | |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 92 | timeout = 0; |
| 93 | alarm(3); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 94 | |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 95 | work(); |
| 96 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 97 | /* |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 98 | * At least some CPU time must be used in system space. This is |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 99 | * achieved by executing the times(2) call for |
| 100 | * atleast 5 secs. This logic makes it independant |
| 101 | * of the processor speed. |
| 102 | */ |
| 103 | start_time = time(NULL); |
| 104 | for (;;) { |
Garrett Cooper | f1d2a63 | 2010-12-19 08:23:06 -0800 | [diff] [blame] | 105 | if (times(&buf1) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 106 | tst_resm(TFAIL | TERRNO, "times failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 107 | end_time = time(NULL); |
| 108 | if ((end_time - start_time) > 5) { |
| 109 | break; |
| 110 | } |
| 111 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 112 | if (times(&buf1) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 113 | tst_resm(TFAIL | TERRNO, "times failed"); |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 114 | } else { |
| 115 | if (buf1.tms_utime == 0) |
| 116 | tst_resm(TFAIL, "times report " "0 user time"); |
| 117 | if (buf1.tms_stime == 0) |
| 118 | tst_resm(TFAIL, "times report " |
| 119 | "0 system time"); |
| 120 | if (buf1.tms_cutime != 0) |
| 121 | tst_resm(TFAIL, "times report " |
| 122 | "%ld child user time", |
| 123 | buf1.tms_cutime); |
| 124 | if (buf1.tms_cstime != 0) |
| 125 | tst_resm(TFAIL, |
| 126 | "times report " |
| 127 | "%ld child system time", |
| 128 | buf1.tms_cstime); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 129 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 130 | pid2 = FORK_OR_VFORK(); |
| 131 | if (pid2 < 0) { |
| 132 | tst_brkm(TFAIL, cleanup, "Fork failed"); |
| 133 | } else if (pid2 == 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 134 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 135 | /* Spend some cycles in userspace */ |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 136 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 137 | timeout = 0; |
| 138 | alarm(3); |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 139 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 140 | work(); |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 141 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 142 | /* |
| 143 | * Atleast some CPU system ime must be used |
| 144 | * even in the child process (thereby |
| 145 | * making it independent of the |
| 146 | * processor speed). In fact the child |
| 147 | * uses twice as much CPU time. |
| 148 | */ |
| 149 | start_time = time(NULL); |
| 150 | for (;;) { |
| 151 | if (times(&buf2) == -1) { |
| 152 | tst_resm(TFAIL, |
| 153 | "Call to times " |
| 154 | "failed, " |
| 155 | "errno = %d", errno); |
| 156 | exit(1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 157 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 158 | end_time = time(NULL); |
| 159 | if ((end_time - start_time) |
| 160 | > 10) { |
| 161 | break; |
| 162 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 163 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 164 | exit(0); |
| 165 | } |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 166 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 167 | waitpid(pid2, &status, 0); |
| 168 | if (WEXITSTATUS(status) != 0) { |
| 169 | tst_resm(TFAIL, "Call to times(2) " |
| 170 | "failed in child"); |
| 171 | } |
| 172 | if (times(&buf2) == -1) { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 173 | tst_resm(TFAIL | TTERRNO, "times failed"); |
| 174 | } |
| 175 | if (buf1.tms_utime > buf2.tms_utime) |
| 176 | tst_resm(TFAIL, "Error: parents's " |
| 177 | "user time(%ld) before child " |
| 178 | "> parent's user time (%ld) " |
| 179 | "after child", |
| 180 | buf1.tms_utime, buf2.tms_utime); |
| 181 | if (buf2.tms_cutime == 0) |
| 182 | tst_resm(TFAIL, "times " |
| 183 | "report %ld child user " |
| 184 | "time should be > than " |
| 185 | "zero", buf2.tms_cutime); |
| 186 | if (buf2.tms_cstime == 0) |
| 187 | tst_resm(TFAIL, "times " |
| 188 | "report %ld child system time " |
| 189 | "should be > than zero", |
| 190 | buf2.tms_cstime); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 191 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 192 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 193 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 194 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | /* |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 198 | * sighandler |
| 199 | * Set the timeout to indicate we timed out in the alarm(). |
| 200 | */ |
| 201 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 202 | void sighandler(int signal, siginfo_t * info, void *uc) |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 203 | { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 204 | if (signal == SIGALRM) |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 205 | timeout = 1; |
| 206 | else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 207 | tst_brkm(TBROK, cleanup, "Unexpected signal %d\n", signal); |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 208 | } |
| 209 | |
| 210 | /* |
| 211 | * work |
| 212 | * Do some work in user space, until we get a timeout. |
| 213 | */ |
| 214 | |
| 215 | void work(void) |
| 216 | { |
| 217 | int i, j, k; |
| 218 | |
| 219 | while (!timeout) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 220 | for (i = 0; i < 10000; i++) |
| 221 | for (j = 0; j < 100; j++) |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 222 | k = i * j; |
| 223 | timeout = 0; |
| 224 | } |
| 225 | |
| 226 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 227 | * setup() |
| 228 | * performs all ONE TIME setup for this test |
| 229 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 230 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 231 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 232 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 233 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 234 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 235 | /* Pause if that option was specified |
| 236 | * TEST_PAUSE contains the code to fork the test with the -c option. |
| 237 | */ |
| 238 | TEST_PAUSE; |
| 239 | } |
| 240 | |
| 241 | /* |
| 242 | * cleanup() |
| 243 | * performs all ONE TIME cleanup for this test at |
| 244 | * completion or premature exit |
| 245 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 246 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 247 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 248 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 249 | } |