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 <libkern/coreanalytics/coreanalytics.h>
39 #include "vm_compressor_backing_store.h"
40 #include <vm/vm_page.h>
41 #include <kern/thread_call.h>
42
43 void vm_analytics_tick(void *arg0, void *arg1);
44
45 #define ANALYTICS_PERIOD_HOURS (24ULL)
46
47 static thread_call_t vm_analytics_thread_call;
48
49 CA_EVENT(vm_swapusage,
50 CA_INT, max_alloced,
51 CA_INT, max_used,
52 CA_INT, trial_deployment_id,
53 CA_STATIC_STRING(CA_UUID_LEN), trial_treatment_id,
54 CA_STATIC_STRING(CA_UUID_LEN), trial_experiment_id);
55
56 CA_EVENT(mlock_failures,
57 CA_INT, over_global_limit,
58 CA_INT, over_user_limit,
59 CA_INT, trial_deployment_id,
60 CA_STATIC_STRING(CA_UUID_LEN), trial_treatment_id,
61 CA_STATIC_STRING(CA_UUID_LEN), trial_experiment_id);
62
63 /*
64 * NB: It's a good practice to include these trial
65 * identifiers in all of our events so that we can
66 * measure the impact of any A/B tests on these metrics.
67 */
68 extern uuid_string_t trial_treatment_id;
69 extern uuid_string_t trial_experiment_id;
70 extern int trial_deployment_id;
71
72 static void
add_trial_uuids(char * treatment_id,char * experiment_id)73 add_trial_uuids(char *treatment_id, char *experiment_id)
74 {
75 strlcpy(treatment_id, trial_treatment_id, CA_UUID_LEN);
76 strlcpy(experiment_id, trial_experiment_id, CA_UUID_LEN);
77 }
78
79 static void
report_vm_swapusage()80 report_vm_swapusage()
81 {
82 uint64_t max_alloced, max_used;
83 ca_event_t event = CA_EVENT_ALLOCATE(vm_swapusage);
84 CA_EVENT_TYPE(vm_swapusage) * e = event->data;
85
86 vm_swap_reset_max_segs_tracking(&max_alloced, &max_used);
87 e->max_alloced = max_alloced;
88 e->max_used = max_used;
89 add_trial_uuids(e->trial_treatment_id, e->trial_experiment_id);
90 e->trial_deployment_id = trial_deployment_id;
91 CA_EVENT_SEND(event);
92 }
93
94 static void
report_mlock_failures()95 report_mlock_failures()
96 {
97 ca_event_t event = CA_EVENT_ALLOCATE(mlock_failures);
98 CA_EVENT_TYPE(mlock_failures) * e = event->data;
99
100 e->over_global_limit = os_atomic_load_wide(&vm_add_wire_count_over_global_limit, relaxed);
101 e->over_user_limit = os_atomic_load_wide(&vm_add_wire_count_over_user_limit, relaxed);
102
103 os_atomic_store_wide(&vm_add_wire_count_over_global_limit, 0, relaxed);
104 os_atomic_store_wide(&vm_add_wire_count_over_user_limit, 0, relaxed);
105
106 add_trial_uuids(e->trial_treatment_id, e->trial_experiment_id);
107 e->trial_deployment_id = trial_deployment_id;
108 CA_EVENT_SEND(event);
109 }
110
111 #if XNU_TARGET_OS_WATCH
112 CA_EVENT(compressor_age,
113 CA_INT, hour1,
114 CA_INT, hour6,
115 CA_INT, hour12,
116 CA_INT, hour24,
117 CA_INT, hour36,
118 CA_INT, hour48,
119 CA_INT, hourMax,
120 CA_INT, trial_deployment_id,
121 CA_STATIC_STRING(CA_UUID_LEN), trial_treatment_id,
122 CA_STATIC_STRING(CA_UUID_LEN), trial_experiment_id);
123
124 /**
125 * Compressor age bucket descriptor.
126 */
127 typedef struct {
128 /* Number of segments in this bucket. */
129 uint64_t count;
130 /* The bucket's lower bound (inclusive) */
131 uint64_t lower;
132 /* The bucket's upper bound (exclusive) */
133 uint64_t upper;
134 } c_reporting_bucket_t;
135 #define C_REPORTING_BUCKETS_MAX (UINT64_MAX)
136 #ifndef ARRAY_SIZE
137 #define ARRAY_SIZE(x) (sizeof(x) / sizeof(x[0]))
138 #endif
139 #define HR_TO_S(x) ((x) * 60 * 60)
140
141 /**
142 * Report the age of segments in the compressor.
143 */
144 static void
report_compressor_age()145 report_compressor_age()
146 {
147 const queue_head_t *c_queues[] = {&c_age_list_head, &c_major_list_head};
148 c_reporting_bucket_t c_buckets[] = {
149 {.count = 0, .lower = HR_TO_S(0), .upper = HR_TO_S(1)}, /* [0, 1) hours */
150 {.count = 0, .lower = HR_TO_S(1), .upper = HR_TO_S(6)}, /* [1, 6) hours */
151 {.count = 0, .lower = HR_TO_S(6), .upper = HR_TO_S(12)}, /* [6, 12) hours */
152 {.count = 0, .lower = HR_TO_S(12), .upper = HR_TO_S(24)}, /* [12, 24) hours */
153 {.count = 0, .lower = HR_TO_S(24), .upper = HR_TO_S(36)}, /* [24, 36) hours */
154 {.count = 0, .lower = HR_TO_S(36), .upper = HR_TO_S(48)}, /* [36, 48) hours */
155 {.count = 0, .lower = HR_TO_S(48), .upper = C_REPORTING_BUCKETS_MAX}, /* [48, MAX) hours */
156 };
157 clock_sec_t now;
158 clock_nsec_t nsec;
159
160 /* Collect the segments and update the bucket counts. */
161 lck_mtx_lock_spin_always(c_list_lock);
162 for (unsigned q = 0; q < ARRAY_SIZE(c_queues); q++) {
163 c_segment_t c_seg = (c_segment_t) queue_first(c_queues[q]);
164 while (!queue_end(c_queues[q], (queue_entry_t) c_seg)) {
165 for (unsigned b = 0; b < ARRAY_SIZE(c_buckets); b++) {
166 uint32_t creation_ts = c_seg->c_creation_ts;
167 clock_get_system_nanotime(&now, &nsec);
168 clock_sec_t age = now - creation_ts;
169 if ((age >= c_buckets[b].lower) &&
170 (age < c_buckets[b].upper)) {
171 c_buckets[b].count++;
172 break;
173 }
174 }
175 c_seg = (c_segment_t) queue_next(&c_seg->c_age_list);
176 }
177 }
178 lck_mtx_unlock_always(c_list_lock);
179
180 /* Send the ages to CoreAnalytics. */
181 ca_event_t event = CA_EVENT_ALLOCATE(compressor_age);
182 CA_EVENT_TYPE(compressor_age) * e = event->data;
183 e->hour1 = c_buckets[0].count;
184 e->hour6 = c_buckets[1].count;
185 e->hour12 = c_buckets[2].count;
186 e->hour24 = c_buckets[3].count;
187 e->hour36 = c_buckets[4].count;
188 e->hour48 = c_buckets[5].count;
189 e->hourMax = c_buckets[6].count;
190 add_trial_uuids(e->trial_treatment_id, e->trial_experiment_id);
191 e->trial_deployment_id = trial_deployment_id;
192 CA_EVENT_SEND(event);
193 }
194 #endif /* XNU_TARGET_OS_WATCH */
195
196 static void
schedule_analytics_thread_call()197 schedule_analytics_thread_call()
198 {
199 static const uint64_t analytics_period_ns = ANALYTICS_PERIOD_HOURS * 60 * 60 * NSEC_PER_SEC;
200 uint64_t analytics_period_absolutetime;
201 nanoseconds_to_absolutetime(analytics_period_ns, &analytics_period_absolutetime);
202
203 thread_call_enter_delayed(vm_analytics_thread_call, analytics_period_absolutetime + mach_absolute_time());
204 }
205
206 /*
207 * This is the main entry point for reporting periodic analytics.
208 * It's called once every ANALYTICS_PERIOD_HOURS hours.
209 */
210 void
vm_analytics_tick(void * arg0,void * arg1)211 vm_analytics_tick(void *arg0, void *arg1)
212 {
213 #pragma unused(arg0, arg1)
214 report_vm_swapusage();
215 report_mlock_failures();
216 #if XNU_TARGET_OS_WATCH
217 report_compressor_age();
218 #endif /* XNU_TARGET_OS_WATCH */
219 schedule_analytics_thread_call();
220 }
221
222 static void
vm_analytics_init()223 vm_analytics_init()
224 {
225 vm_analytics_thread_call = thread_call_allocate_with_options(vm_analytics_tick, NULL, THREAD_CALL_PRIORITY_KERNEL, THREAD_CALL_OPTIONS_ONCE);
226 schedule_analytics_thread_call();
227 }
228
229 STARTUP(THREAD_CALL, STARTUP_RANK_MIDDLE, vm_analytics_init);
230