busstat.c revision 7c478bd95313f5f23a4c958a745db2134aa03244
/*
* CDDL HEADER START
*
* The contents of this file are subject to the terms of the
* Common Development and Distribution License, Version 1.0 only
* (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 2004 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#pragma ident "%Z%%M% %I% %E% SMI"
#include <stdio.h>
#include <stdlib.h>
#include <strings.h>
#include <time.h>
#include <signal.h>
#include <sys/systeminfo.h>
#include <limits.h>
#include <signal.h>
#include <fcntl.h>
#include <unistd.h>
#include <stropts.h>
#include <locale.h>
#include <libintl.h>
#include <libgen.h>
#include <nl_types.h>
#include <kstat.h>
#include <ctype.h>
#include <signal.h>
#include <errno.h>
#include <time.h>
#include "busstat.h"
/* Global defines */
static int max_pic_num = 1;
static int initial_read = TRUE;
static char *pgmname;
/*
* Global flags.
*/
static char curr_dev_name[KSTAT_STRLEN];
static int curr_inst_num;
static void print_evt(void);
static void print_dev(int, char *);
static void parse_cmd(int);
static void parse_dev_inst(char *);
static void parse_pic_evt(char *);
static void add_dev_node(char *, int);
static void add_all_dev_node(char *);
static void add_evt_node(dev_node_t *);
static void modify_evt_node(dev_node_t *, char *);
static void prune_evt_nodes(dev_node_t *);
static void setup_evts(void);
static void set_evt(dev_node_t *);
static void read_evts(void);
static void check_dr_ops(void);
static void remove_dev_node(dev_node_t *);
static dev_node_t *find_dev_node(char *, int, int);
static kstat_t *find_pic_kstat(char *, int, char *);
static void print_banner(void);
static void print_timestamp(void);
static void usage(void);
static void *safe_malloc(size_t);
static void set_timer(int);
static void handle_sig(int);
static int strisnum(const char *);
int
{
int c, i;
int count = 0; /* Number of times to sample */
int write_evts = FALSE;
int pos = 0;
#if !defined(TEXT_DOMAIN)
#define TEXT_DOMAIN "SYS_TEST"
#endif
/* For I18N */
(void) textdomain(TEXT_DOMAIN);
exit(1);
}
switch (c) {
case 'a':
break;
case 'e':
(void) print_evt();
break;
case 'h':
usage();
break;
case 'l':
break;
case 'n':
break;
case 'r':
break;
case 'w':
write_evts = TRUE;
break;
default:
"option\n"), pgmname);
usage();
break;
}
}
usage();
/*
* validate remaining operands are numeric.
*/
gettext("%s: syntax error\n"),
pgmname);
usage();
}
pos++;
}
"interval value\n"), pgmname);
exit(1);
}
optind++;
"invalid iteration value.\n"),
pgmname);
exit(1);
}
}
/*
* Set events for the first time.
*/
if (write_evts == TRUE)
setup_evts();
if (count > 0) {
for (i = 0; i < count; i++) {
if (banner)
print_banner();
check_dr_ops();
read_evts();
(void) pause();
}
} else {
for (;;) {
if (banner)
print_banner();
check_dr_ops();
read_evts();
(void) pause();
}
}
read_evts();
return (0);
}
/*
* Display all the events that can be set on a device.
*/
void
{
char *value;
int inst_num = -1;
int i = 0;
int j;
/*
* Search through the value string for a numeric char which will
* be the device instance number, if the user specified one. If
* the user did not specify an instance then the return value from
* strscpn will be equal to the string length. In this case we
* use a default value of -1 for the kstat_lookup which causes
* the device number to be ignored during the search.
*/
device[i] = '\0';
}
/*
* No instance specified.
*/
/*
* Get the "counters" kstat, so that we can get
* the names of the "picN" kstats, which hold the
* event names.
*/
== NULL) {
exit(1);
}
"kstat.\n"), pgmname);
exit(1);
}
/*
* Start at 1 as the first entry in the "counters"
* name of the "picN" kstats. For each one found store
* a pointer to it in pic_data[].
*/
"structure.\n"), pgmname);
exit(1);
}
"pic kstat data structure for %s\n"),
exit(1);
}
"pic kstat.\n"), pgmname);
exit(1);
}
}
(void) printf("\n");
}
exit(0);
}
/*
* Display the names and instances of the devices on the system
* which can support performance monitoring.
*/
void
{
static int first_time = 1;
"permitted with -l option.\n"),
pgmname);
usage();
exit(1);
}
/*
* For each device node, print the node name (device
* name) and the instance numbers.
*/
if (first_time) {
"Device(s):\n"));
first_time = 0;
}
ksp->ks_instance);
}
}
if (first_time)
"in system."), pgmname);
(void) printf("\n");
exit(0);
}
/*
* Parses the cmd line, checks all the values and
* creates the appropiate data structures.
*/
void
{
int arg_num = 0;
/*
* First arg must be device name.
*/
if (!arg_num) {
} else {
"event names or pic values not "
"permitted with -r option.\n"),
pgmname);
usage();
exit(1);
}
/*
* Now dealing with pic values.
*/
}
/*
* After first strtok call, must set first arg
* to null if wish to parse rest of string.
* See strtok man page.
*/
arg_num++;
}
}
/*
* command line.
*/
void
parse_dev_inst(char *value)
{
int i;
int malloc_flag = FALSE;
pgmname);
exit(1);
}
/*
* Break string into device name and
* instance number (if given).
*/
device[i] = '\0';
malloc_flag = TRUE;
}
}
/*
* No instance was specified so we assume
* the user wants to use ALL instances.
*/
"specified\n"), pgmname);
exit(1);
}
/*
* Set global flags.
*/
curr_inst_num = -1;
goto clean_up;
}
/*
* Set global flags.
*/
if (malloc_flag) {
}
}
/*
* Adds new event nodes to existing ones, modifies existing ones, or
* prunes existing ones.
*
* A specific instance call will overwrite an earlier all
* instances call, but *not* vice-versa.
*
* All the state transitions are given below.
*
*
* Call Type
* STATE | Specific Instance All Instances.
* ======================================================
* INIT | Change state to | Change state to ALL,
* | INST, add events | add events.
* | |
* INST | State unchanged, | No change.
* | Add events. |
* | |
* ALL | Change state to | State unchanged,
* | INST, replace events. | add events.
*/
void
parse_pic_evt(char *value)
{
char *evt_name;
int pic_num;
"specified.\n"), pgmname);
exit(1);
}
"specifier\n"), pgmname);
usage();
}
/*
* Step over the 'pic' part of the string to
* get the pic number.
*/
"number.\n"), pgmname);
exit(1);
}
"specified.\n"), pgmname);
exit(1);
}
/*
* Dealing with a specific instance.
*/
if (curr_inst_num >= 0) {
"data structures for %s\n"),
exit(1);
}
}
return;
}
/*
* Dealing with all instances of a specific device.
*/
evt_name);
}
}
}
}
/*
* Create a dev_node structure for this device if one does not
* already exist.
*/
void
{
int pic_num;
"name or instance (%s%d)\n"), pgmname,
exit(1);
}
exit(1);
}
"kstat structure.\n"), pgmname);
exit(1);
}
/*
* max_pic_num used to format headers correctly
* for printing.
*/
/* for each pic... */
/* Node already exists */
continue;
}
"pic kstat structure for %s.\n"),
exit(1);
}
if (dev_list_head == NULL) {
}
}
}
/*
* Add all possible instances of a device.
*/
void
add_all_dev_node(char *dev_name)
{
int match = 0;
match = 1;
}
}
if (match == 0) {
gettext("%s: invalid device name (%s)\n"),
exit(1);
}
}
/*
* Add an event node to a specified device node.
*/
void
{
return;
} else {
}
}
/*
* Fill in or change the fields of an evt node.
*/
void
{
int evt_match = 0;
int i;
/*
* Find the last event node.
*/
}
}
evt_node->prev_count = 0;
"pic kstat.\n"), pgmname);
exit(1);
}
/*
* The event can either be given as a event name (string) or
* as a pcr mask. If given as pcr mask, we try to match it
* to an event name, and use that name. Otherwise we just use
* the pcr mask value.
*/
return;
}
}
gettext("%s: %s is not a valid event name.\n"),
exit(1);
} else {
/*
* See if the pcr mask given by the user matches that for any
* existing event.
*/
evt_match = 1;
break;
}
}
if (evt_match == 0)
}
}
/*
* Removes all bar one of the evt_nodes that are hanging off the
* specified dev_node.
*/
void
{
/*
* Only one evt node, nothing for us to do.
*/
return;
}
}
}
/*
* Set the events for each pic on each device instance.
*/
void
{
}
}
/*
* Set the appropiate events. Only called for event nodes
* that are marked EVT_WRITE.
*/
void
{
int pic_num;
/* Read the "counters" kstat */
"not set event's.\n"), pgmname);
exit(1);
}
"not set event's.\n"), pgmname);
exit(1);
}
gettext("%s: invalid pic #%d.\n"),
exit(1);
}
/*
* Store the previous value that is on the pic
* so that we can calculate the delta value
* later.
*/
/*
* Read the current pcr value from device.
*/
/*
* Clear the section of the pcr which corresponds to the
* pic we are setting events on. Also clear the pcr value
* which is stored in the instance node.
*
*/
/*
* Set the event.
*/
/*
* Write the value back to the kstat, to make it
* visible to the underlying driver.
*/
"(setting events requires root "
"permission).\n"), pgmname);
exit(1);
}
}
/*
* Works through the list of device nodes, reading events
* and where appropiate setting new events (multiplexing).
*/
void
{
char tmp_str[30];
int iter = 0;
if (iter == 0)
/*
* First read of all the counters is done
* to establish a baseline for the counts.
* This data is not printed.
*/
if ((!initial_read) && (iter == 0)) {
}
"(pic %d) no longer valid.\n"),
continue;
}
iter++;
} else {
iter++;
}
iter = 0;
(void) printf("\n");
}
/*
* If there is more than one event node
* per-pic then we are multiplexing.
*/
(!initial_read)) {
}
}
}
/*
* Read a node that is marked as EVT_READ
*/
void
{
int i;
int match = 0;
int evt_blank = 1;
"(pic %d) no longer valid.\n"), pgmname,
return;
}
/*
* Get PCR value from device. We extract the portion
* of the PCR relating to the pic we are interested by
* AND'ing the inverse of the clear mask for this pic.
*
* The clear mask is usually used to clear the appropiate
* section of the PCR before we write events into it. So
* by using the inverse of the mask, we zero everything
* *but* the section we are interested in.
*/
/*
* If the event name is blank this is the first time that
* this node has been accessed, so we read the pcr and
* from that we get the event name if it exists.
*
* If the pcr read from the device does not match that
* stored in the node, then it means that the event has
* changed from its previous value, so we need to re-read
* all the values.
*/
break;
}
}
/*
* Able to resolve pcr value to a event name.
*/
if (match) {
if ((evt_blank) && (!initial_read)) {
(void) printf("%s\t%-8d\t",
evt_blank = 0;
}
} else {
if ((evt_blank) && (!initial_read)) {
(void) printf("%s\t%-8d\t",
evt_blank = 0;
}
}
} else {
/* Deal with wraparound of the counters */
} else {
/* Calcalate delta value */
- evt_node->prev_count;
}
/*
* Store value so that we can calculate delta next
* time through.
*/
/* Update count total */
if (delta) {
(void) printf("%-20s %-9lld ",
} else {
(void) printf("%-20s %-9lld ",
}
}
}
/*
* Read event nodes marked as EVT_WRITE
*/
void
{
return;
}
/*
* Get PCR value from device. We extract the portion
* of the PCR relating to the pic we are interested by
* AND'ing the inverse of the clear mask for this pic.
*
* The clear mask is usually used to clear the appropiate
* section of the PCR before we write events into it. So
* by using the inverse of the mask, we zero everything
* *but* the section we are interested in.
*/
/*
* If the pcr value from the device does not match the
* stored value, then the events on at least one of the
* pics must have been change by another busstat instance.
*
* Regard this as a fatal error.
*/
"by another busstat).\n"), pgmname);
exit(2);
}
/*
* Calculate delta, and then store value just read to allow us to
* calculate delta next time around.
*/
/* Deal with wraparound of the counters */
} else {
/* Calcalate delta value */
- evt_node->prev_count;
}
if (initial_read) {
} else {
/* Update count total */
if (delta) {
(void) printf("%-20s %-9lld ",
} else {
(void) printf("%-20s %-9lld ",
}
}
}
/*
* Check to see if any DR operations have occured, and deal with the
* consequences.
*
* Use the Kstat chain ID to check for DR operations. If the ID has
* changed then some kstats on system have been modified, we check
* all the data structures to see are they still valid. If they are
* not we remove them.
*/
void
{
int match = 0;
"kstat chain id\n"), pgmname);
exit(1);
}
if (new_id == 0) {
/* Kstat chain has not changed. */
return;
}
/*
* Scan the chain of device nodes, making sure that their associated
* kstats are still present. If not we remove the appropiate node.
*/
match = 1;
break;
}
}
if (match == 0) {
" (pic %d) no longer valid.\n"), pgmname,
}
}
}
/*
* Remove a device node and its associated event nodes.
*/
void
{
if (dev_list_head == NULL) {
"devices left to monitor.\n"),
pgmname);
exit(1);
}
/* Remove each event node first */
while (curr_evt_node != start_pos) {
}
return;
}
/* Find the device node */
/* Remove each event node first */
while (curr_evt_node != start_pos) {
}
return;
}
}
}
/*
* Find a device node in the linked list of dev_nodes. Match
* is done on device name, and instance number.
*/
{
return (curr_node);
}
}
return (NULL);
}
/*
* Determines whether the string represents a event name
* or a numeric value. Numeric value can be dec, hex
* or octal. All are converted to long int.
*/
{
return (EVT_STR);
} else {
return (num);
}
}
/*
* Find a pointer to the specified picN kstat. First
* search for the specific kstat, and if that can't
* be found search for any picN kstat belonging to this device.
*/
kstat_t *
{
/* Look for specific picN kstat */
return (ksp);
}
}
}
return (p_ksp);
}
/*
* Print column titles.
* Can be turned off by -n option.
*/
void
{
int i;
(void) printf("time dev ");
for (i = 0; i < max_pic_num; i++)
(void) printf("event%d "
"pic%d ", i, i);
(void) printf("\n");
}
/*
* Print the elapsed time in seconds, since the last call.
*/
void
{
static hrtime_t total_elapsed = 0;
if (initial_read) {
return;
}
/* Round up time value if necessary */
elapsed += 1;
total_elapsed += elapsed;
}
void
usage()
{
" [-e device-inst]\n"
" [-w device-inst "
"[,pic0=<event>] [,picN=<event>] ]\n"
" [-r device-inst]\n"
" [ interval [count] ]\n"));
exit(2);
}
void *
{
void *a;
exit(1);
}
return (a);
}
/*
* Create and arm the timer.
*/
void
{
struct sigevent sig_struct;
/* Create timer */
pgmname);
exit(1);
}
"handler"), pgmname);
exit(1);
}
/* Arm timer */
pgmname);
exit(1);
}
}
/* ARGSUSED */
void
handle_sig(int x)
{
}
/*
* return a boolean value indicating whether or not
* a string consists solely of characters which are
* digits 0..9
*/
int
strisnum(const char *s)
{
for (; *s != '\0'; s++) {
if (*s < '0' || *s > '9')
return (0);
}
return (1);
}