xref: /titanic_50/usr/src/lib/libdtrace/common/dt_work.c (revision 554ff184129088135ad2643c1c9832174a17be88)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <dt_impl.h>
30 #include <stddef.h>
31 #include <errno.h>
32 #include <assert.h>
33 #include <time.h>
34 
35 static const struct {
36 	int dtslt_option;
37 	size_t dtslt_offs;
38 } _dtrace_sleeptab[] = {
39 	{ DTRACEOPT_STATUSRATE, offsetof(dtrace_hdl_t, dt_laststatus) },
40 	{ DTRACEOPT_AGGRATE, offsetof(dtrace_hdl_t, dt_lastagg) },
41 	{ DTRACEOPT_SWITCHRATE, offsetof(dtrace_hdl_t, dt_lastswitch) },
42 	{ DTRACEOPT_MAX, 0 }
43 };
44 
45 void
46 dtrace_sleep(dtrace_hdl_t *dtp)
47 {
48 	dt_proc_hash_t *dph = dtp->dt_procs;
49 	dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY];
50 	dt_proc_t *dpr;
51 
52 	hrtime_t earliest = INT64_MAX;
53 	struct timespec tv;
54 	hrtime_t now;
55 	int i;
56 
57 	for (i = 0; _dtrace_sleeptab[i].dtslt_option < DTRACEOPT_MAX; i++) {
58 		uintptr_t a = (uintptr_t)dtp + _dtrace_sleeptab[i].dtslt_offs;
59 		int opt = _dtrace_sleeptab[i].dtslt_option;
60 		dtrace_optval_t interval = dtp->dt_options[opt];
61 
62 		/*
63 		 * If the buffering policy is set to anything other than
64 		 * "switch", we ignore the aggrate and switchrate -- they're
65 		 * meaningless.
66 		 */
67 		if (policy != DTRACEOPT_BUFPOLICY_SWITCH &&
68 		    _dtrace_sleeptab[i].dtslt_option != DTRACEOPT_STATUSRATE)
69 			continue;
70 
71 		if (*((hrtime_t *)a) + interval < earliest)
72 			earliest = *((hrtime_t *)a) + interval;
73 	}
74 
75 	(void) pthread_mutex_lock(&dph->dph_lock);
76 
77 	now = gethrtime();
78 
79 	if (earliest < now) {
80 		(void) pthread_mutex_unlock(&dph->dph_lock);
81 		return; /* sleep duration has already past */
82 	}
83 
84 	tv.tv_sec = (earliest - now) / NANOSEC;
85 	tv.tv_nsec = (earliest - now) % NANOSEC;
86 
87 	/*
88 	 * Wait for either 'tv' nanoseconds to pass or to receive notification
89 	 * that a process is in an interesting state.  Regardless of why we
90 	 * awaken, iterate over any pending notifications and process them.
91 	 */
92 	(void) pthread_cond_reltimedwait_np(&dph->dph_cv, &dph->dph_lock, &tv);
93 
94 	while ((dpr = dph->dph_notify) != NULL) {
95 		dph->dph_notify = dpr->dpr_notify;
96 		dpr->dpr_notify = NULL;
97 
98 		if (dtp->dt_prochdlr != NULL)
99 			dtp->dt_prochdlr(dpr->dpr_proc, dtp->dt_procarg);
100 	}
101 
102 	(void) pthread_mutex_unlock(&dph->dph_lock);
103 }
104 
105 int
106 dtrace_status(dtrace_hdl_t *dtp)
107 {
108 	int gen = dtp->dt_statusgen;
109 	dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_STATUSRATE];
110 	hrtime_t now = gethrtime();
111 
112 	if (!dtp->dt_active)
113 		return (DTRACE_STATUS_NONE);
114 
115 	if (dtp->dt_stopped)
116 		return (DTRACE_STATUS_STOPPED);
117 
118 	if (dtp->dt_laststatus != 0) {
119 		if (now - dtp->dt_laststatus < interval)
120 			return (DTRACE_STATUS_NONE);
121 
122 		dtp->dt_laststatus += interval;
123 	} else {
124 		dtp->dt_laststatus = now;
125 	}
126 
127 	if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1)
128 		return (dt_set_errno(dtp, errno));
129 
130 	dtp->dt_statusgen ^= 1;
131 
132 	if (dt_handle_status(dtp, &dtp->dt_status[dtp->dt_statusgen],
133 	    &dtp->dt_status[gen]) == -1)
134 		return (-1);
135 
136 	if (dtp->dt_status[gen].dtst_exiting) {
137 		if (!dtp->dt_stopped)
138 			(void) dtrace_stop(dtp);
139 
140 		return (DTRACE_STATUS_EXITED);
141 	}
142 
143 	if (dtp->dt_status[gen].dtst_filled == 0)
144 		return (DTRACE_STATUS_OKAY);
145 
146 	if (dtp->dt_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
147 		return (DTRACE_STATUS_OKAY);
148 
149 	if (!dtp->dt_stopped) {
150 		if (dtrace_stop(dtp) == -1)
151 			return (-1);
152 	}
153 
154 	return (DTRACE_STATUS_FILLED);
155 }
156 
157 dtrace_workstatus_t
158 dtrace_work(dtrace_hdl_t *dtp, FILE *fp,
159     dtrace_consume_probe_f *pfunc, dtrace_consume_rec_f *rfunc, void *arg)
160 {
161 	int status = dtrace_status(dtp);
162 	dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY];
163 	dtrace_workstatus_t rval;
164 
165 	switch (status) {
166 	case DTRACE_STATUS_EXITED:
167 	case DTRACE_STATUS_FILLED:
168 	case DTRACE_STATUS_STOPPED:
169 		/*
170 		 * Tracing is stopped.  We now want to force dtrace_consume()
171 		 * and dtrace_aggregate_snap() to proceed, regardless of
172 		 * switchrate and aggrate.  We do this by clearing the times.
173 		 */
174 		dtp->dt_lastswitch = 0;
175 		dtp->dt_lastagg = 0;
176 		rval = DTRACE_WORKSTATUS_DONE;
177 		break;
178 
179 	case DTRACE_STATUS_NONE:
180 	case DTRACE_STATUS_OKAY:
181 		rval = DTRACE_WORKSTATUS_OKAY;
182 		break;
183 
184 	case -1:
185 		return (DTRACE_WORKSTATUS_ERROR);
186 	}
187 
188 	if ((status == DTRACE_STATUS_NONE || status == DTRACE_STATUS_OKAY) &&
189 	    policy != DTRACEOPT_BUFPOLICY_SWITCH) {
190 		/*
191 		 * There either isn't any status or things are fine -- and
192 		 * this is a "ring" or "fill" buffer.  We don't want to consume
193 		 * any of the trace data or snapshot the aggregations; we just
194 		 * return.
195 		 */
196 		assert(rval == DTRACE_WORKSTATUS_OKAY);
197 		return (rval);
198 	}
199 
200 	if (dtrace_aggregate_snap(dtp) == -1)
201 		return (DTRACE_WORKSTATUS_ERROR);
202 
203 	if (dtrace_consume(dtp, fp, pfunc, rfunc, arg) == -1)
204 		return (DTRACE_WORKSTATUS_ERROR);
205 
206 	return (rval);
207 }
208