本节包含资源池接口的代码示例。
sysconf(3C) 提供有关整个系统中的 CPU 数的信息。以下示例提供了用于确定特定应用程序的池 pset 中所定义 CPU 数的粒度。
本示例的要点包括以下内容:
pvals[] 应该为以 NULL 结尾的数组。
pool_query_pool_resources() 返回与应用程序的池 my_pool 中的 pvals 数组类型 pset 匹配的所有资源的列表。由于池只能有一个 pset 资源实例,因此每个实例始终在 nelem 中返回。reslist[] 仅包含一个元素,即 pset 资源。
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 中定义的所有资源池。
该示例的要点包括以下内容:
采用 PO_RDONLY 以只读方式打开动态 conf 文件。pool_query_pools() 将池列表返回到 pl 中,并将池数目返回到 nelem 中。对于每个池,请调用 pool_get_property() 以将 pool.name 属性从元素放入 pval 值中。
pool_get_property() 将调用 pool_to_elem() 以将 libpool 实体转换为不透明的值。pool_value_get_string() 将从不透明的池值中获取字符串。
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);
该示例的要点包括以下内容:
pool_query_pool_resources() 将获取 rl 中的所有资源的列表。由于 pool_query_pool_resources() 的最后一个参数为 NULL,因此将返回所有资源。对于每种资源,系统都会读取并列显 name、load 和 size 属性。
对 strdup() 的调用将分配本地内存并复制由 get_string() 返回的字符串。对 get_string() 的调用将返回一个指针,该指针由对 get_property() 的下一次调用释放。如果不包括对 strdup() 的调用,则对字符串的后续引用将导致应用程序出现故障,并出现段错误。
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);
以下示例设置了 pset 的 pool.comment 属性。该示例还在 pool.newprop 中创建了新的属性。
该示例的要点包括以下内容:
在对 pool_conf_open() 的调用中,使用静态配置文件中的 PO_RDWR 要求调用方为超级用户。
要在运行此实用程序后提交对 pset 所做的更改,请发出 pooladm -c 命令。要使实用程序提交更改,请使用第二个非零参数调用 pool_conf_commit()。
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")'