xref: /xnu-8796.141.3/tests/int_time.c (revision 1b191cb58250d0705d8a51287127505aa4bc0789)
1*1b191cb5SApple OSS Distributions #include <darwintest.h>
2*1b191cb5SApple OSS Distributions #include <darwintest_utils.h>
3*1b191cb5SApple OSS Distributions #include <errno.h>
4*1b191cb5SApple OSS Distributions #include <libproc_internal.h>
5*1b191cb5SApple OSS Distributions #include <stdio.h>
6*1b191cb5SApple OSS Distributions 
7*1b191cb5SApple OSS Distributions // waste some time.
8*1b191cb5SApple OSS Distributions static uint64_t
ackermann(uint64_t m,uint64_t n)9*1b191cb5SApple OSS Distributions ackermann(uint64_t m, uint64_t n)
10*1b191cb5SApple OSS Distributions {
11*1b191cb5SApple OSS Distributions 	if (m == 0) {
12*1b191cb5SApple OSS Distributions 		return n + 1;
13*1b191cb5SApple OSS Distributions 	} else if (n == 0) {
14*1b191cb5SApple OSS Distributions 		return ackermann(m - 1, 1);
15*1b191cb5SApple OSS Distributions 	} else {
16*1b191cb5SApple OSS Distributions 		return ackermann(m - 1, ackermann(m, n - 1));
17*1b191cb5SApple OSS Distributions 	}
18*1b191cb5SApple OSS Distributions }
19*1b191cb5SApple OSS Distributions 
20*1b191cb5SApple OSS Distributions T_DECL(int_time, "interrupt time collection")
21*1b191cb5SApple OSS Distributions {
22*1b191cb5SApple OSS Distributions 	struct proc_threadschedinfo info, info_new;
23*1b191cb5SApple OSS Distributions 
24*1b191cb5SApple OSS Distributions 	// for checking that the kernel filled in values.
25*1b191cb5SApple OSS Distributions 	info.int_time_ns = (uint64_t)-3;
26*1b191cb5SApple OSS Distributions 	info_new.int_time_ns = (uint64_t)-3;
27*1b191cb5SApple OSS Distributions 
28*1b191cb5SApple OSS Distributions 	int retval = proc_current_thread_schedinfo((void*)&info, sizeof(info));
29*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ_INT(retval, 0, "proc_current_thread_schedinfo succeeded");
30*1b191cb5SApple OSS Distributions 	T_ASSERT_NE(info.int_time_ns, (uint64_t)-3, "info.int_time_ns was filled");
31*1b191cb5SApple OSS Distributions 
32*1b191cb5SApple OSS Distributions 	printf("result: %llu\n", ackermann(3, 10));
33*1b191cb5SApple OSS Distributions 
34*1b191cb5SApple OSS Distributions 	retval = proc_current_thread_schedinfo((void*)&info_new, sizeof(info_new));
35*1b191cb5SApple OSS Distributions 	T_ASSERT_EQ_INT(retval, 0, "proc_current_thread_schedinfo succeeded (2nd time)");
36*1b191cb5SApple OSS Distributions 	T_ASSERT_NE(info_new.int_time_ns, (uint64_t)-3, "info.int_time_ns was filled (2nd time)");
37*1b191cb5SApple OSS Distributions 
38*1b191cb5SApple OSS Distributions 	int64_t duration_ns = (int64_t)info_new.int_time_ns - (int64_t)info.int_time_ns;
39*1b191cb5SApple OSS Distributions 
40*1b191cb5SApple OSS Distributions 	printf("before   : %lluns\n",
41*1b191cb5SApple OSS Distributions 	    info.int_time_ns);
42*1b191cb5SApple OSS Distributions 	printf("after    : %lluns\n",
43*1b191cb5SApple OSS Distributions 	    info_new.int_time_ns);
44*1b191cb5SApple OSS Distributions 	printf("duration : %llins\n", duration_ns);
45*1b191cb5SApple OSS Distributions 
46*1b191cb5SApple OSS Distributions 	/*
47*1b191cb5SApple OSS Distributions 	 * If time went backwards, life is missing its monotony.
48*1b191cb5SApple OSS Distributions 	 */
49*1b191cb5SApple OSS Distributions 
50*1b191cb5SApple OSS Distributions 	T_EXPECT_FALSE(duration_ns < 0, "Kernel claims time went forewards");
51*1b191cb5SApple OSS Distributions 
52*1b191cb5SApple OSS Distributions 	/*
53*1b191cb5SApple OSS Distributions 	 * If the kernel says we spent more than 10 seconds in an interrupt context, something is definitely wrong.
54*1b191cb5SApple OSS Distributions 	 */
55*1b191cb5SApple OSS Distributions 
56*1b191cb5SApple OSS Distributions 	int64_t const limit_ns = 10 * (int64_t)NSEC_PER_SEC;
57*1b191cb5SApple OSS Distributions 	T_EXPECT_FALSE(duration_ns > limit_ns, "Kernel claims we spent less than 10 seconds in interrupt context");
58*1b191cb5SApple OSS Distributions }
59