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, Version 1.0 only
   6  * (the "License").  You may not use this file except in compliance
   7  * with the License.
   8  *
   9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
  10  * or http://www.opensolaris.org/os/licensing.
  11  * See the License for the specific language governing permissions
  12  * and limitations under the License.
  13  *
  14  * When distributing Covered Code, include this CDDL HEADER in each
  15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
  16  * If applicable, add the following below this CDDL HEADER, with the
  17  * fields enclosed by brackets "[]" replaced with your own identifying
  18  * information: Portions Copyright [yyyy] [name of copyright owner]
  19  *
  20  * CDDL HEADER END
  21  */
  22 /*
  23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
  24  * Use is subject to license terms.
  25  */
  26 
  27 #pragma ident   "%Z%%M% %I%     %E% SMI"
  28 
  29 /*
  30  *
  31  *                      stats_stats.c
  32  *
  33  * Routines for the `clean interface' to cachefs statistics.
  34  */
  35 
  36 #include <libintl.h>
  37 #include <sys/types.h>
  38 #include <sys/stat.h>
  39 #include <assert.h>
  40 #include <sys/fs/cachefs_fs.h>
  41 #include "stats.h"
  42 
  43 static kstat_t *
  44 stats_read_stat(stats_cookie_t *st)
  45 {
  46         kstat_t *stat;
  47 
  48         assert(stats_good(st));
  49         assert(st->st_flags & ST_BOUND);
  50 
  51         if (((stat = kstat_lookup(st->st_kstat_cookie,
  52             "cachefs", st->st_fsid, "stats")) == NULL) ||
  53             (kstat_read(st->st_kstat_cookie, stat, NULL) < 0)) {
  54                 stats_perror(st, SE_KERNEL,
  55                     gettext("Cannot lookup statistics"),
  56                     st->st_fsid);
  57                 goto out;
  58         }
  59 out:
  60         return (stat);
  61 }
  62 
  63 u_int
  64 stats_hits(stats_cookie_t *st)
  65 {
  66         kstat_t *ks;
  67         cachefs_stats_t *stats;
  68         u_int rc = 0;
  69 
  70         if ((ks = stats_read_stat(st)) != NULL) {
  71                 stats = (cachefs_stats_t *) ks->ks_data;
  72                 rc = stats->st_hits;
  73         } else {
  74                 stats_perror(st, SE_KERNEL,
  75                     gettext("Cannot read statistics"));
  76         }
  77 
  78         return (rc);
  79 }
  80 
  81 u_int
  82 stats_misses(stats_cookie_t *st)
  83 {
  84         kstat_t *ks;
  85         cachefs_stats_t *stats;
  86         u_int rc = 0;
  87 
  88         if ((ks = stats_read_stat(st)) != NULL) {
  89                 stats = (cachefs_stats_t *) ks->ks_data;
  90                 rc = stats->st_misses;
  91         } else {
  92                 stats_perror(st, SE_KERNEL,
  93                     gettext("Cannot read statistics"));
  94         }
  95 
  96         return (rc);
  97 }
  98 
  99 u_int
 100 stats_passes(stats_cookie_t *st)
 101 {
 102         kstat_t *ks;
 103         cachefs_stats_t *stats;
 104         u_int rc = 0;
 105 
 106         if ((ks = stats_read_stat(st)) != NULL) {
 107                 stats = (cachefs_stats_t *) ks->ks_data;
 108                 rc = stats->st_passes;
 109         } else {
 110                 stats_perror(st, SE_KERNEL,
 111                     gettext("Cannot read statistics"));
 112         }
 113 
 114         return (rc);
 115 }
 116 
 117 u_int
 118 stats_fails(stats_cookie_t *st)
 119 {
 120         kstat_t *ks;
 121         cachefs_stats_t *stats;
 122         u_int rc = 0;
 123 
 124         if ((ks = stats_read_stat(st)) != NULL) {
 125                 stats = (cachefs_stats_t *) ks->ks_data;
 126                 rc = stats->st_fails;
 127         } else {
 128                 stats_perror(st, SE_KERNEL,
 129                     gettext("Cannot read statistics"));
 130         }
 131 
 132         return (rc);
 133 }
 134 
 135 u_int
 136 stats_modifies(stats_cookie_t *st)
 137 {
 138         kstat_t *ks;
 139         cachefs_stats_t *stats;
 140         u_int rc = 0;
 141 
 142         if ((ks = stats_read_stat(st)) != NULL) {
 143                 stats = (cachefs_stats_t *) ks->ks_data;
 144                 rc = stats->st_modifies;
 145         } else {
 146                 stats_perror(st, SE_KERNEL,
 147                     gettext("Cannot read statistics"));
 148         }
 149 
 150         return (rc);
 151 }
 152 
 153 u_int
 154 stats_gc_count(stats_cookie_t *st)
 155 {
 156         kstat_t *ks;
 157         cachefs_stats_t *stats;
 158         u_int rc = 0;
 159 
 160         if ((ks = stats_read_stat(st)) != NULL) {
 161                 stats = (cachefs_stats_t *) ks->ks_data;
 162                 rc = stats->st_gc_count;
 163         } else {
 164                 stats_perror(st, SE_KERNEL,
 165                     gettext("Cannot read statistics"));
 166         }
 167 
 168         return (rc);
 169 }
 170 
 171 time_t
 172 stats_gc_time(stats_cookie_t *st)
 173 {
 174         kstat_t *ks;
 175         cachefs_stats_t *stats;
 176         time_t rc = 0;
 177 
 178         if ((ks = stats_read_stat(st)) != NULL) {
 179                 stats = (cachefs_stats_t *) ks->ks_data;
 180                 rc = stats->st_gc_time;
 181         } else {
 182                 stats_perror(st, SE_KERNEL,
 183                     gettext("Cannot read statistics"));
 184         }
 185 
 186         return (rc);
 187 }
 188 
 189 time_t
 190 stats_gc_before(stats_cookie_t *st)
 191 {
 192         kstat_t *ks;
 193         cachefs_stats_t *stats;
 194         time_t rc = 0;
 195 
 196         if ((ks = stats_read_stat(st)) != NULL) {
 197                 stats = (cachefs_stats_t *) ks->ks_data;
 198                 rc = stats->st_gc_before_atime;
 199         } else {
 200                 stats_perror(st, SE_KERNEL,
 201                     gettext("Cannot read statistics"));
 202         }
 203 
 204         return (rc);
 205 }
 206 
 207 time_t
 208 stats_gc_after(stats_cookie_t *st)
 209 {
 210         kstat_t *ks;
 211         cachefs_stats_t *stats;
 212         time_t rc = 0;
 213 
 214         if ((ks = stats_read_stat(st)) != NULL) {
 215                 stats = (cachefs_stats_t *) ks->ks_data;
 216                 rc = stats->st_gc_after_atime;
 217         } else {
 218                 stats_perror(st, SE_KERNEL,
 219                     gettext("Cannot read statistics"));
 220         }
 221 
 222         return (rc);
 223 }
 224 
 225 int
 226 stats_zero_stats(stats_cookie_t *st)
 227 {
 228         cachefs_stats_t stats;
 229         kstat_t *ks;
 230         int rc = 0;
 231         void *memset();
 232 
 233         assert(stats_good(st));
 234         assert(st->st_flags & ST_BOUND);
 235 
 236         if ((ks = stats_read_stat(st)) == NULL) {
 237                 rc = -1;
 238                 goto out;
 239         }
 240 
 241         memset(&stats, '\0', sizeof (stats));
 242         ks->ks_data = &stats;
 243 
 244         if (kstat_write(st->st_kstat_cookie, ks, NULL) < 0) {
 245                 stats_perror(st, SE_KERNEL,
 246                     gettext("Cannot zero statistics"));
 247                 rc = -1;
 248                 goto out;
 249         }
 250 
 251 out:
 252         return (rc);
 253 }