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 2008 Sun Microsystems, Inc.  All rights reserved.
  23  * Use is subject to license terms.
  24  */
  25 
  26 #include <stdio.h>
  27 #include <unistd.h>
  28 #include <errno.h>
  29 
  30 #include <kstat.h>
  31 
  32 #include "ii_stats.h"
  33 #include "sdbc_stats.h"
  34 #include "sndr_stats.h"
  35 
  36 #include "multi_stats.h"
  37 
  38 #include "dsstat.h"
  39 #include "common.h"
  40 #include "report.h"
  41 
  42 /*
  43  * do_stats() - called by main() to start monitoring
  44  *
  45  */
  46 int
  47 do_stats()
  48 {
  49         int error;
  50         int pass;
  51 
  52         /* Collection/reporting loop */
  53         for (pass = 0; ; pass++) { /* CSTYLED */
  54                 if (iterations != -1 && pass >= iterations)
  55                         return (0);
  56 
  57                 error = discover();
  58 
  59                 if (error == ENOMEM || error == EINVAL)
  60                         return (error);
  61 
  62                 if (error == EAGAIN && pass == 0)
  63                         return (error);
  64 
  65                 (void) sleep(interval);
  66 
  67                 if ((error = update()) != 0)
  68                         return (error);
  69 
  70                 if (report())
  71                         break;
  72         }
  73 
  74         /* No stats on this system */
  75         return (EAGAIN);
  76 }
  77 
  78 int
  79 discover()
  80 {
  81         int err = 0;
  82 
  83         int sdbc_err = 0;
  84         int sndr_err = 0;
  85         int ii_err = 0;
  86 
  87         kstat_ctl_t *kc;
  88 
  89         if ((kc = kstat_open()) == NULL)
  90                 return (ENOMEM);
  91 
  92         if (mode & SDBC) {
  93             sdbc_err = sdbc_discover(kc);
  94             err = sdbc_err;
  95             if (sdbc_err && !(mode & MULTI))
  96                 goto fail;
  97             if (sdbc_err && (mode & MULTI) && sdbc_err != EAGAIN)
  98                 goto fail;
  99         }
 100 
 101         if (mode & SNDR) {
 102             sndr_err = sndr_discover(kc);
 103             err = sndr_err;
 104             if (sndr_err && !(mode & MULTI))
 105                 goto fail;
 106             if (sndr_err && (mode & MULTI) && sndr_err != EAGAIN)
 107                 goto fail;
 108         }
 109 
 110         if (mode & IIMG) {
 111             ii_err = ii_discover(kc);
 112             err = ii_err;
 113             if (ii_err && !(mode & MULTI))
 114                 goto fail;
 115             if (ii_err && ii_err != EAGAIN && (mode & MULTI))
 116                 goto fail;
 117         }
 118 
 119         (void) kstat_close(kc);
 120         if (sdbc_err && sndr_err && ii_err)
 121             return (err);
 122         else
 123             return (0);
 124 
 125 fail:
 126         (void) kstat_close(kc);
 127         return (err);
 128 }
 129 
 130 int
 131 update()
 132 {
 133         int err = 0;
 134 
 135         int sdbc_err = 0;
 136         int sndr_err = 0;
 137         int ii_err = 0;
 138 
 139         kstat_ctl_t *kc;
 140 
 141         if ((kc = kstat_open()) == NULL)
 142                 goto fail;
 143 
 144         if (mode & SDBC) {
 145                 sdbc_err = sdbc_update(kc);
 146                 err = sdbc_err;
 147                 if (sdbc_err && !(mode & MULTI))
 148                         goto fail;
 149                 if (sdbc_err && (mode & MULTI) && sdbc_err != EAGAIN)
 150                         goto fail;
 151         }
 152 
 153         if (mode & SNDR) {
 154                 sndr_err = sndr_update(kc);
 155                 err = sndr_err;
 156                 if (sndr_err && !(mode & MULTI))
 157                         goto fail;
 158                 if (sndr_err && (mode & MULTI) && sndr_err != EAGAIN)
 159                         goto fail;
 160         }
 161 
 162         if (mode & IIMG) {
 163                 ii_err = ii_update(kc);
 164                 err = ii_err;
 165                 if (ii_err && !(mode & MULTI))
 166                         goto fail;
 167                 if (ii_err && (mode & MULTI) && ii_err != EAGAIN)
 168                         goto fail;
 169         }
 170 
 171         (void) kstat_close(kc);
 172         if (sdbc_err && sndr_err && ii_err)
 173             return (err);
 174         else
 175             return (0);
 176 
 177 fail:
 178         (void) kstat_close(kc);
 179         return (err);
 180 }
 181 
 182 int
 183 report()
 184 {
 185         int err = 0;
 186 
 187         int sdbc_err = 0;
 188         int sndr_err = 0;
 189         int ii_err = 0;
 190 
 191         hflags &= (HEADERS_EXL | HEADERS_ATT | HEADERS_BOR);
 192 
 193         if (mode & SNDR)
 194                 if (sndr_err = sndr_report())
 195                     err = sndr_err;
 196 
 197         if (mode & IIMG)
 198                 if (ii_err = ii_report())
 199                     err = ii_err;
 200 
 201         if ((mode & SDBC) && !(mode & MULTI))
 202                 if (sdbc_err = sdbc_report())
 203                     err = sdbc_err;
 204 
 205         return (err);
 206 }