When you have a Linux embedded system and you’re programming in C (that is quite common), sometimes you need to calculate inside a program the resources usage for a given process ID such as CPU or memory; or in general you could need to know the network bandwith or the list of mounted devices.
The most commmon way for gathering this data is to parse out the files inside /proc/, but this can be tedious. There is a discussion at stackoverflow about it.
An alternative, if you already have or can install glib2, is to use LibGTop that “is a library to get system specific data such as CPU and Memory Usage and information about running Processes”.
Compiling and installing it in a PC is not a problem as the nice Beyond Linux From Scratch guide shows, but for cross-compiling it is not straight forward.
Here’s the simple procedure:
This library is built on top of other libraries such as glib2 and intltool. I wrote a post some time ago that could be helpul for compiling glib2. If you’re using buildroot, it already builds glib2 for you if selected.
As normal, set the environment variables that make our life easier. Change them according to your needs. These settings are for an ARM based processor:
$ export HOST=arm-linux $ export BUILD=i386-linux $ export PREFIX=/home/projects/rootfs/usr/local
Since I have an embedded system without graphical interface and it’s a single-core processor, I don’t need X nor the SMP parameter. Adjust it according to your system:
GLIB_CFLAGS="-I$PREFIX/usr/include/glib-2.0 -I$PREFIX/usr/lib/glib-2.0/include -I$PREFIX/usr/include" GLIB_LIBS="-L$PREFIX/lib" ./configure --build=$BUILD --host=$HOST --prefix=$PREFIX/usr --without-x --without-libgtop-smp --disable-gtk-doc-html --disable-nls
The examples/ directory doesn’t build properly unless you modify the Makefile. If you don’t want to compile this directory you can skip it by adding to the above configure command the parameter –with-examples.
Let’s assume that you want the examples/ directory:
Edit the file examples/Makefile:
Set the LIBS variables to the following value:
LIBS = -L/home/projects/rootfs/usr -L/home/projects/rootfs/usr/lib -lglib-2.0 -lintl
Compilation and installation
make make install
For compiling and linking a program that uses LibGTop2 you need to include the header files and the libraries. Here’s a fragment of the Makefile I use:
CC = arm-linux-gcc CFLAGS = -Wall -Os INCLUDES = -I. -I$(PREFIX)/usr/include -I$(PREFIX)/usr/include/glib-2.0 -I$(PREFIX)/usr/lib/glib-2.0/include -I$(PREFIX)/usr/include/libgtop-2.0 LDFLAGS = -L$(PREFIX)/usr/lib -L$(PREFIX)/usr/local/lib -lgobject-2.0 -lgmodule-2.0 -lgthread-2.0 -lglib-2.0 -lintl -lgtop-2.0 SRC = my_top.c OBJS = $(patsubst %.c, %.o, $(SRC)) # Link my_top: my_top.o @echo LD my_top @$(CC) $(LDFLAGS) $(OBJS) -o $@ # Pull in dependency info for *existing* .o files -include $(OBJS:.o=.d) # Compile all: $(OBJS) %.o: %.c @echo CC $*.c @$(CC) $(INCLUDES) -c $(CFLAGS) $*.c -o $*.o @$(CC) -MM $(CFLAGS) $*.c > $*.d
That’s it. Have fun!