RAM quantification, process CPU usage in C under Linux

How to find out how much RAM and processor "eat" a particular process in Linux? And how to find out all running processes (including daemons and system ones)? =)

UPD: using the C language

+4
source share
2 answers

Use top or ps .

For example, ps aux will list all processes along with their owner, state, used memory, etc.

EDIT: To do this using C under Linux, you need to read the process files in the proc file system. For example, /proc/1/status contains information about your init process (which always has PID 1 ):

 char buf[512]; unsigned long vmsize; const char *token = "VmSize:"; FILE *status = fopen("/proc/1/status", "r"); if (status != NULL) { while (fgets(buf, sizeof(buf), status)) { if (strncmp(buf, token, strlen(token)) == 0) { sscanf(buf, "%*s %lu", &vmsize); printf("The INIT process' VM size is %lu kilobytes.\n", vmsize); break; } } fclose(status); } 
+5
source

Measuring how many bars a process uses is almost impossible. The difficulty is that each piece of a ram is not used by exactly one process, and not all bars that the process uses are actually “owned” by them.

For example, two processes may have common mappings of the same file, in which case any pages that are in the kernel for display will “belong” to both processes. But what if only one of these processes used it?

Private pages can also be copied to a record if the process is forked, or if they have been displayed but not yet used (consider the case when the process has malloc'd a huge area, but has not affected most of it). In this case, which process owns these pages?

Processes can also efficiently use parts of the buffer cache and many other types of kernel buffers that do not belong to them.


Two dimensions are available, which are the size of the virtual machine (the amount of memory that has been processed now) and the size of the resident set (RSS). None of them actually talks about how much memory this process uses, because they both count shared pages and don't count non-displayed pages.

So is there an answer? Some of these can be measured by examining the page map structures that are now available in / proc (/ proc / pid / pagemap), but there is no need for a trivial way to separate the "ownership" of shared pages.

See Linux Documentation / vm / pagemap.txt for a discussion of this.

+2
source

All Articles