/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License (the "License").
* You may not use this file except in compliance with the License.
*
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
* See the License for the specific language governing permissions
* and limitations under the License.
*
* When distributing Covered Code, include this CDDL HEADER in each
* file and include the License file at usr/src/OPENSOLARIS.LICENSE.
* If applicable, add the following below this CDDL HEADER, with the
* fields enclosed by brackets "[]" replaced with your own identifying
* information: Portions Copyright [yyyy] [name of copyright owner]
*
* CDDL HEADER END
*/
/*
* Copyright (c) 2008-2009, Intel Corporation.
* All Rights Reserved.
*/
#include <unistd.h>
#include <stdio.h>
#include <dtrace.h>
#include <string.h>
#include <stdlib.h>
#include <memory.h>
#include <limits.h>
#include "latencytop.h"
/*
* Ignore sched if sched is not tracked.
* Also ignore ourselves (i.e., latencytop).
*/
/*
* Get an integer value from dtrace record.
*/
static uint64_t
{
switch (b) {
case sizeof (uint64_t):
break;
case sizeof (uint32_t):
break;
case sizeof (uint16_t):
break;
case sizeof (uint8_t):
break;
default:
break;
}
return (ret);
}
/*
* activities) in the snapshot.
*/
static int
{
unsigned int stack_depth;
unsigned int pc_size;
int ptrsize;
unsigned int buffersize;
unsigned int priority;
NREC };
/* Check action type */
return (-1);
}
pid = rec_get_value(
if (SHOULD_IGNORE(pid)) {
return (0);
}
tid = rec_get_value(
/* Parse stack array from dtagd_rec */
/* Print the stack */
while (stack_depth > 0) {
if (pc == 0) {
break;
}
int len;
if (ptrsize <= 0) {
/*
* snprintf returns "desired" length, so
* reaching here means our buffer is full.
* Move ptr to the last byte of the buffer and
* break.
*/
break;
} else {
}
}
}
/*
* We have printed something, so it is safe to remove
* the last ' '.
*/
}
}
return (0);
}
/*
* Callback to process aggregation lt_named_* (related to lock spinning etc.),
* in the snapshot.
*/
static int
{
int cause_id;
/* Check action type */
return (-1);
}
pid = rec_get_value(
if (SHOULD_IGNORE(pid)) {
return (0);
}
tid = rec_get_value(
return (0);
}
/*
* Callback to process aggregation lt_sync_* (related to synchronization
* objects), in the snapshot.
*/
static int
{
int stype;
unsigned long long wchan;
/* Check action type */
return (-1);
}
pid = rec_get_value(
if (SHOULD_IGNORE(pid)) {
return (0);
}
tid = rec_get_value(
return (0);
}
/*
* Callback to process various aggregations in the snapshot. Called by
* different aggwalk_* functions.
*/
/* ARGSUSED */
static int
{
char *tmp;
goto done;
}
goto done;
func = aggwalk_call;
func = aggwalk_sync;
} else {
goto done;
}
goto done;
} else {
goto done;
}
done:
/* We have our data, so remove it from DTrace now */
return (DTRACE_AGGWALK_REMOVE);
}
/*
* Callback to handle event caused by DTrace dropping data.
*/
/*ARGSUSED*/
static int
{
/* Pretend nothing happened, so just continue */
return (DTRACE_HANDLE_OK);
}
#ifndef EMBED_CONFIGS
/*
* Copy the content from a "real" file into a temp file.
*/
static int
{
int bytes;
return (-1);
}
return (-1);
}
}
return (0);
}
#endif
/*
* DTrace initialization. D script starts running when this function returns.
*/
int
lt_dtrace_init(void)
{
int err;
lt_display_error("Cannot open dtrace library: %s\n",
return (-1);
}
lt_display_error("Cannot install DTrace handle: %s\n",
return (-1);
}
if (g_config.lt_cfg_enable_filter) {
"ENABLE_FILTER")) != 0) {
"Failed to set option ENABLE_FILTER.\n");
return (err);
}
}
if (g_config.lt_cfg_trace_syncobj) {
"ENABLE_SYNCOBJ")) != 0) {
"Failed to set option ENABLE_SYNCOBJ.\n");
return (err);
}
}
if (g_config.lt_cfg_trace_sched) {
"ENABLE_SCHED")) != 0) {
"Failed to set option ENABLE_SCHED.\n");
return (err);
}
}
if (g_config.lt_cfg_trace_pid != 0) {
"Failed to set option TRACE_PID.\n");
return (err);
}
}
if (g_config.lt_cfg_trace_pgid != 0) {
"Failed to set option TRACE_PGID.\n");
return (err);
}
}
if (g_config.lt_cfg_low_overhead_mode) {
"ENABLE_LOW_OVERHEAD")) != 0) {
"Failed to set option ENABLE_LOW_OVERHEAD.\n");
return (err);
}
}
/* Create a temp file; libdtrace needs it for cpp(1) */
lt_display_error("Cannot create tmp file\n");
return (-1);
}
/* Copy the main D script into the temp file */
#ifdef EMBED_CONFIGS
if (fwrite(&latencytop_d_start,
!= 1) {
lt_display_error("Could not copy D script, fwrite() failed\n");
return (-1);
}
#else
lt_display_error("Cannot open script file %s\n",
return (-1);
}
#endif /* EMBED_CONFIGS */
if (lt_table_append_trans(fp_script) != 0) {
return (-1);
}
lt_display_error("Failed to compile D script.\n");
return (dtrace_errno(g_dtp));
}
/* Execute the D script */
lt_display_error("Failed to enable probes.\n");
return (dtrace_errno(g_dtp));
}
lt_display_error("Failed to run D script.\n");
return (dtrace_errno(g_dtp));
}
return (0);
}
/*
* Worker function to move aggregate data to user space. Called periodically
* to prevent the kernel from running out of memory.
*/
int
{
}
lt_display_error("Failed when getting status: %s\n",
return (-1);
}
if (dtrace_aggregate_snap(g_dtp) != 0) {
lt_display_error("Failed to snap aggregate: %s\n",
return (-1);
}
return (0);
}
/*
* Walk through dtrace aggregator and collect data for latencytop to display.
* Called immediately before UI update.
*/
int
lt_dtrace_collect(void)
{
if (lt_dtrace_work(1) != 0) {
return (-1);
}
lt_display_error("Failed to sort aggregate: %s\n",
return (-1);
}
/*
* Probably we don't need to clear again, because we have removed
* everything. Paranoid ?
*/
return (0);
}
/*
* dtrace clean up.
*/
int
lt_dtrace_deinit(void)
{
int ret = 0;
if (dtrace_stop(g_dtp) != 0) {
lt_display_error("dtrace_stop failed: %s\n",
ret = -1;
}
return (ret);
}