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 If you want to see him visit this site
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
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 .....
static int pid_mem ;
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);
return 0;
static void pages_unload(void)
printk("\nbye bye\n");

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
fill_kobj_path: path = '/module/pages'
the process id to look up as 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?

How to find PAGE SIZE in linux kernel

Sometimes i was finding what is the PAGE SIZE of my linux kernel? . I knew that it could be 4KB,8KB,16KB or 64KB. But the the real question was , how do i find the value? After lot of serching, i came to know the following command from Rik Van Riel (kernelnewbies mailing list)..
[suman@ajit ~]$ getconf PAGE_SIZE
it says that i am using 4KB PAGE SIZE. Now i went to find out where the PAGE SIZE has been wriiten into the source code. I found it /include/asm-i386/page.h in my kernel-source(2.6.17) directory.My system architecture is i386 machine. I could see
#ifndef _I386_PAGE_H
#define _I386_PAGE_H
/* PAGE_SHIFT determines the page size */
#define PAGE_SHIFT 12
i see that PAGE_SIZE depends on the value of PAGE_SHIFT. i yet to see if i change the PAGE_SHIFT value,what would be the impact of my kernel? Let see..i will be informing....

Also i came to know that PAGE SIZE varries on architecture to architecure. Here is the code snapshot for ia64 archi.
 #ifndef _ASM_IA64_PAGE_H
#define _ASM_IA64_PAGE_H
* PAGE_SHIFT determines the actual kernel page size. */
#if defined(CONFIG_IA64_PAGE_SIZE_4KB)
# define PAGE_SHIFT 12
#elif defined(CONFIG_IA64_PAGE_SIZE_8KB)
# define PAGE_SHIFT 13
#elif defined(CONFIG_IA64_PAGE_SIZE_16KB)
# define PAGE_SHIFT 14
#elif defined(CONFIG_IA64_PAGE_SIZE_64KB)
# define PAGE_SHIFT 16
So ia64 machine can use 4KB,8KB,16KB or 64KB depends upon the configuration during compilation.