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