xref: /netbsd-src/tests/lib/libc/sys/t_getrusage.c (revision ca453df649ce9db45b64d73678ba06cbccf9aa11)
1 /* $NetBSD: t_getrusage.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $ */
2 
3 /*-
4  * Copyright (c) 2011 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Jukka Ruohonen.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getrusage.c,v 1.1 2011/07/07 06:57:53 jruoho Exp $");
33 
34 #include <sys/resource.h>
35 #include <sys/time.h>
36 
37 #include <atf-c.h>
38 #include <errno.h>
39 #include <limits.h>
40 #include <signal.h>
41 #include <string.h>
42 
43 static void		work(void);
44 static void		sighandler(int);
45 
46 static const size_t	maxiter = 2000;
47 
48 static void
49 sighandler(int signo)
50 {
51 	/* Nothing. */
52 }
53 
54 static void
55 work(void)
56 {
57 	size_t n = UINT16_MAX * 10;
58 
59 	while (n > 0) {
60 		 asm volatile("nop");	/* Do something. */
61 		 n--;
62 	}
63 }
64 
65 ATF_TC(getrusage_err);
66 ATF_TC_HEAD(getrusage_err, tc)
67 {
68 	atf_tc_set_md_var(tc, "descr", "Test error conditions");
69 }
70 
71 ATF_TC_BODY(getrusage_err, tc)
72 {
73 	struct rusage ru;
74 
75 	errno = 0;
76 
77 	ATF_REQUIRE(getrusage(INT_MAX, &ru) != 0);
78 	ATF_REQUIRE(errno == EINVAL);
79 
80 	errno = 0;
81 
82 	ATF_REQUIRE(getrusage(RUSAGE_SELF, (void *)0) != 0);
83 	ATF_REQUIRE(errno == EFAULT);
84 }
85 
86 ATF_TC(getrusage_sig);
87 ATF_TC_HEAD(getrusage_sig, tc)
88 {
89 	atf_tc_set_md_var(tc, "descr", "Test signal count with getrusage(2)");
90 }
91 
92 ATF_TC_BODY(getrusage_sig, tc)
93 {
94 	struct rusage ru;
95 	const long n = 5;
96 	int i;
97 
98 	/*
99 	 * Test that signals are recorded.
100 	 */
101 	ATF_REQUIRE(signal(SIGUSR1, sighandler) != SIG_ERR);
102 
103 	for (i = 0; i < n; i++)
104 		ATF_REQUIRE(raise(SIGUSR1) == 0);
105 
106 	(void)memset(&ru, 0, sizeof(struct rusage));
107 	ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
108 
109 	if (n != ru.ru_nsignals)
110 		atf_tc_fail("getrusage(2) did not record signals");
111 }
112 
113 ATF_TC(getrusage_utime_back);
114 ATF_TC_HEAD(getrusage_utime_back, tc)
115 {
116 	atf_tc_set_md_var(tc, "descr", "Test bogus values from getrusage(2)");
117 }
118 
119 ATF_TC_BODY(getrusage_utime_back, tc)
120 {
121 	struct rusage ru1, ru2;
122 	size_t i;
123 
124 	/*
125 	 * Test that two consecutive calls are sane.
126 	 */
127 	atf_tc_expect_fail("PR kern/30115");
128 
129 	for (i = 0; i < maxiter; i++) {
130 
131 		(void)memset(&ru1, 0, sizeof(struct rusage));
132 		(void)memset(&ru2, 0, sizeof(struct rusage));
133 
134 		work();
135 
136 		ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru1) == 0);
137 
138 		work();
139 
140 		ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru2) == 0);
141 
142 		if (timercmp(&ru2.ru_utime, &ru1.ru_utime, <) != 0)
143 			atf_tc_fail("user time went backwards");
144 	}
145 
146 	atf_tc_fail("anticipated error did not occur");
147 }
148 
149 ATF_TC(getrusage_utime_zero);
150 ATF_TC_HEAD(getrusage_utime_zero, tc)
151 {
152 	atf_tc_set_md_var(tc, "descr", "Test zero utime from getrusage(2)");
153 }
154 
155 ATF_TC_BODY(getrusage_utime_zero, tc)
156 {
157 	struct rusage ru;
158 	size_t i;
159 
160 	/*
161 	 * Test that getrusage(2) does not return
162 	 * zero user time for the calling process.
163 	 *
164 	 * See also (duplicate) PR port-amd64/41734.
165 	 */
166 	atf_tc_expect_fail("PR kern/30115");
167 
168 	for (i = 0; i < maxiter; i++) {
169 
170 		work();
171 
172 		(void)memset(&ru, 0, sizeof(struct rusage));
173 
174 		ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
175 
176 		if (ru.ru_utime.tv_sec == 0 && ru.ru_utime.tv_usec == 0)
177 			atf_tc_fail("zero user time from getrusage(2)");
178 	}
179 
180 	atf_tc_fail("anticipated error did not occur");
181 }
182 
183 ATF_TP_ADD_TCS(tp)
184 {
185 
186 	ATF_TP_ADD_TC(tp, getrusage_err);
187 	ATF_TP_ADD_TC(tp, getrusage_sig);
188 	ATF_TP_ADD_TC(tp, getrusage_utime_back);
189 	ATF_TP_ADD_TC(tp, getrusage_utime_zero);
190 
191 	return atf_no_error();
192 }
193