17c478bd9Sstevel@tonic-gate /* 27c478bd9Sstevel@tonic-gate * CDDL HEADER START 37c478bd9Sstevel@tonic-gate * 47c478bd9Sstevel@tonic-gate * The contents of this file are subject to the terms of the 5*04fdf455SRalph Turner - Sun UK - Contractor * Common Development and Distribution License (the "License"). 6*04fdf455SRalph Turner - Sun UK - Contractor * You may not use this file except in compliance with the License. 77c478bd9Sstevel@tonic-gate * 87c478bd9Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 97c478bd9Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing. 107c478bd9Sstevel@tonic-gate * See the License for the specific language governing permissions 117c478bd9Sstevel@tonic-gate * and limitations under the License. 127c478bd9Sstevel@tonic-gate * 137c478bd9Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each 147c478bd9Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 157c478bd9Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the 167c478bd9Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying 177c478bd9Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner] 187c478bd9Sstevel@tonic-gate * 197c478bd9Sstevel@tonic-gate * CDDL HEADER END 207c478bd9Sstevel@tonic-gate */ 217c478bd9Sstevel@tonic-gate /* 22*04fdf455SRalph Turner - Sun UK - Contractor * Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved. 237c478bd9Sstevel@tonic-gate */ 247c478bd9Sstevel@tonic-gate 257c478bd9Sstevel@tonic-gate /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */ 267c478bd9Sstevel@tonic-gate /* All Rights Reserved */ 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate /* 307c478bd9Sstevel@tonic-gate * sadc.c writes system activity binary data to a file or stdout. 317c478bd9Sstevel@tonic-gate * 327c478bd9Sstevel@tonic-gate * Usage: sadc [t n] [file] 337c478bd9Sstevel@tonic-gate * 347c478bd9Sstevel@tonic-gate * if t and n are not specified, it writes a dummy record to data file. This 357c478bd9Sstevel@tonic-gate * usage is particularly used at system booting. If t and n are specified, it 367c478bd9Sstevel@tonic-gate * writes system data n times to file every t seconds. In both cases, if file 377c478bd9Sstevel@tonic-gate * is not specified, it writes data to stdout. 387c478bd9Sstevel@tonic-gate */ 397c478bd9Sstevel@tonic-gate 407c478bd9Sstevel@tonic-gate #include <sys/fcntl.h> 417c478bd9Sstevel@tonic-gate #include <sys/flock.h> 427c478bd9Sstevel@tonic-gate #include <sys/proc.h> 437c478bd9Sstevel@tonic-gate #include <sys/stat.h> 447c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h> 457c478bd9Sstevel@tonic-gate #include <sys/time.h> 467c478bd9Sstevel@tonic-gate #include <sys/types.h> 477c478bd9Sstevel@tonic-gate #include <sys/var.h> 487c478bd9Sstevel@tonic-gate 497c478bd9Sstevel@tonic-gate #include <ctype.h> 507c478bd9Sstevel@tonic-gate #include <errno.h> 517c478bd9Sstevel@tonic-gate #include <fcntl.h> 527c478bd9Sstevel@tonic-gate #include <kstat.h> 537c478bd9Sstevel@tonic-gate #include <memory.h> 547c478bd9Sstevel@tonic-gate #include <nlist.h> 557c478bd9Sstevel@tonic-gate #include <signal.h> 567c478bd9Sstevel@tonic-gate #include <stdarg.h> 577c478bd9Sstevel@tonic-gate #include <stdio.h> 587c478bd9Sstevel@tonic-gate #include <stdlib.h> 597c478bd9Sstevel@tonic-gate #include <string.h> 607c478bd9Sstevel@tonic-gate #include <time.h> 617c478bd9Sstevel@tonic-gate #include <unistd.h> 627c478bd9Sstevel@tonic-gate #include <strings.h> 637c478bd9Sstevel@tonic-gate 647c478bd9Sstevel@tonic-gate #include "sa.h" 657c478bd9Sstevel@tonic-gate 667c478bd9Sstevel@tonic-gate #define MAX(x1, x2) ((x1) >= (x2) ? (x1) : (x2)) 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate static kstat_ctl_t *kc; /* libkstat cookie */ 697c478bd9Sstevel@tonic-gate static int ncpus; 707c478bd9Sstevel@tonic-gate static int oncpus; 717c478bd9Sstevel@tonic-gate static kstat_t **cpu_stat_list = NULL; 727c478bd9Sstevel@tonic-gate static kstat_t **ocpu_stat_list = NULL; 737c478bd9Sstevel@tonic-gate static int ncaches; 747c478bd9Sstevel@tonic-gate static kstat_t **kmem_cache_list = NULL; 757c478bd9Sstevel@tonic-gate 767c478bd9Sstevel@tonic-gate static kstat_t *sysinfo_ksp, *vminfo_ksp, *var_ksp; 777c478bd9Sstevel@tonic-gate static kstat_t *system_misc_ksp, *ufs_inode_ksp, *kmem_oversize_ksp; 787c478bd9Sstevel@tonic-gate static kstat_t *file_cache_ksp; 797c478bd9Sstevel@tonic-gate static kstat_named_t *ufs_inode_size_knp, *nproc_knp; 807c478bd9Sstevel@tonic-gate static kstat_named_t *file_total_knp, *file_avail_knp; 817c478bd9Sstevel@tonic-gate static kstat_named_t *oversize_alloc_knp, *oversize_fail_knp; 827c478bd9Sstevel@tonic-gate static int slab_create_index, slab_destroy_index, slab_size_index; 837c478bd9Sstevel@tonic-gate static int buf_size_index, buf_avail_index, alloc_fail_index; 847c478bd9Sstevel@tonic-gate 857c478bd9Sstevel@tonic-gate static struct iodevinfo zeroiodev = { NULL, NULL }; 867c478bd9Sstevel@tonic-gate static struct iodevinfo *firstiodev = NULL; 877c478bd9Sstevel@tonic-gate static struct iodevinfo *lastiodev = NULL; 887c478bd9Sstevel@tonic-gate static struct iodevinfo *snip = NULL; 897c478bd9Sstevel@tonic-gate static ulong_t niodevs; 907c478bd9Sstevel@tonic-gate 917c478bd9Sstevel@tonic-gate static void all_stat_init(void); 927c478bd9Sstevel@tonic-gate static int all_stat_load(void); 937c478bd9Sstevel@tonic-gate static void fail(int, char *, ...); 947c478bd9Sstevel@tonic-gate static void safe_zalloc(void **, int, int); 957c478bd9Sstevel@tonic-gate static kid_t safe_kstat_read(kstat_ctl_t *, kstat_t *, void *); 967c478bd9Sstevel@tonic-gate static kstat_t *safe_kstat_lookup(kstat_ctl_t *, char *, int, char *); 977c478bd9Sstevel@tonic-gate static void *safe_kstat_data_lookup(kstat_t *, char *); 987c478bd9Sstevel@tonic-gate static int safe_kstat_data_index(kstat_t *, char *); 997c478bd9Sstevel@tonic-gate static void init_iodevs(void); 1007c478bd9Sstevel@tonic-gate static int iodevinfo_load(void); 1017c478bd9Sstevel@tonic-gate static int kstat_copy(const kstat_t *, kstat_t *); 1027c478bd9Sstevel@tonic-gate static void diff_two_arrays(kstat_t ** const [], size_t, size_t, 1037c478bd9Sstevel@tonic-gate kstat_t ** const []); 1047c478bd9Sstevel@tonic-gate static void compute_cpu_stat_adj(void); 1057c478bd9Sstevel@tonic-gate 1067c478bd9Sstevel@tonic-gate static char *cmdname = "sadc"; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate static struct var var; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate static struct sa d; 1117c478bd9Sstevel@tonic-gate static int64_t cpu_stat_adj[CPU_STATES] = {0}; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate static long ninode; 1147c478bd9Sstevel@tonic-gate 115*04fdf455SRalph Turner - Sun UK - Contractor int caught_cont = 0; 116*04fdf455SRalph Turner - Sun UK - Contractor 117*04fdf455SRalph Turner - Sun UK - Contractor /* 118*04fdf455SRalph Turner - Sun UK - Contractor * Sleep until *wakeup + interval, keeping cadence where desired 119*04fdf455SRalph Turner - Sun UK - Contractor * 120*04fdf455SRalph Turner - Sun UK - Contractor * *wakeup - The time we last wanted to wake up. Updated. 121*04fdf455SRalph Turner - Sun UK - Contractor * interval - We want to sleep until *wakeup + interval 122*04fdf455SRalph Turner - Sun UK - Contractor * *caught_cont - Global set by signal handler if we got a SIGCONT 123*04fdf455SRalph Turner - Sun UK - Contractor */ 124*04fdf455SRalph Turner - Sun UK - Contractor void 125*04fdf455SRalph Turner - Sun UK - Contractor sleep_until(hrtime_t *wakeup, hrtime_t interval, int *caught_cont) 126*04fdf455SRalph Turner - Sun UK - Contractor { 127*04fdf455SRalph Turner - Sun UK - Contractor hrtime_t now, pause, pause_left; 128*04fdf455SRalph Turner - Sun UK - Contractor struct timespec pause_tv; 129*04fdf455SRalph Turner - Sun UK - Contractor int status; 130*04fdf455SRalph Turner - Sun UK - Contractor now = gethrtime(); 131*04fdf455SRalph Turner - Sun UK - Contractor pause = *wakeup + interval - now; 132*04fdf455SRalph Turner - Sun UK - Contractor 133*04fdf455SRalph Turner - Sun UK - Contractor if (pause <= 0 || pause < (interval / 4)) 134*04fdf455SRalph Turner - Sun UK - Contractor if (*caught_cont) { 135*04fdf455SRalph Turner - Sun UK - Contractor /* Reset our cadence (see comment below) */ 136*04fdf455SRalph Turner - Sun UK - Contractor *wakeup = now + interval; 137*04fdf455SRalph Turner - Sun UK - Contractor pause = interval; 138*04fdf455SRalph Turner - Sun UK - Contractor } else { 139*04fdf455SRalph Turner - Sun UK - Contractor /* 140*04fdf455SRalph Turner - Sun UK - Contractor * If we got here, then the time between the 141*04fdf455SRalph Turner - Sun UK - Contractor * output we just did, and the scheduled time 142*04fdf455SRalph Turner - Sun UK - Contractor * for the next output is < 1/4 of our requested 143*04fdf455SRalph Turner - Sun UK - Contractor * interval AND the number of intervals has been 144*04fdf455SRalph Turner - Sun UK - Contractor * requested AND we have never caught a SIGCONT 145*04fdf455SRalph Turner - Sun UK - Contractor * (so we have never been suspended). In this 146*04fdf455SRalph Turner - Sun UK - Contractor * case, we'll try to stay to the desired 147*04fdf455SRalph Turner - Sun UK - Contractor * cadence, and we will pause for 1/2 the normal 148*04fdf455SRalph Turner - Sun UK - Contractor * interval this time. 149*04fdf455SRalph Turner - Sun UK - Contractor */ 150*04fdf455SRalph Turner - Sun UK - Contractor pause = interval / 2; 151*04fdf455SRalph Turner - Sun UK - Contractor *wakeup += interval; 152*04fdf455SRalph Turner - Sun UK - Contractor } 153*04fdf455SRalph Turner - Sun UK - Contractor else 154*04fdf455SRalph Turner - Sun UK - Contractor *wakeup += interval; 155*04fdf455SRalph Turner - Sun UK - Contractor if (pause < 1000) 156*04fdf455SRalph Turner - Sun UK - Contractor /* Near enough */ 157*04fdf455SRalph Turner - Sun UK - Contractor return; 158*04fdf455SRalph Turner - Sun UK - Contractor 159*04fdf455SRalph Turner - Sun UK - Contractor /* Now do the actual sleep */ 160*04fdf455SRalph Turner - Sun UK - Contractor pause_left = pause; 161*04fdf455SRalph Turner - Sun UK - Contractor do { 162*04fdf455SRalph Turner - Sun UK - Contractor pause_tv.tv_sec = pause_left / NANOSEC; 163*04fdf455SRalph Turner - Sun UK - Contractor pause_tv.tv_nsec = pause_left % NANOSEC; 164*04fdf455SRalph Turner - Sun UK - Contractor status = nanosleep(&pause_tv, (struct timespec *)NULL); 165*04fdf455SRalph Turner - Sun UK - Contractor if (status < 0) 166*04fdf455SRalph Turner - Sun UK - Contractor if (errno == EINTR) { 167*04fdf455SRalph Turner - Sun UK - Contractor now = gethrtime(); 168*04fdf455SRalph Turner - Sun UK - Contractor pause_left = *wakeup - now; 169*04fdf455SRalph Turner - Sun UK - Contractor if (pause_left < 1000) 170*04fdf455SRalph Turner - Sun UK - Contractor /* Near enough */ 171*04fdf455SRalph Turner - Sun UK - Contractor return; 172*04fdf455SRalph Turner - Sun UK - Contractor } else { 173*04fdf455SRalph Turner - Sun UK - Contractor fail(1, "nanosleep failed"); 174*04fdf455SRalph Turner - Sun UK - Contractor } 175*04fdf455SRalph Turner - Sun UK - Contractor } while (status != 0); 176*04fdf455SRalph Turner - Sun UK - Contractor } 177*04fdf455SRalph Turner - Sun UK - Contractor 178*04fdf455SRalph Turner - Sun UK - Contractor /* 179*04fdf455SRalph Turner - Sun UK - Contractor * Signal handler - so we can be aware of SIGCONT 180*04fdf455SRalph Turner - Sun UK - Contractor */ 181*04fdf455SRalph Turner - Sun UK - Contractor void 182*04fdf455SRalph Turner - Sun UK - Contractor cont_handler(int sig_number) 183*04fdf455SRalph Turner - Sun UK - Contractor { 184*04fdf455SRalph Turner - Sun UK - Contractor /* Re-set the signal handler */ 185*04fdf455SRalph Turner - Sun UK - Contractor (void) signal(sig_number, cont_handler); 186*04fdf455SRalph Turner - Sun UK - Contractor caught_cont = 1; 187*04fdf455SRalph Turner - Sun UK - Contractor } 188*04fdf455SRalph Turner - Sun UK - Contractor 1897c478bd9Sstevel@tonic-gate int 1907c478bd9Sstevel@tonic-gate main(int argc, char *argv[]) 1917c478bd9Sstevel@tonic-gate { 1927c478bd9Sstevel@tonic-gate int ct; 1937c478bd9Sstevel@tonic-gate unsigned ti; 1947c478bd9Sstevel@tonic-gate int fp; 1957c478bd9Sstevel@tonic-gate time_t min; 1967c478bd9Sstevel@tonic-gate struct stat buf; 1977c478bd9Sstevel@tonic-gate char *fname; 1987c478bd9Sstevel@tonic-gate struct iodevinfo *iodev; 1997c478bd9Sstevel@tonic-gate off_t flength; 200*04fdf455SRalph Turner - Sun UK - Contractor hrtime_t start_n; 201*04fdf455SRalph Turner - Sun UK - Contractor hrtime_t period_n; 202*04fdf455SRalph Turner - Sun UK - Contractor 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate ct = argc >= 3? atoi(argv[2]): 0; 2057c478bd9Sstevel@tonic-gate min = time((time_t *)0); 2067c478bd9Sstevel@tonic-gate ti = argc >= 3? atoi(argv[1]): 0; 2077c478bd9Sstevel@tonic-gate 208*04fdf455SRalph Turner - Sun UK - Contractor period_n = (hrtime_t)ti * NANOSEC; 209*04fdf455SRalph Turner - Sun UK - Contractor 2107c478bd9Sstevel@tonic-gate if ((kc = kstat_open()) == NULL) 2117c478bd9Sstevel@tonic-gate fail(1, "kstat_open(): can't open /dev/kstat"); 212*04fdf455SRalph Turner - Sun UK - Contractor 213*04fdf455SRalph Turner - Sun UK - Contractor /* Set up handler for SIGCONT */ 214*04fdf455SRalph Turner - Sun UK - Contractor if (signal(SIGCONT, cont_handler) == SIG_ERR) 215*04fdf455SRalph Turner - Sun UK - Contractor fail(1, "signal failed"); 216*04fdf455SRalph Turner - Sun UK - Contractor 2177c478bd9Sstevel@tonic-gate all_stat_init(); 2187c478bd9Sstevel@tonic-gate init_iodevs(); 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate if (argc == 3 || argc == 1) { 2217c478bd9Sstevel@tonic-gate /* 2227c478bd9Sstevel@tonic-gate * no data file is specified, direct data to stdout. 2237c478bd9Sstevel@tonic-gate */ 2247c478bd9Sstevel@tonic-gate fp = 1; 2257c478bd9Sstevel@tonic-gate } else { 2267c478bd9Sstevel@tonic-gate struct flock lock; 2277c478bd9Sstevel@tonic-gate 2287c478bd9Sstevel@tonic-gate fname = (argc == 2) ? argv[1] : argv[3]; 2297c478bd9Sstevel@tonic-gate /* 2307c478bd9Sstevel@tonic-gate * Open or Create a data file. If the file doesn't exist, then 2317c478bd9Sstevel@tonic-gate * it will be created. 2327c478bd9Sstevel@tonic-gate */ 2337c478bd9Sstevel@tonic-gate if ((fp = open(fname, O_WRONLY | O_APPEND | O_CREAT, 0644)) 2347c478bd9Sstevel@tonic-gate == -1) 2357c478bd9Sstevel@tonic-gate fail(1, "can't open data file"); 2367c478bd9Sstevel@tonic-gate /* 2377c478bd9Sstevel@tonic-gate * Lock the entire data file to prevent data corruption 2387c478bd9Sstevel@tonic-gate */ 2397c478bd9Sstevel@tonic-gate lock.l_type = F_WRLCK; 2407c478bd9Sstevel@tonic-gate lock.l_whence = SEEK_SET; 2417c478bd9Sstevel@tonic-gate lock.l_start = 0; 2427c478bd9Sstevel@tonic-gate lock.l_len = 0; 2437c478bd9Sstevel@tonic-gate if (fcntl(fp, F_SETLK, &lock) == -1) 2447c478bd9Sstevel@tonic-gate fail(1, "can't lock data file"); 2457c478bd9Sstevel@tonic-gate /* 2467c478bd9Sstevel@tonic-gate * Get data file statistics for use in determining whether 2477c478bd9Sstevel@tonic-gate * truncation required and where rollback recovery should 2487c478bd9Sstevel@tonic-gate * be applied. 2497c478bd9Sstevel@tonic-gate */ 2507c478bd9Sstevel@tonic-gate if (fstat(fp, &buf) == -1) 2517c478bd9Sstevel@tonic-gate fail(1, "can't get data file information"); 2527c478bd9Sstevel@tonic-gate /* 2537c478bd9Sstevel@tonic-gate * If the data file was opened and is too old, truncate it 2547c478bd9Sstevel@tonic-gate */ 2557c478bd9Sstevel@tonic-gate if (min - buf.st_mtime > 86400) 2567c478bd9Sstevel@tonic-gate if (ftruncate(fp, 0) == -1) 2577c478bd9Sstevel@tonic-gate fail(1, "can't truncate data file"); 2587c478bd9Sstevel@tonic-gate /* 2597c478bd9Sstevel@tonic-gate * Remember filesize for rollback on error (bug #1223549) 2607c478bd9Sstevel@tonic-gate */ 2617c478bd9Sstevel@tonic-gate flength = buf.st_size; 2627c478bd9Sstevel@tonic-gate } 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate memset(&d, 0, sizeof (d)); 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate /* 2677c478bd9Sstevel@tonic-gate * If n == 0, write the additional dummy record. 2687c478bd9Sstevel@tonic-gate */ 2697c478bd9Sstevel@tonic-gate if (ct == 0) { 2707c478bd9Sstevel@tonic-gate d.valid = 0; 2717c478bd9Sstevel@tonic-gate d.ts = min; 2727c478bd9Sstevel@tonic-gate d.niodevs = niodevs; 2737c478bd9Sstevel@tonic-gate 2747c478bd9Sstevel@tonic-gate if (write(fp, &d, sizeof (struct sa)) != sizeof (struct sa)) 2757c478bd9Sstevel@tonic-gate ftruncate(fp, flength), fail(1, "write failed"); 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate for (iodev = firstiodev; iodev; iodev = iodev->next) { 2787c478bd9Sstevel@tonic-gate if (write(fp, iodev, sizeof (struct iodevinfo)) != 2797c478bd9Sstevel@tonic-gate sizeof (struct iodevinfo)) 2807c478bd9Sstevel@tonic-gate ftruncate(fp, flength), fail(1, "write failed"); 2817c478bd9Sstevel@tonic-gate } 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate 284*04fdf455SRalph Turner - Sun UK - Contractor start_n = gethrtime(); 285*04fdf455SRalph Turner - Sun UK - Contractor 2867c478bd9Sstevel@tonic-gate for (;;) { 2877c478bd9Sstevel@tonic-gate do { 2887c478bd9Sstevel@tonic-gate (void) kstat_chain_update(kc); 2897c478bd9Sstevel@tonic-gate all_stat_init(); 2907c478bd9Sstevel@tonic-gate init_iodevs(); 2917c478bd9Sstevel@tonic-gate } while (all_stat_load() || iodevinfo_load()); 2927c478bd9Sstevel@tonic-gate 2937c478bd9Sstevel@tonic-gate d.ts = time((time_t *)0); 2947c478bd9Sstevel@tonic-gate d.valid = 1; 2957c478bd9Sstevel@tonic-gate d.niodevs = niodevs; 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate if (write(fp, &d, sizeof (struct sa)) != sizeof (struct sa)) 2987c478bd9Sstevel@tonic-gate ftruncate(fp, flength), fail(1, "write failed"); 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate for (iodev = firstiodev; iodev; iodev = iodev->next) { 3017c478bd9Sstevel@tonic-gate if (write(fp, iodev, sizeof (struct iodevinfo)) != 3027c478bd9Sstevel@tonic-gate sizeof (struct iodevinfo)) 3037c478bd9Sstevel@tonic-gate ftruncate(fp, flength), fail(1, "write failed"); 3047c478bd9Sstevel@tonic-gate } 3057c478bd9Sstevel@tonic-gate if (--ct > 0) { 306*04fdf455SRalph Turner - Sun UK - Contractor sleep_until(&start_n, period_n, &caught_cont); 3077c478bd9Sstevel@tonic-gate } else { 3087c478bd9Sstevel@tonic-gate close(fp); 3097c478bd9Sstevel@tonic-gate return (0); 3107c478bd9Sstevel@tonic-gate } 3117c478bd9Sstevel@tonic-gate } 3127c478bd9Sstevel@tonic-gate 3137c478bd9Sstevel@tonic-gate /*NOTREACHED*/ 3147c478bd9Sstevel@tonic-gate } 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate /* 3177c478bd9Sstevel@tonic-gate * Get various KIDs for subsequent all_stat_load operations. 3187c478bd9Sstevel@tonic-gate */ 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate static void 3217c478bd9Sstevel@tonic-gate all_stat_init(void) 3227c478bd9Sstevel@tonic-gate { 3237c478bd9Sstevel@tonic-gate kstat_t *ksp; 3247c478bd9Sstevel@tonic-gate 3257c478bd9Sstevel@tonic-gate /* 3267c478bd9Sstevel@tonic-gate * Initialize global statistics 3277c478bd9Sstevel@tonic-gate */ 3287c478bd9Sstevel@tonic-gate 3297c478bd9Sstevel@tonic-gate sysinfo_ksp = safe_kstat_lookup(kc, "unix", 0, "sysinfo"); 3307c478bd9Sstevel@tonic-gate vminfo_ksp = safe_kstat_lookup(kc, "unix", 0, "vminfo"); 3317c478bd9Sstevel@tonic-gate kmem_oversize_ksp = safe_kstat_lookup(kc, "vmem", -1, "kmem_oversize"); 3327c478bd9Sstevel@tonic-gate var_ksp = safe_kstat_lookup(kc, "unix", 0, "var"); 3337c478bd9Sstevel@tonic-gate system_misc_ksp = safe_kstat_lookup(kc, "unix", 0, "system_misc"); 3347c478bd9Sstevel@tonic-gate file_cache_ksp = safe_kstat_lookup(kc, "unix", 0, "file_cache"); 3357c478bd9Sstevel@tonic-gate ufs_inode_ksp = kstat_lookup(kc, "ufs", 0, "inode_cache"); 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate safe_kstat_read(kc, system_misc_ksp, NULL); 3387c478bd9Sstevel@tonic-gate nproc_knp = safe_kstat_data_lookup(system_misc_ksp, "nproc"); 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate safe_kstat_read(kc, file_cache_ksp, NULL); 3417c478bd9Sstevel@tonic-gate file_avail_knp = safe_kstat_data_lookup(file_cache_ksp, "buf_avail"); 3427c478bd9Sstevel@tonic-gate file_total_knp = safe_kstat_data_lookup(file_cache_ksp, "buf_total"); 3437c478bd9Sstevel@tonic-gate 3447c478bd9Sstevel@tonic-gate safe_kstat_read(kc, kmem_oversize_ksp, NULL); 3457c478bd9Sstevel@tonic-gate oversize_alloc_knp = safe_kstat_data_lookup(kmem_oversize_ksp, 3467c478bd9Sstevel@tonic-gate "mem_total"); 3477c478bd9Sstevel@tonic-gate oversize_fail_knp = safe_kstat_data_lookup(kmem_oversize_ksp, "fail"); 3487c478bd9Sstevel@tonic-gate 3497c478bd9Sstevel@tonic-gate if (ufs_inode_ksp != NULL) { 3507c478bd9Sstevel@tonic-gate safe_kstat_read(kc, ufs_inode_ksp, NULL); 3517c478bd9Sstevel@tonic-gate ufs_inode_size_knp = safe_kstat_data_lookup(ufs_inode_ksp, 3527c478bd9Sstevel@tonic-gate "size"); 3537c478bd9Sstevel@tonic-gate ninode = ((kstat_named_t *) 3547c478bd9Sstevel@tonic-gate safe_kstat_data_lookup(ufs_inode_ksp, 3557c478bd9Sstevel@tonic-gate "maxsize"))->value.l; 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate /* 3597c478bd9Sstevel@tonic-gate * Load constant values now -- no need to reread each time 3607c478bd9Sstevel@tonic-gate */ 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate safe_kstat_read(kc, var_ksp, (void *) &var); 3637c478bd9Sstevel@tonic-gate 3647c478bd9Sstevel@tonic-gate /* 3657c478bd9Sstevel@tonic-gate * Initialize per-CPU and per-kmem-cache statistics 3667c478bd9Sstevel@tonic-gate */ 3677c478bd9Sstevel@tonic-gate 3687c478bd9Sstevel@tonic-gate ncpus = ncaches = 0; 3697c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { 3707c478bd9Sstevel@tonic-gate if (strncmp(ksp->ks_name, "cpu_stat", 8) == 0) 3717c478bd9Sstevel@tonic-gate ncpus++; 3727c478bd9Sstevel@tonic-gate if (strcmp(ksp->ks_class, "kmem_cache") == 0) 3737c478bd9Sstevel@tonic-gate ncaches++; 3747c478bd9Sstevel@tonic-gate } 3757c478bd9Sstevel@tonic-gate 3767c478bd9Sstevel@tonic-gate safe_zalloc((void **)&cpu_stat_list, ncpus * sizeof (kstat_t *), 1); 3777c478bd9Sstevel@tonic-gate safe_zalloc((void **)&kmem_cache_list, ncaches * sizeof (kstat_t *), 1); 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate ncpus = ncaches = 0; 3807c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { 3817c478bd9Sstevel@tonic-gate if (strncmp(ksp->ks_name, "cpu_stat", 8) == 0 && 3827c478bd9Sstevel@tonic-gate kstat_read(kc, ksp, NULL) != -1) 3837c478bd9Sstevel@tonic-gate cpu_stat_list[ncpus++] = ksp; 3847c478bd9Sstevel@tonic-gate if (strcmp(ksp->ks_class, "kmem_cache") == 0 && 3857c478bd9Sstevel@tonic-gate kstat_read(kc, ksp, NULL) != -1) 3867c478bd9Sstevel@tonic-gate kmem_cache_list[ncaches++] = ksp; 3877c478bd9Sstevel@tonic-gate } 3887c478bd9Sstevel@tonic-gate 3897c478bd9Sstevel@tonic-gate if (ncpus == 0) 3907c478bd9Sstevel@tonic-gate fail(1, "can't find any cpu statistics"); 3917c478bd9Sstevel@tonic-gate 3927c478bd9Sstevel@tonic-gate if (ncaches == 0) 3937c478bd9Sstevel@tonic-gate fail(1, "can't find any kmem_cache statistics"); 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate ksp = kmem_cache_list[0]; 3967c478bd9Sstevel@tonic-gate safe_kstat_read(kc, ksp, NULL); 3977c478bd9Sstevel@tonic-gate buf_size_index = safe_kstat_data_index(ksp, "buf_size"); 3987c478bd9Sstevel@tonic-gate slab_create_index = safe_kstat_data_index(ksp, "slab_create"); 3997c478bd9Sstevel@tonic-gate slab_destroy_index = safe_kstat_data_index(ksp, "slab_destroy"); 4007c478bd9Sstevel@tonic-gate slab_size_index = safe_kstat_data_index(ksp, "slab_size"); 4017c478bd9Sstevel@tonic-gate buf_avail_index = safe_kstat_data_index(ksp, "buf_avail"); 4027c478bd9Sstevel@tonic-gate alloc_fail_index = safe_kstat_data_index(ksp, "alloc_fail"); 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate /* 4067c478bd9Sstevel@tonic-gate * load statistics, summing across CPUs where needed 4077c478bd9Sstevel@tonic-gate */ 4087c478bd9Sstevel@tonic-gate 4097c478bd9Sstevel@tonic-gate static int 4107c478bd9Sstevel@tonic-gate all_stat_load(void) 4117c478bd9Sstevel@tonic-gate { 4127c478bd9Sstevel@tonic-gate int i, j; 4137c478bd9Sstevel@tonic-gate cpu_stat_t cs; 4147c478bd9Sstevel@tonic-gate ulong_t *np, *tp; 4157c478bd9Sstevel@tonic-gate uint64_t cpu_tick[4] = {0, 0, 0, 0}; 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate memset(&d, 0, sizeof (d)); 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate /* 4207c478bd9Sstevel@tonic-gate * Global statistics 4217c478bd9Sstevel@tonic-gate */ 4227c478bd9Sstevel@tonic-gate 4237c478bd9Sstevel@tonic-gate safe_kstat_read(kc, sysinfo_ksp, (void *) &d.si); 4247c478bd9Sstevel@tonic-gate safe_kstat_read(kc, vminfo_ksp, (void *) &d.vmi); 4257c478bd9Sstevel@tonic-gate safe_kstat_read(kc, system_misc_ksp, NULL); 4267c478bd9Sstevel@tonic-gate safe_kstat_read(kc, file_cache_ksp, NULL); 4277c478bd9Sstevel@tonic-gate 4287c478bd9Sstevel@tonic-gate if (ufs_inode_ksp != NULL) { 4297c478bd9Sstevel@tonic-gate safe_kstat_read(kc, ufs_inode_ksp, NULL); 4307c478bd9Sstevel@tonic-gate d.szinode = ufs_inode_size_knp->value.ul; 4317c478bd9Sstevel@tonic-gate } 4327c478bd9Sstevel@tonic-gate 4337c478bd9Sstevel@tonic-gate d.szfile = file_total_knp->value.ui64 - file_avail_knp->value.ui64; 4347c478bd9Sstevel@tonic-gate d.szproc = nproc_knp->value.ul; 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate d.mszinode = (ninode > d.szinode) ? ninode : d.szinode; 4377c478bd9Sstevel@tonic-gate d.mszfile = d.szfile; 4387c478bd9Sstevel@tonic-gate d.mszproc = var.v_proc; 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate /* 4417c478bd9Sstevel@tonic-gate * Per-CPU statistics. 4427c478bd9Sstevel@tonic-gate */ 4437c478bd9Sstevel@tonic-gate 4447c478bd9Sstevel@tonic-gate for (i = 0; i < ncpus; i++) { 4457c478bd9Sstevel@tonic-gate if (kstat_read(kc, cpu_stat_list[i], (void *) &cs) == -1) 4467c478bd9Sstevel@tonic-gate return (1); 4477c478bd9Sstevel@tonic-gate 4487c478bd9Sstevel@tonic-gate np = (ulong_t *)&d.csi; 4497c478bd9Sstevel@tonic-gate tp = (ulong_t *)&cs.cpu_sysinfo; 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate /* 4527c478bd9Sstevel@tonic-gate * Accumulate cpu ticks for CPU_IDLE, CPU_USER, CPU_KERNEL and 4537c478bd9Sstevel@tonic-gate * CPU_WAIT with respect to each of the cpus. 4547c478bd9Sstevel@tonic-gate */ 4557c478bd9Sstevel@tonic-gate for (j = 0; j < CPU_STATES; j++) 4567c478bd9Sstevel@tonic-gate cpu_tick[j] += tp[j]; 4577c478bd9Sstevel@tonic-gate 4587c478bd9Sstevel@tonic-gate for (j = 0; j < sizeof (cpu_sysinfo_t); j += sizeof (ulong_t)) 4597c478bd9Sstevel@tonic-gate *np++ += *tp++; 4607c478bd9Sstevel@tonic-gate np = (ulong_t *)&d.cvmi; 4617c478bd9Sstevel@tonic-gate tp = (ulong_t *)&cs.cpu_vminfo; 4627c478bd9Sstevel@tonic-gate for (j = 0; j < sizeof (cpu_vminfo_t); j += sizeof (ulong_t)) 4637c478bd9Sstevel@tonic-gate *np++ += *tp++; 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate 4667c478bd9Sstevel@tonic-gate /* 4677c478bd9Sstevel@tonic-gate * Per-cache kmem statistics. 4687c478bd9Sstevel@tonic-gate */ 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate for (i = 0; i < ncaches; i++) { 4717c478bd9Sstevel@tonic-gate kstat_named_t *knp; 4727c478bd9Sstevel@tonic-gate u_longlong_t slab_create, slab_destroy, slab_size, mem_total; 4737c478bd9Sstevel@tonic-gate u_longlong_t buf_size, buf_avail, alloc_fail; 4747c478bd9Sstevel@tonic-gate int kmi_index; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate if (kstat_read(kc, kmem_cache_list[i], NULL) == -1) 4777c478bd9Sstevel@tonic-gate return (1); 4787c478bd9Sstevel@tonic-gate knp = kmem_cache_list[i]->ks_data; 4797c478bd9Sstevel@tonic-gate slab_create = knp[slab_create_index].value.ui64; 4807c478bd9Sstevel@tonic-gate slab_destroy = knp[slab_destroy_index].value.ui64; 4817c478bd9Sstevel@tonic-gate slab_size = knp[slab_size_index].value.ui64; 4827c478bd9Sstevel@tonic-gate buf_size = knp[buf_size_index].value.ui64; 4837c478bd9Sstevel@tonic-gate buf_avail = knp[buf_avail_index].value.ui64; 4847c478bd9Sstevel@tonic-gate alloc_fail = knp[alloc_fail_index].value.ui64; 4857c478bd9Sstevel@tonic-gate if (buf_size <= 256) 4867c478bd9Sstevel@tonic-gate kmi_index = KMEM_SMALL; 4877c478bd9Sstevel@tonic-gate else 4887c478bd9Sstevel@tonic-gate kmi_index = KMEM_LARGE; 4897c478bd9Sstevel@tonic-gate mem_total = (slab_create - slab_destroy) * slab_size; 4907c478bd9Sstevel@tonic-gate 4917c478bd9Sstevel@tonic-gate d.kmi.km_mem[kmi_index] += (ulong_t)mem_total; 4927c478bd9Sstevel@tonic-gate d.kmi.km_alloc[kmi_index] += 4937c478bd9Sstevel@tonic-gate (ulong_t)mem_total - buf_size * buf_avail; 4947c478bd9Sstevel@tonic-gate d.kmi.km_fail[kmi_index] += (ulong_t)alloc_fail; 4957c478bd9Sstevel@tonic-gate } 4967c478bd9Sstevel@tonic-gate 4977c478bd9Sstevel@tonic-gate safe_kstat_read(kc, kmem_oversize_ksp, NULL); 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate d.kmi.km_alloc[KMEM_OSIZE] = d.kmi.km_mem[KMEM_OSIZE] = 5007c478bd9Sstevel@tonic-gate oversize_alloc_knp->value.ui64; 5017c478bd9Sstevel@tonic-gate d.kmi.km_fail[KMEM_OSIZE] = oversize_fail_knp->value.ui64; 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate /* 5047c478bd9Sstevel@tonic-gate * Adjust CPU statistics so the delta calculations in sar will 5057c478bd9Sstevel@tonic-gate * be correct when facing changes to the set of online CPUs. 5067c478bd9Sstevel@tonic-gate */ 5077c478bd9Sstevel@tonic-gate compute_cpu_stat_adj(); 5087c478bd9Sstevel@tonic-gate for (i = 0; i < CPU_STATES; i++) 50930cb31d3Srm88369 d.csi.cpu[i] = (cpu_tick[i] + cpu_stat_adj[i]) / ncpus; 5107c478bd9Sstevel@tonic-gate 5117c478bd9Sstevel@tonic-gate return (0); 5127c478bd9Sstevel@tonic-gate } 5137c478bd9Sstevel@tonic-gate 5147c478bd9Sstevel@tonic-gate static void 5157c478bd9Sstevel@tonic-gate fail(int do_perror, char *message, ...) 5167c478bd9Sstevel@tonic-gate { 5177c478bd9Sstevel@tonic-gate va_list args; 5187c478bd9Sstevel@tonic-gate 5197c478bd9Sstevel@tonic-gate va_start(args, message); 5207c478bd9Sstevel@tonic-gate fprintf(stderr, "%s: ", cmdname); 5217c478bd9Sstevel@tonic-gate vfprintf(stderr, message, args); 5227c478bd9Sstevel@tonic-gate va_end(args); 5237c478bd9Sstevel@tonic-gate if (do_perror) 5247c478bd9Sstevel@tonic-gate fprintf(stderr, ": %s", strerror(errno)); 5257c478bd9Sstevel@tonic-gate fprintf(stderr, "\n"); 5267c478bd9Sstevel@tonic-gate exit(2); 5277c478bd9Sstevel@tonic-gate } 5287c478bd9Sstevel@tonic-gate 5297c478bd9Sstevel@tonic-gate static void 5307c478bd9Sstevel@tonic-gate safe_zalloc(void **ptr, int size, int free_first) 5317c478bd9Sstevel@tonic-gate { 5327c478bd9Sstevel@tonic-gate if (free_first && *ptr != NULL) 5337c478bd9Sstevel@tonic-gate free(*ptr); 5347c478bd9Sstevel@tonic-gate if ((*ptr = malloc(size)) == NULL) 5357c478bd9Sstevel@tonic-gate fail(1, "malloc failed"); 5367c478bd9Sstevel@tonic-gate memset(*ptr, 0, size); 5377c478bd9Sstevel@tonic-gate } 5387c478bd9Sstevel@tonic-gate 5397c478bd9Sstevel@tonic-gate static kid_t 5407c478bd9Sstevel@tonic-gate safe_kstat_read(kstat_ctl_t *kc, kstat_t *ksp, void *data) 5417c478bd9Sstevel@tonic-gate { 5427c478bd9Sstevel@tonic-gate kid_t kstat_chain_id = kstat_read(kc, ksp, data); 5437c478bd9Sstevel@tonic-gate 5447c478bd9Sstevel@tonic-gate if (kstat_chain_id == -1) 5457c478bd9Sstevel@tonic-gate fail(1, "kstat_read(%x, '%s') failed", kc, ksp->ks_name); 5467c478bd9Sstevel@tonic-gate return (kstat_chain_id); 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate static kstat_t * 5507c478bd9Sstevel@tonic-gate safe_kstat_lookup(kstat_ctl_t *kc, char *ks_module, int ks_instance, 5517c478bd9Sstevel@tonic-gate char *ks_name) 5527c478bd9Sstevel@tonic-gate { 5537c478bd9Sstevel@tonic-gate kstat_t *ksp = kstat_lookup(kc, ks_module, ks_instance, ks_name); 5547c478bd9Sstevel@tonic-gate 5557c478bd9Sstevel@tonic-gate if (ksp == NULL) 5567c478bd9Sstevel@tonic-gate fail(0, "kstat_lookup('%s', %d, '%s') failed", 5577c478bd9Sstevel@tonic-gate ks_module == NULL ? "" : ks_module, 5587c478bd9Sstevel@tonic-gate ks_instance, 5597c478bd9Sstevel@tonic-gate ks_name == NULL ? "" : ks_name); 5607c478bd9Sstevel@tonic-gate return (ksp); 5617c478bd9Sstevel@tonic-gate } 5627c478bd9Sstevel@tonic-gate 5637c478bd9Sstevel@tonic-gate static void * 5647c478bd9Sstevel@tonic-gate safe_kstat_data_lookup(kstat_t *ksp, char *name) 5657c478bd9Sstevel@tonic-gate { 5667c478bd9Sstevel@tonic-gate void *fp = kstat_data_lookup(ksp, name); 5677c478bd9Sstevel@tonic-gate 5687c478bd9Sstevel@tonic-gate if (fp == NULL) 5697c478bd9Sstevel@tonic-gate fail(0, "kstat_data_lookup('%s', '%s') failed", 5707c478bd9Sstevel@tonic-gate ksp->ks_name, name); 5717c478bd9Sstevel@tonic-gate return (fp); 5727c478bd9Sstevel@tonic-gate } 5737c478bd9Sstevel@tonic-gate 5747c478bd9Sstevel@tonic-gate static int 5757c478bd9Sstevel@tonic-gate safe_kstat_data_index(kstat_t *ksp, char *name) 5767c478bd9Sstevel@tonic-gate { 5777c478bd9Sstevel@tonic-gate return ((int)((char *)safe_kstat_data_lookup(ksp, name) - 5787c478bd9Sstevel@tonic-gate (char *)ksp->ks_data) / (ksp->ks_data_size / ksp->ks_ndata)); 5797c478bd9Sstevel@tonic-gate } 5807c478bd9Sstevel@tonic-gate 5817c478bd9Sstevel@tonic-gate static int 5827c478bd9Sstevel@tonic-gate kscmp(kstat_t *ks1, kstat_t *ks2) 5837c478bd9Sstevel@tonic-gate { 5847c478bd9Sstevel@tonic-gate int cmp; 5857c478bd9Sstevel@tonic-gate 5867c478bd9Sstevel@tonic-gate cmp = strcmp(ks1->ks_module, ks2->ks_module); 5877c478bd9Sstevel@tonic-gate if (cmp != 0) 5887c478bd9Sstevel@tonic-gate return (cmp); 5897c478bd9Sstevel@tonic-gate cmp = ks1->ks_instance - ks2->ks_instance; 5907c478bd9Sstevel@tonic-gate if (cmp != 0) 5917c478bd9Sstevel@tonic-gate return (cmp); 5927c478bd9Sstevel@tonic-gate return (strcmp(ks1->ks_name, ks2->ks_name)); 5937c478bd9Sstevel@tonic-gate } 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate static void 5967c478bd9Sstevel@tonic-gate init_iodevs(void) 5977c478bd9Sstevel@tonic-gate { 5987c478bd9Sstevel@tonic-gate struct iodevinfo *iodev, *previodev, *comp; 5997c478bd9Sstevel@tonic-gate kstat_t *ksp; 6007c478bd9Sstevel@tonic-gate 6017c478bd9Sstevel@tonic-gate iodev = &zeroiodev; 6027c478bd9Sstevel@tonic-gate niodevs = 0; 6037c478bd9Sstevel@tonic-gate 6047c478bd9Sstevel@tonic-gate /* 6057c478bd9Sstevel@tonic-gate * Patch the snip in the iodevinfo list (see below) 6067c478bd9Sstevel@tonic-gate */ 6077c478bd9Sstevel@tonic-gate if (snip) 6087c478bd9Sstevel@tonic-gate lastiodev->next = snip; 6097c478bd9Sstevel@tonic-gate 6107c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { 6117c478bd9Sstevel@tonic-gate 6127c478bd9Sstevel@tonic-gate if (ksp->ks_type != KSTAT_TYPE_IO) 6137c478bd9Sstevel@tonic-gate continue; 6147c478bd9Sstevel@tonic-gate previodev = iodev; 6157c478bd9Sstevel@tonic-gate if (iodev->next) 6167c478bd9Sstevel@tonic-gate iodev = iodev->next; 6177c478bd9Sstevel@tonic-gate else { 6187c478bd9Sstevel@tonic-gate safe_zalloc((void **) &iodev->next, 6197c478bd9Sstevel@tonic-gate sizeof (struct iodevinfo), 0); 6207c478bd9Sstevel@tonic-gate iodev = iodev->next; 6217c478bd9Sstevel@tonic-gate iodev->next = NULL; 6227c478bd9Sstevel@tonic-gate } 6237c478bd9Sstevel@tonic-gate iodev->ksp = ksp; 6247c478bd9Sstevel@tonic-gate iodev->ks = *ksp; 6257c478bd9Sstevel@tonic-gate memset((void *)&iodev->kios, 0, sizeof (kstat_io_t)); 6267c478bd9Sstevel@tonic-gate iodev->kios.wlastupdate = iodev->ks.ks_crtime; 6277c478bd9Sstevel@tonic-gate iodev->kios.rlastupdate = iodev->ks.ks_crtime; 6287c478bd9Sstevel@tonic-gate 6297c478bd9Sstevel@tonic-gate /* 6307c478bd9Sstevel@tonic-gate * Insertion sort on (ks_module, ks_instance, ks_name) 6317c478bd9Sstevel@tonic-gate */ 6327c478bd9Sstevel@tonic-gate comp = &zeroiodev; 6337c478bd9Sstevel@tonic-gate while (kscmp(&iodev->ks, &comp->next->ks) > 0) 6347c478bd9Sstevel@tonic-gate comp = comp->next; 6357c478bd9Sstevel@tonic-gate if (previodev != comp) { 6367c478bd9Sstevel@tonic-gate previodev->next = iodev->next; 6377c478bd9Sstevel@tonic-gate iodev->next = comp->next; 6387c478bd9Sstevel@tonic-gate comp->next = iodev; 6397c478bd9Sstevel@tonic-gate iodev = previodev; 6407c478bd9Sstevel@tonic-gate } 6417c478bd9Sstevel@tonic-gate niodevs++; 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate /* 6447c478bd9Sstevel@tonic-gate * Put a snip in the linked list of iodevinfos. The idea: 6457c478bd9Sstevel@tonic-gate * If there was a state change such that now there are fewer 6467c478bd9Sstevel@tonic-gate * iodevs, we snip the list and retain the tail, rather than 6477c478bd9Sstevel@tonic-gate * freeing it. At the next state change, we clip the tail back on. 6487c478bd9Sstevel@tonic-gate * This prevents a lot of malloc/free activity, and it's simpler. 6497c478bd9Sstevel@tonic-gate */ 6507c478bd9Sstevel@tonic-gate lastiodev = iodev; 6517c478bd9Sstevel@tonic-gate snip = iodev->next; 6527c478bd9Sstevel@tonic-gate iodev->next = NULL; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate firstiodev = zeroiodev.next; 6557c478bd9Sstevel@tonic-gate } 6567c478bd9Sstevel@tonic-gate 6577c478bd9Sstevel@tonic-gate static int 6587c478bd9Sstevel@tonic-gate iodevinfo_load(void) 6597c478bd9Sstevel@tonic-gate { 6607c478bd9Sstevel@tonic-gate struct iodevinfo *iodev; 6617c478bd9Sstevel@tonic-gate 6627c478bd9Sstevel@tonic-gate for (iodev = firstiodev; iodev; iodev = iodev->next) { 6637c478bd9Sstevel@tonic-gate if (kstat_read(kc, iodev->ksp, (void *) &iodev->kios) == -1) 6647c478bd9Sstevel@tonic-gate return (1); 6657c478bd9Sstevel@tonic-gate } 6667c478bd9Sstevel@tonic-gate return (0); 6677c478bd9Sstevel@tonic-gate } 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate static int 6707c478bd9Sstevel@tonic-gate kstat_copy(const kstat_t *src, kstat_t *dst) 6717c478bd9Sstevel@tonic-gate { 6727c478bd9Sstevel@tonic-gate *dst = *src; 6737c478bd9Sstevel@tonic-gate 6747c478bd9Sstevel@tonic-gate if (src->ks_data != NULL) { 6757c478bd9Sstevel@tonic-gate if ((dst->ks_data = malloc(src->ks_data_size)) == NULL) 6767c478bd9Sstevel@tonic-gate return (-1); 6777c478bd9Sstevel@tonic-gate bcopy(src->ks_data, dst->ks_data, src->ks_data_size); 6787c478bd9Sstevel@tonic-gate } else { 6797c478bd9Sstevel@tonic-gate dst->ks_data = NULL; 6807c478bd9Sstevel@tonic-gate dst->ks_data_size = 0; 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate return (0); 6837c478bd9Sstevel@tonic-gate } 6847c478bd9Sstevel@tonic-gate 6857c478bd9Sstevel@tonic-gate /* 6867c478bd9Sstevel@tonic-gate * Determine what is different between two sets of kstats; s[0] and s[1] 6877c478bd9Sstevel@tonic-gate * are arrays of kstats of size ns0 and ns1, respectively, and sorted by 6887c478bd9Sstevel@tonic-gate * instance number. u[0] and u[1] are two arrays which must be 6897c478bd9Sstevel@tonic-gate * caller-zallocated; each must be of size MAX(ns0, ns1). When the 6907c478bd9Sstevel@tonic-gate * function terminates, u[0] contains all s[0]-unique items and u[1] 6917c478bd9Sstevel@tonic-gate * contains all s[1]-unique items. Any unused entries in u[0] and u[1] 6927c478bd9Sstevel@tonic-gate * are left NULL. 6937c478bd9Sstevel@tonic-gate */ 6947c478bd9Sstevel@tonic-gate static void 6957c478bd9Sstevel@tonic-gate diff_two_arrays(kstat_t ** const s[], size_t ns0, size_t ns1, 6967c478bd9Sstevel@tonic-gate kstat_t ** const u[]) 6977c478bd9Sstevel@tonic-gate { 6987c478bd9Sstevel@tonic-gate kstat_t **s0p = s[0], **s1p = s[1]; 6997c478bd9Sstevel@tonic-gate kstat_t **u0p = u[0], **u1p = u[1]; 7007c478bd9Sstevel@tonic-gate int i = 0, j = 0; 7017c478bd9Sstevel@tonic-gate 7027c478bd9Sstevel@tonic-gate while (i < ns0 && j < ns1) { 7037c478bd9Sstevel@tonic-gate if ((*s0p)->ks_instance == (*s1p)->ks_instance) { 7047c478bd9Sstevel@tonic-gate if ((*s0p)->ks_kid != (*s1p)->ks_kid) { 7057c478bd9Sstevel@tonic-gate /* 7067c478bd9Sstevel@tonic-gate * The instance is the same, but this 7077c478bd9Sstevel@tonic-gate * CPU has been offline during the 7087c478bd9Sstevel@tonic-gate * interval, so we consider *u0p to 7097c478bd9Sstevel@tonic-gate * be s0p-unique, and similarly for 7107c478bd9Sstevel@tonic-gate * *u1p. 7117c478bd9Sstevel@tonic-gate */ 7127c478bd9Sstevel@tonic-gate *(u0p++) = *s0p; 7137c478bd9Sstevel@tonic-gate *(u1p++) = *s1p; 7147c478bd9Sstevel@tonic-gate } 7157c478bd9Sstevel@tonic-gate s0p++; 7167c478bd9Sstevel@tonic-gate i++; 7177c478bd9Sstevel@tonic-gate s1p++; 7187c478bd9Sstevel@tonic-gate j++; 7197c478bd9Sstevel@tonic-gate } else if ((*s0p)->ks_instance < (*s1p)->ks_instance) { 7207c478bd9Sstevel@tonic-gate *(u0p++) = *(s0p++); 7217c478bd9Sstevel@tonic-gate i++; 7227c478bd9Sstevel@tonic-gate } else { 7237c478bd9Sstevel@tonic-gate *(u1p++) = *(s1p++); 7247c478bd9Sstevel@tonic-gate j++; 7257c478bd9Sstevel@tonic-gate } 7267c478bd9Sstevel@tonic-gate } 7277c478bd9Sstevel@tonic-gate 7287c478bd9Sstevel@tonic-gate while (i < ns0) { 7297c478bd9Sstevel@tonic-gate *(u0p++) = *(s0p++); 7307c478bd9Sstevel@tonic-gate i++; 7317c478bd9Sstevel@tonic-gate } 7327c478bd9Sstevel@tonic-gate while (j < ns1) { 7337c478bd9Sstevel@tonic-gate *(u1p++) = *(s1p++); 7347c478bd9Sstevel@tonic-gate j++; 7357c478bd9Sstevel@tonic-gate } 7367c478bd9Sstevel@tonic-gate } 7377c478bd9Sstevel@tonic-gate 7387c478bd9Sstevel@tonic-gate static int 7397c478bd9Sstevel@tonic-gate cpuid_compare(const void *p1, const void *p2) 7407c478bd9Sstevel@tonic-gate { 7417c478bd9Sstevel@tonic-gate return ((*(kstat_t **)p1)->ks_instance - 7427c478bd9Sstevel@tonic-gate (*(kstat_t **)p2)->ks_instance); 7437c478bd9Sstevel@tonic-gate } 7447c478bd9Sstevel@tonic-gate 7457c478bd9Sstevel@tonic-gate /* 7467c478bd9Sstevel@tonic-gate * Identify those CPUs which were not present for the whole interval so 7477c478bd9Sstevel@tonic-gate * their statistics can be removed from the aggregate. 7487c478bd9Sstevel@tonic-gate */ 7497c478bd9Sstevel@tonic-gate static void 7507c478bd9Sstevel@tonic-gate compute_cpu_stat_adj(void) 7517c478bd9Sstevel@tonic-gate { 7527c478bd9Sstevel@tonic-gate int i, j; 7537c478bd9Sstevel@tonic-gate 7547c478bd9Sstevel@tonic-gate if (ocpu_stat_list) { 7557c478bd9Sstevel@tonic-gate kstat_t **s[2]; 7567c478bd9Sstevel@tonic-gate kstat_t **inarray[2]; 7577c478bd9Sstevel@tonic-gate int max_cpus = MAX(ncpus, oncpus); 7587c478bd9Sstevel@tonic-gate 7597c478bd9Sstevel@tonic-gate qsort(cpu_stat_list, ncpus, sizeof (*cpu_stat_list), 7607c478bd9Sstevel@tonic-gate cpuid_compare); 7617c478bd9Sstevel@tonic-gate qsort(ocpu_stat_list, oncpus, sizeof (*ocpu_stat_list), 7627c478bd9Sstevel@tonic-gate cpuid_compare); 7637c478bd9Sstevel@tonic-gate 7647c478bd9Sstevel@tonic-gate s[0] = ocpu_stat_list; 7657c478bd9Sstevel@tonic-gate s[1] = cpu_stat_list; 7667c478bd9Sstevel@tonic-gate 7677c478bd9Sstevel@tonic-gate safe_zalloc((void *)&inarray[0], sizeof (**inarray) * max_cpus, 7687c478bd9Sstevel@tonic-gate 0); 7697c478bd9Sstevel@tonic-gate safe_zalloc((void *)&inarray[1], sizeof (**inarray) * max_cpus, 7707c478bd9Sstevel@tonic-gate 0); 7717c478bd9Sstevel@tonic-gate diff_two_arrays(s, oncpus, ncpus, inarray); 7727c478bd9Sstevel@tonic-gate 7737c478bd9Sstevel@tonic-gate for (i = 0; i < max_cpus; i++) { 7747c478bd9Sstevel@tonic-gate if (inarray[0][i]) 7757c478bd9Sstevel@tonic-gate for (j = 0; j < CPU_STATES; j++) 7767c478bd9Sstevel@tonic-gate cpu_stat_adj[j] += 7777c478bd9Sstevel@tonic-gate ((cpu_stat_t *)inarray[0][i] 7787c478bd9Sstevel@tonic-gate ->ks_data)->cpu_sysinfo.cpu[j]; 7797c478bd9Sstevel@tonic-gate if (inarray[1][i]) 7807c478bd9Sstevel@tonic-gate for (j = 0; j < CPU_STATES; j++) 7817c478bd9Sstevel@tonic-gate cpu_stat_adj[j] -= 7827c478bd9Sstevel@tonic-gate ((cpu_stat_t *)inarray[1][i] 7837c478bd9Sstevel@tonic-gate ->ks_data)->cpu_sysinfo.cpu[j]; 7847c478bd9Sstevel@tonic-gate } 7857c478bd9Sstevel@tonic-gate 7867c478bd9Sstevel@tonic-gate free(inarray[0]); 7877c478bd9Sstevel@tonic-gate free(inarray[1]); 7887c478bd9Sstevel@tonic-gate } 7897c478bd9Sstevel@tonic-gate 7907c478bd9Sstevel@tonic-gate /* 7917c478bd9Sstevel@tonic-gate * Preserve the last interval's CPU stats. 7927c478bd9Sstevel@tonic-gate */ 7937c478bd9Sstevel@tonic-gate if (cpu_stat_list) { 7947c478bd9Sstevel@tonic-gate for (i = 0; i < oncpus; i++) 7957c478bd9Sstevel@tonic-gate free(ocpu_stat_list[i]->ks_data); 7967c478bd9Sstevel@tonic-gate 7977c478bd9Sstevel@tonic-gate oncpus = ncpus; 7987c478bd9Sstevel@tonic-gate safe_zalloc((void **)&ocpu_stat_list, oncpus * 7997c478bd9Sstevel@tonic-gate sizeof (*ocpu_stat_list), 1); 8007c478bd9Sstevel@tonic-gate for (i = 0; i < ncpus; i++) { 8017c478bd9Sstevel@tonic-gate safe_zalloc((void *)&ocpu_stat_list[i], 8027c478bd9Sstevel@tonic-gate sizeof (*ocpu_stat_list[0]), 0); 8037c478bd9Sstevel@tonic-gate if (kstat_copy(cpu_stat_list[i], ocpu_stat_list[i])) 8047c478bd9Sstevel@tonic-gate fail(1, "kstat_copy() failed"); 8057c478bd9Sstevel@tonic-gate } 8067c478bd9Sstevel@tonic-gate } 8077c478bd9Sstevel@tonic-gate } 808