From: Waldemar Kozaczuk <[email protected]> Committer: Waldemar Kozaczuk <[email protected]> Branch: master
modules: add simple sysinfo utility This adds simple tiny sysinfo utility which once added to an image will periodically report system memory utilization every 1 second by default which can be changed by parameter. Eventually sysinfo can be enhanced to report CPU utilization as well. Signed-off-by: Waldemar Kozaczuk <[email protected]> --- diff --git a/modules/sysinfo/.gitignore b/modules/sysinfo/.gitignore --- a/modules/sysinfo/.gitignore +++ b/modules/sysinfo/.gitignore @@ -0,0 +1 @@ +sysinfo*.so diff --git a/modules/sysinfo/Makefile b/modules/sysinfo/Makefile --- a/modules/sysinfo/Makefile +++ b/modules/sysinfo/Makefile @@ -0,0 +1,14 @@ +INCLUDES = -I../../include +COMMON_FLAGS = -g -Wall -fPIC $(INCLUDES) -O2 +CXXFLAGS = -std=c++11 $(COMMON_FLAGS) + +.PHONY: module +module: sysinfo.so + +quiet = $(if $V, $1, @echo " $2"; $1) + +sysinfo.so: sysinfo.cc + $(call quiet, $(CXX) $(CXXFLAGS) -shared -o $@ sysinfo.cc, LINK $@) + +clean: + rm -f sysinfo.so sysinfo.d diff --git a/modules/sysinfo/module.py b/modules/sysinfo/module.py --- a/modules/sysinfo/module.py +++ b/modules/sysinfo/module.py @@ -0,0 +1,4 @@ +from osv.modules import api + +daemon = api.run_on_init('/sysinfo.so --interval 1000000 &!') +default = daemon diff --git a/modules/sysinfo/sysinfo.cc b/modules/sysinfo/sysinfo.cc --- a/modules/sysinfo/sysinfo.cc +++ b/modules/sysinfo/sysinfo.cc @@ -0,0 +1,36 @@ +#include <iostream> +#include <sys/sysinfo.h> +#include <unistd.h> +#include <functional> +#include <osv/options.hh> + +static void usage() +{ + std::cout << "Allowed options:\n"; + std::cout << " --interval arg (=1000000) repeat every N microseconds\n"; +} + +static void handle_parse_error(const std::string &message) +{ + std::cout << message << std::endl; + usage(); + exit(1); +} + +int main(int ac, char** av) +{ + int interval_in_usecs = 1000000; + + auto options_values = options::parse_options_values(ac - 1, av + 1, handle_parse_error); + if (options::option_value_exists(options_values, "interval")) { + interval_in_usecs = options::extract_option_int_value(options_values, "interval", handle_parse_error); + } + + while (1) { + struct sysinfo info; + sysinfo(&info); + std::cout << "--> memory, total: " << info.totalram / 0x100000 << " MiB, used: " << + (info.totalram - info.freeram) / 0x100000 << " MiB" << std::endl; + usleep(interval_in_usecs); + } +} diff --git a/modules/sysinfo/usr.manifest b/modules/sysinfo/usr.manifest --- a/modules/sysinfo/usr.manifest +++ b/modules/sysinfo/usr.manifest @@ -0,0 +1 @@ +/sysinfo.so: ${MODULE_DIR}/sysinfo.so -- You received this message because you are subscribed to the Google Groups "OSv Development" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/osv-dev/0000000000005ca264059d127005%40google.com.
