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 /* 23 * Copyright (c) 1999, 2010, Oracle and/or its affiliates. All rights reserved. 24 */ 25 26 /* 27 * Kstat.xs is a Perl XS (eXStension module) that makes the Solaris 28 * kstat(3KSTAT) facility available to Perl scripts. Kstat is a general-purpose 29 * mechanism for providing kernel statistics to users. The Solaris API is 30 * function-based (see the manpage for details), but for ease of use in Perl 31 * scripts this module presents the information as a nested hash data structure. 32 * It would be too inefficient to read every kstat in the system, so this module 33 * uses the Perl TIEHASH mechanism to implement a read-on-demand semantic, which 34 * only reads and updates kstats as and when they are actually accessed. 35 */ 36 37 /* 38 * Ignored raw kstats. 39 * 40 * Some raw kstats are ignored by this module, these are listed below. The 41 * most common reason is that the kstats are stored as arrays and the ks_ndata 42 * and/or ks_data_size fields are invalid. In this case it is impossible to 43 * know how many records are in the array, so they can't be read. 44 * 45 * unix:*:sfmmu_percpu_stat 46 * This is stored as an array with one entry per cpu. Each element is of type 47 * struct sfmmu_percpu_stat. The ks_ndata and ks_data_size fields are bogus. 48 * 49 * ufs directio:*:UFS DirectIO Stats 50 * The structure definition used for these kstats (ufs_directio_kstats) is in a 51 * C file (uts/common/fs/ufs/ufs_directio.c) rather than a header file, so it 52 * isn't accessible. 53 * 54 * qlc:*:statistics 55 * This is a third-party driver for which we don't have source. 56 * 57 * mm:*:phys_installed 58 * This is stored as an array of uint64_t, with each pair of values being the 59 * (address, size) of a memory segment. The ks_ndata and ks_data_size fields 60 * are both zero. 61 * 62 * sockfs:*:sock_unix_list 63 * This is stored as an array with one entry per active socket. Each element 64 * is of type struct k_sockinfo. The ks_ndata and ks_data_size fields are both 65 * zero. 66 * 67 * Note that the ks_ndata and ks_data_size of many non-array raw kstats are 68 * also incorrect. The relevant assertions are therefore commented out in the 69 * appropriate raw kstat read routines. 70 */ 71 72 /* Kstat related includes */ 73 #include <libgen.h> 74 #include <kstat.h> 75 #include <sys/var.h> 76 #include <sys/utsname.h> 77 #include <sys/sysinfo.h> 78 #include <sys/flock.h> 79 #include <sys/dnlc.h> 80 #include <nfs/nfs.h> 81 #include <nfs/nfs_clnt.h> 82 83 /* Ultra-specific kstat includes */ 84 #ifdef __sparc 85 #include <vm/hat_sfmmu.h> /* from /usr/platform/sun4u/include */ 86 #include <sys/simmstat.h> /* from /usr/platform/sun4u/include */ 87 #include <sys/sysctrl.h> /* from /usr/platform/sun4u/include */ 88 #include <sys/fhc.h> /* from /usr/include */ 89 #endif 90 91 /* 92 * Solaris #defines SP, which conflicts with the perl definition of SP 93 * We don't need the Solaris one, so get rid of it to avoid warnings 94 */ 95 #undef SP 96 97 /* Perl XS includes */ 98 #include "EXTERN.h" 99 #include "perl.h" 100 #include "XSUB.h" 101 102 /* Debug macros */ 103 #define DEBUG_ID "Sun::Solaris::Kstat" 104 #ifdef KSTAT_DEBUG 105 #define PERL_ASSERT(EXP) \ 106 ((void)((EXP) || (croak("%s: assertion failed at %s:%d: %s", \ 107 DEBUG_ID, __FILE__, __LINE__, #EXP), 0), 0)) 108 #define PERL_ASSERTMSG(EXP, MSG) \ 109 ((void)((EXP) || (croak(DEBUG_ID ": " MSG), 0), 0)) 110 #else 111 #define PERL_ASSERT(EXP) ((void)0) 112 #define PERL_ASSERTMSG(EXP, MSG) ((void)0) 113 #endif 114 115 /* Macros for saving the contents of KSTAT_RAW structures */ 116 #if defined(HAS_QUAD) && defined(USE_64_BIT_INT) 117 #define NEW_IV(V) \ 118 (newSViv((IVTYPE) V)) 119 #define NEW_UV(V) \ 120 (newSVuv((UVTYPE) V)) 121 #else 122 #define NEW_IV(V) \ 123 (V >= IV_MIN && V <= IV_MAX ? newSViv((IVTYPE) V) : newSVnv((NVTYPE) V)) 124 #if defined(UVTYPE) 125 #define NEW_UV(V) \ 126 (V <= UV_MAX ? newSVuv((UVTYPE) V) : newSVnv((NVTYPE) V)) 127 # else 128 #define NEW_UV(V) \ 129 (V <= IV_MAX ? newSViv((IVTYPE) V) : newSVnv((NVTYPE) V)) 130 #endif 131 #endif 132 #define NEW_HRTIME(V) \ 133 newSVnv((NVTYPE) (V / 1000000000.0)) 134 135 #define SAVE_FNP(H, F, K) \ 136 hv_store(H, K, sizeof (K) - 1, newSViv((IVTYPE) &F), 0) 137 #define SAVE_STRING(H, S, K, SS) \ 138 hv_store(H, #K, sizeof (#K) - 1, \ 139 newSVpvn(S->K, SS ? strlen(S->K) : sizeof(S->K)), 0) 140 #define SAVE_INT32(H, S, K) \ 141 hv_store(H, #K, sizeof (#K) - 1, NEW_IV(S->K), 0) 142 #define SAVE_UINT32(H, S, K) \ 143 hv_store(H, #K, sizeof (#K) - 1, NEW_UV(S->K), 0) 144 #define SAVE_INT64(H, S, K) \ 145 hv_store(H, #K, sizeof (#K) - 1, NEW_IV(S->K), 0) 146 #define SAVE_UINT64(H, S, K) \ 147 hv_store(H, #K, sizeof (#K) - 1, NEW_UV(S->K), 0) 148 #define SAVE_HRTIME(H, S, K) \ 149 hv_store(H, #K, sizeof (#K) - 1, NEW_HRTIME(S->K), 0) 150 151 /* Private structure used for saving kstat info in the tied hashes */ 152 typedef struct { 153 char read; /* Kstat block has been read before */ 154 char valid; /* Kstat still exists in kstat chain */ 155 char strip_str; /* Strip KSTAT_DATA_CHAR fields */ 156 kstat_ctl_t *kstat_ctl; /* Handle returned by kstat_open */ 157 kstat_t *kstat; /* Handle used by kstat_read */ 158 } KstatInfo_t; 159 160 /* typedef for apply_to_ties callback functions */ 161 typedef int (*ATTCb_t)(HV *, void *); 162 163 /* typedef for raw kstat reader functions */ 164 typedef void (*kstat_raw_reader_t)(HV *, kstat_t *, int); 165 166 /* Hash of "module:name" to KSTAT_RAW read functions */ 167 static HV *raw_kstat_lookup; 168 169 /* 170 * Kstats come in two flavours, named and raw. Raw kstats are just C structs, 171 * so we need a function per raw kstat to convert the C struct into the 172 * corresponding perl hash. All such conversion functions are in the following 173 * section. 174 */ 175 176 /* 177 * Definitions in /usr/include/sys/cpuvar.h and /usr/include/sys/sysinfo.h 178 */ 179 180 static void 181 save_cpu_stat(HV *self, kstat_t *kp, int strip_str) 182 { 183 cpu_stat_t *statp; 184 cpu_sysinfo_t *sysinfop; 185 cpu_syswait_t *syswaitp; 186 cpu_vminfo_t *vminfop; 187 188 /* PERL_ASSERT(kp->ks_ndata == 1); */ 189 PERL_ASSERT(kp->ks_data_size == sizeof (cpu_stat_t)); 190 statp = (cpu_stat_t *)(kp->ks_data); 191 sysinfop = &statp->cpu_sysinfo; 192 syswaitp = &statp->cpu_syswait; 193 vminfop = &statp->cpu_vminfo; 194 195 hv_store(self, "idle", 4, NEW_UV(sysinfop->cpu[CPU_IDLE]), 0); 196 hv_store(self, "user", 4, NEW_UV(sysinfop->cpu[CPU_USER]), 0); 197 hv_store(self, "kernel", 6, NEW_UV(sysinfop->cpu[CPU_KERNEL]), 0); 198 hv_store(self, "wait", 4, NEW_UV(sysinfop->cpu[CPU_WAIT]), 0); 199 hv_store(self, "wait_io", 7, NEW_UV(sysinfop->wait[W_IO]), 0); 200 hv_store(self, "wait_swap", 9, NEW_UV(sysinfop->wait[W_SWAP]), 0); 201 hv_store(self, "wait_pio", 8, NEW_UV(sysinfop->wait[W_PIO]), 0); 202 SAVE_UINT32(self, sysinfop, bread); 203 SAVE_UINT32(self, sysinfop, bwrite); 204 SAVE_UINT32(self, sysinfop, lread); 205 SAVE_UINT32(self, sysinfop, lwrite); 206 SAVE_UINT32(self, sysinfop, phread); 207 SAVE_UINT32(self, sysinfop, phwrite); 208 SAVE_UINT32(self, sysinfop, pswitch); 209 SAVE_UINT32(self, sysinfop, trap); 210 SAVE_UINT32(self, sysinfop, intr); 211 SAVE_UINT32(self, sysinfop, syscall); 212 SAVE_UINT32(self, sysinfop, sysread); 213 SAVE_UINT32(self, sysinfop, syswrite); 214 SAVE_UINT32(self, sysinfop, sysfork); 215 SAVE_UINT32(self, sysinfop, sysvfork); 216 SAVE_UINT32(self, sysinfop, sysexec); 217 SAVE_UINT32(self, sysinfop, readch); 218 SAVE_UINT32(self, sysinfop, writech); 219 SAVE_UINT32(self, sysinfop, rcvint); 220 SAVE_UINT32(self, sysinfop, xmtint); 221 SAVE_UINT32(self, sysinfop, mdmint); 222 SAVE_UINT32(self, sysinfop, rawch); 223 SAVE_UINT32(self, sysinfop, canch); 224 SAVE_UINT32(self, sysinfop, outch); 225 SAVE_UINT32(self, sysinfop, msg); 226 SAVE_UINT32(self, sysinfop, sema); 227 SAVE_UINT32(self, sysinfop, namei); 228 SAVE_UINT32(self, sysinfop, ufsiget); 229 SAVE_UINT32(self, sysinfop, ufsdirblk); 230 SAVE_UINT32(self, sysinfop, ufsipage); 231 SAVE_UINT32(self, sysinfop, ufsinopage); 232 SAVE_UINT32(self, sysinfop, inodeovf); 233 SAVE_UINT32(self, sysinfop, fileovf); 234 SAVE_UINT32(self, sysinfop, procovf); 235 SAVE_UINT32(self, sysinfop, intrthread); 236 SAVE_UINT32(self, sysinfop, intrblk); 237 SAVE_UINT32(self, sysinfop, idlethread); 238 SAVE_UINT32(self, sysinfop, inv_swtch); 239 SAVE_UINT32(self, sysinfop, nthreads); 240 SAVE_UINT32(self, sysinfop, cpumigrate); 241 SAVE_UINT32(self, sysinfop, xcalls); 242 SAVE_UINT32(self, sysinfop, mutex_adenters); 243 SAVE_UINT32(self, sysinfop, rw_rdfails); 244 SAVE_UINT32(self, sysinfop, rw_wrfails); 245 SAVE_UINT32(self, sysinfop, modload); 246 SAVE_UINT32(self, sysinfop, modunload); 247 SAVE_UINT32(self, sysinfop, bawrite); 248 #ifdef STATISTICS /* see header file */ 249 SAVE_UINT32(self, sysinfop, rw_enters); 250 SAVE_UINT32(self, sysinfop, win_uo_cnt); 251 SAVE_UINT32(self, sysinfop, win_uu_cnt); 252 SAVE_UINT32(self, sysinfop, win_so_cnt); 253 SAVE_UINT32(self, sysinfop, win_su_cnt); 254 SAVE_UINT32(self, sysinfop, win_suo_cnt); 255 #endif 256 257 SAVE_INT32(self, syswaitp, iowait); 258 SAVE_INT32(self, syswaitp, swap); 259 SAVE_INT32(self, syswaitp, physio); 260 261 SAVE_UINT32(self, vminfop, pgrec); 262 SAVE_UINT32(self, vminfop, pgfrec); 263 SAVE_UINT32(self, vminfop, pgin); 264 SAVE_UINT32(self, vminfop, pgpgin); 265 SAVE_UINT32(self, vminfop, pgout); 266 SAVE_UINT32(self, vminfop, pgpgout); 267 SAVE_UINT32(self, vminfop, swapin); 268 SAVE_UINT32(self, vminfop, pgswapin); 269 SAVE_UINT32(self, vminfop, swapout); 270 SAVE_UINT32(self, vminfop, pgswapout); 271 SAVE_UINT32(self, vminfop, zfod); 272 SAVE_UINT32(self, vminfop, dfree); 273 SAVE_UINT32(self, vminfop, scan); 274 SAVE_UINT32(self, vminfop, rev); 275 SAVE_UINT32(self, vminfop, hat_fault); 276 SAVE_UINT32(self, vminfop, as_fault); 277 SAVE_UINT32(self, vminfop, maj_fault); 278 SAVE_UINT32(self, vminfop, cow_fault); 279 SAVE_UINT32(self, vminfop, prot_fault); 280 SAVE_UINT32(self, vminfop, softlock); 281 SAVE_UINT32(self, vminfop, kernel_asflt); 282 SAVE_UINT32(self, vminfop, pgrrun); 283 SAVE_UINT32(self, vminfop, execpgin); 284 SAVE_UINT32(self, vminfop, execpgout); 285 SAVE_UINT32(self, vminfop, execfree); 286 SAVE_UINT32(self, vminfop, anonpgin); 287 SAVE_UINT32(self, vminfop, anonpgout); 288 SAVE_UINT32(self, vminfop, anonfree); 289 SAVE_UINT32(self, vminfop, fspgin); 290 SAVE_UINT32(self, vminfop, fspgout); 291 SAVE_UINT32(self, vminfop, fsfree); 292 } 293 294 /* 295 * Definitions in /usr/include/sys/var.h 296 */ 297 298 static void 299 save_var(HV *self, kstat_t *kp, int strip_str) 300 { 301 struct var *varp; 302 303 /* PERL_ASSERT(kp->ks_ndata == 1); */ 304 PERL_ASSERT(kp->ks_data_size == sizeof (struct var)); 305 varp = (struct var *)(kp->ks_data); 306 307 SAVE_INT32(self, varp, v_buf); 308 SAVE_INT32(self, varp, v_call); 309 SAVE_INT32(self, varp, v_proc); 310 SAVE_INT32(self, varp, v_maxupttl); 311 SAVE_INT32(self, varp, v_nglobpris); 312 SAVE_INT32(self, varp, v_maxsyspri); 313 SAVE_INT32(self, varp, v_clist); 314 SAVE_INT32(self, varp, v_maxup); 315 SAVE_INT32(self, varp, v_hbuf); 316 SAVE_INT32(self, varp, v_hmask); 317 SAVE_INT32(self, varp, v_pbuf); 318 SAVE_INT32(self, varp, v_sptmap); 319 SAVE_INT32(self, varp, v_maxpmem); 320 SAVE_INT32(self, varp, v_autoup); 321 SAVE_INT32(self, varp, v_bufhwm); 322 } 323 324 /* 325 * Definition in /usr/include/sys/dnlc.h 326 */ 327 328 static void 329 save_ncstats(HV *self, kstat_t *kp, int strip_str) 330 { 331 struct ncstats *ncstatsp; 332 333 /* PERL_ASSERT(kp->ks_ndata == 1); */ 334 PERL_ASSERT(kp->ks_data_size == sizeof (struct ncstats)); 335 ncstatsp = (struct ncstats *)(kp->ks_data); 336 337 SAVE_INT32(self, ncstatsp, hits); 338 SAVE_INT32(self, ncstatsp, misses); 339 SAVE_INT32(self, ncstatsp, enters); 340 SAVE_INT32(self, ncstatsp, dbl_enters); 341 SAVE_INT32(self, ncstatsp, long_enter); 342 SAVE_INT32(self, ncstatsp, long_look); 343 SAVE_INT32(self, ncstatsp, move_to_front); 344 SAVE_INT32(self, ncstatsp, purges); 345 } 346 347 /* 348 * Definition in /usr/include/sys/sysinfo.h 349 */ 350 351 static void 352 save_sysinfo(HV *self, kstat_t *kp, int strip_str) 353 { 354 sysinfo_t *sysinfop; 355 356 /* PERL_ASSERT(kp->ks_ndata == 1); */ 357 PERL_ASSERT(kp->ks_data_size == sizeof (sysinfo_t)); 358 sysinfop = (sysinfo_t *)(kp->ks_data); 359 360 SAVE_UINT32(self, sysinfop, updates); 361 SAVE_UINT32(self, sysinfop, runque); 362 SAVE_UINT32(self, sysinfop, runocc); 363 SAVE_UINT32(self, sysinfop, swpque); 364 SAVE_UINT32(self, sysinfop, swpocc); 365 SAVE_UINT32(self, sysinfop, waiting); 366 } 367 368 /* 369 * Definition in /usr/include/sys/sysinfo.h 370 */ 371 372 static void 373 save_vminfo(HV *self, kstat_t *kp, int strip_str) 374 { 375 vminfo_t *vminfop; 376 377 /* PERL_ASSERT(kp->ks_ndata == 1); */ 378 PERL_ASSERT(kp->ks_data_size == sizeof (vminfo_t)); 379 vminfop = (vminfo_t *)(kp->ks_data); 380 381 SAVE_UINT64(self, vminfop, freemem); 382 SAVE_UINT64(self, vminfop, swap_resv); 383 SAVE_UINT64(self, vminfop, swap_alloc); 384 SAVE_UINT64(self, vminfop, swap_avail); 385 SAVE_UINT64(self, vminfop, swap_free); 386 } 387 388 /* 389 * Definition in /usr/include/nfs/nfs_clnt.h 390 */ 391 392 static void 393 save_nfs(HV *self, kstat_t *kp, int strip_str) 394 { 395 struct mntinfo_kstat *mntinfop; 396 397 /* PERL_ASSERT(kp->ks_ndata == 1); */ 398 PERL_ASSERT(kp->ks_data_size == sizeof (struct mntinfo_kstat)); 399 mntinfop = (struct mntinfo_kstat *)(kp->ks_data); 400 401 SAVE_STRING(self, mntinfop, mik_proto, strip_str); 402 SAVE_UINT32(self, mntinfop, mik_vers); 403 SAVE_UINT32(self, mntinfop, mik_flags); 404 SAVE_UINT32(self, mntinfop, mik_secmod); 405 SAVE_UINT32(self, mntinfop, mik_curread); 406 SAVE_UINT32(self, mntinfop, mik_curwrite); 407 SAVE_INT32(self, mntinfop, mik_timeo); 408 SAVE_INT32(self, mntinfop, mik_retrans); 409 SAVE_UINT32(self, mntinfop, mik_acregmin); 410 SAVE_UINT32(self, mntinfop, mik_acregmax); 411 SAVE_UINT32(self, mntinfop, mik_acdirmin); 412 SAVE_UINT32(self, mntinfop, mik_acdirmax); 413 hv_store(self, "lookup_srtt", 11, 414 NEW_UV(mntinfop->mik_timers[0].srtt), 0); 415 hv_store(self, "lookup_deviate", 14, 416 NEW_UV(mntinfop->mik_timers[0].deviate), 0); 417 hv_store(self, "lookup_rtxcur", 13, 418 NEW_UV(mntinfop->mik_timers[0].rtxcur), 0); 419 hv_store(self, "read_srtt", 9, 420 NEW_UV(mntinfop->mik_timers[1].srtt), 0); 421 hv_store(self, "read_deviate", 12, 422 NEW_UV(mntinfop->mik_timers[1].deviate), 0); 423 hv_store(self, "read_rtxcur", 11, 424 NEW_UV(mntinfop->mik_timers[1].rtxcur), 0); 425 hv_store(self, "write_srtt", 10, 426 NEW_UV(mntinfop->mik_timers[2].srtt), 0); 427 hv_store(self, "write_deviate", 13, 428 NEW_UV(mntinfop->mik_timers[2].deviate), 0); 429 hv_store(self, "write_rtxcur", 12, 430 NEW_UV(mntinfop->mik_timers[2].rtxcur), 0); 431 SAVE_UINT32(self, mntinfop, mik_noresponse); 432 SAVE_UINT32(self, mntinfop, mik_failover); 433 SAVE_UINT32(self, mntinfop, mik_remap); 434 SAVE_STRING(self, mntinfop, mik_curserver, strip_str); 435 } 436 437 /* 438 * The following struct => hash functions are all only present on the sparc 439 * platform, so they are all conditionally compiled depending on __sparc 440 */ 441 442 /* 443 * Definition in /usr/platform/sun4u/include/vm/hat_sfmmu.h 444 */ 445 446 #ifdef __sparc 447 static void 448 save_sfmmu_global_stat(HV *self, kstat_t *kp, int strip_str) 449 { 450 struct sfmmu_global_stat *sfmmugp; 451 452 /* PERL_ASSERT(kp->ks_ndata == 1); */ 453 PERL_ASSERT(kp->ks_data_size == sizeof (struct sfmmu_global_stat)); 454 sfmmugp = (struct sfmmu_global_stat *)(kp->ks_data); 455 456 SAVE_INT32(self, sfmmugp, sf_tsb_exceptions); 457 SAVE_INT32(self, sfmmugp, sf_tsb_raise_exception); 458 SAVE_INT32(self, sfmmugp, sf_pagefaults); 459 SAVE_INT32(self, sfmmugp, sf_uhash_searches); 460 SAVE_INT32(self, sfmmugp, sf_uhash_links); 461 SAVE_INT32(self, sfmmugp, sf_khash_searches); 462 SAVE_INT32(self, sfmmugp, sf_khash_links); 463 SAVE_INT32(self, sfmmugp, sf_swapout); 464 SAVE_INT32(self, sfmmugp, sf_tsb_alloc); 465 SAVE_INT32(self, sfmmugp, sf_tsb_allocfail); 466 SAVE_INT32(self, sfmmugp, sf_tsb_sectsb_create); 467 SAVE_INT32(self, sfmmugp, sf_scd_1sttsb_alloc); 468 SAVE_INT32(self, sfmmugp, sf_scd_2ndtsb_alloc); 469 SAVE_INT32(self, sfmmugp, sf_scd_1sttsb_allocfail); 470 SAVE_INT32(self, sfmmugp, sf_scd_2ndtsb_allocfail); 471 SAVE_INT32(self, sfmmugp, sf_tteload8k); 472 SAVE_INT32(self, sfmmugp, sf_tteload64k); 473 SAVE_INT32(self, sfmmugp, sf_tteload512k); 474 SAVE_INT32(self, sfmmugp, sf_tteload4m); 475 SAVE_INT32(self, sfmmugp, sf_tteload32m); 476 SAVE_INT32(self, sfmmugp, sf_tteload256m); 477 SAVE_INT32(self, sfmmugp, sf_tsb_load8k); 478 SAVE_INT32(self, sfmmugp, sf_tsb_load4m); 479 SAVE_INT32(self, sfmmugp, sf_hblk_hit); 480 SAVE_INT32(self, sfmmugp, sf_hblk8_ncreate); 481 SAVE_INT32(self, sfmmugp, sf_hblk8_nalloc); 482 SAVE_INT32(self, sfmmugp, sf_hblk1_ncreate); 483 SAVE_INT32(self, sfmmugp, sf_hblk1_nalloc); 484 SAVE_INT32(self, sfmmugp, sf_hblk_slab_cnt); 485 SAVE_INT32(self, sfmmugp, sf_hblk_reserve_cnt); 486 SAVE_INT32(self, sfmmugp, sf_hblk_recurse_cnt); 487 SAVE_INT32(self, sfmmugp, sf_hblk_reserve_hit); 488 SAVE_INT32(self, sfmmugp, sf_get_free_success); 489 SAVE_INT32(self, sfmmugp, sf_get_free_throttle); 490 SAVE_INT32(self, sfmmugp, sf_get_free_fail); 491 SAVE_INT32(self, sfmmugp, sf_put_free_success); 492 SAVE_INT32(self, sfmmugp, sf_put_free_fail); 493 SAVE_INT32(self, sfmmugp, sf_pgcolor_conflict); 494 SAVE_INT32(self, sfmmugp, sf_uncache_conflict); 495 SAVE_INT32(self, sfmmugp, sf_unload_conflict); 496 SAVE_INT32(self, sfmmugp, sf_ism_uncache); 497 SAVE_INT32(self, sfmmugp, sf_ism_recache); 498 SAVE_INT32(self, sfmmugp, sf_recache); 499 SAVE_INT32(self, sfmmugp, sf_steal_count); 500 SAVE_INT32(self, sfmmugp, sf_pagesync); 501 SAVE_INT32(self, sfmmugp, sf_clrwrt); 502 SAVE_INT32(self, sfmmugp, sf_pagesync_invalid); 503 SAVE_INT32(self, sfmmugp, sf_kernel_xcalls); 504 SAVE_INT32(self, sfmmugp, sf_user_xcalls); 505 SAVE_INT32(self, sfmmugp, sf_tsb_grow); 506 SAVE_INT32(self, sfmmugp, sf_tsb_shrink); 507 SAVE_INT32(self, sfmmugp, sf_tsb_resize_failures); 508 SAVE_INT32(self, sfmmugp, sf_tsb_reloc); 509 SAVE_INT32(self, sfmmugp, sf_user_vtop); 510 SAVE_INT32(self, sfmmugp, sf_ctx_inv); 511 SAVE_INT32(self, sfmmugp, sf_tlb_reprog_pgsz); 512 SAVE_INT32(self, sfmmugp, sf_region_remap_demap); 513 SAVE_INT32(self, sfmmugp, sf_create_scd); 514 SAVE_INT32(self, sfmmugp, sf_join_scd); 515 SAVE_INT32(self, sfmmugp, sf_leave_scd); 516 SAVE_INT32(self, sfmmugp, sf_destroy_scd); 517 } 518 #endif 519 520 /* 521 * Definition in /usr/platform/sun4u/include/vm/hat_sfmmu.h 522 */ 523 524 #ifdef __sparc 525 static void 526 save_sfmmu_tsbsize_stat(HV *self, kstat_t *kp, int strip_str) 527 { 528 struct sfmmu_tsbsize_stat *sfmmutp; 529 530 /* PERL_ASSERT(kp->ks_ndata == 1); */ 531 PERL_ASSERT(kp->ks_data_size == sizeof (struct sfmmu_tsbsize_stat)); 532 sfmmutp = (struct sfmmu_tsbsize_stat *)(kp->ks_data); 533 534 SAVE_INT32(self, sfmmutp, sf_tsbsz_8k); 535 SAVE_INT32(self, sfmmutp, sf_tsbsz_16k); 536 SAVE_INT32(self, sfmmutp, sf_tsbsz_32k); 537 SAVE_INT32(self, sfmmutp, sf_tsbsz_64k); 538 SAVE_INT32(self, sfmmutp, sf_tsbsz_128k); 539 SAVE_INT32(self, sfmmutp, sf_tsbsz_256k); 540 SAVE_INT32(self, sfmmutp, sf_tsbsz_512k); 541 SAVE_INT32(self, sfmmutp, sf_tsbsz_1m); 542 SAVE_INT32(self, sfmmutp, sf_tsbsz_2m); 543 SAVE_INT32(self, sfmmutp, sf_tsbsz_4m); 544 } 545 #endif 546 547 /* 548 * Definition in /usr/platform/sun4u/include/sys/simmstat.h 549 */ 550 551 #ifdef __sparc 552 static void 553 save_simmstat(HV *self, kstat_t *kp, int strip_str) 554 { 555 uchar_t *simmstatp; 556 SV *list; 557 int i; 558 559 /* PERL_ASSERT(kp->ks_ndata == 1); */ 560 PERL_ASSERT(kp->ks_data_size == sizeof (uchar_t) * SIMM_COUNT); 561 562 list = newSVpv("", 0); 563 for (i = 0, simmstatp = (uchar_t *)(kp->ks_data); 564 i < SIMM_COUNT - 1; i++, simmstatp++) { 565 sv_catpvf(list, "%d,", *simmstatp); 566 } 567 sv_catpvf(list, "%d", *simmstatp); 568 hv_store(self, "status", 6, list, 0); 569 } 570 #endif 571 572 /* 573 * Used by save_temperature to make CSV lists from arrays of 574 * short temperature values 575 */ 576 577 #ifdef __sparc 578 static SV * 579 short_array_to_SV(short *shortp, int len) 580 { 581 SV *list; 582 583 list = newSVpv("", 0); 584 for (; len > 1; len--, shortp++) { 585 sv_catpvf(list, "%d,", *shortp); 586 } 587 sv_catpvf(list, "%d", *shortp); 588 return (list); 589 } 590 591 /* 592 * Definition in /usr/platform/sun4u/include/sys/fhc.h 593 */ 594 595 static void 596 save_temperature(HV *self, kstat_t *kp, int strip_str) 597 { 598 struct temp_stats *tempsp; 599 600 /* PERL_ASSERT(kp->ks_ndata == 1); */ 601 PERL_ASSERT(kp->ks_data_size == sizeof (struct temp_stats)); 602 tempsp = (struct temp_stats *)(kp->ks_data); 603 604 SAVE_UINT32(self, tempsp, index); 605 hv_store(self, "l1", 2, short_array_to_SV(tempsp->l1, L1_SZ), 0); 606 hv_store(self, "l2", 2, short_array_to_SV(tempsp->l2, L2_SZ), 0); 607 hv_store(self, "l3", 2, short_array_to_SV(tempsp->l3, L3_SZ), 0); 608 hv_store(self, "l4", 2, short_array_to_SV(tempsp->l4, L4_SZ), 0); 609 hv_store(self, "l5", 2, short_array_to_SV(tempsp->l5, L5_SZ), 0); 610 SAVE_INT32(self, tempsp, max); 611 SAVE_INT32(self, tempsp, min); 612 SAVE_INT32(self, tempsp, state); 613 SAVE_INT32(self, tempsp, temp_cnt); 614 SAVE_INT32(self, tempsp, shutdown_cnt); 615 SAVE_INT32(self, tempsp, version); 616 SAVE_INT32(self, tempsp, trend); 617 SAVE_INT32(self, tempsp, override); 618 } 619 #endif 620 621 /* 622 * Not actually defined anywhere - just a short. Yuck. 623 */ 624 625 #ifdef __sparc 626 static void 627 save_temp_over(HV *self, kstat_t *kp, int strip_str) 628 { 629 short *shortp; 630 631 /* PERL_ASSERT(kp->ks_ndata == 1); */ 632 PERL_ASSERT(kp->ks_data_size == sizeof (short)); 633 634 shortp = (short *)(kp->ks_data); 635 hv_store(self, "override", 8, newSViv(*shortp), 0); 636 } 637 #endif 638 639 /* 640 * Defined in /usr/platform/sun4u/include/sys/sysctrl.h 641 * (Well, sort of. Actually there's no structure, just a list of #defines 642 * enumerating *some* of the array indexes.) 643 */ 644 645 #ifdef __sparc 646 static void 647 save_ps_shadow(HV *self, kstat_t *kp, int strip_str) 648 { 649 uchar_t *ucharp; 650 651 /* PERL_ASSERT(kp->ks_ndata == 1); */ 652 PERL_ASSERT(kp->ks_data_size == SYS_PS_COUNT); 653 654 ucharp = (uchar_t *)(kp->ks_data); 655 hv_store(self, "core_0", 6, newSViv(*ucharp++), 0); 656 hv_store(self, "core_1", 6, newSViv(*ucharp++), 0); 657 hv_store(self, "core_2", 6, newSViv(*ucharp++), 0); 658 hv_store(self, "core_3", 6, newSViv(*ucharp++), 0); 659 hv_store(self, "core_4", 6, newSViv(*ucharp++), 0); 660 hv_store(self, "core_5", 6, newSViv(*ucharp++), 0); 661 hv_store(self, "core_6", 6, newSViv(*ucharp++), 0); 662 hv_store(self, "core_7", 6, newSViv(*ucharp++), 0); 663 hv_store(self, "pps_0", 5, newSViv(*ucharp++), 0); 664 hv_store(self, "clk_33", 6, newSViv(*ucharp++), 0); 665 hv_store(self, "clk_50", 6, newSViv(*ucharp++), 0); 666 hv_store(self, "v5_p", 4, newSViv(*ucharp++), 0); 667 hv_store(self, "v12_p", 5, newSViv(*ucharp++), 0); 668 hv_store(self, "v5_aux", 6, newSViv(*ucharp++), 0); 669 hv_store(self, "v5_p_pch", 8, newSViv(*ucharp++), 0); 670 hv_store(self, "v12_p_pch", 9, newSViv(*ucharp++), 0); 671 hv_store(self, "v3_pch", 6, newSViv(*ucharp++), 0); 672 hv_store(self, "v5_pch", 6, newSViv(*ucharp++), 0); 673 hv_store(self, "p_fan", 5, newSViv(*ucharp++), 0); 674 } 675 #endif 676 677 /* 678 * Definition in /usr/platform/sun4u/include/sys/fhc.h 679 */ 680 681 #ifdef __sparc 682 static void 683 save_fault_list(HV *self, kstat_t *kp, int strip_str) 684 { 685 struct ft_list *faultp; 686 int i; 687 char name[KSTAT_STRLEN + 7]; /* room for 999999 faults */ 688 689 /* PERL_ASSERT(kp->ks_ndata == 1); */ 690 /* PERL_ASSERT(kp->ks_data_size == sizeof (struct ft_list)); */ 691 692 for (i = 1, faultp = (struct ft_list *)(kp->ks_data); 693 i <= 999999 && i <= kp->ks_data_size / sizeof (struct ft_list); 694 i++, faultp++) { 695 (void) snprintf(name, sizeof (name), "unit_%d", i); 696 hv_store(self, name, strlen(name), newSViv(faultp->unit), 0); 697 (void) snprintf(name, sizeof (name), "type_%d", i); 698 hv_store(self, name, strlen(name), newSViv(faultp->type), 0); 699 (void) snprintf(name, sizeof (name), "fclass_%d", i); 700 hv_store(self, name, strlen(name), newSViv(faultp->fclass), 0); 701 (void) snprintf(name, sizeof (name), "create_time_%d", i); 702 hv_store(self, name, strlen(name), 703 NEW_UV(faultp->create_time), 0); 704 (void) snprintf(name, sizeof (name), "msg_%d", i); 705 hv_store(self, name, strlen(name), newSVpv(faultp->msg, 0), 0); 706 } 707 } 708 #endif 709 710 /* 711 * We need to be able to find the function corresponding to a particular raw 712 * kstat. To do this we ignore the instance and glue the module and name 713 * together to form a composite key. We can then use the data in the kstat 714 * structure to find the appropriate function. We use a perl hash to manage the 715 * lookup, where the key is "module:name" and the value is a pointer to the 716 * appropriate C function. 717 * 718 * Note that some kstats include the instance number as part of the module 719 * and/or name. This could be construed as a bug. However, to work around this 720 * we omit any digits from the module and name as we build the table in 721 * build_raw_kstat_loopup(), and we remove any digits from the module and name 722 * when we look up the functions in lookup_raw_kstat_fn() 723 */ 724 725 /* 726 * This function is called when the XS is first dlopen()ed, and builds the 727 * lookup table as described above. 728 */ 729 730 static void 731 build_raw_kstat_lookup() 732 { 733 /* Create new hash */ 734 raw_kstat_lookup = newHV(); 735 736 SAVE_FNP(raw_kstat_lookup, save_cpu_stat, "cpu_stat:cpu_stat"); 737 SAVE_FNP(raw_kstat_lookup, save_var, "unix:var"); 738 SAVE_FNP(raw_kstat_lookup, save_ncstats, "unix:ncstats"); 739 SAVE_FNP(raw_kstat_lookup, save_sysinfo, "unix:sysinfo"); 740 SAVE_FNP(raw_kstat_lookup, save_vminfo, "unix:vminfo"); 741 SAVE_FNP(raw_kstat_lookup, save_nfs, "nfs:mntinfo"); 742 #ifdef __sparc 743 SAVE_FNP(raw_kstat_lookup, save_sfmmu_global_stat, 744 "unix:sfmmu_global_stat"); 745 SAVE_FNP(raw_kstat_lookup, save_sfmmu_tsbsize_stat, 746 "unix:sfmmu_tsbsize_stat"); 747 SAVE_FNP(raw_kstat_lookup, save_simmstat, "unix:simm-status"); 748 SAVE_FNP(raw_kstat_lookup, save_temperature, "unix:temperature"); 749 SAVE_FNP(raw_kstat_lookup, save_temp_over, "unix:temperature override"); 750 SAVE_FNP(raw_kstat_lookup, save_ps_shadow, "unix:ps_shadow"); 751 SAVE_FNP(raw_kstat_lookup, save_fault_list, "unix:fault_list"); 752 #endif 753 } 754 755 /* 756 * This finds and returns the raw kstat reader function corresponding to the 757 * supplied module and name. If no matching function exists, 0 is returned. 758 */ 759 760 static kstat_raw_reader_t lookup_raw_kstat_fn(char *module, char *name) 761 { 762 char key[KSTAT_STRLEN * 2]; 763 register char *f, *t; 764 SV **entry; 765 kstat_raw_reader_t fnp; 766 767 /* Copy across module & name, removing any digits - see comment above */ 768 for (f = module, t = key; *f != '\0'; f++, t++) { 769 while (*f != '\0' && isdigit(*f)) { f++; } 770 *t = *f; 771 } 772 *t++ = ':'; 773 for (f = name; *f != '\0'; f++, t++) { 774 while (*f != '\0' && isdigit(*f)) { 775 f++; 776 } 777 *t = *f; 778 } 779 *t = '\0'; 780 781 /* look up & return the function, or teturn 0 if not found */ 782 if ((entry = hv_fetch(raw_kstat_lookup, key, strlen(key), FALSE)) == 0) 783 { 784 fnp = 0; 785 } else { 786 fnp = (kstat_raw_reader_t)(uintptr_t)SvIV(*entry); 787 } 788 return (fnp); 789 } 790 791 /* 792 * This module converts the flat list returned by kstat_read() into a perl hash 793 * tree keyed on module, instance, name and statistic. The following functions 794 * provide code to create the nested hashes, and to iterate over them. 795 */ 796 797 /* 798 * Given module, instance and name keys return a pointer to the hash tied to 799 * the bottommost hash. If the hash already exists, we just return a pointer 800 * to it, otherwise we create the hash and any others also required above it in 801 * the hierarchy. The returned tiehash is blessed into the 802 * Sun::Solaris::Kstat::_Stat class, so that the appropriate TIEHASH methods are 803 * called when the bottommost hash is accessed. If the is_new parameter is 804 * non-null it will be set to TRUE if a new tie has been created, and FALSE if 805 * the tie already existed. 806 */ 807 808 static HV * 809 get_tie(SV *self, char *module, int instance, char *name, int *is_new) 810 { 811 char str_inst[11]; /* big enough for up to 10^10 instances */ 812 char *key[3]; /* 3 part key: module, instance, name */ 813 int k; 814 int new; 815 HV *hash; 816 HV *tie; 817 818 /* Create the keys */ 819 (void) snprintf(str_inst, sizeof (str_inst), "%d", instance); 820 key[0] = module; 821 key[1] = str_inst; 822 key[2] = name; 823 824 /* Iteratively descend the tree, creating new hashes as required */ 825 hash = (HV *)SvRV(self); 826 for (k = 0; k < 3; k++) { 827 SV **entry; 828 829 SvREADONLY_off(hash); 830 entry = hv_fetch(hash, key[k], strlen(key[k]), TRUE); 831 832 /* If the entry doesn't exist, create it */ 833 if (! SvOK(*entry)) { 834 HV *newhash; 835 SV *rv; 836 837 newhash = newHV(); 838 rv = newRV_noinc((SV *)newhash); 839 sv_setsv(*entry, rv); 840 SvREFCNT_dec(rv); 841 if (k < 2) { 842 SvREADONLY_on(newhash); 843 } 844 SvREADONLY_on(*entry); 845 SvREADONLY_on(hash); 846 hash = newhash; 847 new = 1; 848 849 /* Otherwise it already existed */ 850 } else { 851 SvREADONLY_on(hash); 852 hash = (HV *)SvRV(*entry); 853 new = 0; 854 } 855 } 856 857 /* Create and bless a hash for the tie, if necessary */ 858 if (new) { 859 SV *tieref; 860 HV *stash; 861 862 tie = newHV(); 863 tieref = newRV_noinc((SV *)tie); 864 stash = gv_stashpv("Sun::Solaris::Kstat::_Stat", TRUE); 865 sv_bless(tieref, stash); 866 867 /* Add TIEHASH magic */ 868 hv_magic(hash, (GV *)tieref, 'P'); 869 SvREADONLY_on(hash); 870 871 /* Otherwise, just find the existing tied hash */ 872 } else { 873 MAGIC *mg; 874 875 mg = mg_find((SV *)hash, 'P'); 876 PERL_ASSERTMSG(mg != 0, "get_tie: lost P magic"); 877 tie = (HV *)SvRV(mg->mg_obj); 878 } 879 if (is_new) { 880 *is_new = new; 881 } 882 return (tie); 883 } 884 885 /* 886 * This is an iterator function used to traverse the hash hierarchy and apply 887 * the passed function to the tied hashes at the bottom of the hierarchy. If 888 * any of the callback functions return 0, 0 is returned, otherwise 1 889 */ 890 891 static int 892 apply_to_ties(SV *self, ATTCb_t cb, void *arg) 893 { 894 HV *hash1; 895 HE *entry1; 896 long s; 897 int ret; 898 899 hash1 = (HV *)SvRV(self); 900 hv_iterinit(hash1); 901 ret = 1; 902 903 /* Iterate over each module */ 904 while (entry1 = hv_iternext(hash1)) { 905 HV *hash2; 906 HE *entry2; 907 908 hash2 = (HV *)SvRV(hv_iterval(hash1, entry1)); 909 hv_iterinit(hash2); 910 911 /* Iterate over each module:instance */ 912 while (entry2 = hv_iternext(hash2)) { 913 HV *hash3; 914 HE *entry3; 915 916 hash3 = (HV *)SvRV(hv_iterval(hash2, entry2)); 917 hv_iterinit(hash3); 918 919 /* Iterate over each module:instance:name */ 920 while (entry3 = hv_iternext(hash3)) { 921 HV *hash4; 922 MAGIC *mg; 923 HV *tie; 924 925 /* Get the tie */ 926 hash4 = (HV *)SvRV(hv_iterval(hash3, entry3)); 927 mg = mg_find((SV *)hash4, 'P'); 928 PERL_ASSERTMSG(mg != 0, 929 "apply_to_ties: lost P magic"); 930 931 /* Apply the callback */ 932 if (! cb((HV *)SvRV(mg->mg_obj), arg)) { 933 ret = 0; 934 } 935 } 936 } 937 } 938 return (ret); 939 } 940 941 /* 942 * Mark this HV as valid - used by update() when pruning deleted kstat nodes 943 */ 944 945 static int 946 set_valid(HV *self, void *arg) 947 { 948 MAGIC *mg; 949 950 mg = mg_find((SV *)self, '~'); 951 PERL_ASSERTMSG(mg != 0, "set_valid: lost ~ magic"); 952 ((KstatInfo_t *)SvPVX(mg->mg_obj))->valid = (int)arg; 953 return (1); 954 } 955 956 /* 957 * Prune invalid kstat nodes. This is called when kstat_chain_update() detects 958 * that the kstat chain has been updated. This removes any hash tree entries 959 * that no longer have a corresponding kstat. If del is non-null it will be 960 * set to the keys of the deleted kstat nodes, if any. If any entries are 961 * deleted 1 will be retured, otherwise 0 962 */ 963 964 static int 965 prune_invalid(SV *self, AV *del) 966 { 967 HV *hash1; 968 HE *entry1; 969 STRLEN klen; 970 char *module, *instance, *name, *key; 971 int ret; 972 973 hash1 = (HV *)SvRV(self); 974 hv_iterinit(hash1); 975 ret = 0; 976 977 /* Iterate over each module */ 978 while (entry1 = hv_iternext(hash1)) { 979 HV *hash2; 980 HE *entry2; 981 982 module = HePV(entry1, PL_na); 983 hash2 = (HV *)SvRV(hv_iterval(hash1, entry1)); 984 hv_iterinit(hash2); 985 986 /* Iterate over each module:instance */ 987 while (entry2 = hv_iternext(hash2)) { 988 HV *hash3; 989 HE *entry3; 990 991 instance = HePV(entry2, PL_na); 992 hash3 = (HV *)SvRV(hv_iterval(hash2, entry2)); 993 hv_iterinit(hash3); 994 995 /* Iterate over each module:instance:name */ 996 while (entry3 = hv_iternext(hash3)) { 997 HV *hash4; 998 MAGIC *mg; 999 HV *tie; 1000 1001 name = HePV(entry3, PL_na); 1002 hash4 = (HV *)SvRV(hv_iterval(hash3, entry3)); 1003 mg = mg_find((SV *)hash4, 'P'); 1004 PERL_ASSERTMSG(mg != 0, 1005 "prune_invalid: lost P magic"); 1006 tie = (HV *)SvRV(mg->mg_obj); 1007 mg = mg_find((SV *)tie, '~'); 1008 PERL_ASSERTMSG(mg != 0, 1009 "prune_invalid: lost ~ magic"); 1010 1011 /* If this is marked as invalid, prune it */ 1012 if (((KstatInfo_t *)SvPVX( 1013 (SV *)mg->mg_obj))->valid == FALSE) { 1014 SvREADONLY_off(hash3); 1015 key = HePV(entry3, klen); 1016 hv_delete(hash3, key, klen, G_DISCARD); 1017 SvREADONLY_on(hash3); 1018 if (del) { 1019 av_push(del, 1020 newSVpvf("%s:%s:%s", 1021 module, instance, name)); 1022 } 1023 ret = 1; 1024 } 1025 } 1026 1027 /* If the module:instance:name hash is empty prune it */ 1028 if (HvKEYS(hash3) == 0) { 1029 SvREADONLY_off(hash2); 1030 key = HePV(entry2, klen); 1031 hv_delete(hash2, key, klen, G_DISCARD); 1032 SvREADONLY_on(hash2); 1033 } 1034 } 1035 /* If the module:instance hash is empty prune it */ 1036 if (HvKEYS(hash2) == 0) { 1037 SvREADONLY_off(hash1); 1038 key = HePV(entry1, klen); 1039 hv_delete(hash1, key, klen, G_DISCARD); 1040 SvREADONLY_on(hash1); 1041 } 1042 } 1043 return (ret); 1044 } 1045 1046 /* 1047 * Named kstats are returned as a list of key/values. This function converts 1048 * such a list into the equivalent perl datatypes, and stores them in the passed 1049 * hash. 1050 */ 1051 1052 static void 1053 save_named(HV *self, kstat_t *kp, int strip_str) 1054 { 1055 kstat_named_t *knp; 1056 int n; 1057 SV* value; 1058 1059 for (n = kp->ks_ndata, knp = KSTAT_NAMED_PTR(kp); n > 0; n--, knp++) { 1060 switch (knp->data_type) { 1061 case KSTAT_DATA_CHAR: 1062 value = newSVpv(knp->value.c, strip_str ? 1063 strlen(knp->value.c) : sizeof (knp->value.c)); 1064 break; 1065 case KSTAT_DATA_INT32: 1066 value = newSViv(knp->value.i32); 1067 break; 1068 case KSTAT_DATA_UINT32: 1069 value = NEW_UV(knp->value.ui32); 1070 break; 1071 case KSTAT_DATA_INT64: 1072 value = NEW_UV(knp->value.i64); 1073 break; 1074 case KSTAT_DATA_UINT64: 1075 value = NEW_UV(knp->value.ui64); 1076 break; 1077 case KSTAT_DATA_STRING: 1078 if (KSTAT_NAMED_STR_PTR(knp) == NULL) 1079 value = newSVpv("null", sizeof ("null") - 1); 1080 else 1081 value = newSVpv(KSTAT_NAMED_STR_PTR(knp), 1082 KSTAT_NAMED_STR_BUFLEN(knp) -1); 1083 break; 1084 default: 1085 PERL_ASSERTMSG(0, "kstat_read: invalid data type"); 1086 break; 1087 } 1088 hv_store(self, knp->name, strlen(knp->name), value, 0); 1089 } 1090 } 1091 1092 /* 1093 * Save kstat interrupt statistics 1094 */ 1095 1096 static void 1097 save_intr(HV *self, kstat_t *kp, int strip_str) 1098 { 1099 kstat_intr_t *kintrp; 1100 int i; 1101 static char *intr_names[] = 1102 { "hard", "soft", "watchdog", "spurious", "multiple_service" }; 1103 1104 PERL_ASSERT(kp->ks_ndata == 1); 1105 PERL_ASSERT(kp->ks_data_size == sizeof (kstat_intr_t)); 1106 kintrp = KSTAT_INTR_PTR(kp); 1107 1108 for (i = 0; i < KSTAT_NUM_INTRS; i++) { 1109 hv_store(self, intr_names[i], strlen(intr_names[i]), 1110 NEW_UV(kintrp->intrs[i]), 0); 1111 } 1112 } 1113 1114 /* 1115 * Save IO statistics 1116 */ 1117 1118 static void 1119 save_io(HV *self, kstat_t *kp, int strip_str) 1120 { 1121 kstat_io_t *kiop; 1122 1123 PERL_ASSERT(kp->ks_ndata == 1); 1124 PERL_ASSERT(kp->ks_data_size == sizeof (kstat_io_t)); 1125 kiop = KSTAT_IO_PTR(kp); 1126 SAVE_UINT64(self, kiop, nread); 1127 SAVE_UINT64(self, kiop, nwritten); 1128 SAVE_UINT32(self, kiop, reads); 1129 SAVE_UINT32(self, kiop, writes); 1130 SAVE_HRTIME(self, kiop, wtime); 1131 SAVE_HRTIME(self, kiop, wlentime); 1132 SAVE_HRTIME(self, kiop, wlastupdate); 1133 SAVE_HRTIME(self, kiop, rtime); 1134 SAVE_HRTIME(self, kiop, rlentime); 1135 SAVE_HRTIME(self, kiop, rlastupdate); 1136 SAVE_UINT32(self, kiop, wcnt); 1137 SAVE_UINT32(self, kiop, rcnt); 1138 } 1139 1140 /* 1141 * Save timer statistics 1142 */ 1143 1144 static void 1145 save_timer(HV *self, kstat_t *kp, int strip_str) 1146 { 1147 kstat_timer_t *ktimerp; 1148 1149 PERL_ASSERT(kp->ks_ndata == 1); 1150 PERL_ASSERT(kp->ks_data_size == sizeof (kstat_timer_t)); 1151 ktimerp = KSTAT_TIMER_PTR(kp); 1152 SAVE_STRING(self, ktimerp, name, strip_str); 1153 SAVE_UINT64(self, ktimerp, num_events); 1154 SAVE_HRTIME(self, ktimerp, elapsed_time); 1155 SAVE_HRTIME(self, ktimerp, min_time); 1156 SAVE_HRTIME(self, ktimerp, max_time); 1157 SAVE_HRTIME(self, ktimerp, start_time); 1158 SAVE_HRTIME(self, ktimerp, stop_time); 1159 } 1160 1161 /* 1162 * Read kstats and copy into the supplied perl hash structure. If refresh is 1163 * true, this function is being called as part of the update() method. In this 1164 * case it is only necessary to read the kstats if they have previously been 1165 * accessed (kip->read == TRUE). If refresh is false, this function is being 1166 * called prior to returning a value to the caller. In this case, it is only 1167 * necessary to read the kstats if they have not previously been read. If the 1168 * kstat_read() fails, 0 is returned, otherwise 1 1169 */ 1170 1171 static int 1172 read_kstats(HV *self, int refresh) 1173 { 1174 MAGIC *mg; 1175 KstatInfo_t *kip; 1176 kstat_raw_reader_t fnp; 1177 1178 /* Find the MAGIC KstatInfo_t data structure */ 1179 mg = mg_find((SV *)self, '~'); 1180 PERL_ASSERTMSG(mg != 0, "read_kstats: lost ~ magic"); 1181 kip = (KstatInfo_t *)SvPVX(mg->mg_obj); 1182 1183 /* Return early if we don't need to actually read the kstats */ 1184 if ((refresh && ! kip->read) || (! refresh && kip->read)) { 1185 return (1); 1186 } 1187 1188 /* Read the kstats and return 0 if this fails */ 1189 if (kstat_read(kip->kstat_ctl, kip->kstat, NULL) < 0) { 1190 return (0); 1191 } 1192 1193 /* Save the read data */ 1194 hv_store(self, "snaptime", 8, NEW_HRTIME(kip->kstat->ks_snaptime), 0); 1195 switch (kip->kstat->ks_type) { 1196 case KSTAT_TYPE_RAW: 1197 if ((fnp = lookup_raw_kstat_fn(kip->kstat->ks_module, 1198 kip->kstat->ks_name)) != 0) { 1199 fnp(self, kip->kstat, kip->strip_str); 1200 } 1201 break; 1202 case KSTAT_TYPE_NAMED: 1203 save_named(self, kip->kstat, kip->strip_str); 1204 break; 1205 case KSTAT_TYPE_INTR: 1206 save_intr(self, kip->kstat, kip->strip_str); 1207 break; 1208 case KSTAT_TYPE_IO: 1209 save_io(self, kip->kstat, kip->strip_str); 1210 break; 1211 case KSTAT_TYPE_TIMER: 1212 save_timer(self, kip->kstat, kip->strip_str); 1213 break; 1214 default: 1215 PERL_ASSERTMSG(0, "read_kstats: illegal kstat type"); 1216 break; 1217 } 1218 kip->read = TRUE; 1219 return (1); 1220 } 1221 1222 /* 1223 * The XS code exported to perl is below here. Note that the XS preprocessor 1224 * has its own commenting syntax, so all comments from this point on are in 1225 * that form. 1226 */ 1227 1228 /* The following XS methods are the ABI of the Sun::Solaris::Kstat package */ 1229 1230 MODULE = Sun::Solaris::Kstat PACKAGE = Sun::Solaris::Kstat 1231 PROTOTYPES: ENABLE 1232 1233 # Create the raw kstat to store function lookup table on load 1234 BOOT: 1235 build_raw_kstat_lookup(); 1236 1237 # 1238 # The Sun::Solaris::Kstat constructor. This builds the nested 1239 # name::instance::module hash structure, but doesn't actually read the 1240 # underlying kstats. This is done on demand by the TIEHASH methods in 1241 # Sun::Solaris::Kstat::_Stat 1242 # 1243 1244 SV* 1245 new(class, ...) 1246 char *class; 1247 PREINIT: 1248 HV *stash; 1249 kstat_ctl_t *kc; 1250 SV *kcsv; 1251 kstat_t *kp; 1252 KstatInfo_t kstatinfo; 1253 int sp, strip_str; 1254 CODE: 1255 /* Check we have an even number of arguments, excluding the class */ 1256 sp = 1; 1257 if (((items - sp) % 2) != 0) { 1258 croak(DEBUG_ID ": new: invalid number of arguments"); 1259 } 1260 1261 /* Process any (name => value) arguments */ 1262 strip_str = 0; 1263 while (sp < items) { 1264 SV *name, *value; 1265 1266 name = ST(sp); 1267 sp++; 1268 value = ST(sp); 1269 sp++; 1270 if (strcmp(SvPVX(name), "strip_strings") == 0) { 1271 strip_str = SvTRUE(value); 1272 } else { 1273 croak(DEBUG_ID ": new: invalid parameter name '%s'", 1274 SvPVX(name)); 1275 } 1276 } 1277 1278 /* Open the kstats handle */ 1279 if ((kc = kstat_open()) == 0) { 1280 XSRETURN_UNDEF; 1281 } 1282 1283 /* Create a blessed hash ref */ 1284 RETVAL = (SV *)newRV_noinc((SV *)newHV()); 1285 stash = gv_stashpv(class, TRUE); 1286 sv_bless(RETVAL, stash); 1287 1288 /* Create a place to save the KstatInfo_t structure */ 1289 kcsv = newSVpv((char *)&kc, sizeof (kc)); 1290 sv_magic(SvRV(RETVAL), kcsv, '~', 0, 0); 1291 SvREFCNT_dec(kcsv); 1292 1293 /* Initialise the KstatsInfo_t structure */ 1294 kstatinfo.read = FALSE; 1295 kstatinfo.valid = TRUE; 1296 kstatinfo.strip_str = strip_str; 1297 kstatinfo.kstat_ctl = kc; 1298 1299 /* Scan the kstat chain, building hash entries for the kstats */ 1300 for (kp = kc->kc_chain; kp != 0; kp = kp->ks_next) { 1301 HV *tie; 1302 SV *kstatsv; 1303 1304 /* Don't bother storing the kstat headers */ 1305 if (strncmp(kp->ks_name, "kstat_", 6) == 0) { 1306 continue; 1307 } 1308 1309 /* Don't bother storing raw stats we don't understand */ 1310 if (kp->ks_type == KSTAT_TYPE_RAW && 1311 lookup_raw_kstat_fn(kp->ks_module, kp->ks_name) == 0) { 1312 #ifdef REPORT_UNKNOWN 1313 (void) fprintf(stderr, 1314 "Unknown kstat type %s:%d:%s - %d of size %d\n", 1315 kp->ks_module, kp->ks_instance, kp->ks_name, 1316 kp->ks_ndata, kp->ks_data_size); 1317 #endif 1318 continue; 1319 } 1320 1321 /* Create a 3-layer hash hierarchy - module.instance.name */ 1322 tie = get_tie(RETVAL, kp->ks_module, kp->ks_instance, 1323 kp->ks_name, 0); 1324 1325 /* Save the data necessary to read the kstat info on demand */ 1326 hv_store(tie, "class", 5, newSVpv(kp->ks_class, 0), 0); 1327 hv_store(tie, "crtime", 6, NEW_HRTIME(kp->ks_crtime), 0); 1328 kstatinfo.kstat = kp; 1329 kstatsv = newSVpv((char *)&kstatinfo, sizeof (kstatinfo)); 1330 sv_magic((SV *)tie, kstatsv, '~', 0, 0); 1331 SvREFCNT_dec(kstatsv); 1332 } 1333 SvREADONLY_on(SvRV(RETVAL)); 1334 /* SvREADONLY_on(RETVAL); */ 1335 OUTPUT: 1336 RETVAL 1337 1338 # 1339 # Update the perl hash structure so that it is in line with the kernel kstats 1340 # data. Only kstats athat have previously been accessed are read, 1341 # 1342 1343 # Scalar context: true/false 1344 # Array context: (\@added, \@deleted) 1345 void 1346 update(self) 1347 SV* self; 1348 PREINIT: 1349 MAGIC *mg; 1350 kstat_ctl_t *kc; 1351 kstat_t *kp; 1352 int ret; 1353 AV *add, *del; 1354 PPCODE: 1355 /* Find the hidden KstatInfo_t structure */ 1356 mg = mg_find(SvRV(self), '~'); 1357 PERL_ASSERTMSG(mg != 0, "update: lost ~ magic"); 1358 kc = *(kstat_ctl_t **)SvPVX(mg->mg_obj); 1359 1360 /* Update the kstat chain, and return immediately on error. */ 1361 if ((ret = kstat_chain_update(kc)) == -1) { 1362 if (GIMME_V == G_ARRAY) { 1363 EXTEND(SP, 2); 1364 PUSHs(sv_newmortal()); 1365 PUSHs(sv_newmortal()); 1366 } else { 1367 EXTEND(SP, 1); 1368 PUSHs(sv_2mortal(newSViv(ret))); 1369 } 1370 } 1371 1372 /* Create the arrays to be returned if in an array context */ 1373 if (GIMME_V == G_ARRAY) { 1374 add = newAV(); 1375 del = newAV(); 1376 } else { 1377 add = 0; 1378 del = 0; 1379 } 1380 1381 /* 1382 * If the kstat chain hasn't changed we can just reread any stats 1383 * that have already been read 1384 */ 1385 if (ret == 0) { 1386 if (! apply_to_ties(self, (ATTCb_t)read_kstats, (void *)TRUE)) { 1387 if (GIMME_V == G_ARRAY) { 1388 EXTEND(SP, 2); 1389 PUSHs(sv_2mortal(newRV_noinc((SV *)add))); 1390 PUSHs(sv_2mortal(newRV_noinc((SV *)del))); 1391 } else { 1392 EXTEND(SP, 1); 1393 PUSHs(sv_2mortal(newSViv(-1))); 1394 } 1395 } 1396 1397 /* 1398 * Otherwise we have to update the Perl structure so that it is in 1399 * agreement with the new kstat chain. We do this in such a way as to 1400 * retain all the existing structures, just adding or deleting the 1401 * bare minimum. 1402 */ 1403 } else { 1404 KstatInfo_t kstatinfo; 1405 1406 /* 1407 * Step 1: set the 'invalid' flag on each entry 1408 */ 1409 apply_to_ties(self, &set_valid, (void *)FALSE); 1410 1411 /* 1412 * Step 2: Set the 'valid' flag on all entries still in the 1413 * kernel kstat chain 1414 */ 1415 kstatinfo.read = FALSE; 1416 kstatinfo.valid = TRUE; 1417 kstatinfo.kstat_ctl = kc; 1418 for (kp = kc->kc_chain; kp != 0; kp = kp->ks_next) { 1419 int new; 1420 HV *tie; 1421 1422 /* Don't bother storing the kstat headers or types */ 1423 if (strncmp(kp->ks_name, "kstat_", 6) == 0) { 1424 continue; 1425 } 1426 1427 /* Don't bother storing raw stats we don't understand */ 1428 if (kp->ks_type == KSTAT_TYPE_RAW && 1429 lookup_raw_kstat_fn(kp->ks_module, kp->ks_name) 1430 == 0) { 1431 #ifdef REPORT_UNKNOWN 1432 (void) printf("Unknown kstat type %s:%d:%s " 1433 "- %d of size %d\n", kp->ks_module, 1434 kp->ks_instance, kp->ks_name, 1435 kp->ks_ndata, kp->ks_data_size); 1436 #endif 1437 continue; 1438 } 1439 1440 /* Find the tied hash associated with the kstat entry */ 1441 tie = get_tie(self, kp->ks_module, kp->ks_instance, 1442 kp->ks_name, &new); 1443 1444 /* If newly created store the associated kstat info */ 1445 if (new) { 1446 SV *kstatsv; 1447 1448 /* 1449 * Save the data necessary to read the kstat 1450 * info on demand 1451 */ 1452 hv_store(tie, "class", 5, 1453 newSVpv(kp->ks_class, 0), 0); 1454 hv_store(tie, "crtime", 6, 1455 NEW_HRTIME(kp->ks_crtime), 0); 1456 kstatinfo.kstat = kp; 1457 kstatsv = newSVpv((char *)&kstatinfo, 1458 sizeof (kstatinfo)); 1459 sv_magic((SV *)tie, kstatsv, '~', 0, 0); 1460 SvREFCNT_dec(kstatsv); 1461 1462 /* Save the key on the add list, if required */ 1463 if (GIMME_V == G_ARRAY) { 1464 av_push(add, newSVpvf("%s:%d:%s", 1465 kp->ks_module, kp->ks_instance, 1466 kp->ks_name)); 1467 } 1468 1469 /* If the stats already exist, just update them */ 1470 } else { 1471 MAGIC *mg; 1472 KstatInfo_t *kip; 1473 1474 /* Find the hidden KstatInfo_t */ 1475 mg = mg_find((SV *)tie, '~'); 1476 PERL_ASSERTMSG(mg != 0, "update: lost ~ magic"); 1477 kip = (KstatInfo_t *)SvPVX(mg->mg_obj); 1478 1479 /* Mark the tie as valid */ 1480 kip->valid = TRUE; 1481 1482 /* Re-save the kstat_t pointer. If the kstat 1483 * has been deleted and re-added since the last 1484 * update, the address of the kstat structure 1485 * will have changed, even though the kstat will 1486 * still live at the same place in the perl 1487 * hash tree structure. 1488 */ 1489 kip->kstat = kp; 1490 1491 /* Reread the stats, if read previously */ 1492 read_kstats(tie, TRUE); 1493 } 1494 } 1495 1496 /* 1497 *Step 3: Delete any entries still marked as 'invalid' 1498 */ 1499 ret = prune_invalid(self, del); 1500 1501 } 1502 if (GIMME_V == G_ARRAY) { 1503 EXTEND(SP, 2); 1504 PUSHs(sv_2mortal(newRV_noinc((SV *)add))); 1505 PUSHs(sv_2mortal(newRV_noinc((SV *)del))); 1506 } else { 1507 EXTEND(SP, 1); 1508 PUSHs(sv_2mortal(newSViv(ret))); 1509 } 1510 1511 1512 # 1513 # Destructor. Closes the kstat connection 1514 # 1515 1516 void 1517 DESTROY(self) 1518 SV *self; 1519 PREINIT: 1520 MAGIC *mg; 1521 kstat_ctl_t *kc; 1522 CODE: 1523 mg = mg_find(SvRV(self), '~'); 1524 PERL_ASSERTMSG(mg != 0, "DESTROY: lost ~ magic"); 1525 kc = *(kstat_ctl_t **)SvPVX(mg->mg_obj); 1526 if (kstat_close(kc) != 0) { 1527 croak(DEBUG_ID ": kstat_close: failed"); 1528 } 1529 1530 # 1531 # The following XS methods implement the TIEHASH mechanism used to update the 1532 # kstats hash structure. These are blessed into a package that isn't 1533 # visible to callers of the Sun::Solaris::Kstat module 1534 # 1535 1536 MODULE = Sun::Solaris::Kstat PACKAGE = Sun::Solaris::Kstat::_Stat 1537 PROTOTYPES: ENABLE 1538 1539 # 1540 # If a value has already been read, return it. Otherwise read the appropriate 1541 # kstat and then return the value 1542 # 1543 1544 SV* 1545 FETCH(self, key) 1546 SV* self; 1547 SV* key; 1548 PREINIT: 1549 char *k; 1550 STRLEN klen; 1551 SV **value; 1552 CODE: 1553 self = SvRV(self); 1554 k = SvPV(key, klen); 1555 if (strNE(k, "class") && strNE(k, "crtime")) { 1556 read_kstats((HV *)self, FALSE); 1557 } 1558 value = hv_fetch((HV *)self, k, klen, FALSE); 1559 if (value) { 1560 RETVAL = *value; SvREFCNT_inc(RETVAL); 1561 } else { 1562 RETVAL = &PL_sv_undef; 1563 } 1564 OUTPUT: 1565 RETVAL 1566 1567 # 1568 # Save the passed value into the kstat hash. Read the appropriate kstat first, 1569 # if necessary. Note that this DOES NOT update the underlying kernel kstat 1570 # structure. 1571 # 1572 1573 SV* 1574 STORE(self, key, value) 1575 SV* self; 1576 SV* key; 1577 SV* value; 1578 PREINIT: 1579 char *k; 1580 STRLEN klen; 1581 CODE: 1582 self = SvRV(self); 1583 k = SvPV(key, klen); 1584 if (strNE(k, "class") && strNE(k, "crtime")) { 1585 read_kstats((HV *)self, FALSE); 1586 } 1587 SvREFCNT_inc(value); 1588 RETVAL = *(hv_store((HV *)self, k, klen, value, 0)); 1589 SvREFCNT_inc(RETVAL); 1590 OUTPUT: 1591 RETVAL 1592 1593 # 1594 # Check for the existence of the passed key. Read the kstat first if necessary 1595 # 1596 1597 bool 1598 EXISTS(self, key) 1599 SV* self; 1600 SV* key; 1601 PREINIT: 1602 char *k; 1603 CODE: 1604 self = SvRV(self); 1605 k = SvPV(key, PL_na); 1606 if (strNE(k, "class") && strNE(k, "crtime")) { 1607 read_kstats((HV *)self, FALSE); 1608 } 1609 RETVAL = hv_exists_ent((HV *)self, key, 0); 1610 OUTPUT: 1611 RETVAL 1612 1613 1614 # 1615 # Hash iterator initialisation. Read the kstats if necessary. 1616 # 1617 1618 SV* 1619 FIRSTKEY(self) 1620 SV* self; 1621 PREINIT: 1622 HE *he; 1623 PPCODE: 1624 self = SvRV(self); 1625 read_kstats((HV *)self, FALSE); 1626 hv_iterinit((HV *)self); 1627 if (he = hv_iternext((HV *)self)) { 1628 EXTEND(SP, 1); 1629 PUSHs(hv_iterkeysv(he)); 1630 } 1631 1632 # 1633 # Return hash iterator next value. Read the kstats if necessary. 1634 # 1635 1636 SV* 1637 NEXTKEY(self, lastkey) 1638 SV* self; 1639 SV* lastkey; 1640 PREINIT: 1641 HE *he; 1642 PPCODE: 1643 self = SvRV(self); 1644 if (he = hv_iternext((HV *)self)) { 1645 EXTEND(SP, 1); 1646 PUSHs(hv_iterkeysv(he)); 1647 } 1648 1649 1650 # 1651 # Delete the specified hash entry. 1652 # 1653 1654 SV* 1655 DELETE(self, key) 1656 SV *self; 1657 SV *key; 1658 CODE: 1659 self = SvRV(self); 1660 RETVAL = hv_delete_ent((HV *)self, key, 0, 0); 1661 if (RETVAL) { 1662 SvREFCNT_inc(RETVAL); 1663 } else { 1664 RETVAL = &PL_sv_undef; 1665 } 1666 OUTPUT: 1667 RETVAL 1668 1669 # 1670 # Clear the entire hash. This will stop any update() calls rereading this 1671 # kstat until it is accessed again. 1672 # 1673 1674 void 1675 CLEAR(self) 1676 SV* self; 1677 PREINIT: 1678 MAGIC *mg; 1679 KstatInfo_t *kip; 1680 CODE: 1681 self = SvRV(self); 1682 hv_clear((HV *)self); 1683 mg = mg_find(self, '~'); 1684 PERL_ASSERTMSG(mg != 0, "CLEAR: lost ~ magic"); 1685 kip = (KstatInfo_t *)SvPVX(mg->mg_obj); 1686 kip->read = FALSE; 1687 kip->valid = TRUE; 1688 hv_store((HV *)self, "class", 5, newSVpv(kip->kstat->ks_class, 0), 0); 1689 hv_store((HV *)self, "crtime", 6, NEW_HRTIME(kip->kstat->ks_crtime), 0); 1690