raboof / nethogs

Linux 'net top' tool
GNU General Public License v2.0
3.19k stars 289 forks source link

100% CPU usage in libnethogs when invoking multiple times in an application. Fix suggested. #253

Open daniel64 opened 1 year ago

daniel64 commented 1 year ago

Hi, I've written a a small application using libnethogs. It sometimes loops using 100% CPU when invoked multiple times within the same application (that is, leaving the nethogs screen and going back in, within the same program).

I think I've tracked down the problem - when using high CPU, vector pc_loop_fd_list contains duplicate fd's and so also in pc_loop_fd_set, causing select() in procedure wait_for_next_trigger() to immediately return. I assume these duplicates are from a previous invocation of libnethogs.

I've added pc_loop_fd_list.clear() in procedure nethogsmonitor_init() which seems to have fixed the problem. Perhaps you would kind enough to consider adding this fix. Maybe other static variables also need re-initialising in such circumstances.

Also, is there a way to get messages and errors back from libnethogs via an error buffer/call? At the moment it writes to the screen with cout/cerr/stdout/stderr which kind of messes up the ncurses output.

Regards, Daniel