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> |
| 51 | #include <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 | { |
Cyril Hrubis | 0b9589f | 2014-05-27 17:40:33 +0200 | [diff] [blame] | 70 | const char *msg; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 71 | |
| 72 | struct tms buf1, buf2; |
| 73 | time_t start_time, end_time; |
Garrett Cooper | f1d2a63 | 2010-12-19 08:23:06 -0800 | [diff] [blame] | 74 | int pid2, status; |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 75 | struct sigaction sa; |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 76 | |
Garrett Cooper | f1d2a63 | 2010-12-19 08:23:06 -0800 | [diff] [blame] | 77 | if ((msg = parse_opts(argc, argv, NULL, NULL)) != NULL) |
Garrett Cooper | 60fa801 | 2010-11-22 13:50:58 -0800 | [diff] [blame] | 78 | tst_brkm(TBROK, NULL, "OPTION PARSING ERROR - %s", msg); |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 79 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 80 | setup(); |
| 81 | |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 82 | /* |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 83 | * We spend time in userspace using the following mechanism : |
| 84 | * Setup an alarm() for 3 secs and do some simple loop operations |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 85 | * until we get the signal. This makes the test independent of |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 86 | * processor speed. |
| 87 | */ |
| 88 | sa.sa_sigaction = sighandler; |
| 89 | sigemptyset(&sa.sa_mask); |
| 90 | sa.sa_flags = SA_SIGINFO; |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 91 | |
Garrett Cooper | f1d2a63 | 2010-12-19 08:23:06 -0800 | [diff] [blame] | 92 | if (sigaction(SIGALRM, &sa, NULL) < 0) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 93 | tst_brkm(TBROK | TERRNO, cleanup, "Sigaction failed !\n"); |
subrata_modak | bdbaec5 | 2009-02-26 12:14:51 +0000 | [diff] [blame] | 94 | |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 95 | timeout = 0; |
| 96 | alarm(3); |
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 | work(); |
| 99 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 100 | /* |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 101 | * At least some CPU time must be used in system space. This is |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 102 | * achieved by executing the times(2) call for |
| 103 | * atleast 5 secs. This logic makes it independant |
| 104 | * of the processor speed. |
| 105 | */ |
| 106 | start_time = time(NULL); |
| 107 | for (;;) { |
Garrett Cooper | f1d2a63 | 2010-12-19 08:23:06 -0800 | [diff] [blame] | 108 | if (times(&buf1) == -1) |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 109 | tst_resm(TFAIL | TERRNO, "times failed"); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 110 | end_time = time(NULL); |
| 111 | if ((end_time - start_time) > 5) { |
| 112 | break; |
| 113 | } |
| 114 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 115 | if (times(&buf1) == -1) { |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 116 | tst_resm(TFAIL | TERRNO, "times failed"); |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 117 | } else { |
| 118 | if (buf1.tms_utime == 0) |
| 119 | tst_resm(TFAIL, "times report " "0 user time"); |
| 120 | if (buf1.tms_stime == 0) |
| 121 | tst_resm(TFAIL, "times report " |
| 122 | "0 system time"); |
| 123 | if (buf1.tms_cutime != 0) |
| 124 | tst_resm(TFAIL, "times report " |
| 125 | "%ld child user time", |
| 126 | buf1.tms_cutime); |
| 127 | if (buf1.tms_cstime != 0) |
| 128 | tst_resm(TFAIL, |
| 129 | "times report " |
| 130 | "%ld child system time", |
| 131 | buf1.tms_cstime); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 132 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 133 | pid2 = FORK_OR_VFORK(); |
| 134 | if (pid2 < 0) { |
| 135 | tst_brkm(TFAIL, cleanup, "Fork failed"); |
| 136 | } else if (pid2 == 0) { |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 137 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 138 | /* Spend some cycles in userspace */ |
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 | timeout = 0; |
| 141 | alarm(3); |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 142 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 143 | work(); |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 144 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 145 | /* |
| 146 | * Atleast some CPU system ime must be used |
| 147 | * even in the child process (thereby |
| 148 | * making it independent of the |
| 149 | * processor speed). In fact the child |
| 150 | * uses twice as much CPU time. |
| 151 | */ |
| 152 | start_time = time(NULL); |
| 153 | for (;;) { |
| 154 | if (times(&buf2) == -1) { |
| 155 | tst_resm(TFAIL, |
| 156 | "Call to times " |
| 157 | "failed, " |
| 158 | "errno = %d", errno); |
| 159 | exit(1); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 160 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 161 | end_time = time(NULL); |
| 162 | if ((end_time - start_time) |
| 163 | > 10) { |
| 164 | break; |
| 165 | } |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 166 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 167 | exit(0); |
| 168 | } |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 169 | |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 170 | waitpid(pid2, &status, 0); |
| 171 | if (WEXITSTATUS(status) != 0) { |
| 172 | tst_resm(TFAIL, "Call to times(2) " |
| 173 | "failed in child"); |
| 174 | } |
| 175 | if (times(&buf2) == -1) { |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 176 | tst_resm(TFAIL | TTERRNO, "times failed"); |
| 177 | } |
| 178 | if (buf1.tms_utime > buf2.tms_utime) |
| 179 | tst_resm(TFAIL, "Error: parents's " |
| 180 | "user time(%ld) before child " |
| 181 | "> parent's user time (%ld) " |
| 182 | "after child", |
| 183 | buf1.tms_utime, buf2.tms_utime); |
| 184 | if (buf2.tms_cutime == 0) |
| 185 | tst_resm(TFAIL, "times " |
| 186 | "report %ld child user " |
| 187 | "time should be > than " |
| 188 | "zero", buf2.tms_cutime); |
| 189 | if (buf2.tms_cstime == 0) |
| 190 | tst_resm(TFAIL, "times " |
| 191 | "report %ld child system time " |
| 192 | "should be > than zero", |
| 193 | buf2.tms_cstime); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 194 | } |
Cyril Hrubis | e38b961 | 2014-06-02 17:20:57 +0200 | [diff] [blame] | 195 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 196 | cleanup(); |
Garrett Cooper | 7d0a4a5 | 2010-12-16 10:05:08 -0800 | [diff] [blame] | 197 | tst_exit(); |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | /* |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 201 | * sighandler |
| 202 | * Set the timeout to indicate we timed out in the alarm(). |
| 203 | */ |
| 204 | |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 205 | void sighandler(int signal, siginfo_t * info, void *uc) |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 206 | { |
subrata_modak | 4bb656a | 2009-02-26 12:02:09 +0000 | [diff] [blame] | 207 | if (signal == SIGALRM) |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 208 | timeout = 1; |
| 209 | else |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 210 | tst_brkm(TBROK, cleanup, "Unexpected signal %d\n", signal); |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | /* |
| 214 | * work |
| 215 | * Do some work in user space, until we get a timeout. |
| 216 | */ |
| 217 | |
| 218 | void work(void) |
| 219 | { |
| 220 | int i, j, k; |
| 221 | |
| 222 | while (!timeout) |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 223 | for (i = 0; i < 10000; i++) |
| 224 | for (j = 0; j < 100; j++) |
subrata_modak | 551313c | 2007-08-06 10:53:51 +0000 | [diff] [blame] | 225 | k = i * j; |
| 226 | timeout = 0; |
| 227 | } |
| 228 | |
| 229 | /* |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 230 | * setup() |
| 231 | * performs all ONE TIME setup for this test |
| 232 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 233 | void setup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 234 | { |
Garrett Cooper | 2c28215 | 2010-12-16 00:55:50 -0800 | [diff] [blame] | 235 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 236 | tst_sig(FORK, DEF_HANDLER, cleanup); |
| 237 | |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 238 | /* Pause if that option was specified |
| 239 | * TEST_PAUSE contains the code to fork the test with the -c option. |
| 240 | */ |
| 241 | TEST_PAUSE; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * cleanup() |
| 246 | * performs all ONE TIME cleanup for this test at |
| 247 | * completion or premature exit |
| 248 | */ |
subrata_modak | 56207ce | 2009-03-23 13:35:39 +0000 | [diff] [blame] | 249 | void cleanup(void) |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 250 | { |
plars | 865695b | 2001-08-27 22:15:12 +0000 | [diff] [blame] | 251 | |
Wanlong Gao | 354ebb4 | 2012-12-07 10:10:04 +0800 | [diff] [blame] | 252 | } |