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
5ae115bc7Smrj * Common Development and Distribution License (the "License").
6ae115bc7Smrj * 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 /*
22*dff8ce88SGary Mills * Copyright (c) 2014 Gary Mills
23ae115bc7Smrj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
24ae115bc7Smrj * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate */
267c478bd9Sstevel@tonic-gate
277c478bd9Sstevel@tonic-gate #include <sys/types.h>
287c478bd9Sstevel@tonic-gate #include <sys/ddi.h>
297c478bd9Sstevel@tonic-gate #include <sys/pte.h>
307c478bd9Sstevel@tonic-gate #include <sys/cpr.h>
317c478bd9Sstevel@tonic-gate
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate * Support routines for CPR statistic collection
347c478bd9Sstevel@tonic-gate */
357c478bd9Sstevel@tonic-gate struct cpr_event cpr_events_buf[CPR_E_MAX_EVENTNUM];
367c478bd9Sstevel@tonic-gate
377c478bd9Sstevel@tonic-gate extern struct cpr_terminator cpr_term;
387c478bd9Sstevel@tonic-gate
397c478bd9Sstevel@tonic-gate struct cpr_event *cpr_find_event(char *name, int new);
407c478bd9Sstevel@tonic-gate
417c478bd9Sstevel@tonic-gate #define CPR_DEFAULT_PROMTIME 30
427c478bd9Sstevel@tonic-gate #define CE_START_MASK 0x8000000
437c478bd9Sstevel@tonic-gate
447c478bd9Sstevel@tonic-gate /*
457c478bd9Sstevel@tonic-gate * Use ctp to specify another time point instead of the current time;
467c478bd9Sstevel@tonic-gate * Otherwise, ctp is NULL.
477c478bd9Sstevel@tonic-gate */
487c478bd9Sstevel@tonic-gate void
cpr_stat_event_start(char * name,cpr_time_t * ctp)497c478bd9Sstevel@tonic-gate cpr_stat_event_start(char *name, cpr_time_t *ctp)
507c478bd9Sstevel@tonic-gate {
517c478bd9Sstevel@tonic-gate struct cpr_event *cep;
527c478bd9Sstevel@tonic-gate cpr_time_t tv;
537c478bd9Sstevel@tonic-gate
547c478bd9Sstevel@tonic-gate if (ctp)
557c478bd9Sstevel@tonic-gate tv = *ctp;
567c478bd9Sstevel@tonic-gate else {
577c478bd9Sstevel@tonic-gate /* need relative time even when hrestime is stoped */
587c478bd9Sstevel@tonic-gate cpr_tod_get(&tv);
597c478bd9Sstevel@tonic-gate }
607c478bd9Sstevel@tonic-gate
617c478bd9Sstevel@tonic-gate if ((cep = cpr_find_event(name, 1)) == NULL) {
627c478bd9Sstevel@tonic-gate cpr_err(CE_WARN, "cpr_stat: run out of event buffers");
637c478bd9Sstevel@tonic-gate return;
647c478bd9Sstevel@tonic-gate }
657c478bd9Sstevel@tonic-gate /*
667c478bd9Sstevel@tonic-gate * disallow entering start twice without calling end first
677c478bd9Sstevel@tonic-gate */
687c478bd9Sstevel@tonic-gate if (cep->ce_ntests & CE_START_MASK)
697c478bd9Sstevel@tonic-gate return;
707c478bd9Sstevel@tonic-gate
717c478bd9Sstevel@tonic-gate cep->ce_ntests |= CE_START_MASK;
727c478bd9Sstevel@tonic-gate cep->ce_sec.stime = cep->ce_sec.etime = tv.tv_sec;
73*dff8ce88SGary Mills cep->ce_sec.ltime = 0;
747c478bd9Sstevel@tonic-gate cep->ce_msec.stime = cep->ce_msec.etime = tv.tv_nsec / 100000000;
75*dff8ce88SGary Mills cep->ce_msec.ltime = 0;
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate
787c478bd9Sstevel@tonic-gate void
cpr_stat_event_end(char * name,cpr_time_t * ctp)797c478bd9Sstevel@tonic-gate cpr_stat_event_end(char *name, cpr_time_t *ctp)
807c478bd9Sstevel@tonic-gate {
817c478bd9Sstevel@tonic-gate struct cpr_stat *cp = STAT;
827c478bd9Sstevel@tonic-gate struct cpr_event *cep;
837c478bd9Sstevel@tonic-gate cpr_time_t tv;
847c478bd9Sstevel@tonic-gate
857c478bd9Sstevel@tonic-gate if (ctp)
867c478bd9Sstevel@tonic-gate tv = *ctp;
877c478bd9Sstevel@tonic-gate else
887c478bd9Sstevel@tonic-gate cpr_tod_get(&tv);
897c478bd9Sstevel@tonic-gate
907c478bd9Sstevel@tonic-gate if ((cep = cpr_find_event(name, 0)) == NULL) {
917c478bd9Sstevel@tonic-gate #ifdef CPR_STAT
92ae115bc7Smrj prom_printf("cpr_stat: event \"%s\" is not monitored\n", name);
937c478bd9Sstevel@tonic-gate #endif /* CPR_STAT */
947c478bd9Sstevel@tonic-gate return;
957c478bd9Sstevel@tonic-gate }
967c478bd9Sstevel@tonic-gate
977c478bd9Sstevel@tonic-gate /*
987c478bd9Sstevel@tonic-gate * diallow entering end twice without calling end first
997c478bd9Sstevel@tonic-gate */
1007c478bd9Sstevel@tonic-gate if (!(cep->ce_ntests & CE_START_MASK))
1017c478bd9Sstevel@tonic-gate return;
1027c478bd9Sstevel@tonic-gate
1037c478bd9Sstevel@tonic-gate cep->ce_ntests &= ~CE_START_MASK;
1047c478bd9Sstevel@tonic-gate cep->ce_ntests++;
1057c478bd9Sstevel@tonic-gate
1067c478bd9Sstevel@tonic-gate /*
1077c478bd9Sstevel@tonic-gate * calculate seconds
1087c478bd9Sstevel@tonic-gate */
1097c478bd9Sstevel@tonic-gate cep->ce_sec.etime = tv.tv_sec;
1107c478bd9Sstevel@tonic-gate cep->ce_sec.ltime = cep->ce_sec.etime - cep->ce_sec.stime;
1117c478bd9Sstevel@tonic-gate cep->ce_sec.mtime = ((cep->ce_sec.mtime * (cep->ce_ntests - 1)) +
1127c478bd9Sstevel@tonic-gate cep->ce_sec.ltime) / cep->ce_ntests;
1137c478bd9Sstevel@tonic-gate
1147c478bd9Sstevel@tonic-gate /*
1157c478bd9Sstevel@tonic-gate * calculate 100*milliseconds
1167c478bd9Sstevel@tonic-gate */
1177c478bd9Sstevel@tonic-gate if (cep->ce_sec.ltime == 0) {
1187c478bd9Sstevel@tonic-gate cep->ce_msec.etime = tv.tv_nsec / 100000000;
1197c478bd9Sstevel@tonic-gate cep->ce_msec.ltime =
1207c478bd9Sstevel@tonic-gate (cep->ce_msec.etime <= cep->ce_msec.stime) ? 0 :
1217c478bd9Sstevel@tonic-gate (cep->ce_msec.etime - cep->ce_msec.stime);
1227c478bd9Sstevel@tonic-gate cep->ce_msec.mtime =
1237c478bd9Sstevel@tonic-gate ((cep->ce_msec.mtime * (cep->ce_ntests - 1)) +
1247c478bd9Sstevel@tonic-gate cep->ce_msec.ltime) / cep->ce_ntests;
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate cp->cs_ntests = cep->ce_ntests & ~CE_START_MASK;
1277c478bd9Sstevel@tonic-gate }
1287c478bd9Sstevel@tonic-gate
1297c478bd9Sstevel@tonic-gate void
cpr_stat_cleanup()1307c478bd9Sstevel@tonic-gate cpr_stat_cleanup()
1317c478bd9Sstevel@tonic-gate {
1327c478bd9Sstevel@tonic-gate struct cpr_stat *cp = STAT;
1337c478bd9Sstevel@tonic-gate struct cpr_event *cep;
1347c478bd9Sstevel@tonic-gate
1357c478bd9Sstevel@tonic-gate for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
1367c478bd9Sstevel@tonic-gate if ((cep->ce_ntests & CE_START_MASK) &&
1377c478bd9Sstevel@tonic-gate strcmp(cep->ce_name, "POST CPR DELAY") != NULL) {
1387c478bd9Sstevel@tonic-gate cpr_stat_event_end(cep->ce_name, 0);
1397c478bd9Sstevel@tonic-gate cep->ce_ntests &= ~CE_START_MASK;
1407c478bd9Sstevel@tonic-gate }
1417c478bd9Sstevel@tonic-gate }
1427c478bd9Sstevel@tonic-gate }
1437c478bd9Sstevel@tonic-gate
1447c478bd9Sstevel@tonic-gate void
cpr_stat_init()1457c478bd9Sstevel@tonic-gate cpr_stat_init()
1467c478bd9Sstevel@tonic-gate {
1477c478bd9Sstevel@tonic-gate STAT->cs_real_statefsz = 0;
1487c478bd9Sstevel@tonic-gate STAT->cs_dumped_statefsz = 0;
1497c478bd9Sstevel@tonic-gate }
1507c478bd9Sstevel@tonic-gate
1517c478bd9Sstevel@tonic-gate void
cpr_stat_record_events()1527c478bd9Sstevel@tonic-gate cpr_stat_record_events()
1537c478bd9Sstevel@tonic-gate {
1547c478bd9Sstevel@tonic-gate if (cpr_term.real_statef_size) {
1557c478bd9Sstevel@tonic-gate int cur_comprate;
1567c478bd9Sstevel@tonic-gate
1577c478bd9Sstevel@tonic-gate STAT->cs_real_statefsz = cpr_term.real_statef_size;
1587c478bd9Sstevel@tonic-gate cur_comprate = ((longlong_t)((longlong_t)
1597c478bd9Sstevel@tonic-gate STAT->cs_nocomp_statefsz*100)/
1607c478bd9Sstevel@tonic-gate STAT->cs_real_statefsz);
1617c478bd9Sstevel@tonic-gate if (STAT->cs_min_comprate == 0 ||
1627c478bd9Sstevel@tonic-gate (STAT->cs_min_comprate > cur_comprate))
1637c478bd9Sstevel@tonic-gate STAT->cs_min_comprate = cur_comprate;
1647c478bd9Sstevel@tonic-gate }
1657c478bd9Sstevel@tonic-gate }
1667c478bd9Sstevel@tonic-gate
1677c478bd9Sstevel@tonic-gate void
cpr_stat_event_print()1687c478bd9Sstevel@tonic-gate cpr_stat_event_print()
1697c478bd9Sstevel@tonic-gate {
1707c478bd9Sstevel@tonic-gate struct cpr_stat *cp = STAT;
1717c478bd9Sstevel@tonic-gate struct cpr_event *cep;
1727c478bd9Sstevel@tonic-gate char *fmt, *tabs;
1737c478bd9Sstevel@tonic-gate int len;
1747c478bd9Sstevel@tonic-gate
1757c478bd9Sstevel@tonic-gate printf("\n");
1767c478bd9Sstevel@tonic-gate printf("---------------\t\tCPR PERFORMANCE SUMMARY\t\t-------------\n");
1777c478bd9Sstevel@tonic-gate printf("Events\t\t\tRepeat[times]\tMeantime[sec]\tLastEvnt[sec]\n");
1787c478bd9Sstevel@tonic-gate
1797c478bd9Sstevel@tonic-gate for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
1807c478bd9Sstevel@tonic-gate len = strlen(cep->ce_name);
1817c478bd9Sstevel@tonic-gate if (len < 8)
1827c478bd9Sstevel@tonic-gate tabs = "\t\t\t";
1837c478bd9Sstevel@tonic-gate else if (len < 16)
1847c478bd9Sstevel@tonic-gate tabs = "\t\t";
1857c478bd9Sstevel@tonic-gate else
1867c478bd9Sstevel@tonic-gate tabs = "\t";
1877c478bd9Sstevel@tonic-gate if (strcmp(cep->ce_name, "Suspend Total") == NULL ||
1887c478bd9Sstevel@tonic-gate strcmp(cep->ce_name, "Resume Total") == NULL ||
1897c478bd9Sstevel@tonic-gate strcmp(cep->ce_name, "POST CPR DELAY") == NULL ||
1907c478bd9Sstevel@tonic-gate strcmp(cep->ce_name, "WHOLE CYCLE") == NULL)
1917c478bd9Sstevel@tonic-gate fmt = "%s%s%d\t\t%3d.%1d\t\t%3d.%1d\n";
1927c478bd9Sstevel@tonic-gate else
1937c478bd9Sstevel@tonic-gate fmt = "%s%s%d\t\t %3d.%1d\t\t %3d.%1d\n";
1947c478bd9Sstevel@tonic-gate printf(fmt, cep->ce_name, tabs, (int)cep->ce_ntests,
1957c478bd9Sstevel@tonic-gate (int)cep->ce_sec.mtime, (int)(cep->ce_msec.mtime / 10),
1967c478bd9Sstevel@tonic-gate (int)cep->ce_sec.ltime, (int)(cep->ce_msec.ltime / 10));
1977c478bd9Sstevel@tonic-gate }
1987c478bd9Sstevel@tonic-gate delay(drv_usectohz(10000)); /* otherwise the next line goes to prom */
1997c478bd9Sstevel@tonic-gate /*
2007c478bd9Sstevel@tonic-gate * print the rest of the stat data
2017c478bd9Sstevel@tonic-gate */
2027c478bd9Sstevel@tonic-gate printf("\nMISCELLANEOUS STATISTICS INFORMATION (units in KBytes)\n\n");
2037c478bd9Sstevel@tonic-gate printf("\tUser Pages w/o Swapspace:\t%8lu (%lu pages)\n",
2047c478bd9Sstevel@tonic-gate cp->cs_nosw_pages*PAGESIZE/1000, cp->cs_nosw_pages);
2057c478bd9Sstevel@tonic-gate printf("\tTotal Upages Saved to Statefile:%8d (%d pages)\n",
2067c478bd9Sstevel@tonic-gate cp->cs_upage2statef*PAGESIZE/1000, cp->cs_upage2statef);
2077c478bd9Sstevel@tonic-gate if (cp->cs_mclustsz)
2087c478bd9Sstevel@tonic-gate printf("\tAverage Cluster Size:\t\t%8d (%d.%1d%1d pages)\n\n",
2097c478bd9Sstevel@tonic-gate cp->cs_mclustsz/1000, cp->cs_mclustsz/PAGESIZE,
2107c478bd9Sstevel@tonic-gate ((cp->cs_mclustsz%PAGESIZE)*10/PAGESIZE),
2117c478bd9Sstevel@tonic-gate ((cp->cs_mclustsz%PAGESIZE)*100/PAGESIZE)%10);
2127c478bd9Sstevel@tonic-gate printf("\tKernel Memory Size:\t\t%8lu\n", cp->cs_nocomp_statefsz/1000);
2137c478bd9Sstevel@tonic-gate printf("\tEstimated Statefile Size:\t%8lu\n", cp->cs_est_statefsz/1000);
2147c478bd9Sstevel@tonic-gate printf("\tActual Statefile Size:\t\t%8lu\n", cp->cs_real_statefsz/1000);
2157c478bd9Sstevel@tonic-gate if (cp->cs_real_statefsz) {
2167c478bd9Sstevel@tonic-gate int min = cp->cs_min_comprate;
2177c478bd9Sstevel@tonic-gate int new = ((longlong_t)((longlong_t)
2187c478bd9Sstevel@tonic-gate cp->cs_nocomp_statefsz*100)/cp->cs_real_statefsz);
2197c478bd9Sstevel@tonic-gate
2207c478bd9Sstevel@tonic-gate printf("\tCompression Ratio:\t\t%5d.%1d%1d (worst %d.%1d%1d)\n",
2217c478bd9Sstevel@tonic-gate new/100, (new%100)/10, new%10,
2227c478bd9Sstevel@tonic-gate min/100, (min%100)/10, min%10);
2237c478bd9Sstevel@tonic-gate }
2247c478bd9Sstevel@tonic-gate }
2257c478bd9Sstevel@tonic-gate
2267c478bd9Sstevel@tonic-gate struct cpr_event *
cpr_find_event(char * name,int new)2277c478bd9Sstevel@tonic-gate cpr_find_event(char *name, int new)
2287c478bd9Sstevel@tonic-gate {
2297c478bd9Sstevel@tonic-gate struct cpr_stat *cp = STAT;
2307c478bd9Sstevel@tonic-gate struct cpr_event *cep;
2317c478bd9Sstevel@tonic-gate int i;
2327c478bd9Sstevel@tonic-gate
2337c478bd9Sstevel@tonic-gate for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
2347c478bd9Sstevel@tonic-gate if (strcmp(name, cep->ce_name) == NULL)
2357c478bd9Sstevel@tonic-gate return (cep);
2367c478bd9Sstevel@tonic-gate }
2377c478bd9Sstevel@tonic-gate
2387c478bd9Sstevel@tonic-gate /* if not begin not end either */
2397c478bd9Sstevel@tonic-gate if (new == NULL)
2407c478bd9Sstevel@tonic-gate return (NULL);
2417c478bd9Sstevel@tonic-gate
2427c478bd9Sstevel@tonic-gate for (i = 0; i < CPR_E_MAX_EVENTNUM; i++) {
2437c478bd9Sstevel@tonic-gate for (cep = cp->cs_event_head; cep; cep = cep->ce_next) {
2447c478bd9Sstevel@tonic-gate if (&cpr_events_buf[i] == cep)
2457c478bd9Sstevel@tonic-gate break;
2467c478bd9Sstevel@tonic-gate }
2477c478bd9Sstevel@tonic-gate if (!cep) {
2487c478bd9Sstevel@tonic-gate struct cpr_event *new_cep;
2497c478bd9Sstevel@tonic-gate
2507c478bd9Sstevel@tonic-gate new_cep = &cpr_events_buf[i];
2517c478bd9Sstevel@tonic-gate (void) strcpy(new_cep->ce_name, name);
2527c478bd9Sstevel@tonic-gate
2537c478bd9Sstevel@tonic-gate if (!cp->cs_event_head) {
2547c478bd9Sstevel@tonic-gate /* The 1st one */
2557c478bd9Sstevel@tonic-gate cp->cs_event_head = new_cep;
2567c478bd9Sstevel@tonic-gate } else {
2577c478bd9Sstevel@tonic-gate /* insert to tail */
2587c478bd9Sstevel@tonic-gate new_cep->ce_next = cp->cs_event_tail->ce_next;
2597c478bd9Sstevel@tonic-gate cp->cs_event_tail->ce_next = new_cep;
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate cp->cs_event_tail = new_cep;
2627c478bd9Sstevel@tonic-gate return (new_cep);
2637c478bd9Sstevel@tonic-gate }
2647c478bd9Sstevel@tonic-gate }
2657c478bd9Sstevel@tonic-gate return (NULL);
2667c478bd9Sstevel@tonic-gate }
2677c478bd9Sstevel@tonic-gate
2687c478bd9Sstevel@tonic-gate static time_t min_promtime;
2697c478bd9Sstevel@tonic-gate
2707c478bd9Sstevel@tonic-gate void
cpr_convert_promtime(cpr_time_t * pop)2717c478bd9Sstevel@tonic-gate cpr_convert_promtime(cpr_time_t *pop)
2727c478bd9Sstevel@tonic-gate {
2737c478bd9Sstevel@tonic-gate time_t pwroff_time, cb_time;
2747c478bd9Sstevel@tonic-gate cpr_time_t *startp, *shdnp, *endp;
2757c478bd9Sstevel@tonic-gate
2767c478bd9Sstevel@tonic-gate startp = &cpr_term.tm_cprboot_start;
2777c478bd9Sstevel@tonic-gate shdnp = &cpr_term.tm_shutdown;
2787c478bd9Sstevel@tonic-gate endp = &cpr_term.tm_cprboot_end;
2797c478bd9Sstevel@tonic-gate
2807c478bd9Sstevel@tonic-gate cb_time = endp->tv_sec - startp->tv_sec;
2817c478bd9Sstevel@tonic-gate
2827c478bd9Sstevel@tonic-gate cpr_tod_get(endp);
2837c478bd9Sstevel@tonic-gate startp->tv_sec = endp->tv_sec - cb_time;
2847c478bd9Sstevel@tonic-gate
2857c478bd9Sstevel@tonic-gate if (min_promtime == 0 ||
2867c478bd9Sstevel@tonic-gate min_promtime > (endp->tv_sec - shdnp->tv_sec - cb_time))
2877c478bd9Sstevel@tonic-gate min_promtime = endp->tv_sec - shdnp->tv_sec - cb_time;
2887c478bd9Sstevel@tonic-gate
2897c478bd9Sstevel@tonic-gate if (min_promtime > CPR_DEFAULT_PROMTIME)
2907c478bd9Sstevel@tonic-gate min_promtime = CPR_DEFAULT_PROMTIME;
2917c478bd9Sstevel@tonic-gate
2927c478bd9Sstevel@tonic-gate pwroff_time = startp->tv_sec - shdnp->tv_sec - min_promtime;
2937c478bd9Sstevel@tonic-gate
2947c478bd9Sstevel@tonic-gate wholecycle_tv.tv_sec += pwroff_time; /* offset the poweroff time */
2957c478bd9Sstevel@tonic-gate
2967c478bd9Sstevel@tonic-gate pop->tv_sec = startp->tv_sec - min_promtime;
2977c478bd9Sstevel@tonic-gate }
298