Print this page
INTRO-118 enabling USDT probes in zones should be more scalable
Split |
Close |
Expand all |
Collapse all |
--- old/usr/src/lib/libdtrace/common/dt_work.c
+++ new/usr/src/lib/libdtrace/common/dt_work.c
1 1 /*
2 2 * CDDL HEADER START
3 3 *
4 4 * The contents of this file are subject to the terms of the
5 5 * Common Development and Distribution License, Version 1.0 only
6 6 * (the "License"). You may not use this file except in compliance
7 7 * with the License.
8 8 *
9 9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 10 * or http://www.opensolaris.org/os/licensing.
11 11 * See the License for the specific language governing permissions
12 12 * and limitations under the License.
13 13 *
14 14 * When distributing Covered Code, include this CDDL HEADER in each
15 15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 16 * If applicable, add the following below this CDDL HEADER, with the
17 17 * fields enclosed by brackets "[]" replaced with your own identifying
↓ open down ↓ |
17 lines elided |
↑ open up ↑ |
18 18 * information: Portions Copyright [yyyy] [name of copyright owner]
19 19 *
20 20 * CDDL HEADER END
21 21 */
22 22
23 23 /*
24 24 * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
25 25 * Use is subject to license terms.
26 26 */
27 27
28 -#pragma ident "%Z%%M% %I% %E% SMI"
28 +/*
29 + * Copyright (c) 2011, Joyent, Inc. All rights reserved.
30 + */
29 31
30 32 #include <dt_impl.h>
31 33 #include <stddef.h>
32 34 #include <errno.h>
33 35 #include <assert.h>
34 36 #include <time.h>
35 37
36 38 static const struct {
37 39 int dtslt_option;
38 40 size_t dtslt_offs;
39 41 } _dtrace_sleeptab[] = {
40 42 { DTRACEOPT_STATUSRATE, offsetof(dtrace_hdl_t, dt_laststatus) },
41 43 { DTRACEOPT_AGGRATE, offsetof(dtrace_hdl_t, dt_lastagg) },
42 44 { DTRACEOPT_SWITCHRATE, offsetof(dtrace_hdl_t, dt_lastswitch) },
43 45 { DTRACEOPT_MAX, 0 }
44 46 };
45 47
46 48 void
47 49 dtrace_sleep(dtrace_hdl_t *dtp)
48 50 {
49 51 dt_proc_hash_t *dph = dtp->dt_procs;
50 52 dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY];
51 53 dt_proc_notify_t *dprn;
52 54
53 55 hrtime_t earliest = INT64_MAX;
54 56 struct timespec tv;
55 57 hrtime_t now;
56 58 int i;
57 59
58 60 for (i = 0; _dtrace_sleeptab[i].dtslt_option < DTRACEOPT_MAX; i++) {
59 61 uintptr_t a = (uintptr_t)dtp + _dtrace_sleeptab[i].dtslt_offs;
60 62 int opt = _dtrace_sleeptab[i].dtslt_option;
61 63 dtrace_optval_t interval = dtp->dt_options[opt];
62 64
63 65 /*
64 66 * If the buffering policy is set to anything other than
65 67 * "switch", we ignore the aggrate and switchrate -- they're
66 68 * meaningless.
67 69 */
68 70 if (policy != DTRACEOPT_BUFPOLICY_SWITCH &&
69 71 _dtrace_sleeptab[i].dtslt_option != DTRACEOPT_STATUSRATE)
70 72 continue;
71 73
72 74 if (*((hrtime_t *)a) + interval < earliest)
73 75 earliest = *((hrtime_t *)a) + interval;
74 76 }
75 77
76 78 (void) pthread_mutex_lock(&dph->dph_lock);
77 79
78 80 now = gethrtime();
79 81
80 82 if (earliest < now) {
81 83 (void) pthread_mutex_unlock(&dph->dph_lock);
82 84 return; /* sleep duration has already past */
83 85 }
84 86
85 87 tv.tv_sec = (earliest - now) / NANOSEC;
86 88 tv.tv_nsec = (earliest - now) % NANOSEC;
87 89
88 90 /*
89 91 * Wait for either 'tv' nanoseconds to pass or to receive notification
90 92 * that a process is in an interesting state. Regardless of why we
91 93 * awaken, iterate over any pending notifications and process them.
92 94 */
93 95 (void) pthread_cond_reltimedwait_np(&dph->dph_cv, &dph->dph_lock, &tv);
94 96
95 97 while ((dprn = dph->dph_notify) != NULL) {
96 98 if (dtp->dt_prochdlr != NULL) {
97 99 char *err = dprn->dprn_errmsg;
98 100 if (*err == '\0')
99 101 err = NULL;
100 102
101 103 dtp->dt_prochdlr(dprn->dprn_dpr->dpr_proc, err,
102 104 dtp->dt_procarg);
103 105 }
104 106
105 107 dph->dph_notify = dprn->dprn_next;
106 108 dt_free(dtp, dprn);
107 109 }
108 110
109 111 (void) pthread_mutex_unlock(&dph->dph_lock);
110 112 }
111 113
112 114 int
113 115 dtrace_status(dtrace_hdl_t *dtp)
114 116 {
115 117 int gen = dtp->dt_statusgen;
116 118 dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_STATUSRATE];
117 119 hrtime_t now = gethrtime();
118 120
119 121 if (!dtp->dt_active)
120 122 return (DTRACE_STATUS_NONE);
121 123
122 124 if (dtp->dt_stopped)
123 125 return (DTRACE_STATUS_STOPPED);
124 126
125 127 if (dtp->dt_laststatus != 0) {
126 128 if (now - dtp->dt_laststatus < interval)
127 129 return (DTRACE_STATUS_NONE);
128 130
129 131 dtp->dt_laststatus += interval;
130 132 } else {
131 133 dtp->dt_laststatus = now;
132 134 }
133 135
134 136 if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1)
135 137 return (dt_set_errno(dtp, errno));
136 138
137 139 dtp->dt_statusgen ^= 1;
138 140
139 141 if (dt_handle_status(dtp, &dtp->dt_status[dtp->dt_statusgen],
140 142 &dtp->dt_status[gen]) == -1)
141 143 return (-1);
142 144
143 145 if (dtp->dt_status[gen].dtst_exiting) {
144 146 if (!dtp->dt_stopped)
145 147 (void) dtrace_stop(dtp);
146 148
147 149 return (DTRACE_STATUS_EXITED);
148 150 }
149 151
150 152 if (dtp->dt_status[gen].dtst_filled == 0)
151 153 return (DTRACE_STATUS_OKAY);
152 154
153 155 if (dtp->dt_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
154 156 return (DTRACE_STATUS_OKAY);
155 157
156 158 if (!dtp->dt_stopped) {
↓ open down ↓ |
118 lines elided |
↑ open up ↑ |
157 159 if (dtrace_stop(dtp) == -1)
158 160 return (-1);
159 161 }
160 162
161 163 return (DTRACE_STATUS_FILLED);
162 164 }
163 165
164 166 int
165 167 dtrace_go(dtrace_hdl_t *dtp)
166 168 {
167 - void *dof;
168 - int err;
169 -
170 169 if (dtp->dt_active)
171 170 return (dt_set_errno(dtp, EINVAL));
172 171
173 172 /*
174 173 * If a dtrace:::ERROR program and callback are registered, enable the
175 174 * program before we start tracing. If this fails for a vector open
176 175 * with ENOTTY, we permit dtrace_go() to succeed so that vector clients
177 176 * such as mdb's dtrace module can execute the rest of dtrace_go() even
178 177 * though they do not provide support for the DTRACEIOC_ENABLE ioctl.
179 178 */
180 179 if (dtp->dt_errprog != NULL &&
181 180 dtrace_program_exec(dtp, dtp->dt_errprog, NULL) == -1 && (
182 181 dtp->dt_errno != ENOTTY || dtp->dt_vector == NULL))
183 182 return (-1); /* dt_errno has been set for us */
184 183
185 - if ((dof = dtrace_getopt_dof(dtp)) == NULL)
186 - return (-1); /* dt_errno has been set for us */
187 -
188 - err = dt_ioctl(dtp, DTRACEIOC_ENABLE, dof);
189 - dtrace_dof_destroy(dtp, dof);
190 -
191 - if (err == -1 && (errno != ENOTTY || dtp->dt_vector == NULL))
192 - return (dt_set_errno(dtp, errno));
193 -
194 184 if (dt_ioctl(dtp, DTRACEIOC_GO, &dtp->dt_beganon) == -1) {
195 185 if (errno == EACCES)
196 186 return (dt_set_errno(dtp, EDT_DESTRUCTIVE));
197 187
198 188 if (errno == EALREADY)
199 189 return (dt_set_errno(dtp, EDT_ISANON));
200 190
201 191 if (errno == ENOENT)
202 192 return (dt_set_errno(dtp, EDT_NOANON));
203 193
204 194 if (errno == E2BIG)
205 195 return (dt_set_errno(dtp, EDT_ENDTOOBIG));
206 196
207 197 if (errno == ENOSPC)
208 198 return (dt_set_errno(dtp, EDT_BUFTOOSMALL));
209 199
210 200 return (dt_set_errno(dtp, errno));
211 201 }
212 202
213 203 dtp->dt_active = 1;
214 204
215 205 if (dt_options_load(dtp) == -1)
216 206 return (dt_set_errno(dtp, errno));
217 207
218 208 return (dt_aggregate_go(dtp));
219 209 }
220 210
221 211 int
222 212 dtrace_stop(dtrace_hdl_t *dtp)
223 213 {
224 214 int gen = dtp->dt_statusgen;
225 215
226 216 if (dtp->dt_stopped)
227 217 return (0);
228 218
229 219 if (dt_ioctl(dtp, DTRACEIOC_STOP, &dtp->dt_endedon) == -1)
230 220 return (dt_set_errno(dtp, errno));
231 221
232 222 dtp->dt_stopped = 1;
233 223
234 224 /*
235 225 * Now that we're stopped, we're going to get status one final time.
236 226 */
237 227 if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1)
238 228 return (dt_set_errno(dtp, errno));
239 229
240 230 if (dt_handle_status(dtp, &dtp->dt_status[gen ^ 1],
241 231 &dtp->dt_status[gen]) == -1)
242 232 return (-1);
243 233
244 234 return (0);
245 235 }
246 236
247 237
248 238 dtrace_workstatus_t
249 239 dtrace_work(dtrace_hdl_t *dtp, FILE *fp,
250 240 dtrace_consume_probe_f *pfunc, dtrace_consume_rec_f *rfunc, void *arg)
251 241 {
252 242 int status = dtrace_status(dtp);
253 243 dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY];
254 244 dtrace_workstatus_t rval;
255 245
256 246 switch (status) {
257 247 case DTRACE_STATUS_EXITED:
258 248 case DTRACE_STATUS_FILLED:
259 249 case DTRACE_STATUS_STOPPED:
260 250 /*
261 251 * Tracing is stopped. We now want to force dtrace_consume()
262 252 * and dtrace_aggregate_snap() to proceed, regardless of
263 253 * switchrate and aggrate. We do this by clearing the times.
264 254 */
265 255 dtp->dt_lastswitch = 0;
266 256 dtp->dt_lastagg = 0;
267 257 rval = DTRACE_WORKSTATUS_DONE;
268 258 break;
269 259
270 260 case DTRACE_STATUS_NONE:
271 261 case DTRACE_STATUS_OKAY:
272 262 rval = DTRACE_WORKSTATUS_OKAY;
273 263 break;
274 264
275 265 case -1:
276 266 return (DTRACE_WORKSTATUS_ERROR);
277 267 }
278 268
279 269 if ((status == DTRACE_STATUS_NONE || status == DTRACE_STATUS_OKAY) &&
280 270 policy != DTRACEOPT_BUFPOLICY_SWITCH) {
281 271 /*
282 272 * There either isn't any status or things are fine -- and
283 273 * this is a "ring" or "fill" buffer. We don't want to consume
284 274 * any of the trace data or snapshot the aggregations; we just
285 275 * return.
286 276 */
287 277 assert(rval == DTRACE_WORKSTATUS_OKAY);
288 278 return (rval);
289 279 }
290 280
291 281 if (dtrace_aggregate_snap(dtp) == -1)
292 282 return (DTRACE_WORKSTATUS_ERROR);
293 283
294 284 if (dtrace_consume(dtp, fp, pfunc, rfunc, arg) == -1)
295 285 return (DTRACE_WORKSTATUS_ERROR);
296 286
297 287 return (rval);
298 288 }
↓ open down ↓ |
95 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX