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.


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, ie /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! Ie 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.


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?


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.

链接地址: http://www.djcxy.com/p/35052.html

上一篇: 为什么我的程序泄漏虚拟内存?

下一篇: 跟踪线程内存和CPU消耗