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