xref: /onnv-gate/usr/src/uts/common/cpr/cpr_stat.c (revision 5295:a21f2449e5f9)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
53446Smrj  * Common Development and Distribution License (the "License").
63446Smrj  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
210Sstevel@tonic-gate /*
223446Smrj  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
233446Smrj  * Use is subject to license terms.
240Sstevel@tonic-gate  */
250Sstevel@tonic-gate 
260Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
270Sstevel@tonic-gate 
280Sstevel@tonic-gate #include <sys/types.h>
290Sstevel@tonic-gate #include <sys/ddi.h>
300Sstevel@tonic-gate #include <sys/pte.h>
310Sstevel@tonic-gate #include <sys/cpr.h>
320Sstevel@tonic-gate 
330Sstevel@tonic-gate /*
340Sstevel@tonic-gate  * Support routines for CPR statistic collection
350Sstevel@tonic-gate  */
360Sstevel@tonic-gate struct cpr_event cpr_events_buf[CPR_E_MAX_EVENTNUM];
370Sstevel@tonic-gate 
380Sstevel@tonic-gate extern struct cpr_terminator cpr_term;
390Sstevel@tonic-gate 
400Sstevel@tonic-gate struct cpr_event *cpr_find_event(char *name, int new);
410Sstevel@tonic-gate 
420Sstevel@tonic-gate #define	CPR_DEFAULT_PROMTIME		30
430Sstevel@tonic-gate #define	CE_START_MASK			0x8000000
440Sstevel@tonic-gate 
450Sstevel@tonic-gate /*
460Sstevel@tonic-gate  * Use ctp to specify another time point instead of the current time;
470Sstevel@tonic-gate  * Otherwise, ctp is NULL.
480Sstevel@tonic-gate  */
490Sstevel@tonic-gate void
cpr_stat_event_start(char * name,cpr_time_t * ctp)500Sstevel@tonic-gate cpr_stat_event_start(char *name, cpr_time_t *ctp)
510Sstevel@tonic-gate {
520Sstevel@tonic-gate 	struct cpr_event *cep;
530Sstevel@tonic-gate 	cpr_time_t tv;
540Sstevel@tonic-gate 
550Sstevel@tonic-gate 	if (ctp)
560Sstevel@tonic-gate 		tv = *ctp;
570Sstevel@tonic-gate 	else {
580Sstevel@tonic-gate 		/* need relative time even when hrestime is stoped */
590Sstevel@tonic-gate 		cpr_tod_get(&tv);
600Sstevel@tonic-gate 	}
610Sstevel@tonic-gate 
620Sstevel@tonic-gate 	if ((cep = cpr_find_event(name, 1)) == NULL) {
630Sstevel@tonic-gate 		cpr_err(CE_WARN, "cpr_stat: run out of event buffers");
640Sstevel@tonic-gate 		return;
650Sstevel@tonic-gate 	}
660Sstevel@tonic-gate 	/*
670Sstevel@tonic-gate 	 * disallow entering start twice without calling end first
680Sstevel@tonic-gate 	 */
690Sstevel@tonic-gate 	if (cep->ce_ntests & CE_START_MASK)
700Sstevel@tonic-gate 		return;
710Sstevel@tonic-gate 
720Sstevel@tonic-gate 	cep->ce_ntests |= CE_START_MASK;
730Sstevel@tonic-gate 	cep->ce_sec.stime = cep->ce_sec.etime = tv.tv_sec;
740Sstevel@tonic-gate 	cep->ce_sec.ltime = cep->ce_sec.ltime = 0;
750Sstevel@tonic-gate 	cep->ce_msec.stime = cep->ce_msec.etime = tv.tv_nsec / 100000000;
760Sstevel@tonic-gate 	cep->ce_msec.ltime = cep->ce_msec.ltime = 0;
770Sstevel@tonic-gate }
780Sstevel@tonic-gate 
790Sstevel@tonic-gate void
cpr_stat_event_end(char * name,cpr_time_t * ctp)800Sstevel@tonic-gate cpr_stat_event_end(char *name, cpr_time_t *ctp)
810Sstevel@tonic-gate {
820Sstevel@tonic-gate 	struct cpr_stat *cp = STAT;
830Sstevel@tonic-gate 	struct cpr_event *cep;
840Sstevel@tonic-gate 	cpr_time_t tv;
850Sstevel@tonic-gate 
860Sstevel@tonic-gate 	if (ctp)
870Sstevel@tonic-gate 		tv = *ctp;
880Sstevel@tonic-gate 	else
890Sstevel@tonic-gate 		cpr_tod_get(&tv);
900Sstevel@tonic-gate 
910Sstevel@tonic-gate 	if ((cep = cpr_find_event(name, 0)) == NULL) {
920Sstevel@tonic-gate #ifdef CPR_STAT
933446Smrj 		prom_printf("cpr_stat: event \"%s\" is not monitored\n", name);
940Sstevel@tonic-gate #endif /* CPR_STAT */
950Sstevel@tonic-gate 		return;
960Sstevel@tonic-gate 	}
970Sstevel@tonic-gate 
980Sstevel@tonic-gate 	/*
990Sstevel@tonic-gate 	 * diallow entering end twice without calling end first
1000Sstevel@tonic-gate 	 */
1010Sstevel@tonic-gate 	if (!(cep->ce_ntests & CE_START_MASK))
1020Sstevel@tonic-gate 		return;
1030Sstevel@tonic-gate 
1040Sstevel@tonic-gate 	cep->ce_ntests &= ~CE_START_MASK;
1050Sstevel@tonic-gate 	cep->ce_ntests++;
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * calculate seconds
1090Sstevel@tonic-gate 	 */
1100Sstevel@tonic-gate 	cep->ce_sec.etime = tv.tv_sec;
1110Sstevel@tonic-gate 	cep->ce_sec.ltime = cep->ce_sec.etime - cep->ce_sec.stime;
1120Sstevel@tonic-gate 	cep->ce_sec.mtime = ((cep->ce_sec.mtime * (cep->ce_ntests - 1)) +
113*5295Srandyf 	    cep->ce_sec.ltime) / cep->ce_ntests;
1140Sstevel@tonic-gate 
1150Sstevel@tonic-gate 	/*
1160Sstevel@tonic-gate 	 * calculate 100*milliseconds
1170Sstevel@tonic-gate 	 */
1180Sstevel@tonic-gate 	if (cep->ce_sec.ltime == 0) {
1190Sstevel@tonic-gate 		cep->ce_msec.etime = tv.tv_nsec / 100000000;
1200Sstevel@tonic-gate 		cep->ce_msec.ltime =
1210Sstevel@tonic-gate 		    (cep->ce_msec.etime <= cep->ce_msec.stime) ? 0 :
1220Sstevel@tonic-gate 		    (cep->ce_msec.etime - cep->ce_msec.stime);
1230Sstevel@tonic-gate 		cep->ce_msec.mtime =
1240Sstevel@tonic-gate 		    ((cep->ce_msec.mtime * (cep->ce_ntests - 1)) +
1250Sstevel@tonic-gate 		    cep->ce_msec.ltime) / cep->ce_ntests;
1260Sstevel@tonic-gate 	}
1270Sstevel@tonic-gate 	cp->cs_ntests = cep->ce_ntests & ~CE_START_MASK;
1280Sstevel@tonic-gate }
1290Sstevel@tonic-gate 
1300Sstevel@tonic-gate void
cpr_stat_cleanup()1310Sstevel@tonic-gate cpr_stat_cleanup()
1320Sstevel@tonic-gate {
1330Sstevel@tonic-gate 	struct cpr_stat *cp = STAT;
1340Sstevel@tonic-gate 	struct cpr_event *cep;
1350Sstevel@tonic-gate 
1360Sstevel@tonic-gate 	for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
1370Sstevel@tonic-gate 		if ((cep->ce_ntests & CE_START_MASK) &&
1380Sstevel@tonic-gate 		    strcmp(cep->ce_name, "POST CPR DELAY") != NULL) {
1390Sstevel@tonic-gate 			cpr_stat_event_end(cep->ce_name, 0);
1400Sstevel@tonic-gate 			cep->ce_ntests &= ~CE_START_MASK;
1410Sstevel@tonic-gate 		}
1420Sstevel@tonic-gate 	}
1430Sstevel@tonic-gate }
1440Sstevel@tonic-gate 
1450Sstevel@tonic-gate void
cpr_stat_init()1460Sstevel@tonic-gate cpr_stat_init()
1470Sstevel@tonic-gate {
1480Sstevel@tonic-gate 	STAT->cs_real_statefsz = 0;
1490Sstevel@tonic-gate 	STAT->cs_dumped_statefsz = 0;
1500Sstevel@tonic-gate }
1510Sstevel@tonic-gate 
1520Sstevel@tonic-gate void
cpr_stat_record_events()1530Sstevel@tonic-gate cpr_stat_record_events()
1540Sstevel@tonic-gate {
1550Sstevel@tonic-gate 	if (cpr_term.real_statef_size) {
1560Sstevel@tonic-gate 		int cur_comprate;
1570Sstevel@tonic-gate 
1580Sstevel@tonic-gate 		STAT->cs_real_statefsz = cpr_term.real_statef_size;
1590Sstevel@tonic-gate 		cur_comprate = ((longlong_t)((longlong_t)
160*5295Srandyf 		    STAT->cs_nocomp_statefsz*100)/
161*5295Srandyf 		    STAT->cs_real_statefsz);
1620Sstevel@tonic-gate 		if (STAT->cs_min_comprate == 0 ||
163*5295Srandyf 		    (STAT->cs_min_comprate > cur_comprate))
1640Sstevel@tonic-gate 			STAT->cs_min_comprate = cur_comprate;
1650Sstevel@tonic-gate 	}
1660Sstevel@tonic-gate }
1670Sstevel@tonic-gate 
1680Sstevel@tonic-gate void
cpr_stat_event_print()1690Sstevel@tonic-gate cpr_stat_event_print()
1700Sstevel@tonic-gate {
1710Sstevel@tonic-gate 	struct cpr_stat *cp = STAT;
1720Sstevel@tonic-gate 	struct cpr_event *cep;
1730Sstevel@tonic-gate 	char *fmt, *tabs;
1740Sstevel@tonic-gate 	int len;
1750Sstevel@tonic-gate 
1760Sstevel@tonic-gate 	printf("\n");
1770Sstevel@tonic-gate 	printf("---------------\t\tCPR PERFORMANCE SUMMARY\t\t-------------\n");
1780Sstevel@tonic-gate 	printf("Events\t\t\tRepeat[times]\tMeantime[sec]\tLastEvnt[sec]\n");
1790Sstevel@tonic-gate 
1800Sstevel@tonic-gate 	for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
1810Sstevel@tonic-gate 		len = strlen(cep->ce_name);
1820Sstevel@tonic-gate 		if (len < 8)
1830Sstevel@tonic-gate 			tabs = "\t\t\t";
1840Sstevel@tonic-gate 		else if (len < 16)
1850Sstevel@tonic-gate 			tabs = "\t\t";
1860Sstevel@tonic-gate 		else
1870Sstevel@tonic-gate 			tabs = "\t";
1880Sstevel@tonic-gate 		if (strcmp(cep->ce_name, "Suspend Total") == NULL ||
1890Sstevel@tonic-gate 		    strcmp(cep->ce_name, "Resume Total") == NULL ||
1900Sstevel@tonic-gate 		    strcmp(cep->ce_name, "POST CPR DELAY") == NULL ||
1910Sstevel@tonic-gate 		    strcmp(cep->ce_name, "WHOLE CYCLE") == NULL)
1920Sstevel@tonic-gate 			fmt = "%s%s%d\t\t%3d.%1d\t\t%3d.%1d\n";
1930Sstevel@tonic-gate 		else
1940Sstevel@tonic-gate 			fmt = "%s%s%d\t\t  %3d.%1d\t\t  %3d.%1d\n";
1950Sstevel@tonic-gate 		printf(fmt, cep->ce_name, tabs, (int)cep->ce_ntests,
1960Sstevel@tonic-gate 		    (int)cep->ce_sec.mtime, (int)(cep->ce_msec.mtime / 10),
1970Sstevel@tonic-gate 		    (int)cep->ce_sec.ltime, (int)(cep->ce_msec.ltime / 10));
1980Sstevel@tonic-gate 	}
1990Sstevel@tonic-gate 	delay(drv_usectohz(10000)); /* otherwise the next line goes to prom */
2000Sstevel@tonic-gate 	/*
2010Sstevel@tonic-gate 	 * print the rest of the stat data
2020Sstevel@tonic-gate 	 */
2030Sstevel@tonic-gate 	printf("\nMISCELLANEOUS STATISTICS INFORMATION (units in KBytes)\n\n");
2040Sstevel@tonic-gate 	printf("\tUser Pages w/o Swapspace:\t%8lu (%lu pages)\n",
205*5295Srandyf 	    cp->cs_nosw_pages*PAGESIZE/1000, cp->cs_nosw_pages);
2060Sstevel@tonic-gate 	printf("\tTotal Upages Saved to Statefile:%8d (%d pages)\n",
207*5295Srandyf 	    cp->cs_upage2statef*PAGESIZE/1000, cp->cs_upage2statef);
2080Sstevel@tonic-gate 	if (cp->cs_mclustsz)
2090Sstevel@tonic-gate 		printf("\tAverage Cluster Size:\t\t%8d (%d.%1d%1d pages)\n\n",
210*5295Srandyf 		    cp->cs_mclustsz/1000, cp->cs_mclustsz/PAGESIZE,
211*5295Srandyf 		    ((cp->cs_mclustsz%PAGESIZE)*10/PAGESIZE),
212*5295Srandyf 		    ((cp->cs_mclustsz%PAGESIZE)*100/PAGESIZE)%10);
2130Sstevel@tonic-gate 	printf("\tKernel Memory Size:\t\t%8lu\n", cp->cs_nocomp_statefsz/1000);
2140Sstevel@tonic-gate 	printf("\tEstimated Statefile Size:\t%8lu\n", cp->cs_est_statefsz/1000);
2150Sstevel@tonic-gate 	printf("\tActual Statefile Size:\t\t%8lu\n", cp->cs_real_statefsz/1000);
2160Sstevel@tonic-gate 	if (cp->cs_real_statefsz) {
2170Sstevel@tonic-gate 		int min = cp->cs_min_comprate;
2180Sstevel@tonic-gate 		int new = ((longlong_t)((longlong_t)
219*5295Srandyf 		    cp->cs_nocomp_statefsz*100)/cp->cs_real_statefsz);
2200Sstevel@tonic-gate 
2210Sstevel@tonic-gate 		printf("\tCompression Ratio:\t\t%5d.%1d%1d (worst %d.%1d%1d)\n",
222*5295Srandyf 		    new/100, (new%100)/10, new%10,
223*5295Srandyf 		    min/100, (min%100)/10, min%10);
2240Sstevel@tonic-gate 	}
2250Sstevel@tonic-gate }
2260Sstevel@tonic-gate 
2270Sstevel@tonic-gate struct cpr_event *
cpr_find_event(char * name,int new)2280Sstevel@tonic-gate cpr_find_event(char *name, int new)
2290Sstevel@tonic-gate {
2300Sstevel@tonic-gate 	struct cpr_stat *cp = STAT;
2310Sstevel@tonic-gate 	struct cpr_event *cep;
2320Sstevel@tonic-gate 	int i;
2330Sstevel@tonic-gate 
2340Sstevel@tonic-gate 	for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
2350Sstevel@tonic-gate 		if (strcmp(name, cep->ce_name) == NULL)
2360Sstevel@tonic-gate 			return (cep);
2370Sstevel@tonic-gate 	}
2380Sstevel@tonic-gate 
2390Sstevel@tonic-gate 	/* if not begin not end either */
2400Sstevel@tonic-gate 	if (new == NULL)
2410Sstevel@tonic-gate 		return (NULL);
2420Sstevel@tonic-gate 
2430Sstevel@tonic-gate 	for (i = 0; i < CPR_E_MAX_EVENTNUM; i++) {
2440Sstevel@tonic-gate 		for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
2450Sstevel@tonic-gate 			if (&cpr_events_buf[i] == cep)
2460Sstevel@tonic-gate 				break;
2470Sstevel@tonic-gate 		}
2480Sstevel@tonic-gate 		if (!cep) {
2490Sstevel@tonic-gate 			struct cpr_event *new_cep;
2500Sstevel@tonic-gate 
2510Sstevel@tonic-gate 			new_cep = &cpr_events_buf[i];
2520Sstevel@tonic-gate 			(void) strcpy(new_cep->ce_name, name);
2530Sstevel@tonic-gate 
2540Sstevel@tonic-gate 			if (!cp->cs_event_head) {
2550Sstevel@tonic-gate 				/* The 1st one */
2560Sstevel@tonic-gate 				cp->cs_event_head = new_cep;
2570Sstevel@tonic-gate 			} else {
2580Sstevel@tonic-gate 				/* insert to tail */
2590Sstevel@tonic-gate 				new_cep->ce_next = cp->cs_event_tail->ce_next;
2600Sstevel@tonic-gate 				cp->cs_event_tail->ce_next = new_cep;
2610Sstevel@tonic-gate 			}
2620Sstevel@tonic-gate 			cp->cs_event_tail = new_cep;
2630Sstevel@tonic-gate 			return (new_cep);
2640Sstevel@tonic-gate 		}
2650Sstevel@tonic-gate 	}
2660Sstevel@tonic-gate 	return (NULL);
2670Sstevel@tonic-gate }
2680Sstevel@tonic-gate 
2690Sstevel@tonic-gate static time_t min_promtime;
2700Sstevel@tonic-gate 
2710Sstevel@tonic-gate void
cpr_convert_promtime(cpr_time_t * pop)2720Sstevel@tonic-gate cpr_convert_promtime(cpr_time_t *pop)
2730Sstevel@tonic-gate {
2740Sstevel@tonic-gate 	time_t pwroff_time, cb_time;
2750Sstevel@tonic-gate 	cpr_time_t *startp, *shdnp, *endp;
2760Sstevel@tonic-gate 
2770Sstevel@tonic-gate 	startp = &cpr_term.tm_cprboot_start;
2780Sstevel@tonic-gate 	shdnp = &cpr_term.tm_shutdown;
2790Sstevel@tonic-gate 	endp = &cpr_term.tm_cprboot_end;
2800Sstevel@tonic-gate 
2810Sstevel@tonic-gate 	cb_time = endp->tv_sec - startp->tv_sec;
2820Sstevel@tonic-gate 
2830Sstevel@tonic-gate 	cpr_tod_get(endp);
2840Sstevel@tonic-gate 	startp->tv_sec = endp->tv_sec - cb_time;
2850Sstevel@tonic-gate 
2860Sstevel@tonic-gate 	if (min_promtime == 0 ||
2870Sstevel@tonic-gate 	    min_promtime > (endp->tv_sec - shdnp->tv_sec - cb_time))
2880Sstevel@tonic-gate 		min_promtime = endp->tv_sec - shdnp->tv_sec - cb_time;
2890Sstevel@tonic-gate 
2900Sstevel@tonic-gate 	if (min_promtime > CPR_DEFAULT_PROMTIME)
2910Sstevel@tonic-gate 		min_promtime = CPR_DEFAULT_PROMTIME;
2920Sstevel@tonic-gate 
2930Sstevel@tonic-gate 	pwroff_time = startp->tv_sec - shdnp->tv_sec - min_promtime;
2940Sstevel@tonic-gate 
2950Sstevel@tonic-gate 	wholecycle_tv.tv_sec += pwroff_time; /* offset the poweroff time */
2960Sstevel@tonic-gate 
2970Sstevel@tonic-gate 	pop->tv_sec = startp->tv_sec - min_promtime;
2980Sstevel@tonic-gate }
299