xref: /minix3/tests/lib/libc/sys/t_getrusage.c (revision 0a6a1f1d05b60e214de2f05a7310ddd1f0e590e7)
1*0a6a1f1dSLionel Sambuc /* $NetBSD: t_getrusage.c,v 1.3 2014/09/03 19:24:12 matt Exp $ */
211be35a1SLionel Sambuc 
311be35a1SLionel Sambuc /*-
411be35a1SLionel Sambuc  * Copyright (c) 2011 The NetBSD Foundation, Inc.
511be35a1SLionel Sambuc  * All rights reserved.
611be35a1SLionel Sambuc  *
711be35a1SLionel Sambuc  * This code is derived from software contributed to The NetBSD Foundation
811be35a1SLionel Sambuc  * by Jukka Ruohonen.
911be35a1SLionel Sambuc  *
1011be35a1SLionel Sambuc  * Redistribution and use in source and binary forms, with or without
1111be35a1SLionel Sambuc  * modification, are permitted provided that the following conditions
1211be35a1SLionel Sambuc  * are met:
1311be35a1SLionel Sambuc  * 1. Redistributions of source code must retain the above copyright
1411be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer.
1511be35a1SLionel Sambuc  * 2. Redistributions in binary form must reproduce the above copyright
1611be35a1SLionel Sambuc  *    notice, this list of conditions and the following disclaimer in the
1711be35a1SLionel Sambuc  *    documentation and/or other materials provided with the distribution.
1811be35a1SLionel Sambuc  *
1911be35a1SLionel Sambuc  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
2011be35a1SLionel Sambuc  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2111be35a1SLionel Sambuc  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2211be35a1SLionel Sambuc  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2311be35a1SLionel Sambuc  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2411be35a1SLionel Sambuc  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2511be35a1SLionel Sambuc  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2611be35a1SLionel Sambuc  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2711be35a1SLionel Sambuc  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2811be35a1SLionel Sambuc  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2911be35a1SLionel Sambuc  * POSSIBILITY OF SUCH DAMAGE.
3011be35a1SLionel Sambuc  */
3111be35a1SLionel Sambuc #include <sys/cdefs.h>
32*0a6a1f1dSLionel Sambuc __RCSID("$NetBSD: t_getrusage.c,v 1.3 2014/09/03 19:24:12 matt Exp $");
3311be35a1SLionel Sambuc 
3411be35a1SLionel Sambuc #include <sys/resource.h>
3511be35a1SLionel Sambuc #include <sys/time.h>
3611be35a1SLionel Sambuc 
3711be35a1SLionel Sambuc #include <atf-c.h>
3811be35a1SLionel Sambuc #include <errno.h>
3911be35a1SLionel Sambuc #include <limits.h>
4011be35a1SLionel Sambuc #include <signal.h>
4111be35a1SLionel Sambuc #include <stdint.h>
4211be35a1SLionel Sambuc #include <string.h>
4311be35a1SLionel Sambuc 
4411be35a1SLionel Sambuc static void		work(void);
4511be35a1SLionel Sambuc static void		sighandler(int);
4611be35a1SLionel Sambuc 
4711be35a1SLionel Sambuc static const size_t	maxiter = 2000;
4811be35a1SLionel Sambuc 
4911be35a1SLionel Sambuc static void
sighandler(int signo)5011be35a1SLionel Sambuc sighandler(int signo)
5111be35a1SLionel Sambuc {
5211be35a1SLionel Sambuc 	/* Nothing. */
5311be35a1SLionel Sambuc }
5411be35a1SLionel Sambuc 
5511be35a1SLionel Sambuc static void
work(void)5611be35a1SLionel Sambuc work(void)
5711be35a1SLionel Sambuc {
5811be35a1SLionel Sambuc 	size_t n = UINT16_MAX * 10;
5911be35a1SLionel Sambuc 
6011be35a1SLionel Sambuc 	while (n > 0) {
61*0a6a1f1dSLionel Sambuc #ifdef __or1k__
62*0a6a1f1dSLionel Sambuc 		 asm volatile("l.nop");	/* Do something. */
63*0a6a1f1dSLionel Sambuc #else
6411be35a1SLionel Sambuc 		 asm volatile("nop");	/* Do something. */
65*0a6a1f1dSLionel Sambuc #endif
6611be35a1SLionel Sambuc 		 n--;
6711be35a1SLionel Sambuc 	}
6811be35a1SLionel Sambuc }
6911be35a1SLionel Sambuc 
7011be35a1SLionel Sambuc ATF_TC(getrusage_err);
ATF_TC_HEAD(getrusage_err,tc)7111be35a1SLionel Sambuc ATF_TC_HEAD(getrusage_err, tc)
7211be35a1SLionel Sambuc {
7311be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test error conditions");
7411be35a1SLionel Sambuc }
7511be35a1SLionel Sambuc 
ATF_TC_BODY(getrusage_err,tc)7611be35a1SLionel Sambuc ATF_TC_BODY(getrusage_err, tc)
7711be35a1SLionel Sambuc {
7811be35a1SLionel Sambuc 	struct rusage ru;
7911be35a1SLionel Sambuc 
8011be35a1SLionel Sambuc 	errno = 0;
8111be35a1SLionel Sambuc 
8211be35a1SLionel Sambuc 	ATF_REQUIRE(getrusage(INT_MAX, &ru) != 0);
8311be35a1SLionel Sambuc 	ATF_REQUIRE(errno == EINVAL);
8411be35a1SLionel Sambuc 
8511be35a1SLionel Sambuc 	errno = 0;
8611be35a1SLionel Sambuc 
8711be35a1SLionel Sambuc 	ATF_REQUIRE(getrusage(RUSAGE_SELF, (void *)0) != 0);
8811be35a1SLionel Sambuc 	ATF_REQUIRE(errno == EFAULT);
8911be35a1SLionel Sambuc }
9011be35a1SLionel Sambuc 
9111be35a1SLionel Sambuc ATF_TC(getrusage_sig);
ATF_TC_HEAD(getrusage_sig,tc)9211be35a1SLionel Sambuc ATF_TC_HEAD(getrusage_sig, tc)
9311be35a1SLionel Sambuc {
9411be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test signal count with getrusage(2)");
9511be35a1SLionel Sambuc }
9611be35a1SLionel Sambuc 
ATF_TC_BODY(getrusage_sig,tc)9711be35a1SLionel Sambuc ATF_TC_BODY(getrusage_sig, tc)
9811be35a1SLionel Sambuc {
9911be35a1SLionel Sambuc 	struct rusage ru;
10011be35a1SLionel Sambuc 	const long n = 5;
10111be35a1SLionel Sambuc 	int i;
10211be35a1SLionel Sambuc 
10311be35a1SLionel Sambuc 	/*
10411be35a1SLionel Sambuc 	 * Test that signals are recorded.
10511be35a1SLionel Sambuc 	 */
10611be35a1SLionel Sambuc 	ATF_REQUIRE(signal(SIGUSR1, sighandler) != SIG_ERR);
10711be35a1SLionel Sambuc 
10811be35a1SLionel Sambuc 	for (i = 0; i < n; i++)
10911be35a1SLionel Sambuc 		ATF_REQUIRE(raise(SIGUSR1) == 0);
11011be35a1SLionel Sambuc 
11111be35a1SLionel Sambuc 	(void)memset(&ru, 0, sizeof(struct rusage));
11211be35a1SLionel Sambuc 	ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
11311be35a1SLionel Sambuc 
11411be35a1SLionel Sambuc 	if (n != ru.ru_nsignals)
11511be35a1SLionel Sambuc 		atf_tc_fail("getrusage(2) did not record signals");
11611be35a1SLionel Sambuc }
11711be35a1SLionel Sambuc 
11811be35a1SLionel Sambuc ATF_TC(getrusage_utime_back);
ATF_TC_HEAD(getrusage_utime_back,tc)11911be35a1SLionel Sambuc ATF_TC_HEAD(getrusage_utime_back, tc)
12011be35a1SLionel Sambuc {
12111be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test bogus values from getrusage(2)");
12211be35a1SLionel Sambuc }
12311be35a1SLionel Sambuc 
ATF_TC_BODY(getrusage_utime_back,tc)12411be35a1SLionel Sambuc ATF_TC_BODY(getrusage_utime_back, tc)
12511be35a1SLionel Sambuc {
12611be35a1SLionel Sambuc 	struct rusage ru1, ru2;
12711be35a1SLionel Sambuc 	size_t i;
12811be35a1SLionel Sambuc 
12911be35a1SLionel Sambuc 	/*
13011be35a1SLionel Sambuc 	 * Test that two consecutive calls are sane.
13111be35a1SLionel Sambuc 	 */
13211be35a1SLionel Sambuc 	atf_tc_expect_fail("PR kern/30115");
13311be35a1SLionel Sambuc 
13411be35a1SLionel Sambuc 	for (i = 0; i < maxiter; i++) {
13511be35a1SLionel Sambuc 
13611be35a1SLionel Sambuc 		(void)memset(&ru1, 0, sizeof(struct rusage));
13711be35a1SLionel Sambuc 		(void)memset(&ru2, 0, sizeof(struct rusage));
13811be35a1SLionel Sambuc 
13911be35a1SLionel Sambuc 		work();
14011be35a1SLionel Sambuc 
14111be35a1SLionel Sambuc 		ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru1) == 0);
14211be35a1SLionel Sambuc 
14311be35a1SLionel Sambuc 		work();
14411be35a1SLionel Sambuc 
14511be35a1SLionel Sambuc 		ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru2) == 0);
14611be35a1SLionel Sambuc 
14711be35a1SLionel Sambuc 		if (timercmp(&ru2.ru_utime, &ru1.ru_utime, <) != 0)
14811be35a1SLionel Sambuc 			atf_tc_fail("user time went backwards");
14911be35a1SLionel Sambuc 	}
15011be35a1SLionel Sambuc 
15111be35a1SLionel Sambuc 	atf_tc_fail("anticipated error did not occur");
15211be35a1SLionel Sambuc }
15311be35a1SLionel Sambuc 
15411be35a1SLionel Sambuc ATF_TC(getrusage_utime_zero);
ATF_TC_HEAD(getrusage_utime_zero,tc)15511be35a1SLionel Sambuc ATF_TC_HEAD(getrusage_utime_zero, tc)
15611be35a1SLionel Sambuc {
15711be35a1SLionel Sambuc 	atf_tc_set_md_var(tc, "descr", "Test zero utime from getrusage(2)");
15811be35a1SLionel Sambuc }
15911be35a1SLionel Sambuc 
ATF_TC_BODY(getrusage_utime_zero,tc)16011be35a1SLionel Sambuc ATF_TC_BODY(getrusage_utime_zero, tc)
16111be35a1SLionel Sambuc {
16211be35a1SLionel Sambuc 	struct rusage ru;
16311be35a1SLionel Sambuc 	size_t i;
16411be35a1SLionel Sambuc 
16511be35a1SLionel Sambuc 	/*
16611be35a1SLionel Sambuc 	 * Test that getrusage(2) does not return
16711be35a1SLionel Sambuc 	 * zero user time for the calling process.
16811be35a1SLionel Sambuc 	 *
16911be35a1SLionel Sambuc 	 * See also (duplicate) PR port-amd64/41734.
17011be35a1SLionel Sambuc 	 */
17111be35a1SLionel Sambuc 	atf_tc_expect_fail("PR kern/30115");
17211be35a1SLionel Sambuc 
17311be35a1SLionel Sambuc 	for (i = 0; i < maxiter; i++) {
17411be35a1SLionel Sambuc 
17511be35a1SLionel Sambuc 		work();
17611be35a1SLionel Sambuc 
17711be35a1SLionel Sambuc 		(void)memset(&ru, 0, sizeof(struct rusage));
17811be35a1SLionel Sambuc 
17911be35a1SLionel Sambuc 		ATF_REQUIRE(getrusage(RUSAGE_SELF, &ru) == 0);
18011be35a1SLionel Sambuc 
18111be35a1SLionel Sambuc 		if (ru.ru_utime.tv_sec == 0 && ru.ru_utime.tv_usec == 0)
18211be35a1SLionel Sambuc 			atf_tc_fail("zero user time from getrusage(2)");
18311be35a1SLionel Sambuc 	}
18411be35a1SLionel Sambuc 
18511be35a1SLionel Sambuc 	atf_tc_fail("anticipated error did not occur");
18611be35a1SLionel Sambuc }
18711be35a1SLionel Sambuc 
ATF_TP_ADD_TCS(tp)18811be35a1SLionel Sambuc ATF_TP_ADD_TCS(tp)
18911be35a1SLionel Sambuc {
19011be35a1SLionel Sambuc 
19111be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, getrusage_err);
19211be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, getrusage_sig);
19311be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, getrusage_utime_back);
19411be35a1SLionel Sambuc 	ATF_TP_ADD_TC(tp, getrusage_utime_zero);
19511be35a1SLionel Sambuc 
19611be35a1SLionel Sambuc 	return atf_no_error();
19711be35a1SLionel Sambuc }
198