blob: f6f83232b0bed210af950b23cd72a38c5a10da9f [file] [log] [blame]
plars865695b2001-08-27 22:15:12 +00001/*
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 Gao4548c6c2012-10-19 18:03:36 +080017 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
plars865695b2001-08-27 22:15:12 +000018 */
19
20/*
21 * NAME
nstrazfa31d552002-05-14 16:50:06 +000022 * times03.c
plars865695b2001-08-27 22:15:12 +000023 *
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>
nstrazfa31d552002-05-14 16:50:06 +000034 * times03 [-c n] [-f] [-P x] [-t]
plars865695b2001-08-27 22:15:12 +000035 * 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 Jackson249f4052016-12-13 16:16:00 +000051#include <sys/wait.h>
plars74948ad2002-11-14 16:16:14 +000052#include <time.h>
Garrett Coopere8530df2010-12-21 11:37:57 -080053#include "test.h"
subrata_modak551313c2007-08-06 10:53:51 +000054#include <signal.h>
subrata_modak923b23f2009-11-02 13:57:16 +000055#include <stdint.h>
plars865695b2001-08-27 22:15:12 +000056
nstrazfa31d552002-05-14 16:50:06 +000057char *TCID = "times03";
plars865695b2001-08-27 22:15:12 +000058int TST_TOTAL = 1;
Wanlong Gao354ebb42012-12-07 10:10:04 +080059
subrata_modak0c2e2e22008-01-28 11:33:15 +000060volatile int timeout; /* Did we timeout in alarm() ? */
subrata_modak551313c2007-08-06 10:53:51 +000061
62void work(void);
subrata_modak56207ce2009-03-23 13:35:39 +000063void sighandler(int signal, siginfo_t * info, void *uc);
plars865695b2001-08-27 22:15:12 +000064
65void setup(void);
66void cleanup(void);
67
plars74948ad2002-11-14 16:16:14 +000068int main(int argc, char **argv)
plars865695b2001-08-27 22:15:12 +000069{
plars865695b2001-08-27 22:15:12 +000070 struct tms buf1, buf2;
71 time_t start_time, end_time;
Garrett Cooperf1d2a632010-12-19 08:23:06 -080072 int pid2, status;
subrata_modak551313c2007-08-06 10:53:51 +000073 struct sigaction sa;
plars865695b2001-08-27 22:15:12 +000074
Cyril Hrubisd6d11d02015-03-09 17:35:43 +010075 tst_parse_opts(argc, argv, NULL, NULL);
Garrett Cooper2c282152010-12-16 00:55:50 -080076
plars865695b2001-08-27 22:15:12 +000077 setup();
78
subrata_modak4bb656a2009-02-26 12:02:09 +000079 /*
subrata_modak551313c2007-08-06 10:53:51 +000080 * We spend time in userspace using the following mechanism :
81 * Setup an alarm() for 3 secs and do some simple loop operations
subrata_modak4bb656a2009-02-26 12:02:09 +000082 * until we get the signal. This makes the test independent of
subrata_modak551313c2007-08-06 10:53:51 +000083 * processor speed.
84 */
85 sa.sa_sigaction = sighandler;
86 sigemptyset(&sa.sa_mask);
87 sa.sa_flags = SA_SIGINFO;
subrata_modakbdbaec52009-02-26 12:14:51 +000088
Garrett Cooperf1d2a632010-12-19 08:23:06 -080089 if (sigaction(SIGALRM, &sa, NULL) < 0)
Wanlong Gao354ebb42012-12-07 10:10:04 +080090 tst_brkm(TBROK | TERRNO, cleanup, "Sigaction failed !\n");
subrata_modakbdbaec52009-02-26 12:14:51 +000091
subrata_modak551313c2007-08-06 10:53:51 +000092 timeout = 0;
93 alarm(3);
plars865695b2001-08-27 22:15:12 +000094
subrata_modak551313c2007-08-06 10:53:51 +000095 work();
96
plars865695b2001-08-27 22:15:12 +000097 /*
subrata_modak551313c2007-08-06 10:53:51 +000098 * At least some CPU time must be used in system space. This is
plars865695b2001-08-27 22:15:12 +000099 * 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 Cooperf1d2a632010-12-19 08:23:06 -0800105 if (times(&buf1) == -1)
Wanlong Gao354ebb42012-12-07 10:10:04 +0800106 tst_resm(TFAIL | TERRNO, "times failed");
plars865695b2001-08-27 22:15:12 +0000107 end_time = time(NULL);
108 if ((end_time - start_time) > 5) {
109 break;
110 }
111 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200112 if (times(&buf1) == -1) {
Wanlong Gao354ebb42012-12-07 10:10:04 +0800113 tst_resm(TFAIL | TERRNO, "times failed");
Cyril Hrubise38b9612014-06-02 17:20:57 +0200114 } 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);
plars865695b2001-08-27 22:15:12 +0000129
Cyril Hrubise38b9612014-06-02 17:20:57 +0200130 pid2 = FORK_OR_VFORK();
131 if (pid2 < 0) {
132 tst_brkm(TFAIL, cleanup, "Fork failed");
133 } else if (pid2 == 0) {
subrata_modak56207ce2009-03-23 13:35:39 +0000134
Cyril Hrubise38b9612014-06-02 17:20:57 +0200135 /* Spend some cycles in userspace */
subrata_modak551313c2007-08-06 10:53:51 +0000136
Cyril Hrubise38b9612014-06-02 17:20:57 +0200137 timeout = 0;
138 alarm(3);
subrata_modak551313c2007-08-06 10:53:51 +0000139
Cyril Hrubise38b9612014-06-02 17:20:57 +0200140 work();
subrata_modak551313c2007-08-06 10:53:51 +0000141
Cyril Hrubise38b9612014-06-02 17:20:57 +0200142 /*
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);
plars865695b2001-08-27 22:15:12 +0000157 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200158 end_time = time(NULL);
159 if ((end_time - start_time)
160 > 10) {
161 break;
162 }
plars865695b2001-08-27 22:15:12 +0000163 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200164 exit(0);
165 }
subrata_modak551313c2007-08-06 10:53:51 +0000166
Cyril Hrubise38b9612014-06-02 17:20:57 +0200167 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 Hrubise38b9612014-06-02 17:20:57 +0200173 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);
plars865695b2001-08-27 22:15:12 +0000191 }
Cyril Hrubise38b9612014-06-02 17:20:57 +0200192
plars865695b2001-08-27 22:15:12 +0000193 cleanup();
Garrett Cooper7d0a4a52010-12-16 10:05:08 -0800194 tst_exit();
plars865695b2001-08-27 22:15:12 +0000195}
196
197/*
subrata_modak551313c2007-08-06 10:53:51 +0000198 * sighandler
199 * Set the timeout to indicate we timed out in the alarm().
200 */
201
subrata_modak56207ce2009-03-23 13:35:39 +0000202void sighandler(int signal, siginfo_t * info, void *uc)
subrata_modak551313c2007-08-06 10:53:51 +0000203{
subrata_modak4bb656a2009-02-26 12:02:09 +0000204 if (signal == SIGALRM)
subrata_modak551313c2007-08-06 10:53:51 +0000205 timeout = 1;
206 else
subrata_modak56207ce2009-03-23 13:35:39 +0000207 tst_brkm(TBROK, cleanup, "Unexpected signal %d\n", signal);
subrata_modak551313c2007-08-06 10:53:51 +0000208}
209
210/*
211 * work
212 * Do some work in user space, until we get a timeout.
213 */
214
215void work(void)
216{
217 int i, j, k;
218
219 while (!timeout)
subrata_modak56207ce2009-03-23 13:35:39 +0000220 for (i = 0; i < 10000; i++)
221 for (j = 0; j < 100; j++)
subrata_modak551313c2007-08-06 10:53:51 +0000222 k = i * j;
223 timeout = 0;
224}
225
226/*
plars865695b2001-08-27 22:15:12 +0000227 * setup()
228 * performs all ONE TIME setup for this test
229 */
subrata_modak56207ce2009-03-23 13:35:39 +0000230void setup(void)
plars865695b2001-08-27 22:15:12 +0000231{
Garrett Cooper2c282152010-12-16 00:55:50 -0800232
plars865695b2001-08-27 22:15:12 +0000233 tst_sig(FORK, DEF_HANDLER, cleanup);
234
plars865695b2001-08-27 22:15:12 +0000235 /* 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_modak56207ce2009-03-23 13:35:39 +0000246void cleanup(void)
plars865695b2001-08-27 22:15:12 +0000247{
plars865695b2001-08-27 22:15:12 +0000248
Wanlong Gao354ebb42012-12-07 10:10:04 +0800249}