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