/*
* 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) 1984, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* sadc.c writes system activity binary data to a file or stdout.
*
* Usage: sadc [t n] [file]
*
* if t and n are not specified, it writes a dummy record to data file. This
* usage is particularly used at system booting. If t and n are specified, it
* writes system data n times to file every t seconds. In both cases, if file
* is not specified, it writes data to stdout.
*/
#include <ctype.h>
#include <errno.h>
#include <fcntl.h>
#include <kstat.h>
#include <memory.h>
#include <nlist.h>
#include <signal.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <strings.h>
#include "sa.h"
static int ncpus;
static int oncpus;
static int ncaches;
static void all_stat_init(void);
static int all_stat_load(void);
static void fail(int, char *, ...);
static void safe_zalloc(void **, int, int);
static void *safe_kstat_data_lookup(kstat_t *, char *);
static int safe_kstat_data_index(kstat_t *, char *);
static void init_iodevs(void);
static int iodevinfo_load(void);
kstat_t ** const []);
static void compute_cpu_stat_adj(void);
static struct sa d;
static long ninode;
int caught_cont = 0;
/*
* Sleep until *wakeup + interval, keeping cadence where desired
*
* *wakeup - The time we last wanted to wake up. Updated.
* interval - We want to sleep until *wakeup + interval
* *caught_cont - Global set by signal handler if we got a SIGCONT
*/
void
{
int status;
if (*caught_cont) {
/* Reset our cadence (see comment below) */
} else {
/*
* If we got here, then the time between the
* output we just did, and the scheduled time
* for the next output is < 1/4 of our requested
* interval AND the number of intervals has been
* requested AND we have never caught a SIGCONT
* (so we have never been suspended). In this
* case, we'll try to stay to the desired
* cadence, and we will pause for 1/2 the normal
* interval this time.
*/
}
else
if (pause < 1000)
/* Near enough */
return;
/* Now do the actual sleep */
pause_left = pause;
do {
if (status < 0)
if (pause_left < 1000)
/* Near enough */
return;
} else {
}
} while (status != 0);
}
/*
* Signal handler - so we can be aware of SIGCONT
*/
void
{
/* Re-set the signal handler */
caught_cont = 1;
}
int
{
int ct;
unsigned ti;
int fp;
char *fname;
/* Set up handler for SIGCONT */
init_iodevs();
/*
* no data file is specified, direct data to stdout.
*/
fp = 1;
} else {
/*
* Open or Create a data file. If the file doesn't exist, then
* it will be created.
*/
== -1)
/*
* Lock the entire data file to prevent data corruption
*/
/*
* Get data file statistics for use in determining whether
* truncation required and where rollback recovery should
* be applied.
*/
/*
* If the data file was opened and is too old, truncate it
*/
/*
* Remember filesize for rollback on error (bug #1223549)
*/
}
memset(&d, 0, sizeof (d));
/*
* If n == 0, write the additional dummy record.
*/
if (ct == 0) {
d.valid = 0;
sizeof (struct iodevinfo))
}
}
for (;;) {
do {
(void) kstat_chain_update(kc);
init_iodevs();
} while (all_stat_load() || iodevinfo_load());
d.valid = 1;
sizeof (struct iodevinfo))
}
if (--ct > 0) {
} else {
return (0);
}
}
/*NOTREACHED*/
}
/*
* Get various KIDs for subsequent all_stat_load operations.
*/
static void
all_stat_init(void)
{
/*
* Initialize global statistics
*/
"mem_total");
if (ufs_inode_ksp != NULL) {
"size");
ninode = ((kstat_named_t *)
"maxsize"))->value.l;
}
/*
* Load constant values now -- no need to reread each time
*/
/*
* Initialize per-CPU and per-kmem-cache statistics
*/
ncpus++;
ncaches++;
}
}
if (ncpus == 0)
if (ncaches == 0)
ksp = kmem_cache_list[0];
}
/*
* load statistics, summing across CPUs where needed
*/
static int
all_stat_load(void)
{
int i, j;
memset(&d, 0, sizeof (d));
/*
* Global statistics
*/
if (ufs_inode_ksp != NULL) {
}
/*
* Per-CPU statistics.
*/
for (i = 0; i < ncpus; i++) {
return (1);
/*
* Accumulate cpu ticks for CPU_IDLE, CPU_USER, CPU_KERNEL and
* CPU_WAIT with respect to each of the cpus.
*/
for (j = 0; j < CPU_STATES; j++)
for (j = 0; j < sizeof (cpu_sysinfo_t); j += sizeof (ulong_t))
for (j = 0; j < sizeof (cpu_vminfo_t); j += sizeof (ulong_t))
}
/*
* Per-cache kmem statistics.
*/
for (i = 0; i < ncaches; i++) {
int kmi_index;
return (1);
if (buf_size <= 256)
else
}
/*
* Adjust CPU statistics so the delta calculations in sar will
* be correct when facing changes to the set of online CPUs.
*/
for (i = 0; i < CPU_STATES; i++)
return (0);
}
static void
{
if (do_perror)
exit(2);
}
static void
{
}
static kid_t
{
if (kstat_chain_id == -1)
return (kstat_chain_id);
}
static kstat_t *
char *ks_name)
{
fail(0, "kstat_lookup('%s', %d, '%s') failed",
return (ksp);
}
static void *
{
fail(0, "kstat_data_lookup('%s', '%s') failed",
return (fp);
}
static int
{
}
static int
{
int cmp;
if (cmp != 0)
return (cmp);
if (cmp != 0)
return (cmp);
}
static void
init_iodevs(void)
{
niodevs = 0;
/*
* Patch the snip in the iodevinfo list (see below)
*/
if (snip)
continue;
else {
sizeof (struct iodevinfo), 0);
}
/*
* Insertion sort on (ks_module, ks_instance, ks_name)
*/
}
niodevs++;
}
/*
* Put a snip in the linked list of iodevinfos. The idea:
* If there was a state change such that now there are fewer
* iodevs, we snip the list and retain the tail, rather than
* freeing it. At the next state change, we clip the tail back on.
*/
}
static int
iodevinfo_load(void)
{
return (1);
}
return (0);
}
static int
{
return (-1);
} else {
dst->ks_data_size = 0;
}
return (0);
}
/*
* Determine what is different between two sets of kstats; s[0] and s[1]
* are arrays of kstats of size ns0 and ns1, respectively, and sorted by
* instance number. u[0] and u[1] are two arrays which must be
* caller-zallocated; each must be of size MAX(ns0, ns1). When the
* function terminates, u[0] contains all s[0]-unique items and u[1]
* contains all s[1]-unique items. Any unused entries in u[0] and u[1]
* are left NULL.
*/
static void
kstat_t ** const u[])
{
int i = 0, j = 0;
/*
* The instance is the same, but this
* CPU has been offline during the
* interval, so we consider *u0p to
* be s0p-unique, and similarly for
* *u1p.
*/
}
s0p++;
i++;
s1p++;
j++;
i++;
} else {
j++;
}
}
while (i < ns0) {
i++;
}
while (j < ns1) {
j++;
}
}
static int
{
}
/*
* Identify those CPUs which were not present for the whole interval so
* their statistics can be removed from the aggregate.
*/
static void
compute_cpu_stat_adj(void)
{
int i, j;
if (ocpu_stat_list) {
kstat_t **s[2];
s[0] = ocpu_stat_list;
s[1] = cpu_stat_list;
0);
0);
for (i = 0; i < max_cpus; i++) {
if (inarray[0][i])
for (j = 0; j < CPU_STATES; j++)
cpu_stat_adj[j] +=
((cpu_stat_t *)inarray[0][i]
if (inarray[1][i])
for (j = 0; j < CPU_STATES; j++)
cpu_stat_adj[j] -=
}
}
/*
* Preserve the last interval's CPU stats.
*/
if (cpu_stat_list) {
for (i = 0; i < oncpus; i++)
sizeof (*ocpu_stat_list), 1);
for (i = 0; i < ncpus; i++) {
safe_zalloc((void *)&ocpu_stat_list[i],
sizeof (*ocpu_stat_list[0]), 0);
}
}
}