📄 dt_work.c
字号:
/* * Copyright 2005 Sun Microsystems, Inc. All rights reserved. * * The contents of this file are subject to the terms of the * Common Development and Distribution License, Version 1.0 only. * See the file usr/src/LICENSING.NOTICE in this distribution or * http://www.opensolaris.org/license/ for details. */#pragma ident "@(#)dt_work.c 1.4 04/06/23 SMI"#include <dt_impl.h>#include <stddef.h>#include <errno.h>#include <assert.h>#include <time.h>static const struct { int dtslt_option; size_t dtslt_offs;} _dtrace_sleeptab[] = { { DTRACEOPT_STATUSRATE, offsetof(dtrace_hdl_t, dt_laststatus) }, { DTRACEOPT_AGGRATE, offsetof(dtrace_hdl_t, dt_lastagg) }, { DTRACEOPT_SWITCHRATE, offsetof(dtrace_hdl_t, dt_lastswitch) }, { DTRACEOPT_MAX, 0 }};voiddtrace_sleep(dtrace_hdl_t *dtp){ dt_proc_hash_t *dph = dtp->dt_procs; dt_proc_t *dpr; hrtime_t earliest = INT64_MAX; struct timespec tv; hrtime_t now; int i; for (i = 0; _dtrace_sleeptab[i].dtslt_option < DTRACEOPT_MAX; i++) { uintptr_t a = (uintptr_t)dtp + _dtrace_sleeptab[i].dtslt_offs; int opt = _dtrace_sleeptab[i].dtslt_option; dtrace_optval_t interval = dtp->dt_options[opt]; if (*((hrtime_t *)a) + interval < earliest) earliest = *((hrtime_t *)a) + interval; } (void) pthread_mutex_lock(&dph->dph_lock); now = gethrtime(); if (earliest < now) { (void) pthread_mutex_unlock(&dph->dph_lock); return; /* sleep duration has already past */ } tv.tv_sec = (earliest - now) / NANOSEC; tv.tv_nsec = (earliest - now) % NANOSEC; /* * Wait for either 'tv' nanoseconds to pass or to receive notification * that a process is in an interesting state. Regardless of why we * awaken, iterate over any pending notifications and process them. */ (void) pthread_cond_reltimedwait_np(&dph->dph_cv, &dph->dph_lock, &tv); while ((dpr = dph->dph_notify) != NULL) { dph->dph_notify = dpr->dpr_notify; dpr->dpr_notify = NULL; if (dtp->dt_prochdlr != NULL) dtp->dt_prochdlr(dpr->dpr_proc, dtp->dt_procarg); } (void) pthread_mutex_unlock(&dph->dph_lock);}intdtrace_status(dtrace_hdl_t *dtp){ int gen = dtp->dt_statusgen; dtrace_optval_t interval = dtp->dt_options[DTRACEOPT_STATUSRATE]; hrtime_t now = gethrtime(); if (!dtp->dt_active) return (DTRACE_STATUS_NONE); if (dtp->dt_stopped) return (DTRACE_STATUS_STOPPED); if (dtp->dt_laststatus != 0) { if (now - dtp->dt_laststatus < interval) return (DTRACE_STATUS_NONE); dtp->dt_laststatus += interval; } else { dtp->dt_laststatus = now; } if (dt_ioctl(dtp, DTRACEIOC_STATUS, &dtp->dt_status[gen]) == -1) return (dt_set_errno(dtp, errno)); dtp->dt_statusgen ^= 1; if (dt_handle_status(dtp, &dtp->dt_status[dtp->dt_statusgen], &dtp->dt_status[gen]) == -1) return (-1); if (dtp->dt_status[gen].dtst_exiting) { if (!dtp->dt_stopped) (void) dtrace_stop(dtp); return (DTRACE_STATUS_EXITED); } if (dtp->dt_status[gen].dtst_filled == 0) return (DTRACE_STATUS_OKAY); if (dtp->dt_options[DTRACEOPT_BUFPOLICY] != DTRACEOPT_BUFPOLICY_FILL) return (DTRACE_STATUS_OKAY); if (!dtp->dt_stopped) { if (dtrace_stop(dtp) == -1) return (-1); } return (DTRACE_STATUS_FILLED);}dtrace_workstatus_tdtrace_work(dtrace_hdl_t *dtp, FILE *fp, dtrace_consume_probe_f *pfunc, dtrace_consume_rec_f *rfunc, void *arg){ int status = dtrace_status(dtp); dtrace_optval_t policy = dtp->dt_options[DTRACEOPT_BUFPOLICY]; dtrace_workstatus_t rval; switch (status) { case DTRACE_STATUS_EXITED: case DTRACE_STATUS_FILLED: case DTRACE_STATUS_STOPPED: /* * Tracing is stopped. We now want to force dtrace_consume() * and dtrace_aggregate_snap() to proceed, regardless of * switchrate and aggrate. We do this by clearing the times. */ dtp->dt_lastswitch = 0; dtp->dt_lastagg = 0; rval = DTRACE_WORKSTATUS_DONE; break; case DTRACE_STATUS_NONE: case DTRACE_STATUS_OKAY: rval = DTRACE_WORKSTATUS_OKAY; break; case -1: return (DTRACE_WORKSTATUS_ERROR); } if ((status == DTRACE_STATUS_NONE || status == DTRACE_STATUS_OKAY) && policy != DTRACEOPT_BUFPOLICY_SWITCH) { /* * There either isn't any status or things are fine -- and * this is a "ring" or "fill" buffer. We don't want to consume * any of the trace data or snapshot the aggregations; we just * return. */ assert(rval == DTRACE_WORKSTATUS_OKAY); return (rval); } if (dtrace_aggregate_snap(dtp) == -1) return (DTRACE_WORKSTATUS_ERROR); if (dtrace_consume(dtp, fp, pfunc, rfunc, arg) == -1) return (DTRACE_WORKSTATUS_ERROR); return (rval);}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -