提交 e4c1027d 编写于 作者: N nocjj

proc: get qemu process info from procfs

Read /proc/qemupid/stat file to refresh process info,
include: parent pid, userspace time, task state and etc.
According to these data, add two display itemsi to vmtop:
S: task state
P: physical processor that task is runnning on
Signed-off-by: NJiajun Chen <1250062498@qq.com>
上级 a4d9aca3
......@@ -3,5 +3,7 @@ noinst_LIBRARIES=libdomain.a
libdomain_a_SOURCES= \
domain.c \
domain.h \
proc.c \
proc.h \
utils.c \
utils.h
......@@ -17,10 +17,10 @@
#include <dirent.h>
#include "domain.h"
#include "utils.h"
#include "proc.h"
#define VAR_RUN_QEMU_PATH "/var/run/libvirt/qemu"
#define PID_STRING_MAX 12
#define BUF_SIZE 1024
#define CGROUP_PATH_SIZE 30
/* domain list operation */
......@@ -114,6 +114,9 @@ static int set_domain(struct domain *dom, const char *name)
if (dom->domain_id < 0) {
return -1;
}
if (get_proc_stat(dom) < 0) {
return -1;
}
return 1;
}
......
......@@ -14,14 +14,7 @@
#define SRC_DOMAIN_H
#include <unistd.h>
#define DOMAIN_NAME_MAX 256
struct domain {
int domain_id;
char vmname[DOMAIN_NAME_MAX];
pid_t pid;
};
#include "type.h"
struct domain_list {
struct domain *domains;
......
......@@ -23,7 +23,9 @@ const char *filter_help = ""
FID fields[] = {
/* name . flag . align */
{"VMname", FIELDS_DISPLAY, 8 },
{"DID", FIELDS_DISPLAY, 5 },
{"PID", FIELDS_DISPLAY, 8 }
{"VMname", FIELDS_DISPLAY, 14 },
{"DID", FIELDS_DISPLAY, 5 },
{"PID", FIELDS_DISPLAY, 8 },
{"S", FIELDS_DISPLAY, 5 },
{"P", FIELDS_DISPLAY, 5 }
};
......@@ -20,6 +20,8 @@ enum fields_type {
FD_VMNAME,
FD_DID,
FD_PID,
FD_STATE,
FD_P,
FD_END
};
......
/******************************************************************************
* 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 procfs data
********************************************************************************/
#include <stdio.h>
#include <string.h>
#include "type.h"
#include "proc.h"
#include "utils.h"
#define STAT_PATH_SIZE 40
struct file_item proc_stab[] = {
#define GF(f) (DFUN_T)GET_NAME(f)
{"%c", GF(state)},
{"%d", GF(ppid)},
{"%d", GF(pgrd)},
{"%d", GF(session)},
{"%d", GF(tty)},
{"%d", GF(tpgid)},
{"%lu", GF(flags)},
{"%lu", GF(min_flt)},
{"%lu", GF(cmin_flt)},
{"%lu", GF(maj_flt)},
{"%lu", GF(cmaj_flt)},
{"%llu", GF(utime)},
{"%llu", GF(stime)},
{"%llu", GF(cutime)},
{"%llu", GF(cstime)},
{"%ld", GF(priority)},
{"%ld", GF(nice)},
{"%d", GF(nlwp)},
{"%ld", GF(alarm)},
{"%llu", GF(start_time)},
{"%lu", GF(vsize)},
{"%ld", GF(rss)},
{"%lu", GF(rss_rlim)},
{"%lu", GF(start_code)},
{"%lu", GF(end_code)},
{"%lu", GF(start_stack)},
{"%lu", GF(kstk_esp)},
{"%lu", GF(kstk_eip)},
{"%*s", NULL}, /* discard signal */
{"%*s", NULL}, /* discard blocked */
{"%*s", NULL}, /* discard sigignore */
{"%*s", NULL}, /* discard sigcatch */
{"%lu", GF(wchan)},
{"%*u", NULL}, /* dsicard nswap */
{"%*u", NULL}, /* discard cnswap */
{"%d", GF(exit_signal)},
{"%d", GF(processor)},
{"%lu", GF(rtprio)},
{"%lu", GF(sched)}
#undef GF
};
const int stat_size = sizeof(proc_stab) / sizeof(struct file_item);
int get_proc_stat(struct domain *dom)
{
char buf[BUF_SIZE];
char path[STAT_PATH_SIZE];
char *tmp = NULL;
char *p = NULL;
char *p_next = NULL;
int i = 0;
if (snprintf(path, STAT_PATH_SIZE, "/proc/%lu/stat", dom->pid) < 0) {
return -1;
}
if (read_file(buf, BUF_SIZE, path) < 0) {
return -1;
}
/* read from state item of "...) S ..." */
tmp = strrchr(buf, ')');
tmp = tmp + 2;
for (p = strtok_r(tmp, " \t\r\n", &p_next); p && i < stat_size;
p = strtok_r(NULL, " \t\r\n", &p_next)) {
if (proc_stab[i].get_fun != NULL) {
sscanf(p, proc_stab[i].format, (*proc_stab[i].get_fun)(dom));
}
++i;
}
return 1;
}
/******************************************************************************
* 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.
********************************************************************************/
#ifndef SRC_PROC_H
#define SRC_PROC_H
GET_FUN(state)
GET_FUN(ppid)
GET_FUN(pgrd)
GET_FUN(session)
GET_FUN(tty)
GET_FUN(tpgid)
GET_FUN(flags)
GET_FUN(min_flt)
GET_FUN(cmin_flt)
GET_FUN(maj_flt)
GET_FUN(cmaj_flt)
GET_FUN(utime)
GET_FUN(stime)
GET_FUN(cutime)
GET_FUN(cstime)
GET_FUN(priority)
GET_FUN(nice)
GET_FUN(nlwp)
GET_FUN(alarm)
GET_FUN(start_time)
GET_FUN(vsize)
GET_FUN(rss)
GET_FUN(rss_rlim)
GET_FUN(start_code)
GET_FUN(end_code)
GET_FUN(start_stack)
GET_FUN(kstk_esp)
GET_FUN(kstk_eip)
GET_FUN(wchan)
GET_FUN(exit_signal)
GET_FUN(processor)
GET_FUN(rtprio)
GET_FUN(sched)
int get_proc_stat(struct domain *dom);
#endif
/******************************************************************************
* 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.
********************************************************************************/
#ifndef SRC_TYPE_H
#define SRC_TYPE_H
#include <unistd.h>
typedef void *(*DFUN_T)(const void *);
typedef void *(DFUN_VOID_T)(void *, void *);
typedef unsigned long long u64;
#define DOMAIN_NAME_MAX 256
#define BUF_SIZE 1024
#define DELTA_VALUE(v) delta_ ## v
#define DFX_VALUE(v) v, DELTA_VALUE(v)
#define GET_NAME(v) domain_get_ ## v
#define GET_FUN(v) \
static inline void *GET_NAME(v)(struct domain *dom) \
{ \
return (void *)(&(dom->v)); \
}
#define GET_DELTA_NAME(v) domain_get_delta_ ## v
#define GET_DELTA_FUN(v) \
static inline void *GET_DELTA_NAME(v)(struct domain *dom) \
{ \
return (void *)(&(dom->DELTA_VALUE(v))); \
}
struct file_item {
const char *format;
DFUN_T get_fun;
DFUN_T delta_fun;
};
struct domain {
int domain_id;
char vmname[DOMAIN_NAME_MAX];
pid_t pid;
char state;
int
ppid,
pgrd,
session,
tty,
tpgid,
nlwp,
exit_signal,
processor;
long int
priority,
nice,
alarm,
rss;
unsigned int
flags,
min_flt,
cmin_flt,
maj_flt,
cmaj_flt,
vsize,
rss_rlim,
start_code,
end_code,
start_stack,
kstk_esp,
kstk_eip,
rtprio,
sched,
wchan;
u64
utime,
stime,
cutime,
cstime,
start_time;
};
#endif
......@@ -38,7 +38,7 @@ int read_file(char *buf, int bufsize, const char *path)
buf[len] = '\0';
}
close(fd);
return 1;
return len;
}
int get_time_str(char *buf, int bufsize)
......
......@@ -101,6 +101,14 @@ static void print_domain_field(struct domain *dom, int field)
printw("%*lu", fields[i].align, dom->pid);
break;
}
case FD_STATE: {
printw("%*c", fields[i].align, dom->state);
break;
}
case FD_P: {
printw("%*d", fields[i].align, dom->processor);
break;
}
default:
break;
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册