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