1 /* $NetBSD: t_getrusage.c,v 1.4 2016/08/05 15:01:39 scole Exp $ */
4 * Copyright (c) 2011 The NetBSD Foundation, Inc.
7 * This code is derived from software contributed to The NetBSD Foundation
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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.
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.
31 #include <sys/cdefs.h>
32 __RCSID("$NetBSD: t_getrusage.c,v 1.4 2016/08/05 15:01:39 scole Exp $");
34 #include <sys/resource.h>
44 static void work(void);
45 static void sighandler(int);
47 static const size_t maxiter = 2000;
51 sighandler(int signo __unused)
62 size_t n = UINT16_MAX * 10;
66 asm volatile("l.nop"); /* Do something. */
67 #elif defined(__ia64__)
68 asm volatile("nop 0"); /* Do something. */
70 asm volatile("nop"); /* Do something. */
76 ATF_TC(getrusage_err);
77 ATF_TC_HEAD(getrusage_err, tc)
79 atf_tc_set_md_var(tc, "descr", "Test error conditions");
82 ATF_TC_BODY(getrusage_err, tc)
88 ATF_REQUIRE(getrusage(INT_MAX, &ru) != 0);
89 ATF_REQUIRE(errno == EINVAL);
93 ATF_REQUIRE(getrusage(RUSAGE_SELF, (void *)0) != 0);
94 ATF_REQUIRE(errno == EFAULT);
97 ATF_TC(getrusage_sig);
98 ATF_TC_HEAD(getrusage_sig, tc)
100 atf_tc_set_md_var(tc, "descr", "Test signal count with getrusage(2)");
103 ATF_TC_BODY(getrusage_sig, tc)
110 * Test that signals are recorded.
112 ATF_REQUIRE(signal(SIGUSR1, sighandler) != SIG_ERR);
114 for (i = 0; i < n; i++)
115 ATF_REQUIRE(raise(SIGUSR1) == 0);
117 (void)memset(&ru, 0, sizeof(struct rusage));
118 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
120 if (n != ru.ru_nsignals)
121 atf_tc_fail("getrusage(2) did not record signals");
124 ATF_TC(getrusage_utime_back);
125 ATF_TC_HEAD(getrusage_utime_back, tc)
127 atf_tc_set_md_var(tc, "descr", "Test bogus values from getrusage(2)");
130 ATF_TC_BODY(getrusage_utime_back, tc)
132 struct rusage ru1, ru2;
136 * Test that two consecutive calls are sane.
139 atf_tc_expect_fail("PR kern/30115");
142 for (i = 0; i < maxiter; i++) {
144 (void)memset(&ru1, 0, sizeof(struct rusage));
145 (void)memset(&ru2, 0, sizeof(struct rusage));
149 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru1) == 0);
153 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru2) == 0);
155 if (timercmp(&ru2.ru_utime, &ru1.ru_utime, <) != 0)
156 atf_tc_fail("user time went backwards");
160 atf_tc_fail("anticipated error did not occur");
164 ATF_TC(getrusage_utime_zero);
165 ATF_TC_HEAD(getrusage_utime_zero, tc)
167 atf_tc_set_md_var(tc, "descr", "Test zero utime from getrusage(2)");
170 ATF_TC_BODY(getrusage_utime_zero, tc)
176 atf_tc_skip("this testcase passes/fails sporadically on FreeBSD/i386 "
177 "@ r273153 (at least)");
181 * Test that getrusage(2) does not return
182 * zero user time for the calling process.
184 * See also (duplicate) PR port-amd64/41734.
186 atf_tc_expect_fail("PR kern/30115");
188 for (i = 0; i < maxiter; i++) {
192 (void)memset(&ru, 0, sizeof(struct rusage));
194 ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
196 if (ru.ru_utime.tv_sec == 0 && ru.ru_utime.tv_usec == 0)
197 atf_tc_fail("zero user time from getrusage(2)");
200 atf_tc_fail("anticipated error did not occur");
206 ATF_TP_ADD_TC(tp, getrusage_err);
207 ATF_TP_ADD_TC(tp, getrusage_sig);
208 ATF_TP_ADD_TC(tp, getrusage_utime_back);
209 ATF_TP_ADD_TC(tp, getrusage_utime_zero);
211 return atf_no_error();