Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tracking threads memory and CPU consumption

I'm writing a Linux application which observes other applications and tracks consumption of resources . I'm planning work with Java, but programming language isn't important for me. The Goal is important, so I can switch to another technology or use modules. My application runs any selected third party application as child process. Mostly child software solves some algorithm like graphs, string search, etc. Observer program tracks child's resources while it ends the job.

If child application is multi-threaded, maybe somehow is possible to track how much resources consumes each thread? Application could be written using any not distributive-memory threads technology: Java threads, Boost threads, POSIX threads, OpenMP, any other.

like image 988
Pawka Avatar asked Sep 16 '09 07:09

Pawka


3 Answers

In modern Linux systems (2.6), each thread has a separate identifier that has nearly the same treatment as the pid. It is shown in the process table (at least, in htop program) and it also has its separate /proc entry, i.e. /proc/<tid>/stat.

Check man 5 proc and pay particular attention to stat, statm, status etc. You should find the information you're interested in there.

An only obstacle is to obtain this thread identifier. It is different with the process id! I.e. getpid() calls in all threads return the same value. To get the actual thread identifier, you should use (within a C program):

pid_t tid = syscall(SYS_gettid);

By the way, java virtual machine (at least, its OpenJDK Linux implementation) does that internally and uses it for debugging purposes in its back-end, but doesn't expose it to the java interface.

like image 189
P Shved Avatar answered Nov 16 '22 03:11

P Shved


Memory is not allocated to threads, and often shared across threads. This makes it generally impossible and at least meaningless to talk about the memory consumption of a thread.

An example could be a program with 11 threads; 1 creating objects and 10 using those objects. Most of the work is done on those 10 threads, but all memory was allocated on the one thread that created the objects. Now how does one account for that?

like image 27
MSalters Avatar answered Nov 16 '22 02:11

MSalters


If you're willing to use Perl take a look at this: Sys-Statistics-Linux

I used it together with some of the GD graphing packages to generate system resource usage graphs for various processes.

One thing to watch out for - you'll really need to read up on /proc and understand jiffies - last time I looked they're not documented correctly in the man pages, you'll need to read kernel source probably:

http://lxr.linux.no/#linux+v2.6.18/include/linux/jiffies.h

Also, remember that in Linux the only difference between a thread and process is that threads share memory - other than that they're identical in how the kernel implements them.

like image 29
Robert S. Barnes Avatar answered Nov 16 '22 03:11

Robert S. Barnes