1N/A/*
1N/A * CDDL HEADER START
1N/A *
1N/A * The contents of this file are subject to the terms of the
1N/A * Common Development and Distribution License (the "License").
1N/A * You may not use this file except in compliance with the License.
1N/A *
1N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
1N/A * or http://www.opensolaris.org/os/licensing.
1N/A * See the License for the specific language governing permissions
1N/A * and limitations under the License.
1N/A *
1N/A * When distributing Covered Code, include this CDDL HEADER in each
1N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
1N/A * If applicable, add the following below this CDDL HEADER, with the
1N/A * fields enclosed by brackets "[]" replaced with your own identifying
1N/A * information: Portions Copyright [yyyy] [name of copyright owner]
1N/A *
1N/A * CDDL HEADER END
1N/A */
1N/A
1N/A/*
2N/A * Copyright (c) 1999, 2012, Oracle and/or its affiliates. All rights reserved.
1N/A */
1N/A
1N/A/*
1N/A * Kstat.xs is a Perl XS (eXStension module) that makes the Solaris
1N/A * kstat(3KSTAT) facility available to Perl scripts. Kstat is a general-purpose
1N/A * mechanism for providing kernel statistics to users. The Solaris API is
1N/A * function-based (see the manpage for details), but for ease of use in Perl
1N/A * scripts this module presents the information as a nested hash data structure.
1N/A * It would be too inefficient to read every kstat in the system, so this module
1N/A * uses the Perl TIEHASH mechanism to implement a read-on-demand semantic, which
1N/A * only reads and updates kstats as and when they are actually accessed.
1N/A */
1N/A
1N/A/*
1N/A * Ignored raw kstats.
1N/A *
1N/A * Some raw kstats are ignored by this module, these are listed below. The
1N/A * most common reason is that the kstats are stored as arrays and the ks_ndata
1N/A * and/or ks_data_size fields are invalid. In this case it is impossible to
1N/A * know how many records are in the array, so they can't be read.
1N/A *
1N/A * unix:*:sfmmu_percpu_stat
1N/A * This is stored as an array with one entry per cpu. Each element is of type
1N/A * struct sfmmu_percpu_stat. The ks_ndata and ks_data_size fields are bogus.
1N/A *
1N/A * ufs directio:*:UFS DirectIO Stats
1N/A * The structure definition used for these kstats (ufs_directio_kstats) is in a
1N/A * C file (uts/common/fs/ufs/ufs_directio.c) rather than a header file, so it
1N/A * isn't accessible.
1N/A *
1N/A * qlc:*:statistics
1N/A * This is a third-party driver for which we don't have source.
1N/A *
1N/A * mm:*:phys_installed
1N/A * This is stored as an array of uint64_t, with each pair of values being the
1N/A * (address, size) of a memory segment. The ks_ndata and ks_data_size fields
1N/A * are both zero.
1N/A *
1N/A * sockfs:*:sock_unix_list
1N/A * This is stored as an array with one entry per active socket. Each element
1N/A * is of type struct k_sockinfo. The ks_ndata and ks_data_size fields are both
1N/A * zero.
1N/A *
1N/A * Note that the ks_ndata and ks_data_size of many non-array raw kstats are
1N/A * also incorrect. The relevant assertions are therefore commented out in the
1N/A * appropriate raw kstat read routines.
1N/A */
1N/A
1N/A/* Kstat related includes */
1N/A#include <libgen.h>
1N/A#include <kstat.h>
1N/A#include <sys/var.h>
1N/A#include <sys/utsname.h>
1N/A#include <sys/sysinfo.h>
1N/A#include <sys/flock.h>
1N/A#include <sys/dnlc.h>
1N/A#include <nfs/nfs.h>
1N/A#include <nfs/nfs_clnt.h>
1N/A
1N/A/* Ultra-specific kstat includes */
1N/A#ifdef __sparc
1N/A#include <vm/hat_sfmmu.h> /* from /usr/platform/sun4u/include */
1N/A#include <sys/simmstat.h> /* from /usr/platform/sun4u/include */
1N/A#include <sys/sysctrl.h> /* from /usr/platform/sun4u/include */
1N/A#include <sys/fhc.h> /* from /usr/include */
1N/A#endif
1N/A
1N/A/*
1N/A * Solaris #defines SP, which conflicts with the perl definition of SP
1N/A * We don't need the Solaris one, so get rid of it to avoid warnings
1N/A */
1N/A#undef SP
1N/A
1N/A/* Perl XS includes */
1N/A#include "EXTERN.h"
1N/A#include "perl.h"
1N/A#include "XSUB.h"
1N/A
1N/A/* Debug macros */
1N/A#define DEBUG_ID "Sun::Solaris::Kstat"
1N/A#ifdef KSTAT_DEBUG
1N/A#define PERL_ASSERT(EXP) \
1N/A ((void)((EXP) || (croak("%s: assertion failed at %s:%d: %s", \
1N/A DEBUG_ID, __FILE__, __LINE__, #EXP), 0), 0))
1N/A#define PERL_ASSERTMSG(EXP, MSG) \
1N/A ((void)((EXP) || (croak(DEBUG_ID ": " MSG), 0), 0))
1N/A#else
1N/A#define PERL_ASSERT(EXP) ((void)0)
1N/A#define PERL_ASSERTMSG(EXP, MSG) ((void)0)
1N/A#endif
1N/A
1N/A/* Macros for saving the contents of KSTAT_RAW structures */
1N/A#if defined(HAS_QUAD) && defined(USE_64_BIT_INT)
1N/A#define NEW_IV(V) \
1N/A (newSViv((IVTYPE) V))
1N/A#define NEW_UV(V) \
1N/A (newSVuv((UVTYPE) V))
1N/A#else
1N/A#define NEW_IV(V) \
1N/A (V >= IV_MIN && V <= IV_MAX ? newSViv((IVTYPE) V) : newSVnv((NVTYPE) V))
1N/A#if defined(UVTYPE)
1N/A#define NEW_UV(V) \
1N/A (V <= UV_MAX ? newSVuv((UVTYPE) V) : newSVnv((NVTYPE) V))
1N/A# else
1N/A#define NEW_UV(V) \
1N/A (V <= IV_MAX ? newSViv((IVTYPE) V) : newSVnv((NVTYPE) V))
1N/A#endif
1N/A#endif
1N/A#define NEW_HRTIME(V) \
1N/A newSVnv((NVTYPE) (V / 1000000000.0))
1N/A
1N/A#define SAVE_FNP(H, F, K) \
1N/A hv_store(H, K, sizeof (K) - 1, newSViv((IVTYPE) &F), 0)
1N/A#define SAVE_STRING(H, S, K, SS) \
1N/A hv_store(H, #K, sizeof (#K) - 1, \
1N/A newSVpvn(S->K, SS ? strlen(S->K) : sizeof(S->K)), 0)
1N/A#define SAVE_INT32(H, S, K) \
1N/A hv_store(H, #K, sizeof (#K) - 1, NEW_IV(S->K), 0)
1N/A#define SAVE_UINT32(H, S, K) \
1N/A hv_store(H, #K, sizeof (#K) - 1, NEW_UV(S->K), 0)
1N/A#define SAVE_INT64(H, S, K) \
1N/A hv_store(H, #K, sizeof (#K) - 1, NEW_IV(S->K), 0)
1N/A#define SAVE_UINT64(H, S, K) \
1N/A hv_store(H, #K, sizeof (#K) - 1, NEW_UV(S->K), 0)
1N/A#define SAVE_HRTIME(H, S, K) \
1N/A hv_store(H, #K, sizeof (#K) - 1, NEW_HRTIME(S->K), 0)
1N/A
1N/A/* Private structure used for saving kstat info in the tied hashes */
1N/Atypedef struct {
1N/A char read; /* Kstat block has been read before */
1N/A char valid; /* Kstat still exists in kstat chain */
1N/A char strip_str; /* Strip KSTAT_DATA_CHAR fields */
1N/A kstat_ctl_t *kstat_ctl; /* Handle returned by kstat_open */
1N/A kstat_t *kstat; /* Handle used by kstat_read */
1N/A} KstatInfo_t;
1N/A
1N/A/* typedef for apply_to_ties callback functions */
1N/Atypedef int (*ATTCb_t)(HV *, void *);
1N/A
1N/A/* typedef for raw kstat reader functions */
1N/Atypedef void (*kstat_raw_reader_t)(HV *, kstat_t *, int);
1N/A
1N/A/* Hash of "module:name" to KSTAT_RAW read functions */
1N/Astatic HV *raw_kstat_lookup;
1N/A
1N/A/*
1N/A * Kstats come in two flavours, named and raw. Raw kstats are just C structs,
1N/A * so we need a function per raw kstat to convert the C struct into the
1N/A * corresponding perl hash. All such conversion functions are in the following
1N/A * section.
1N/A */
1N/A
1N/A/*
1N/A * Definitions in /usr/include/sys/cpuvar.h and /usr/include/sys/sysinfo.h
1N/A */
1N/A
1N/Astatic void
1N/Asave_cpu_stat(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A cpu_stat_t *statp;
1N/A cpu_sysinfo_t *sysinfop;
1N/A cpu_syswait_t *syswaitp;
1N/A cpu_vminfo_t *vminfop;
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (cpu_stat_t));
1N/A statp = (cpu_stat_t *)(kp->ks_data);
1N/A sysinfop = &statp->cpu_sysinfo;
1N/A syswaitp = &statp->cpu_syswait;
1N/A vminfop = &statp->cpu_vminfo;
1N/A
1N/A hv_store(self, "idle", 4, NEW_UV(sysinfop->cpu[CPU_IDLE]), 0);
1N/A hv_store(self, "user", 4, NEW_UV(sysinfop->cpu[CPU_USER]), 0);
1N/A hv_store(self, "kernel", 6, NEW_UV(sysinfop->cpu[CPU_KERNEL]), 0);
1N/A hv_store(self, "wait", 4, NEW_UV(sysinfop->cpu[CPU_WAIT]), 0);
1N/A hv_store(self, "wait_io", 7, NEW_UV(sysinfop->wait[W_IO]), 0);
1N/A hv_store(self, "wait_swap", 9, NEW_UV(sysinfop->wait[W_SWAP]), 0);
1N/A hv_store(self, "wait_pio", 8, NEW_UV(sysinfop->wait[W_PIO]), 0);
1N/A SAVE_UINT32(self, sysinfop, bread);
1N/A SAVE_UINT32(self, sysinfop, bwrite);
1N/A SAVE_UINT32(self, sysinfop, lread);
1N/A SAVE_UINT32(self, sysinfop, lwrite);
1N/A SAVE_UINT32(self, sysinfop, phread);
1N/A SAVE_UINT32(self, sysinfop, phwrite);
1N/A SAVE_UINT32(self, sysinfop, pswitch);
1N/A SAVE_UINT32(self, sysinfop, trap);
1N/A SAVE_UINT32(self, sysinfop, intr);
1N/A SAVE_UINT32(self, sysinfop, syscall);
1N/A SAVE_UINT32(self, sysinfop, sysread);
1N/A SAVE_UINT32(self, sysinfop, syswrite);
1N/A SAVE_UINT32(self, sysinfop, sysfork);
1N/A SAVE_UINT32(self, sysinfop, sysvfork);
1N/A SAVE_UINT32(self, sysinfop, sysexec);
1N/A SAVE_UINT32(self, sysinfop, readch);
1N/A SAVE_UINT32(self, sysinfop, writech);
1N/A SAVE_UINT32(self, sysinfop, rcvint);
1N/A SAVE_UINT32(self, sysinfop, xmtint);
1N/A SAVE_UINT32(self, sysinfop, mdmint);
1N/A SAVE_UINT32(self, sysinfop, rawch);
1N/A SAVE_UINT32(self, sysinfop, canch);
1N/A SAVE_UINT32(self, sysinfop, outch);
1N/A SAVE_UINT32(self, sysinfop, msg);
1N/A SAVE_UINT32(self, sysinfop, sema);
1N/A SAVE_UINT32(self, sysinfop, namei);
1N/A SAVE_UINT32(self, sysinfop, ufsiget);
1N/A SAVE_UINT32(self, sysinfop, ufsdirblk);
1N/A SAVE_UINT32(self, sysinfop, ufsipage);
1N/A SAVE_UINT32(self, sysinfop, ufsinopage);
1N/A SAVE_UINT32(self, sysinfop, inodeovf);
1N/A SAVE_UINT32(self, sysinfop, fileovf);
1N/A SAVE_UINT32(self, sysinfop, procovf);
1N/A SAVE_UINT32(self, sysinfop, intrthread);
1N/A SAVE_UINT32(self, sysinfop, intrblk);
1N/A SAVE_UINT32(self, sysinfop, idlethread);
1N/A SAVE_UINT32(self, sysinfop, inv_swtch);
1N/A SAVE_UINT32(self, sysinfop, nthreads);
1N/A SAVE_UINT32(self, sysinfop, cpumigrate);
1N/A SAVE_UINT32(self, sysinfop, xcalls);
1N/A SAVE_UINT32(self, sysinfop, mutex_adenters);
1N/A SAVE_UINT32(self, sysinfop, rw_rdfails);
1N/A SAVE_UINT32(self, sysinfop, rw_wrfails);
1N/A SAVE_UINT32(self, sysinfop, modload);
1N/A SAVE_UINT32(self, sysinfop, modunload);
1N/A SAVE_UINT32(self, sysinfop, bawrite);
1N/A#ifdef STATISTICS /* see header file */
1N/A SAVE_UINT32(self, sysinfop, rw_enters);
1N/A SAVE_UINT32(self, sysinfop, win_uo_cnt);
1N/A SAVE_UINT32(self, sysinfop, win_uu_cnt);
1N/A SAVE_UINT32(self, sysinfop, win_so_cnt);
1N/A SAVE_UINT32(self, sysinfop, win_su_cnt);
1N/A SAVE_UINT32(self, sysinfop, win_suo_cnt);
1N/A#endif
1N/A
1N/A SAVE_INT32(self, syswaitp, iowait);
1N/A SAVE_INT32(self, syswaitp, swap);
1N/A SAVE_INT32(self, syswaitp, physio);
1N/A
1N/A SAVE_UINT32(self, vminfop, pgrec);
1N/A SAVE_UINT32(self, vminfop, pgfrec);
1N/A SAVE_UINT32(self, vminfop, pgin);
1N/A SAVE_UINT32(self, vminfop, pgpgin);
1N/A SAVE_UINT32(self, vminfop, pgout);
1N/A SAVE_UINT32(self, vminfop, pgpgout);
1N/A SAVE_UINT32(self, vminfop, swapin);
1N/A SAVE_UINT32(self, vminfop, pgswapin);
1N/A SAVE_UINT32(self, vminfop, swapout);
1N/A SAVE_UINT32(self, vminfop, pgswapout);
1N/A SAVE_UINT32(self, vminfop, zfod);
1N/A SAVE_UINT32(self, vminfop, dfree);
1N/A SAVE_UINT32(self, vminfop, scan);
1N/A SAVE_UINT32(self, vminfop, rev);
1N/A SAVE_UINT32(self, vminfop, hat_fault);
1N/A SAVE_UINT32(self, vminfop, as_fault);
1N/A SAVE_UINT32(self, vminfop, maj_fault);
1N/A SAVE_UINT32(self, vminfop, cow_fault);
1N/A SAVE_UINT32(self, vminfop, prot_fault);
1N/A SAVE_UINT32(self, vminfop, softlock);
1N/A SAVE_UINT32(self, vminfop, kernel_asflt);
1N/A SAVE_UINT32(self, vminfop, pgrrun);
1N/A SAVE_UINT32(self, vminfop, execpgin);
1N/A SAVE_UINT32(self, vminfop, execpgout);
1N/A SAVE_UINT32(self, vminfop, execfree);
1N/A SAVE_UINT32(self, vminfop, anonpgin);
1N/A SAVE_UINT32(self, vminfop, anonpgout);
1N/A SAVE_UINT32(self, vminfop, anonfree);
1N/A SAVE_UINT32(self, vminfop, fspgin);
1N/A SAVE_UINT32(self, vminfop, fspgout);
1N/A SAVE_UINT32(self, vminfop, fsfree);
1N/A}
1N/A
1N/A/*
1N/A * Definitions in /usr/include/sys/var.h
1N/A */
1N/A
1N/Astatic void
1N/Asave_var(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A struct var *varp;
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (struct var));
1N/A varp = (struct var *)(kp->ks_data);
1N/A
1N/A SAVE_INT32(self, varp, v_buf);
1N/A SAVE_INT32(self, varp, v_call);
1N/A SAVE_INT32(self, varp, v_proc);
1N/A SAVE_INT32(self, varp, v_maxupttl);
1N/A SAVE_INT32(self, varp, v_nglobpris);
1N/A SAVE_INT32(self, varp, v_maxsyspri);
1N/A SAVE_INT32(self, varp, v_clist);
1N/A SAVE_INT32(self, varp, v_maxup);
1N/A SAVE_INT32(self, varp, v_hbuf);
1N/A SAVE_INT32(self, varp, v_hmask);
1N/A SAVE_INT32(self, varp, v_pbuf);
1N/A SAVE_INT32(self, varp, v_sptmap);
1N/A SAVE_INT32(self, varp, v_maxpmem);
1N/A SAVE_INT32(self, varp, v_autoup);
1N/A SAVE_INT32(self, varp, v_bufhwm);
1N/A}
1N/A
1N/A/*
1N/A * Definition in /usr/include/sys/dnlc.h
1N/A */
1N/A
1N/Astatic void
1N/Asave_ncstats(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A struct ncstats *ncstatsp;
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (struct ncstats));
1N/A ncstatsp = (struct ncstats *)(kp->ks_data);
1N/A
1N/A SAVE_INT32(self, ncstatsp, hits);
1N/A SAVE_INT32(self, ncstatsp, misses);
1N/A SAVE_INT32(self, ncstatsp, enters);
1N/A SAVE_INT32(self, ncstatsp, dbl_enters);
1N/A SAVE_INT32(self, ncstatsp, long_enter);
1N/A SAVE_INT32(self, ncstatsp, long_look);
1N/A SAVE_INT32(self, ncstatsp, move_to_front);
1N/A SAVE_INT32(self, ncstatsp, purges);
1N/A}
1N/A
1N/A/*
1N/A * Definition in /usr/include/sys/sysinfo.h
1N/A */
1N/A
1N/Astatic void
1N/Asave_sysinfo(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A sysinfo_t *sysinfop;
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (sysinfo_t));
1N/A sysinfop = (sysinfo_t *)(kp->ks_data);
1N/A
1N/A SAVE_UINT32(self, sysinfop, updates);
1N/A SAVE_UINT32(self, sysinfop, runque);
1N/A SAVE_UINT32(self, sysinfop, runocc);
1N/A SAVE_UINT32(self, sysinfop, swpque);
1N/A SAVE_UINT32(self, sysinfop, swpocc);
1N/A SAVE_UINT32(self, sysinfop, waiting);
1N/A}
1N/A
1N/A/*
1N/A * Definition in /usr/include/sys/sysinfo.h
1N/A */
1N/A
1N/Astatic void
1N/Asave_vminfo(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A vminfo_t *vminfop;
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (vminfo_t));
1N/A vminfop = (vminfo_t *)(kp->ks_data);
1N/A
1N/A SAVE_UINT64(self, vminfop, freemem);
1N/A SAVE_UINT64(self, vminfop, swap_resv);
1N/A SAVE_UINT64(self, vminfop, swap_alloc);
1N/A SAVE_UINT64(self, vminfop, swap_avail);
1N/A SAVE_UINT64(self, vminfop, swap_free);
1N/A}
1N/A
1N/A/*
1N/A * Definition in /usr/include/nfs/nfs_clnt.h
1N/A */
1N/A
1N/Astatic void
1N/Asave_nfs(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A struct mntinfo_kstat *mntinfop;
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (struct mntinfo_kstat));
1N/A mntinfop = (struct mntinfo_kstat *)(kp->ks_data);
1N/A
1N/A SAVE_STRING(self, mntinfop, mik_proto, strip_str);
1N/A SAVE_UINT32(self, mntinfop, mik_vers);
1N/A SAVE_UINT32(self, mntinfop, mik_flags);
1N/A SAVE_UINT32(self, mntinfop, mik_secmod);
1N/A SAVE_UINT32(self, mntinfop, mik_curread);
1N/A SAVE_UINT32(self, mntinfop, mik_curwrite);
1N/A SAVE_INT32(self, mntinfop, mik_timeo);
1N/A SAVE_INT32(self, mntinfop, mik_retrans);
1N/A SAVE_UINT32(self, mntinfop, mik_acregmin);
1N/A SAVE_UINT32(self, mntinfop, mik_acregmax);
1N/A SAVE_UINT32(self, mntinfop, mik_acdirmin);
1N/A SAVE_UINT32(self, mntinfop, mik_acdirmax);
1N/A hv_store(self, "lookup_srtt", 11,
1N/A NEW_UV(mntinfop->mik_timers[0].srtt), 0);
1N/A hv_store(self, "lookup_deviate", 14,
1N/A NEW_UV(mntinfop->mik_timers[0].deviate), 0);
1N/A hv_store(self, "lookup_rtxcur", 13,
1N/A NEW_UV(mntinfop->mik_timers[0].rtxcur), 0);
1N/A hv_store(self, "read_srtt", 9,
1N/A NEW_UV(mntinfop->mik_timers[1].srtt), 0);
1N/A hv_store(self, "read_deviate", 12,
1N/A NEW_UV(mntinfop->mik_timers[1].deviate), 0);
1N/A hv_store(self, "read_rtxcur", 11,
1N/A NEW_UV(mntinfop->mik_timers[1].rtxcur), 0);
1N/A hv_store(self, "write_srtt", 10,
1N/A NEW_UV(mntinfop->mik_timers[2].srtt), 0);
1N/A hv_store(self, "write_deviate", 13,
1N/A NEW_UV(mntinfop->mik_timers[2].deviate), 0);
1N/A hv_store(self, "write_rtxcur", 12,
1N/A NEW_UV(mntinfop->mik_timers[2].rtxcur), 0);
1N/A SAVE_UINT32(self, mntinfop, mik_noresponse);
1N/A SAVE_UINT32(self, mntinfop, mik_failover);
1N/A SAVE_UINT32(self, mntinfop, mik_remap);
1N/A SAVE_STRING(self, mntinfop, mik_curserver, strip_str);
1N/A}
1N/A
1N/A/*
1N/A * The following struct => hash functions are all only present on the sparc
1N/A * platform, so they are all conditionally compiled depending on __sparc
1N/A */
1N/A
1N/A/*
1N/A * Definition in /usr/platform/sun4u/include/sys/simmstat.h
1N/A */
1N/A
1N/A#ifdef __sparc
1N/Astatic void
1N/Asave_simmstat(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A uchar_t *simmstatp;
1N/A SV *list;
1N/A int i;
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (uchar_t) * SIMM_COUNT);
1N/A
1N/A list = newSVpv("", 0);
1N/A for (i = 0, simmstatp = (uchar_t *)(kp->ks_data);
1N/A i < SIMM_COUNT - 1; i++, simmstatp++) {
1N/A sv_catpvf(list, "%d,", *simmstatp);
1N/A }
1N/A sv_catpvf(list, "%d", *simmstatp);
1N/A hv_store(self, "status", 6, list, 0);
1N/A}
1N/A#endif
1N/A
1N/A/*
1N/A * Used by save_temperature to make CSV lists from arrays of
1N/A * short temperature values
1N/A */
1N/A
1N/A#ifdef __sparc
1N/Astatic SV *
1N/Ashort_array_to_SV(short *shortp, int len)
1N/A{
1N/A SV *list;
1N/A
1N/A list = newSVpv("", 0);
1N/A for (; len > 1; len--, shortp++) {
1N/A sv_catpvf(list, "%d,", *shortp);
1N/A }
1N/A sv_catpvf(list, "%d", *shortp);
1N/A return (list);
1N/A}
1N/A
1N/A/*
1N/A * Definition in /usr/platform/sun4u/include/sys/fhc.h
1N/A */
1N/A
1N/Astatic void
1N/Asave_temperature(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A struct temp_stats *tempsp;
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (struct temp_stats));
1N/A tempsp = (struct temp_stats *)(kp->ks_data);
1N/A
1N/A SAVE_UINT32(self, tempsp, index);
1N/A hv_store(self, "l1", 2, short_array_to_SV(tempsp->l1, L1_SZ), 0);
1N/A hv_store(self, "l2", 2, short_array_to_SV(tempsp->l2, L2_SZ), 0);
1N/A hv_store(self, "l3", 2, short_array_to_SV(tempsp->l3, L3_SZ), 0);
1N/A hv_store(self, "l4", 2, short_array_to_SV(tempsp->l4, L4_SZ), 0);
1N/A hv_store(self, "l5", 2, short_array_to_SV(tempsp->l5, L5_SZ), 0);
1N/A SAVE_INT32(self, tempsp, max);
1N/A SAVE_INT32(self, tempsp, min);
1N/A SAVE_INT32(self, tempsp, state);
1N/A SAVE_INT32(self, tempsp, temp_cnt);
1N/A SAVE_INT32(self, tempsp, shutdown_cnt);
1N/A SAVE_INT32(self, tempsp, version);
1N/A SAVE_INT32(self, tempsp, trend);
1N/A SAVE_INT32(self, tempsp, override);
1N/A}
1N/A#endif
1N/A
1N/A/*
1N/A * Not actually defined anywhere - just a short. Yuck.
1N/A */
1N/A
1N/A#ifdef __sparc
1N/Astatic void
1N/Asave_temp_over(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A short *shortp;
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (short));
1N/A
1N/A shortp = (short *)(kp->ks_data);
1N/A hv_store(self, "override", 8, newSViv(*shortp), 0);
1N/A}
1N/A#endif
1N/A
1N/A/*
1N/A * Defined in /usr/platform/sun4u/include/sys/sysctrl.h
1N/A * (Well, sort of. Actually there's no structure, just a list of #defines
1N/A * enumerating *some* of the array indexes.)
1N/A */
1N/A
1N/A#ifdef __sparc
1N/Astatic void
1N/Asave_ps_shadow(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A uchar_t *ucharp;
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A PERL_ASSERT(kp->ks_data_size == SYS_PS_COUNT);
1N/A
1N/A ucharp = (uchar_t *)(kp->ks_data);
1N/A hv_store(self, "core_0", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "core_1", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "core_2", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "core_3", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "core_4", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "core_5", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "core_6", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "core_7", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "pps_0", 5, newSViv(*ucharp++), 0);
1N/A hv_store(self, "clk_33", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "clk_50", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "v5_p", 4, newSViv(*ucharp++), 0);
1N/A hv_store(self, "v12_p", 5, newSViv(*ucharp++), 0);
1N/A hv_store(self, "v5_aux", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "v5_p_pch", 8, newSViv(*ucharp++), 0);
1N/A hv_store(self, "v12_p_pch", 9, newSViv(*ucharp++), 0);
1N/A hv_store(self, "v3_pch", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "v5_pch", 6, newSViv(*ucharp++), 0);
1N/A hv_store(self, "p_fan", 5, newSViv(*ucharp++), 0);
1N/A}
1N/A#endif
1N/A
1N/A/*
1N/A * Definition in /usr/platform/sun4u/include/sys/fhc.h
1N/A */
1N/A
1N/A#ifdef __sparc
1N/Astatic void
1N/Asave_fault_list(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A struct ft_list *faultp;
1N/A int i;
1N/A char name[KSTAT_STRLEN + 7]; /* room for 999999 faults */
1N/A
1N/A /* PERL_ASSERT(kp->ks_ndata == 1); */
1N/A /* PERL_ASSERT(kp->ks_data_size == sizeof (struct ft_list)); */
1N/A
1N/A for (i = 1, faultp = (struct ft_list *)(kp->ks_data);
1N/A i <= 999999 && i <= kp->ks_data_size / sizeof (struct ft_list);
1N/A i++, faultp++) {
1N/A (void) snprintf(name, sizeof (name), "unit_%d", i);
1N/A hv_store(self, name, strlen(name), newSViv(faultp->unit), 0);
1N/A (void) snprintf(name, sizeof (name), "type_%d", i);
1N/A hv_store(self, name, strlen(name), newSViv(faultp->type), 0);
1N/A (void) snprintf(name, sizeof (name), "fclass_%d", i);
1N/A hv_store(self, name, strlen(name), newSViv(faultp->fclass), 0);
1N/A (void) snprintf(name, sizeof (name), "create_time_%d", i);
1N/A hv_store(self, name, strlen(name),
1N/A NEW_UV(faultp->create_time), 0);
1N/A (void) snprintf(name, sizeof (name), "msg_%d", i);
1N/A hv_store(self, name, strlen(name), newSVpv(faultp->msg, 0), 0);
1N/A }
1N/A}
1N/A#endif
1N/A
1N/A/*
1N/A * We need to be able to find the function corresponding to a particular raw
1N/A * kstat. To do this we ignore the instance and glue the module and name
1N/A * together to form a composite key. We can then use the data in the kstat
1N/A * structure to find the appropriate function. We use a perl hash to manage the
1N/A * lookup, where the key is "module:name" and the value is a pointer to the
1N/A * appropriate C function.
1N/A *
1N/A * Note that some kstats include the instance number as part of the module
1N/A * and/or name. This could be construed as a bug. However, to work around this
1N/A * we omit any digits from the module and name as we build the table in
1N/A * build_raw_kstat_loopup(), and we remove any digits from the module and name
1N/A * when we look up the functions in lookup_raw_kstat_fn()
1N/A */
1N/A
1N/A/*
1N/A * This function is called when the XS is first dlopen()ed, and builds the
1N/A * lookup table as described above.
1N/A */
1N/A
1N/Astatic void
1N/Abuild_raw_kstat_lookup()
1N/A {
1N/A /* Create new hash */
1N/A raw_kstat_lookup = newHV();
1N/A
1N/A SAVE_FNP(raw_kstat_lookup, save_cpu_stat, "cpu_stat:cpu_stat");
1N/A SAVE_FNP(raw_kstat_lookup, save_var, "unix:var");
1N/A SAVE_FNP(raw_kstat_lookup, save_ncstats, "unix:ncstats");
1N/A SAVE_FNP(raw_kstat_lookup, save_sysinfo, "unix:sysinfo");
1N/A SAVE_FNP(raw_kstat_lookup, save_vminfo, "unix:vminfo");
1N/A SAVE_FNP(raw_kstat_lookup, save_nfs, "nfs:mntinfo");
1N/A#ifdef __sparc
1N/A SAVE_FNP(raw_kstat_lookup, save_simmstat, "unix:simm-status");
1N/A SAVE_FNP(raw_kstat_lookup, save_temperature, "unix:temperature");
1N/A SAVE_FNP(raw_kstat_lookup, save_temp_over, "unix:temperature override");
1N/A SAVE_FNP(raw_kstat_lookup, save_ps_shadow, "unix:ps_shadow");
1N/A SAVE_FNP(raw_kstat_lookup, save_fault_list, "unix:fault_list");
1N/A#endif
1N/A}
1N/A
1N/A/*
1N/A * This finds and returns the raw kstat reader function corresponding to the
1N/A * supplied module and name. If no matching function exists, 0 is returned.
1N/A */
1N/A
1N/Astatic kstat_raw_reader_t lookup_raw_kstat_fn(char *module, char *name)
1N/A {
1N/A char key[KSTAT_STRLEN * 2];
1N/A register char *f, *t;
1N/A SV **entry;
1N/A kstat_raw_reader_t fnp;
1N/A
1N/A /* Copy across module & name, removing any digits - see comment above */
1N/A for (f = module, t = key; *f != '\0'; f++, t++) {
1N/A while (*f != '\0' && isdigit(*f)) { f++; }
1N/A *t = *f;
1N/A }
1N/A *t++ = ':';
1N/A for (f = name; *f != '\0'; f++, t++) {
1N/A while (*f != '\0' && isdigit(*f)) {
1N/A f++;
1N/A }
1N/A *t = *f;
1N/A }
1N/A *t = '\0';
1N/A
1N/A /* look up & return the function, or teturn 0 if not found */
1N/A if ((entry = hv_fetch(raw_kstat_lookup, key, strlen(key), FALSE)) == 0)
1N/A {
1N/A fnp = 0;
1N/A } else {
1N/A fnp = (kstat_raw_reader_t)(uintptr_t)SvIV(*entry);
1N/A }
1N/A return (fnp);
1N/A}
1N/A
1N/A/*
1N/A * This module converts the flat list returned by kstat_read() into a perl hash
1N/A * tree keyed on module, instance, name and statistic. The following functions
1N/A * provide code to create the nested hashes, and to iterate over them.
1N/A */
1N/A
1N/A/*
1N/A * Given module, instance and name keys return a pointer to the hash tied to
1N/A * the bottommost hash. If the hash already exists, we just return a pointer
1N/A * to it, otherwise we create the hash and any others also required above it in
1N/A * the hierarchy. The returned tiehash is blessed into the
1N/A * Sun::Solaris::Kstat::_Stat class, so that the appropriate TIEHASH methods are
1N/A * called when the bottommost hash is accessed. If the is_new parameter is
1N/A * non-null it will be set to TRUE if a new tie has been created, and FALSE if
1N/A * the tie already existed.
1N/A */
1N/A
1N/Astatic HV *
1N/Aget_tie(SV *self, char *module, int instance, char *name, int *is_new)
1N/A{
1N/A char str_inst[11]; /* big enough for up to 10^10 instances */
1N/A char *key[3]; /* 3 part key: module, instance, name */
1N/A int k;
1N/A int new;
1N/A HV *hash;
1N/A HV *tie;
1N/A
1N/A /* Create the keys */
1N/A (void) snprintf(str_inst, sizeof (str_inst), "%d", instance);
1N/A key[0] = module;
1N/A key[1] = str_inst;
1N/A key[2] = name;
1N/A
1N/A /* Iteratively descend the tree, creating new hashes as required */
1N/A hash = (HV *)SvRV(self);
1N/A for (k = 0; k < 3; k++) {
1N/A SV **entry;
1N/A
1N/A SvREADONLY_off(hash);
1N/A entry = hv_fetch(hash, key[k], strlen(key[k]), TRUE);
1N/A
1N/A /* If the entry doesn't exist, create it */
1N/A if (! SvOK(*entry)) {
1N/A HV *newhash;
1N/A SV *rv;
1N/A
1N/A newhash = newHV();
1N/A rv = newRV_noinc((SV *)newhash);
1N/A sv_setsv(*entry, rv);
1N/A SvREFCNT_dec(rv);
1N/A if (k < 2) {
1N/A SvREADONLY_on(newhash);
1N/A }
1N/A SvREADONLY_on(*entry);
1N/A SvREADONLY_on(hash);
1N/A hash = newhash;
1N/A new = 1;
1N/A
1N/A /* Otherwise it already existed */
1N/A } else {
1N/A SvREADONLY_on(hash);
1N/A hash = (HV *)SvRV(*entry);
1N/A new = 0;
1N/A }
1N/A }
1N/A
1N/A /* Create and bless a hash for the tie, if necessary */
1N/A if (new) {
1N/A SV *tieref;
1N/A HV *stash;
1N/A
1N/A tie = newHV();
1N/A tieref = newRV_noinc((SV *)tie);
1N/A stash = gv_stashpv("Sun::Solaris::Kstat::_Stat", TRUE);
1N/A sv_bless(tieref, stash);
1N/A
1N/A /* Add TIEHASH magic */
1N/A hv_magic(hash, (GV *)tieref, 'P');
1N/A SvREADONLY_on(hash);
1N/A
1N/A /* Otherwise, just find the existing tied hash */
1N/A } else {
1N/A MAGIC *mg;
1N/A
1N/A mg = mg_find((SV *)hash, 'P');
1N/A PERL_ASSERTMSG(mg != 0, "get_tie: lost P magic");
1N/A tie = (HV *)SvRV(mg->mg_obj);
1N/A }
1N/A if (is_new) {
1N/A *is_new = new;
1N/A }
1N/A return (tie);
1N/A}
1N/A
1N/A/*
1N/A * This is an iterator function used to traverse the hash hierarchy and apply
1N/A * the passed function to the tied hashes at the bottom of the hierarchy. If
1N/A * any of the callback functions return 0, 0 is returned, otherwise 1
1N/A */
1N/A
1N/Astatic int
1N/Aapply_to_ties(SV *self, ATTCb_t cb, void *arg)
1N/A{
1N/A HV *hash1;
1N/A HE *entry1;
1N/A long s;
1N/A int ret;
1N/A
1N/A hash1 = (HV *)SvRV(self);
1N/A hv_iterinit(hash1);
1N/A ret = 1;
1N/A
1N/A /* Iterate over each module */
1N/A while (entry1 = hv_iternext(hash1)) {
1N/A HV *hash2;
1N/A HE *entry2;
1N/A
1N/A hash2 = (HV *)SvRV(hv_iterval(hash1, entry1));
1N/A hv_iterinit(hash2);
1N/A
1N/A /* Iterate over each module:instance */
1N/A while (entry2 = hv_iternext(hash2)) {
1N/A HV *hash3;
1N/A HE *entry3;
1N/A
1N/A hash3 = (HV *)SvRV(hv_iterval(hash2, entry2));
1N/A hv_iterinit(hash3);
1N/A
1N/A /* Iterate over each module:instance:name */
1N/A while (entry3 = hv_iternext(hash3)) {
1N/A HV *hash4;
1N/A MAGIC *mg;
1N/A HV *tie;
1N/A
1N/A /* Get the tie */
1N/A hash4 = (HV *)SvRV(hv_iterval(hash3, entry3));
1N/A mg = mg_find((SV *)hash4, 'P');
1N/A PERL_ASSERTMSG(mg != 0,
1N/A "apply_to_ties: lost P magic");
1N/A
1N/A /* Apply the callback */
1N/A if (! cb((HV *)SvRV(mg->mg_obj), arg)) {
1N/A ret = 0;
1N/A }
1N/A }
1N/A }
1N/A }
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * Mark this HV as valid - used by update() when pruning deleted kstat nodes
1N/A */
1N/A
1N/Astatic int
1N/Aset_valid(HV *self, void *arg)
1N/A{
1N/A MAGIC *mg;
1N/A
1N/A mg = mg_find((SV *)self, '~');
1N/A PERL_ASSERTMSG(mg != 0, "set_valid: lost ~ magic");
1N/A ((KstatInfo_t *)SvPVX(mg->mg_obj))->valid = (int)arg;
1N/A return (1);
1N/A}
1N/A
1N/A/*
1N/A * Prune invalid kstat nodes. This is called when kstat_chain_update() detects
1N/A * that the kstat chain has been updated. This removes any hash tree entries
1N/A * that no longer have a corresponding kstat. If del is non-null it will be
1N/A * set to the keys of the deleted kstat nodes, if any. If any entries are
1N/A * deleted 1 will be retured, otherwise 0
1N/A */
1N/A
1N/Astatic int
1N/Aprune_invalid(SV *self, AV *del)
1N/A{
1N/A HV *hash1;
1N/A HE *entry1;
1N/A STRLEN klen;
1N/A char *module, *instance, *name, *key;
1N/A int ret;
1N/A
1N/A hash1 = (HV *)SvRV(self);
1N/A hv_iterinit(hash1);
1N/A ret = 0;
1N/A
1N/A /* Iterate over each module */
1N/A while (entry1 = hv_iternext(hash1)) {
1N/A HV *hash2;
1N/A HE *entry2;
1N/A
1N/A module = HePV(entry1, PL_na);
1N/A hash2 = (HV *)SvRV(hv_iterval(hash1, entry1));
1N/A hv_iterinit(hash2);
1N/A
1N/A /* Iterate over each module:instance */
1N/A while (entry2 = hv_iternext(hash2)) {
1N/A HV *hash3;
1N/A HE *entry3;
1N/A
1N/A instance = HePV(entry2, PL_na);
1N/A hash3 = (HV *)SvRV(hv_iterval(hash2, entry2));
1N/A hv_iterinit(hash3);
1N/A
1N/A /* Iterate over each module:instance:name */
1N/A while (entry3 = hv_iternext(hash3)) {
1N/A HV *hash4;
1N/A MAGIC *mg;
1N/A HV *tie;
1N/A
1N/A name = HePV(entry3, PL_na);
1N/A hash4 = (HV *)SvRV(hv_iterval(hash3, entry3));
1N/A mg = mg_find((SV *)hash4, 'P');
1N/A PERL_ASSERTMSG(mg != 0,
1N/A "prune_invalid: lost P magic");
1N/A tie = (HV *)SvRV(mg->mg_obj);
1N/A mg = mg_find((SV *)tie, '~');
1N/A PERL_ASSERTMSG(mg != 0,
1N/A "prune_invalid: lost ~ magic");
1N/A
1N/A /* If this is marked as invalid, prune it */
1N/A if (((KstatInfo_t *)SvPVX(
1N/A (SV *)mg->mg_obj))->valid == FALSE) {
1N/A SvREADONLY_off(hash3);
1N/A key = HePV(entry3, klen);
1N/A hv_delete(hash3, key, klen, G_DISCARD);
1N/A SvREADONLY_on(hash3);
1N/A if (del) {
1N/A av_push(del,
1N/A newSVpvf("%s:%s:%s",
1N/A module, instance, name));
1N/A }
1N/A ret = 1;
1N/A }
1N/A }
1N/A
1N/A /* If the module:instance:name hash is empty prune it */
1N/A if (HvKEYS(hash3) == 0) {
1N/A SvREADONLY_off(hash2);
1N/A key = HePV(entry2, klen);
1N/A hv_delete(hash2, key, klen, G_DISCARD);
1N/A SvREADONLY_on(hash2);
1N/A }
1N/A }
1N/A /* If the module:instance hash is empty prune it */
1N/A if (HvKEYS(hash2) == 0) {
1N/A SvREADONLY_off(hash1);
1N/A key = HePV(entry1, klen);
1N/A hv_delete(hash1, key, klen, G_DISCARD);
1N/A SvREADONLY_on(hash1);
1N/A }
1N/A }
1N/A return (ret);
1N/A}
1N/A
1N/A/*
1N/A * Named kstats are returned as a list of key/values. This function converts
1N/A * such a list into the equivalent perl datatypes, and stores them in the passed
1N/A * hash.
1N/A */
1N/A
1N/Astatic void
1N/Asave_named(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A kstat_named_t *knp;
1N/A int n;
1N/A SV* value;
1N/A
1N/A for (n = kp->ks_ndata, knp = KSTAT_NAMED_PTR(kp); n > 0; n--, knp++) {
1N/A switch (knp->data_type) {
1N/A case KSTAT_DATA_CHAR:
1N/A value = newSVpv(knp->value.c, strip_str ?
1N/A strlen(knp->value.c) : sizeof (knp->value.c));
1N/A break;
1N/A case KSTAT_DATA_INT32:
1N/A value = newSViv(knp->value.i32);
1N/A break;
1N/A case KSTAT_DATA_UINT32:
1N/A value = NEW_UV(knp->value.ui32);
1N/A break;
1N/A case KSTAT_DATA_INT64:
1N/A value = NEW_UV(knp->value.i64);
1N/A break;
1N/A case KSTAT_DATA_UINT64:
1N/A value = NEW_UV(knp->value.ui64);
1N/A break;
1N/A case KSTAT_DATA_STRING:
1N/A if (KSTAT_NAMED_STR_PTR(knp) == NULL)
1N/A value = newSVpv("null", sizeof ("null") - 1);
1N/A else
1N/A value = newSVpv(KSTAT_NAMED_STR_PTR(knp),
1N/A KSTAT_NAMED_STR_BUFLEN(knp) -1);
1N/A break;
1N/A default:
1N/A PERL_ASSERTMSG(0, "kstat_read: invalid data type");
1N/A break;
1N/A }
1N/A hv_store(self, knp->name, strlen(knp->name), value, 0);
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * Save kstat interrupt statistics
1N/A */
1N/A
1N/Astatic void
1N/Asave_intr(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A kstat_intr_t *kintrp;
1N/A int i;
1N/A static char *intr_names[] =
1N/A { "hard", "soft", "watchdog", "spurious", "multiple_service" };
1N/A
1N/A PERL_ASSERT(kp->ks_ndata == 1);
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (kstat_intr_t));
1N/A kintrp = KSTAT_INTR_PTR(kp);
1N/A
1N/A for (i = 0; i < KSTAT_NUM_INTRS; i++) {
1N/A hv_store(self, intr_names[i], strlen(intr_names[i]),
1N/A NEW_UV(kintrp->intrs[i]), 0);
1N/A }
1N/A}
1N/A
1N/A/*
1N/A * Save IO statistics
1N/A */
1N/A
1N/Astatic void
1N/Asave_io(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A kstat_io_t *kiop;
1N/A
1N/A PERL_ASSERT(kp->ks_ndata == 1);
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (kstat_io_t));
1N/A kiop = KSTAT_IO_PTR(kp);
1N/A SAVE_UINT64(self, kiop, nread);
1N/A SAVE_UINT64(self, kiop, nwritten);
1N/A SAVE_UINT32(self, kiop, reads);
1N/A SAVE_UINT32(self, kiop, writes);
1N/A SAVE_HRTIME(self, kiop, wtime);
1N/A SAVE_HRTIME(self, kiop, wlentime);
1N/A SAVE_HRTIME(self, kiop, wlastupdate);
1N/A SAVE_HRTIME(self, kiop, rtime);
1N/A SAVE_HRTIME(self, kiop, rlentime);
1N/A SAVE_HRTIME(self, kiop, rlastupdate);
1N/A SAVE_UINT32(self, kiop, wcnt);
1N/A SAVE_UINT32(self, kiop, rcnt);
1N/A}
1N/A
1N/A/*
1N/A * Save timer statistics
1N/A */
1N/A
1N/Astatic void
1N/Asave_timer(HV *self, kstat_t *kp, int strip_str)
1N/A{
1N/A kstat_timer_t *ktimerp;
1N/A
1N/A PERL_ASSERT(kp->ks_ndata == 1);
1N/A PERL_ASSERT(kp->ks_data_size == sizeof (kstat_timer_t));
1N/A ktimerp = KSTAT_TIMER_PTR(kp);
1N/A SAVE_STRING(self, ktimerp, name, strip_str);
1N/A SAVE_UINT64(self, ktimerp, num_events);
1N/A SAVE_HRTIME(self, ktimerp, elapsed_time);
1N/A SAVE_HRTIME(self, ktimerp, min_time);
1N/A SAVE_HRTIME(self, ktimerp, max_time);
1N/A SAVE_HRTIME(self, ktimerp, start_time);
1N/A SAVE_HRTIME(self, ktimerp, stop_time);
1N/A}
1N/A
1N/A/*
1N/A * Read kstats and copy into the supplied perl hash structure. If refresh is
1N/A * true, this function is being called as part of the update() method. In this
1N/A * case it is only necessary to read the kstats if they have previously been
1N/A * accessed (kip->read == TRUE). If refresh is false, this function is being
1N/A * called prior to returning a value to the caller. In this case, it is only
1N/A * necessary to read the kstats if they have not previously been read. If the
1N/A * kstat_read() fails, 0 is returned, otherwise 1
1N/A */
1N/A
1N/Astatic int
1N/Aread_kstats(HV *self, int refresh)
1N/A{
1N/A MAGIC *mg;
1N/A KstatInfo_t *kip;
1N/A kstat_raw_reader_t fnp;
1N/A
1N/A /* Find the MAGIC KstatInfo_t data structure */
1N/A mg = mg_find((SV *)self, '~');
1N/A PERL_ASSERTMSG(mg != 0, "read_kstats: lost ~ magic");
1N/A kip = (KstatInfo_t *)SvPVX(mg->mg_obj);
1N/A
1N/A /* Return early if we don't need to actually read the kstats */
1N/A if ((refresh && ! kip->read) || (! refresh && kip->read)) {
1N/A return (1);
1N/A }
1N/A
1N/A /* Read the kstats and return 0 if this fails */
1N/A if (kstat_read(kip->kstat_ctl, kip->kstat, NULL) < 0) {
1N/A return (0);
1N/A }
1N/A
1N/A /* Save the read data */
1N/A hv_store(self, "snaptime", 8, NEW_HRTIME(kip->kstat->ks_snaptime), 0);
1N/A switch (kip->kstat->ks_type) {
1N/A case KSTAT_TYPE_RAW:
1N/A if ((fnp = lookup_raw_kstat_fn(kip->kstat->ks_module,
1N/A kip->kstat->ks_name)) != 0) {
1N/A fnp(self, kip->kstat, kip->strip_str);
1N/A }
1N/A break;
1N/A case KSTAT_TYPE_NAMED:
1N/A save_named(self, kip->kstat, kip->strip_str);
1N/A break;
1N/A case KSTAT_TYPE_INTR:
1N/A save_intr(self, kip->kstat, kip->strip_str);
1N/A break;
1N/A case KSTAT_TYPE_IO:
1N/A save_io(self, kip->kstat, kip->strip_str);
1N/A break;
1N/A case KSTAT_TYPE_TIMER:
1N/A save_timer(self, kip->kstat, kip->strip_str);
1N/A break;
1N/A default:
1N/A PERL_ASSERTMSG(0, "read_kstats: illegal kstat type");
1N/A break;
1N/A }
1N/A kip->read = TRUE;
1N/A return (1);
1N/A}
1N/A
1N/A/*
1N/A * The XS code exported to perl is below here. Note that the XS preprocessor
1N/A * has its own commenting syntax, so all comments from this point on are in
1N/A * that form.
1N/A */
1N/A
1N/A/* The following XS methods are the ABI of the Sun::Solaris::Kstat package */
1N/A
1N/AMODULE = Sun::Solaris::Kstat PACKAGE = Sun::Solaris::Kstat
1N/APROTOTYPES: ENABLE
1N/A
1N/A # Create the raw kstat to store function lookup table on load
1N/ABOOT:
1N/A build_raw_kstat_lookup();
1N/A
1N/A #
1N/A # The Sun::Solaris::Kstat constructor. This builds the nested
1N/A # name::instance::module hash structure, but doesn't actually read the
1N/A # underlying kstats. This is done on demand by the TIEHASH methods in
1N/A # Sun::Solaris::Kstat::_Stat
1N/A #
1N/A
1N/ASV*
1N/Anew(class, ...)
1N/A char *class;
1N/APREINIT:
1N/A HV *stash;
1N/A kstat_ctl_t *kc;
1N/A SV *kcsv;
1N/A kstat_t *kp;
1N/A KstatInfo_t kstatinfo;
1N/A int sp, strip_str;
1N/ACODE:
1N/A /* Check we have an even number of arguments, excluding the class */
1N/A sp = 1;
1N/A if (((items - sp) % 2) != 0) {
1N/A croak(DEBUG_ID ": new: invalid number of arguments");
1N/A }
1N/A
1N/A /* Process any (name => value) arguments */
1N/A strip_str = 0;
1N/A while (sp < items) {
1N/A SV *name, *value;
1N/A
1N/A name = ST(sp);
1N/A sp++;
1N/A value = ST(sp);
1N/A sp++;
1N/A if (strcmp(SvPVX(name), "strip_strings") == 0) {
1N/A strip_str = SvTRUE(value);
1N/A } else {
1N/A croak(DEBUG_ID ": new: invalid parameter name '%s'",
1N/A SvPVX(name));
1N/A }
1N/A }
1N/A
1N/A /* Open the kstats handle */
1N/A if ((kc = kstat_open()) == 0) {
1N/A XSRETURN_UNDEF;
1N/A }
1N/A
1N/A /* Create a blessed hash ref */
1N/A RETVAL = (SV *)newRV_noinc((SV *)newHV());
1N/A stash = gv_stashpv(class, TRUE);
1N/A sv_bless(RETVAL, stash);
1N/A
1N/A /* Create a place to save the KstatInfo_t structure */
1N/A kcsv = newSVpv((char *)&kc, sizeof (kc));
1N/A sv_magic(SvRV(RETVAL), kcsv, '~', 0, 0);
1N/A SvREFCNT_dec(kcsv);
1N/A
1N/A /* Initialise the KstatsInfo_t structure */
1N/A kstatinfo.read = FALSE;
1N/A kstatinfo.valid = TRUE;
1N/A kstatinfo.strip_str = strip_str;
1N/A kstatinfo.kstat_ctl = kc;
1N/A
1N/A /* Scan the kstat chain, building hash entries for the kstats */
1N/A for (kp = kc->kc_chain; kp != 0; kp = kp->ks_next) {
1N/A HV *tie;
1N/A SV *kstatsv;
1N/A
1N/A /* Don't bother storing the kstat headers */
1N/A if (strncmp(kp->ks_name, "kstat_", 6) == 0) {
1N/A continue;
1N/A }
1N/A
1N/A /* Don't bother storing raw stats we don't understand */
1N/A if (kp->ks_type == KSTAT_TYPE_RAW &&
1N/A lookup_raw_kstat_fn(kp->ks_module, kp->ks_name) == 0) {
1N/A#ifdef REPORT_UNKNOWN
1N/A (void) fprintf(stderr,
1N/A "Unknown kstat type %s:%d:%s - %d of size %d\n",
1N/A kp->ks_module, kp->ks_instance, kp->ks_name,
1N/A kp->ks_ndata, kp->ks_data_size);
1N/A#endif
1N/A continue;
1N/A }
1N/A
1N/A /* Create a 3-layer hash hierarchy - module.instance.name */
1N/A tie = get_tie(RETVAL, kp->ks_module, kp->ks_instance,
1N/A kp->ks_name, 0);
1N/A
1N/A /* Save the data necessary to read the kstat info on demand */
1N/A hv_store(tie, "class", 5, newSVpv(kp->ks_class, 0), 0);
1N/A hv_store(tie, "crtime", 6, NEW_HRTIME(kp->ks_crtime), 0);
1N/A kstatinfo.kstat = kp;
1N/A kstatsv = newSVpv((char *)&kstatinfo, sizeof (kstatinfo));
1N/A sv_magic((SV *)tie, kstatsv, '~', 0, 0);
1N/A SvREFCNT_dec(kstatsv);
1N/A }
1N/A SvREADONLY_on(SvRV(RETVAL));
1N/A /* SvREADONLY_on(RETVAL); */
1N/AOUTPUT:
1N/A RETVAL
1N/A
1N/A #
1N/A # Update the perl hash structure so that it is in line with the kernel kstats
1N/A # data. Only kstats athat have previously been accessed are read,
1N/A #
1N/A
1N/A # Scalar context: true/false
1N/A # Array context: (\@added, \@deleted)
1N/Avoid
1N/Aupdate(self)
1N/A SV* self;
1N/APREINIT:
1N/A MAGIC *mg;
1N/A kstat_ctl_t *kc;
1N/A kstat_t *kp;
1N/A int ret;
1N/A AV *add, *del;
1N/APPCODE:
1N/A /* Find the hidden KstatInfo_t structure */
1N/A mg = mg_find(SvRV(self), '~');
1N/A PERL_ASSERTMSG(mg != 0, "update: lost ~ magic");
1N/A kc = *(kstat_ctl_t **)SvPVX(mg->mg_obj);
1N/A
1N/A /* Update the kstat chain, and return immediately on error. */
1N/A if ((ret = kstat_chain_update(kc)) == -1) {
1N/A if (GIMME_V == G_ARRAY) {
1N/A EXTEND(SP, 2);
1N/A PUSHs(sv_newmortal());
1N/A PUSHs(sv_newmortal());
1N/A } else {
1N/A EXTEND(SP, 1);
1N/A PUSHs(sv_2mortal(newSViv(ret)));
1N/A }
1N/A }
1N/A
1N/A /* Create the arrays to be returned if in an array context */
1N/A if (GIMME_V == G_ARRAY) {
1N/A add = newAV();
1N/A del = newAV();
1N/A } else {
1N/A add = 0;
1N/A del = 0;
1N/A }
1N/A
1N/A /*
1N/A * If the kstat chain hasn't changed we can just reread any stats
1N/A * that have already been read
1N/A */
1N/A if (ret == 0) {
1N/A if (! apply_to_ties(self, (ATTCb_t)read_kstats, (void *)TRUE)) {
1N/A if (GIMME_V == G_ARRAY) {
1N/A EXTEND(SP, 2);
1N/A PUSHs(sv_2mortal(newRV_noinc((SV *)add)));
1N/A PUSHs(sv_2mortal(newRV_noinc((SV *)del)));
1N/A } else {
1N/A EXTEND(SP, 1);
1N/A PUSHs(sv_2mortal(newSViv(-1)));
1N/A }
1N/A }
1N/A
1N/A /*
1N/A * Otherwise we have to update the Perl structure so that it is in
1N/A * agreement with the new kstat chain. We do this in such a way as to
1N/A * retain all the existing structures, just adding or deleting the
1N/A * bare minimum.
1N/A */
1N/A } else {
1N/A KstatInfo_t kstatinfo;
1N/A
1N/A /*
1N/A * Step 1: set the 'invalid' flag on each entry
1N/A */
1N/A apply_to_ties(self, &set_valid, (void *)FALSE);
1N/A
1N/A /*
1N/A * Step 2: Set the 'valid' flag on all entries still in the
1N/A * kernel kstat chain
1N/A */
1N/A kstatinfo.read = FALSE;
1N/A kstatinfo.valid = TRUE;
1N/A kstatinfo.kstat_ctl = kc;
1N/A for (kp = kc->kc_chain; kp != 0; kp = kp->ks_next) {
1N/A int new;
1N/A HV *tie;
1N/A
1N/A /* Don't bother storing the kstat headers or types */
1N/A if (strncmp(kp->ks_name, "kstat_", 6) == 0) {
1N/A continue;
1N/A }
1N/A
1N/A /* Don't bother storing raw stats we don't understand */
1N/A if (kp->ks_type == KSTAT_TYPE_RAW &&
1N/A lookup_raw_kstat_fn(kp->ks_module, kp->ks_name)
1N/A == 0) {
1N/A#ifdef REPORT_UNKNOWN
1N/A (void) printf("Unknown kstat type %s:%d:%s "
1N/A "- %d of size %d\n", kp->ks_module,
1N/A kp->ks_instance, kp->ks_name,
1N/A kp->ks_ndata, kp->ks_data_size);
1N/A#endif
1N/A continue;
1N/A }
1N/A
1N/A /* Find the tied hash associated with the kstat entry */
1N/A tie = get_tie(self, kp->ks_module, kp->ks_instance,
1N/A kp->ks_name, &new);
1N/A
1N/A /* If newly created store the associated kstat info */
1N/A if (new) {
1N/A SV *kstatsv;
1N/A
1N/A /*
1N/A * Save the data necessary to read the kstat
1N/A * info on demand
1N/A */
1N/A hv_store(tie, "class", 5,
1N/A newSVpv(kp->ks_class, 0), 0);
1N/A hv_store(tie, "crtime", 6,
1N/A NEW_HRTIME(kp->ks_crtime), 0);
1N/A kstatinfo.kstat = kp;
1N/A kstatsv = newSVpv((char *)&kstatinfo,
1N/A sizeof (kstatinfo));
1N/A sv_magic((SV *)tie, kstatsv, '~', 0, 0);
1N/A SvREFCNT_dec(kstatsv);
1N/A
1N/A /* Save the key on the add list, if required */
1N/A if (GIMME_V == G_ARRAY) {
1N/A av_push(add, newSVpvf("%s:%d:%s",
1N/A kp->ks_module, kp->ks_instance,
1N/A kp->ks_name));
1N/A }
1N/A
1N/A /* If the stats already exist, just update them */
1N/A } else {
1N/A MAGIC *mg;
1N/A KstatInfo_t *kip;
1N/A
1N/A /* Find the hidden KstatInfo_t */
1N/A mg = mg_find((SV *)tie, '~');
1N/A PERL_ASSERTMSG(mg != 0, "update: lost ~ magic");
1N/A kip = (KstatInfo_t *)SvPVX(mg->mg_obj);
1N/A
1N/A /* Mark the tie as valid */
1N/A kip->valid = TRUE;
1N/A
1N/A /* Re-save the kstat_t pointer. If the kstat
1N/A * has been deleted and re-added since the last
1N/A * update, the address of the kstat structure
1N/A * will have changed, even though the kstat will
1N/A * still live at the same place in the perl
1N/A * hash tree structure.
1N/A */
1N/A kip->kstat = kp;
1N/A
1N/A /* Reread the stats, if read previously */
1N/A read_kstats(tie, TRUE);
1N/A }
1N/A }
1N/A
1N/A /*
1N/A *Step 3: Delete any entries still marked as 'invalid'
1N/A */
1N/A ret = prune_invalid(self, del);
1N/A
1N/A }
1N/A if (GIMME_V == G_ARRAY) {
1N/A EXTEND(SP, 2);
1N/A PUSHs(sv_2mortal(newRV_noinc((SV *)add)));
1N/A PUSHs(sv_2mortal(newRV_noinc((SV *)del)));
1N/A } else {
1N/A EXTEND(SP, 1);
1N/A PUSHs(sv_2mortal(newSViv(ret)));
1N/A }
1N/A
1N/A
1N/A #
1N/A # Destructor. Closes the kstat connection
1N/A #
1N/A
1N/Avoid
1N/ADESTROY(self)
1N/A SV *self;
1N/APREINIT:
1N/A MAGIC *mg;
1N/A kstat_ctl_t *kc;
1N/ACODE:
1N/A mg = mg_find(SvRV(self), '~');
1N/A PERL_ASSERTMSG(mg != 0, "DESTROY: lost ~ magic");
1N/A kc = *(kstat_ctl_t **)SvPVX(mg->mg_obj);
1N/A if (kstat_close(kc) != 0) {
1N/A croak(DEBUG_ID ": kstat_close: failed");
1N/A }
1N/A
1N/A #
1N/A # The following XS methods implement the TIEHASH mechanism used to update the
1N/A # kstats hash structure. These are blessed into a package that isn't
1N/A # visible to callers of the Sun::Solaris::Kstat module
1N/A #
1N/A
1N/AMODULE = Sun::Solaris::Kstat PACKAGE = Sun::Solaris::Kstat::_Stat
1N/APROTOTYPES: ENABLE
1N/A
1N/A #
1N/A # If a value has already been read, return it. Otherwise read the appropriate
1N/A # kstat and then return the value
1N/A #
1N/A
1N/ASV*
1N/AFETCH(self, key)
1N/A SV* self;
1N/A SV* key;
1N/APREINIT:
1N/A char *k;
1N/A STRLEN klen;
1N/A SV **value;
1N/ACODE:
1N/A self = SvRV(self);
1N/A k = SvPV(key, klen);
1N/A if (strNE(k, "class") && strNE(k, "crtime")) {
1N/A read_kstats((HV *)self, FALSE);
1N/A }
1N/A value = hv_fetch((HV *)self, k, klen, FALSE);
1N/A if (value) {
1N/A RETVAL = *value; SvREFCNT_inc(RETVAL);
1N/A } else {
1N/A RETVAL = &PL_sv_undef;
1N/A }
1N/AOUTPUT:
1N/A RETVAL
1N/A
1N/A #
1N/A # Save the passed value into the kstat hash. Read the appropriate kstat first,
1N/A # if necessary. Note that this DOES NOT update the underlying kernel kstat
1N/A # structure.
1N/A #
1N/A
1N/ASV*
1N/ASTORE(self, key, value)
1N/A SV* self;
1N/A SV* key;
1N/A SV* value;
1N/APREINIT:
1N/A char *k;
1N/A STRLEN klen;
1N/ACODE:
1N/A self = SvRV(self);
1N/A k = SvPV(key, klen);
1N/A if (strNE(k, "class") && strNE(k, "crtime")) {
1N/A read_kstats((HV *)self, FALSE);
1N/A }
1N/A SvREFCNT_inc(value);
1N/A RETVAL = *(hv_store((HV *)self, k, klen, value, 0));
1N/A SvREFCNT_inc(RETVAL);
1N/AOUTPUT:
1N/A RETVAL
1N/A
1N/A #
1N/A # Check for the existence of the passed key. Read the kstat first if necessary
1N/A #
1N/A
1N/Abool
1N/AEXISTS(self, key)
1N/A SV* self;
1N/A SV* key;
1N/APREINIT:
1N/A char *k;
1N/ACODE:
1N/A self = SvRV(self);
1N/A k = SvPV(key, PL_na);
1N/A if (strNE(k, "class") && strNE(k, "crtime")) {
1N/A read_kstats((HV *)self, FALSE);
1N/A }
1N/A RETVAL = hv_exists_ent((HV *)self, key, 0);
1N/AOUTPUT:
1N/A RETVAL
1N/A
1N/A
1N/A #
1N/A # Hash iterator initialisation. Read the kstats if necessary.
1N/A #
1N/A
1N/ASV*
1N/AFIRSTKEY(self)
1N/A SV* self;
1N/APREINIT:
1N/A HE *he;
1N/APPCODE:
1N/A self = SvRV(self);
1N/A read_kstats((HV *)self, FALSE);
1N/A hv_iterinit((HV *)self);
1N/A if (he = hv_iternext((HV *)self)) {
1N/A EXTEND(SP, 1);
1N/A PUSHs(hv_iterkeysv(he));
1N/A }
1N/A
1N/A #
1N/A # Return hash iterator next value. Read the kstats if necessary.
1N/A #
1N/A
1N/ASV*
1N/ANEXTKEY(self, lastkey)
1N/A SV* self;
1N/A SV* lastkey;
1N/APREINIT:
1N/A HE *he;
1N/APPCODE:
1N/A self = SvRV(self);
1N/A if (he = hv_iternext((HV *)self)) {
1N/A EXTEND(SP, 1);
1N/A PUSHs(hv_iterkeysv(he));
1N/A }
1N/A
1N/A
1N/A #
1N/A # Delete the specified hash entry.
1N/A #
1N/A
1N/ASV*
1N/ADELETE(self, key)
1N/A SV *self;
1N/A SV *key;
1N/ACODE:
1N/A self = SvRV(self);
1N/A RETVAL = hv_delete_ent((HV *)self, key, 0, 0);
1N/A if (RETVAL) {
1N/A SvREFCNT_inc(RETVAL);
1N/A } else {
1N/A RETVAL = &PL_sv_undef;
1N/A }
1N/AOUTPUT:
1N/A RETVAL
1N/A
1N/A #
1N/A # Clear the entire hash. This will stop any update() calls rereading this
1N/A # kstat until it is accessed again.
1N/A #
1N/A
1N/Avoid
1N/ACLEAR(self)
1N/A SV* self;
1N/APREINIT:
1N/A MAGIC *mg;
1N/A KstatInfo_t *kip;
1N/ACODE:
1N/A self = SvRV(self);
1N/A hv_clear((HV *)self);
1N/A mg = mg_find(self, '~');
1N/A PERL_ASSERTMSG(mg != 0, "CLEAR: lost ~ magic");
1N/A kip = (KstatInfo_t *)SvPVX(mg->mg_obj);
1N/A kip->read = FALSE;
1N/A kip->valid = TRUE;
1N/A hv_store((HV *)self, "class", 5, newSVpv(kip->kstat->ks_class, 0), 0);
1N/A hv_store((HV *)self, "crtime", 6, NEW_HRTIME(kip->kstat->ks_crtime), 0);