Solaris 10 资源管理器开发者指南

资源池代码示例

本节包含资源池接口的代码示例。

确定资源池中的 CPU 数

sysconf(3C) 提供有关整个系统中的 CPU 数的信息。以下示例提供了用于确定特定应用程序的池 pset 中所定义 CPU 数的粒度。

本示例的要点包括以下内容:

pool_value_t *pvals[2] = {NULL};  /* pvals[] should be NULL terminated */



/* NOTE: Return value checking/error processing omitted */

/* in all examples for brevity */



conf_loc = pool_dynamic_location();

conf = pool_conf_alloc();

pool_conf_open(conf, conf_loc, PO_RDONLY);

my_pool_name = pool_get_binding(getpid());

my_pool = pool_get_pool(conf, my_pool_name);

pvals[0] = pool_value_alloc();

pvals2[2] = { NULL, NULL };

pool_value_set_name(pvals[0], "type");

pool_value_set_string(pvals[0], "pset");



reslist = pool_query_pool_resources(conf, my_pool, &nelem, pvals);

pool_value_free(pvals[0]);

pool_query_resource_components(conf, reslist[0], &nelem, NULL);

printf("pool %s: %u cpu", my_pool_ name, nelem);

pool_conf_close(conf);

列出所有的资源池

以下示例列出了在应用程序的池 pset 中定义的所有资源池。

该示例的要点包括以下内容:

conf	= pool_conf_alloc();

pool_conf_open(conf, pool_dynamic_location(), PO_RDONLY);

pl = pool_query_pools(conf, &nelem, NULL);

pval = pool_value_alloc();

for (i = 0; i < nelem; i++) {

    pool_get_property(conf, pool_to_elem(conf, pl[i]), "pool.name", pval);

    pool_value_get_string(pval, &fname);

    printf("%s\n", name);

}

pool_value_free(pval);

free(pl);

pool_conf_close(conf);

报告给定池的池统计信息

以下示例报告了指定池的统计信息。

该示例的要点包括以下内容:

printf("pool %s\n:" pool_name);

pool = pool_get_pool(conf, pool_name);

rl = pool_query_pool_resources(conf, pool, &nelem, NULL);

for (i = 0; i < nelem; i++) {

  pool_get_property(conf, pool_resource_to_elem(conf, rl[i]), "type", pval);

  pool_value_get_string(pval, &type);

  type = strdup(type);

  snprintf(prop_name, 32, "%s.%s", type, "name");

  pool_get_property(conf, pool_resource_to_elem(conf, rl[i]), 

        prop_name, pval);

  pool_value_get_string(val, &res_name);

  res_name = strdup(res_name);

  snprintf(prop_name, 32, "%s.%s", type, "load");

  pool_get_property(conf, pool_resource_to_elem(conf, rl[i]), 

        prop_name, pval);

  pool_value_get_uint64(val, &load);

  snprintf(prop_name, 32, "%s.%s", type, "size");

  pool_get_property(conf, pool_resource_to_elem(conf, rl[i]), 

        prop_name, pval);

  pool_value_get_uint64(val, &size);

  printf("resource %s: size %llu load %llu\n", res_name, size, load);

  free(type);

  free(res_name);

}

free(rl);

设置 pool.comment 属性并添加新属性

以下示例设置了 psetpool.comment 属性。该示例还在 pool.newprop 中创建了新的属性。

该示例的要点包括以下内容:

pool_set_comment(const char *pool_name, const char *comment)

{

  pool_t *pool;

  pool_elem_t *pool_elem;

  pool_value_t *pval = pool_value_alloc(); 

  pool_conf_t  *conf = pool_conf_alloc();

  /* NOTE: need to be root to use PO_RDWR on static configuration file */

  pool_conf_open(conf, pool_static_location(), PO_RDWR);

  pool = pool_get_pool(conf,  pool_name);

  pool_value_set_string(pval, comment);

  pool_elem = pool_to_elem(conf, pool);

  pool_put_property(conf, pool_elem, "pool.comment", pval);

  printf("pool %s: pool.comment set to %s\n:" pool_name, comment);

  /* Now, create a new property, customized to installation site */

  pool_value_set_string(pval, "New String Property");

  pool_put_property(conf, pool_elem, "pool.newprop", pval);

  pool_conf_commit(conf, 0); /* NOTE: use 0 to ensure only */

                             /* static file gets updated */

  pool_value_free(pval);

  pool_conf_close(conf);

  pool_conf_free(conf);

  /* NOTE: Use "pooladm -c" later, or pool_conf_commit(conf, 1) */

  /* above for changes to the running system */

}

另一种修改池的注释并添加新的池属性的方法是使用 poolcfg(1M)。

poolcfg -c 'modify pool pool-name (string pool.comment = "cmt-string")'

poolcfg -c 'modify pool pool-name (string pool.newprop = 

                                   "New String Property")'