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 537fbbce5Scth * Common Development and Distribution License (the "License"). 637fbbce5Scth * 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 /* 2244c4f64bSJohn Levon * Copyright (c) 2004, 2010, Oracle and/or its affiliates. All rights reserved. 23*1f9f06cfSMatthew Ahrens * Copyright (c) 2012 by Delphix. All rights reserved. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate 267c478bd9Sstevel@tonic-gate #include "statcommon.h" 277c478bd9Sstevel@tonic-gate #include "dsr.h" 287c478bd9Sstevel@tonic-gate 297c478bd9Sstevel@tonic-gate #include <stdlib.h> 307c478bd9Sstevel@tonic-gate #include <unistd.h> 317c478bd9Sstevel@tonic-gate #include <strings.h> 327c478bd9Sstevel@tonic-gate #include <errno.h> 3337fbbce5Scth #include <limits.h> 347c478bd9Sstevel@tonic-gate #include <poll.h> 357c478bd9Sstevel@tonic-gate 367c478bd9Sstevel@tonic-gate #define ARRAY_SIZE(a) (sizeof (a) / sizeof (*a)) 377c478bd9Sstevel@tonic-gate 387c478bd9Sstevel@tonic-gate /* 397c478bd9Sstevel@tonic-gate * The time we delay before retrying after an allocation 407c478bd9Sstevel@tonic-gate * failure, in milliseconds 417c478bd9Sstevel@tonic-gate */ 427c478bd9Sstevel@tonic-gate #define RETRY_DELAY 200 437c478bd9Sstevel@tonic-gate 447c478bd9Sstevel@tonic-gate static char *cpu_states[] = { 457c478bd9Sstevel@tonic-gate "cpu_ticks_idle", 467c478bd9Sstevel@tonic-gate "cpu_ticks_user", 477c478bd9Sstevel@tonic-gate "cpu_ticks_kernel", 487c478bd9Sstevel@tonic-gate "cpu_ticks_wait" 497c478bd9Sstevel@tonic-gate }; 507c478bd9Sstevel@tonic-gate 517c478bd9Sstevel@tonic-gate static kstat_t * 527c478bd9Sstevel@tonic-gate kstat_lookup_read(kstat_ctl_t *kc, char *module, 537c478bd9Sstevel@tonic-gate int instance, char *name) 547c478bd9Sstevel@tonic-gate { 557c478bd9Sstevel@tonic-gate kstat_t *ksp = kstat_lookup(kc, module, instance, name); 567c478bd9Sstevel@tonic-gate if (ksp == NULL) 577c478bd9Sstevel@tonic-gate return (NULL); 587c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) 597c478bd9Sstevel@tonic-gate return (NULL); 607c478bd9Sstevel@tonic-gate return (ksp); 617c478bd9Sstevel@tonic-gate } 627c478bd9Sstevel@tonic-gate 637c478bd9Sstevel@tonic-gate /* 647c478bd9Sstevel@tonic-gate * Note: the following helpers do not clean up on the failure case, 657c478bd9Sstevel@tonic-gate * because it is left to the free_snapshot() in the acquire_snapshot() 667c478bd9Sstevel@tonic-gate * failure path. 677c478bd9Sstevel@tonic-gate */ 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate static int 707c478bd9Sstevel@tonic-gate acquire_cpus(struct snapshot *ss, kstat_ctl_t *kc) 717c478bd9Sstevel@tonic-gate { 727c478bd9Sstevel@tonic-gate size_t i; 737c478bd9Sstevel@tonic-gate 747c478bd9Sstevel@tonic-gate ss->s_nr_cpus = sysconf(_SC_CPUID_MAX) + 1; 757c478bd9Sstevel@tonic-gate ss->s_cpus = calloc(ss->s_nr_cpus, sizeof (struct cpu_snapshot)); 767c478bd9Sstevel@tonic-gate if (ss->s_cpus == NULL) 777c478bd9Sstevel@tonic-gate goto out; 787c478bd9Sstevel@tonic-gate 797c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_cpus; i++) { 807c478bd9Sstevel@tonic-gate kstat_t *ksp; 817c478bd9Sstevel@tonic-gate 827c478bd9Sstevel@tonic-gate ss->s_cpus[i].cs_id = ID_NO_CPU; 837c478bd9Sstevel@tonic-gate ss->s_cpus[i].cs_state = p_online(i, P_STATUS); 847c478bd9Sstevel@tonic-gate /* If no valid CPU is present, move on to the next one */ 857c478bd9Sstevel@tonic-gate if (ss->s_cpus[i].cs_state == -1) 867c478bd9Sstevel@tonic-gate continue; 877c478bd9Sstevel@tonic-gate ss->s_cpus[i].cs_id = i; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup_read(kc, "cpu_info", i, NULL)) == NULL) 907c478bd9Sstevel@tonic-gate goto out; 917c478bd9Sstevel@tonic-gate 927c478bd9Sstevel@tonic-gate (void) pset_assign(PS_QUERY, i, &ss->s_cpus[i].cs_pset_id); 937c478bd9Sstevel@tonic-gate if (ss->s_cpus[i].cs_pset_id == PS_NONE) 947c478bd9Sstevel@tonic-gate ss->s_cpus[i].cs_pset_id = ID_NO_PSET; 957c478bd9Sstevel@tonic-gate 967c478bd9Sstevel@tonic-gate if (!CPU_ACTIVE(&ss->s_cpus[i])) 977c478bd9Sstevel@tonic-gate continue; 987c478bd9Sstevel@tonic-gate 997c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup_read(kc, "cpu", i, "vm")) == NULL) 1007c478bd9Sstevel@tonic-gate goto out; 1017c478bd9Sstevel@tonic-gate 1027c478bd9Sstevel@tonic-gate if (kstat_copy(ksp, &ss->s_cpus[i].cs_vm)) 1037c478bd9Sstevel@tonic-gate goto out; 1047c478bd9Sstevel@tonic-gate 1057c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup_read(kc, "cpu", i, "sys")) == NULL) 1067c478bd9Sstevel@tonic-gate goto out; 1077c478bd9Sstevel@tonic-gate 1087c478bd9Sstevel@tonic-gate if (kstat_copy(ksp, &ss->s_cpus[i].cs_sys)) 1097c478bd9Sstevel@tonic-gate goto out; 1107c478bd9Sstevel@tonic-gate } 1117c478bd9Sstevel@tonic-gate 1127c478bd9Sstevel@tonic-gate errno = 0; 1137c478bd9Sstevel@tonic-gate out: 1147c478bd9Sstevel@tonic-gate return (errno); 1157c478bd9Sstevel@tonic-gate } 1167c478bd9Sstevel@tonic-gate 1177c478bd9Sstevel@tonic-gate static int 1187c478bd9Sstevel@tonic-gate acquire_psets(struct snapshot *ss) 1197c478bd9Sstevel@tonic-gate { 1207c478bd9Sstevel@tonic-gate psetid_t *pids = NULL; 1217c478bd9Sstevel@tonic-gate struct pset_snapshot *ps; 1227c478bd9Sstevel@tonic-gate size_t pids_nr; 1237c478bd9Sstevel@tonic-gate size_t i, j; 1247c478bd9Sstevel@tonic-gate 1257c478bd9Sstevel@tonic-gate /* 1267c478bd9Sstevel@tonic-gate * Careful in this code. We have to use pset_list 1277c478bd9Sstevel@tonic-gate * twice, but inbetween pids_nr can change at will. 1287c478bd9Sstevel@tonic-gate * We delay the setting of s_nr_psets until we have 1297c478bd9Sstevel@tonic-gate * the "final" value of pids_nr. 1307c478bd9Sstevel@tonic-gate */ 1317c478bd9Sstevel@tonic-gate 1327c478bd9Sstevel@tonic-gate if (pset_list(NULL, &pids_nr) < 0) 1337c478bd9Sstevel@tonic-gate return (errno); 1347c478bd9Sstevel@tonic-gate 1357c478bd9Sstevel@tonic-gate if ((pids = calloc(pids_nr, sizeof (psetid_t))) == NULL) 1367c478bd9Sstevel@tonic-gate goto out; 1377c478bd9Sstevel@tonic-gate 1387c478bd9Sstevel@tonic-gate if (pset_list(pids, &pids_nr) < 0) 1397c478bd9Sstevel@tonic-gate goto out; 1407c478bd9Sstevel@tonic-gate 1417c478bd9Sstevel@tonic-gate ss->s_psets = calloc(pids_nr + 1, sizeof (struct pset_snapshot)); 1427c478bd9Sstevel@tonic-gate if (ss->s_psets == NULL) 1437c478bd9Sstevel@tonic-gate goto out; 1447c478bd9Sstevel@tonic-gate ss->s_nr_psets = pids_nr + 1; 1457c478bd9Sstevel@tonic-gate 1467c478bd9Sstevel@tonic-gate /* CPUs not in any actual pset */ 1477c478bd9Sstevel@tonic-gate ps = &ss->s_psets[0]; 1487c478bd9Sstevel@tonic-gate ps->ps_id = 0; 1497c478bd9Sstevel@tonic-gate ps->ps_cpus = calloc(ss->s_nr_cpus, sizeof (struct cpu_snapshot *)); 1507c478bd9Sstevel@tonic-gate if (ps->ps_cpus == NULL) 1517c478bd9Sstevel@tonic-gate goto out; 1527c478bd9Sstevel@tonic-gate 1537c478bd9Sstevel@tonic-gate /* CPUs in a a pset */ 1547c478bd9Sstevel@tonic-gate for (i = 1; i < ss->s_nr_psets; i++) { 1557c478bd9Sstevel@tonic-gate ps = &ss->s_psets[i]; 1567c478bd9Sstevel@tonic-gate 1577c478bd9Sstevel@tonic-gate ps->ps_id = pids[i - 1]; 1587c478bd9Sstevel@tonic-gate ps->ps_cpus = 1597c478bd9Sstevel@tonic-gate calloc(ss->s_nr_cpus, sizeof (struct cpu_snapshot *)); 1607c478bd9Sstevel@tonic-gate if (ps->ps_cpus == NULL) 1617c478bd9Sstevel@tonic-gate goto out; 1627c478bd9Sstevel@tonic-gate } 1637c478bd9Sstevel@tonic-gate 1647c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_psets; i++) { 1657c478bd9Sstevel@tonic-gate ps = &ss->s_psets[i]; 1667c478bd9Sstevel@tonic-gate 1677c478bd9Sstevel@tonic-gate for (j = 0; j < ss->s_nr_cpus; j++) { 1687c478bd9Sstevel@tonic-gate if (!CPU_ACTIVE(&ss->s_cpus[j])) 1697c478bd9Sstevel@tonic-gate continue; 1707c478bd9Sstevel@tonic-gate if (ss->s_cpus[j].cs_pset_id != ps->ps_id) 1717c478bd9Sstevel@tonic-gate continue; 1727c478bd9Sstevel@tonic-gate 1737c478bd9Sstevel@tonic-gate ps->ps_cpus[ps->ps_nr_cpus++] = &ss->s_cpus[j]; 1747c478bd9Sstevel@tonic-gate } 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate errno = 0; 1787c478bd9Sstevel@tonic-gate out: 1797c478bd9Sstevel@tonic-gate free(pids); 1807c478bd9Sstevel@tonic-gate return (errno); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate static int 1847c478bd9Sstevel@tonic-gate acquire_intrs(struct snapshot *ss, kstat_ctl_t *kc) 1857c478bd9Sstevel@tonic-gate { 1867c478bd9Sstevel@tonic-gate kstat_t *ksp; 1877c478bd9Sstevel@tonic-gate size_t i = 0; 1887c478bd9Sstevel@tonic-gate kstat_t *sys_misc; 1897c478bd9Sstevel@tonic-gate kstat_named_t *clock; 1907c478bd9Sstevel@tonic-gate 1917c478bd9Sstevel@tonic-gate /* clock interrupt */ 1927c478bd9Sstevel@tonic-gate ss->s_nr_intrs = 1; 1937c478bd9Sstevel@tonic-gate 1947c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { 1957c478bd9Sstevel@tonic-gate if (ksp->ks_type == KSTAT_TYPE_INTR) 1967c478bd9Sstevel@tonic-gate ss->s_nr_intrs++; 1977c478bd9Sstevel@tonic-gate } 1987c478bd9Sstevel@tonic-gate 1997c478bd9Sstevel@tonic-gate ss->s_intrs = calloc(ss->s_nr_intrs, sizeof (struct intr_snapshot)); 2007c478bd9Sstevel@tonic-gate if (ss->s_intrs == NULL) 2017c478bd9Sstevel@tonic-gate return (errno); 2027c478bd9Sstevel@tonic-gate 2037c478bd9Sstevel@tonic-gate sys_misc = kstat_lookup_read(kc, "unix", 0, "system_misc"); 2047c478bd9Sstevel@tonic-gate if (sys_misc == NULL) 2057c478bd9Sstevel@tonic-gate goto out; 2067c478bd9Sstevel@tonic-gate 2077c478bd9Sstevel@tonic-gate clock = (kstat_named_t *)kstat_data_lookup(sys_misc, "clk_intr"); 2087c478bd9Sstevel@tonic-gate if (clock == NULL) 2097c478bd9Sstevel@tonic-gate goto out; 2107c478bd9Sstevel@tonic-gate 2117c478bd9Sstevel@tonic-gate (void) strlcpy(ss->s_intrs[0].is_name, "clock", KSTAT_STRLEN); 2127c478bd9Sstevel@tonic-gate ss->s_intrs[0].is_total = clock->value.ui32; 2137c478bd9Sstevel@tonic-gate 2147c478bd9Sstevel@tonic-gate i = 1; 2157c478bd9Sstevel@tonic-gate 2167c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { 2177c478bd9Sstevel@tonic-gate kstat_intr_t *ki; 2187c478bd9Sstevel@tonic-gate int j; 2197c478bd9Sstevel@tonic-gate 2207c478bd9Sstevel@tonic-gate if (ksp->ks_type != KSTAT_TYPE_INTR) 2217c478bd9Sstevel@tonic-gate continue; 2227c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) 2237c478bd9Sstevel@tonic-gate goto out; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate ki = KSTAT_INTR_PTR(ksp); 2267c478bd9Sstevel@tonic-gate 2277c478bd9Sstevel@tonic-gate (void) strlcpy(ss->s_intrs[i].is_name, ksp->ks_name, 2287c478bd9Sstevel@tonic-gate KSTAT_STRLEN); 2297c478bd9Sstevel@tonic-gate ss->s_intrs[i].is_total = 0; 2307c478bd9Sstevel@tonic-gate 2317c478bd9Sstevel@tonic-gate for (j = 0; j < KSTAT_NUM_INTRS; j++) 2327c478bd9Sstevel@tonic-gate ss->s_intrs[i].is_total += ki->intrs[j]; 2337c478bd9Sstevel@tonic-gate 2347c478bd9Sstevel@tonic-gate i++; 2357c478bd9Sstevel@tonic-gate } 2367c478bd9Sstevel@tonic-gate 2377c478bd9Sstevel@tonic-gate errno = 0; 2387c478bd9Sstevel@tonic-gate out: 2397c478bd9Sstevel@tonic-gate return (errno); 2407c478bd9Sstevel@tonic-gate } 2417c478bd9Sstevel@tonic-gate 2427c478bd9Sstevel@tonic-gate int 2437c478bd9Sstevel@tonic-gate acquire_sys(struct snapshot *ss, kstat_ctl_t *kc) 2447c478bd9Sstevel@tonic-gate { 2457c478bd9Sstevel@tonic-gate size_t i; 2467c478bd9Sstevel@tonic-gate kstat_named_t *knp; 2477c478bd9Sstevel@tonic-gate kstat_t *ksp; 2487c478bd9Sstevel@tonic-gate 2497c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "unix", 0, "sysinfo")) == NULL) 2507c478bd9Sstevel@tonic-gate return (errno); 2517c478bd9Sstevel@tonic-gate 2527c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, &ss->s_sys.ss_sysinfo) == -1) 2537c478bd9Sstevel@tonic-gate return (errno); 2547c478bd9Sstevel@tonic-gate 2557c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "unix", 0, "vminfo")) == NULL) 2567c478bd9Sstevel@tonic-gate return (errno); 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, &ss->s_sys.ss_vminfo) == -1) 2597c478bd9Sstevel@tonic-gate return (errno); 2607c478bd9Sstevel@tonic-gate 2617c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "unix", 0, "dnlcstats")) == NULL) 2627c478bd9Sstevel@tonic-gate return (errno); 2637c478bd9Sstevel@tonic-gate 2647c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, &ss->s_sys.ss_nc) == -1) 2657c478bd9Sstevel@tonic-gate return (errno); 2667c478bd9Sstevel@tonic-gate 2677c478bd9Sstevel@tonic-gate if ((ksp = kstat_lookup(kc, "unix", 0, "system_misc")) == NULL) 2687c478bd9Sstevel@tonic-gate return (errno); 2697c478bd9Sstevel@tonic-gate 2707c478bd9Sstevel@tonic-gate if (kstat_read(kc, ksp, NULL) == -1) 2717c478bd9Sstevel@tonic-gate return (errno); 2727c478bd9Sstevel@tonic-gate 2737c478bd9Sstevel@tonic-gate knp = (kstat_named_t *)kstat_data_lookup(ksp, "clk_intr"); 2747c478bd9Sstevel@tonic-gate if (knp == NULL) 2757c478bd9Sstevel@tonic-gate return (errno); 2767c478bd9Sstevel@tonic-gate 2777c478bd9Sstevel@tonic-gate ss->s_sys.ss_ticks = knp->value.l; 2787c478bd9Sstevel@tonic-gate 2797c478bd9Sstevel@tonic-gate knp = (kstat_named_t *)kstat_data_lookup(ksp, "deficit"); 2807c478bd9Sstevel@tonic-gate if (knp == NULL) 2817c478bd9Sstevel@tonic-gate return (errno); 2827c478bd9Sstevel@tonic-gate 2837c478bd9Sstevel@tonic-gate ss->s_sys.ss_deficit = knp->value.l; 2847c478bd9Sstevel@tonic-gate 2857c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_cpus; i++) { 2867c478bd9Sstevel@tonic-gate if (!CPU_ACTIVE(&ss->s_cpus[i])) 2877c478bd9Sstevel@tonic-gate continue; 2887c478bd9Sstevel@tonic-gate 2897c478bd9Sstevel@tonic-gate if (kstat_add(&ss->s_cpus[i].cs_sys, &ss->s_sys.ss_agg_sys)) 2907c478bd9Sstevel@tonic-gate return (errno); 2917c478bd9Sstevel@tonic-gate if (kstat_add(&ss->s_cpus[i].cs_vm, &ss->s_sys.ss_agg_vm)) 2927c478bd9Sstevel@tonic-gate return (errno); 293*1f9f06cfSMatthew Ahrens ss->s_nr_active_cpus++; 2947c478bd9Sstevel@tonic-gate } 2957c478bd9Sstevel@tonic-gate 2967c478bd9Sstevel@tonic-gate return (0); 2977c478bd9Sstevel@tonic-gate } 2987c478bd9Sstevel@tonic-gate 2997c478bd9Sstevel@tonic-gate struct snapshot * 3007c478bd9Sstevel@tonic-gate acquire_snapshot(kstat_ctl_t *kc, int types, struct iodev_filter *iodev_filter) 3017c478bd9Sstevel@tonic-gate { 3027c478bd9Sstevel@tonic-gate struct snapshot *ss = NULL; 3037c478bd9Sstevel@tonic-gate int err; 3047c478bd9Sstevel@tonic-gate 3057c478bd9Sstevel@tonic-gate retry: 3067c478bd9Sstevel@tonic-gate err = 0; 3077c478bd9Sstevel@tonic-gate /* ensure any partial resources are freed on a retry */ 3087c478bd9Sstevel@tonic-gate free_snapshot(ss); 3097c478bd9Sstevel@tonic-gate 3107c478bd9Sstevel@tonic-gate ss = safe_alloc(sizeof (struct snapshot)); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate (void) memset(ss, 0, sizeof (struct snapshot)); 3137c478bd9Sstevel@tonic-gate 3147c478bd9Sstevel@tonic-gate ss->s_types = types; 3157c478bd9Sstevel@tonic-gate 3167c478bd9Sstevel@tonic-gate /* wait for a possibly up-to-date chain */ 3177c478bd9Sstevel@tonic-gate while (kstat_chain_update(kc) == -1) { 3187c478bd9Sstevel@tonic-gate if (errno == EAGAIN) 3197c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, RETRY_DELAY); 3207c478bd9Sstevel@tonic-gate else 3217c478bd9Sstevel@tonic-gate fail(1, "kstat_chain_update failed"); 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate if (!err && (types & SNAP_INTERRUPTS)) 3257c478bd9Sstevel@tonic-gate err = acquire_intrs(ss, kc); 3267c478bd9Sstevel@tonic-gate 3277c478bd9Sstevel@tonic-gate if (!err && (types & (SNAP_CPUS | SNAP_SYSTEM | SNAP_PSETS))) 3287c478bd9Sstevel@tonic-gate err = acquire_cpus(ss, kc); 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate if (!err && (types & SNAP_PSETS)) 3317c478bd9Sstevel@tonic-gate err = acquire_psets(ss); 3327c478bd9Sstevel@tonic-gate 33337fbbce5Scth if (!err && (types & (SNAP_IODEVS | SNAP_CONTROLLERS | 33437fbbce5Scth SNAP_IOPATHS_LI | SNAP_IOPATHS_LTI))) 3357c478bd9Sstevel@tonic-gate err = acquire_iodevs(ss, kc, iodev_filter); 3367c478bd9Sstevel@tonic-gate 3377c478bd9Sstevel@tonic-gate if (!err && (types & SNAP_SYSTEM)) 3387c478bd9Sstevel@tonic-gate err = acquire_sys(ss, kc); 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate switch (err) { 3417c478bd9Sstevel@tonic-gate case 0: 3427c478bd9Sstevel@tonic-gate break; 3437c478bd9Sstevel@tonic-gate case EAGAIN: 3447c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, RETRY_DELAY); 3457c478bd9Sstevel@tonic-gate /* a kstat disappeared from under us */ 3467c478bd9Sstevel@tonic-gate /*FALLTHRU*/ 3477c478bd9Sstevel@tonic-gate case ENXIO: 3487c478bd9Sstevel@tonic-gate case ENOENT: 3497c478bd9Sstevel@tonic-gate goto retry; 3507c478bd9Sstevel@tonic-gate default: 3517c478bd9Sstevel@tonic-gate fail(1, "acquiring snapshot failed"); 3527c478bd9Sstevel@tonic-gate } 3537c478bd9Sstevel@tonic-gate 3547c478bd9Sstevel@tonic-gate return (ss); 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate 3577c478bd9Sstevel@tonic-gate void 3587c478bd9Sstevel@tonic-gate free_snapshot(struct snapshot *ss) 3597c478bd9Sstevel@tonic-gate { 3607c478bd9Sstevel@tonic-gate size_t i; 3617c478bd9Sstevel@tonic-gate 3627c478bd9Sstevel@tonic-gate if (ss == NULL) 3637c478bd9Sstevel@tonic-gate return; 3647c478bd9Sstevel@tonic-gate 3657c478bd9Sstevel@tonic-gate while (ss->s_iodevs) { 3667c478bd9Sstevel@tonic-gate struct iodev_snapshot *tmp = ss->s_iodevs; 3677c478bd9Sstevel@tonic-gate ss->s_iodevs = ss->s_iodevs->is_next; 3687c478bd9Sstevel@tonic-gate free_iodev(tmp); 3697c478bd9Sstevel@tonic-gate } 3707c478bd9Sstevel@tonic-gate 3717c478bd9Sstevel@tonic-gate if (ss->s_cpus) { 3727c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_cpus; i++) { 3737c478bd9Sstevel@tonic-gate free(ss->s_cpus[i].cs_vm.ks_data); 3747c478bd9Sstevel@tonic-gate free(ss->s_cpus[i].cs_sys.ks_data); 3757c478bd9Sstevel@tonic-gate } 3767c478bd9Sstevel@tonic-gate free(ss->s_cpus); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate if (ss->s_psets) { 3807c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_psets; i++) 3817c478bd9Sstevel@tonic-gate free(ss->s_psets[i].ps_cpus); 3827c478bd9Sstevel@tonic-gate free(ss->s_psets); 3837c478bd9Sstevel@tonic-gate } 3847c478bd9Sstevel@tonic-gate 3857c478bd9Sstevel@tonic-gate free(ss->s_sys.ss_agg_sys.ks_data); 3867c478bd9Sstevel@tonic-gate free(ss->s_sys.ss_agg_vm.ks_data); 3877c478bd9Sstevel@tonic-gate free(ss); 3887c478bd9Sstevel@tonic-gate } 3897c478bd9Sstevel@tonic-gate 3907c478bd9Sstevel@tonic-gate kstat_ctl_t * 3917c478bd9Sstevel@tonic-gate open_kstat(void) 3927c478bd9Sstevel@tonic-gate { 3937c478bd9Sstevel@tonic-gate kstat_ctl_t *kc; 3947c478bd9Sstevel@tonic-gate 3957c478bd9Sstevel@tonic-gate while ((kc = kstat_open()) == NULL) { 3967c478bd9Sstevel@tonic-gate if (errno == EAGAIN) 3977c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, RETRY_DELAY); 3987c478bd9Sstevel@tonic-gate else 3997c478bd9Sstevel@tonic-gate fail(1, "kstat_open failed"); 4007c478bd9Sstevel@tonic-gate } 4017c478bd9Sstevel@tonic-gate 4027c478bd9Sstevel@tonic-gate return (kc); 4037c478bd9Sstevel@tonic-gate } 4047c478bd9Sstevel@tonic-gate 4057c478bd9Sstevel@tonic-gate void * 4067c478bd9Sstevel@tonic-gate safe_alloc(size_t size) 4077c478bd9Sstevel@tonic-gate { 4087c478bd9Sstevel@tonic-gate void *ptr; 4097c478bd9Sstevel@tonic-gate 4107c478bd9Sstevel@tonic-gate while ((ptr = malloc(size)) == 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, "malloc failed"); 4157c478bd9Sstevel@tonic-gate } 4167c478bd9Sstevel@tonic-gate return (ptr); 4177c478bd9Sstevel@tonic-gate } 4187c478bd9Sstevel@tonic-gate 4197c478bd9Sstevel@tonic-gate char * 4207c478bd9Sstevel@tonic-gate safe_strdup(char *str) 4217c478bd9Sstevel@tonic-gate { 4227c478bd9Sstevel@tonic-gate char *ret; 4237c478bd9Sstevel@tonic-gate 4247c478bd9Sstevel@tonic-gate if (str == NULL) 4257c478bd9Sstevel@tonic-gate return (NULL); 4267c478bd9Sstevel@tonic-gate 4277c478bd9Sstevel@tonic-gate while ((ret = strdup(str)) == NULL) { 4287c478bd9Sstevel@tonic-gate if (errno == EAGAIN) 4297c478bd9Sstevel@tonic-gate (void) poll(NULL, 0, RETRY_DELAY); 4307c478bd9Sstevel@tonic-gate else 4317c478bd9Sstevel@tonic-gate fail(1, "malloc failed"); 4327c478bd9Sstevel@tonic-gate } 4337c478bd9Sstevel@tonic-gate return (ret); 4347c478bd9Sstevel@tonic-gate } 4357c478bd9Sstevel@tonic-gate 4367c478bd9Sstevel@tonic-gate uint64_t 4377c478bd9Sstevel@tonic-gate kstat_delta(kstat_t *old, kstat_t *new, char *name) 4387c478bd9Sstevel@tonic-gate { 4397c478bd9Sstevel@tonic-gate kstat_named_t *knew = kstat_data_lookup(new, name); 4407c478bd9Sstevel@tonic-gate if (old && old->ks_data) { 4417c478bd9Sstevel@tonic-gate kstat_named_t *kold = kstat_data_lookup(old, name); 4427c478bd9Sstevel@tonic-gate return (knew->value.ui64 - kold->value.ui64); 4437c478bd9Sstevel@tonic-gate } 4447c478bd9Sstevel@tonic-gate return (knew->value.ui64); 4457c478bd9Sstevel@tonic-gate } 4467c478bd9Sstevel@tonic-gate 4477c478bd9Sstevel@tonic-gate int 4487c478bd9Sstevel@tonic-gate kstat_copy(const kstat_t *src, kstat_t *dst) 4497c478bd9Sstevel@tonic-gate { 4507c478bd9Sstevel@tonic-gate *dst = *src; 4517c478bd9Sstevel@tonic-gate 4527c478bd9Sstevel@tonic-gate if (src->ks_data != NULL) { 4537c478bd9Sstevel@tonic-gate if ((dst->ks_data = malloc(src->ks_data_size)) == NULL) 4547c478bd9Sstevel@tonic-gate return (-1); 4557c478bd9Sstevel@tonic-gate bcopy(src->ks_data, dst->ks_data, src->ks_data_size); 4567c478bd9Sstevel@tonic-gate } else { 4577c478bd9Sstevel@tonic-gate dst->ks_data = NULL; 4587c478bd9Sstevel@tonic-gate dst->ks_data_size = 0; 4597c478bd9Sstevel@tonic-gate } 4607c478bd9Sstevel@tonic-gate return (0); 4617c478bd9Sstevel@tonic-gate } 4627c478bd9Sstevel@tonic-gate 4637c478bd9Sstevel@tonic-gate int 4647c478bd9Sstevel@tonic-gate kstat_add(const kstat_t *src, kstat_t *dst) 4657c478bd9Sstevel@tonic-gate { 4667c478bd9Sstevel@tonic-gate size_t i; 4677c478bd9Sstevel@tonic-gate kstat_named_t *from; 4687c478bd9Sstevel@tonic-gate kstat_named_t *to; 4697c478bd9Sstevel@tonic-gate 4707c478bd9Sstevel@tonic-gate if (dst->ks_data == NULL) 4717c478bd9Sstevel@tonic-gate return (kstat_copy(src, dst)); 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate from = src->ks_data; 4747c478bd9Sstevel@tonic-gate to = dst->ks_data; 4757c478bd9Sstevel@tonic-gate 4767c478bd9Sstevel@tonic-gate for (i = 0; i < src->ks_ndata; i++) { 4777c478bd9Sstevel@tonic-gate /* "addition" makes little sense for strings */ 4787c478bd9Sstevel@tonic-gate if (from->data_type != KSTAT_DATA_CHAR && 4797c478bd9Sstevel@tonic-gate from->data_type != KSTAT_DATA_STRING) 4807c478bd9Sstevel@tonic-gate (to)->value.ui64 += (from)->value.ui64; 4817c478bd9Sstevel@tonic-gate from++; 4827c478bd9Sstevel@tonic-gate to++; 4837c478bd9Sstevel@tonic-gate } 4847c478bd9Sstevel@tonic-gate 4857c478bd9Sstevel@tonic-gate return (0); 4867c478bd9Sstevel@tonic-gate } 4877c478bd9Sstevel@tonic-gate 4887c478bd9Sstevel@tonic-gate uint64_t 4897c478bd9Sstevel@tonic-gate cpu_ticks_delta(kstat_t *old, kstat_t *new) 4907c478bd9Sstevel@tonic-gate { 4917c478bd9Sstevel@tonic-gate uint64_t ticks = 0; 4927c478bd9Sstevel@tonic-gate size_t i; 4937c478bd9Sstevel@tonic-gate for (i = 0; i < ARRAY_SIZE(cpu_states); i++) 4947c478bd9Sstevel@tonic-gate ticks += kstat_delta(old, new, cpu_states[i]); 4957c478bd9Sstevel@tonic-gate return (ticks); 4967c478bd9Sstevel@tonic-gate } 4977c478bd9Sstevel@tonic-gate 4987c478bd9Sstevel@tonic-gate int 4997c478bd9Sstevel@tonic-gate nr_active_cpus(struct snapshot *ss) 5007c478bd9Sstevel@tonic-gate { 5017c478bd9Sstevel@tonic-gate size_t i; 5027c478bd9Sstevel@tonic-gate int count = 0; 5037c478bd9Sstevel@tonic-gate for (i = 0; i < ss->s_nr_cpus; i++) { 5047c478bd9Sstevel@tonic-gate if (CPU_ACTIVE(&ss->s_cpus[i])) 5057c478bd9Sstevel@tonic-gate count++; 5067c478bd9Sstevel@tonic-gate } 5077c478bd9Sstevel@tonic-gate 5087c478bd9Sstevel@tonic-gate return (count); 5097c478bd9Sstevel@tonic-gate } 51037fbbce5Scth 51137fbbce5Scth /* 51237fbbce5Scth * Return the number of ticks delta between two hrtime_t 51337fbbce5Scth * values. Attempt to cater for various kinds of overflow 51437fbbce5Scth * in hrtime_t - no matter how improbable. 51537fbbce5Scth */ 51637fbbce5Scth uint64_t 51737fbbce5Scth hrtime_delta(hrtime_t old, hrtime_t new) 51837fbbce5Scth { 51937fbbce5Scth uint64_t del; 52037fbbce5Scth 52137fbbce5Scth if ((new >= old) && (old >= 0L)) 52237fbbce5Scth return (new - old); 52337fbbce5Scth else { 52437fbbce5Scth /* 52537fbbce5Scth * We've overflowed the positive portion of an 52637fbbce5Scth * hrtime_t. 52737fbbce5Scth */ 52837fbbce5Scth if (new < 0L) { 52937fbbce5Scth /* 53037fbbce5Scth * The new value is negative. Handle the 53137fbbce5Scth * case where the old value is positive or 53237fbbce5Scth * negative. 53337fbbce5Scth */ 53437fbbce5Scth uint64_t n1; 53537fbbce5Scth uint64_t o1; 53637fbbce5Scth 53737fbbce5Scth n1 = -new; 53837fbbce5Scth if (old > 0L) 53937fbbce5Scth return (n1 - old); 54037fbbce5Scth else { 54137fbbce5Scth o1 = -old; 54237fbbce5Scth del = n1 - o1; 54337fbbce5Scth return (del); 54437fbbce5Scth } 54537fbbce5Scth } else { 54637fbbce5Scth /* 54737fbbce5Scth * Either we've just gone from being negative 54837fbbce5Scth * to positive *or* the last entry was positive 54937fbbce5Scth * and the new entry is also positive but *less* 55037fbbce5Scth * than the old entry. This implies we waited 55137fbbce5Scth * quite a few days on a very fast system between 55237fbbce5Scth * iostat displays. 55337fbbce5Scth */ 55437fbbce5Scth if (old < 0L) { 55537fbbce5Scth uint64_t o2; 55637fbbce5Scth 55737fbbce5Scth o2 = -old; 55837fbbce5Scth del = UINT64_MAX - o2; 55937fbbce5Scth } else { 56037fbbce5Scth del = UINT64_MAX - old; 56137fbbce5Scth } 56237fbbce5Scth del += new; 56337fbbce5Scth return (del); 56437fbbce5Scth } 56537fbbce5Scth } 56637fbbce5Scth } 567