domain.c 8.6 KB
Newer Older
N
nocjj 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/******************************************************************************
 * Copyright (c) Huawei Technologies Co., Ltd. 2020-2020. All rights reserved.
 * vmtop licensed under the Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan PSL v2.
 * You may obtain a copy of Mulan PSL v2 at:
 *     http://license.coscl.org.cn/MulanPSL2
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR
 * PURPOSE.
 * See the Mulan PSL v2 for more details.
 * Description: get domain's data
 ********************************************************************************/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dirent.h>
#include "domain.h"
#include "utils.h"
N
nocjj 已提交
20
#include "proc.h"
21
#include "vcpu_stat.h"
N
nocjj 已提交
22 23 24 25

#define VAR_RUN_QEMU_PATH "/var/run/libvirt/qemu"
#define PID_STRING_MAX 12
#define CGROUP_PATH_SIZE 30
26
#define TASK_STRING_SIZE 30
N
nocjj 已提交
27 28 29 30

/* domain list operation */
void init_domains(struct domain_list *list)
{
31 32 33 34 35 36 37 38 39 40 41
    list->domains = NULL;
    list->num = 0;
}
void clear_domains(struct domain_list *list)
{
    for (int i = 0; i < list->num; i++) {
        if (list->domains[i].threads != NULL) {
            free(list->domains[i].threads);
            list->domains[i].threads = NULL;
        }
    }
N
nocjj 已提交
42 43 44
    if (list->domains != NULL) {
        free(list->domains);
    }
45
    init_domains(list);
N
nocjj 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
}

static struct domain *add_domains(struct domain_list *list)
{
    struct domain *new_list = malloc(sizeof(struct domain) * (list->num + 1));

    if (new_list == NULL) {
        return NULL;
    }
    memset(new_list, 0, sizeof(struct domain) * (list->num + 1));
    memcpy(new_list, list->domains, sizeof(struct domain) * list->num);
    free(list->domains);
    list->domains = new_list;
    list->num++;
    return &(list->domains[list->num - 1]);
}

N
nocjj 已提交
63 64
static void copy_domains(struct domain_list *now, struct domain_list *pre)
{
65
    clear_domains(pre);
N
nocjj 已提交
66
    pre->num = now->num;
67 68 69
    if (pre->num <= 0) {
        return;
    }
N
nocjj 已提交
70 71 72 73 74 75
    pre->domains = malloc(sizeof(struct domain) * pre->num);
    if (pre->domains == NULL) {
        pre->num = 0;
        return;
    }
    memcpy(pre->domains, now->domains, sizeof(struct domain) * pre->num);
76
    for (int i = 0; i < pre->num; i++) {
77 78 79
        if (pre->domains[i].nlwp <= 0) {
            continue;
        }
80 81 82 83 84 85 86 87
        pre->domains[i].threads = malloc(sizeof(struct domain) *
                                         pre->domains[i].nlwp);
        if (pre->domains[i].threads == NULL) {
            continue;
        }
        memcpy(pre->domains[i].threads, now->domains[i].threads,
               sizeof(struct domain) * pre->domains[i].nlwp);
    }
N
nocjj 已提交
88 89
}

N
nocjj 已提交
90 91 92 93 94
static void pop_domains(struct domain_list *list)
{
    list->num--;
}

N
nocjj 已提交
95 96 97 98 99 100
/*
 * get domain from domain list
 */
static struct domain *get_domain_from_id(int id, struct domain_list *list)
{
    for (int i = 0; i < list->num; i++) {
101
        if (list->domains != NULL && list->domains[i].domain_id == id) {
N
nocjj 已提交
102 103 104 105 106 107
            return &(list->domains[i]);
        }
    }
    return NULL;
}

108 109 110 111 112 113 114 115 116 117
static struct domain *get_thread_from_pid(pid_t pid, struct domain *dom)
{
    for (int i = 0; i < dom->nlwp; i++) {
        if (dom->threads != NULL && dom->threads[i].pid == pid) {
            return &(dom->threads[i]);
        }
    }
    return NULL;
}

N
nocjj 已提交
118 119 120 121 122 123 124 125
static int get_id_from_cgroup(pid_t pid)
{
    char path[CGROUP_PATH_SIZE];
    FILE *fp = NULL;
    char buf[BUF_SIZE];
    char *tmp = NULL;
    int id = -1;

126
    if (snprintf(path, CGROUP_PATH_SIZE, "/proc/%u/cgroup", pid) < 0) {
N
nocjj 已提交
127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150
        return id;
    }
    fp = fopen(path, "r");
    if (fp == NULL) {
        return id;
    }
    /* parse id from "cpuset:machine.slice/machine-qemu\x2d$id" */
    while (fgets(buf, BUF_SIZE - 1, fp)) {
        if (strstr(buf, "cpuset:") == NULL) {
            memset(buf, 0, BUF_SIZE);
            continue;
        }
        tmp = strstr(buf, "machine-qemu\\x2d");
        if (tmp == NULL) {
            continue;
        }
        tmp += strlen("machine-qemu\\x2d");
        id = atoi(tmp);
        break;
    }
    fclose(fp);
    return id;
}

151 152 153 154 155 156 157 158
static int get_child_pid(struct domain *dom)
{
    char path[TASK_STRING_SIZE];
    DIR *dirptr = NULL;
    struct dirent *dirt = NULL;
    char *end = NULL;
    int i = 0;

159
    if (snprintf(path, TASK_STRING_SIZE, "/proc/%u/task", dom->pid) < 0) {
160 161 162 163 164 165
        return -1;
    }
    dirptr = opendir(path);
    if (dirptr == NULL) {
        return -1;
    }
166 167 168
    if (dom->nlwp <= 0) {
        return -1;
    }
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
    dom->threads = (struct domain *)malloc(sizeof(struct domain) * dom->nlwp);
    if (dom->threads == NULL) {
        closedir(dirptr);
        return -1;
    }
    memset(dom->threads, 0, sizeof(struct domain) * dom->nlwp);

    while ((dirt = readdir(dirptr)) != NULL && i < dom->nlwp) {
        if (!strcmp(dirt->d_name, ".") || !strcmp(dirt->d_name, "..")) {
            continue;
        }
        if (dirt->d_type != DT_DIR) {
            continue;
        }
        dom->threads[i].pid = strtoul(dirt->d_name, &end, 10);
        if (end <= dirt->d_name || dom->threads[i].pid < 1) {
            dom->threads[i].pid = 0;
            continue;
        }
        dom->threads[i].type = ISTHREAD;
        dom->threads[i].ppid = dom->pid;
        if (get_proc_stat(&(dom->threads[i])) < 0 ||
            get_proc_comm(&(dom->threads[i])) < 0) {
            continue;
        }
194 195 196 197
        if (strstr(dom->threads[i].vmname, "CPU") != NULL
            && get_vcpu_stat(&(dom->threads[i])) > 0) {
            dom->threads[i].type = ISVCPU;
        }
198 199 200 201 202 203
        i++;
    }
    closedir(dirptr);
    return dom->nlwp;
}

N
nocjj 已提交
204 205 206
static int set_domain(struct domain *dom, const char *name)
{
    int len = strlen(name);
207
    char path[BUF_SIZE];
N
nocjj 已提交
208 209
    char pid[PID_STRING_MAX];
    char *end = NULL;
N
nocjj 已提交
210

N
nocjj 已提交
211 212 213 214 215 216
    if (len >= DOMAIN_NAME_MAX - 1) {
        return -1;
    }
    strcpy(dom->vmname, name);
    dom->vmname[len - strlen(".pid")] = '\0';

217
    if (snprintf(path, BUF_SIZE, "%s/%s", VAR_RUN_QEMU_PATH, name) < 0) {
N
nocjj 已提交
218 219 220 221 222 223 224 225 226 227 228 229 230 231
        return -1;
    }
    if (read_file(pid, PID_STRING_MAX, path) < 0) {
        return -1;
    }
    dom->pid = strtoul(pid, &end, 10);
    if (end <= pid || dom->pid < 1) {
        return -1;
    }

    dom->domain_id = get_id_from_cgroup(dom->pid);
    if (dom->domain_id < 0) {
        return -1;
    }
232 233
    dom->type = ISDOMAIN;
    if (get_proc_stat(dom) < 0 || get_child_pid(dom) < 0) {
N
nocjj 已提交
234 235
        return -1;
    }
N
nocjj 已提交
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278
    return 1;
}

/*
 * check whether filename end with ".pid"
 */
static int check_pid_file(const char *d_name)
{
    char *extern_name = ".pid";
    int extern_len = strlen(extern_name);
    int len = strlen(d_name);

    return strcmp(d_name + len - extern_len, extern_name);
}

static int get_qemu_id(struct domain_list *list)
{
    DIR *dir = NULL;
    struct dirent *dirent = NULL;

    dir = opendir(VAR_RUN_QEMU_PATH);
    if (dir == NULL) {
        return -1;
    }
    while ((dirent = readdir(dir)) != NULL) {
        if (!strcmp(dirent->d_name, ".") || !strcmp(dirent->d_name, "..")) {
            continue;
        }
        if (dirent->d_type != DT_REG) {
            continue;
        }
        if (check_pid_file(dirent->d_name)) {
            continue;
        }
        struct domain *dom = add_domains(list);
        if (set_domain(dom, dirent->d_name) < 0) {
            pop_domains(list);
        }
    }
    closedir(dir);
    return list->num;
}

279 280 281 282 283 284 285 286 287
static void refresh_threads(struct domain *dom, struct domain *old_dom)
{
    for (int i = 0; i < dom->nlwp; i++) {
        int pid = dom->threads[i].pid;
        struct domain *old_thread = get_thread_from_pid(pid, old_dom);
        if (old_thread == NULL) {
            continue;
        }
        refresh_delta_stat(&(dom->threads[i]), old_thread);
288 289
        if (dom->threads[i].type == ISVCPU) {
            refresh_delta_vcpu_stat(&(dom->threads[i]), old_thread);
N
nocjj 已提交
290
            sum_vcpu_stat(dom, &(dom->threads[i]));
291
        }
292 293 294
    }
}

N
nocjj 已提交
295
int refresh_domains(struct domain_list *now, struct domain_list *pre)
N
nocjj 已提交
296 297 298
{
    int num;

N
nocjj 已提交
299
    copy_domains(now, pre);    /* save last data int pre */
300
    clear_domains(now);
N
nocjj 已提交
301 302 303 304 305 306 307 308 309 310
    num = get_qemu_id(now);

    for (int i = 0; i < now->num; i++) {
        int id = now->domains[i].domain_id;
        struct domain *old_domain = get_domain_from_id(id, pre);

        if (old_domain == NULL) {
            continue;
        }
        refresh_delta_stat(&(now->domains[i]), old_domain);
311
        refresh_threads(&(now->domains[i]), old_domain);
N
nocjj 已提交
312
    }
N
nocjj 已提交
313 314 315

    return num;
}
316 317 318 319 320 321 322 323 324 325 326

int get_task_num(struct domain_list *list)
{
    int sum = 0;

    for (int i = 0; i < list->num; i++) {
        sum += list->domains[i].nlwp;
    }
    sum += list->num;
    return sum;
}