latencytop.c revision 1a1f79f5a554655d51ccfdc74953bc86dc99d174
/*
* 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 <getopt.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <limits.h>
#include <libgen.h>
#include <signal.h>
#include "latencytop.h"
/*
* This variable is used to check if "dynamic variable drop" in dtrace
* has happened.
*/
typedef enum {
LT_CMDOPT__LAST /* Must be last one */
/*
* Check for duplicate command line options.
* Returns TRUE if duplicate options with different values are found,
* returns FALSE otherwise.
*/
static int
static int opt_set[(int)LT_CMDOPT__LAST];
const char *errmsg[] = {
"-t is set more than once with different values.",
"-o is set more than once.",
"-k is set more than once with different values.",
"-l is set more than once with different values.",
"-c is set more than once.",
"-f [no]filter is set more than once with different values.",
"-f [no]sched is set more than once with different values.",
"-f [no]sobj is set more than once with different values.",
"-f [no]low is set more than once with different values.",
};
return (FALSE);
}
return (TRUE);
}
return (FALSE);
}
/*
* Print command-line help message.
*/
static void
{
if (!long_help) {
/* Print short help to stderr. */
return;
}
(void) printf("Options:\n"
" -h, --help\n"
" Print this help.\n"
" -t, --interval TIME\n"
" Set refresh interval to TIME. "
"Valid range [1...60] seconds, default = 5\n"
/*
* Option "-c, --config FILE" is not user-visible for now.
* When we have chance to properly document the format of translation
* rules, we'll make it user-visible.
*/
" -o, --output-log-file FILE\n"
" Output kernel log to FILE. Default = "
DEFAULT_KLOG_FILE "\n"
" -k, --kernel-log-level LEVEL\n"
" Set kernel log level to LEVEL.\n"
" 0(default) = None, 1 = Unmapped, 2 = Mapped, 3 = All.\n"
" -f, --feature [no]feature1,[no]feature2,...\n"
" [no]filter:\n"
" Filter large interruptible latencies, e.g. sleep.\n"
" [no]sched:\n"
" Monitors sched (PID=0).\n"
" [no]sobj:\n"
" Monitors synchronization objects.\n"
" [no]low:\n"
" Lower overhead by sampling small latencies.\n"
" -l, --log-period TIME\n"
" Write and restart log every TIME seconds, TIME >= 60\n");
}
/*
* Properly exit latencytop when it receives SIGINT or SIGTERM.
*/
/* ARGSUSED */
static void
signal_handler(int sig)
{
lt_gpipe_break("q");
}
/*
* Convert string to integer. It returns error if extra characters are found.
*/
static int
{
long ret;
return (-1);
}
return (-1);
}
return (0);
}
/*
* The main function.
*/
int
{
const char *opt_string = "t:o:k:hf:l:c:";
};
int optc;
int longind = 0;
int running = 1;
int unknown_option = FALSE;
int refresh_interval = 5;
int klog_level = 0;
int log_interval = 0;
long long last_logged = 0;
int retval = 0;
int gpipe;
int err;
/* Default global settings */
/* dtrace snapshot every 1 second */
#ifdef EMBED_CONFIGS
#else
#endif
/* Parse command line arguments. */
switch (optc) {
case 'h':
goto end_none;
case 't':
"Invalid refresh interval: %s\n", optarg);
} else if (check_opt_dup(LT_CMDOPT_INTERVAL,
refresh_interval)) {
}
break;
case 'k':
lt_klog_set_log_level(klog_level) != 0) {
"Invalid log level: %s\n", optarg);
} else if (check_opt_dup(LT_CMDOPT_LOG_LEVEL,
refresh_interval)) {
}
break;
case 'o':
"Log file name is too long: %s\n",
optarg);
} else {
sizeof (logfile));
}
break;
case 'f':
int v = TRUE;
v = FALSE;
}
v)) {
} else {
= v;
}
v)) {
} else {
= v;
}
v)) {
} else {
= v;
}
v)) {
} else {
= v;
}
} else {
"Unknown feature: %s\n", token);
}
}
break;
case 'l':
log_interval < 60) {
"Invalid log interval: %s\n", optarg);
} else if (check_opt_dup(LT_CMDOPT_LOG_INTERVAL,
log_interval)) {
}
break;
case 'c':
"Configuration name is too long.\n");
} else if (check_opt_dup(LT_CMDOPT_CONFIG_FILE,
optind)) {
} else {
}
break;
default:
break;
}
}
if (err == -1) {
lt_display_error("Log file name is too long: %s\n",
logfile);
} else if (err == -2) {
lt_display_error("Cannot write to log file: %s\n",
logfile);
}
}
}
}
if (unknown_option) {
retval = 1;
goto end_none;
}
/*
* Initialization
*/
lt_klog_init();
if (lt_table_init() != 0) {
lt_display_error("Unable to load configuration table.\n");
retval = 1;
goto end_notable;
}
if (lt_dtrace_init() != 0) {
lt_display_error("Unable to initialize dtrace.\n");
retval = 1;
goto end_nodtrace;
}
(void) printf("Collecting data for %d seconds...\n",
gpipe = lt_gpipe_readfd();
for (;;) {
if (tsleep <= 0) {
break;
}
/*
* Interval when we call dtrace_status() and collect
* aggregated data.
*/
}
goto end_ubreak;
}
(void) lt_dtrace_work(0);
}
do {
(void) lt_dtrace_collect();
if (log_interval > 0 &&
}
/*
* This is to avoid dynamic variable drop
* in DTrace.
*/
if (lt_drop_detected == B_TRUE) {
if (lt_dtrace_deinit() != 0) {
retval = 1;
break;
}
if (lt_dtrace_init() != 0) {
retval = 1;
break;
}
}
} while (running != 0);
/* Cleanup */
retval = 1;
}
return (retval);
}