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