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*c42872d4Smh27603 * Common Development and Distribution License (the "License"). 6*c42872d4Smh27603 * 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*c42872d4Smh27603 * Copyright 2006 Sun Microsystems, Inc. All rights reserved. 237c478bd9Sstevel@tonic-gate * Use is subject to license terms. 247c478bd9Sstevel@tonic-gate */ 257c478bd9Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI" 267c478bd9Sstevel@tonic-gate 277c478bd9Sstevel@tonic-gate #include <stdio.h> 287c478bd9Sstevel@tonic-gate #include <stdlib.h> 297c478bd9Sstevel@tonic-gate #include <stdarg.h> 307c478bd9Sstevel@tonic-gate #include <unistd.h> /* sleep() */ 317c478bd9Sstevel@tonic-gate #include <string.h> 327c478bd9Sstevel@tonic-gate #include <errno.h> 337c478bd9Sstevel@tonic-gate #include <syslog.h> 347c478bd9Sstevel@tonic-gate #include <thread.h> 357c478bd9Sstevel@tonic-gate #include <time.h> 367c478bd9Sstevel@tonic-gate #include <kstat.h> 377c478bd9Sstevel@tonic-gate #include <sys/sysinfo.h> 387c478bd9Sstevel@tonic-gate #include <sys/sysmacros.h> 397c478bd9Sstevel@tonic-gate #include "powerd.h" 407c478bd9Sstevel@tonic-gate 417c478bd9Sstevel@tonic-gate /* 427c478bd9Sstevel@tonic-gate * External Variables 437c478bd9Sstevel@tonic-gate */ 447c478bd9Sstevel@tonic-gate extern pwr_info_t *info; 457c478bd9Sstevel@tonic-gate 467c478bd9Sstevel@tonic-gate /* 477c478bd9Sstevel@tonic-gate * State Variables 487c478bd9Sstevel@tonic-gate */ 497c478bd9Sstevel@tonic-gate static kstat_ctl_t *kc; /* libkstat cookie */ 507c478bd9Sstevel@tonic-gate static int ncpus; 517c478bd9Sstevel@tonic-gate static kstat_t **cpu_stats_list = NULL; 527c478bd9Sstevel@tonic-gate static kstat_t old_cpu_stats, new_cpu_stats; 537c478bd9Sstevel@tonic-gate static hrtime_t tty_snaptime; 547c478bd9Sstevel@tonic-gate static kstat_t *load_ave_ksp; 557c478bd9Sstevel@tonic-gate static ulong_t load_ave; 567c478bd9Sstevel@tonic-gate static hrtime_t last_load_ave_change; 577c478bd9Sstevel@tonic-gate static kstat_t *conskbd_ksp, *consms_ksp; 587c478bd9Sstevel@tonic-gate static kstat_t *nfs_client2_kstat, *nfs_client3_kstat; 597c478bd9Sstevel@tonic-gate static kstat_t *nfs_server2_kstat, *nfs_server3_kstat; 607c478bd9Sstevel@tonic-gate static uint64_t old_nfs_calls, new_nfs_calls; 617c478bd9Sstevel@tonic-gate 627c478bd9Sstevel@tonic-gate typedef struct activity_data { 637c478bd9Sstevel@tonic-gate struct activity_data *next; 647c478bd9Sstevel@tonic-gate struct activity_data *prev; 657c478bd9Sstevel@tonic-gate int activity_delta; 667c478bd9Sstevel@tonic-gate hrtime_t snaptime; 677c478bd9Sstevel@tonic-gate } activity_data_t; 687c478bd9Sstevel@tonic-gate 697c478bd9Sstevel@tonic-gate #define NULLACTIVITY (activity_data_t *)0 707c478bd9Sstevel@tonic-gate static activity_data_t *disk_act_start = NULLACTIVITY; 717c478bd9Sstevel@tonic-gate static activity_data_t *disk_act_end = NULLACTIVITY; 727c478bd9Sstevel@tonic-gate static activity_data_t *tty_act_start = NULLACTIVITY; 737c478bd9Sstevel@tonic-gate static activity_data_t *tty_act_end = NULLACTIVITY; 747c478bd9Sstevel@tonic-gate static activity_data_t *nfs_act_start = NULLACTIVITY; 757c478bd9Sstevel@tonic-gate static activity_data_t *nfs_act_end = NULLACTIVITY; 767c478bd9Sstevel@tonic-gate 777c478bd9Sstevel@tonic-gate struct diskinfo { 787c478bd9Sstevel@tonic-gate struct diskinfo *next; 797c478bd9Sstevel@tonic-gate kstat_t *ks; 807c478bd9Sstevel@tonic-gate kstat_io_t new_kios, old_kios; 817c478bd9Sstevel@tonic-gate }; 827c478bd9Sstevel@tonic-gate 837c478bd9Sstevel@tonic-gate #define NULLDISK (struct diskinfo *)0 847c478bd9Sstevel@tonic-gate static struct diskinfo zerodisk = { NULL, NULL }; 857c478bd9Sstevel@tonic-gate static struct diskinfo *firstdisk = NULLDISK; 867c478bd9Sstevel@tonic-gate static struct diskinfo *lastdisk = NULLDISK; 877c478bd9Sstevel@tonic-gate static struct diskinfo *snip = NULLDISK; 887c478bd9Sstevel@tonic-gate 897c478bd9Sstevel@tonic-gate #define CPU_STAT(ksp, name) (((kstat_named_t *)safe_kstat_data_lookup( \ 907c478bd9Sstevel@tonic-gate (ksp), (name)))->value.ui64) 917c478bd9Sstevel@tonic-gate #define DISK_DELTA(x) (disk->new_kios.x - disk->old_kios.x) 927c478bd9Sstevel@tonic-gate #define CPU_DELTA(x) (CPU_STAT(&new_cpu_stats, (x)) - \ 937c478bd9Sstevel@tonic-gate CPU_STAT(&old_cpu_stats, (x))) 947c478bd9Sstevel@tonic-gate #define FSHIFT 8 957c478bd9Sstevel@tonic-gate #define FSCALE (1<<FSHIFT) 967c478bd9Sstevel@tonic-gate 977c478bd9Sstevel@tonic-gate /* 987c478bd9Sstevel@tonic-gate * Local Functions 997c478bd9Sstevel@tonic-gate */ 1007c478bd9Sstevel@tonic-gate static void init_all(void); 1017c478bd9Sstevel@tonic-gate static void init_disks(void); 1027c478bd9Sstevel@tonic-gate static void cpu_stats_init(void); 1037c478bd9Sstevel@tonic-gate static void load_ave_init(void); 1047c478bd9Sstevel@tonic-gate static void nfs_init(void); 1057c478bd9Sstevel@tonic-gate static void conskbd_init(void); 1067c478bd9Sstevel@tonic-gate static void consms_init(void); 1077c478bd9Sstevel@tonic-gate static int diskinfo_load(void); 1087c478bd9Sstevel@tonic-gate static int cpu_stats_load(void); 1097c478bd9Sstevel@tonic-gate static int load_ave_load(void); 1107c478bd9Sstevel@tonic-gate static int nfs_load(void); 1117c478bd9Sstevel@tonic-gate static void fail(char *, ...); 1127c478bd9Sstevel@tonic-gate static void safe_zalloc(void **, int, int); 1137c478bd9Sstevel@tonic-gate static void *safe_kstat_data_lookup(kstat_t *, char *); 1147c478bd9Sstevel@tonic-gate static int kscmp(kstat_t *, kstat_t *); 1157c478bd9Sstevel@tonic-gate static void keep_activity_data(activity_data_t **, activity_data_t **, 1167c478bd9Sstevel@tonic-gate int *, int, hrtime_t); 1177c478bd9Sstevel@tonic-gate static int check_activity(activity_data_t *, int, hrtime_t *, int); 1187c478bd9Sstevel@tonic-gate static void kstat_copy(kstat_t *, kstat_t *, int); 1197c478bd9Sstevel@tonic-gate 1207c478bd9Sstevel@tonic-gate void 1217c478bd9Sstevel@tonic-gate sysstat_init() 1227c478bd9Sstevel@tonic-gate { 1237c478bd9Sstevel@tonic-gate info->pd_ttychars_sum = 0; 1247c478bd9Sstevel@tonic-gate info->pd_loadaverage = 0; 1257c478bd9Sstevel@tonic-gate info->pd_diskreads_sum = 0; 1267c478bd9Sstevel@tonic-gate info->pd_nfsreqs_sum = 0; 1277c478bd9Sstevel@tonic-gate 1287c478bd9Sstevel@tonic-gate if ((kc = kstat_open()) == NULL) { 1297c478bd9Sstevel@tonic-gate fail("kstat_open(): can't open /dev/kstat"); 1307c478bd9Sstevel@tonic-gate } 1317c478bd9Sstevel@tonic-gate init_all(); 1327c478bd9Sstevel@tonic-gate } 1337c478bd9Sstevel@tonic-gate 1347c478bd9Sstevel@tonic-gate static void 1357c478bd9Sstevel@tonic-gate init_all(void) 1367c478bd9Sstevel@tonic-gate { 1377c478bd9Sstevel@tonic-gate char *msg = "kstat_read(): can't read kstat"; 1387c478bd9Sstevel@tonic-gate 1397c478bd9Sstevel@tonic-gate init_disks(); 1407c478bd9Sstevel@tonic-gate if (diskinfo_load() != 0) { 1417c478bd9Sstevel@tonic-gate fail(msg); 1427c478bd9Sstevel@tonic-gate } 1437c478bd9Sstevel@tonic-gate 1447c478bd9Sstevel@tonic-gate cpu_stats_init(); 1457c478bd9Sstevel@tonic-gate if (cpu_stats_load() != 0) { 1467c478bd9Sstevel@tonic-gate fail(msg); 1477c478bd9Sstevel@tonic-gate } 1487c478bd9Sstevel@tonic-gate 1497c478bd9Sstevel@tonic-gate load_ave_init(); 1507c478bd9Sstevel@tonic-gate last_load_ave_change = gethrtime(); 1517c478bd9Sstevel@tonic-gate if (load_ave_load() != 0) { 1527c478bd9Sstevel@tonic-gate fail(msg); 1537c478bd9Sstevel@tonic-gate } 1547c478bd9Sstevel@tonic-gate 1557c478bd9Sstevel@tonic-gate nfs_init(); 1567c478bd9Sstevel@tonic-gate if (nfs_load() != 0) { 1577c478bd9Sstevel@tonic-gate fail(msg); 1587c478bd9Sstevel@tonic-gate } 1597c478bd9Sstevel@tonic-gate conskbd_init(); 1607c478bd9Sstevel@tonic-gate consms_init(); 1617c478bd9Sstevel@tonic-gate } 1627c478bd9Sstevel@tonic-gate 1637c478bd9Sstevel@tonic-gate int 1647c478bd9Sstevel@tonic-gate last_disk_activity(hrtime_t *hr_now, int threshold) 1657c478bd9Sstevel@tonic-gate { 1667c478bd9Sstevel@tonic-gate return (check_activity(disk_act_start, info->pd_diskreads_sum, hr_now, 1677c478bd9Sstevel@tonic-gate threshold)); 1687c478bd9Sstevel@tonic-gate } 1697c478bd9Sstevel@tonic-gate 1707c478bd9Sstevel@tonic-gate int 1717c478bd9Sstevel@tonic-gate last_tty_activity(hrtime_t *hr_now, int threshold) 1727c478bd9Sstevel@tonic-gate { 1737c478bd9Sstevel@tonic-gate return (check_activity(tty_act_start, info->pd_ttychars_sum, hr_now, 1747c478bd9Sstevel@tonic-gate threshold)); 1757c478bd9Sstevel@tonic-gate } 1767c478bd9Sstevel@tonic-gate 1777c478bd9Sstevel@tonic-gate int 1787c478bd9Sstevel@tonic-gate last_load_ave_activity(hrtime_t *hr_now) 1797c478bd9Sstevel@tonic-gate { 1807c478bd9Sstevel@tonic-gate return ((*hr_now - last_load_ave_change) / NANOSEC); 1817c478bd9Sstevel@tonic-gate } 1827c478bd9Sstevel@tonic-gate 1837c478bd9Sstevel@tonic-gate int 1847c478bd9Sstevel@tonic-gate last_nfs_activity(hrtime_t *hr_now, int threshold) 1857c478bd9Sstevel@tonic-gate { 1867c478bd9Sstevel@tonic-gate return (check_activity(nfs_act_start, info->pd_nfsreqs_sum, hr_now, 1877c478bd9Sstevel@tonic-gate threshold)); 1887c478bd9Sstevel@tonic-gate } 1897c478bd9Sstevel@tonic-gate 1907c478bd9Sstevel@tonic-gate static void 1917c478bd9Sstevel@tonic-gate init_disks(void) 1927c478bd9Sstevel@tonic-gate { 1937c478bd9Sstevel@tonic-gate struct diskinfo *disk, *prevdisk, *comp; 1947c478bd9Sstevel@tonic-gate kstat_t *ksp; 1957c478bd9Sstevel@tonic-gate 1967c478bd9Sstevel@tonic-gate disk = &zerodisk; 1977c478bd9Sstevel@tonic-gate 1987c478bd9Sstevel@tonic-gate /* 1997c478bd9Sstevel@tonic-gate * Patch the snip in the diskinfo list (see below) 2007c478bd9Sstevel@tonic-gate */ 2017c478bd9Sstevel@tonic-gate if (snip) { 2027c478bd9Sstevel@tonic-gate lastdisk->next = snip; 2037c478bd9Sstevel@tonic-gate } 2047c478bd9Sstevel@tonic-gate 2057c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { 2067c478bd9Sstevel@tonic-gate if (ksp->ks_type != KSTAT_TYPE_IO || 2077c478bd9Sstevel@tonic-gate strcmp(ksp->ks_class, "disk") != 0) { 2087c478bd9Sstevel@tonic-gate continue; 2097c478bd9Sstevel@tonic-gate } 2107c478bd9Sstevel@tonic-gate prevdisk = disk; 2117c478bd9Sstevel@tonic-gate if (disk->next) { 2127c478bd9Sstevel@tonic-gate disk = disk->next; 2137c478bd9Sstevel@tonic-gate } else { 2147c478bd9Sstevel@tonic-gate safe_zalloc((void **)&disk->next, 2157c478bd9Sstevel@tonic-gate sizeof (struct diskinfo), 0); 2167c478bd9Sstevel@tonic-gate disk = disk->next; 2177c478bd9Sstevel@tonic-gate disk->next = NULLDISK; 2187c478bd9Sstevel@tonic-gate } 2197c478bd9Sstevel@tonic-gate disk->ks = ksp; 2207c478bd9Sstevel@tonic-gate (void *) memset((void *)&disk->new_kios, 0, 2217c478bd9Sstevel@tonic-gate sizeof (kstat_io_t)); 2227c478bd9Sstevel@tonic-gate disk->new_kios.wlastupdate = disk->ks->ks_crtime; 2237c478bd9Sstevel@tonic-gate disk->new_kios.rlastupdate = disk->ks->ks_crtime; 2247c478bd9Sstevel@tonic-gate 2257c478bd9Sstevel@tonic-gate /* 2267c478bd9Sstevel@tonic-gate * Insertion sort on (ks_module, ks_instance, ks_name) 2277c478bd9Sstevel@tonic-gate */ 2287c478bd9Sstevel@tonic-gate comp = &zerodisk; 2297c478bd9Sstevel@tonic-gate while (kscmp(disk->ks, comp->next->ks) > 0) { 2307c478bd9Sstevel@tonic-gate comp = comp->next; 2317c478bd9Sstevel@tonic-gate } 2327c478bd9Sstevel@tonic-gate if (prevdisk != comp) { 2337c478bd9Sstevel@tonic-gate prevdisk->next = disk->next; 2347c478bd9Sstevel@tonic-gate disk->next = comp->next; 2357c478bd9Sstevel@tonic-gate comp->next = disk; 2367c478bd9Sstevel@tonic-gate disk = prevdisk; 2377c478bd9Sstevel@tonic-gate } 2387c478bd9Sstevel@tonic-gate } 2397c478bd9Sstevel@tonic-gate /* 2407c478bd9Sstevel@tonic-gate * Put a snip in the linked list of diskinfos. The idea: 2417c478bd9Sstevel@tonic-gate * If there was a state change such that now there are fewer 2427c478bd9Sstevel@tonic-gate * disks, we snip the list and retain the tail, rather than 2437c478bd9Sstevel@tonic-gate * freeing it. At the next state change, we clip the tail back on. 2447c478bd9Sstevel@tonic-gate * This prevents a lot of malloc/free activity, and it's simpler. 2457c478bd9Sstevel@tonic-gate */ 2467c478bd9Sstevel@tonic-gate lastdisk = disk; 2477c478bd9Sstevel@tonic-gate snip = disk->next; 2487c478bd9Sstevel@tonic-gate disk->next = NULLDISK; 2497c478bd9Sstevel@tonic-gate 2507c478bd9Sstevel@tonic-gate firstdisk = zerodisk.next; 2517c478bd9Sstevel@tonic-gate } 2527c478bd9Sstevel@tonic-gate 2537c478bd9Sstevel@tonic-gate static int 2547c478bd9Sstevel@tonic-gate diskinfo_load(void) 2557c478bd9Sstevel@tonic-gate { 2567c478bd9Sstevel@tonic-gate struct diskinfo *disk; 2577c478bd9Sstevel@tonic-gate 2587c478bd9Sstevel@tonic-gate for (disk = firstdisk; disk; disk = disk->next) { 2597c478bd9Sstevel@tonic-gate disk->old_kios = disk->new_kios; 2607c478bd9Sstevel@tonic-gate if (kstat_read(kc, disk->ks, 2617c478bd9Sstevel@tonic-gate (void *)&disk->new_kios) == -1) { 2627c478bd9Sstevel@tonic-gate return (1); 2637c478bd9Sstevel@tonic-gate } 2647c478bd9Sstevel@tonic-gate } 2657c478bd9Sstevel@tonic-gate 2667c478bd9Sstevel@tonic-gate return (0); 2677c478bd9Sstevel@tonic-gate } 2687c478bd9Sstevel@tonic-gate 2697c478bd9Sstevel@tonic-gate int 2707c478bd9Sstevel@tonic-gate check_disks(hrtime_t *hr_now, int threshold) 2717c478bd9Sstevel@tonic-gate { 2727c478bd9Sstevel@tonic-gate struct diskinfo *disk; 2737c478bd9Sstevel@tonic-gate int delta = 0; 2747c478bd9Sstevel@tonic-gate hrtime_t time = 0; 2757c478bd9Sstevel@tonic-gate 2767c478bd9Sstevel@tonic-gate while (kstat_chain_update(kc) || diskinfo_load()) { 2777c478bd9Sstevel@tonic-gate init_all(); 2787c478bd9Sstevel@tonic-gate } 2797c478bd9Sstevel@tonic-gate for (disk = firstdisk; disk; disk = disk->next) { 2807c478bd9Sstevel@tonic-gate if (time == 0) { 2817c478bd9Sstevel@tonic-gate time = disk->new_kios.wlastupdate; 2827c478bd9Sstevel@tonic-gate } 2837c478bd9Sstevel@tonic-gate delta += DISK_DELTA(reads); 2847c478bd9Sstevel@tonic-gate if (DISK_DELTA(reads) > 0) { 2857c478bd9Sstevel@tonic-gate time = MAX(time, disk->new_kios.wlastupdate); 2867c478bd9Sstevel@tonic-gate } 2877c478bd9Sstevel@tonic-gate } 2887c478bd9Sstevel@tonic-gate keep_activity_data(&disk_act_start, &disk_act_end, 2897c478bd9Sstevel@tonic-gate &info->pd_diskreads_sum, delta, time); 2907c478bd9Sstevel@tonic-gate #ifdef DEBUG 2917c478bd9Sstevel@tonic-gate (void) printf(" Disk reads = %d\n", delta); 2927c478bd9Sstevel@tonic-gate #endif 2937c478bd9Sstevel@tonic-gate return (check_activity(disk_act_start, info->pd_diskreads_sum, hr_now, 2947c478bd9Sstevel@tonic-gate threshold)); 2957c478bd9Sstevel@tonic-gate } 2967c478bd9Sstevel@tonic-gate 2977c478bd9Sstevel@tonic-gate static void 2987c478bd9Sstevel@tonic-gate cpu_stats_init(void) 2997c478bd9Sstevel@tonic-gate { 3007c478bd9Sstevel@tonic-gate kstat_t *ksp; 3017c478bd9Sstevel@tonic-gate 3027c478bd9Sstevel@tonic-gate ncpus = 0; 3037c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { 3047c478bd9Sstevel@tonic-gate if (strcmp(ksp->ks_module, "cpu") == 0 && 3057c478bd9Sstevel@tonic-gate strcmp(ksp->ks_name, "sys") == 0) 3067c478bd9Sstevel@tonic-gate ncpus++; 3077c478bd9Sstevel@tonic-gate } 3087c478bd9Sstevel@tonic-gate 3097c478bd9Sstevel@tonic-gate safe_zalloc((void **)&cpu_stats_list, ncpus * sizeof (*cpu_stats_list), 3107c478bd9Sstevel@tonic-gate 1); 3117c478bd9Sstevel@tonic-gate 3127c478bd9Sstevel@tonic-gate ncpus = 0; 3137c478bd9Sstevel@tonic-gate for (ksp = kc->kc_chain; ksp; ksp = ksp->ks_next) { 3147c478bd9Sstevel@tonic-gate if (strcmp(ksp->ks_module, "cpu") == 0 && 3157c478bd9Sstevel@tonic-gate strcmp(ksp->ks_name, "sys") == 0 && 3167c478bd9Sstevel@tonic-gate kstat_read(kc, ksp, NULL) != -1) 3177c478bd9Sstevel@tonic-gate cpu_stats_list[ncpus++] = ksp; 3187c478bd9Sstevel@tonic-gate } 3197c478bd9Sstevel@tonic-gate 3207c478bd9Sstevel@tonic-gate if (ncpus == 0) 3217c478bd9Sstevel@tonic-gate fail("can't find any cpu statistics"); 3227c478bd9Sstevel@tonic-gate } 3237c478bd9Sstevel@tonic-gate 3247c478bd9Sstevel@tonic-gate static int 3257c478bd9Sstevel@tonic-gate cpu_stats_load(void) 3267c478bd9Sstevel@tonic-gate { 3277c478bd9Sstevel@tonic-gate int i, j; 3287c478bd9Sstevel@tonic-gate kstat_named_t *nkp, *tkp; 3297c478bd9Sstevel@tonic-gate 3307c478bd9Sstevel@tonic-gate tty_snaptime = 0; 3317c478bd9Sstevel@tonic-gate kstat_copy(&new_cpu_stats, &old_cpu_stats, 1); 3327c478bd9Sstevel@tonic-gate 3337c478bd9Sstevel@tonic-gate /* 3347c478bd9Sstevel@tonic-gate * Sum across all cpus 3357c478bd9Sstevel@tonic-gate */ 3367c478bd9Sstevel@tonic-gate for (i = 0; i < ncpus; i++) { 3377c478bd9Sstevel@tonic-gate if (kstat_read(kc, cpu_stats_list[i], NULL) == -1) 3387c478bd9Sstevel@tonic-gate return (1); 3397c478bd9Sstevel@tonic-gate 3407c478bd9Sstevel@tonic-gate if (i == 0) { 3417c478bd9Sstevel@tonic-gate kstat_copy(cpu_stats_list[i], &new_cpu_stats, 1); 3427c478bd9Sstevel@tonic-gate continue; 3437c478bd9Sstevel@tonic-gate } else { 3447c478bd9Sstevel@tonic-gate /* 3457c478bd9Sstevel@tonic-gate * Other CPUs' statistics are accumulated in 3467c478bd9Sstevel@tonic-gate * new_cpu_stats, initialized at the first iteration of 3477c478bd9Sstevel@tonic-gate * the loop. 3487c478bd9Sstevel@tonic-gate */ 3497c478bd9Sstevel@tonic-gate nkp = (kstat_named_t *)new_cpu_stats.ks_data; 3507c478bd9Sstevel@tonic-gate tkp = (kstat_named_t *)cpu_stats_list[i]->ks_data; 3517c478bd9Sstevel@tonic-gate for (j = 0; j < cpu_stats_list[i]->ks_ndata; j++) 3527c478bd9Sstevel@tonic-gate (nkp++)->value.ui64 += (tkp++)->value.ui64; 3537c478bd9Sstevel@tonic-gate tty_snaptime = MAX(tty_snaptime, 3547c478bd9Sstevel@tonic-gate cpu_stats_list[i]->ks_snaptime); 3557c478bd9Sstevel@tonic-gate } 3567c478bd9Sstevel@tonic-gate } 3577c478bd9Sstevel@tonic-gate 3587c478bd9Sstevel@tonic-gate return (0); 3597c478bd9Sstevel@tonic-gate } 3607c478bd9Sstevel@tonic-gate 3617c478bd9Sstevel@tonic-gate int 3627c478bd9Sstevel@tonic-gate check_tty(hrtime_t *hr_now, int threshold) 3637c478bd9Sstevel@tonic-gate { 3647c478bd9Sstevel@tonic-gate int delta; 3657c478bd9Sstevel@tonic-gate 3667c478bd9Sstevel@tonic-gate while (kstat_chain_update(kc) || cpu_stats_load()) { 3677c478bd9Sstevel@tonic-gate init_all(); 3687c478bd9Sstevel@tonic-gate } 3697c478bd9Sstevel@tonic-gate delta = CPU_DELTA("rawch") + CPU_DELTA("outch"); 3707c478bd9Sstevel@tonic-gate keep_activity_data(&tty_act_start, &tty_act_end, 3717c478bd9Sstevel@tonic-gate &info->pd_ttychars_sum, delta, tty_snaptime); 3727c478bd9Sstevel@tonic-gate #ifdef DEBUG 3737c478bd9Sstevel@tonic-gate (void) printf(" Tty chars = %d\n", delta); 3747c478bd9Sstevel@tonic-gate #endif 3757c478bd9Sstevel@tonic-gate return (check_activity(tty_act_start, info->pd_ttychars_sum, hr_now, 3767c478bd9Sstevel@tonic-gate threshold)); 3777c478bd9Sstevel@tonic-gate } 3787c478bd9Sstevel@tonic-gate 3797c478bd9Sstevel@tonic-gate static void 3807c478bd9Sstevel@tonic-gate load_ave_init(void) 3817c478bd9Sstevel@tonic-gate { 3827c478bd9Sstevel@tonic-gate if ((load_ave_ksp = kstat_lookup(kc, "unix", 0, "system_misc")) == 3837c478bd9Sstevel@tonic-gate NULL) { 3847c478bd9Sstevel@tonic-gate fail("kstat_lookup('unix', 0, 'system_misc') failed"); 3857c478bd9Sstevel@tonic-gate } 3867c478bd9Sstevel@tonic-gate } 3877c478bd9Sstevel@tonic-gate 3887c478bd9Sstevel@tonic-gate static int 3897c478bd9Sstevel@tonic-gate load_ave_load(void) 3907c478bd9Sstevel@tonic-gate { 3917c478bd9Sstevel@tonic-gate if (kstat_read(kc, load_ave_ksp, NULL) == -1) { 3927c478bd9Sstevel@tonic-gate return (1); 3937c478bd9Sstevel@tonic-gate } 3947c478bd9Sstevel@tonic-gate load_ave = ((kstat_named_t *)safe_kstat_data_lookup( 3957c478bd9Sstevel@tonic-gate load_ave_ksp, "avenrun_1min"))->value.l; 3967c478bd9Sstevel@tonic-gate 3977c478bd9Sstevel@tonic-gate return (0); 3987c478bd9Sstevel@tonic-gate } 3997c478bd9Sstevel@tonic-gate 4007c478bd9Sstevel@tonic-gate int 4017c478bd9Sstevel@tonic-gate check_load_ave(hrtime_t *hr_now, float threshold) 4027c478bd9Sstevel@tonic-gate { 4037c478bd9Sstevel@tonic-gate while (kstat_chain_update(kc) || load_ave_load()) { 4047c478bd9Sstevel@tonic-gate init_all(); 4057c478bd9Sstevel@tonic-gate } 4067c478bd9Sstevel@tonic-gate info->pd_loadaverage = (double)load_ave / FSCALE; 4077c478bd9Sstevel@tonic-gate if (info->pd_loadaverage > threshold) { 4087c478bd9Sstevel@tonic-gate last_load_ave_change = load_ave_ksp->ks_snaptime; 4097c478bd9Sstevel@tonic-gate } 4107c478bd9Sstevel@tonic-gate #ifdef DEBUG 4117c478bd9Sstevel@tonic-gate (void) printf(" Load average = %f\n", ((double)load_ave / FSCALE)); 4127c478bd9Sstevel@tonic-gate #endif 4137c478bd9Sstevel@tonic-gate return ((*hr_now - last_load_ave_change) / NANOSEC); 4147c478bd9Sstevel@tonic-gate } 4157c478bd9Sstevel@tonic-gate 4167c478bd9Sstevel@tonic-gate static void 4177c478bd9Sstevel@tonic-gate nfs_init(void) 4187c478bd9Sstevel@tonic-gate { 4197c478bd9Sstevel@tonic-gate nfs_client2_kstat = kstat_lookup(kc, "nfs", 0, "rfsreqcnt_v2"); 4207c478bd9Sstevel@tonic-gate nfs_client3_kstat = kstat_lookup(kc, "nfs", 0, "rfsreqcnt_v3"); 4217c478bd9Sstevel@tonic-gate nfs_server2_kstat = kstat_lookup(kc, "nfs", 0, "rfsproccnt_v2"); 4227c478bd9Sstevel@tonic-gate nfs_server3_kstat = kstat_lookup(kc, "nfs", 0, "rfsproccnt_v3"); 4237c478bd9Sstevel@tonic-gate } 4247c478bd9Sstevel@tonic-gate 4257c478bd9Sstevel@tonic-gate static int 4267c478bd9Sstevel@tonic-gate nfs_load(void) 4277c478bd9Sstevel@tonic-gate { 4287c478bd9Sstevel@tonic-gate kstat_named_t *kstat_ptr; 4297c478bd9Sstevel@tonic-gate int index; 4307c478bd9Sstevel@tonic-gate uint64_t total_calls = 0; 4317c478bd9Sstevel@tonic-gate uint64_t getattr_calls = 0; 4327c478bd9Sstevel@tonic-gate uint64_t null_calls = 0; 4337c478bd9Sstevel@tonic-gate uint64_t access_calls = 0; 4347c478bd9Sstevel@tonic-gate 4357c478bd9Sstevel@tonic-gate if (!nfs_client2_kstat && !nfs_client3_kstat && !nfs_server2_kstat && 4367c478bd9Sstevel@tonic-gate !nfs_server3_kstat) { 4377c478bd9Sstevel@tonic-gate return (0); 4387c478bd9Sstevel@tonic-gate } 4397c478bd9Sstevel@tonic-gate 4407c478bd9Sstevel@tonic-gate /* 4417c478bd9Sstevel@tonic-gate * NFS client "getattr", NFS3 client "access", and NFS server "null" 4427c478bd9Sstevel@tonic-gate * requests are excluded from consideration. 4437c478bd9Sstevel@tonic-gate */ 4447c478bd9Sstevel@tonic-gate if (nfs_client2_kstat) { 4457c478bd9Sstevel@tonic-gate if (kstat_read(kc, nfs_client2_kstat, NULL) == -1) { 4467c478bd9Sstevel@tonic-gate return (1); 4477c478bd9Sstevel@tonic-gate } 4487c478bd9Sstevel@tonic-gate kstat_ptr = KSTAT_NAMED_PTR(nfs_client2_kstat); 4497c478bd9Sstevel@tonic-gate for (index = 0; index < nfs_client2_kstat->ks_ndata; index++) { 4507c478bd9Sstevel@tonic-gate total_calls += kstat_ptr[index].value.ui64; 4517c478bd9Sstevel@tonic-gate } 4527c478bd9Sstevel@tonic-gate getattr_calls = 4537c478bd9Sstevel@tonic-gate ((kstat_named_t *)safe_kstat_data_lookup( 4547c478bd9Sstevel@tonic-gate nfs_client2_kstat, "getattr"))->value.ui64; 4557c478bd9Sstevel@tonic-gate } 4567c478bd9Sstevel@tonic-gate 4577c478bd9Sstevel@tonic-gate if (nfs_client3_kstat) { 4587c478bd9Sstevel@tonic-gate if (kstat_read(kc, nfs_client3_kstat, NULL) == -1) { 4597c478bd9Sstevel@tonic-gate return (1); 4607c478bd9Sstevel@tonic-gate } 4617c478bd9Sstevel@tonic-gate kstat_ptr = KSTAT_NAMED_PTR(nfs_client3_kstat); 4627c478bd9Sstevel@tonic-gate for (index = 0; index < nfs_client3_kstat->ks_ndata; index++) { 4637c478bd9Sstevel@tonic-gate total_calls += kstat_ptr[index].value.ui64; 4647c478bd9Sstevel@tonic-gate } 4657c478bd9Sstevel@tonic-gate getattr_calls += 4667c478bd9Sstevel@tonic-gate ((kstat_named_t *)safe_kstat_data_lookup( 4677c478bd9Sstevel@tonic-gate nfs_client3_kstat, "getattr"))->value.ui64; 4687c478bd9Sstevel@tonic-gate access_calls = 4697c478bd9Sstevel@tonic-gate ((kstat_named_t *)safe_kstat_data_lookup( 4707c478bd9Sstevel@tonic-gate nfs_client3_kstat, "access"))->value.ui64; 4717c478bd9Sstevel@tonic-gate } 4727c478bd9Sstevel@tonic-gate 4737c478bd9Sstevel@tonic-gate if (nfs_server2_kstat) { 4747c478bd9Sstevel@tonic-gate if (kstat_read(kc, nfs_server2_kstat, NULL) == -1) { 4757c478bd9Sstevel@tonic-gate return (1); 4767c478bd9Sstevel@tonic-gate } 4777c478bd9Sstevel@tonic-gate kstat_ptr = KSTAT_NAMED_PTR(nfs_server2_kstat); 4787c478bd9Sstevel@tonic-gate for (index = 0; index < nfs_server2_kstat->ks_ndata; index++) { 4797c478bd9Sstevel@tonic-gate total_calls += kstat_ptr[index].value.ui64; 4807c478bd9Sstevel@tonic-gate } 4817c478bd9Sstevel@tonic-gate null_calls = 4827c478bd9Sstevel@tonic-gate ((kstat_named_t *)safe_kstat_data_lookup( 4837c478bd9Sstevel@tonic-gate nfs_server2_kstat, "null"))->value.ui64; 4847c478bd9Sstevel@tonic-gate } 4857c478bd9Sstevel@tonic-gate 4867c478bd9Sstevel@tonic-gate if (nfs_server3_kstat) { 4877c478bd9Sstevel@tonic-gate if (kstat_read(kc, nfs_server3_kstat, NULL) == -1) { 4887c478bd9Sstevel@tonic-gate return (1); 4897c478bd9Sstevel@tonic-gate } 4907c478bd9Sstevel@tonic-gate kstat_ptr = KSTAT_NAMED_PTR(nfs_server3_kstat); 4917c478bd9Sstevel@tonic-gate for (index = 0; index < nfs_server3_kstat->ks_ndata; index++) { 4927c478bd9Sstevel@tonic-gate total_calls += kstat_ptr[index].value.ui64; 4937c478bd9Sstevel@tonic-gate } 4947c478bd9Sstevel@tonic-gate null_calls += 4957c478bd9Sstevel@tonic-gate ((kstat_named_t *)safe_kstat_data_lookup( 4967c478bd9Sstevel@tonic-gate nfs_server3_kstat, "null"))->value.ui64; 4977c478bd9Sstevel@tonic-gate } 4987c478bd9Sstevel@tonic-gate 4997c478bd9Sstevel@tonic-gate old_nfs_calls = new_nfs_calls; 5007c478bd9Sstevel@tonic-gate new_nfs_calls = total_calls - 5017c478bd9Sstevel@tonic-gate (getattr_calls + access_calls + null_calls); 5027c478bd9Sstevel@tonic-gate 5037c478bd9Sstevel@tonic-gate return (0); 5047c478bd9Sstevel@tonic-gate } 5057c478bd9Sstevel@tonic-gate 5067c478bd9Sstevel@tonic-gate int 5077c478bd9Sstevel@tonic-gate check_nfs(hrtime_t *hr_now, int threshold) 5087c478bd9Sstevel@tonic-gate { 5097c478bd9Sstevel@tonic-gate int delta; 5107c478bd9Sstevel@tonic-gate hrtime_t time = 0; 5117c478bd9Sstevel@tonic-gate 5127c478bd9Sstevel@tonic-gate while (kstat_chain_update(kc) || nfs_load()) { 5137c478bd9Sstevel@tonic-gate init_all(); 5147c478bd9Sstevel@tonic-gate } 5157c478bd9Sstevel@tonic-gate 5167c478bd9Sstevel@tonic-gate if (!nfs_client2_kstat && !nfs_client3_kstat && !nfs_server2_kstat && 5177c478bd9Sstevel@tonic-gate !nfs_server3_kstat) { 5187c478bd9Sstevel@tonic-gate return (0); 5197c478bd9Sstevel@tonic-gate } 5207c478bd9Sstevel@tonic-gate 5217c478bd9Sstevel@tonic-gate if (nfs_client2_kstat) { 5227c478bd9Sstevel@tonic-gate time = MAX(time, nfs_client2_kstat->ks_snaptime); 5237c478bd9Sstevel@tonic-gate } 5247c478bd9Sstevel@tonic-gate if (nfs_client3_kstat) { 5257c478bd9Sstevel@tonic-gate time = MAX(time, nfs_client3_kstat->ks_snaptime); 5267c478bd9Sstevel@tonic-gate } 5277c478bd9Sstevel@tonic-gate if (nfs_server2_kstat) { 5287c478bd9Sstevel@tonic-gate time = MAX(time, nfs_server2_kstat->ks_snaptime); 5297c478bd9Sstevel@tonic-gate } 5307c478bd9Sstevel@tonic-gate if (nfs_server3_kstat) { 5317c478bd9Sstevel@tonic-gate time = MAX(time, nfs_server3_kstat->ks_snaptime); 5327c478bd9Sstevel@tonic-gate } 5337c478bd9Sstevel@tonic-gate delta = (int)(new_nfs_calls - old_nfs_calls); 5347c478bd9Sstevel@tonic-gate keep_activity_data(&nfs_act_start, &nfs_act_end, 5357c478bd9Sstevel@tonic-gate &info->pd_nfsreqs_sum, delta, time); 5367c478bd9Sstevel@tonic-gate #ifdef DEBUG 5377c478bd9Sstevel@tonic-gate (void) printf(" NFS requests = %d\n", delta); 5387c478bd9Sstevel@tonic-gate #endif 5397c478bd9Sstevel@tonic-gate return (check_activity(nfs_act_start, info->pd_nfsreqs_sum, hr_now, 5407c478bd9Sstevel@tonic-gate threshold)); 5417c478bd9Sstevel@tonic-gate } 5427c478bd9Sstevel@tonic-gate 5437c478bd9Sstevel@tonic-gate static void 5447c478bd9Sstevel@tonic-gate conskbd_init(void) 5457c478bd9Sstevel@tonic-gate { 5467c478bd9Sstevel@tonic-gate conskbd_ksp = kstat_lookup(kc, "conskbd", 0, "activity"); 5477c478bd9Sstevel@tonic-gate } 5487c478bd9Sstevel@tonic-gate 5497c478bd9Sstevel@tonic-gate /* 5507c478bd9Sstevel@tonic-gate * Return the number of seconds since the last keystroke on console keyboard. 5517c478bd9Sstevel@tonic-gate * Caller responsible for error reporting. 5527c478bd9Sstevel@tonic-gate */ 5537c478bd9Sstevel@tonic-gate long 5547c478bd9Sstevel@tonic-gate conskbd_idle_time(void) 5557c478bd9Sstevel@tonic-gate { 5567c478bd9Sstevel@tonic-gate void *p; 5577c478bd9Sstevel@tonic-gate 5587c478bd9Sstevel@tonic-gate if (conskbd_ksp == NULL || kstat_read(kc, conskbd_ksp, NULL) == -1 || 5597c478bd9Sstevel@tonic-gate (p = kstat_data_lookup(conskbd_ksp, "idle_sec")) == NULL) 5607c478bd9Sstevel@tonic-gate return ((time_t)-1); 5617c478bd9Sstevel@tonic-gate 5627c478bd9Sstevel@tonic-gate return (((kstat_named_t *)p)->value.l); 5637c478bd9Sstevel@tonic-gate } 5647c478bd9Sstevel@tonic-gate 5657c478bd9Sstevel@tonic-gate static void 5667c478bd9Sstevel@tonic-gate consms_init(void) 5677c478bd9Sstevel@tonic-gate { 5687c478bd9Sstevel@tonic-gate consms_ksp = kstat_lookup(kc, "consms", 0, "activity"); 5697c478bd9Sstevel@tonic-gate } 5707c478bd9Sstevel@tonic-gate 5717c478bd9Sstevel@tonic-gate /* 5727c478bd9Sstevel@tonic-gate * Return the number of seconds since the most recent action (movement or 5737c478bd9Sstevel@tonic-gate * click) of the console mouse. Caller responsible for error reporting. 5747c478bd9Sstevel@tonic-gate */ 5757c478bd9Sstevel@tonic-gate long 5767c478bd9Sstevel@tonic-gate consms_idle_time(void) 5777c478bd9Sstevel@tonic-gate { 5787c478bd9Sstevel@tonic-gate void *p; 5797c478bd9Sstevel@tonic-gate 5807c478bd9Sstevel@tonic-gate if (consms_ksp == NULL || kstat_read(kc, consms_ksp, NULL) == -1 || 5817c478bd9Sstevel@tonic-gate (p = kstat_data_lookup(consms_ksp, "idle_sec")) == NULL) 5827c478bd9Sstevel@tonic-gate return ((time_t)-1); 5837c478bd9Sstevel@tonic-gate 5847c478bd9Sstevel@tonic-gate return (((kstat_named_t *)p)->value.l); 5857c478bd9Sstevel@tonic-gate } 5867c478bd9Sstevel@tonic-gate 5877c478bd9Sstevel@tonic-gate static void 5887c478bd9Sstevel@tonic-gate fail(char *fmt, ...) 5897c478bd9Sstevel@tonic-gate { 5907c478bd9Sstevel@tonic-gate char new_fmt[256]; 591*c42872d4Smh27603 const char *fmtptr = new_fmt; 5927c478bd9Sstevel@tonic-gate va_list args; 5937c478bd9Sstevel@tonic-gate size_t len; 5947c478bd9Sstevel@tonic-gate 5957c478bd9Sstevel@tonic-gate len = sizeof (new_fmt); 5967c478bd9Sstevel@tonic-gate va_start(args, fmt); 5977c478bd9Sstevel@tonic-gate if (snprintf(new_fmt, len, "powerd: %s", fmt) > len) 5987c478bd9Sstevel@tonic-gate syslog(LOG_ERR, "powerd: syslog message too large"); 5997c478bd9Sstevel@tonic-gate else 600*c42872d4Smh27603 vsyslog(LOG_ERR, fmtptr, args); 6017c478bd9Sstevel@tonic-gate va_end(args); 6027c478bd9Sstevel@tonic-gate 6037c478bd9Sstevel@tonic-gate thr_exit((void *) 0); 6047c478bd9Sstevel@tonic-gate } 6057c478bd9Sstevel@tonic-gate 6067c478bd9Sstevel@tonic-gate static void 6077c478bd9Sstevel@tonic-gate safe_zalloc(void **ptr, int size, int free_first) 6087c478bd9Sstevel@tonic-gate { 6097c478bd9Sstevel@tonic-gate if (free_first && *ptr != NULL) { 6107c478bd9Sstevel@tonic-gate free(*ptr); 6117c478bd9Sstevel@tonic-gate } 6127c478bd9Sstevel@tonic-gate if ((*ptr = (void *) malloc(size)) == NULL) { 6137c478bd9Sstevel@tonic-gate fail("malloc failed"); 6147c478bd9Sstevel@tonic-gate } 6157c478bd9Sstevel@tonic-gate (void *) memset(*ptr, 0, size); 6167c478bd9Sstevel@tonic-gate } 6177c478bd9Sstevel@tonic-gate 6187c478bd9Sstevel@tonic-gate static void * 6197c478bd9Sstevel@tonic-gate safe_kstat_data_lookup(kstat_t *ksp, char *name) 6207c478bd9Sstevel@tonic-gate { 6217c478bd9Sstevel@tonic-gate void *fp = kstat_data_lookup(ksp, name); 6227c478bd9Sstevel@tonic-gate 6237c478bd9Sstevel@tonic-gate if (fp == NULL) { 6247c478bd9Sstevel@tonic-gate fail("kstat_data_lookup('%s', '%s') failed", 6257c478bd9Sstevel@tonic-gate ksp->ks_name, name); 6267c478bd9Sstevel@tonic-gate } 6277c478bd9Sstevel@tonic-gate return (fp); 6287c478bd9Sstevel@tonic-gate } 6297c478bd9Sstevel@tonic-gate 6307c478bd9Sstevel@tonic-gate static int 6317c478bd9Sstevel@tonic-gate kscmp(kstat_t *ks1, kstat_t *ks2) 6327c478bd9Sstevel@tonic-gate { 6337c478bd9Sstevel@tonic-gate int cmp; 6347c478bd9Sstevel@tonic-gate 6357c478bd9Sstevel@tonic-gate cmp = strcmp(ks1->ks_module, ks2->ks_module); 6367c478bd9Sstevel@tonic-gate if (cmp != 0) { 6377c478bd9Sstevel@tonic-gate return (cmp); 6387c478bd9Sstevel@tonic-gate } 6397c478bd9Sstevel@tonic-gate cmp = ks1->ks_instance - ks2->ks_instance; 6407c478bd9Sstevel@tonic-gate if (cmp != 0) { 6417c478bd9Sstevel@tonic-gate return (cmp); 6427c478bd9Sstevel@tonic-gate } 6437c478bd9Sstevel@tonic-gate return (strcmp(ks1->ks_name, ks2->ks_name)); 6447c478bd9Sstevel@tonic-gate } 6457c478bd9Sstevel@tonic-gate 6467c478bd9Sstevel@tonic-gate static void 6477c478bd9Sstevel@tonic-gate keep_activity_data(activity_data_t **act_start, activity_data_t **act_end, 6487c478bd9Sstevel@tonic-gate int *delta_sum, int delta, hrtime_t time) 6497c478bd9Sstevel@tonic-gate { 6507c478bd9Sstevel@tonic-gate activity_data_t *node = NULLACTIVITY; 6517c478bd9Sstevel@tonic-gate hrtime_t hr_now; 6527c478bd9Sstevel@tonic-gate int idle_time = info->pd_idle_time * 60; 6537c478bd9Sstevel@tonic-gate 6547c478bd9Sstevel@tonic-gate /* 6557c478bd9Sstevel@tonic-gate * Add new nodes to the beginning of the list. 6567c478bd9Sstevel@tonic-gate */ 6577c478bd9Sstevel@tonic-gate safe_zalloc((void **)&node, sizeof (activity_data_t), 0); 6587c478bd9Sstevel@tonic-gate node->activity_delta = delta; 6597c478bd9Sstevel@tonic-gate *delta_sum += delta; 6607c478bd9Sstevel@tonic-gate node->snaptime = time; 6617c478bd9Sstevel@tonic-gate node->next = *act_start; 6627c478bd9Sstevel@tonic-gate if (*act_start == NULLACTIVITY) { 6637c478bd9Sstevel@tonic-gate *act_end = node; 6647c478bd9Sstevel@tonic-gate } else { 6657c478bd9Sstevel@tonic-gate (*act_start)->prev = node; 6667c478bd9Sstevel@tonic-gate } 6677c478bd9Sstevel@tonic-gate *act_start = node; 6687c478bd9Sstevel@tonic-gate 6697c478bd9Sstevel@tonic-gate /* 6707c478bd9Sstevel@tonic-gate * Remove nodes that are time-stamped later than the idle time. 6717c478bd9Sstevel@tonic-gate */ 6727c478bd9Sstevel@tonic-gate hr_now = gethrtime(); 6737c478bd9Sstevel@tonic-gate node = *act_end; 6747c478bd9Sstevel@tonic-gate while ((int)((hr_now - node->snaptime) / NANOSEC) > idle_time && 6757c478bd9Sstevel@tonic-gate node->prev != NULLACTIVITY) { 6767c478bd9Sstevel@tonic-gate *delta_sum -= node->activity_delta; 6777c478bd9Sstevel@tonic-gate *act_end = node->prev; 6787c478bd9Sstevel@tonic-gate (*act_end)->next = NULLACTIVITY; 6797c478bd9Sstevel@tonic-gate free(node); 6807c478bd9Sstevel@tonic-gate node = *act_end; 6817c478bd9Sstevel@tonic-gate } 6827c478bd9Sstevel@tonic-gate } 6837c478bd9Sstevel@tonic-gate 6847c478bd9Sstevel@tonic-gate static int 6857c478bd9Sstevel@tonic-gate check_activity(activity_data_t *act_start, int delta_sum, hrtime_t *time, 6867c478bd9Sstevel@tonic-gate int thold) 6877c478bd9Sstevel@tonic-gate { 6887c478bd9Sstevel@tonic-gate activity_data_t *node; 6897c478bd9Sstevel@tonic-gate int sum = 0; 6907c478bd9Sstevel@tonic-gate int idle_time = info->pd_idle_time * 60; 6917c478bd9Sstevel@tonic-gate 6927c478bd9Sstevel@tonic-gate /* 6937c478bd9Sstevel@tonic-gate * No need to walk the list if the sum of the deltas are not greater 6947c478bd9Sstevel@tonic-gate * than the threshold value. 6957c478bd9Sstevel@tonic-gate */ 6967c478bd9Sstevel@tonic-gate if (delta_sum <= thold) { 6977c478bd9Sstevel@tonic-gate return (idle_time); 6987c478bd9Sstevel@tonic-gate } 6997c478bd9Sstevel@tonic-gate 7007c478bd9Sstevel@tonic-gate /* 7017c478bd9Sstevel@tonic-gate * Walk through the list and add up the activity deltas. When the 7027c478bd9Sstevel@tonic-gate * sum is greater than the threshold value, difference of current 7037c478bd9Sstevel@tonic-gate * time and the snaptime of that node will give us the idle time. 7047c478bd9Sstevel@tonic-gate */ 7057c478bd9Sstevel@tonic-gate node = act_start; 7067c478bd9Sstevel@tonic-gate while (node->next != NULLACTIVITY) { 7077c478bd9Sstevel@tonic-gate sum += node->activity_delta; 7087c478bd9Sstevel@tonic-gate if (sum > thold) { 7097c478bd9Sstevel@tonic-gate return ((*time - node->snaptime) / NANOSEC); 7107c478bd9Sstevel@tonic-gate } 7117c478bd9Sstevel@tonic-gate node = node->next; 7127c478bd9Sstevel@tonic-gate } 7137c478bd9Sstevel@tonic-gate sum += node->activity_delta; 7147c478bd9Sstevel@tonic-gate if (sum > thold) { 7157c478bd9Sstevel@tonic-gate return ((*time - node->snaptime) / NANOSEC); 7167c478bd9Sstevel@tonic-gate } 7177c478bd9Sstevel@tonic-gate 7187c478bd9Sstevel@tonic-gate return (idle_time); 7197c478bd9Sstevel@tonic-gate } 7207c478bd9Sstevel@tonic-gate 7217c478bd9Sstevel@tonic-gate static void 7227c478bd9Sstevel@tonic-gate kstat_copy(kstat_t *src, kstat_t *dst, int fr) 7237c478bd9Sstevel@tonic-gate { 7247c478bd9Sstevel@tonic-gate if (fr) 7257c478bd9Sstevel@tonic-gate free(dst->ks_data); 7267c478bd9Sstevel@tonic-gate 7277c478bd9Sstevel@tonic-gate *dst = *src; 7287c478bd9Sstevel@tonic-gate if (src->ks_data != NULL) { 7297c478bd9Sstevel@tonic-gate safe_zalloc(&dst->ks_data, src->ks_data_size, 0); 7307c478bd9Sstevel@tonic-gate (void) memcpy(dst->ks_data, src->ks_data, src->ks_data_size); 7317c478bd9Sstevel@tonic-gate } else { 7327c478bd9Sstevel@tonic-gate dst->ks_data = NULL; 7337c478bd9Sstevel@tonic-gate dst->ks_data_size = 0; 7347c478bd9Sstevel@tonic-gate } 7357c478bd9Sstevel@tonic-gate } 736