1fcf3ce44SJohn Forte /* 2fcf3ce44SJohn Forte * CDDL HEADER START 3fcf3ce44SJohn Forte * 4fcf3ce44SJohn Forte * The contents of this file are subject to the terms of the 5fcf3ce44SJohn Forte * Common Development and Distribution License (the "License"). 6fcf3ce44SJohn Forte * You may not use this file except in compliance with the License. 7fcf3ce44SJohn Forte * 8fcf3ce44SJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE 9fcf3ce44SJohn Forte * or http://www.opensolaris.org/os/licensing. 10fcf3ce44SJohn Forte * See the License for the specific language governing permissions 11fcf3ce44SJohn Forte * and limitations under the License. 12fcf3ce44SJohn Forte * 13fcf3ce44SJohn Forte * When distributing Covered Code, include this CDDL HEADER in each 14fcf3ce44SJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE. 15fcf3ce44SJohn Forte * If applicable, add the following below this CDDL HEADER, with the 16fcf3ce44SJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying 17fcf3ce44SJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner] 18fcf3ce44SJohn Forte * 19fcf3ce44SJohn Forte * CDDL HEADER END 20fcf3ce44SJohn Forte */ 21fcf3ce44SJohn Forte /* 22*570de38fSSurya Prakki * Copyright 2010 Sun Microsystems, Inc. All rights reserved. 23fcf3ce44SJohn Forte * Use is subject to license terms. 24fcf3ce44SJohn Forte */ 25fcf3ce44SJohn Forte 26fcf3ce44SJohn Forte #include <stdio.h> 27fcf3ce44SJohn Forte #include <stdlib.h> 28fcf3ce44SJohn Forte #include <string.h> 29fcf3ce44SJohn Forte #include <strings.h> 30fcf3ce44SJohn Forte #include <errno.h> 31fcf3ce44SJohn Forte #include <kstat.h> 32fcf3ce44SJohn Forte #include <signal.h> 33fcf3ce44SJohn Forte #include <setjmp.h> 34fcf3ce44SJohn Forte 35fcf3ce44SJohn Forte #include "sdbc_stats.h" 36fcf3ce44SJohn Forte #include "report.h" 37fcf3ce44SJohn Forte #include "common.h" 38fcf3ce44SJohn Forte 39fcf3ce44SJohn Forte static sigjmp_buf env; 40fcf3ce44SJohn Forte static sig_atomic_t sig_raised = 0; 41fcf3ce44SJohn Forte static void sig_handler(int); 42fcf3ce44SJohn Forte 43fcf3ce44SJohn Forte void 44fcf3ce44SJohn Forte sig_handler(int sig) 45fcf3ce44SJohn Forte { 46fcf3ce44SJohn Forte switch (sig) { 47fcf3ce44SJohn Forte case SIGSEGV: 48fcf3ce44SJohn Forte sig_raised = 1; 49fcf3ce44SJohn Forte siglongjmp(env, sig); 50fcf3ce44SJohn Forte default: 51fcf3ce44SJohn Forte exit(sig); 52fcf3ce44SJohn Forte } 53fcf3ce44SJohn Forte } 54fcf3ce44SJohn Forte 55fcf3ce44SJohn Forte /* 56fcf3ce44SJohn Forte * kstat_retrieve() - populate the ks_data field of the kstat_t structure 57fcf3ce44SJohn Forte * 58fcf3ce44SJohn Forte * This function is a user-land equivalent of a ks_snapshot 59fcf3ce44SJohn Forte * 60fcf3ce44SJohn Forte * parameters 61fcf3ce44SJohn Forte * kstat_ctl_t *kc - kstat_ctl_t structure representing returned from 62fcf3ce44SJohn Forte * kstat_open() 63fcf3ce44SJohn Forte * kstat_t *ksp - kstat_t strcture to popluate ks_data into 64fcf3ce44SJohn Forte * 65fcf3ce44SJohn Forte * returns 66fcf3ce44SJohn Forte * NULL pointer on failure 67fcf3ce44SJohn Forte * kstat_t * structure on success 68fcf3ce44SJohn Forte */ 69fcf3ce44SJohn Forte kstat_t * 70fcf3ce44SJohn Forte kstat_retrieve(kstat_ctl_t *kc, kstat_t *ksp) 71fcf3ce44SJohn Forte { 72fcf3ce44SJohn Forte 73fcf3ce44SJohn Forte kstat_t *rval; 74fcf3ce44SJohn Forte kstat_named_t *knp; 75fcf3ce44SJohn Forte char *end; 76fcf3ce44SJohn Forte int i; 77fcf3ce44SJohn Forte struct sigaction segv_act; /* default actions */ 78fcf3ce44SJohn Forte 79fcf3ce44SJohn Forte if (ksp == NULL) 80fcf3ce44SJohn Forte return (NULL); 81fcf3ce44SJohn Forte 82fcf3ce44SJohn Forte if (ksp->ks_data == NULL && 83fcf3ce44SJohn Forte kstat_read(kc, ksp, NULL) == -1) 84fcf3ce44SJohn Forte return (NULL); 85fcf3ce44SJohn Forte 86fcf3ce44SJohn Forte rval = (kstat_t *)calloc(1, sizeof (*ksp)); 87*570de38fSSurya Prakki (void) memcpy(rval, ksp, sizeof (*ksp)); 88fcf3ce44SJohn Forte 89fcf3ce44SJohn Forte rval->ks_data = (void *) calloc(1, ksp->ks_data_size); 90*570de38fSSurya Prakki (void) memcpy(rval->ks_data, ksp->ks_data, 91fcf3ce44SJohn Forte sizeof (kstat_named_t) * ksp->ks_ndata); 92fcf3ce44SJohn Forte 93fcf3ce44SJohn Forte /* special handling for variable length string KSTAT_DATA_STRING */ 94fcf3ce44SJohn Forte knp = (kstat_named_t *)rval->ks_data; 95fcf3ce44SJohn Forte end = (char *)(knp + ksp->ks_ndata); 96fcf3ce44SJohn Forte for (i = 0; i < ksp->ks_ndata; i++, knp++) { 97fcf3ce44SJohn Forte if (knp->data_type == KSTAT_DATA_STRING && 98fcf3ce44SJohn Forte KSTAT_NAMED_STR_PTR(knp) != NULL) { 99fcf3ce44SJohn Forte /* catch SIGSEGV (bug 6384130) */ 100fcf3ce44SJohn Forte sig_raised = 0; 101fcf3ce44SJohn Forte (void) sigaction(SIGSEGV, NULL, &segv_act); 102fcf3ce44SJohn Forte (void) signal(SIGSEGV, sig_handler); 103fcf3ce44SJohn Forte 104*570de38fSSurya Prakki (void) strncpy(end, KSTAT_NAMED_STR_PTR(knp), 105fcf3ce44SJohn Forte KSTAT_NAMED_STR_BUFLEN(knp)); 106fcf3ce44SJohn Forte KSTAT_NAMED_STR_PTR(knp) = end; 107fcf3ce44SJohn Forte end += KSTAT_NAMED_STR_BUFLEN(knp); 108fcf3ce44SJohn Forte 109fcf3ce44SJohn Forte /* bug 6384130 */ 110fcf3ce44SJohn Forte (void) sigsetjmp(env, 0); 111fcf3ce44SJohn Forte if (sig_raised) { 112fcf3ce44SJohn Forte bzero(end, KSTAT_NAMED_STR_BUFLEN(knp)); 113fcf3ce44SJohn Forte KSTAT_NAMED_STR_PTR(knp) = end; 114fcf3ce44SJohn Forte end += KSTAT_NAMED_STR_BUFLEN(knp); 115fcf3ce44SJohn Forte } 116fcf3ce44SJohn Forte (void) sigaction(SIGSEGV, &segv_act, NULL); 117fcf3ce44SJohn Forte } 118fcf3ce44SJohn Forte } 119fcf3ce44SJohn Forte 120fcf3ce44SJohn Forte return (rval); 121fcf3ce44SJohn Forte } 122fcf3ce44SJohn Forte 123fcf3ce44SJohn Forte /* 124fcf3ce44SJohn Forte * kstat_value() - retrieve value of a field in a kstat_named_t kstat. 125fcf3ce44SJohn Forte * 126fcf3ce44SJohn Forte * parameters 127fcf3ce44SJohn Forte * kstat_t *ksp - kstat containing the field 128fcf3ce44SJohn Forte * char *name - text string representing the field name 129fcf3ce44SJohn Forte * 130fcf3ce44SJohn Forte * returns 131fcf3ce44SJohn Forte * void * - pointer to data retrieved 132fcf3ce44SJohn Forte */ 133fcf3ce44SJohn Forte void * 134fcf3ce44SJohn Forte kstat_value(kstat_t *ksp, char *name) 135fcf3ce44SJohn Forte { 136fcf3ce44SJohn Forte kstat_named_t *knm; 137fcf3ce44SJohn Forte 138fcf3ce44SJohn Forte if ((knm = kstat_data_lookup(ksp, name)) == NULL) 139fcf3ce44SJohn Forte return (NULL); 140fcf3ce44SJohn Forte 141fcf3ce44SJohn Forte switch (knm->data_type) { 142fcf3ce44SJohn Forte case KSTAT_DATA_CHAR : 143fcf3ce44SJohn Forte return (knm->value.c); 144fcf3ce44SJohn Forte case KSTAT_DATA_INT32 : 145fcf3ce44SJohn Forte return (&knm->value.i32); 146fcf3ce44SJohn Forte case KSTAT_DATA_UINT32 : 147fcf3ce44SJohn Forte return (&knm->value.ui32); 148fcf3ce44SJohn Forte case KSTAT_DATA_INT64 : 149fcf3ce44SJohn Forte return (&knm->value.i64); 150fcf3ce44SJohn Forte case KSTAT_DATA_UINT64 : 151fcf3ce44SJohn Forte return (&knm->value.ui64); 152fcf3ce44SJohn Forte case KSTAT_DATA_STRING : 153fcf3ce44SJohn Forte return (KSTAT_NAMED_STR_PTR(knm)); 154fcf3ce44SJohn Forte } 155fcf3ce44SJohn Forte 156fcf3ce44SJohn Forte return (NULL); 157fcf3ce44SJohn Forte } 158fcf3ce44SJohn Forte 159fcf3ce44SJohn Forte /* 160fcf3ce44SJohn Forte * kstat_free() - deallocated memory associated with a kstat 161fcf3ce44SJohn Forte * 162fcf3ce44SJohn Forte * paramters 163fcf3ce44SJohn Forte * kstat_t ksp - kstat to be deallocated 164fcf3ce44SJohn Forte * 165fcf3ce44SJohn Forte * returns 166fcf3ce44SJohn Forte * void 167fcf3ce44SJohn Forte */ 168fcf3ce44SJohn Forte void 169fcf3ce44SJohn Forte kstat_free(kstat_t *ksp) 170fcf3ce44SJohn Forte { 171fcf3ce44SJohn Forte if (ksp != NULL) { 172fcf3ce44SJohn Forte if (ksp->ks_data != NULL) 173fcf3ce44SJohn Forte free(ksp->ks_data); 174fcf3ce44SJohn Forte free(ksp); 175fcf3ce44SJohn Forte } 176fcf3ce44SJohn Forte } 177fcf3ce44SJohn Forte 178fcf3ce44SJohn Forte uint32_t 179fcf3ce44SJohn Forte kstat_delta(kstat_t *pksp, kstat_t *cksp, char *name) 180fcf3ce44SJohn Forte { 181fcf3ce44SJohn Forte uint32_t *pv, *cv; 182fcf3ce44SJohn Forte 183fcf3ce44SJohn Forte pv = kstat_value(pksp, name); 184fcf3ce44SJohn Forte cv = kstat_value(cksp, name); 185fcf3ce44SJohn Forte 186fcf3ce44SJohn Forte return (u32_delta(*pv, *cv)); 187fcf3ce44SJohn Forte } 188