Skip to content

Commit 3fdce4a

Browse files
committed
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 <jwkozaczuk@gmail.com>
1 parent c297ddf commit 3fdce4a

File tree

5 files changed

+56
-0
lines changed

5 files changed

+56
-0
lines changed

modules/sysinfo/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sysinfo*.so

modules/sysinfo/Makefile

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
INCLUDES = -I../../include
2+
COMMON_FLAGS = -g -Wall -fPIC $(INCLUDES) -O2
3+
CXXFLAGS = -std=c++11 $(COMMON_FLAGS)
4+
5+
.PHONY: module
6+
module: sysinfo.so
7+
8+
quiet = $(if $V, $1, @echo " $2"; $1)
9+
10+
sysinfo.so: sysinfo.cc
11+
$(call quiet, $(CXX) $(CXXFLAGS) -shared -o $@ sysinfo.cc, LINK $@)
12+
13+
clean:
14+
rm -f sysinfo.so sysinfo.d

modules/sysinfo/module.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
from osv.modules import api
2+
3+
daemon = api.run_on_init('/sysinfo.so --interval 1000000 &!')
4+
default = daemon

modules/sysinfo/sysinfo.cc

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
#include <iostream>
2+
#include <sys/sysinfo.h>
3+
#include <unistd.h>
4+
#include <functional>
5+
#include <osv/options.hh>
6+
7+
static void usage()
8+
{
9+
std::cout << "Allowed options:\n";
10+
std::cout << " --interval arg (=1000000) repeat every N microseconds\n";
11+
}
12+
13+
static void handle_parse_error(const std::string &message)
14+
{
15+
std::cout << message << std::endl;
16+
usage();
17+
exit(1);
18+
}
19+
20+
int main(int ac, char** av)
21+
{
22+
int interval_in_usecs = 1000000;
23+
24+
auto options_values = options::parse_options_values(ac - 1, av + 1, handle_parse_error);
25+
if (options::option_value_exists(options_values, "interval")) {
26+
interval_in_usecs = options::extract_option_int_value(options_values, "interval", handle_parse_error);
27+
}
28+
29+
while (1) {
30+
struct sysinfo info;
31+
sysinfo(&info);
32+
std::cout << "--> memory, total: " << info.totalram / 0x100000 << " MiB, used: " <<
33+
(info.totalram - info.freeram) / 0x100000 << " MiB" << std::endl;
34+
usleep(interval_in_usecs);
35+
}
36+
}

modules/sysinfo/usr.manifest

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/sysinfo.so: ${MODULE_DIR}/sysinfo.so

0 commit comments

Comments
 (0)