xref: /illumos-gate/usr/src/uts/common/cpr/cpr_stat.c (revision 3db86aab)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 1993-2001 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/types.h>
30 #include <sys/ddi.h>
31 #include <sys/pte.h>
32 #include <sys/intreg.h>
33 #include <sys/cpr.h>
34 
35 /*
36  * Support routines for CPR statistic collection
37  */
38 struct cpr_event cpr_events_buf[CPR_E_MAX_EVENTNUM];
39 
40 extern struct cpr_terminator cpr_term;
41 
42 struct cpr_event *cpr_find_event(char *name, int new);
43 
44 #define	CPR_DEFAULT_PROMTIME		30
45 #define	CE_START_MASK			0x8000000
46 
47 /*
48  * Use ctp to specify another time point instead of the current time;
49  * Otherwise, ctp is NULL.
50  */
51 void
52 cpr_stat_event_start(char *name, cpr_time_t *ctp)
53 {
54 	struct cpr_event *cep;
55 	cpr_time_t tv;
56 
57 	if (ctp)
58 		tv = *ctp;
59 	else {
60 		/* need relative time even when hrestime is stoped */
61 		cpr_tod_get(&tv);
62 	}
63 
64 	if ((cep = cpr_find_event(name, 1)) == NULL) {
65 		cpr_err(CE_WARN, "cpr_stat: run out of event buffers");
66 		return;
67 	}
68 	/*
69 	 * disallow entering start twice without calling end first
70 	 */
71 	if (cep->ce_ntests & CE_START_MASK)
72 		return;
73 
74 	cep->ce_ntests |= CE_START_MASK;
75 	cep->ce_sec.stime = cep->ce_sec.etime = tv.tv_sec;
76 	cep->ce_sec.ltime = cep->ce_sec.ltime = 0;
77 	cep->ce_msec.stime = cep->ce_msec.etime = tv.tv_nsec / 100000000;
78 	cep->ce_msec.ltime = cep->ce_msec.ltime = 0;
79 }
80 
81 void
82 cpr_stat_event_end(char *name, cpr_time_t *ctp)
83 {
84 	struct cpr_stat *cp = STAT;
85 	struct cpr_event *cep;
86 	cpr_time_t tv;
87 
88 	if (ctp)
89 		tv = *ctp;
90 	else
91 		cpr_tod_get(&tv);
92 
93 	if ((cep = cpr_find_event(name, 0)) == NULL) {
94 #ifdef CPR_STAT
95 		errp("cpr_stat: event \"%s\" is not monitored\n", name);
96 #endif /* CPR_STAT */
97 		return;
98 	}
99 
100 	/*
101 	 * diallow entering end twice without calling end first
102 	 */
103 	if (!(cep->ce_ntests & CE_START_MASK))
104 		return;
105 
106 	cep->ce_ntests &= ~CE_START_MASK;
107 	cep->ce_ntests++;
108 
109 	/*
110 	 * calculate seconds
111 	 */
112 	cep->ce_sec.etime = tv.tv_sec;
113 	cep->ce_sec.ltime = cep->ce_sec.etime - cep->ce_sec.stime;
114 	cep->ce_sec.mtime = ((cep->ce_sec.mtime * (cep->ce_ntests - 1)) +
115 		cep->ce_sec.ltime) / cep->ce_ntests;
116 
117 	/*
118 	 * calculate 100*milliseconds
119 	 */
120 	if (cep->ce_sec.ltime == 0) {
121 		cep->ce_msec.etime = tv.tv_nsec / 100000000;
122 		cep->ce_msec.ltime =
123 		    (cep->ce_msec.etime <= cep->ce_msec.stime) ? 0 :
124 		    (cep->ce_msec.etime - cep->ce_msec.stime);
125 		cep->ce_msec.mtime =
126 		    ((cep->ce_msec.mtime * (cep->ce_ntests - 1)) +
127 		    cep->ce_msec.ltime) / cep->ce_ntests;
128 	}
129 	cp->cs_ntests = cep->ce_ntests & ~CE_START_MASK;
130 }
131 
132 void
133 cpr_stat_cleanup()
134 {
135 	struct cpr_stat *cp = STAT;
136 	struct cpr_event *cep;
137 
138 	for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
139 		if ((cep->ce_ntests & CE_START_MASK) &&
140 		    strcmp(cep->ce_name, "POST CPR DELAY") != NULL) {
141 			cpr_stat_event_end(cep->ce_name, 0);
142 			cep->ce_ntests &= ~CE_START_MASK;
143 		}
144 	}
145 }
146 
147 void
148 cpr_stat_init()
149 {
150 	STAT->cs_real_statefsz = 0;
151 	STAT->cs_dumped_statefsz = 0;
152 }
153 
154 void
155 cpr_stat_record_events()
156 {
157 	if (cpr_term.real_statef_size) {
158 		int cur_comprate;
159 
160 		STAT->cs_real_statefsz = cpr_term.real_statef_size;
161 		cur_comprate = ((longlong_t)((longlong_t)
162 			STAT->cs_nocomp_statefsz*100)/
163 			STAT->cs_real_statefsz);
164 		if (STAT->cs_min_comprate == 0 ||
165 			(STAT->cs_min_comprate > cur_comprate))
166 			STAT->cs_min_comprate = cur_comprate;
167 	}
168 }
169 
170 void
171 cpr_stat_event_print()
172 {
173 	struct cpr_stat *cp = STAT;
174 	struct cpr_event *cep;
175 	char *fmt, *tabs;
176 	int len;
177 
178 	printf("\n");
179 	printf("---------------\t\tCPR PERFORMANCE SUMMARY\t\t-------------\n");
180 	printf("Events\t\t\tRepeat[times]\tMeantime[sec]\tLastEvnt[sec]\n");
181 
182 	for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
183 		len = strlen(cep->ce_name);
184 		if (len < 8)
185 			tabs = "\t\t\t";
186 		else if (len < 16)
187 			tabs = "\t\t";
188 		else
189 			tabs = "\t";
190 		if (strcmp(cep->ce_name, "Suspend Total") == NULL ||
191 		    strcmp(cep->ce_name, "Resume Total") == NULL ||
192 		    strcmp(cep->ce_name, "POST CPR DELAY") == NULL ||
193 		    strcmp(cep->ce_name, "WHOLE CYCLE") == NULL)
194 			fmt = "%s%s%d\t\t%3d.%1d\t\t%3d.%1d\n";
195 		else
196 			fmt = "%s%s%d\t\t  %3d.%1d\t\t  %3d.%1d\n";
197 		printf(fmt, cep->ce_name, tabs, (int)cep->ce_ntests,
198 		    (int)cep->ce_sec.mtime, (int)(cep->ce_msec.mtime / 10),
199 		    (int)cep->ce_sec.ltime, (int)(cep->ce_msec.ltime / 10));
200 	}
201 	delay(drv_usectohz(10000)); /* otherwise the next line goes to prom */
202 	/*
203 	 * print the rest of the stat data
204 	 */
205 	printf("\nMISCELLANEOUS STATISTICS INFORMATION (units in KBytes)\n\n");
206 	printf("\tUser Pages w/o Swapspace:\t%8lu (%lu pages)\n",
207 		cp->cs_nosw_pages*PAGESIZE/1000, cp->cs_nosw_pages);
208 	printf("\tTotal Upages Saved to Statefile:%8d (%d pages)\n",
209 		cp->cs_upage2statef*PAGESIZE/1000, cp->cs_upage2statef);
210 	if (cp->cs_mclustsz)
211 		printf("\tAverage Cluster Size:\t\t%8d (%d.%1d%1d pages)\n\n",
212 		cp->cs_mclustsz/1000, cp->cs_mclustsz/PAGESIZE,
213 		((cp->cs_mclustsz%PAGESIZE)*10/PAGESIZE),
214 		((cp->cs_mclustsz%PAGESIZE)*100/PAGESIZE)%10);
215 	printf("\tKernel Memory Size:\t\t%8lu\n", cp->cs_nocomp_statefsz/1000);
216 	printf("\tEstimated Statefile Size:\t%8lu\n", cp->cs_est_statefsz/1000);
217 	printf("\tActual Statefile Size:\t\t%8lu\n", cp->cs_real_statefsz/1000);
218 	if (cp->cs_real_statefsz) {
219 		int min = cp->cs_min_comprate;
220 		int new = ((longlong_t)((longlong_t)
221 			cp->cs_nocomp_statefsz*100)/cp->cs_real_statefsz);
222 
223 		printf("\tCompression Ratio:\t\t%5d.%1d%1d (worst %d.%1d%1d)\n",
224 			new/100, (new%100)/10, new%10,
225 			min/100, (min%100)/10, min%10);
226 	}
227 }
228 
229 struct cpr_event *
230 cpr_find_event(char *name, int new)
231 {
232 	struct cpr_stat *cp = STAT;
233 	struct cpr_event *cep;
234 	int i;
235 
236 	for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
237 		if (strcmp(name, cep->ce_name) == NULL)
238 			return (cep);
239 	}
240 
241 	/* if not begin not end either */
242 	if (new == NULL)
243 		return (NULL);
244 
245 	for (i = 0; i < CPR_E_MAX_EVENTNUM; i++) {
246 		for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
247 			if (&cpr_events_buf[i] == cep)
248 				break;
249 		}
250 		if (!cep) {
251 			struct cpr_event *new_cep;
252 
253 			new_cep = &cpr_events_buf[i];
254 			(void) strcpy(new_cep->ce_name, name);
255 
256 			if (!cp->cs_event_head) {
257 				/* The 1st one */
258 				cp->cs_event_head = new_cep;
259 			} else {
260 				/* insert to tail */
261 				new_cep->ce_next = cp->cs_event_tail->ce_next;
262 				cp->cs_event_tail->ce_next = new_cep;
263 			}
264 			cp->cs_event_tail = new_cep;
265 			return (new_cep);
266 		}
267 	}
268 	return (NULL);
269 }
270 
271 static time_t min_promtime;
272 
273 void
274 cpr_convert_promtime(cpr_time_t *pop)
275 {
276 	time_t pwroff_time, cb_time;
277 	cpr_time_t *startp, *shdnp, *endp;
278 
279 	startp = &cpr_term.tm_cprboot_start;
280 	shdnp = &cpr_term.tm_shutdown;
281 	endp = &cpr_term.tm_cprboot_end;
282 
283 	cb_time = endp->tv_sec - startp->tv_sec;
284 
285 	cpr_tod_get(endp);
286 	startp->tv_sec = endp->tv_sec - cb_time;
287 
288 	if (min_promtime == 0 ||
289 	    min_promtime > (endp->tv_sec - shdnp->tv_sec - cb_time))
290 		min_promtime = endp->tv_sec - shdnp->tv_sec - cb_time;
291 
292 	if (min_promtime > CPR_DEFAULT_PROMTIME)
293 		min_promtime = CPR_DEFAULT_PROMTIME;
294 
295 	pwroff_time = startp->tv_sec - shdnp->tv_sec - min_promtime;
296 
297 	wholecycle_tv.tv_sec += pwroff_time; /* offset the poweroff time */
298 
299 	pop->tv_sec = startp->tv_sec - min_promtime;
300 }
301