xref: /xnu-8796.121.2/bsd/dev/dtrace/lockprof.c (revision c54f35ca767986246321eb901baf8f5ff7923f6a)
1*c54f35caSApple OSS Distributions /*
2*c54f35caSApple OSS Distributions  * Copyright (c) 2019 Apple Inc. All rights reserved.
3*c54f35caSApple OSS Distributions  *
4*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
5*c54f35caSApple OSS Distributions  *
6*c54f35caSApple OSS Distributions  * This file contains Original Code and/or Modifications of Original Code
7*c54f35caSApple OSS Distributions  * as defined in and that are subject to the Apple Public Source License
8*c54f35caSApple OSS Distributions  * Version 2.0 (the 'License'). You may not use this file except in
9*c54f35caSApple OSS Distributions  * compliance with the License. The rights granted to you under the License
10*c54f35caSApple OSS Distributions  * may not be used to create, or enable the creation or redistribution of,
11*c54f35caSApple OSS Distributions  * unlawful or unlicensed copies of an Apple operating system, or to
12*c54f35caSApple OSS Distributions  * circumvent, violate, or enable the circumvention or violation of, any
13*c54f35caSApple OSS Distributions  * terms of an Apple operating system software license agreement.
14*c54f35caSApple OSS Distributions  *
15*c54f35caSApple OSS Distributions  * Please obtain a copy of the License at
16*c54f35caSApple OSS Distributions  * http://www.opensource.apple.com/apsl/ and read it before using this file.
17*c54f35caSApple OSS Distributions  *
18*c54f35caSApple OSS Distributions  * The Original Code and all software distributed under the License are
19*c54f35caSApple OSS Distributions  * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20*c54f35caSApple OSS Distributions  * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21*c54f35caSApple OSS Distributions  * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22*c54f35caSApple OSS Distributions  * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23*c54f35caSApple OSS Distributions  * Please see the License for the specific language governing rights and
24*c54f35caSApple OSS Distributions  * limitations under the License.
25*c54f35caSApple OSS Distributions  *
26*c54f35caSApple OSS Distributions  * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
27*c54f35caSApple OSS Distributions  */
28*c54f35caSApple OSS Distributions #include <sys/ioctl.h>
29*c54f35caSApple OSS Distributions 
30*c54f35caSApple OSS Distributions #include <sys/stat.h>
31*c54f35caSApple OSS Distributions #include <miscfs/devfs/devfs.h>
32*c54f35caSApple OSS Distributions #include <sys/conf.h>
33*c54f35caSApple OSS Distributions #include <sys/systm.h>
34*c54f35caSApple OSS Distributions #include <sys/dtrace.h>
35*c54f35caSApple OSS Distributions #include <sys/dtrace_impl.h>
36*c54f35caSApple OSS Distributions #include <kern/lock_group.h>
37*c54f35caSApple OSS Distributions #include <kern/lock_stat.h>
38*c54f35caSApple OSS Distributions 
39*c54f35caSApple OSS Distributions #if LOCK_STATS
40*c54f35caSApple OSS Distributions 
41*c54f35caSApple OSS Distributions #define LP_NODE "lockprof"
42*c54f35caSApple OSS Distributions 
43*c54f35caSApple OSS Distributions #define LOCKPROF_AFRAMES 3
44*c54f35caSApple OSS Distributions #define LOCKPROF_LEN 64
45*c54f35caSApple OSS Distributions 
46*c54f35caSApple OSS Distributions static dtrace_provider_id_t lockprof_id;
47*c54f35caSApple OSS Distributions 
48*c54f35caSApple OSS Distributions #define LOCKPROF_MAX 10000 /* maximum number of lockprof probes */
49*c54f35caSApple OSS Distributions static uint32_t lockprof_count; /* current number of lockprof probes */
50*c54f35caSApple OSS Distributions 
51*c54f35caSApple OSS Distributions enum probe_flags {
52*c54f35caSApple OSS Distributions 	/*
53*c54f35caSApple OSS Distributions 	 * Counts time spent spinning/blocking
54*c54f35caSApple OSS Distributions 	 */
55*c54f35caSApple OSS Distributions 	TIME_EVENT = 0x01,
56*c54f35caSApple OSS Distributions 	/*
57*c54f35caSApple OSS Distributions 	 * Requires LCK_GRP_ATTR_STAT to be set on the lock
58*c54f35caSApple OSS Distributions 	 * group, either via lck_grp_attr_setsta on the lock group,
59*c54f35caSApple OSS Distributions 	 * or globally via the lcks=3 boot-arg
60*c54f35caSApple OSS Distributions 	 */
61*c54f35caSApple OSS Distributions 	STAT_NEEDED = 0x02
62*c54f35caSApple OSS Distributions };
63*c54f35caSApple OSS Distributions 
64*c54f35caSApple OSS Distributions static const struct {
65*c54f35caSApple OSS Distributions 	const char *prefix;
66*c54f35caSApple OSS Distributions 	int flags;
67*c54f35caSApple OSS Distributions 	size_t count_offset;
68*c54f35caSApple OSS Distributions 	size_t stat_offset;
69*c54f35caSApple OSS Distributions } probes[] = {
70*c54f35caSApple OSS Distributions 	{"spin-held-", 0, offsetof(lck_grp_t, lck_grp_spincnt), offsetof(lck_grp_stats_t, lgss_spin_held)},
71*c54f35caSApple OSS Distributions 	{"spin-miss-", 0, offsetof(lck_grp_t, lck_grp_spincnt), offsetof(lck_grp_stats_t, lgss_spin_miss)},
72*c54f35caSApple OSS Distributions 	{"spin-spin-", TIME_EVENT, offsetof(lck_grp_t, lck_grp_spincnt), offsetof(lck_grp_stats_t, lgss_spin_spin)},
73*c54f35caSApple OSS Distributions 	{"ticket-held-", 0, offsetof(lck_grp_t, lck_grp_ticketcnt), offsetof(lck_grp_stats_t, lgss_ticket_held)},
74*c54f35caSApple OSS Distributions 	{"ticket-miss-", 0, offsetof(lck_grp_t, lck_grp_ticketcnt), offsetof(lck_grp_stats_t, lgss_ticket_miss)},
75*c54f35caSApple OSS Distributions 	{"ticket-spin-", TIME_EVENT, offsetof(lck_grp_t, lck_grp_ticketcnt), offsetof(lck_grp_stats_t, lgss_ticket_spin)},
76*c54f35caSApple OSS Distributions 	{"adaptive-held-", STAT_NEEDED, offsetof(lck_grp_t, lck_grp_mtxcnt), offsetof(lck_grp_stats_t, lgss_mtx_held)},
77*c54f35caSApple OSS Distributions 	{"adaptive-miss-", STAT_NEEDED, offsetof(lck_grp_t, lck_grp_mtxcnt), offsetof(lck_grp_stats_t, lgss_mtx_miss)},
78*c54f35caSApple OSS Distributions 	{"adaptive-wait-", STAT_NEEDED, offsetof(lck_grp_t, lck_grp_mtxcnt), offsetof(lck_grp_stats_t, lgss_mtx_wait)},
79*c54f35caSApple OSS Distributions 	{"adaptive-direct-wait-", STAT_NEEDED, offsetof(lck_grp_t, lck_grp_mtxcnt), offsetof(lck_grp_stats_t, lgss_mtx_direct_wait)},
80*c54f35caSApple OSS Distributions 	{NULL, false, 0, 0}
81*c54f35caSApple OSS Distributions };
82*c54f35caSApple OSS Distributions 
83*c54f35caSApple OSS Distributions /*
84*c54f35caSApple OSS Distributions  * Default defined probes for counting events
85*c54f35caSApple OSS Distributions  */
86*c54f35caSApple OSS Distributions const static int hold_defaults[] = {
87*c54f35caSApple OSS Distributions 	10000 /* 10000 events */
88*c54f35caSApple OSS Distributions };
89*c54f35caSApple OSS Distributions 
90*c54f35caSApple OSS Distributions /*
91*c54f35caSApple OSS Distributions  * Default defined probes for time events
92*c54f35caSApple OSS Distributions  */
93*c54f35caSApple OSS Distributions const static struct {
94*c54f35caSApple OSS Distributions 	unsigned int time;
95*c54f35caSApple OSS Distributions 	const char *suffix;
96*c54f35caSApple OSS Distributions 	uint64_t mult;
97*c54f35caSApple OSS Distributions } cont_defaults[] = {
98*c54f35caSApple OSS Distributions 	{100, "ms", NANOSEC / MILLISEC} /* 100 ms */
99*c54f35caSApple OSS Distributions };
100*c54f35caSApple OSS Distributions 
101*c54f35caSApple OSS Distributions typedef struct lockprof_probe {
102*c54f35caSApple OSS Distributions 	int lockprof_kind;
103*c54f35caSApple OSS Distributions 	dtrace_id_t lockprof_id;
104*c54f35caSApple OSS Distributions 	uint64_t lockprof_limit;
105*c54f35caSApple OSS Distributions 	lck_grp_t *lockprof_grp;
106*c54f35caSApple OSS Distributions } lockprof_probe_t;
107*c54f35caSApple OSS Distributions 
108*c54f35caSApple OSS Distributions static int
lockprof_lock_count(lck_grp_t * grp,int kind)109*c54f35caSApple OSS Distributions lockprof_lock_count(lck_grp_t *grp, int kind)
110*c54f35caSApple OSS Distributions {
111*c54f35caSApple OSS Distributions 	return *(int*)((uintptr_t)(grp) + probes[kind].count_offset);
112*c54f35caSApple OSS Distributions }
113*c54f35caSApple OSS Distributions 
114*c54f35caSApple OSS Distributions static void
probe_create(int kind,const char * suffix,const char * grp_name,uint64_t count,uint64_t mult)115*c54f35caSApple OSS Distributions probe_create(int kind, const char *suffix, const char *grp_name, uint64_t count, uint64_t mult)
116*c54f35caSApple OSS Distributions {
117*c54f35caSApple OSS Distributions 	uint64_t limit = count * mult;
118*c54f35caSApple OSS Distributions 
119*c54f35caSApple OSS Distributions 	if (probes[kind].flags & TIME_EVENT) {
120*c54f35caSApple OSS Distributions 		nanoseconds_to_absolutetime(limit, &limit);
121*c54f35caSApple OSS Distributions 	}
122*c54f35caSApple OSS Distributions 
123*c54f35caSApple OSS Distributions 	lck_grp_foreach(^bool (lck_grp_t *grp) {
124*c54f35caSApple OSS Distributions 		char name[LOCKPROF_LEN];
125*c54f35caSApple OSS Distributions 
126*c54f35caSApple OSS Distributions 		if (!grp_name || grp_name[0] == '\0' || strcmp(grp_name, grp->lck_grp_name) == 0) {
127*c54f35caSApple OSS Distributions 		        snprintf(name, sizeof(name), "%s%llu%s", probes[kind].prefix, count, suffix ?: "");
128*c54f35caSApple OSS Distributions 
129*c54f35caSApple OSS Distributions 		        if (dtrace_probe_lookup(lockprof_id, grp->lck_grp_name, NULL, name) != 0) {
130*c54f35caSApple OSS Distributions 		                return true;
131*c54f35caSApple OSS Distributions 			}
132*c54f35caSApple OSS Distributions 		        if (lockprof_lock_count(grp, kind) == 0) {
133*c54f35caSApple OSS Distributions 		                return true;
134*c54f35caSApple OSS Distributions 			}
135*c54f35caSApple OSS Distributions 		        if ((probes[kind].flags & STAT_NEEDED) && !lck_grp_has_stats(grp)) {
136*c54f35caSApple OSS Distributions 		                return true;
137*c54f35caSApple OSS Distributions 			}
138*c54f35caSApple OSS Distributions 		        if (lockprof_count >= LOCKPROF_MAX) {
139*c54f35caSApple OSS Distributions 		                return false;
140*c54f35caSApple OSS Distributions 			}
141*c54f35caSApple OSS Distributions 
142*c54f35caSApple OSS Distributions 		        lockprof_probe_t *probe = kmem_zalloc(sizeof(lockprof_probe_t), KM_SLEEP);
143*c54f35caSApple OSS Distributions 		        probe->lockprof_kind = kind;
144*c54f35caSApple OSS Distributions 		        probe->lockprof_limit = limit;
145*c54f35caSApple OSS Distributions 		        probe->lockprof_grp = grp;
146*c54f35caSApple OSS Distributions 
147*c54f35caSApple OSS Distributions 		        lck_grp_reference(grp, NULL);
148*c54f35caSApple OSS Distributions 
149*c54f35caSApple OSS Distributions 		        probe->lockprof_id = dtrace_probe_create(lockprof_id, grp->lck_grp_name, NULL, name,
150*c54f35caSApple OSS Distributions 		        LOCKPROF_AFRAMES, probe);
151*c54f35caSApple OSS Distributions 
152*c54f35caSApple OSS Distributions 		        lockprof_count++;
153*c54f35caSApple OSS Distributions 		}
154*c54f35caSApple OSS Distributions 
155*c54f35caSApple OSS Distributions 		return true;
156*c54f35caSApple OSS Distributions 	});
157*c54f35caSApple OSS Distributions }
158*c54f35caSApple OSS Distributions 
159*c54f35caSApple OSS Distributions static void
lockprof_provide(void * arg,const dtrace_probedesc_t * desc)160*c54f35caSApple OSS Distributions lockprof_provide(void *arg, const dtrace_probedesc_t *desc)
161*c54f35caSApple OSS Distributions {
162*c54f35caSApple OSS Distributions #pragma unused(arg)
163*c54f35caSApple OSS Distributions 	size_t event_id, i, j, len;
164*c54f35caSApple OSS Distributions 
165*c54f35caSApple OSS Distributions 	if (desc == NULL) {
166*c54f35caSApple OSS Distributions 		for (i = 0; i < sizeof(hold_defaults) / sizeof(hold_defaults[0]); i++) {
167*c54f35caSApple OSS Distributions 			for (j = 0; probes[j].prefix != NULL; j++) {
168*c54f35caSApple OSS Distributions 				if (!(probes[j].flags & TIME_EVENT)) {
169*c54f35caSApple OSS Distributions 					probe_create(j, NULL, NULL, hold_defaults[i], 1);
170*c54f35caSApple OSS Distributions 				}
171*c54f35caSApple OSS Distributions 			}
172*c54f35caSApple OSS Distributions 		}
173*c54f35caSApple OSS Distributions 		for (i = 0; i < sizeof(cont_defaults) / sizeof(cont_defaults[0]); i++) {
174*c54f35caSApple OSS Distributions 			for (j = 0; probes[j].prefix != NULL; j++) {
175*c54f35caSApple OSS Distributions 				if (probes[j].flags & TIME_EVENT) {
176*c54f35caSApple OSS Distributions 					probe_create(j, cont_defaults[i].suffix, NULL, cont_defaults[i].time, cont_defaults[i].mult);
177*c54f35caSApple OSS Distributions 				}
178*c54f35caSApple OSS Distributions 			}
179*c54f35caSApple OSS Distributions 		}
180*c54f35caSApple OSS Distributions 		return;
181*c54f35caSApple OSS Distributions 	}
182*c54f35caSApple OSS Distributions 
183*c54f35caSApple OSS Distributions 	const char *name, *suffix = NULL;
184*c54f35caSApple OSS Distributions 	hrtime_t val = 0, mult = 1;
185*c54f35caSApple OSS Distributions 
186*c54f35caSApple OSS Distributions 	const struct {
187*c54f35caSApple OSS Distributions 		const char *name;
188*c54f35caSApple OSS Distributions 		hrtime_t mult;
189*c54f35caSApple OSS Distributions 	} suffixes[] = {
190*c54f35caSApple OSS Distributions 		{ "us", NANOSEC / MICROSEC },
191*c54f35caSApple OSS Distributions 		{ "usec", NANOSEC / MICROSEC },
192*c54f35caSApple OSS Distributions 		{ "ms", NANOSEC / MILLISEC },
193*c54f35caSApple OSS Distributions 		{ "msec", NANOSEC / MILLISEC },
194*c54f35caSApple OSS Distributions 		{ "s", NANOSEC / SEC },
195*c54f35caSApple OSS Distributions 		{ "sec", NANOSEC / SEC },
196*c54f35caSApple OSS Distributions 		{ NULL, 0 }
197*c54f35caSApple OSS Distributions 	};
198*c54f35caSApple OSS Distributions 
199*c54f35caSApple OSS Distributions 	name = desc->dtpd_name;
200*c54f35caSApple OSS Distributions 
201*c54f35caSApple OSS Distributions 	for (event_id = 0; probes[event_id].prefix != NULL; event_id++) {
202*c54f35caSApple OSS Distributions 		len = strlen(probes[event_id].prefix);
203*c54f35caSApple OSS Distributions 
204*c54f35caSApple OSS Distributions 		if (strncmp(name, probes[event_id].prefix, len) != 0) {
205*c54f35caSApple OSS Distributions 			continue;
206*c54f35caSApple OSS Distributions 		}
207*c54f35caSApple OSS Distributions 		break;
208*c54f35caSApple OSS Distributions 	}
209*c54f35caSApple OSS Distributions 
210*c54f35caSApple OSS Distributions 	if (probes[event_id].prefix == NULL) {
211*c54f35caSApple OSS Distributions 		return;
212*c54f35caSApple OSS Distributions 	}
213*c54f35caSApple OSS Distributions 
214*c54f35caSApple OSS Distributions 
215*c54f35caSApple OSS Distributions 	/*
216*c54f35caSApple OSS Distributions 	 * We need to start before any time suffix.
217*c54f35caSApple OSS Distributions 	 */
218*c54f35caSApple OSS Distributions 	for (i = strlen(name); i >= len; i--) {
219*c54f35caSApple OSS Distributions 		if (name[i] >= '0' && name[i] <= '9') {
220*c54f35caSApple OSS Distributions 			break;
221*c54f35caSApple OSS Distributions 		}
222*c54f35caSApple OSS Distributions 		suffix = &name[i];
223*c54f35caSApple OSS Distributions 	}
224*c54f35caSApple OSS Distributions 
225*c54f35caSApple OSS Distributions 	/*
226*c54f35caSApple OSS Distributions 	 * Now determine the numerical value present in the probe name.
227*c54f35caSApple OSS Distributions 	 */
228*c54f35caSApple OSS Distributions 	for (uint64_t m = 1; i >= len; i--) {
229*c54f35caSApple OSS Distributions 		if (name[i] < '0' || name[i] > '9') {
230*c54f35caSApple OSS Distributions 			return;
231*c54f35caSApple OSS Distributions 		}
232*c54f35caSApple OSS Distributions 
233*c54f35caSApple OSS Distributions 		val += (name[i] - '0') * m;
234*c54f35caSApple OSS Distributions 		m *= (hrtime_t)10;
235*c54f35caSApple OSS Distributions 	}
236*c54f35caSApple OSS Distributions 
237*c54f35caSApple OSS Distributions 	if (val == 0) {
238*c54f35caSApple OSS Distributions 		return;
239*c54f35caSApple OSS Distributions 	}
240*c54f35caSApple OSS Distributions 
241*c54f35caSApple OSS Distributions 	if (probes[event_id].flags & TIME_EVENT) {
242*c54f35caSApple OSS Distributions 		for (i = 0, mult = 0; suffixes[i].name != NULL; i++) {
243*c54f35caSApple OSS Distributions 			if (suffix && (strncasecmp(suffixes[i].name, suffix, strlen(suffixes[i].name) + 1) == 0)) {
244*c54f35caSApple OSS Distributions 				mult = suffixes[i].mult;
245*c54f35caSApple OSS Distributions 				break;
246*c54f35caSApple OSS Distributions 			}
247*c54f35caSApple OSS Distributions 		}
248*c54f35caSApple OSS Distributions 		if (suffixes[i].name == NULL) {
249*c54f35caSApple OSS Distributions 			return;
250*c54f35caSApple OSS Distributions 		}
251*c54f35caSApple OSS Distributions 	} else if (suffix && (*suffix != '\0')) {
252*c54f35caSApple OSS Distributions 		return;
253*c54f35caSApple OSS Distributions 	}
254*c54f35caSApple OSS Distributions 
255*c54f35caSApple OSS Distributions 	probe_create(event_id, suffix, desc->dtpd_mod, val, mult);
256*c54f35caSApple OSS Distributions }
257*c54f35caSApple OSS Distributions 
258*c54f35caSApple OSS Distributions 
259*c54f35caSApple OSS Distributions static lck_grp_stat_t*
lockprof_stat(lck_grp_t * grp,int kind)260*c54f35caSApple OSS Distributions lockprof_stat(lck_grp_t *grp, int kind)
261*c54f35caSApple OSS Distributions {
262*c54f35caSApple OSS Distributions 	return (lck_grp_stat_t*)((uintptr_t)&grp->lck_grp_stats + probes[kind].stat_offset);
263*c54f35caSApple OSS Distributions }
264*c54f35caSApple OSS Distributions 
265*c54f35caSApple OSS Distributions static int
lockprof_enable(void * arg,dtrace_id_t id,void * parg)266*c54f35caSApple OSS Distributions lockprof_enable(void *arg, dtrace_id_t id, void *parg)
267*c54f35caSApple OSS Distributions {
268*c54f35caSApple OSS Distributions #pragma unused(arg, id, parg)
269*c54f35caSApple OSS Distributions 	lockprof_probe_t *probe = (lockprof_probe_t*)parg;
270*c54f35caSApple OSS Distributions 	lck_grp_t *grp = probe->lockprof_grp;
271*c54f35caSApple OSS Distributions 	lck_grp_stat_t *stat;
272*c54f35caSApple OSS Distributions 
273*c54f35caSApple OSS Distributions 	if (grp == NULL) {
274*c54f35caSApple OSS Distributions 		return -1;
275*c54f35caSApple OSS Distributions 	}
276*c54f35caSApple OSS Distributions 
277*c54f35caSApple OSS Distributions 	if ((stat = lockprof_stat(grp, probe->lockprof_kind)) == NULL) {
278*c54f35caSApple OSS Distributions 		return -1;
279*c54f35caSApple OSS Distributions 	}
280*c54f35caSApple OSS Distributions 
281*c54f35caSApple OSS Distributions 	/*
282*c54f35caSApple OSS Distributions 	 * lockprof_enable/disable are called with
283*c54f35caSApple OSS Distributions 	 * dtrace_lock held
284*c54f35caSApple OSS Distributions 	 */
285*c54f35caSApple OSS Distributions 	if (stat->lgs_limit != 0) {
286*c54f35caSApple OSS Distributions 		return -1;
287*c54f35caSApple OSS Distributions 	}
288*c54f35caSApple OSS Distributions 
289*c54f35caSApple OSS Distributions 	stat->lgs_limit = probe->lockprof_limit;
290*c54f35caSApple OSS Distributions 	stat->lgs_probeid = probe->lockprof_id;
291*c54f35caSApple OSS Distributions 	lck_grp_stat_enable(stat);
292*c54f35caSApple OSS Distributions 	lck_grp_enable_feature(LCK_DEBUG_LOCKPROF);
293*c54f35caSApple OSS Distributions 
294*c54f35caSApple OSS Distributions 	return 0;
295*c54f35caSApple OSS Distributions }
296*c54f35caSApple OSS Distributions 
297*c54f35caSApple OSS Distributions static void
lockprof_disable(void * arg,dtrace_id_t id,void * parg)298*c54f35caSApple OSS Distributions lockprof_disable(void *arg, dtrace_id_t id, void *parg)
299*c54f35caSApple OSS Distributions {
300*c54f35caSApple OSS Distributions #pragma unused(arg, id)
301*c54f35caSApple OSS Distributions 	lockprof_probe_t *probe = (lockprof_probe_t*)parg;
302*c54f35caSApple OSS Distributions 	lck_grp_t *grp = probe->lockprof_grp;
303*c54f35caSApple OSS Distributions 	lck_grp_stat_t *stat;
304*c54f35caSApple OSS Distributions 
305*c54f35caSApple OSS Distributions 	if (grp == NULL) {
306*c54f35caSApple OSS Distributions 		return;
307*c54f35caSApple OSS Distributions 	}
308*c54f35caSApple OSS Distributions 
309*c54f35caSApple OSS Distributions 	if ((stat = lockprof_stat(grp, probe->lockprof_kind)) == NULL) {
310*c54f35caSApple OSS Distributions 		return;
311*c54f35caSApple OSS Distributions 	}
312*c54f35caSApple OSS Distributions 
313*c54f35caSApple OSS Distributions 	if (stat->lgs_limit == 0 || !lck_grp_stat_enabled(stat)) {
314*c54f35caSApple OSS Distributions 		return;
315*c54f35caSApple OSS Distributions 	}
316*c54f35caSApple OSS Distributions 
317*c54f35caSApple OSS Distributions 	stat->lgs_limit = 0;
318*c54f35caSApple OSS Distributions 	stat->lgs_probeid = 0;
319*c54f35caSApple OSS Distributions 	lck_grp_stat_disable(stat);
320*c54f35caSApple OSS Distributions 	lck_grp_disable_feature(LCK_DEBUG_LOCKPROF);
321*c54f35caSApple OSS Distributions }
322*c54f35caSApple OSS Distributions 
323*c54f35caSApple OSS Distributions static void
lockprof_destroy(void * arg,dtrace_id_t id,void * parg)324*c54f35caSApple OSS Distributions lockprof_destroy(void *arg, dtrace_id_t id, void *parg)
325*c54f35caSApple OSS Distributions {
326*c54f35caSApple OSS Distributions #pragma unused(arg, id)
327*c54f35caSApple OSS Distributions 	lockprof_probe_t *probe = (lockprof_probe_t*)parg;
328*c54f35caSApple OSS Distributions 	lck_grp_deallocate(probe->lockprof_grp, NULL);
329*c54f35caSApple OSS Distributions 	kmem_free(probe, sizeof(lockprof_probe_t));
330*c54f35caSApple OSS Distributions 	lockprof_count--;
331*c54f35caSApple OSS Distributions }
332*c54f35caSApple OSS Distributions 
333*c54f35caSApple OSS Distributions static void
lockprof_getargdesc(void * arg,dtrace_id_t id,void * parg,dtrace_argdesc_t * desc)334*c54f35caSApple OSS Distributions lockprof_getargdesc(void *arg, dtrace_id_t id, void *parg, dtrace_argdesc_t *desc)
335*c54f35caSApple OSS Distributions {
336*c54f35caSApple OSS Distributions #pragma unused(arg, id, parg)
337*c54f35caSApple OSS Distributions 	const char *argdesc = NULL;
338*c54f35caSApple OSS Distributions 	switch (desc->dtargd_ndx) {
339*c54f35caSApple OSS Distributions 	case 0:
340*c54f35caSApple OSS Distributions 		argdesc = "lck_grp_t*";
341*c54f35caSApple OSS Distributions 		break;
342*c54f35caSApple OSS Distributions 	case 1:
343*c54f35caSApple OSS Distributions 		argdesc = "uint64_t";
344*c54f35caSApple OSS Distributions 		break;
345*c54f35caSApple OSS Distributions 	}
346*c54f35caSApple OSS Distributions 
347*c54f35caSApple OSS Distributions 	if (argdesc) {
348*c54f35caSApple OSS Distributions 		strlcpy(desc->dtargd_native, argdesc, DTRACE_ARGTYPELEN);
349*c54f35caSApple OSS Distributions 	} else {
350*c54f35caSApple OSS Distributions 		desc->dtargd_ndx = DTRACE_ARGNONE;
351*c54f35caSApple OSS Distributions 	}
352*c54f35caSApple OSS Distributions }
353*c54f35caSApple OSS Distributions static dtrace_pattr_t lockprof_attr = {
354*c54f35caSApple OSS Distributions 	{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
355*c54f35caSApple OSS Distributions 	{ DTRACE_STABILITY_UNSTABLE, DTRACE_STABILITY_UNSTABLE, DTRACE_CLASS_UNKNOWN },
356*c54f35caSApple OSS Distributions 	{ DTRACE_STABILITY_PRIVATE, DTRACE_STABILITY_PRIVATE, DTRACE_CLASS_UNKNOWN },
357*c54f35caSApple OSS Distributions 	{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
358*c54f35caSApple OSS Distributions 	{ DTRACE_STABILITY_EVOLVING, DTRACE_STABILITY_EVOLVING, DTRACE_CLASS_COMMON },
359*c54f35caSApple OSS Distributions };
360*c54f35caSApple OSS Distributions 
361*c54f35caSApple OSS Distributions static dtrace_pops_t lockprof_pops = {
362*c54f35caSApple OSS Distributions 	.dtps_provide =         lockprof_provide,
363*c54f35caSApple OSS Distributions 	.dtps_provide_module =  NULL,
364*c54f35caSApple OSS Distributions 	.dtps_enable =          lockprof_enable,
365*c54f35caSApple OSS Distributions 	.dtps_disable =         lockprof_disable,
366*c54f35caSApple OSS Distributions 	.dtps_suspend =         NULL,
367*c54f35caSApple OSS Distributions 	.dtps_resume =          NULL,
368*c54f35caSApple OSS Distributions 	.dtps_getargdesc =      lockprof_getargdesc,
369*c54f35caSApple OSS Distributions 	.dtps_getargval =       NULL,
370*c54f35caSApple OSS Distributions 	.dtps_usermode =        NULL,
371*c54f35caSApple OSS Distributions 	.dtps_destroy =         lockprof_destroy
372*c54f35caSApple OSS Distributions };
373*c54f35caSApple OSS Distributions 
374*c54f35caSApple OSS Distributions static int
_lockprof_open(dev_t dev,int flags,int devtype,struct proc * p)375*c54f35caSApple OSS Distributions _lockprof_open(dev_t dev, int flags, int devtype, struct proc *p)
376*c54f35caSApple OSS Distributions {
377*c54f35caSApple OSS Distributions #pragma unused(dev,flags,devtype,p)
378*c54f35caSApple OSS Distributions 	return 0;
379*c54f35caSApple OSS Distributions }
380*c54f35caSApple OSS Distributions 
381*c54f35caSApple OSS Distributions static const struct cdevsw lockprof_cdevsw =
382*c54f35caSApple OSS Distributions {
383*c54f35caSApple OSS Distributions 	.d_open = _lockprof_open,
384*c54f35caSApple OSS Distributions 	.d_close = eno_opcl,
385*c54f35caSApple OSS Distributions 	.d_read = eno_rdwrt,
386*c54f35caSApple OSS Distributions 	.d_write = eno_rdwrt,
387*c54f35caSApple OSS Distributions 	.d_ioctl = eno_ioctl,
388*c54f35caSApple OSS Distributions 	.d_stop = eno_stop,
389*c54f35caSApple OSS Distributions 	.d_reset = eno_reset,
390*c54f35caSApple OSS Distributions 	.d_select = eno_select,
391*c54f35caSApple OSS Distributions 	.d_mmap = eno_mmap,
392*c54f35caSApple OSS Distributions 	.d_strategy = eno_strat,
393*c54f35caSApple OSS Distributions 	.d_reserved_1 = eno_getc,
394*c54f35caSApple OSS Distributions 	.d_reserved_2 = eno_putc,
395*c54f35caSApple OSS Distributions };
396*c54f35caSApple OSS Distributions 
397*c54f35caSApple OSS Distributions 
398*c54f35caSApple OSS Distributions #endif /* LOCK_STATS */
399*c54f35caSApple OSS Distributions void lockprof_init(void);
400*c54f35caSApple OSS Distributions void
lockprof_init(void)401*c54f35caSApple OSS Distributions lockprof_init(void)
402*c54f35caSApple OSS Distributions {
403*c54f35caSApple OSS Distributions #if LOCK_STATS
404*c54f35caSApple OSS Distributions 	int majorno = cdevsw_add(-1, &lockprof_cdevsw);
405*c54f35caSApple OSS Distributions 
406*c54f35caSApple OSS Distributions 	if (majorno < 0) {
407*c54f35caSApple OSS Distributions 		panic("dtrace: failed to allocate a major number");
408*c54f35caSApple OSS Distributions 		return;
409*c54f35caSApple OSS Distributions 	}
410*c54f35caSApple OSS Distributions 
411*c54f35caSApple OSS Distributions 	if (dtrace_register(LP_NODE, &lockprof_attr, DTRACE_PRIV_KERNEL,
412*c54f35caSApple OSS Distributions 	    NULL, &lockprof_pops, NULL, &lockprof_id) != 0) {
413*c54f35caSApple OSS Distributions 		panic("dtrace: failed to register lockprof provider");
414*c54f35caSApple OSS Distributions 	}
415*c54f35caSApple OSS Distributions 
416*c54f35caSApple OSS Distributions 	dev_t dev = makedev(majorno, 0);
417*c54f35caSApple OSS Distributions 
418*c54f35caSApple OSS Distributions 	if (devfs_make_node( dev, DEVFS_CHAR, UID_ROOT, GID_WHEEL, 0666,
419*c54f35caSApple OSS Distributions 	    LP_NODE ) == NULL) {
420*c54f35caSApple OSS Distributions 		panic("dtrace: devfs_make_node failed for lockprof");
421*c54f35caSApple OSS Distributions 	}
422*c54f35caSApple OSS Distributions 
423*c54f35caSApple OSS Distributions #endif /* LOCK_STATS */
424*c54f35caSApple OSS Distributions }
425