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