xref: /xnu-12377.41.6/tests/rm/coalition_info_resource_usage.c (revision bbb1b6f9e71b8cdde6e5cd6f4841f207dee3d828)
1 /*
2  * Copyright (c) 2025 Apple Inc. All rights reserved.
3  *
4  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5  *
6  * This file contains Original Code and/or Modifications of Original Code
7  * as defined in and that are subject to the Apple Public Source License
8  * Version 2.0 (the 'License'). You may not use this file except in
9  * compliance with the License. The rights granted to you under the License
10  * may not be used to create, or enable the creation or redistribution of,
11  * unlawful or unlicensed copies of an Apple operating system, or to
12  * circumvent, violate, or enable the circumvention or violation of, any
13  * terms of an Apple operating system software license agreement.
14  *
15  * Please obtain a copy of the License at
16  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17  *
18  * The Original Code and all software distributed under the License are
19  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23  * Please see the License for the specific language governing rights and
24  * limitations under the License.
25  *
26  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27  */
28 #include <darwintest.h>
29 #include <darwintest_utils.h>
30 #include <mach-o/dyld.h>
31 #include <spawn_private.h>
32 #include <sys/coalition.h>
33 #include <sys/kauth.h>
34 #include <sys/param.h>
35 #include <sys/spawn_internal.h>
36 #include <sys/sysctl.h>
37 #include <sys/types.h>
38 #include <sys/unistd.h>
39 
40 T_GLOBAL_META(T_META_NAMESPACE("xnu.rm"),
41     T_META_RADAR_COMPONENT_NAME("xnu"),
42     T_META_RADAR_COMPONENT_VERSION("rm"),
43     T_META_OWNER("m_staveleytaylor"));
44 
45 static uint64_t
create_coalition(int type)46 create_coalition(int type)
47 {
48 	uint64_t id = 0;
49 	uint32_t flags = 0;
50 	uint64_t param[2];
51 	int ret;
52 
53 	COALITION_CREATE_FLAGS_SET_TYPE(flags, type);
54 	ret = coalition_create(&id, flags);
55 	T_ASSERT_POSIX_SUCCESS(ret, "coalition_create");
56 	T_QUIET;
57 	T_ASSERT_GE(id, 0ULL, "coalition_create returned a valid id");
58 
59 	T_LOG("coalition has id %lld\n", id);
60 
61 	/* disable notifications for this coalition so launchd doesn't freak out */
62 	param[0] = id;
63 	param[1] = 0;
64 	ret = sysctlbyname("kern.coalition_notify", NULL, NULL, param, sizeof(param));
65 	T_QUIET;
66 	T_ASSERT_POSIX_SUCCESS(ret, "kern.coalition_notify");
67 
68 	return id;
69 }
70 
71 static pid_t
spawn_helper_in_coalition(char * helper_name,uint64_t coal_id)72 spawn_helper_in_coalition(char *helper_name, uint64_t coal_id)
73 {
74 	int ret;
75 	posix_spawnattr_t attr;
76 	extern char **environ;
77 	pid_t new_pid = 0;
78 	char path[PATH_MAX];
79 	uint32_t path_size = sizeof(path);
80 
81 	T_QUIET;
82 	T_ASSERT_POSIX_ZERO(_NSGetExecutablePath(path, &path_size),
83 	    "_NSGetExecutablePath");
84 	char *args[] = {path, "-n", helper_name, NULL};
85 
86 	ret = posix_spawnattr_init(&attr);
87 	T_QUIET;
88 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_init");
89 
90 	T_QUIET;
91 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_setcoalition_np");
92 	ret = posix_spawnattr_setcoalition_np(&attr, coal_id,
93 	    COALITION_TYPE_RESOURCE,
94 	    COALITION_TASKROLE_LEADER);
95 	T_QUIET;
96 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_setcoalition_np");
97 
98 	T_LOG("posix_spawn %s %s %s", args[0], args[1], args[2]);
99 	ret = posix_spawn(&new_pid, path, NULL, &attr, args, environ);
100 	T_QUIET;
101 	T_ASSERT_POSIX_ZERO(ret, "posix_spawn");
102 
103 	ret = posix_spawnattr_destroy(&attr);
104 	T_QUIET;
105 	T_ASSERT_POSIX_ZERO(ret, "posix_spawnattr_destroy");
106 	return new_pid;
107 }
108 
109 T_HELPER_DECL(qos_expense, "qos_expense")
110 {
111 	mach_timebase_info_data_t tb_info;
112 	mach_timebase_info(&tb_info);
113 
114 	T_LOG("starting busy work in child");
115 
116 	uint64_t start_ns = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
117 
118 	/* Do 500ms of busy work to pad our QoS stats */
119 	while (true) {
120 		uint64_t now_ns = clock_gettime_nsec_np(CLOCK_UPTIME_RAW);
121 		uint64_t diff_ms = (now_ns - start_ns) / (1000ULL * 1000ULL);
122 		if (diff_ms > 500) {
123 			break;
124 		}
125 	}
126 
127 	T_PASS("finished busy work in child");
128 }
129 
130 static uint64_t
get_qos_sum(uint64_t coalition_id)131 get_qos_sum(uint64_t coalition_id)
132 {
133 	struct coalition_resource_usage cru;
134 	int ret = coalition_info_resource_usage(coalition_id, &cru, sizeof(cru));
135 	T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_resource_usage");
136 
137 	uint64_t sum = 0;
138 	for (int i = 0; i < COALITION_NUM_THREAD_QOS_TYPES; i++) {
139 		sum += cru.cpu_time_eqos[i];
140 	}
141 	return sum;
142 }
143 
144 static uint64_t coalition_id;
145 
146 static void
terminate_and_reap_coalition(void)147 terminate_and_reap_coalition(void)
148 {
149 	T_LOG("coalition_terminate"); coalition_terminate(coalition_id, 0);
150 	T_LOG("coalition_reap"); coalition_reap(coalition_id, 0);
151 }
152 
153 T_DECL(coalition_info_resource_usage_qos_monotonic,
154     "Make sure CPU time QoS values are accumulated from dead tasks",
155     T_META_ASROOT(true),
156     T_META_SYSCTL_INT("kern.unrestrict_coalitions=1"),
157     T_META_TAG_VM_PREFERRED)
158 {
159 	T_SETUPBEGIN;
160 	coalition_id = create_coalition(COALITION_TYPE_RESOURCE);
161 	T_ATEND(terminate_and_reap_coalition);
162 	T_SETUPEND;
163 
164 	T_ASSERT_EQ_ULLONG(get_qos_sum(coalition_id), 0ULL, "cpu_time_eqos == 0");
165 
166 	pid_t child_pid = spawn_helper_in_coalition("qos_expense", coalition_id);
167 
168 	T_LOG("waitpid(%d)\n", child_pid);
169 	int stat;
170 	int ret = waitpid(child_pid, &stat, 0);
171 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "waitpid");
172 	T_QUIET; T_ASSERT_TRUE(WIFEXITED(stat), "child exited.");
173 	T_QUIET; T_ASSERT_EQ(WEXITSTATUS(stat), 0, "child exited cleanly.");
174 
175 	T_ASSERT_GT_ULLONG(get_qos_sum(coalition_id), 0ULL, "cpu_time_eqos > 0");
176 }
177