xref: /freebsd-src/cddl/contrib/opensolaris/lib/libdtrace/common/dt_work.c (revision 98e0ffaefb0f241cda3a72395d3be04192ae0d47)
16ff6d951SJohn Birrell /*
26ff6d951SJohn Birrell  * CDDL HEADER START
36ff6d951SJohn Birrell  *
46ff6d951SJohn Birrell  * The contents of this file are subject to the terms of the
56ff6d951SJohn Birrell  * Common Development and Distribution License, Version 1.0 only
66ff6d951SJohn Birrell  * (the "License").  You may not use this file except in compliance
76ff6d951SJohn Birrell  * with the License.
86ff6d951SJohn Birrell  *
96ff6d951SJohn Birrell  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
106ff6d951SJohn Birrell  * or http://www.opensolaris.org/os/licensing.
116ff6d951SJohn Birrell  * See the License for the specific language governing permissions
126ff6d951SJohn Birrell  * and limitations under the License.
136ff6d951SJohn Birrell  *
146ff6d951SJohn Birrell  * When distributing Covered Code, include this CDDL HEADER in each
156ff6d951SJohn Birrell  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
166ff6d951SJohn Birrell  * If applicable, add the following below this CDDL HEADER, with the
176ff6d951SJohn Birrell  * fields enclosed by brackets "[]" replaced with your own identifying
186ff6d951SJohn Birrell  * information: Portions Copyright [yyyy] [name of copyright owner]
196ff6d951SJohn Birrell  *
206ff6d951SJohn Birrell  * CDDL HEADER END
216ff6d951SJohn Birrell  */
226ff6d951SJohn Birrell 
236ff6d951SJohn Birrell /*
246ff6d951SJohn Birrell  * Copyright 2006 Sun Microsystems, Inc.  All rights reserved.
256ff6d951SJohn Birrell  * Use is subject to license terms.
266ff6d951SJohn Birrell  */
276ff6d951SJohn Birrell 
286ff6d951SJohn Birrell #pragma ident	"%Z%%M%	%I%	%E% SMI"
296ff6d951SJohn Birrell 
306ff6d951SJohn Birrell #include <dt_impl.h>
316ff6d951SJohn Birrell #include <stddef.h>
326ff6d951SJohn Birrell #include <errno.h>
336ff6d951SJohn Birrell #include <assert.h>
346ff6d951SJohn Birrell #include <time.h>
356ff6d951SJohn Birrell 
366ff6d951SJohn Birrell static const struct {
376ff6d951SJohn Birrell 	int dtslt_option;
386ff6d951SJohn Birrell 	size_t dtslt_offs;
396ff6d951SJohn Birrell } _dtrace_sleeptab[] = {
406ff6d951SJohn Birrell 	{ DTRACEOPT_STATUSRATE, offsetof(dtrace_hdl_t, dt_laststatus) },
416ff6d951SJohn Birrell 	{ DTRACEOPT_AGGRATE, offsetof(dtrace_hdl_t, dt_lastagg) },
426ff6d951SJohn Birrell 	{ DTRACEOPT_SWITCHRATE, offsetof(dtrace_hdl_t, dt_lastswitch) },
436ff6d951SJohn Birrell 	{ DTRACEOPT_MAX, 0 }
446ff6d951SJohn Birrell };
456ff6d951SJohn Birrell 
466ff6d951SJohn Birrell void
dtrace_sleep(dtrace_hdl_t * dtp)476ff6d951SJohn Birrell dtrace_sleep(dtrace_hdl_t *dtp)
486ff6d951SJohn Birrell {
496ff6d951SJohn Birrell 	dt_proc_hash_t *dph = dtp->dt_procs;
506ff6d951SJohn Birrell 	dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY];
516ff6d951SJohn Birrell 	dt_proc_notify_t *dprn;
526ff6d951SJohn Birrell 
536ff6d951SJohn Birrell 	hrtime_t earliest = INT64_MAX;
546ff6d951SJohn Birrell 	struct timespec tv;
556ff6d951SJohn Birrell 	hrtime_t now;
566ff6d951SJohn Birrell 	int i;
576ff6d951SJohn Birrell 
586ff6d951SJohn Birrell 	for (i = 0; _dtrace_sleeptab[i].dtslt_option < DTRACEOPT_MAX; i++) {
596ff6d951SJohn Birrell 		uintptr_t a = (uintptr_t)dtp + _dtrace_sleeptab[i].dtslt_offs;
606ff6d951SJohn Birrell 		int opt = _dtrace_sleeptab[i].dtslt_option;
616ff6d951SJohn Birrell 		dtrace_optval_t interval = dtp->dt_options[opt];
626ff6d951SJohn Birrell 
636ff6d951SJohn Birrell 		/*
646ff6d951SJohn Birrell 		 * If the buffering policy is set to anything other than
656ff6d951SJohn Birrell 		 * "switch", we ignore the aggrate and switchrate -- they're
666ff6d951SJohn Birrell 		 * meaningless.
676ff6d951SJohn Birrell 		 */
686ff6d951SJohn Birrell 		if (policy != DTRACEOPT_BUFPOLICY_SWITCH &&
696ff6d951SJohn Birrell 		    _dtrace_sleeptab[i].dtslt_option != DTRACEOPT_STATUSRATE)
706ff6d951SJohn Birrell 			continue;
716ff6d951SJohn Birrell 
726ff6d951SJohn Birrell 		if (*((hrtime_t *)a) + interval < earliest)
736ff6d951SJohn Birrell 			earliest = *((hrtime_t *)a) + interval;
746ff6d951SJohn Birrell 	}
756ff6d951SJohn Birrell 
766ff6d951SJohn Birrell 	(void) pthread_mutex_lock(&dph->dph_lock);
776ff6d951SJohn Birrell 
786ff6d951SJohn Birrell 	now = gethrtime();
796ff6d951SJohn Birrell 
806ff6d951SJohn Birrell 	if (earliest < now) {
816ff6d951SJohn Birrell 		(void) pthread_mutex_unlock(&dph->dph_lock);
826ff6d951SJohn Birrell 		return; /* sleep duration has already past */
836ff6d951SJohn Birrell 	}
846ff6d951SJohn Birrell 
85*bc96366cSSteven Hartland #ifdef illumos
866ff6d951SJohn Birrell 	tv.tv_sec = (earliest - now) / NANOSEC;
876ff6d951SJohn Birrell 	tv.tv_nsec = (earliest - now) % NANOSEC;
886ff6d951SJohn Birrell 
896ff6d951SJohn Birrell 	/*
906ff6d951SJohn Birrell 	 * Wait for either 'tv' nanoseconds to pass or to receive notification
916ff6d951SJohn Birrell 	 * that a process is in an interesting state.  Regardless of why we
926ff6d951SJohn Birrell 	 * awaken, iterate over any pending notifications and process them.
936ff6d951SJohn Birrell 	 */
946ff6d951SJohn Birrell 	(void) pthread_cond_reltimedwait_np(&dph->dph_cv, &dph->dph_lock, &tv);
951cbe5a98SJohn Birrell #else
961cbe5a98SJohn Birrell 	earliest -= now;
971cbe5a98SJohn Birrell 	clock_gettime(CLOCK_REALTIME,&tv);
981cbe5a98SJohn Birrell 	tv.tv_sec += earliest / NANOSEC;
991cbe5a98SJohn Birrell 	tv.tv_nsec += earliest % NANOSEC;
1001cbe5a98SJohn Birrell 	while (tv.tv_nsec > NANOSEC) {
1011cbe5a98SJohn Birrell 		tv.tv_sec += 1;
1021cbe5a98SJohn Birrell 		tv.tv_nsec -= NANOSEC;
1031cbe5a98SJohn Birrell 	}
1041cbe5a98SJohn Birrell 
1051cbe5a98SJohn Birrell 	/*
1061cbe5a98SJohn Birrell 	 * Wait for either 'tv' nanoseconds to pass or to receive notification
1071cbe5a98SJohn Birrell 	 * that a process is in an interesting state.  Regardless of why we
1081cbe5a98SJohn Birrell 	 * awaken, iterate over any pending notifications and process them.
1091cbe5a98SJohn Birrell 	 */
1101cbe5a98SJohn Birrell 	(void) pthread_cond_timedwait(&dph->dph_cv, &dph->dph_lock, &tv);
1111cbe5a98SJohn Birrell #endif
1126ff6d951SJohn Birrell 
1136ff6d951SJohn Birrell 	while ((dprn = dph->dph_notify) != NULL) {
1146ff6d951SJohn Birrell 		if (dtp->dt_prochdlr != NULL) {
1156ff6d951SJohn Birrell 			char *err = dprn->dprn_errmsg;
1166ff6d951SJohn Birrell 			if (*err == '\0')
1176ff6d951SJohn Birrell 				err = NULL;
1186ff6d951SJohn Birrell 
1196ff6d951SJohn Birrell 			dtp->dt_prochdlr(dprn->dprn_dpr->dpr_proc, err,
1206ff6d951SJohn Birrell 			    dtp->dt_procarg);
1216ff6d951SJohn Birrell 		}
1226ff6d951SJohn Birrell 
1236ff6d951SJohn Birrell 		dph->dph_notify = dprn->dprn_next;
1246ff6d951SJohn Birrell 		dt_free(dtp, dprn);
1256ff6d951SJohn Birrell 	}
1266ff6d951SJohn Birrell 
1276ff6d951SJohn Birrell 	(void) pthread_mutex_unlock(&dph->dph_lock);
1286ff6d951SJohn Birrell }
1296ff6d951SJohn Birrell 
1306ff6d951SJohn Birrell int
dtrace_status(dtrace_hdl_t * dtp)1316ff6d951SJohn Birrell dtrace_status(dtrace_hdl_t *dtp)
1326ff6d951SJohn Birrell {
1336ff6d951SJohn Birrell 	int gen = dtp->dt_statusgen;
1346ff6d951SJohn Birrell 	dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_STATUSRATE];
1356ff6d951SJohn Birrell 	hrtime_t now = gethrtime();
1366ff6d951SJohn Birrell 
1376ff6d951SJohn Birrell 	if (!dtp->dt_active)
1386ff6d951SJohn Birrell 		return (DTRACE_STATUS_NONE);
1396ff6d951SJohn Birrell 
1406ff6d951SJohn Birrell 	if (dtp->dt_stopped)
1416ff6d951SJohn Birrell 		return (DTRACE_STATUS_STOPPED);
1426ff6d951SJohn Birrell 
1436ff6d951SJohn Birrell 	if (dtp->dt_laststatus != 0) {
1446ff6d951SJohn Birrell 		if (now - dtp->dt_laststatus < interval)
1456ff6d951SJohn Birrell 			return (DTRACE_STATUS_NONE);
1466ff6d951SJohn Birrell 
1476ff6d951SJohn Birrell 		dtp->dt_laststatus += interval;
1486ff6d951SJohn Birrell 	} else {
1496ff6d951SJohn Birrell 		dtp->dt_laststatus = now;
1506ff6d951SJohn Birrell 	}
1516ff6d951SJohn Birrell 
1526ff6d951SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1)
1536ff6d951SJohn Birrell 		return (dt_set_errno(dtp, errno));
1546ff6d951SJohn Birrell 
1556ff6d951SJohn Birrell 	dtp->dt_statusgen ^= 1;
1566ff6d951SJohn Birrell 
1576ff6d951SJohn Birrell 	if (dt_handle_status(dtp, &dtp->dt_status[dtp->dt_statusgen],
1586ff6d951SJohn Birrell 	    &dtp->dt_status[gen]) == -1)
1596ff6d951SJohn Birrell 		return (-1);
1606ff6d951SJohn Birrell 
1616ff6d951SJohn Birrell 	if (dtp->dt_status[gen].dtst_exiting) {
1626ff6d951SJohn Birrell 		if (!dtp->dt_stopped)
1636ff6d951SJohn Birrell 			(void) dtrace_stop(dtp);
1646ff6d951SJohn Birrell 
1656ff6d951SJohn Birrell 		return (DTRACE_STATUS_EXITED);
1666ff6d951SJohn Birrell 	}
1676ff6d951SJohn Birrell 
1686ff6d951SJohn Birrell 	if (dtp->dt_status[gen].dtst_filled == 0)
1696ff6d951SJohn Birrell 		return (DTRACE_STATUS_OKAY);
1706ff6d951SJohn Birrell 
1716ff6d951SJohn Birrell 	if (dtp->dt_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL)
1726ff6d951SJohn Birrell 		return (DTRACE_STATUS_OKAY);
1736ff6d951SJohn Birrell 
1746ff6d951SJohn Birrell 	if (!dtp->dt_stopped) {
1756ff6d951SJohn Birrell 		if (dtrace_stop(dtp) == -1)
1766ff6d951SJohn Birrell 			return (-1);
1776ff6d951SJohn Birrell 	}
1786ff6d951SJohn Birrell 
1796ff6d951SJohn Birrell 	return (DTRACE_STATUS_FILLED);
1806ff6d951SJohn Birrell }
1816ff6d951SJohn Birrell 
1826ff6d951SJohn Birrell int
dtrace_go(dtrace_hdl_t * dtp)1836ff6d951SJohn Birrell dtrace_go(dtrace_hdl_t *dtp)
1846ff6d951SJohn Birrell {
1851cbe5a98SJohn Birrell 	dtrace_enable_io_t args;
1866ff6d951SJohn Birrell 	void *dof;
18747890cb5SMark Johnston 	int error, r;
1886ff6d951SJohn Birrell 
1896ff6d951SJohn Birrell 	if (dtp->dt_active)
1906ff6d951SJohn Birrell 		return (dt_set_errno(dtp, EINVAL));
1916ff6d951SJohn Birrell 
1926ff6d951SJohn Birrell 	/*
1936ff6d951SJohn Birrell 	 * If a dtrace:::ERROR program and callback are registered, enable the
1946ff6d951SJohn Birrell 	 * program before we start tracing.  If this fails for a vector open
1956ff6d951SJohn Birrell 	 * with ENOTTY, we permit dtrace_go() to succeed so that vector clients
1966ff6d951SJohn Birrell 	 * such as mdb's dtrace module can execute the rest of dtrace_go() even
1976ff6d951SJohn Birrell 	 * though they do not provide support for the DTRACEIOC_ENABLE ioctl.
1986ff6d951SJohn Birrell 	 */
1996ff6d951SJohn Birrell 	if (dtp->dt_errprog != NULL &&
2006ff6d951SJohn Birrell 	    dtrace_program_exec(dtp, dtp->dt_errprog, NULL) == -1 && (
2016ff6d951SJohn Birrell 	    dtp->dt_errno != ENOTTY || dtp->dt_vector == NULL))
2026ff6d951SJohn Birrell 		return (-1); /* dt_errno has been set for us */
2036ff6d951SJohn Birrell 
2046ff6d951SJohn Birrell 	if ((dof = dtrace_getopt_dof(dtp)) == NULL)
2056ff6d951SJohn Birrell 		return (-1); /* dt_errno has been set for us */
2066ff6d951SJohn Birrell 
2071cbe5a98SJohn Birrell 	args.dof = dof;
2081cbe5a98SJohn Birrell 	args.n_matched = 0;
20947890cb5SMark Johnston 	r = dt_ioctl(dtp, DTRACEIOC_ENABLE, &args);
21047890cb5SMark Johnston 	error = errno;
2116ff6d951SJohn Birrell 	dtrace_dof_destroy(dtp, dof);
2126ff6d951SJohn Birrell 
21347890cb5SMark Johnston 	if (r == -1 && (error != ENOTTY || dtp->dt_vector == NULL))
21447890cb5SMark Johnston 		return (dt_set_errno(dtp, error));
2156ff6d951SJohn Birrell 
2166ff6d951SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_GO, &dtp->dt_beganon) == -1) {
2176ff6d951SJohn Birrell 		if (errno == EACCES)
2186ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_DESTRUCTIVE));
2196ff6d951SJohn Birrell 
2206ff6d951SJohn Birrell 		if (errno == EALREADY)
2216ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_ISANON));
2226ff6d951SJohn Birrell 
2236ff6d951SJohn Birrell 		if (errno == ENOENT)
2246ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_NOANON));
2256ff6d951SJohn Birrell 
2266ff6d951SJohn Birrell 		if (errno == E2BIG)
2276ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_ENDTOOBIG));
2286ff6d951SJohn Birrell 
2296ff6d951SJohn Birrell 		if (errno == ENOSPC)
2306ff6d951SJohn Birrell 			return (dt_set_errno(dtp, EDT_BUFTOOSMALL));
2316ff6d951SJohn Birrell 
2326ff6d951SJohn Birrell 		return (dt_set_errno(dtp, errno));
2336ff6d951SJohn Birrell 	}
2346ff6d951SJohn Birrell 
2356ff6d951SJohn Birrell 	dtp->dt_active = 1;
2366ff6d951SJohn Birrell 
2376ff6d951SJohn Birrell 	if (dt_options_load(dtp) == -1)
2386ff6d951SJohn Birrell 		return (dt_set_errno(dtp, errno));
2396ff6d951SJohn Birrell 
2406ff6d951SJohn Birrell 	return (dt_aggregate_go(dtp));
2416ff6d951SJohn Birrell }
2426ff6d951SJohn Birrell 
2436ff6d951SJohn Birrell int
dtrace_stop(dtrace_hdl_t * dtp)2446ff6d951SJohn Birrell dtrace_stop(dtrace_hdl_t *dtp)
2456ff6d951SJohn Birrell {
2466ff6d951SJohn Birrell 	int gen = dtp->dt_statusgen;
2476ff6d951SJohn Birrell 
2486ff6d951SJohn Birrell 	if (dtp->dt_stopped)
2496ff6d951SJohn Birrell 		return (0);
2506ff6d951SJohn Birrell 
2516ff6d951SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_STOP, &dtp->dt_endedon) == -1)
2526ff6d951SJohn Birrell 		return (dt_set_errno(dtp, errno));
2536ff6d951SJohn Birrell 
2546ff6d951SJohn Birrell 	dtp->dt_stopped = 1;
2556ff6d951SJohn Birrell 
2566ff6d951SJohn Birrell 	/*
2576ff6d951SJohn Birrell 	 * Now that we're stopped, we're going to get status one final time.
2586ff6d951SJohn Birrell 	 */
2596ff6d951SJohn Birrell 	if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1)
2606ff6d951SJohn Birrell 		return (dt_set_errno(dtp, errno));
2616ff6d951SJohn Birrell 
2626ff6d951SJohn Birrell 	if (dt_handle_status(dtp, &dtp->dt_status[gen ^ 1],
2636ff6d951SJohn Birrell 	    &dtp->dt_status[gen]) == -1)
2646ff6d951SJohn Birrell 		return (-1);
2656ff6d951SJohn Birrell 
2666ff6d951SJohn Birrell 	return (0);
2676ff6d951SJohn Birrell }
2686ff6d951SJohn Birrell 
2696ff6d951SJohn Birrell 
2706ff6d951SJohn Birrell dtrace_workstatus_t
dtrace_work(dtrace_hdl_t * dtp,FILE * fp,dtrace_consume_probe_f * pfunc,dtrace_consume_rec_f * rfunc,void * arg)2716ff6d951SJohn Birrell dtrace_work(dtrace_hdl_t *dtp, FILE *fp,
2726ff6d951SJohn Birrell     dtrace_consume_probe_f *pfunc, dtrace_consume_rec_f *rfunc, void *arg)
2736ff6d951SJohn Birrell {
2746ff6d951SJohn Birrell 	int status = dtrace_status(dtp);
2756ff6d951SJohn Birrell 	dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY];
2766ff6d951SJohn Birrell 	dtrace_workstatus_t rval;
2776ff6d951SJohn Birrell 
2786ff6d951SJohn Birrell 	switch (status) {
2796ff6d951SJohn Birrell 	case DTRACE_STATUS_EXITED:
2806ff6d951SJohn Birrell 	case DTRACE_STATUS_FILLED:
2816ff6d951SJohn Birrell 	case DTRACE_STATUS_STOPPED:
2826ff6d951SJohn Birrell 		/*
2836ff6d951SJohn Birrell 		 * Tracing is stopped.  We now want to force dtrace_consume()
2846ff6d951SJohn Birrell 		 * and dtrace_aggregate_snap() to proceed, regardless of
2856ff6d951SJohn Birrell 		 * switchrate and aggrate.  We do this by clearing the times.
2866ff6d951SJohn Birrell 		 */
2876ff6d951SJohn Birrell 		dtp->dt_lastswitch = 0;
2886ff6d951SJohn Birrell 		dtp->dt_lastagg = 0;
2896ff6d951SJohn Birrell 		rval = DTRACE_WORKSTATUS_DONE;
2906ff6d951SJohn Birrell 		break;
2916ff6d951SJohn Birrell 
2926ff6d951SJohn Birrell 	case DTRACE_STATUS_NONE:
2936ff6d951SJohn Birrell 	case DTRACE_STATUS_OKAY:
2946ff6d951SJohn Birrell 		rval = DTRACE_WORKSTATUS_OKAY;
2956ff6d951SJohn Birrell 		break;
2966ff6d951SJohn Birrell 
2976ff6d951SJohn Birrell 	case -1:
2986ff6d951SJohn Birrell 		return (DTRACE_WORKSTATUS_ERROR);
2996ff6d951SJohn Birrell 	}
3006ff6d951SJohn Birrell 
3016ff6d951SJohn Birrell 	if ((status == DTRACE_STATUS_NONE || status == DTRACE_STATUS_OKAY) &&
3026ff6d951SJohn Birrell 	    policy != DTRACEOPT_BUFPOLICY_SWITCH) {
3036ff6d951SJohn Birrell 		/*
3046ff6d951SJohn Birrell 		 * There either isn't any status or things are fine -- and
3056ff6d951SJohn Birrell 		 * this is a "ring" or "fill" buffer.  We don't want to consume
3066ff6d951SJohn Birrell 		 * any of the trace data or snapshot the aggregations; we just
3076ff6d951SJohn Birrell 		 * return.
3086ff6d951SJohn Birrell 		 */
3096ff6d951SJohn Birrell 		assert(rval == DTRACE_WORKSTATUS_OKAY);
3106ff6d951SJohn Birrell 		return (rval);
3116ff6d951SJohn Birrell 	}
3126ff6d951SJohn Birrell 
3136ff6d951SJohn Birrell 	if (dtrace_aggregate_snap(dtp) == -1)
3146ff6d951SJohn Birrell 		return (DTRACE_WORKSTATUS_ERROR);
3156ff6d951SJohn Birrell 
3166ff6d951SJohn Birrell 	if (dtrace_consume(dtp, fp, pfunc, rfunc, arg) == -1)
3176ff6d951SJohn Birrell 		return (DTRACE_WORKSTATUS_ERROR);
3186ff6d951SJohn Birrell 
3196ff6d951SJohn Birrell 	return (rval);
3206ff6d951SJohn Birrell }
321