xref: /xnu-12377.61.12/tests/coalition_gpu_energy.c (revision 4d495c6e23c53686cf65f45067f79024cf5dcee8)
1 /*
2  * Copyright (c) 2024 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 
29 #include <mach/coalition.h>
30 #include <sys/coalition.h>
31 #include <libproc.h>
32 
33 #include <sys/types.h>
34 #include <unistd.h>
35 
36 #include <darwintest.h>
37 #include <darwintest_utils.h>
38 #include <test_utils.h>
39 #include <mach/task.h>
40 #include <mach/mach.h>
41 
42 T_GLOBAL_META(T_META_NAMESPACE("xnu.scheduler"),
43     T_META_RADAR_COMPONENT_NAME("xnu"),
44     T_META_RADAR_COMPONENT_VERSION("scheduler"),
45     T_META_OWNER("chimene"),
46     XNU_T_META_REQUIRES_DEVELOPMENT_KERNEL,     /* needed for development-only sysctls */
47     T_META_RUN_CONCURRENTLY(false));
48 
49 static uint64_t
get_res_id(void)50 get_res_id(void)
51 {
52 	struct proc_pidcoalitioninfo idinfo;
53 
54 	int ret = proc_pidinfo(getpid(), PROC_PIDCOALITIONINFO, 0,
55 	    &idinfo, sizeof(idinfo));
56 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "proc_pidinfo(... PROC_PIDCOALITIONINFO ...)");
57 
58 	uint64_t res_id = idinfo.coalition_id[COALITION_TYPE_RESOURCE];
59 	uint64_t jet_id = idinfo.coalition_id[COALITION_TYPE_JETSAM];
60 
61 	T_LOG("Resource coalition: %lld, Jetsam coalition: %lld", res_id, jet_id);
62 
63 	return res_id;
64 }
65 
66 static uint64_t
get_energy_id(void)67 get_energy_id(void)
68 {
69 	int ret;
70 	uint64_t out_val = 0;
71 	size_t out_size = sizeof(out_val);
72 
73 	uint64_t param[4] = {
74 		1,
75 	};
76 
77 	ret = sysctlbyname("kern.coalition_gpu_energy_test",
78 	    &out_val, &out_size, param, sizeof(param));
79 
80 	T_ASSERT_POSIX_SUCCESS(ret, "get_energy_id() = %lld", out_val);
81 
82 	return out_val;
83 }
84 
85 static int
test_task_id_token_to_energy_id(mach_port_name_t name,uint64_t * energy_id_out)86 test_task_id_token_to_energy_id(mach_port_name_t name, uint64_t *energy_id_out)
87 {
88 	int ret;
89 	uint64_t out_val = 0;
90 	size_t out_size = sizeof(out_val);
91 
92 	uint64_t param[4] = {
93 		2,
94 		name,
95 	};
96 
97 	ret = sysctlbyname("kern.coalition_gpu_energy_test",
98 	    &out_val, &out_size, param, sizeof(param));
99 
100 	*energy_id_out = out_val;
101 
102 	return ret;
103 }
104 
105 static int
test_energy_id_report_energy(uint64_t self_id,uint64_t on_behalf_of_id,uint64_t energy)106 test_energy_id_report_energy(uint64_t self_id, uint64_t on_behalf_of_id, uint64_t energy)
107 {
108 	uint64_t out_val = 0;
109 	size_t out_size = sizeof(out_val);
110 
111 	uint64_t param[4] = {
112 		3,
113 		self_id,
114 		on_behalf_of_id,
115 		energy,
116 	};
117 
118 	return sysctlbyname("kern.coalition_gpu_energy_test",
119 	           &out_val, &out_size, param, sizeof(param));
120 }
121 
122 
123 T_DECL(current_energy_id, "current_energy_id returns res id")
124 {
125 	uint64_t res_id = get_res_id();
126 	uint64_t energy_id = get_energy_id();
127 
128 	T_ASSERT_EQ(energy_id, res_id, "energy id is res id");
129 }
130 
131 
132 T_DECL(task_id_token_to_energy_id, "task_id_token_to_energy_id looks up energy id")
133 {
134 	uint64_t energy_id = get_energy_id();
135 
136 	uint64_t energy_id_out = 0;
137 
138 	task_id_token_t token;
139 
140 	kern_return_t kr = task_create_identity_token(mach_task_self(), &token);
141 	T_ASSERT_MACH_SUCCESS(kr, "task_create_identity_token() = 0x%x", token);
142 
143 	int ret = test_task_id_token_to_energy_id(token, &energy_id_out);
144 	T_ASSERT_POSIX_SUCCESS(ret, "task_id_token_to_energy_id(0x%x) = %lld", token, energy_id_out);
145 
146 	T_EXPECT_EQ(energy_id_out, energy_id, "token energy id is self energy id");
147 
148 	kr = mach_port_deallocate(mach_task_self(), token);
149 	T_ASSERT_MACH_SUCCESS(kr, "mach_port_deallocate(0x%x)", token);
150 }
151 
152 
153 T_DECL(energy_id_report_energy_time_bad_value, "energy_id_report_energy_time handles bad value")
154 {
155 	int ret = test_energy_id_report_energy((uint64_t)-1, 0, 1);
156 	T_ASSERT_POSIX_FAILURE(ret, ENOENT, "energy_id_report_energy(-1) = ENOENT");
157 
158 	ret = test_energy_id_report_energy(0, 0, 1);
159 	T_ASSERT_POSIX_FAILURE(ret, EINVAL, "energy_id_report_energy(0) = EINVAL");
160 
161 	uint64_t res_id = get_res_id();
162 
163 	/* It ignores bad on_behalf_of_ids */
164 	ret = test_energy_id_report_energy(res_id, (uint64_t)-1, 1);
165 	T_ASSERT_POSIX_SUCCESS(ret, "energy_id_report_energy(%lld, -1)", res_id);
166 }
167 
168 
169 T_DECL(energy_id_report_energy_time_self, "energy_id_report_energy_time increments gpu energy for self")
170 {
171 	uint64_t res_id = get_res_id();
172 	uint64_t energy_id = get_energy_id();
173 
174 	struct coalition_resource_usage coalusage_1 = {0};
175 	int ret = coalition_info_resource_usage(res_id, &coalusage_1, sizeof(coalusage_1));
176 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_resource_usage() 1");
177 
178 	T_LOG("before: gpu_energy_nj: %lld, gpu_energy_nj_billed_to_me: %lld, gpu_energy_nj_billed_to_others: %lld",
179 	    coalusage_1.gpu_energy_nj, coalusage_1.gpu_energy_nj_billed_to_me,
180 	    coalusage_1.gpu_energy_nj_billed_to_others);
181 
182 	ret = test_energy_id_report_energy(energy_id, 0, 1);
183 	T_ASSERT_POSIX_SUCCESS(ret, "energy_id_report_energy(%lld, 0, 1)", energy_id);
184 
185 	struct coalition_resource_usage coalusage_2 = {0};
186 	ret = coalition_info_resource_usage(res_id, &coalusage_2, sizeof(coalusage_2));
187 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_resource_usage() 2");
188 
189 	T_LOG("after: gpu_energy_nj: %lld, gpu_energy_nj_billed_to_me: %lld, gpu_energy_nj_billed_to_others: %lld",
190 	    coalusage_2.gpu_energy_nj, coalusage_2.gpu_energy_nj_billed_to_me,
191 	    coalusage_2.gpu_energy_nj_billed_to_others);
192 
193 	T_EXPECT_LT(coalusage_1.gpu_energy_nj, coalusage_2.gpu_energy_nj,
194 	    "gpu_energy_nj should have increased");
195 }
196 
197 
198 T_DECL(energy_id_report_energy_time_billed, "energy_id_report_energy_time increments billed energy")
199 {
200 	uint64_t res_id = get_res_id();
201 	uint64_t energy_id = get_energy_id();
202 
203 	struct coalition_resource_usage coalusage_1 = {0};
204 	int ret = coalition_info_resource_usage(res_id, &coalusage_1, sizeof(coalusage_1));
205 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_resource_usage() 1");
206 
207 	T_LOG("before: gpu_energy_nj: %lld, gpu_energy_nj_billed_to_me: %lld, gpu_energy_nj_billed_to_others: %lld",
208 	    coalusage_1.gpu_energy_nj, coalusage_1.gpu_energy_nj_billed_to_me,
209 	    coalusage_1.gpu_energy_nj_billed_to_others);
210 
211 	ret = test_energy_id_report_energy(energy_id, energy_id, 1);
212 	T_ASSERT_POSIX_SUCCESS(ret, "energy_id_report_energy(%lld, %lld, 1)", energy_id, energy_id);
213 
214 	struct coalition_resource_usage coalusage_2 = {0};
215 	ret = coalition_info_resource_usage(res_id, &coalusage_2, sizeof(coalusage_2));
216 	T_QUIET; T_ASSERT_POSIX_SUCCESS(ret, "coalition_info_resource_usage() 2");
217 
218 	T_LOG("after: gpu_energy_nj: %lld, gpu_energy_nj_billed_to_me: %lld, gpu_energy_nj_billed_to_others: %lld",
219 	    coalusage_2.gpu_energy_nj, coalusage_2.gpu_energy_nj_billed_to_me,
220 	    coalusage_2.gpu_energy_nj_billed_to_others);
221 
222 	T_EXPECT_LT(coalusage_1.gpu_energy_nj, coalusage_2.gpu_energy_nj,
223 	    "gpu_energy_nj should have increased");
224 	T_EXPECT_LT(coalusage_1.gpu_energy_nj_billed_to_me, coalusage_2.gpu_energy_nj_billed_to_me,
225 	    "gpu_energy_nj_billed_to_me should have increased");
226 	T_EXPECT_LT(coalusage_1.gpu_energy_nj_billed_to_others, coalusage_2.gpu_energy_nj_billed_to_others,
227 	    "gpu_energy_nj_billed_to_others should have increased");
228 }
229