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
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18 */
19
20 /*
21 * NAME
22 * times03.c
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>
34 * times03 [-c n] [-f] [-P x] [-t]
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>
52 #include <time.h>
53 #include "test.h"
54 #include <signal.h>
55 #include <stdint.h>
56
57 char *TCID = "times03";
58 int TST_TOTAL = 1;
59
60 volatile int timeout; /* Did we timeout in alarm() ? */
61
62 void work(void);
63 void sighandler(int signal, siginfo_t * info, void *uc);
64
65 void setup(void);
66 void cleanup(void);
67
main(int argc,char ** argv)68 int main(int argc, char **argv)
69 {
70 struct tms buf1, buf2;
71 time_t start_time, end_time;
72 int pid2, status;
73 struct sigaction sa;
74
75 tst_parse_opts(argc, argv, NULL, NULL);
76
77 setup();
78
79 /*
80 * We spend time in userspace using the following mechanism :
81 * Setup an alarm() for 3 secs and do some simple loop operations
82 * until we get the signal. This makes the test independent of
83 * processor speed.
84 */
85 sa.sa_sigaction = sighandler;
86 sigemptyset(&sa.sa_mask);
87 sa.sa_flags = SA_SIGINFO;
88
89 if (sigaction(SIGALRM, &sa, NULL) < 0)
90 tst_brkm(TBROK | TERRNO, cleanup, "Sigaction failed !\n");
91
92 timeout = 0;
93 alarm(3);
94
95 work();
96
97 /*
98 * At least some CPU time must be used in system space. This is
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 (;;) {
105 if (times(&buf1) == -1)
106 tst_resm(TFAIL | TERRNO, "times failed");
107 end_time = time(NULL);
108 if ((end_time - start_time) > 5) {
109 break;
110 }
111 }
112 if (times(&buf1) == -1) {
113 tst_resm(TFAIL | TERRNO, "times failed");
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);
129
130 pid2 = FORK_OR_VFORK();
131 if (pid2 < 0) {
132 tst_brkm(TFAIL, cleanup, "Fork failed");
133 } else if (pid2 == 0) {
134
135 /* Spend some cycles in userspace */
136
137 timeout = 0;
138 alarm(3);
139
140 work();
141
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);
157 }
158 end_time = time(NULL);
159 if ((end_time - start_time)
160 > 10) {
161 break;
162 }
163 }
164 exit(0);
165 }
166
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) {
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);
191 }
192
193 cleanup();
194 tst_exit();
195 }
196
197 /*
198 * sighandler
199 * Set the timeout to indicate we timed out in the alarm().
200 */
201
sighandler(int signal,siginfo_t * info,void * uc)202 void sighandler(int signal, siginfo_t * info, void *uc)
203 {
204 if (signal == SIGALRM)
205 timeout = 1;
206 else
207 tst_brkm(TBROK, cleanup, "Unexpected signal %d\n", signal);
208 }
209
210 /*
211 * work
212 * Do some work in user space, until we get a timeout.
213 */
214
work(void)215 void work(void)
216 {
217 int i, j, k;
218
219 while (!timeout)
220 for (i = 0; i < 10000; i++)
221 for (j = 0; j < 100; j++)
222 k = i * j;
223 timeout = 0;
224 }
225
226 /*
227 * setup()
228 * performs all ONE TIME setup for this test
229 */
setup(void)230 void setup(void)
231 {
232
233 tst_sig(FORK, DEF_HANDLER, cleanup);
234
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 */
cleanup(void)246 void cleanup(void)
247 {
248
249 }
250