dwrapper.c revision de3d2ce46fc25c7b67ccbae4afe5f15e5357568f
/*
* 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"
/*
* Checks if the process is latencytop itself or sched (if we are not tracing
* sched), we should ignore them.
*/
#define SHOULD_IGNORE(pid) \
/*
* Get an integer value from dtrace record.
*/
static uint64_t
rec_get_value(void *a, size_t b)
{
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);
}
/*
* Callback to process each aggregation in the snapshot.
*/
static int
{
const int REC_PID = 1;
const int REC_TID = 2;
const int REC_STACK = 3;
const int REC_AGG = 4;
const int NREC = 5;
unsigned int stack_depth;
unsigned int pc_size;
int ptrsize;
unsigned int buffersize;
/* Not enough records */
goto err;
}
/* Record is not PID, this is an error. */
goto err;
}
pid = rec_get_value(
if (SHOULD_IGNORE(pid)) {
goto done;
}
/* Record is not TID, this is an error. */
goto err;
}
tid = rec_get_value(
/* Record is not stack(), this is an error. */
goto err;
}
/* 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 last byte in the buffer and
* break early.
*/
break;
} else {
}
}
}
/*
* We have printed something,
* so it is safe to remove last ' '.
*/
*(ptr-1) = 0;
}
/* Parsing aggregation data */
/* Record is not aggregation, this is an error. */
goto err;
}
done:
}
return (0);
err:
}
return (-1);
}
/*
* Callback to process each aggregation in the snapshot.
* This one processes lt_named_*, which contains data such as lock spinning.
*/
static int
{
const int REC_PID = 1;
const int REC_TID = 2;
const int REC_TYPE = 3;
const int REC_AGG = 4;
const int NREC = 5;
int cause_id;
/* Not enough records */
return (-1);
}
/* Record is not PID, this is an error. */
return (-1);
}
pid = rec_get_value(
if (SHOULD_IGNORE(pid)) {
return (0);
}
/* Record is not TID, this is an error. */
return (-1);
}
tid = rec_get_value(
/* Record is not type, this is an error. */
return (-1);
}
/* Parsing aggregation data */
/* Record is not aggregation, this is an error. */
return (-1);
}
return (0);
}
/*
* Callback to process each aggregation in the snapshot.
* This one processes lt_sync_*, which traces synchronization objects.
*/
static int
{
const int REC_PID = 1;
const int REC_TID = 2;
const int REC_STYPE = 3;
const int REC_WCHAN = 4;
const int REC_AGG = 5;
const int NREC = 6;
int stype;
unsigned long long wchan;
/* Not enough records */
return (-1);
}
/* Record is not PID, this is an error. */
return (-1);
}
pid = rec_get_value(
if (SHOULD_IGNORE(pid)) {
return (0);
}
/* Record is not TID, this is an error. */
return (-1);
}
tid = rec_get_value(
/* Record is not stype, this is an error. */
return (-1);
}
/* Record is not wchan, this is an error. */
return (-1);
}
/* Parsing aggregation data */
/* Record is not aggregation, this is an error. */
return (-1);
}
return (0);
}
/*
* Callback to process each aggregation in the snapshot.
* This one dispatches to different aggwalk_*().
*/
/* ARGSUSED */
static int
{
char *tmp;
char buffer[32];
goto done;
}
func = aggwalk_call;
func = aggwalk_sync;
} else {
goto done;
}
} else {
goto done;
}
done:
/* We have our data, remove it from DTrace. */
return (DTRACE_AGGWALK_REMOVE);
}
/*
* Callback to handle DTrace drop data events.
*/
/*ARGSUSED*/
static int
{
/*
* Pretend nothing happened. So our program can continue.
*/
return (DTRACE_HANDLE_OK);
}
/*
* DTrace initialization. The D script is running when this function returns.
*/
int
lt_dtrace_init(void)
{
int err;
/* Open dtrace, set up handler */
lt_display_error("Cannot open dtrace library: %s\n",
return (-1);
}
lt_display_error("Cannot install DTrace handle: %s\n",
return (-1);
}
/* Load D script, set up macro and compile */
#ifdef EMBED_CONFIGS
/* Create a temp file because libdtrace use cpp(1) on files only. */
lt_display_error("Cannot create tmp file\n");
return (-1);
}
#else
lt_display_error("Cannot open script file %s\n",
return (-1);
}
#endif /* EMBED_CONFIGS */
if (g_config.enable_filter) {
}
if (g_config.trace_syncobj) {
}
if (g_config.trace_sched) {
}
if (g_config.low_overhead_mode) {
}
DTRACE_C_CPP, 0, NULL);
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 aggregator data to user space.
* Needs to be called periodically to prevent running out of kernel memory.
*/
int
lt_dtrace_work(int force)
{
}
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 aggregator and collect data to LatencyTOP.
* Different from lt_dtrace_work, this one moves data from libdtrace
* to latencytop.
* This needs to be called immediately before update UI.
*/
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 no need to clear again, because we removed everything.
* Paranoid.
*/
return (0);
}
/*
* Clean up and close DTrace.
*/
void
lt_dtrace_deinit(void)
{
(void) dtrace_stop(g_dtp);
}