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