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