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