Re: Task_Struct->mm->RSS different from ps -aux RSS

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Hi Irfan,

On 9/21/06, Irfan Habib <irfan.habib@xxxxxxxxx > wrote:
Hi,

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?

" 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.

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?

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 <linux/kernel.h>
 #include <linux/module.h>
 #include <linux/sched.h>
 #include <linux/mm.h>

 static int pid_mem = 1;

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: <NULL>, 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
suman


[Index of Archives]     [Newbies FAQ]     [Linux Kernel Mentors]     [Linux Kernel Development]     [IETF Annouce]     [Git]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux RAID]     [Linux SCSI]     [Linux ACPI]
  Powered by Linux