1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate /*
28*7c478bd9Sstevel@tonic-gate  * sa_libpool - libpool statistic adapter, collect statistic data provided
29*7c478bd9Sstevel@tonic-gate  * by libpool.
30*7c478bd9Sstevel@tonic-gate  */
31*7c478bd9Sstevel@tonic-gate 
32*7c478bd9Sstevel@tonic-gate #include <string.h>
33*7c478bd9Sstevel@tonic-gate #include <locale.h>
34*7c478bd9Sstevel@tonic-gate #include <assert.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #include <pool.h>
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate #include "utils.h"
39*7c478bd9Sstevel@tonic-gate #include "poolstat.h"
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate typedef int (*prop_walk_cb_t)
42*7c478bd9Sstevel@tonic-gate 	(pool_conf_t *, pool_elem_t *, const char *, pool_value_t *, void *);
43*7c478bd9Sstevel@tonic-gate 
44*7c478bd9Sstevel@tonic-gate /* user data used in the property walk callback function.	*/
45*7c478bd9Sstevel@tonic-gate typedef struct {
46*7c478bd9Sstevel@tonic-gate 	int	ud_result;
47*7c478bd9Sstevel@tonic-gate 	void*   ud_bag;
48*7c478bd9Sstevel@tonic-gate } userdata_cb_t;
49*7c478bd9Sstevel@tonic-gate 
50*7c478bd9Sstevel@tonic-gate static pool_conf_t *conf;
51*7c478bd9Sstevel@tonic-gate static const char *conf_loc;
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static void update_pset(statistic_bag_t *);
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate /*
56*7c478bd9Sstevel@tonic-gate  * If not NULL use the passed 'configuration' to access the pool framework,
57*7c478bd9Sstevel@tonic-gate  * otherwise create and open a private access point.
58*7c478bd9Sstevel@tonic-gate  */
59*7c478bd9Sstevel@tonic-gate void
sa_libpool_init(void * configuration)60*7c478bd9Sstevel@tonic-gate sa_libpool_init(void *configuration)
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	if (configuration) {
63*7c478bd9Sstevel@tonic-gate 		conf = configuration;
64*7c478bd9Sstevel@tonic-gate 	} else {
65*7c478bd9Sstevel@tonic-gate 		conf_loc = pool_dynamic_location();
66*7c478bd9Sstevel@tonic-gate 		if ((conf = pool_conf_alloc()) == NULL)
67*7c478bd9Sstevel@tonic-gate 			die(gettext(ERR_NOMEM));
68*7c478bd9Sstevel@tonic-gate 		if (pool_conf_open(conf, conf_loc, PO_RDONLY | PO_UPDATE)
69*7c478bd9Sstevel@tonic-gate 			!= PO_SUCCESS)
70*7c478bd9Sstevel@tonic-gate 			die(gettext(ERR_OPEN_STATIC), conf_loc, get_errstr());
71*7c478bd9Sstevel@tonic-gate 	}
72*7c478bd9Sstevel@tonic-gate }
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
75*7c478bd9Sstevel@tonic-gate void
sa_libpool_update(statistic_bag_t * sbag,int flags)76*7c478bd9Sstevel@tonic-gate sa_libpool_update(statistic_bag_t *sbag, int flags)
77*7c478bd9Sstevel@tonic-gate {
78*7c478bd9Sstevel@tonic-gate 	static int changed;
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	/* The SA_REFRESH flag forces the update of local data structures. */
81*7c478bd9Sstevel@tonic-gate 	if (flags & SA_REFRESH) {
82*7c478bd9Sstevel@tonic-gate 		changed = 0;
83*7c478bd9Sstevel@tonic-gate 		if (pool_conf_update(conf, &changed) != PO_SUCCESS)
84*7c478bd9Sstevel@tonic-gate 			die(gettext(ERR_CONF_UPDATE), get_errstr());
85*7c478bd9Sstevel@tonic-gate 		sbag->sb_changed = changed;
86*7c478bd9Sstevel@tonic-gate 	}
87*7c478bd9Sstevel@tonic-gate 	if (strcmp(sbag->sb_type, PSET_TYPE_NAME) == 0) {
88*7c478bd9Sstevel@tonic-gate 		if (changed & POU_PSET || changed & POU_CPU)
89*7c478bd9Sstevel@tonic-gate 			((pset_statistic_bag_t *)sbag->bag)->pset_sb_changed =
90*7c478bd9Sstevel@tonic-gate 				changed;
91*7c478bd9Sstevel@tonic-gate 		else
92*7c478bd9Sstevel@tonic-gate 			((pset_statistic_bag_t *)sbag->bag)->pset_sb_changed =
93*7c478bd9Sstevel@tonic-gate 				0;
94*7c478bd9Sstevel@tonic-gate 		update_pset(sbag);
95*7c478bd9Sstevel@tonic-gate 	} else if (strcmp(sbag->sb_type, POOL_TYPE_NAME) == 0) {
96*7c478bd9Sstevel@tonic-gate 		return;
97*7c478bd9Sstevel@tonic-gate 	} else {
98*7c478bd9Sstevel@tonic-gate 		die(gettext(ERR_UNSUPP_STYPE), sbag->sb_type);
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate }
101*7c478bd9Sstevel@tonic-gate 
102*7c478bd9Sstevel@tonic-gate /*
103*7c478bd9Sstevel@tonic-gate  * callback function to property walker, copies the property value from
104*7c478bd9Sstevel@tonic-gate  * the passed 'pvalue' to the corresponding field in the statistic data bag.
105*7c478bd9Sstevel@tonic-gate  */
106*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/
107*7c478bd9Sstevel@tonic-gate static int
populate_userdata_cb(pool_conf_t * unused1,pool_elem_t * unused2,const char * name,pool_value_t * pval,userdata_cb_t * ud)108*7c478bd9Sstevel@tonic-gate populate_userdata_cb(pool_conf_t *unused1, pool_elem_t *unused2,
109*7c478bd9Sstevel@tonic-gate 	const char *name, pool_value_t *pval, userdata_cb_t *ud)
110*7c478bd9Sstevel@tonic-gate {
111*7c478bd9Sstevel@tonic-gate 	pset_statistic_bag_t *bag = (pset_statistic_bag_t *)ud->ud_bag;
112*7c478bd9Sstevel@tonic-gate 
113*7c478bd9Sstevel@tonic-gate 	ud->ud_result = 0;
114*7c478bd9Sstevel@tonic-gate 	if (strcmp("pset.min", name) == 0) {
115*7c478bd9Sstevel@tonic-gate 		ud->ud_result = pool_value_get_uint64(pval, &bag->pset_sb_min);
116*7c478bd9Sstevel@tonic-gate 	} else if (strcmp("pset.max", name) == 0) {
117*7c478bd9Sstevel@tonic-gate 		ud->ud_result = pool_value_get_uint64(pval, &bag->pset_sb_max);
118*7c478bd9Sstevel@tonic-gate 	} else if (strcmp("pset.load", name) == 0) {
119*7c478bd9Sstevel@tonic-gate 		uint64_t load;
120*7c478bd9Sstevel@tonic-gate 
121*7c478bd9Sstevel@tonic-gate 		ud->ud_result = pool_value_get_uint64(pval, &load);
122*7c478bd9Sstevel@tonic-gate 		bag->pset_sb_load = (double)load / 1000.0;
123*7c478bd9Sstevel@tonic-gate 	} else if (strcmp("pset.size", name) == 0) {
124*7c478bd9Sstevel@tonic-gate 		ud->ud_result = pool_value_get_uint64(pval, &bag->pset_sb_size);
125*7c478bd9Sstevel@tonic-gate 	} else if (strcmp("pset.sys_id", name) == 0) {
126*7c478bd9Sstevel@tonic-gate 		ud->ud_result = pool_value_get_int64(pval, &bag->pset_sb_sysid);
127*7c478bd9Sstevel@tonic-gate 	}
128*7c478bd9Sstevel@tonic-gate 
129*7c478bd9Sstevel@tonic-gate 	return (0);
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate /*
133*7c478bd9Sstevel@tonic-gate  * Update statistic data for the procssor set with the name 'sbag->name'.
134*7c478bd9Sstevel@tonic-gate  * Use 'sbag->bag' to store the data.
135*7c478bd9Sstevel@tonic-gate  */
136*7c478bd9Sstevel@tonic-gate static void
update_pset(statistic_bag_t * sbag)137*7c478bd9Sstevel@tonic-gate update_pset(statistic_bag_t *sbag)
138*7c478bd9Sstevel@tonic-gate {
139*7c478bd9Sstevel@tonic-gate 	pool_resource_t *pset_reso;
140*7c478bd9Sstevel@tonic-gate 	pool_elem_t	*pset_elem;
141*7c478bd9Sstevel@tonic-gate 	userdata_cb_t	ud;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	ud.ud_bag = (void *) sbag->bag;
144*7c478bd9Sstevel@tonic-gate 	if ((pset_reso = pool_get_resource(conf, PSET_TYPE_NAME, sbag->sb_name))
145*7c478bd9Sstevel@tonic-gate 		== NULL)
146*7c478bd9Sstevel@tonic-gate 		die(gettext(ERR_STATS_RES_N), sbag->sb_name, get_errstr());
147*7c478bd9Sstevel@tonic-gate 	if ((pset_elem = pool_resource_to_elem(conf, pset_reso)) == NULL)
148*7c478bd9Sstevel@tonic-gate 		die(gettext(ERR_STATS_RES_N), sbag->sb_name, get_errstr());
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate 	/* use the property walker to collect the resource properties	*/
151*7c478bd9Sstevel@tonic-gate 	if (pool_walk_properties(conf, pset_elem, &ud,
152*7c478bd9Sstevel@tonic-gate 		(prop_walk_cb_t)populate_userdata_cb) == -1)
153*7c478bd9Sstevel@tonic-gate 		die(gettext(ERR_STATS_RES_N), sbag->sb_name, get_errstr());
154*7c478bd9Sstevel@tonic-gate }
155