"Fossies" - the Fresh Open Source Software Archive

Member "xosview-1.23/sunos5/kstats.h" (11 Jul 2020, 3734 Bytes) of package /linux/misc/xosview-1.23.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "kstats.h" see the Fossies "Dox" file reference documentation.

    1 #ifndef _KStatList_H_
    2 #define _KStatList_H_
    3 
    4 #include <stdlib.h>
    5 #include <unistd.h>
    6 #include <string.h>
    7 #include <strings.h>
    8 #include <kstat.h>
    9 #include <vector>
   10 #include <iostream>
   11 
   12 
   13 // Helper to keep track of kstats.
   14 class KStatList {
   15  public:
   16     enum module {  // module:instance:name (class)
   17       CPU_STAT,    // *:*:cpu_stat*
   18       CPU_INFO,    // *:*:cpu_info*
   19       CPU_SYS,     // cpu:*:sys
   20       DISKS,       // *:*:*         (disk)
   21       NETS         // {link,lo}:*:* (net)
   22     };
   23     static KStatList *getList(kstat_ctl_t *kcp, module m) {
   24         switch (m) {
   25             case CPU_STAT:
   26                 static KStatList cpu_stats(kcp, m);
   27                 return &cpu_stats;
   28             case CPU_INFO:
   29                 static KStatList cpu_infos(kcp, m);
   30                 return &cpu_infos;
   31             case CPU_SYS:
   32                 static KStatList cpu_sys(kcp, m);
   33                 return &cpu_sys;
   34             case DISKS:
   35                 static KStatList disks(kcp, m);
   36                 return &disks;
   37             case NETS:
   38                 static KStatList nets(kcp, m);
   39                 return &nets;
   40             default:
   41                 return NULL;
   42         }
   43     }
   44     kstat_t *operator[](size_t i) {
   45         return ( i < _stats.size() ? _stats[i] : NULL );
   46     }
   47     size_t count(void) { return _stats.size(); }
   48     void update(kstat_ctl_t *kcp) {
   49         if (kstat_chain_update(kcp) > 0 || _chain != kcp->kc_chain_id) {
   50             XOSDEBUG("kstat chain id changed to %d\n", kcp->kc_chain_id);
   51             _chain = kcp->kc_chain_id;
   52             _stats.clear();
   53             getstats(kcp);
   54         }
   55     }
   56  private:
   57     KStatList(const KStatList &);
   58     ~KStatList(void) {}
   59     KStatList &operator=(const KStatList &);
   60     KStatList(kstat_ctl_t *kcp, module m) {
   61         _chain = kcp->kc_chain_id;
   62         _m = m;
   63         getstats(kcp);
   64     }
   65     void getstats(kstat_ctl_t *kcp) {
   66         for (kstat_t *ksp = kcp->kc_chain; ksp != NULL; ksp = ksp->ks_next) {
   67             if (_m == CPU_STAT && strncmp(ksp->ks_name, "cpu_stat", 8) == 0)
   68                 _stats.push_back(ksp);
   69             if (_m == CPU_INFO && strncmp(ksp->ks_name, "cpu_info", 8) == 0)
   70                 _stats.push_back(ksp);
   71             if (_m == CPU_SYS && ksp->ks_type == KSTAT_TYPE_NAMED &&
   72                 strncmp(ksp->ks_module, "cpu", 3) == 0 &&
   73                 strncmp(ksp->ks_name, "sys", 3) == 0)
   74                 _stats.push_back(ksp);
   75             if (_m == DISKS && ksp->ks_type == KSTAT_TYPE_IO &&
   76                 strncmp(ksp->ks_class, "disk", 4) == 0)
   77                 _stats.push_back(ksp);
   78             if (_m == NETS && ksp->ks_type == KSTAT_TYPE_NAMED &&
   79                 strncmp(ksp->ks_class, "net", 3) == 0 &&
   80                 ( strncmp(ksp->ks_module, "link", 4) == 0 ||
   81                   strncmp(ksp->ks_module, "lo", 2) == 0 ))
   82                 _stats.push_back(ksp);
   83         }
   84     }
   85     kid_t _chain;
   86     module _m;
   87     std::vector<kstat_t *> _stats;
   88 };
   89 
   90 
   91 // Read the correct value from "named" type kstat.
   92 inline double kstat_to_double(kstat_named_t *k) {
   93     switch (k->data_type) {
   94         case KSTAT_DATA_INT32:
   95             return k->value.i32;
   96         case KSTAT_DATA_UINT32:
   97             return k->value.ui32;
   98 #if defined(_INT64_TYPE)
   99         case KSTAT_DATA_INT64:
  100             return k->value.i64;
  101         case KSTAT_DATA_UINT64:
  102             return k->value.ui64;
  103 #endif
  104         case KSTAT_DATA_FLOAT:
  105             return k->value.f;
  106         case KSTAT_DATA_DOUBLE:
  107             return k->value.d;
  108         case KSTAT_DATA_CHAR:
  109         case KSTAT_DATA_STRING:
  110         default:
  111             std::cerr << "kstat data type " << k->data_type
  112                       << " can not be converted to number."
  113                       << std::endl;
  114             exit(1);
  115     }
  116 }
  117 
  118 inline unsigned long long kstat_to_ui64(kstat_named_t *k) {
  119     switch (k->data_type) {
  120         case KSTAT_DATA_INT32:
  121             return k->value.i32;
  122         case KSTAT_DATA_UINT32:
  123             return k->value.ui32;
  124 #if defined(_INT64_TYPE)
  125         case KSTAT_DATA_INT64:
  126             return k->value.i64;
  127         case KSTAT_DATA_UINT64:
  128             return k->value.ui64;
  129 #endif
  130         case KSTAT_DATA_FLOAT:
  131             return k->value.f;
  132         case KSTAT_DATA_DOUBLE:
  133             return k->value.d;
  134         case KSTAT_DATA_CHAR:
  135         case KSTAT_DATA_STRING:
  136         default:
  137             std::cerr << "kstat data type " << k->data_type
  138                       << " can not be converted to number."
  139                       << std::endl;
  140             exit(1);
  141     }
  142 }
  143 
  144 
  145 #endif
  146