From 3e4d6fe863330154fa10c42d3b4c2e5a50022e35 Mon Sep 17 00:00:00 2001 From: lemoer Date: Thu, 16 Mar 2017 01:50:23 +0100 Subject: [PATCH] gluon-debug: add new package to debug process runtime --- package/gluon-debug/Makefile | 29 +++++ .../files/lib/gluon/respondd/proc.cache | 1 + package/gluon-debug/src/Makefile | 6 ++ package/gluon-debug/src/respondd.c | 101 ++++++++++++++++++ 4 files changed, 137 insertions(+) create mode 100644 package/gluon-debug/Makefile create mode 100644 package/gluon-debug/files/lib/gluon/respondd/proc.cache create mode 100644 package/gluon-debug/src/Makefile create mode 100644 package/gluon-debug/src/respondd.c diff --git a/package/gluon-debug/Makefile b/package/gluon-debug/Makefile new file mode 100644 index 00000000..cbf4e644 --- /dev/null +++ b/package/gluon-debug/Makefile @@ -0,0 +1,29 @@ +include $(TOPDIR)/rules.mk + +PKG_NAME:=gluon-debug +PKG_VERSION:=1 + +PKG_BUILD_DIR := $(BUILD_DIR)/$(PKG_NAME) + +include $(GLUONDIR)/include/package.mk + + +define Package/gluon-debug + SECTION:=gluon + CATEGORY:=Gluon + TITLE:=Provides process runtime information to the network + DEPENDS:=+libgluonutil +respondd +endef + +define Build/Prepare + mkdir -p $(PKG_BUILD_DIR) + $(CP) ./src/* $(PKG_BUILD_DIR)/ +endef + +define Package/gluon-debug/install + $(CP) ./files/* $(1)/ + $(INSTALL_DIR) $(1)/lib/gluon/respondd + $(CP) $(PKG_BUILD_DIR)/respondd.so $(1)/lib/gluon/respondd/debug.so +endef + +$(eval $(call BuildPackage,gluon-debug)) diff --git a/package/gluon-debug/files/lib/gluon/respondd/proc.cache b/package/gluon-debug/files/lib/gluon/respondd/proc.cache new file mode 100644 index 00000000..5caff40c --- /dev/null +++ b/package/gluon-debug/files/lib/gluon/respondd/proc.cache @@ -0,0 +1 @@ +10000 diff --git a/package/gluon-debug/src/Makefile b/package/gluon-debug/src/Makefile new file mode 100644 index 00000000..0360f776 --- /dev/null +++ b/package/gluon-debug/src/Makefile @@ -0,0 +1,6 @@ +all: respondd.so + +CFLAGS += -Wall + +respondd.so: respondd.c + $(CC) $(CPPFLAGS) $(CFLAGS) $(LDFLAGS) -shared -fPIC -D_GNU_SOURCE -o $@ $^ $(LDLIBS) -lgluonutil diff --git a/package/gluon-debug/src/respondd.c b/package/gluon-debug/src/respondd.c new file mode 100644 index 00000000..c80e22a2 --- /dev/null +++ b/package/gluon-debug/src/respondd.c @@ -0,0 +1,101 @@ +/* + Copyright (c) 2016, Leonardo Mörlein + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + 1. Redistributions of source code must retain the above copyright notice, + this list of conditions and the following disclaimer. + 2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE + FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR + SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER + CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, + OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +*/ + +#include +#include +#include +#include "json-c/json.h" +#include + +#include +#include +#include +#include +#include + + +#include + +static struct json_object * respondd_provider_statistics(void) { + struct json_object *proc = json_object_new_object(); + + glob_t globbuf; + int ret = glob("/proc/[0-9]*/stat", 0, NULL, &globbuf); + if (ret != 0) + // we don't really care about the reason and simply return {} + goto end; + + for(int i=0; i < globbuf.gl_pathc; i++) { + FILE* f = fopen(globbuf.gl_pathv[i], "r"); + if (!f) + continue; + + int pid = 0; + unsigned long utime = 0; + unsigned long stime = 0; + long cutime = 0; + long cstime = 0; + + char name[64]; + + int cnt = fscanf(f, "%d %63s " + "%*c " + "%*d %*d %*d %*d %*d " + "%*u %*u %*u %*u %*u " + "%lu %lu %ld %ld", + &pid, name, &utime, &stime, &cutime, &cstime); + if (cnt != 6) + goto next; + + struct json_object *process = json_object_new_object(); + + json_object_object_add(process, "n", gluonutil_wrap_string(name)); + json_object_object_add(process, "u", json_object_new_int(utime)); + json_object_object_add(process, "s", json_object_new_int(stime)); + json_object_object_add(process, "cu", json_object_new_int(cutime)); + json_object_object_add(process, "cs", json_object_new_int(cstime)); + + char pidstr[8]; + snprintf(pidstr, 7, "%d", pid); + json_object_object_add(proc, pidstr, process); + + next: + fclose(f); + + // Normally this should fit in 1280 bytes (compressed). + // - Otherwise it will be fragmented. + if(i>80) + break; + } + +end: + globfree(&globbuf); + return proc; +} + +const struct respondd_provider_info respondd_providers[] = { + {"proc", respondd_provider_statistics}, + {} +};