xref: /xnu-11417.140.69/osfmk/vm/analytics.c (revision 43a90889846e00bfb5cf1d255cdc0a701a1e05a4)
1*43a90889SApple OSS Distributions /*
2*43a90889SApple OSS Distributions  * Copyright (c) 2000-2021 Apple Inc. All rights reserved.
3*43a90889SApple OSS Distributions  *
4*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*43a90889SApple OSS Distributions  *
6*43a90889SApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*43a90889SApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*43a90889SApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*43a90889SApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*43a90889SApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*43a90889SApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*43a90889SApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*43a90889SApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*43a90889SApple OSS Distributions  *
15*43a90889SApple OSS Distributions  * Please obtain a copy of the License at
16*43a90889SApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*43a90889SApple OSS Distributions  *
18*43a90889SApple OSS Distributions  * The Original Code and all software distributed under the License are
19*43a90889SApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*43a90889SApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*43a90889SApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*43a90889SApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*43a90889SApple OSS Distributions  * Please see the License for the specific language governing rights and
24*43a90889SApple OSS Distributions  * limitations under the License.
25*43a90889SApple OSS Distributions  *
26*43a90889SApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*43a90889SApple OSS Distributions  */
28*43a90889SApple OSS Distributions 
29*43a90889SApple OSS Distributions /*
30*43a90889SApple OSS Distributions  * Telemetry from the VM is usually colected at a daily cadence.
31*43a90889SApple OSS Distributions  * All of those events are in this file along with a single thread
32*43a90889SApple OSS Distributions  * call for reporting them.
33*43a90889SApple OSS Distributions  *
34*43a90889SApple OSS Distributions  * NB: The freezer subsystem has its own telemetry based on its budget interval
35*43a90889SApple OSS Distributions  * so it's not included here.
36*43a90889SApple OSS Distributions  */
37*43a90889SApple OSS Distributions 
38*43a90889SApple OSS Distributions #include <kern/thread_call.h>
39*43a90889SApple OSS Distributions #include <libkern/coreanalytics/coreanalytics.h>
40*43a90889SApple OSS Distributions #include <os/log.h>
41*43a90889SApple OSS Distributions #include <vm/vm_page.h>
42*43a90889SApple OSS Distributions #include <vm/vm_compressor_internal.h>
43*43a90889SApple OSS Distributions #if CONFIG_EXCLAVES
44*43a90889SApple OSS Distributions #include <kern/exclaves_memory.h>
45*43a90889SApple OSS Distributions #endif /* CONFIG_EXCLAVES */
46*43a90889SApple OSS Distributions 
47*43a90889SApple OSS Distributions #include "vm_compressor_backing_store_internal.h"
48*43a90889SApple OSS Distributions 
49*43a90889SApple OSS Distributions void vm_analytics_tick(void *arg0, void *arg1);
50*43a90889SApple OSS Distributions 
51*43a90889SApple OSS Distributions #define ANALYTICS_PERIOD_HOURS (24ULL)
52*43a90889SApple OSS Distributions 
53*43a90889SApple OSS Distributions static thread_call_t vm_analytics_thread_call;
54*43a90889SApple OSS Distributions 
55*43a90889SApple OSS Distributions CA_EVENT(vm_swapusage,
56*43a90889SApple OSS Distributions     CA_INT, max_alloced,
57*43a90889SApple OSS Distributions     CA_INT, max_used,
58*43a90889SApple OSS Distributions     CA_INT, trial_deployment_id,
59*43a90889SApple OSS Distributions     CA_STATIC_STRING(CA_UUID_LEN), trial_treatment_id,
60*43a90889SApple OSS Distributions     CA_STATIC_STRING(CA_UUID_LEN), trial_experiment_id);
61*43a90889SApple OSS Distributions 
62*43a90889SApple OSS Distributions CA_EVENT(mlock_failures,
63*43a90889SApple OSS Distributions     CA_INT, over_global_limit,
64*43a90889SApple OSS Distributions     CA_INT, over_user_limit,
65*43a90889SApple OSS Distributions     CA_INT, trial_deployment_id,
66*43a90889SApple OSS Distributions     CA_STATIC_STRING(CA_UUID_LEN), trial_treatment_id,
67*43a90889SApple OSS Distributions     CA_STATIC_STRING(CA_UUID_LEN), trial_experiment_id);
68*43a90889SApple OSS Distributions 
69*43a90889SApple OSS Distributions /*
70*43a90889SApple OSS Distributions  * NB: It's a good practice to include these trial
71*43a90889SApple OSS Distributions  * identifiers in all of our events so that we can
72*43a90889SApple OSS Distributions  * measure the impact of any A/B tests on these metrics.
73*43a90889SApple OSS Distributions  */
74*43a90889SApple OSS Distributions extern uuid_string_t trial_treatment_id;
75*43a90889SApple OSS Distributions extern uuid_string_t trial_experiment_id;
76*43a90889SApple OSS Distributions extern int trial_deployment_id;
77*43a90889SApple OSS Distributions 
78*43a90889SApple OSS Distributions static void
add_trial_uuids(char * treatment_id,char * experiment_id)79*43a90889SApple OSS Distributions add_trial_uuids(char *treatment_id, char *experiment_id)
80*43a90889SApple OSS Distributions {
81*43a90889SApple OSS Distributions 	strlcpy(treatment_id, trial_treatment_id, CA_UUID_LEN);
82*43a90889SApple OSS Distributions 	strlcpy(experiment_id, trial_experiment_id, CA_UUID_LEN);
83*43a90889SApple OSS Distributions }
84*43a90889SApple OSS Distributions 
85*43a90889SApple OSS Distributions static void
report_vm_swapusage(void)86*43a90889SApple OSS Distributions report_vm_swapusage(void)
87*43a90889SApple OSS Distributions {
88*43a90889SApple OSS Distributions 	uint64_t max_alloced, max_used;
89*43a90889SApple OSS Distributions 	ca_event_t event = CA_EVENT_ALLOCATE(vm_swapusage);
90*43a90889SApple OSS Distributions 	CA_EVENT_TYPE(vm_swapusage) * e = event->data;
91*43a90889SApple OSS Distributions 
92*43a90889SApple OSS Distributions 	vm_swap_reset_max_segs_tracking(&max_alloced, &max_used);
93*43a90889SApple OSS Distributions 	e->max_alloced = max_alloced;
94*43a90889SApple OSS Distributions 	e->max_used = max_used;
95*43a90889SApple OSS Distributions 	add_trial_uuids(e->trial_treatment_id, e->trial_experiment_id);
96*43a90889SApple OSS Distributions 	e->trial_deployment_id = trial_deployment_id;
97*43a90889SApple OSS Distributions 	CA_EVENT_SEND(event);
98*43a90889SApple OSS Distributions }
99*43a90889SApple OSS Distributions 
100*43a90889SApple OSS Distributions static void
report_mlock_failures(void)101*43a90889SApple OSS Distributions report_mlock_failures(void)
102*43a90889SApple OSS Distributions {
103*43a90889SApple OSS Distributions 	ca_event_t event = CA_EVENT_ALLOCATE(mlock_failures);
104*43a90889SApple OSS Distributions 	CA_EVENT_TYPE(mlock_failures) * e = event->data;
105*43a90889SApple OSS Distributions 
106*43a90889SApple OSS Distributions 	e->over_global_limit = os_atomic_load_wide(&vm_add_wire_count_over_global_limit, relaxed);
107*43a90889SApple OSS Distributions 	e->over_user_limit = os_atomic_load_wide(&vm_add_wire_count_over_user_limit, relaxed);
108*43a90889SApple OSS Distributions 
109*43a90889SApple OSS Distributions 	os_atomic_store_wide(&vm_add_wire_count_over_global_limit, 0, relaxed);
110*43a90889SApple OSS Distributions 	os_atomic_store_wide(&vm_add_wire_count_over_user_limit, 0, relaxed);
111*43a90889SApple OSS Distributions 
112*43a90889SApple OSS Distributions 	add_trial_uuids(e->trial_treatment_id, e->trial_experiment_id);
113*43a90889SApple OSS Distributions 	e->trial_deployment_id = trial_deployment_id;
114*43a90889SApple OSS Distributions 	CA_EVENT_SEND(event);
115*43a90889SApple OSS Distributions }
116*43a90889SApple OSS Distributions 
117*43a90889SApple OSS Distributions #if XNU_TARGET_OS_WATCH
118*43a90889SApple OSS Distributions CA_EVENT(compressor_age,
119*43a90889SApple OSS Distributions     CA_INT, hour1,
120*43a90889SApple OSS Distributions     CA_INT, hour6,
121*43a90889SApple OSS Distributions     CA_INT, hour12,
122*43a90889SApple OSS Distributions     CA_INT, hour24,
123*43a90889SApple OSS Distributions     CA_INT, hour36,
124*43a90889SApple OSS Distributions     CA_INT, hour48,
125*43a90889SApple OSS Distributions     CA_INT, hourMax,
126*43a90889SApple OSS Distributions     CA_INT, trial_deployment_id,
127*43a90889SApple OSS Distributions     CA_STATIC_STRING(CA_UUID_LEN), trial_treatment_id,
128*43a90889SApple OSS Distributions     CA_STATIC_STRING(CA_UUID_LEN), trial_experiment_id);
129*43a90889SApple OSS Distributions 
130*43a90889SApple OSS Distributions /**
131*43a90889SApple OSS Distributions  * Compressor age bucket descriptor.
132*43a90889SApple OSS Distributions  */
133*43a90889SApple OSS Distributions typedef struct {
134*43a90889SApple OSS Distributions 	/* Number of segments in this bucket. */
135*43a90889SApple OSS Distributions 	uint64_t count;
136*43a90889SApple OSS Distributions 	/* The bucket's lower bound (inclusive) */
137*43a90889SApple OSS Distributions 	uint64_t lower;
138*43a90889SApple OSS Distributions 	/* The bucket's upper bound (exclusive) */
139*43a90889SApple OSS Distributions 	uint64_t upper;
140*43a90889SApple OSS Distributions } c_reporting_bucket_t;
141*43a90889SApple OSS Distributions #define C_REPORTING_BUCKETS_MAX (UINT64_MAX)
142*43a90889SApple OSS Distributions #ifndef ARRAY_SIZE
143*43a90889SApple OSS Distributions #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
144*43a90889SApple OSS Distributions #endif
145*43a90889SApple OSS Distributions #define HR_TO_S(x) ((x) * 60 * 60)
146*43a90889SApple OSS Distributions 
147*43a90889SApple OSS Distributions /**
148*43a90889SApple OSS Distributions  * Report the age of segments in the compressor.
149*43a90889SApple OSS Distributions  */
150*43a90889SApple OSS Distributions static void
report_compressor_age(void)151*43a90889SApple OSS Distributions report_compressor_age(void)
152*43a90889SApple OSS Distributions {
153*43a90889SApple OSS Distributions 	/* If the compressor is not configured, do nothing and return early. */
154*43a90889SApple OSS Distributions 	if (vm_compressor_mode == VM_PAGER_NOT_CONFIGURED) {
155*43a90889SApple OSS Distributions 		os_log(OS_LOG_DEFAULT, "%s: vm_compressor_mode == VM_PAGER_NOT_CONFIGURED, returning early", __func__);
156*43a90889SApple OSS Distributions 		return;
157*43a90889SApple OSS Distributions 	}
158*43a90889SApple OSS Distributions 
159*43a90889SApple OSS Distributions 	const queue_head_t *c_queues[] = {&c_age_list_head, &c_major_list_head};
160*43a90889SApple OSS Distributions 	c_reporting_bucket_t c_buckets[] = {
161*43a90889SApple OSS Distributions 		{.count = 0, .lower = HR_TO_S(0), .upper = HR_TO_S(1)},  /* [0, 1) hours */
162*43a90889SApple OSS Distributions 		{.count = 0, .lower = HR_TO_S(1), .upper = HR_TO_S(6)},  /* [1, 6) hours */
163*43a90889SApple OSS Distributions 		{.count = 0, .lower = HR_TO_S(6), .upper = HR_TO_S(12)},  /* [6, 12) hours */
164*43a90889SApple OSS Distributions 		{.count = 0, .lower = HR_TO_S(12), .upper = HR_TO_S(24)}, /* [12, 24) hours */
165*43a90889SApple OSS Distributions 		{.count = 0, .lower = HR_TO_S(24), .upper = HR_TO_S(36)}, /* [24, 36) hours */
166*43a90889SApple OSS Distributions 		{.count = 0, .lower = HR_TO_S(36), .upper = HR_TO_S(48)}, /* [36, 48) hours */
167*43a90889SApple OSS Distributions 		{.count = 0, .lower = HR_TO_S(48), .upper = C_REPORTING_BUCKETS_MAX}, /* [48, MAX) hours */
168*43a90889SApple OSS Distributions 	};
169*43a90889SApple OSS Distributions 	clock_sec_t now;
170*43a90889SApple OSS Distributions 	clock_nsec_t nsec;
171*43a90889SApple OSS Distributions 
172*43a90889SApple OSS Distributions 	/* Collect the segments and update the bucket counts. */
173*43a90889SApple OSS Distributions 	lck_mtx_lock_spin_always(c_list_lock);
174*43a90889SApple OSS Distributions 	for (unsigned q = 0; q < ARRAY_SIZE(c_queues); q++) {
175*43a90889SApple OSS Distributions 		c_segment_t c_seg = (c_segment_t) queue_first(c_queues[q]);
176*43a90889SApple OSS Distributions 		while (!queue_end(c_queues[q], (queue_entry_t) c_seg)) {
177*43a90889SApple OSS Distributions 			for (unsigned b = 0; b < ARRAY_SIZE(c_buckets); b++) {
178*43a90889SApple OSS Distributions 				uint32_t creation_ts = c_seg->c_creation_ts;
179*43a90889SApple OSS Distributions 				clock_get_system_nanotime(&now, &nsec);
180*43a90889SApple OSS Distributions 				clock_sec_t age = now - creation_ts;
181*43a90889SApple OSS Distributions 				if ((age >= c_buckets[b].lower) &&
182*43a90889SApple OSS Distributions 				    (age < c_buckets[b].upper)) {
183*43a90889SApple OSS Distributions 					c_buckets[b].count++;
184*43a90889SApple OSS Distributions 					break;
185*43a90889SApple OSS Distributions 				}
186*43a90889SApple OSS Distributions 			}
187*43a90889SApple OSS Distributions 			c_seg = (c_segment_t) queue_next(&c_seg->c_age_list);
188*43a90889SApple OSS Distributions 		}
189*43a90889SApple OSS Distributions 	}
190*43a90889SApple OSS Distributions 	lck_mtx_unlock_always(c_list_lock);
191*43a90889SApple OSS Distributions 
192*43a90889SApple OSS Distributions 	/* Send the ages to CoreAnalytics. */
193*43a90889SApple OSS Distributions 	ca_event_t event = CA_EVENT_ALLOCATE(compressor_age);
194*43a90889SApple OSS Distributions 	CA_EVENT_TYPE(compressor_age) * e = event->data;
195*43a90889SApple OSS Distributions 	e->hour1 = c_buckets[0].count;
196*43a90889SApple OSS Distributions 	e->hour6 = c_buckets[1].count;
197*43a90889SApple OSS Distributions 	e->hour12 = c_buckets[2].count;
198*43a90889SApple OSS Distributions 	e->hour24 = c_buckets[3].count;
199*43a90889SApple OSS Distributions 	e->hour36 = c_buckets[4].count;
200*43a90889SApple OSS Distributions 	e->hour48 = c_buckets[5].count;
201*43a90889SApple OSS Distributions 	e->hourMax = c_buckets[6].count;
202*43a90889SApple OSS Distributions 	add_trial_uuids(e->trial_treatment_id, e->trial_experiment_id);
203*43a90889SApple OSS Distributions 	e->trial_deployment_id = trial_deployment_id;
204*43a90889SApple OSS Distributions 	CA_EVENT_SEND(event);
205*43a90889SApple OSS Distributions }
206*43a90889SApple OSS Distributions #endif /* XNU_TARGET_OS_WATCH */
207*43a90889SApple OSS Distributions 
208*43a90889SApple OSS Distributions 
209*43a90889SApple OSS Distributions extern uint64_t max_mem;
210*43a90889SApple OSS Distributions CA_EVENT(accounting_health, CA_INT, percentage);
211*43a90889SApple OSS Distributions /**
212*43a90889SApple OSS Distributions  * Report health of resident vm page accounting.
213*43a90889SApple OSS Distributions  */
214*43a90889SApple OSS Distributions static void
report_accounting_health(void)215*43a90889SApple OSS Distributions report_accounting_health(void)
216*43a90889SApple OSS Distributions {
217*43a90889SApple OSS Distributions 	/**
218*43a90889SApple OSS Distributions 	 * @note If a new accounting bucket is added, it must also be added in
219*43a90889SApple OSS Distributions 	 * MemoryMaintenance sysstatuscheck, which panics when accounting reaches
220*43a90889SApple OSS Distributions 	 * unhealthy levels.
221*43a90889SApple OSS Distributions 	 */
222*43a90889SApple OSS Distributions 	int64_t pages = (vm_page_wire_count
223*43a90889SApple OSS Distributions 	    + vm_page_free_count
224*43a90889SApple OSS Distributions 	    + vm_page_inactive_count
225*43a90889SApple OSS Distributions 	    + vm_page_active_count
226*43a90889SApple OSS Distributions 	    + VM_PAGE_COMPRESSOR_COUNT
227*43a90889SApple OSS Distributions 	    + vm_page_speculative_count
228*43a90889SApple OSS Distributions #if CONFIG_SECLUDED_MEMORY
229*43a90889SApple OSS Distributions 	    + vm_page_secluded_count
230*43a90889SApple OSS Distributions #endif /* CONFIG_SECLUDED_MEMORY */
231*43a90889SApple OSS Distributions 	    );
232*43a90889SApple OSS Distributions 	int64_t percentage = (pages * 100) / (max_mem >> PAGE_SHIFT);
233*43a90889SApple OSS Distributions 
234*43a90889SApple OSS Distributions 	/* Send the percentage health to CoreAnalytics. */
235*43a90889SApple OSS Distributions 	ca_event_t event = CA_EVENT_ALLOCATE(accounting_health);
236*43a90889SApple OSS Distributions 	CA_EVENT_TYPE(accounting_health) * e = event->data;
237*43a90889SApple OSS Distributions 	e->percentage = percentage;
238*43a90889SApple OSS Distributions 	CA_EVENT_SEND(event);
239*43a90889SApple OSS Distributions }
240*43a90889SApple OSS Distributions 
241*43a90889SApple OSS Distributions static void
schedule_analytics_thread_call(void)242*43a90889SApple OSS Distributions schedule_analytics_thread_call(void)
243*43a90889SApple OSS Distributions {
244*43a90889SApple OSS Distributions 	static const uint64_t analytics_period_ns = ANALYTICS_PERIOD_HOURS * 60 * 60 * NSEC_PER_SEC;
245*43a90889SApple OSS Distributions 	uint64_t analytics_period_absolutetime;
246*43a90889SApple OSS Distributions 	nanoseconds_to_absolutetime(analytics_period_ns, &analytics_period_absolutetime);
247*43a90889SApple OSS Distributions 
248*43a90889SApple OSS Distributions 	thread_call_enter_delayed(vm_analytics_thread_call, analytics_period_absolutetime + mach_absolute_time());
249*43a90889SApple OSS Distributions }
250*43a90889SApple OSS Distributions 
251*43a90889SApple OSS Distributions /*
252*43a90889SApple OSS Distributions  * This is the main entry point for reporting periodic analytics.
253*43a90889SApple OSS Distributions  * It's called once every ANALYTICS_PERIOD_HOURS hours.
254*43a90889SApple OSS Distributions  */
255*43a90889SApple OSS Distributions void
vm_analytics_tick(void * arg0,void * arg1)256*43a90889SApple OSS Distributions vm_analytics_tick(void *arg0, void *arg1)
257*43a90889SApple OSS Distributions {
258*43a90889SApple OSS Distributions #pragma unused(arg0, arg1)
259*43a90889SApple OSS Distributions 	report_vm_swapusage();
260*43a90889SApple OSS Distributions 	report_mlock_failures();
261*43a90889SApple OSS Distributions #if XNU_TARGET_OS_WATCH
262*43a90889SApple OSS Distributions 	report_compressor_age();
263*43a90889SApple OSS Distributions #endif /* XNU_TARGET_OS_WATCH */
264*43a90889SApple OSS Distributions 	report_accounting_health();
265*43a90889SApple OSS Distributions #if CONFIG_EXCLAVES
266*43a90889SApple OSS Distributions 	exclaves_memory_report_accounting();
267*43a90889SApple OSS Distributions #endif /* CONFIG_EXCLAVES */
268*43a90889SApple OSS Distributions 	schedule_analytics_thread_call();
269*43a90889SApple OSS Distributions }
270*43a90889SApple OSS Distributions 
271*43a90889SApple OSS Distributions static void
vm_analytics_init(void)272*43a90889SApple OSS Distributions vm_analytics_init(void)
273*43a90889SApple OSS Distributions {
274*43a90889SApple OSS Distributions 	vm_analytics_thread_call = thread_call_allocate_with_options(vm_analytics_tick, NULL, THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
275*43a90889SApple OSS Distributions 	schedule_analytics_thread_call();
276*43a90889SApple OSS Distributions }
277*43a90889SApple OSS Distributions 
278*43a90889SApple OSS Distributions STARTUP(THREAD_CALL, STARTUP_RANK_MIDDLE, vm_analytics_init);
279