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