/*
* 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 2009 Sun Microsystems, Inc. All rights reserved.
* Use is subject to license terms.
*/
#include <stdio.h>
#include <stdlib.h>
#include <inttypes.h>
#include <unistd.h>
#include <string.h>
#include <strings.h>
#include <limits.h>
#include <libintl.h>
#include <locale.h>
#include <errno.h>
#include <kstat.h>
#include <libcpc.h>
#include "cpucmds.h"
static struct options {
int debug;
int verbose;
int dotitle;
int dohelp;
int dotick;
int cpuver;
char *pgmname;
int followfork;
int followexec;
} __options;
/*
* How many signals caught from terminal
* We bail out as soon as possible when interrupt is set
*/
static int interrupt = 0;
/*ARGSUSED*/
static void
{
}
static void
{
}
static void intr(int);
#if defined(__i386)
static void p4_ht_error(void);
#endif
#if !defined(TEXT_DOMAIN)
#endif
int
{
int c, errcnt = 0;
int nsamples;
char *errstr;
int ret;
(void) textdomain(TEXT_DOMAIN);
else
return (1);
}
/*
* Establish (non-zero) defaults
*/
exit(1);
}
switch (c) {
case 'T': /* sample time, seconds */
break;
case 'N': /* number of samples */
if (nsamples < 0)
errcnt++;
else
break;
case 'D': /* enable debugging */
break;
case 'f': /* follow fork */
opts->followfork++;
break;
case 'e': /* follow exec */
opts->followexec++;
break;
case 'n': /* no titles */
break;
case 't': /* print %tick */
break;
case 'v':
break;
case 'o':
errcnt++;
break;
}
"%s: cannot open '%s' for writing\n"),
return (1);
}
break;
case 'c': /* specify statistics */
break;
case 'p': /* grab given pid */
errcnt++;
break;
case 'h':
break;
case '?':
default:
errcnt++;
break;
}
if (errcnt != 0 ||
"Usage:\n\t%s [-T secs] [-N count] [-Defhnv] [-o file]\n"
"\t\t-c events [command [args] | -p pid]\n\n"
"\t-T secs\t seconds between samples, default 1\n"
"\t-N count number of samples, default unlimited\n"
"\t-D\t enable debug mode\n"
"\t-e\t follow exec(2), and execve(2)\n"
"\t-f\t follow fork(2), fork1(2), and vfork(2)\n"
"\t-h\t print extended usage information\n"
"\t-n\t suppress titles\n"
"\t-t\t include virtualized %s register\n"
"\t-v\t verbose mode\n"
"\t-o file\t write cpu statistics to this file\n"
"\t-c events specify processor events to be monitored\n"
"\t-p pid\t pid of existing process to capture\n\n"
"\tUse cpustat(1M) to monitor system-wide statistics.\n"),
(void) putchar('\n');
exit(0);
}
exit(2);
}
/*
* Catch signals from terminal, so they can be handled asynchronously
* when we're ready instead of when we're not (;-)
*/
return (ret);
}
static void
{
if (opts->followfork)
}
static void
{
name = "(unknown)";
if (opts->followfork)
}
static void
{
if (opts->followfork)
}
static void
{
int i;
if (opts->followfork)
for (i = 0; i < nreq; i++) {
}
else
}
struct pstate {
int maxlwpid;
};
static int
{
char *errstr;
int nreq;
if (interrupt)
return (0);
"%6d: init_lwp: out of memory\n"), (int)pid);
return (-1);
}
}
}
"%6d: init_lwp: out of memory\n"), (int)pid);
return (-1);
}
} else {
}
#if defined(__i386)
p4_ht_error();
else
#endif
"%6d: init_lwp: can't bind perf counters "
errstr);
return (-1);
}
return (0);
}
/*ARGSUSED*/
static int
{
char *errstr;
int nreq;
if (interrupt)
return (0);
/*
* When we only have one set of counts, the sample
* gives us the accumulated count.
*/
} else {
/*
* When we have more than one set of counts, the
* sample gives us the count for the latest sample
* period. *data1 contains the accumulated count but
* does not include the count for the latest sample
* period for this set of counters.
*/
}
return (0);
}
switch (errno) {
case EAGAIN:
"lwp%d: perf counter contents invalidated\n"),
break;
default:
"lwp%d: can't access perf counters - %s\n"),
break;
}
return (-1);
}
/*ARGSUSED*/
static int
{
int nreq;
if (interrupt)
return (0);
return (0);
}
/*ARGSUSED*/
static int
{
int nreq;
if (interrupt)
return (0);
do {
continue;
return (0);
}
/*ARGSUSED*/
static int
{
int nreq;
if (interrupt)
return (0);
/*
* Print the accumulated results from the previous program image
*/
do {
&scratch);
if (hrt == 0)
continue;
}
if (opts->followexec) {
return (0);
}
return (-1);
}
/*ARGSUSED*/
static void
{
int nreq;
if (interrupt)
return;
do {
&scratch);
continue;
}
}
static int
{
char *errstr;
int nreqs;
if (interrupt)
return (0);
/*
* If we're dealing with one set, buffer usage is:
*
* data1 = most recent data snapshot
* data2 = previous data snapshot
* scratch = used for diffing data1 and data2
*
* Save the snapshot from the previous sample in data2
* before putting the current sample in data1.
*/
goto broken;
} else {
/*
* If there is more than set in use, we will need to
* unbind and re-bind on each go-around because each
* time a counter is bound, it is preset to 0 (as it was
* specified when the requests were added to the set).
*
* Buffer usage in this case is:
*
* data1 = total counts for this set since program began
* data2 = unused
* scratch = most recent data snapshot
*/
goto broken;
/*
* No need to unbind the previous set, as binding another set
* automatically unbinds the most recently bound set.
*/
goto broken;
}
return (0);
switch (errno) {
case EAGAIN:
"%6d: tick: lwp%d: perf counter contents invalidated\n"),
break;
default:
"%6d: tick: lwp%d: can't access perf counter - %s\n"),
break;
}
return (-1);
}
/*
* The system has just created a new address space that has a new pid.
* We're running in a child of the controlling process, with a new
* pctx handle already opened on the child of the original controlled process.
*/
static void
{
if (!opts->followfork)
return;
if (pctx_set_events(pctx,
PCTX_NULL_EVENT) == 0) {
}
}
}
/*
* Translate the incoming options into actions, and get the
* tool and the process to control running.
*/
static int
{
int err;
gettext("no program to start"));
return (1);
}
gettext("failed to start program"),
return (1);
}
} else {
gettext("failed to capture pid"),
return (1);
}
}
if (err != 0) {
gettext("can't bind process context ops to process"));
} else {
zerotime();
}
}
return (err != 0 ? 1 : 0);
}
#if defined(__i386)
/*
* cpc_bind_pctx() failed with EACCES, which means the user must first offline
* all but one logical processor on each physical processor. Print to stderr the
* psradm command string to do this.
*/
static void
p4_ht_error(void)
{
kstat_named_t *k;
int i;
int max;
int stat;
int *designees;
int *must_offline;
char *cmd;
int noffline = 0;
int ndone = 0;
gettext("Pentium 4 processors with HyperThreading present.\nOffline"
" all but one logical processor on each physical processor in"
" order to use\ncputrack.\n"));
return;
exit(0);
}
exit(0);
}
for (i = 0; i < max; i++) {
designees[i] = -1;
must_offline[i] = 0;
}
for (i = 0; i < max; i++) {
continue;
return;
}
return;
}
== NULL) {
return;
}
/*
* This chip doesn't yet have a CPU designated to remain
* online; let this one be it.
*/
else {
/*
* This chip already has a designated CPU; this CPU must
* go offline.
*/
must_offline[i] = 1;
noffline++;
}
}
/*
* Now construct a string containing the command line used to offline
* the appropriate processors.
*/
== NULL) {
exit(0);
}
for (i = 0; i < max; i++) {
if (must_offline[i] == 0)
continue;
ndone++;
}
"will configure the system appropriately"), cmd);
exit(1);
}
#endif /* defined(__i386) */
/*ARGSUSED*/
static void
{
interrupt++;
}