Other ways of getting CPU% usage without Top?

Trying to write a small Yab program that displays my total CPU usage as a percentage.

I found “Top” which as some may know, is a command line application. When I typed:

Top --help

If appears there are very few options/arguments you can pass to it. Unlike Linux (I am aware that Haiku is not Linux), but they both use Bash and Top. Usually when Top is running, you can type on the keyboard to alter what data is seen, manipulate the application, etc, but Top on Haiku seems to be pretty bare-bones.

Is there any other way to get CPU usage? I tried coding some C++, but kept getting compiler errors like:

error: 'struct system_info' has no member named 'cpu_info'

While I’m somewhat familiar with C++ and Yab (and a tiny bit of Bash) I can’t seem to crack this one.

Thanks.

1 Like

Did you have a look at Haiku’s activitymonitor « apps « src - haiku - Haiku's main repository ?

1 Like

:open_mouth:

This is a great way for me to get started tackling this! Thank you!! Didn’t know how to get there :slight_smile:
Thanks humdinger!!

1 Like

Happy hacking! :slight_smile:

1 Like

So after you posted the link to Haiku’s main repository (Hooray!) I poked around and ended up finding top.cpp! Eureka!

I’m still learning C++, but I think I’m familiar enough with the language to hunt my way through top.cpp to get what I’m looking for. However, not 15 minutes into my attempt, I’ve found my first pitfall… perhaps you can help. When I compile my code, I get this:

~/Desktop> gcc -o cpu_monitor cpu_monitor.cpp
cpu_monitor.cpp:8:10: fatal error: termcap.h: No such file or directory
8 | #include "termcap.h"
  |           ^----------
compilation terminated.

I understand the difference between source code and compiled binary. I cannot find termcap.h anywhere in the Haiku Repository. Surely they used it when compiling “top.cpp” Why make a .cpp available and not a header dependency?

I found this termcap.h file Link buried in the repository ‘commit’ section after searching for the header under ‘log’, but when I save it as ‘termcap.h’ and compile with the header in the same directory, I still get errors.

Any ideas? Thanks humdinger

EDIT:

So I found termcap.h
path: root/headers/gnu

I’m going to leave my original question in case someone else out there in the internet world is facing the same struggle I was. I’ll let you know if I have an more programming Q’'s.

Ok! I’ve found my match. I can’t figure this one out…

File: Top.cpp

Here’s the code that is causing the issue:

/* SIGINT handler */
static void
sigint_handler(int)
{
	tputs(exit_ca_mode, 1, putchar);
	tputs(restore_cursor, 1, putchar);
	exit(1);
}

This is the command I used to build the program:

gcc -o cpu_monitor cpu_monitor.cpp -ltermcap

And here is the compiler whining:

~/Desktop> gcc -o cpu_monitor cpu_monitor.cpp
/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/13.3.0/../../../../x86_64-unknown-haiku/bin/ld: /tmp//ccxIAwMr.o: in function `sigint_handler(int)':
cpu_monitor.cpp:(.text+0x36): undefined reference to `tputs(char const*, int, int (*)(int))'
/boot/system/develop/tools/bin/../lib/gcc/x86_64-unknown-haiku/13.3.0/../../../../x86_64-unknown-haiku/bin/ld: cpu_monitor.cpp:(.text+0x51): undefined reference to `tputs(char const*, int, int (*)(int))'
collect2: error: ld returned 1 exit status

What my novice brain is reading from this error is that I am not fulfilling all the arguments I must pass to “tputs”? Maybe the compiler is sad because termcap.h isn’t in /boot/system/develop directory…
Help please :slight_smile:

Termcap is a C library, and the linker is trying to find a C++ function (you can see that because it shows undefined reference to a function with parameters, which is only possible in C++, in C there would be just the function name).

My guess would be a missing extern "C" { } block around the declaration of tputs (maybe the simplest way is to put the #include <termcap.h> inside such a block).

Thank you for pointing this out!

I used extern "C" { #include "termcap.h" } and it still throws the error when I compile.

I did make a bit of progress however. I discovered that ncurses.h does something similar to termcap. I went to HaikuDepot and found a package called ncurses6_devel I swiftly installed this, and to my delight, I see ncurses.h and termcap.h in boot/system/develop/headers awesome! I compiled my program, new error. Perhaps you can help me with this PulkoMandy…

~/Desktop/top> gcc -o top top.cpp
top.cpp: In function 'void sigint_handler(int)':
top.cpp:77:9: error: 'tputs' was not declared in this scope; did you mean 'puts'?
   77 |         tputs(exit_ca_mode, 1, putchar);
      |         ^~~~~
      |         puts
top.cpp: In function 'void init_term()':
top.cpp:89:9: error: 'tgetent' was not declared in this scope; did you mean 'getenv'?
   89 |         tgetent(buf, getenv("TERM"));
      |         ^~~~~~~
      |         getenv
top.cpp:90:24: error: 'tgetstr' was not declared in this scope; did you mean 'wgetstr'?
   90 |         exit_ca_mode = tgetstr("te", &entries);
      |                        ^~~~~~~
      |                        wgetstr
top.cpp:97:9: error: 'tputs' was not declared in this scope; did you mean 'puts'?
   97 |         tputs(save_cursor, 1, putchar);
      |         ^~~~~
      |         puts
top.cpp: In function 'void compare(ThreadTimeList*, ThreadTimeList*, bigtime_t, int)':
top.cpp:255:9: error: 'tputs' was not declared in this scope; did you mean 'put'?
  255 |         tputs(clear_string, 1, putchar);
      |         ^~~~~
      |         puts
top.cpp: In function 'int main(int, char**)':
top.cpp:415:9: error: 'tputs' was not declared in this scope; did you mean 'put'?
  415 |         tputs(exit_ca_mode, 1, putchar);
      |         ^~~~~
      |         puts

Still baffled that I can’t just download the source files, compile and voila!

Unless this has something to do with Jam. humdinger pointed out to me in a PM that the jamfile referenced the use of ncurses:

# standard commands that need libncurses.a
Includes [ FGristFiles top.cpp watch.c ]
	: [ BuildFeatureAttribute ncurses : headers ] ;

I know nothing about Jam or jamfile. I took a look at the readme, but still a bit lost. Need to figure out what that is all about and how to use MAKE with Jam.

I really appreciate the help everybody, this is bolstering my experience and helping me with my coding journey!

If you download the entire Haiku sourcecode, there is a README with instructions on how to use jam.

After you have set it up (you need to run the “configure” script with the right options), you can run “jam top” and this will compile top.

If you follow these instructions, it should just work. The jam files will take care of downloading and setting up the dependencies for you. If you get just one sourcefile and try to build it, you are a bit on your own to figure out how to do it. I wouldn’t recommend it if you are not already a bit familiar wiht the existing and documented process.