xref: /onnv-gate/usr/src/cmd/avs/dsstat/common.c (revision 11576:b23c42c0c9d6)
17836SJohn.Forte@Sun.COM /*
27836SJohn.Forte@Sun.COM  * CDDL HEADER START
37836SJohn.Forte@Sun.COM  *
47836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
57836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
67836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
77836SJohn.Forte@Sun.COM  *
87836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
107836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
117836SJohn.Forte@Sun.COM  * and limitations under the License.
127836SJohn.Forte@Sun.COM  *
137836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
147836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
167836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
177836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
187836SJohn.Forte@Sun.COM  *
197836SJohn.Forte@Sun.COM  * CDDL HEADER END
207836SJohn.Forte@Sun.COM  */
217836SJohn.Forte@Sun.COM /*
22*11576SSurya.Prakki@Sun.COM  * Copyright 2010 Sun Microsystems, Inc.  All rights reserved.
237836SJohn.Forte@Sun.COM  * Use is subject to license terms.
247836SJohn.Forte@Sun.COM  */
257836SJohn.Forte@Sun.COM 
267836SJohn.Forte@Sun.COM #include <stdio.h>
277836SJohn.Forte@Sun.COM #include <stdlib.h>
287836SJohn.Forte@Sun.COM #include <string.h>
297836SJohn.Forte@Sun.COM #include <strings.h>
307836SJohn.Forte@Sun.COM #include <errno.h>
317836SJohn.Forte@Sun.COM #include <kstat.h>
327836SJohn.Forte@Sun.COM #include <signal.h>
337836SJohn.Forte@Sun.COM #include <setjmp.h>
347836SJohn.Forte@Sun.COM 
357836SJohn.Forte@Sun.COM #include "sdbc_stats.h"
367836SJohn.Forte@Sun.COM #include "report.h"
377836SJohn.Forte@Sun.COM #include "common.h"
387836SJohn.Forte@Sun.COM 
397836SJohn.Forte@Sun.COM static sigjmp_buf env;
407836SJohn.Forte@Sun.COM static sig_atomic_t sig_raised = 0;
417836SJohn.Forte@Sun.COM static void sig_handler(int);
427836SJohn.Forte@Sun.COM 
437836SJohn.Forte@Sun.COM void
sig_handler(int sig)447836SJohn.Forte@Sun.COM sig_handler(int sig)
457836SJohn.Forte@Sun.COM {
467836SJohn.Forte@Sun.COM 	switch (sig) {
477836SJohn.Forte@Sun.COM 		case SIGSEGV:
487836SJohn.Forte@Sun.COM 			sig_raised = 1;
497836SJohn.Forte@Sun.COM 			siglongjmp(env, sig);
507836SJohn.Forte@Sun.COM 		default:
517836SJohn.Forte@Sun.COM 			exit(sig);
527836SJohn.Forte@Sun.COM 	}
537836SJohn.Forte@Sun.COM }
547836SJohn.Forte@Sun.COM 
557836SJohn.Forte@Sun.COM /*
567836SJohn.Forte@Sun.COM  * kstat_retrieve() - populate the ks_data field of the kstat_t structure
577836SJohn.Forte@Sun.COM  *
587836SJohn.Forte@Sun.COM  * This function is a user-land equivalent of a ks_snapshot
597836SJohn.Forte@Sun.COM  *
607836SJohn.Forte@Sun.COM  * parameters
617836SJohn.Forte@Sun.COM  * 	kstat_ctl_t *kc - kstat_ctl_t structure representing returned from
627836SJohn.Forte@Sun.COM  *			  kstat_open()
637836SJohn.Forte@Sun.COM  * 	kstat_t *ksp - kstat_t strcture to popluate ks_data into
647836SJohn.Forte@Sun.COM  *
657836SJohn.Forte@Sun.COM  * returns
667836SJohn.Forte@Sun.COM  * 	NULL pointer on failure
677836SJohn.Forte@Sun.COM  * 	kstat_t * structure on success
687836SJohn.Forte@Sun.COM  */
697836SJohn.Forte@Sun.COM kstat_t *
kstat_retrieve(kstat_ctl_t * kc,kstat_t * ksp)707836SJohn.Forte@Sun.COM kstat_retrieve(kstat_ctl_t *kc, kstat_t *ksp)
717836SJohn.Forte@Sun.COM {
727836SJohn.Forte@Sun.COM 
737836SJohn.Forte@Sun.COM 	kstat_t *rval;
747836SJohn.Forte@Sun.COM 	kstat_named_t *knp;
757836SJohn.Forte@Sun.COM 	char *end;
767836SJohn.Forte@Sun.COM 	int i;
777836SJohn.Forte@Sun.COM 	struct sigaction segv_act;	/* default actions */
787836SJohn.Forte@Sun.COM 
797836SJohn.Forte@Sun.COM 	if (ksp == NULL)
807836SJohn.Forte@Sun.COM 		return (NULL);
817836SJohn.Forte@Sun.COM 
827836SJohn.Forte@Sun.COM 	if (ksp->ks_data == NULL &&
837836SJohn.Forte@Sun.COM 	    kstat_read(kc, ksp, NULL) == -1)
847836SJohn.Forte@Sun.COM 		return (NULL);
857836SJohn.Forte@Sun.COM 
867836SJohn.Forte@Sun.COM 	rval = (kstat_t *)calloc(1, sizeof (*ksp));
87*11576SSurya.Prakki@Sun.COM 	(void) memcpy(rval, ksp, sizeof (*ksp));
887836SJohn.Forte@Sun.COM 
897836SJohn.Forte@Sun.COM 	rval->ks_data = (void *) calloc(1, ksp->ks_data_size);
90*11576SSurya.Prakki@Sun.COM 	(void) memcpy(rval->ks_data, ksp->ks_data,
917836SJohn.Forte@Sun.COM 	    sizeof (kstat_named_t) * ksp->ks_ndata);
927836SJohn.Forte@Sun.COM 
937836SJohn.Forte@Sun.COM 	/* special handling for variable length string KSTAT_DATA_STRING */
947836SJohn.Forte@Sun.COM 	knp = (kstat_named_t *)rval->ks_data;
957836SJohn.Forte@Sun.COM 	end = (char *)(knp + ksp->ks_ndata);
967836SJohn.Forte@Sun.COM 	for (i = 0; i < ksp->ks_ndata; i++, knp++) {
977836SJohn.Forte@Sun.COM 		if (knp->data_type == KSTAT_DATA_STRING &&
987836SJohn.Forte@Sun.COM 		    KSTAT_NAMED_STR_PTR(knp) != NULL) {
997836SJohn.Forte@Sun.COM 			/* catch SIGSEGV (bug 6384130) */
1007836SJohn.Forte@Sun.COM 			sig_raised = 0;
1017836SJohn.Forte@Sun.COM 			(void) sigaction(SIGSEGV, NULL, &segv_act);
1027836SJohn.Forte@Sun.COM 			(void) signal(SIGSEGV, sig_handler);
1037836SJohn.Forte@Sun.COM 
104*11576SSurya.Prakki@Sun.COM 			(void) strncpy(end, KSTAT_NAMED_STR_PTR(knp),
1057836SJohn.Forte@Sun.COM 			    KSTAT_NAMED_STR_BUFLEN(knp));
1067836SJohn.Forte@Sun.COM 			KSTAT_NAMED_STR_PTR(knp) = end;
1077836SJohn.Forte@Sun.COM 			end += KSTAT_NAMED_STR_BUFLEN(knp);
1087836SJohn.Forte@Sun.COM 
1097836SJohn.Forte@Sun.COM 			/* bug 6384130 */
1107836SJohn.Forte@Sun.COM 			(void) sigsetjmp(env, 0);
1117836SJohn.Forte@Sun.COM 			if (sig_raised) {
1127836SJohn.Forte@Sun.COM 				bzero(end, KSTAT_NAMED_STR_BUFLEN(knp));
1137836SJohn.Forte@Sun.COM 				KSTAT_NAMED_STR_PTR(knp) = end;
1147836SJohn.Forte@Sun.COM 				end += KSTAT_NAMED_STR_BUFLEN(knp);
1157836SJohn.Forte@Sun.COM 			}
1167836SJohn.Forte@Sun.COM 			(void) sigaction(SIGSEGV, &segv_act, NULL);
1177836SJohn.Forte@Sun.COM 		}
1187836SJohn.Forte@Sun.COM 	}
1197836SJohn.Forte@Sun.COM 
1207836SJohn.Forte@Sun.COM 	return (rval);
1217836SJohn.Forte@Sun.COM }
1227836SJohn.Forte@Sun.COM 
1237836SJohn.Forte@Sun.COM /*
1247836SJohn.Forte@Sun.COM  * kstat_value() - retrieve value of a field in a kstat_named_t kstat.
1257836SJohn.Forte@Sun.COM  *
1267836SJohn.Forte@Sun.COM  * parameters
1277836SJohn.Forte@Sun.COM  * 	kstat_t *ksp - kstat containing the field
1287836SJohn.Forte@Sun.COM  * 	char *name - text string representing the field name
1297836SJohn.Forte@Sun.COM  *
1307836SJohn.Forte@Sun.COM  * returns
1317836SJohn.Forte@Sun.COM  *	void * - pointer to data retrieved
1327836SJohn.Forte@Sun.COM  */
1337836SJohn.Forte@Sun.COM void *
kstat_value(kstat_t * ksp,char * name)1347836SJohn.Forte@Sun.COM kstat_value(kstat_t *ksp, char *name)
1357836SJohn.Forte@Sun.COM {
1367836SJohn.Forte@Sun.COM 	kstat_named_t *knm;
1377836SJohn.Forte@Sun.COM 
1387836SJohn.Forte@Sun.COM 	if ((knm = kstat_data_lookup(ksp, name)) == NULL)
1397836SJohn.Forte@Sun.COM 		return (NULL);
1407836SJohn.Forte@Sun.COM 
1417836SJohn.Forte@Sun.COM 	switch (knm->data_type) {
1427836SJohn.Forte@Sun.COM 		case KSTAT_DATA_CHAR :
1437836SJohn.Forte@Sun.COM 			return (knm->value.c);
1447836SJohn.Forte@Sun.COM 		case KSTAT_DATA_INT32 :
1457836SJohn.Forte@Sun.COM 			return (&knm->value.i32);
1467836SJohn.Forte@Sun.COM 		case KSTAT_DATA_UINT32 :
1477836SJohn.Forte@Sun.COM 			return (&knm->value.ui32);
1487836SJohn.Forte@Sun.COM 		case KSTAT_DATA_INT64 :
1497836SJohn.Forte@Sun.COM 			return (&knm->value.i64);
1507836SJohn.Forte@Sun.COM 		case KSTAT_DATA_UINT64 :
1517836SJohn.Forte@Sun.COM 			return (&knm->value.ui64);
1527836SJohn.Forte@Sun.COM 		case KSTAT_DATA_STRING :
1537836SJohn.Forte@Sun.COM 			return (KSTAT_NAMED_STR_PTR(knm));
1547836SJohn.Forte@Sun.COM 	}
1557836SJohn.Forte@Sun.COM 
1567836SJohn.Forte@Sun.COM 	return (NULL);
1577836SJohn.Forte@Sun.COM }
1587836SJohn.Forte@Sun.COM 
1597836SJohn.Forte@Sun.COM /*
1607836SJohn.Forte@Sun.COM  * kstat_free() - deallocated memory associated with a kstat
1617836SJohn.Forte@Sun.COM  *
1627836SJohn.Forte@Sun.COM  * paramters
1637836SJohn.Forte@Sun.COM  * 	kstat_t ksp - kstat to be deallocated
1647836SJohn.Forte@Sun.COM  *
1657836SJohn.Forte@Sun.COM  * returns
1667836SJohn.Forte@Sun.COM  * 	void
1677836SJohn.Forte@Sun.COM  */
1687836SJohn.Forte@Sun.COM void
kstat_free(kstat_t * ksp)1697836SJohn.Forte@Sun.COM kstat_free(kstat_t *ksp)
1707836SJohn.Forte@Sun.COM {
1717836SJohn.Forte@Sun.COM 	if (ksp != NULL) {
1727836SJohn.Forte@Sun.COM 		if (ksp->ks_data != NULL)
1737836SJohn.Forte@Sun.COM 			free(ksp->ks_data);
1747836SJohn.Forte@Sun.COM 		free(ksp);
1757836SJohn.Forte@Sun.COM 	}
1767836SJohn.Forte@Sun.COM }
1777836SJohn.Forte@Sun.COM 
1787836SJohn.Forte@Sun.COM uint32_t
kstat_delta(kstat_t * pksp,kstat_t * cksp,char * name)1797836SJohn.Forte@Sun.COM kstat_delta(kstat_t *pksp, kstat_t *cksp, char *name)
1807836SJohn.Forte@Sun.COM {
1817836SJohn.Forte@Sun.COM 	uint32_t *pv, *cv;
1827836SJohn.Forte@Sun.COM 
1837836SJohn.Forte@Sun.COM 	pv = kstat_value(pksp, name);
1847836SJohn.Forte@Sun.COM 	cv = kstat_value(cksp, name);
1857836SJohn.Forte@Sun.COM 
1867836SJohn.Forte@Sun.COM 	return (u32_delta(*pv, *cv));
1877836SJohn.Forte@Sun.COM }
188