X-Git-Url: http://git.linux-vserver.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Fvserver-stat.c;h=43e15f372b6594819b55102d452fba3bf9bd9df7;hb=148ab3024621f97b9cf95091657db2448098bc66;hp=9f038713000272fa3edcfdbd86e3012c93f05e6b;hpb=4c5e881f765be86bb1da5ce4d3fcc86effc5fdc7;p=util-vserver.git diff --git a/src/vserver-stat.c b/src/vserver-stat.c index 9f03871..43e15f3 100644 --- a/src/vserver-stat.c +++ b/src/vserver-stat.c @@ -24,6 +24,7 @@ #include "vserver.h" #include "util.h" #include "internal.h" +#include "pathconfig.h" #include @@ -116,7 +117,7 @@ showHelp(char const *cmd) WRITE_STR(1, cmd); WRITE_MSG(1, "\n" - "Show informations about all the active context.\n\n" + "Show information about all active contexts.\n\n" " CTX# Context number\n" " #0 = root context\n" " #1 = monitoring context\n" @@ -189,6 +190,12 @@ getUptime() return secs*1000 + msecs; } +static inline uint64_t +toMsec(uint64_t v) +{ + return v*1000llu/hertz; +} + static int cmpData(void const *xid_v, void const *map_v) { @@ -279,10 +286,172 @@ registerXidVstat(struct Vector *vec, unsigned long xid_l) } } -static inline uint64_t -toMsec(uint64_t v) +static void +registerXidCgroups(struct Vector *vec, struct process_info *process) { - return v*1000llu/hertz; + xid_t xid = (xid_t) process->s_context; + struct XidData *res; + + switch (vc_getXIDType(xid)) { + case vcTYPE_STATIC: + case vcTYPE_DYNAMIC: + break; + default: + return; + } + + res = Vector_search(vec, &xid, cmpData); + if (res == 0) { + struct vc_rlimit_stat limit; + struct vc_virt_stat vstat; + struct vc_sched_info sched; + int cpu; + char vhi_name[65], + filename[128], + cgroup[65], + buf[30]; + int fd; + ssize_t cgroup_len; + unsigned long long rss = 0; + char *endptr; + size_t len; + uint64_t stime_total, utime_total; + + + if (vc_virt_stat(xid, &vstat) == -1) { + perror("vc_virt_stat()"); + return; + } + if (vc_rlimit_stat(xid, RLIMIT_NPROC, &limit) == -1) { + perror("vc_rlimit_stat(RLIMIT_NRPOC)"); + return; + } + if (vc_get_vhi_name(xid, vcVHI_CONTEXT, vhi_name, sizeof(vhi_name)) == -1) { + perror("vc_get_vhi_name(CONTEXT)"); + return; + } + + if ((fd = open(DEFAULTCONFDIR "/cgroup/mnt", O_RDONLY)) == -1) { + strcpy(cgroup, "/dev/cgroup/"); + cgroup_len = sizeof("/dev/cgroup"); + } + else { + cgroup_len = read(fd, cgroup, sizeof(cgroup)); + if (cgroup_len == -1) { + perror("read(cgroup/mnt)"); + return; + } + close(fd); + cgroup[cgroup_len] = '/'; + cgroup_len += 1; + cgroup[cgroup_len] = 0; + } + + len = strlen(vhi_name); + if ((len + sizeof("/cgroup/name")) >= sizeof(filename)) { + WRITE_MSG(2, "too long context name: "); + WRITE_STR(2, vhi_name); + WRITE_MSG(2, "\n"); + return; + } + strcpy(filename, vhi_name); + strcpy(filename + len, "/cgroup/name"); + + if ((fd = open(filename, O_RDONLY)) == -1) { + char *dir = strrchr(vhi_name, '/'); + if (dir == NULL) { + WRITE_MSG(2, "invalid context name: "); + WRITE_STR(2, dir); + WRITE_MSG(2, "\n"); + return; + } + len = strlen(dir); + if ((len + cgroup_len) >= sizeof(cgroup)) { + WRITE_MSG(2, "cgroup name too long: "); + WRITE_STR(2, dir); + WRITE_MSG(2, "\n"); + return; + } + strcpy(cgroup + cgroup_len, dir); + cgroup_len += len; + } + else { + ssize_t ret; + ret = read(fd, cgroup + cgroup_len, sizeof(cgroup) - cgroup_len); + if (ret == -1) { + perror("read(cgroup/name)"); + return; + } + cgroup_len += ret; + close(fd); + } + + if ((cgroup_len + sizeof("/memory.usage_in_bytes")) > sizeof(filename)) { + WRITE_MSG(2, "cgroup name too long: "); + WRITE_STR(2, cgroup); + WRITE_MSG(2, "\n"); + return; + } + strcpy(filename, cgroup); + strcpy(filename + cgroup_len, "/memory.usage_in_bytes"); + + if ((fd = open(filename, O_RDONLY)) == -1) + perror("open(memory.usage_in_bytes)"); + else { + if (read(fd, buf, sizeof(buf)) == -1) { + perror("read(memory.usage_in_bytes)"); + return; + } + close(fd); + if ((rss = strtoull(buf, &endptr, 0)) == ULLONG_MAX || + (*endptr != '\n' && *endptr != '\0')) { + perror("strtoull(memory.usage_in_bytes)"); + return; + } + } + + strcpy(filename, cgroup); + strcpy(filename + cgroup_len, "/cpuacct.stat"); + + if ((fd = open(filename, O_RDONLY)) == -1) { + utime_total = 0; + stime_total = 0; + // XXX: arbitrary CPU limit. + for (cpu = 0; cpu < 1024; cpu++) { + sched.cpu_id = cpu; + sched.bucket_id = 0; + if (vc_sched_info(xid, &sched) == -1) + break; + + utime_total += sched.user_msec; + stime_total += sched.sys_msec; + } + } + else { + if (read(fd, buf, sizeof(buf)) == -1) { + perror("read(cpuacct.stat)"); + return; + } + close(fd); + + if (sscanf(buf, "user %llu\nsystem %llu\n", &utime_total, &stime_total) != 2) { + perror("sscanf(cpuacct.stat)"); + return; + } + } + + res = Vector_insert(vec, &xid, cmpData); + res->xid = xid; + + res->process_count = limit.value; + res->VmRSS_total = rss / 4096; + res->start_time_oldest= getUptime() - vstat.uptime/1000000; + + res->utime_total = toMsec(utime_total); + res->stime_total = toMsec(stime_total); + } + + res->VmSize_total += process->VmSize; } @@ -628,6 +797,8 @@ int main(int argc, char **argv) closedir(proc_dir); } else { + void (*handler)(struct Vector *vec, struct process_info *process); + my_pid = getpid(); if (!switchToWatchXid(&errptr)) { @@ -641,6 +812,11 @@ int main(int argc, char **argv) " procfs-security. Please read the FAQ for more details\n" " http://linux-vserver.org/Proc-Security\n"); + if (vc_isSupported(vcFEATURE_MEMCG)) + handler = registerXidCgroups; + else + handler = registerXid; + Echdir(PROC_DIR_NAME); proc_dir = Eopendir("."); while ((dir_entry = readdir(proc_dir)) != NULL) @@ -652,7 +828,7 @@ int main(int argc, char **argv) if (atoi(dir_entry->d_name) != my_pid) { struct process_info * info = get_process_info(dir_entry->d_name); if (info) - registerXid(&xid_data, info); + handler(&xid_data, info); } } closedir(proc_dir);