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