From 8555851160bcc5a04acfa8837af0977b82f7b500 Mon Sep 17 00:00:00 2001 From: John Crispin Date: Tue, 27 Aug 2013 14:50:45 +0200 Subject: [PATCH] add suport for measuring process resource usage Signed-off-by: John Crispin --- CMakeLists.txt | 2 +- instance.c | 14 +++++++++ measure.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ procd.h | 9 ++++++ 4 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 measure.c diff --git a/CMakeLists.txt b/CMakeLists.txt index b174a15..b7b0808 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -10,7 +10,7 @@ IF(APPLE) LINK_DIRECTORIES(/opt/local/lib) ENDIF() -SET(SOURCES main.c ubus.c service.c instance.c utils.c md5.c hotplug.c state.c mkdev.c early.c inittab.c preinit.c coldplug.c syslog.c log.c watchdog.c signal.c system.c debug.c rcS.c trigger.c) +SET(SOURCES main.c ubus.c service.c instance.c utils.c md5.c hotplug.c state.c mkdev.c early.c inittab.c preinit.c coldplug.c syslog.c log.c watchdog.c signal.c system.c debug.c rcS.c trigger.c measure.c) find_library(json NAMES json-c json) SET(LIBS ubox ubus ${json} blobmsg_json json_script) diff --git a/instance.c b/instance.c index d9530e5..45706ba 100644 --- a/instance.c +++ b/instance.c @@ -422,6 +422,7 @@ instance_init(struct service_instance *in, struct service *s, struct blob_attr * void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose) { void *i; + struct pid_info pi; i = blobmsg_open_table(b, in->name); blobmsg_add_u8(b, "running", in->proc.pending); @@ -430,5 +431,18 @@ void instance_dump(struct blob_buf *b, struct service_instance *in, int verbose) blobmsg_add_blob(b, in->command); if (verbose && in->trigger) blobmsg_add_blob(b, in->trigger); + if (!measure_process(in->proc.pid, &pi)) { + struct timespec tp; + long uptime; + + clock_gettime(CLOCK_MONOTONIC, &tp); + uptime = tp.tv_sec - in->start.tv_sec; + + blobmsg_add_u8(b, "ppid", pi.ppid); + blobmsg_add_u16(b, "uid", pi.uid); + blobmsg_add_u32(b, "fdcount", pi.fdcount); + blobmsg_add_u32(b, "vmsize", pi.vmsize); + blobmsg_add_u32(b, "uptime", uptime); + } blobmsg_close_table(b, i); } diff --git a/measure.c b/measure.c new file mode 100644 index 0000000..9e21a66 --- /dev/null +++ b/measure.c @@ -0,0 +1,99 @@ +/** + * Copyright (C) 2010 Steven Barth + * Copyright (C) 2013 John Crispin + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. + * + */ + + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "procd.h" + +static regex_t pat_vmsize, pat_ppid, pat_state, pat_uid; + +static void __attribute__((constructor)) measure_init() { + regcomp(&pat_vmsize, "VmSize:[ \t]*([0-9]*) kB", REG_EXTENDED); + regcomp(&pat_uid, "Uid:[ \t]*([0-9]*).*", REG_EXTENDED); + regcomp(&pat_ppid, "PPid:[ \t]*([0-9]+)", REG_EXTENDED); + regcomp(&pat_state, "State:[ \t]*([A-Z])", REG_EXTENDED); +} + +static void __attribute__((destructor)) measure_fini() { + regfree(&pat_vmsize); + regfree(&pat_ppid); + regfree(&pat_uid); + regfree(&pat_state); +} + +int measure_process(pid_t pid, struct pid_info *pi) { + int fd; + char buffer[512] = ""; + ssize_t rxed; + regmatch_t matches[2]; + glob_t gl; + int i; + + memset(pi, 0, sizeof(*pi)); + + snprintf(buffer, sizeof(buffer), "/proc/%i/fd/*", (int)pid); + + if (glob(buffer, GLOB_NOESCAPE | GLOB_MARK, NULL, &gl)) { + fprintf(stderr, "glob failed on %s\n", buffer); + return -1; + } + + for (i = 0; i < gl.gl_pathc; i++) + if (isdigit(basename(gl.gl_pathv[i])[0])) + pi->fdcount++; + globfree(&gl); + + snprintf(buffer, sizeof(buffer), "/proc/%i/status", (int)pid); + fd = open(buffer, O_RDONLY); + if (fd == -1) + return -1; + + rxed = read(fd, buffer, sizeof(buffer) - 1); + close(fd); + if (rxed == -1) + return -1; + + buffer[rxed] = 0; + + if (!regexec(&pat_state, buffer, 2, matches, 0)) + pi->stat = buffer[matches[1].rm_so]; + + if (!regexec(&pat_ppid, buffer, 2, matches, 0)) + pi->ppid = atoi(buffer + matches[1].rm_so); + + if (!regexec(&pat_uid, buffer, 2, matches, 0)) + pi->uid = atoi(buffer + matches[1].rm_so); + + if (!regexec(&pat_vmsize, buffer, 2, matches, 0)) + pi->vmsize = atoi(buffer + matches[1].rm_so) * 1024; + + return 0; +} diff --git a/procd.h b/procd.h index 4fd45f2..f7a333e 100644 --- a/procd.h +++ b/procd.h @@ -75,4 +75,13 @@ void trigger_event(char *type, struct blob_attr *data); void trigger_add(struct blob_attr *rule, void *id); void trigger_del(void *id); +struct pid_info { + char stat; + uint32_t ppid; + uint32_t fdcount; + uint32_t vmsize; + uint16_t uid; +}; +int measure_process(pid_t pid, struct pid_info *pi); + #endif -- 2.11.0