提交 4724df19 编写于 作者: O openeuler-ci-bot 提交者: Gitee

!135 isulad: support oci isula stats

Merge pull request !135 from jing-rui/dev
{
"description": "runtime stats",
"type": "object",
"properties": {
"data": {
"type": "object",
"properties": {
"pids": {
"type": "object",
"properties": {
"current": {
"$ref": "../../defs.json#/definitions/uint64"
}
}
},
"cpu": {
"type": "object",
"properties": {
"usage": {
"type": "object",
"properties": {
"kernel": {
"$ref": "../../defs.json#/definitions/uint64"
},
"user": {
"$ref": "../../defs.json#/definitions/uint64"
},
"total": {
"$ref": "../../defs.json#/definitions/uint64"
}
}
}
}
},
"memory": {
"type": "object",
"properties": {
"usage": {
"type": "object",
"properties": {
"usage": {
"$ref": "../../defs.json#/definitions/uint64"
},
"limit": {
"$ref": "../../defs.json#/definitions/uint64"
}
}
}
}
}
}
}
}
}
......@@ -26,6 +26,7 @@
#include "engine.h"
#include "constants.h"
#include "shim_client_process_state.h"
#include "shim_client_runtime_stats.h"
#include "oci_runtime_state.h"
#include "isulad_config.h"
#include "utils_string.h"
......@@ -505,6 +506,60 @@ out:
return ret;
}
static int runtime_call_stats(const char *workdir, const char *runtime,
const char *id, struct engine_container_resources_stats_info *info)
{
char *stdout = NULL;
char *stderr = NULL;
shim_client_runtime_stats *stats = NULL;
struct parser_context ctx = {OPT_GEN_SIMPLIFY, 0};
parser_error perr = NULL;
runtime_exec_info rei = { 0 };
int ret = 0;
char *params[PARAM_NUM] = {0};
const char *opts[1] = {"--stats"};
runtime_exec_info_init(&rei, workdir, runtime, "events", opts, 1, id, params, PARAM_NUM);
if (!util_exec_cmd(runtime_exec_func, &rei, NULL, &stdout, &stderr)) {
ERROR("call runtime events --stats failed: %s", stderr);
ret = -1;
goto out;
}
if (stdout == NULL) {
ERROR("call runtime events --stats no stdout");
ret = -1;
goto out;
}
stats = shim_client_runtime_stats_parse_data(stdout, &ctx, &perr);
if (stats == NULL) {
ERROR("call runtime events --stats parse json failed");
ret = -1;
goto out;
}
if (stats != NULL && stats->data != NULL && stats->data->pids != NULL) {
info->pids_current = stats->data->pids->current;
}
if (stats != NULL && stats->data != NULL && stats->data->cpu != NULL && stats->data->cpu->usage) {
info->cpu_use_nanos = stats->data->cpu->usage->total;
info->cpu_system_use = stats->data->cpu->usage->kernel;
}
if (stats != NULL && stats->data != NULL && stats->data->memory != NULL && stats->data->memory->usage) {
info->mem_used = stats->data->memory->usage->usage;
info->mem_limit = stats->data->memory->usage->limit;
}
out:
free_shim_client_runtime_stats(stats);
UTIL_FREE_AND_SET_NULL(stdout);
UTIL_FREE_AND_SET_NULL(stderr);
UTIL_FREE_AND_SET_NULL(perr);
return ret;
}
static int runtime_call_simple(const char *workdir, const char *runtime,
const char *subcmd, const char **opts, size_t opts_len, const char *id)
{
......@@ -1095,13 +1150,34 @@ int rt_isula_listpids(const char *name, const char *runtime, const rt_listpids_p
return -1;
}
int rt_isula_resources_stats(const char *name, const char *runtime,
int rt_isula_resources_stats(const char *id, const char *runtime,
const rt_stats_params_t *params,
struct engine_container_resources_stats_info *rs_stats)
{
ERROR("isula stats not support on isulad-shim");
isulad_set_error_message("isula stats not support on isulad-shim");
char workdir[PATH_MAX] = {0};
int ret = 0;
if (id == NULL || runtime == NULL || params == NULL || rs_stats == NULL) {
ERROR("nullptr arguments not allowed");
return -1;
}
ret = snprintf(workdir, sizeof(workdir), "%s/%s", params->state, id);
if (ret < 0) {
ERROR("failed join full workdir %s/%s", params->rootpath, id);
goto out;
}
if (!shim_alive(workdir)) {
ERROR("shim dead %s", workdir);
ret = -1;
goto out;
}
ret = runtime_call_stats(workdir, runtime, id, rs_stats);
out:
return ret;
}
int rt_isula_resize(const char *id, const char *runtime, const rt_resize_params_t *params)
......
......@@ -82,6 +82,7 @@ typedef struct _rt_status_params_t {
typedef struct _rt_stats_params_t {
const char *rootpath;
const char *state;
} rt_stats_params_t;
typedef struct _rt_exec_params_t {
......
......@@ -867,6 +867,10 @@ void CRIRuntimeServiceImpl::PackContainerStatsAttributes(
container->mutable_attributes()->set_id(id);
auto status = ContainerStatus(std::string(id), error);
if (status == nullptr) {
return;
}
if (status->has_metadata()) {
std::unique_ptr<runtime::v1alpha2::ContainerMetadata> metadata(
new (std::nothrow) runtime::v1alpha2::ContainerMetadata(status->metadata()));
......
......@@ -424,6 +424,7 @@ static int get_containers_stats(char **idsarray, size_t ids_len, const struct st
if (is_running(cont->state)) {
rt_stats_params_t params = { 0 };
params.rootpath = cont->root_path;
params.state = cont->state_path;
nret = runtime_resources_stats(cont->common_config->id, cont->runtime, &params, &einfo);
if (nret != 0) {
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册