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 #pragma ident "%Z%%M% %I% %E% SMI" 28*7c478bd9Sstevel@tonic-gate 29*7c478bd9Sstevel@tonic-gate /* 30*7c478bd9Sstevel@tonic-gate * sa_libpool - libpool statistic adapter, collect statistic data provided 31*7c478bd9Sstevel@tonic-gate * by libpool. 32*7c478bd9Sstevel@tonic-gate */ 33*7c478bd9Sstevel@tonic-gate 34*7c478bd9Sstevel@tonic-gate #include <string.h> 35*7c478bd9Sstevel@tonic-gate #include <locale.h> 36*7c478bd9Sstevel@tonic-gate #include <assert.h> 37*7c478bd9Sstevel@tonic-gate 38*7c478bd9Sstevel@tonic-gate #include <pool.h> 39*7c478bd9Sstevel@tonic-gate 40*7c478bd9Sstevel@tonic-gate #include "utils.h" 41*7c478bd9Sstevel@tonic-gate #include "poolstat.h" 42*7c478bd9Sstevel@tonic-gate 43*7c478bd9Sstevel@tonic-gate typedef int (*prop_walk_cb_t) 44*7c478bd9Sstevel@tonic-gate (pool_conf_t *, pool_elem_t *, const char *, pool_value_t *, void *); 45*7c478bd9Sstevel@tonic-gate 46*7c478bd9Sstevel@tonic-gate /* user data used in the property walk callback function. */ 47*7c478bd9Sstevel@tonic-gate typedef struct { 48*7c478bd9Sstevel@tonic-gate int ud_result; 49*7c478bd9Sstevel@tonic-gate void* ud_bag; 50*7c478bd9Sstevel@tonic-gate } userdata_cb_t; 51*7c478bd9Sstevel@tonic-gate 52*7c478bd9Sstevel@tonic-gate static pool_conf_t *conf; 53*7c478bd9Sstevel@tonic-gate static const char *conf_loc; 54*7c478bd9Sstevel@tonic-gate 55*7c478bd9Sstevel@tonic-gate static void update_pset(statistic_bag_t *); 56*7c478bd9Sstevel@tonic-gate 57*7c478bd9Sstevel@tonic-gate /* 58*7c478bd9Sstevel@tonic-gate * If not NULL use the passed 'configuration' to access the pool framework, 59*7c478bd9Sstevel@tonic-gate * otherwise create and open a private access point. 60*7c478bd9Sstevel@tonic-gate */ 61*7c478bd9Sstevel@tonic-gate void 62*7c478bd9Sstevel@tonic-gate sa_libpool_init(void *configuration) 63*7c478bd9Sstevel@tonic-gate { 64*7c478bd9Sstevel@tonic-gate if (configuration) { 65*7c478bd9Sstevel@tonic-gate conf = configuration; 66*7c478bd9Sstevel@tonic-gate } else { 67*7c478bd9Sstevel@tonic-gate conf_loc = pool_dynamic_location(); 68*7c478bd9Sstevel@tonic-gate if ((conf = pool_conf_alloc()) == NULL) 69*7c478bd9Sstevel@tonic-gate die(gettext(ERR_NOMEM)); 70*7c478bd9Sstevel@tonic-gate if (pool_conf_open(conf, conf_loc, PO_RDONLY | PO_UPDATE) 71*7c478bd9Sstevel@tonic-gate != PO_SUCCESS) 72*7c478bd9Sstevel@tonic-gate die(gettext(ERR_OPEN_STATIC), conf_loc, get_errstr()); 73*7c478bd9Sstevel@tonic-gate } 74*7c478bd9Sstevel@tonic-gate } 75*7c478bd9Sstevel@tonic-gate 76*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 77*7c478bd9Sstevel@tonic-gate void 78*7c478bd9Sstevel@tonic-gate sa_libpool_update(statistic_bag_t *sbag, int flags) 79*7c478bd9Sstevel@tonic-gate { 80*7c478bd9Sstevel@tonic-gate static int changed; 81*7c478bd9Sstevel@tonic-gate 82*7c478bd9Sstevel@tonic-gate /* The SA_REFRESH flag forces the update of local data structures. */ 83*7c478bd9Sstevel@tonic-gate if (flags & SA_REFRESH) { 84*7c478bd9Sstevel@tonic-gate changed = 0; 85*7c478bd9Sstevel@tonic-gate if (pool_conf_update(conf, &changed) != PO_SUCCESS) 86*7c478bd9Sstevel@tonic-gate die(gettext(ERR_CONF_UPDATE), get_errstr()); 87*7c478bd9Sstevel@tonic-gate sbag->sb_changed = changed; 88*7c478bd9Sstevel@tonic-gate } 89*7c478bd9Sstevel@tonic-gate if (strcmp(sbag->sb_type, PSET_TYPE_NAME) == 0) { 90*7c478bd9Sstevel@tonic-gate if (changed & POU_PSET || changed & POU_CPU) 91*7c478bd9Sstevel@tonic-gate ((pset_statistic_bag_t *)sbag->bag)->pset_sb_changed = 92*7c478bd9Sstevel@tonic-gate changed; 93*7c478bd9Sstevel@tonic-gate else 94*7c478bd9Sstevel@tonic-gate ((pset_statistic_bag_t *)sbag->bag)->pset_sb_changed = 95*7c478bd9Sstevel@tonic-gate 0; 96*7c478bd9Sstevel@tonic-gate update_pset(sbag); 97*7c478bd9Sstevel@tonic-gate } else if (strcmp(sbag->sb_type, POOL_TYPE_NAME) == 0) { 98*7c478bd9Sstevel@tonic-gate return; 99*7c478bd9Sstevel@tonic-gate } else { 100*7c478bd9Sstevel@tonic-gate die(gettext(ERR_UNSUPP_STYPE), sbag->sb_type); 101*7c478bd9Sstevel@tonic-gate } 102*7c478bd9Sstevel@tonic-gate } 103*7c478bd9Sstevel@tonic-gate 104*7c478bd9Sstevel@tonic-gate /* 105*7c478bd9Sstevel@tonic-gate * callback function to property walker, copies the property value from 106*7c478bd9Sstevel@tonic-gate * the passed 'pvalue' to the corresponding field in the statistic data bag. 107*7c478bd9Sstevel@tonic-gate */ 108*7c478bd9Sstevel@tonic-gate /*ARGSUSED*/ 109*7c478bd9Sstevel@tonic-gate static int 110*7c478bd9Sstevel@tonic-gate populate_userdata_cb(pool_conf_t *unused1, pool_elem_t *unused2, 111*7c478bd9Sstevel@tonic-gate const char *name, pool_value_t *pval, userdata_cb_t *ud) 112*7c478bd9Sstevel@tonic-gate { 113*7c478bd9Sstevel@tonic-gate pset_statistic_bag_t *bag = (pset_statistic_bag_t *)ud->ud_bag; 114*7c478bd9Sstevel@tonic-gate 115*7c478bd9Sstevel@tonic-gate ud->ud_result = 0; 116*7c478bd9Sstevel@tonic-gate if (strcmp("pset.min", name) == 0) { 117*7c478bd9Sstevel@tonic-gate ud->ud_result = pool_value_get_uint64(pval, &bag->pset_sb_min); 118*7c478bd9Sstevel@tonic-gate } else if (strcmp("pset.max", name) == 0) { 119*7c478bd9Sstevel@tonic-gate ud->ud_result = pool_value_get_uint64(pval, &bag->pset_sb_max); 120*7c478bd9Sstevel@tonic-gate } else if (strcmp("pset.load", name) == 0) { 121*7c478bd9Sstevel@tonic-gate uint64_t load; 122*7c478bd9Sstevel@tonic-gate 123*7c478bd9Sstevel@tonic-gate ud->ud_result = pool_value_get_uint64(pval, &load); 124*7c478bd9Sstevel@tonic-gate bag->pset_sb_load = (double)load / 1000.0; 125*7c478bd9Sstevel@tonic-gate } else if (strcmp("pset.size", name) == 0) { 126*7c478bd9Sstevel@tonic-gate ud->ud_result = pool_value_get_uint64(pval, &bag->pset_sb_size); 127*7c478bd9Sstevel@tonic-gate } else if (strcmp("pset.sys_id", name) == 0) { 128*7c478bd9Sstevel@tonic-gate ud->ud_result = pool_value_get_int64(pval, &bag->pset_sb_sysid); 129*7c478bd9Sstevel@tonic-gate } 130*7c478bd9Sstevel@tonic-gate 131*7c478bd9Sstevel@tonic-gate return (0); 132*7c478bd9Sstevel@tonic-gate } 133*7c478bd9Sstevel@tonic-gate 134*7c478bd9Sstevel@tonic-gate /* 135*7c478bd9Sstevel@tonic-gate * Update statistic data for the procssor set with the name 'sbag->name'. 136*7c478bd9Sstevel@tonic-gate * Use 'sbag->bag' to store the data. 137*7c478bd9Sstevel@tonic-gate */ 138*7c478bd9Sstevel@tonic-gate static void 139*7c478bd9Sstevel@tonic-gate update_pset(statistic_bag_t *sbag) 140*7c478bd9Sstevel@tonic-gate { 141*7c478bd9Sstevel@tonic-gate pool_resource_t *pset_reso; 142*7c478bd9Sstevel@tonic-gate pool_elem_t *pset_elem; 143*7c478bd9Sstevel@tonic-gate userdata_cb_t ud; 144*7c478bd9Sstevel@tonic-gate 145*7c478bd9Sstevel@tonic-gate ud.ud_bag = (void *) sbag->bag; 146*7c478bd9Sstevel@tonic-gate if ((pset_reso = pool_get_resource(conf, PSET_TYPE_NAME, sbag->sb_name)) 147*7c478bd9Sstevel@tonic-gate == NULL) 148*7c478bd9Sstevel@tonic-gate die(gettext(ERR_STATS_RES_N), sbag->sb_name, get_errstr()); 149*7c478bd9Sstevel@tonic-gate if ((pset_elem = pool_resource_to_elem(conf, pset_reso)) == NULL) 150*7c478bd9Sstevel@tonic-gate die(gettext(ERR_STATS_RES_N), sbag->sb_name, get_errstr()); 151*7c478bd9Sstevel@tonic-gate 152*7c478bd9Sstevel@tonic-gate /* use the property walker to collect the resource properties */ 153*7c478bd9Sstevel@tonic-gate if (pool_walk_properties(conf, pset_elem, &ud, 154*7c478bd9Sstevel@tonic-gate (prop_walk_cb_t)populate_userdata_cb) == -1) 155*7c478bd9Sstevel@tonic-gate die(gettext(ERR_STATS_RES_N), sbag->sb_name, get_errstr()); 156*7c478bd9Sstevel@tonic-gate } 157