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*37fbbce5Scth * Common Development and Distribution License (the "License"). 6*37fbbce5Scth * 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*37fbbce5Scth * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 277c478bd9Sstevel@tonic-gate 287c478bd9Sstevel@tonic-gate #include "statcommon.h" 297c478bd9Sstevel@tonic-gate #include "dsr.h" 307c478bd9Sstevel@tonic-gate 317c478bd9Sstevel@tonic-gate #include <stdlib.h> 327c478bd9Sstevel@tonic-gate #include <stdarg.h> 337c478bd9Sstevel@tonic-gate #include <unistd.h> 347c478bd9Sstevel@tonic-gate #include <strings.h> 357c478bd9Sstevel@tonic-gate #include <errno.h> 36*37fbbce5Scth #include <limits.h> 377c478bd9Sstevel@tonic-gate #include <poll.h> 387c478bd9Sstevel@tonic-gate 397c478bd9Sstevel@tonic-gate #define ARRAY_SIZE(a) (sizeof (a) / sizeof (*a)) 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * The time we delay before retrying after an allocation 437c478bd9Sstevel@tonic-gate * failure, in milliseconds 447c478bd9Sstevel@tonic-gate */ 457c478bd9Sstevel@tonic-gate #define RETRY_DELAY 200 467c478bd9Sstevel@tonic-gate 477c478bd9Sstevel@tonic-gate static char *cpu_states[] = { 487c478bd9Sstevel@tonic-gate "cpu_ticks_idle", 497c478bd9Sstevel@tonic-gate "cpu_ticks_user", 507c478bd9Sstevel@tonic-gate "cpu_ticks_kernel", 517c478bd9Sstevel@tonic-gate "cpu_ticks_wait" 527c478bd9Sstevel@tonic-gate }; 537c478bd9Sstevel@tonic-gate 547c478bd9Sstevel@tonic-gate extern char cmdname[]; 557c478bd9Sstevel@tonic-gate 567c478bd9Sstevel@tonic-gate static kstat_t * 577c478bd9Sstevel@tonic-gate kstat_lookup_read(kstat_ctl_t *kc, char *module, 587c478bd9Sstevel@tonic-gate int instance, char *name) 597c478bd9Sstevel@tonic-gate { 607c478bd9Sstevel@tonic-gate kstat_t *ksp = kstat_lookup(kc, module, instance, name); 617c478bd9Sstevel@tonic-gate if (ksp == NULL) 627c478bd9Sstevel@tonic-gate return (NULL); 637c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) 647c478bd9Sstevel@tonic-gate return (NULL); 657c478bd9Sstevel@tonic-gate return (ksp); 667c478bd9Sstevel@tonic-gate } 677c478bd9Sstevel@tonic-gate 687c478bd9Sstevel@tonic-gate /* 697c478bd9Sstevel@tonic-gate * Note: the following helpers do not clean up on the failure case, 707c478bd9Sstevel@tonic-gate * because it is left to the free_snapshot() in the acquire_snapshot() 717c478bd9Sstevel@tonic-gate * failure path. 727c478bd9Sstevel@tonic-gate */ 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate static int 757c478bd9Sstevel@tonic-gate acquire_cpus(struct snapshot *ss, kstat_ctl_t *kc) 767c478bd9Sstevel@tonic-gate { 777c478bd9Sstevel@tonic-gate size_t i; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate ss->s_nr_cpus = sysconf(_SC_CPUID_MAX) + 1; 807c478bd9Sstevel@tonic-gate ss->s_cpus = calloc(ss->s_nr_cpus, sizeof (struct cpu_snapshot)); 817c478bd9Sstevel@tonic-gate if (ss->s_cpus == NULL) 827c478bd9Sstevel@tonic-gate goto out; 837c478bd9Sstevel@tonic-gate 847c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_cpus; i++) { 857c478bd9Sstevel@tonic-gate kstat_t *ksp; 867c478bd9Sstevel@tonic-gate 877c478bd9Sstevel@tonic-gate ss->s_cpus[i].cs_id = ID_NO_CPU; 887c478bd9Sstevel@tonic-gate ss->s_cpus[i].cs_state = p_online(i, P_STATUS); 897c478bd9Sstevel@tonic-gate /* If no valid CPU is present, move on to the next one */ 907c478bd9Sstevel@tonic-gate if (ss->s_cpus[i].cs_state == -1) 917c478bd9Sstevel@tonic-gate continue; 927c478bd9Sstevel@tonic-gate ss->s_cpus[i].cs_id = i; 937c478bd9Sstevel@tonic-gate 947c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup_read(kc, "cpu_info", i, NULL)) == NULL) 957c478bd9Sstevel@tonic-gate goto out; 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate (void) pset_assign(PS_QUERY, i, &ss->s_cpus[i].cs_pset_id); 987c478bd9Sstevel@tonic-gate if (ss->s_cpus[i].cs_pset_id == PS_NONE) 997c478bd9Sstevel@tonic-gate ss->s_cpus[i].cs_pset_id = ID_NO_PSET; 1007c478bd9Sstevel@tonic-gate 1017c478bd9Sstevel@tonic-gate if (!CPU_ACTIVE(&ss->s_cpus[i])) 1027c478bd9Sstevel@tonic-gate continue; 1037c478bd9Sstevel@tonic-gate 1047c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup_read(kc, "cpu", i, "vm")) == NULL) 1057c478bd9Sstevel@tonic-gate goto out; 1067c478bd9Sstevel@tonic-gate 1077c478bd9Sstevel@tonic-gate if (kstat_copy(ksp, &ss->s_cpus[i].cs_vm)) 1087c478bd9Sstevel@tonic-gate goto out; 1097c478bd9Sstevel@tonic-gate 1107c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup_read(kc, "cpu", i, "sys")) == NULL) 1117c478bd9Sstevel@tonic-gate goto out; 1127c478bd9Sstevel@tonic-gate 1137c478bd9Sstevel@tonic-gate if (kstat_copy(ksp, &ss->s_cpus[i].cs_sys)) 1147c478bd9Sstevel@tonic-gate goto out; 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate errno = 0; 1187c478bd9Sstevel@tonic-gate out: 1197c478bd9Sstevel@tonic-gate return (errno); 1207c478bd9Sstevel@tonic-gate } 1217c478bd9Sstevel@tonic-gate 1227c478bd9Sstevel@tonic-gate static int 1237c478bd9Sstevel@tonic-gate acquire_psets(struct snapshot *ss) 1247c478bd9Sstevel@tonic-gate { 1257c478bd9Sstevel@tonic-gate psetid_t *pids = NULL; 1267c478bd9Sstevel@tonic-gate struct pset_snapshot *ps; 1277c478bd9Sstevel@tonic-gate size_t pids_nr; 1287c478bd9Sstevel@tonic-gate size_t i, j; 1297c478bd9Sstevel@tonic-gate 1307c478bd9Sstevel@tonic-gate /* 1317c478bd9Sstevel@tonic-gate * Careful in this code. We have to use pset_list 1327c478bd9Sstevel@tonic-gate * twice, but inbetween pids_nr can change at will. 1337c478bd9Sstevel@tonic-gate * We delay the setting of s_nr_psets until we have 1347c478bd9Sstevel@tonic-gate * the "final" value of pids_nr. 1357c478bd9Sstevel@tonic-gate */ 1367c478bd9Sstevel@tonic-gate 1377c478bd9Sstevel@tonic-gate if (pset_list(NULL, &pids_nr) < 0) 1387c478bd9Sstevel@tonic-gate return (errno); 1397c478bd9Sstevel@tonic-gate 1407c478bd9Sstevel@tonic-gate if ((pids = calloc(pids_nr, sizeof (psetid_t))) == NULL) 1417c478bd9Sstevel@tonic-gate goto out; 1427c478bd9Sstevel@tonic-gate 1437c478bd9Sstevel@tonic-gate if (pset_list(pids, &pids_nr) < 0) 1447c478bd9Sstevel@tonic-gate goto out; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate ss->s_psets = calloc(pids_nr + 1, sizeof (struct pset_snapshot)); 1477c478bd9Sstevel@tonic-gate if (ss->s_psets == NULL) 1487c478bd9Sstevel@tonic-gate goto out; 1497c478bd9Sstevel@tonic-gate ss->s_nr_psets = pids_nr + 1; 1507c478bd9Sstevel@tonic-gate 1517c478bd9Sstevel@tonic-gate /* CPUs not in any actual pset */ 1527c478bd9Sstevel@tonic-gate ps = &ss->s_psets[0]; 1537c478bd9Sstevel@tonic-gate ps->ps_id = 0; 1547c478bd9Sstevel@tonic-gate ps->ps_cpus = calloc(ss->s_nr_cpus, sizeof (struct cpu_snapshot *)); 1557c478bd9Sstevel@tonic-gate if (ps->ps_cpus == NULL) 1567c478bd9Sstevel@tonic-gate goto out; 1577c478bd9Sstevel@tonic-gate 1587c478bd9Sstevel@tonic-gate /* CPUs in a a pset */ 1597c478bd9Sstevel@tonic-gate for (i = 1; i < ss->s_nr_psets; i++) { 1607c478bd9Sstevel@tonic-gate ps = &ss->s_psets[i]; 1617c478bd9Sstevel@tonic-gate 1627c478bd9Sstevel@tonic-gate ps->ps_id = pids[i - 1]; 1637c478bd9Sstevel@tonic-gate ps->ps_cpus = 1647c478bd9Sstevel@tonic-gate calloc(ss->s_nr_cpus, sizeof (struct cpu_snapshot *)); 1657c478bd9Sstevel@tonic-gate if (ps->ps_cpus == NULL) 1667c478bd9Sstevel@tonic-gate goto out; 1677c478bd9Sstevel@tonic-gate } 1687c478bd9Sstevel@tonic-gate 1697c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_psets; i++) { 1707c478bd9Sstevel@tonic-gate ps = &ss->s_psets[i]; 1717c478bd9Sstevel@tonic-gate 1727c478bd9Sstevel@tonic-gate for (j = 0; j < ss->s_nr_cpus; j++) { 1737c478bd9Sstevel@tonic-gate if (!CPU_ACTIVE(&ss->s_cpus[j])) 1747c478bd9Sstevel@tonic-gate continue; 1757c478bd9Sstevel@tonic-gate if (ss->s_cpus[j].cs_pset_id != ps->ps_id) 1767c478bd9Sstevel@tonic-gate continue; 1777c478bd9Sstevel@tonic-gate 1787c478bd9Sstevel@tonic-gate ps->ps_cpus[ps->ps_nr_cpus++] = &ss->s_cpus[j]; 1797c478bd9Sstevel@tonic-gate } 1807c478bd9Sstevel@tonic-gate } 1817c478bd9Sstevel@tonic-gate 1827c478bd9Sstevel@tonic-gate errno = 0; 1837c478bd9Sstevel@tonic-gate out: 1847c478bd9Sstevel@tonic-gate free(pids); 1857c478bd9Sstevel@tonic-gate return (errno); 1867c478bd9Sstevel@tonic-gate } 1877c478bd9Sstevel@tonic-gate 1887c478bd9Sstevel@tonic-gate static int 1897c478bd9Sstevel@tonic-gate acquire_intrs(struct snapshot *ss, kstat_ctl_t *kc) 1907c478bd9Sstevel@tonic-gate { 1917c478bd9Sstevel@tonic-gate kstat_t *ksp; 1927c478bd9Sstevel@tonic-gate size_t i = 0; 1937c478bd9Sstevel@tonic-gate kstat_t *sys_misc; 1947c478bd9Sstevel@tonic-gate kstat_named_t *clock; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate /* clock interrupt */ 1977c478bd9Sstevel@tonic-gate ss->s_nr_intrs = 1; 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { 2007c478bd9Sstevel@tonic-gate if (ksp->ks_type == KSTAT_TYPE_INTR) 2017c478bd9Sstevel@tonic-gate ss->s_nr_intrs++; 2027c478bd9Sstevel@tonic-gate } 2037c478bd9Sstevel@tonic-gate 2047c478bd9Sstevel@tonic-gate ss->s_intrs = calloc(ss->s_nr_intrs, sizeof (struct intr_snapshot)); 2057c478bd9Sstevel@tonic-gate if (ss->s_intrs == NULL) 2067c478bd9Sstevel@tonic-gate return (errno); 2077c478bd9Sstevel@tonic-gate 2087c478bd9Sstevel@tonic-gate sys_misc = kstat_lookup_read(kc, "unix", 0, "system_misc"); 2097c478bd9Sstevel@tonic-gate if (sys_misc == NULL) 2107c478bd9Sstevel@tonic-gate goto out; 2117c478bd9Sstevel@tonic-gate 2127c478bd9Sstevel@tonic-gate clock = (kstat_named_t *)kstat_data_lookup(sys_misc, "clk_intr"); 2137c478bd9Sstevel@tonic-gate if (clock == NULL) 2147c478bd9Sstevel@tonic-gate goto out; 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate (void) strlcpy(ss->s_intrs[0].is_name, "clock", KSTAT_STRLEN); 2177c478bd9Sstevel@tonic-gate ss->s_intrs[0].is_total = clock->value.ui32; 2187c478bd9Sstevel@tonic-gate 2197c478bd9Sstevel@tonic-gate i = 1; 2207c478bd9Sstevel@tonic-gate 2217c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { 2227c478bd9Sstevel@tonic-gate kstat_intr_t *ki; 2237c478bd9Sstevel@tonic-gate int j; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate if (ksp->ks_type != KSTAT_TYPE_INTR) 2267c478bd9Sstevel@tonic-gate continue; 2277c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) 2287c478bd9Sstevel@tonic-gate goto out; 2297c478bd9Sstevel@tonic-gate 2307c478bd9Sstevel@tonic-gate ki = KSTAT_INTR_PTR(ksp); 2317c478bd9Sstevel@tonic-gate 2327c478bd9Sstevel@tonic-gate (void) strlcpy(ss->s_intrs[i].is_name, ksp->ks_name, 2337c478bd9Sstevel@tonic-gate KSTAT_STRLEN); 2347c478bd9Sstevel@tonic-gate ss->s_intrs[i].is_total = 0; 2357c478bd9Sstevel@tonic-gate 2367c478bd9Sstevel@tonic-gate for (j = 0; j < KSTAT_NUM_INTRS; j++) 2377c478bd9Sstevel@tonic-gate ss->s_intrs[i].is_total += ki->intrs[j]; 2387c478bd9Sstevel@tonic-gate 2397c478bd9Sstevel@tonic-gate i++; 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate errno = 0; 2437c478bd9Sstevel@tonic-gate out: 2447c478bd9Sstevel@tonic-gate return (errno); 2457c478bd9Sstevel@tonic-gate } 2467c478bd9Sstevel@tonic-gate 2477c478bd9Sstevel@tonic-gate int 2487c478bd9Sstevel@tonic-gate acquire_sys(struct snapshot *ss, kstat_ctl_t *kc) 2497c478bd9Sstevel@tonic-gate { 2507c478bd9Sstevel@tonic-gate size_t i; 2517c478bd9Sstevel@tonic-gate kstat_named_t *knp; 2527c478bd9Sstevel@tonic-gate kstat_t *ksp; 2537c478bd9Sstevel@tonic-gate 2547c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "unix", 0, "sysinfo")) == NULL) 2557c478bd9Sstevel@tonic-gate return (errno); 2567c478bd9Sstevel@tonic-gate 2577c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, &ss->s_sys.ss_sysinfo) == -1) 2587c478bd9Sstevel@tonic-gate return (errno); 2597c478bd9Sstevel@tonic-gate 2607c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "unix", 0, "vminfo")) == NULL) 2617c478bd9Sstevel@tonic-gate return (errno); 2627c478bd9Sstevel@tonic-gate 2637c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, &ss->s_sys.ss_vminfo) == -1) 2647c478bd9Sstevel@tonic-gate return (errno); 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "unix", 0, "dnlcstats")) == NULL) 2677c478bd9Sstevel@tonic-gate return (errno); 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, &ss->s_sys.ss_nc) == -1) 2707c478bd9Sstevel@tonic-gate return (errno); 2717c478bd9Sstevel@tonic-gate 2727c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "unix", 0, "system_misc")) == NULL) 2737c478bd9Sstevel@tonic-gate return (errno); 2747c478bd9Sstevel@tonic-gate 2757c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) 2767c478bd9Sstevel@tonic-gate return (errno); 2777c478bd9Sstevel@tonic-gate 2787c478bd9Sstevel@tonic-gate knp = (kstat_named_t *)kstat_data_lookup(ksp, "clk_intr"); 2797c478bd9Sstevel@tonic-gate if (knp == NULL) 2807c478bd9Sstevel@tonic-gate return (errno); 2817c478bd9Sstevel@tonic-gate 2827c478bd9Sstevel@tonic-gate ss->s_sys.ss_ticks = knp->value.l; 2837c478bd9Sstevel@tonic-gate 2847c478bd9Sstevel@tonic-gate knp = (kstat_named_t *)kstat_data_lookup(ksp, "deficit"); 2857c478bd9Sstevel@tonic-gate if (knp == NULL) 2867c478bd9Sstevel@tonic-gate return (errno); 2877c478bd9Sstevel@tonic-gate 2887c478bd9Sstevel@tonic-gate ss->s_sys.ss_deficit = knp->value.l; 2897c478bd9Sstevel@tonic-gate 2907c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_cpus; i++) { 2917c478bd9Sstevel@tonic-gate if (!CPU_ACTIVE(&ss->s_cpus[i])) 2927c478bd9Sstevel@tonic-gate continue; 2937c478bd9Sstevel@tonic-gate 2947c478bd9Sstevel@tonic-gate if (kstat_add(&ss->s_cpus[i].cs_sys, &ss->s_sys.ss_agg_sys)) 2957c478bd9Sstevel@tonic-gate return (errno); 2967c478bd9Sstevel@tonic-gate if (kstat_add(&ss->s_cpus[i].cs_vm, &ss->s_sys.ss_agg_vm)) 2977c478bd9Sstevel@tonic-gate return (errno); 2987c478bd9Sstevel@tonic-gate } 2997c478bd9Sstevel@tonic-gate 3007c478bd9Sstevel@tonic-gate return (0); 3017c478bd9Sstevel@tonic-gate } 3027c478bd9Sstevel@tonic-gate 3037c478bd9Sstevel@tonic-gate struct snapshot * 3047c478bd9Sstevel@tonic-gate acquire_snapshot(kstat_ctl_t *kc, int types, struct iodev_filter *iodev_filter) 3057c478bd9Sstevel@tonic-gate { 3067c478bd9Sstevel@tonic-gate struct snapshot *ss = NULL; 3077c478bd9Sstevel@tonic-gate int err; 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate retry: 3107c478bd9Sstevel@tonic-gate err = 0; 3117c478bd9Sstevel@tonic-gate /* ensure any partial resources are freed on a retry */ 3127c478bd9Sstevel@tonic-gate free_snapshot(ss); 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate ss = safe_alloc(sizeof (struct snapshot)); 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate (void) memset(ss, 0, sizeof (struct snapshot)); 3177c478bd9Sstevel@tonic-gate 3187c478bd9Sstevel@tonic-gate ss->s_types = types; 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate /* wait for a possibly up-to-date chain */ 3217c478bd9Sstevel@tonic-gate while (kstat_chain_update(kc) == -1) { 3227c478bd9Sstevel@tonic-gate if (errno == EAGAIN) 3237c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, RETRY_DELAY); 3247c478bd9Sstevel@tonic-gate else 3257c478bd9Sstevel@tonic-gate fail(1, "kstat_chain_update failed"); 3267c478bd9Sstevel@tonic-gate } 3277c478bd9Sstevel@tonic-gate 3287c478bd9Sstevel@tonic-gate if (types & SNAP_FLUSHES) { 3297c478bd9Sstevel@tonic-gate kstat_t *ksp; 3307c478bd9Sstevel@tonic-gate ksp = kstat_lookup(kc, "unix", 0, "flushmeter"); 3317c478bd9Sstevel@tonic-gate if (ksp == NULL) { 3327c478bd9Sstevel@tonic-gate fail(0, "This machine does not have " 3337c478bd9Sstevel@tonic-gate "a virtual address cache"); 3347c478bd9Sstevel@tonic-gate } 3357c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, &ss->s_flushes) == -1) 3367c478bd9Sstevel@tonic-gate err = errno; 3377c478bd9Sstevel@tonic-gate } 3387c478bd9Sstevel@tonic-gate 3397c478bd9Sstevel@tonic-gate if (!err && (types & SNAP_INTERRUPTS)) 3407c478bd9Sstevel@tonic-gate err = acquire_intrs(ss, kc); 3417c478bd9Sstevel@tonic-gate 3427c478bd9Sstevel@tonic-gate if (!err && (types & (SNAP_CPUS | SNAP_SYSTEM | SNAP_PSETS))) 3437c478bd9Sstevel@tonic-gate err = acquire_cpus(ss, kc); 3447c478bd9Sstevel@tonic-gate 3457c478bd9Sstevel@tonic-gate if (!err && (types & SNAP_PSETS)) 3467c478bd9Sstevel@tonic-gate err = acquire_psets(ss); 3477c478bd9Sstevel@tonic-gate 348*37fbbce5Scth if (!err && (types & (SNAP_IODEVS | SNAP_CONTROLLERS | 349*37fbbce5Scth SNAP_IOPATHS_LI | SNAP_IOPATHS_LTI))) 3507c478bd9Sstevel@tonic-gate err = acquire_iodevs(ss, kc, iodev_filter); 3517c478bd9Sstevel@tonic-gate 3527c478bd9Sstevel@tonic-gate if (!err && (types & SNAP_SYSTEM)) 3537c478bd9Sstevel@tonic-gate err = acquire_sys(ss, kc); 3547c478bd9Sstevel@tonic-gate 3557c478bd9Sstevel@tonic-gate switch (err) { 3567c478bd9Sstevel@tonic-gate case 0: 3577c478bd9Sstevel@tonic-gate break; 3587c478bd9Sstevel@tonic-gate case EAGAIN: 3597c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, RETRY_DELAY); 3607c478bd9Sstevel@tonic-gate /* a kstat disappeared from under us */ 3617c478bd9Sstevel@tonic-gate /*FALLTHRU*/ 3627c478bd9Sstevel@tonic-gate case ENXIO: 3637c478bd9Sstevel@tonic-gate case ENOENT: 3647c478bd9Sstevel@tonic-gate goto retry; 3657c478bd9Sstevel@tonic-gate default: 3667c478bd9Sstevel@tonic-gate fail(1, "acquiring snapshot failed"); 3677c478bd9Sstevel@tonic-gate } 3687c478bd9Sstevel@tonic-gate 3697c478bd9Sstevel@tonic-gate return (ss); 3707c478bd9Sstevel@tonic-gate } 3717c478bd9Sstevel@tonic-gate 3727c478bd9Sstevel@tonic-gate void 3737c478bd9Sstevel@tonic-gate free_snapshot(struct snapshot *ss) 3747c478bd9Sstevel@tonic-gate { 3757c478bd9Sstevel@tonic-gate size_t i; 3767c478bd9Sstevel@tonic-gate 3777c478bd9Sstevel@tonic-gate if (ss == NULL) 3787c478bd9Sstevel@tonic-gate return; 3797c478bd9Sstevel@tonic-gate 3807c478bd9Sstevel@tonic-gate while (ss->s_iodevs) { 3817c478bd9Sstevel@tonic-gate struct iodev_snapshot *tmp = ss->s_iodevs; 3827c478bd9Sstevel@tonic-gate ss->s_iodevs = ss->s_iodevs->is_next; 3837c478bd9Sstevel@tonic-gate free_iodev(tmp); 3847c478bd9Sstevel@tonic-gate } 3857c478bd9Sstevel@tonic-gate 3867c478bd9Sstevel@tonic-gate if (ss->s_cpus) { 3877c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_cpus; i++) { 3887c478bd9Sstevel@tonic-gate free(ss->s_cpus[i].cs_vm.ks_data); 3897c478bd9Sstevel@tonic-gate free(ss->s_cpus[i].cs_sys.ks_data); 3907c478bd9Sstevel@tonic-gate } 3917c478bd9Sstevel@tonic-gate free(ss->s_cpus); 3927c478bd9Sstevel@tonic-gate } 3937c478bd9Sstevel@tonic-gate 3947c478bd9Sstevel@tonic-gate if (ss->s_psets) { 3957c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_psets; i++) 3967c478bd9Sstevel@tonic-gate free(ss->s_psets[i].ps_cpus); 3977c478bd9Sstevel@tonic-gate free(ss->s_psets); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate free(ss->s_sys.ss_agg_sys.ks_data); 4017c478bd9Sstevel@tonic-gate free(ss->s_sys.ss_agg_vm.ks_data); 4027c478bd9Sstevel@tonic-gate free(ss); 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate kstat_ctl_t * 4067c478bd9Sstevel@tonic-gate open_kstat(void) 4077c478bd9Sstevel@tonic-gate { 4087c478bd9Sstevel@tonic-gate kstat_ctl_t *kc; 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate while ((kc = kstat_open()) == NULL) { 4117c478bd9Sstevel@tonic-gate if (errno == EAGAIN) 4127c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, RETRY_DELAY); 4137c478bd9Sstevel@tonic-gate else 4147c478bd9Sstevel@tonic-gate fail(1, "kstat_open failed"); 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate 4177c478bd9Sstevel@tonic-gate return (kc); 4187c478bd9Sstevel@tonic-gate } 4197c478bd9Sstevel@tonic-gate 4207c478bd9Sstevel@tonic-gate /*PRINTFLIKE2*/ 4217c478bd9Sstevel@tonic-gate void 4227c478bd9Sstevel@tonic-gate fail(int do_perror, char *message, ...) 4237c478bd9Sstevel@tonic-gate { 4247c478bd9Sstevel@tonic-gate va_list args; 4257c478bd9Sstevel@tonic-gate int save_errno = errno; 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate va_start(args, message); 4287c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "%s: ", cmdname); 4297c478bd9Sstevel@tonic-gate (void) vfprintf(stderr, message, args); 4307c478bd9Sstevel@tonic-gate va_end(args); 4317c478bd9Sstevel@tonic-gate if (do_perror) 4327c478bd9Sstevel@tonic-gate (void) fprintf(stderr, ": %s", strerror(save_errno)); 4337c478bd9Sstevel@tonic-gate (void) fprintf(stderr, "\n"); 4347c478bd9Sstevel@tonic-gate exit(2); 4357c478bd9Sstevel@tonic-gate } 4367c478bd9Sstevel@tonic-gate 4377c478bd9Sstevel@tonic-gate void * 4387c478bd9Sstevel@tonic-gate safe_alloc(size_t size) 4397c478bd9Sstevel@tonic-gate { 4407c478bd9Sstevel@tonic-gate void *ptr; 4417c478bd9Sstevel@tonic-gate 4427c478bd9Sstevel@tonic-gate while ((ptr = malloc(size)) == NULL) { 4437c478bd9Sstevel@tonic-gate if (errno == EAGAIN) 4447c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, RETRY_DELAY); 4457c478bd9Sstevel@tonic-gate else 4467c478bd9Sstevel@tonic-gate fail(1, "malloc failed"); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate return (ptr); 4497c478bd9Sstevel@tonic-gate } 4507c478bd9Sstevel@tonic-gate 4517c478bd9Sstevel@tonic-gate char * 4527c478bd9Sstevel@tonic-gate safe_strdup(char *str) 4537c478bd9Sstevel@tonic-gate { 4547c478bd9Sstevel@tonic-gate char *ret; 4557c478bd9Sstevel@tonic-gate 4567c478bd9Sstevel@tonic-gate if (str == NULL) 4577c478bd9Sstevel@tonic-gate return (NULL); 4587c478bd9Sstevel@tonic-gate 4597c478bd9Sstevel@tonic-gate while ((ret = strdup(str)) == NULL) { 4607c478bd9Sstevel@tonic-gate if (errno == EAGAIN) 4617c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, RETRY_DELAY); 4627c478bd9Sstevel@tonic-gate else 4637c478bd9Sstevel@tonic-gate fail(1, "malloc failed"); 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate return (ret); 4667c478bd9Sstevel@tonic-gate } 4677c478bd9Sstevel@tonic-gate 4687c478bd9Sstevel@tonic-gate uint64_t 4697c478bd9Sstevel@tonic-gate kstat_delta(kstat_t *old, kstat_t *new, char *name) 4707c478bd9Sstevel@tonic-gate { 4717c478bd9Sstevel@tonic-gate kstat_named_t *knew = kstat_data_lookup(new, name); 4727c478bd9Sstevel@tonic-gate if (old && old->ks_data) { 4737c478bd9Sstevel@tonic-gate kstat_named_t *kold = kstat_data_lookup(old, name); 4747c478bd9Sstevel@tonic-gate return (knew->value.ui64 - kold->value.ui64); 4757c478bd9Sstevel@tonic-gate } 4767c478bd9Sstevel@tonic-gate return (knew->value.ui64); 4777c478bd9Sstevel@tonic-gate } 4787c478bd9Sstevel@tonic-gate 4797c478bd9Sstevel@tonic-gate int 4807c478bd9Sstevel@tonic-gate kstat_copy(const kstat_t *src, kstat_t *dst) 4817c478bd9Sstevel@tonic-gate { 4827c478bd9Sstevel@tonic-gate *dst = *src; 4837c478bd9Sstevel@tonic-gate 4847c478bd9Sstevel@tonic-gate if (src->ks_data != NULL) { 4857c478bd9Sstevel@tonic-gate if ((dst->ks_data = malloc(src->ks_data_size)) == NULL) 4867c478bd9Sstevel@tonic-gate return (-1); 4877c478bd9Sstevel@tonic-gate bcopy(src->ks_data, dst->ks_data, src->ks_data_size); 4887c478bd9Sstevel@tonic-gate } else { 4897c478bd9Sstevel@tonic-gate dst->ks_data = NULL; 4907c478bd9Sstevel@tonic-gate dst->ks_data_size = 0; 4917c478bd9Sstevel@tonic-gate } 4927c478bd9Sstevel@tonic-gate return (0); 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate 4957c478bd9Sstevel@tonic-gate int 4967c478bd9Sstevel@tonic-gate kstat_add(const kstat_t *src, kstat_t *dst) 4977c478bd9Sstevel@tonic-gate { 4987c478bd9Sstevel@tonic-gate size_t i; 4997c478bd9Sstevel@tonic-gate kstat_named_t *from; 5007c478bd9Sstevel@tonic-gate kstat_named_t *to; 5017c478bd9Sstevel@tonic-gate 5027c478bd9Sstevel@tonic-gate if (dst->ks_data == NULL) 5037c478bd9Sstevel@tonic-gate return (kstat_copy(src, dst)); 5047c478bd9Sstevel@tonic-gate 5057c478bd9Sstevel@tonic-gate from = src->ks_data; 5067c478bd9Sstevel@tonic-gate to = dst->ks_data; 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate for (i = 0; i < src->ks_ndata; i++) { 5097c478bd9Sstevel@tonic-gate /* "addition" makes little sense for strings */ 5107c478bd9Sstevel@tonic-gate if (from->data_type != KSTAT_DATA_CHAR && 5117c478bd9Sstevel@tonic-gate from->data_type != KSTAT_DATA_STRING) 5127c478bd9Sstevel@tonic-gate (to)->value.ui64 += (from)->value.ui64; 5137c478bd9Sstevel@tonic-gate from++; 5147c478bd9Sstevel@tonic-gate to++; 5157c478bd9Sstevel@tonic-gate } 5167c478bd9Sstevel@tonic-gate 5177c478bd9Sstevel@tonic-gate return (0); 5187c478bd9Sstevel@tonic-gate } 5197c478bd9Sstevel@tonic-gate 5207c478bd9Sstevel@tonic-gate uint64_t 5217c478bd9Sstevel@tonic-gate cpu_ticks_delta(kstat_t *old, kstat_t *new) 5227c478bd9Sstevel@tonic-gate { 5237c478bd9Sstevel@tonic-gate uint64_t ticks = 0; 5247c478bd9Sstevel@tonic-gate size_t i; 5257c478bd9Sstevel@tonic-gate for (i = 0; i < ARRAY_SIZE(cpu_states); i++) 5267c478bd9Sstevel@tonic-gate ticks += kstat_delta(old, new, cpu_states[i]); 5277c478bd9Sstevel@tonic-gate return (ticks); 5287c478bd9Sstevel@tonic-gate } 5297c478bd9Sstevel@tonic-gate 5307c478bd9Sstevel@tonic-gate int 5317c478bd9Sstevel@tonic-gate nr_active_cpus(struct snapshot *ss) 5327c478bd9Sstevel@tonic-gate { 5337c478bd9Sstevel@tonic-gate size_t i; 5347c478bd9Sstevel@tonic-gate int count = 0; 5357c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_cpus; i++) { 5367c478bd9Sstevel@tonic-gate if (CPU_ACTIVE(&ss->s_cpus[i])) 5377c478bd9Sstevel@tonic-gate count++; 5387c478bd9Sstevel@tonic-gate } 5397c478bd9Sstevel@tonic-gate 5407c478bd9Sstevel@tonic-gate return (count); 5417c478bd9Sstevel@tonic-gate } 542*37fbbce5Scth 543*37fbbce5Scth /* 544*37fbbce5Scth * Return the number of ticks delta between two hrtime_t 545*37fbbce5Scth * values. Attempt to cater for various kinds of overflow 546*37fbbce5Scth * in hrtime_t - no matter how improbable. 547*37fbbce5Scth */ 548*37fbbce5Scth uint64_t 549*37fbbce5Scth hrtime_delta(hrtime_t old, hrtime_t new) 550*37fbbce5Scth { 551*37fbbce5Scth uint64_t del; 552*37fbbce5Scth 553*37fbbce5Scth if ((new >= old) && (old >= 0L)) 554*37fbbce5Scth return (new - old); 555*37fbbce5Scth else { 556*37fbbce5Scth /* 557*37fbbce5Scth * We've overflowed the positive portion of an 558*37fbbce5Scth * hrtime_t. 559*37fbbce5Scth */ 560*37fbbce5Scth if (new < 0L) { 561*37fbbce5Scth /* 562*37fbbce5Scth * The new value is negative. Handle the 563*37fbbce5Scth * case where the old value is positive or 564*37fbbce5Scth * negative. 565*37fbbce5Scth */ 566*37fbbce5Scth uint64_t n1; 567*37fbbce5Scth uint64_t o1; 568*37fbbce5Scth 569*37fbbce5Scth n1 = -new; 570*37fbbce5Scth if (old > 0L) 571*37fbbce5Scth return (n1 - old); 572*37fbbce5Scth else { 573*37fbbce5Scth o1 = -old; 574*37fbbce5Scth del = n1 - o1; 575*37fbbce5Scth return (del); 576*37fbbce5Scth } 577*37fbbce5Scth } else { 578*37fbbce5Scth /* 579*37fbbce5Scth * Either we've just gone from being negative 580*37fbbce5Scth * to positive *or* the last entry was positive 581*37fbbce5Scth * and the new entry is also positive but *less* 582*37fbbce5Scth * than the old entry. This implies we waited 583*37fbbce5Scth * quite a few days on a very fast system between 584*37fbbce5Scth * iostat displays. 585*37fbbce5Scth */ 586*37fbbce5Scth if (old < 0L) { 587*37fbbce5Scth uint64_t o2; 588*37fbbce5Scth 589*37fbbce5Scth o2 = -old; 590*37fbbce5Scth del = UINT64_MAX - o2; 591*37fbbce5Scth } else { 592*37fbbce5Scth del = UINT64_MAX - old; 593*37fbbce5Scth } 594*37fbbce5Scth del += new; 595*37fbbce5Scth return (del); 596*37fbbce5Scth } 597*37fbbce5Scth } 598*37fbbce5Scth } 599