Thursday, September 21, 2006

Know rss field in ps command also mm_struct in linux kernel

This post is for intended to be answer of one mail from kernelnewbies..Let me tell the question which was asked by someone..
"Task_Struct->mm->RSS different from ps -aux RSS kernelNewbiesHi,
I'm developing kernel modules for educational purposes, and I was trying to find out the amount of memory used by a process. When you go ps -aux it shows for example that a certain process is using 1.7% of memory and has an RSS of 16000, however when I go to the concerned task_struct and printk the rss from mm, I get a value which is in hundreds, compared to thousands? For finding out the memory three items in the mm_struct look interesting to me: RSS, Total_VM and vm_lock, will adding them
together give me the memory used by a process?
regards Irfan "
Rik Van Riel ( i think u knows this guy) if not so please visit his home page http://www.surriel.com/ If you want to see him visit this site http://www.redhat.com/magazine/014dec05/features/xen/Xen_Rik.jpg
Frankly speaking i am little bit fan of this guy.Anyway he has given the answer "The RSS reported by ps is in kilobytes, right ?The kernel counts in pages, which are larger".
Before that i also didn't look at the rss filed value and also franklt speaking i also didn't know what is the exact meaning of this value. I started hunting(should i say hacking!! Ok little bit) the answer..ofcource Rik's clue has given me the thought and i found the answer but still i am not confirmed which i have given explation regarding this question.i am waiting reply from Rik...i will update this blog if I and You find any wrong explanation or information. Please feel free to write comments. I am directly pasting mail which i have send to kernelnewbies mailing list ...
Hi Irfan,

On 9/21/06, Irfan Habib wrote: Hi,
" man ps "says me that
"The SIZE and RSS fields don't count some parts of a process including the page tables, kernel stack, struct thread_info, and struct task_struct. This is usually at least 20 KiB of memory that is always resident. SIZE is the virtual size of the process (code+data+stack). "
and also rss is for "resresident set size, the non-swapped physical memory that a task has used (in kiloBytes). " it means that the total size of the pages currently in memory.Which kernel r u using? I couldn't get rss fileld in mm_struct. I am using 2.6.17. I could get
mm_counter_t _file_rss;
mm_counter_t _anon_rss;
unsigned long hiwater_rss; /* High-watermark of RSS usage */
i think _file_rss and _anon_rss are for holding the value of the total number of resident pages in memory.So size of (_file_rss+_anon_file) should be equal to rss fileld value in ps command for particular process.Now let me prove:
my system i can get output for ps -aux .
[root@ajit pages_program]# ps -aux
Warning: bad syntax, perhaps a bogus '-'? See /usr/share/doc/procps-3.2.6/FAQ
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.1 1984 660 ? Ss 12:11 0:00 init [5]
root 2 0.0 0.0 0 0 ? SN 12:11 0:00 [ksoftirqd/0]
root 3 0.0 0.0 0 0 ? S 12:11 0:00 [watchdog/0]
[.....]
Now i can see that RSS is 660kb. In my system i am using 4kb page size. So total number of memory resident page is =660/4=165.Now i have written a small kernel module .....
#include
#include
#include
#include
static int pid_mem ;
MODULE_LICENSE("GPL");
MODULE_AUTHOR("Suman Adak");
module_param(pid_mem, int, 0);
static void print_no_pages(struct task_struct *task)
{
struct mm_struct *mm;
mm = task->mm;
printk("\nThis mm_struct has %d vmas.\n", mm->map_count);
printk("No of pages = %ld",mm->_file_rss + mm->_anon_rss);
}
static int pages_load(void){
struct task_struct *task;
printk("\n the process id to look up as %d.\n", pid_mem);
for_each_process(task) {
if ( task->pid == pid_mem) {
printk("%s[%d]\n", task->comm, task->pid);
print_no_pages(task);
}
}
return 0;
}
static void pages_unload(void)
{
printk("\nbye bye\n");
}
module_init(pages_load);
module_exit(pages_unload);

i just cut my dmesg here
root@ajit pages_program]# /sbin/insmod pages.ko pid_mem=1
[root@ajit pages_program]# dmesg
kobject pages: registering. parent: , set: module
kobject_uevent
fill_kobj_path: path = '/module/pages'
the process id to look up as 1.
init[1]
This mm_struct has 23 vmas.
No of pages = 165
I could see that process with pid 1 , It has 23 vmas and total no memory resident pages is 165, which is equal to rss field in ps command.i think you understand..So rss and size field doesn't give the exact size of the process...
Rik ,correct me if i am wrong., please clarify me ...How do we set value for hiwater_rss?
Thank

1 comment:

Nilesh said...

great work