How to get the information from /proc/files from within a kernel module?
Hi all, I want process related information like rss.(we can get this by doing cat /proc/<pid>status) Is there a way to get this from within a kernel module? I checked out the sources on /proc filesystem. They all show how to create, read, write a new proc file. But I want to read from an existing proc file specifically /proc/<pid>/status I could not find rss info of a process from its task struct in kernel version 3.10. I tried to look inside kernel to know how and where are these values (rss and all) are calculated by kernel. But I could not track it. Can anyone please point me to either resources or guide me to accomplish the task of reading information from /proc/files... I looked up call_usermodehelper but did not find it useful for my need. Thanks !
On Mon, 25 Aug 2014 19:57:39 +0530, ravali pullela said:
I want process related information like rss.(we can get this by doing cat /proc/<pid>status) Is there a way to get this from within a kernel module? I checked out the sources on /proc filesystem. They all show how to create, read, write a new proc file. But I want to read from an existing proc file specifically /proc/<pid>/status
Don't bother reading the file. It's a *lot* faster and less buggy to just go get the same data yourself.
I could not find rss info of a process from its task struct in kernel version 3.10.
I tried to look inside kernel to know how and where are these values (rss and all) are calculated by kernel. But I could not track it.
You apparently didn't look very hard: [/usr/src/linux-next] grep RSS fs/proc/*.c fs/proc/array.c: rsslim = ACCESS_ONCE(sig->rlim[RLIMIT_RSS].rlim_ cur); fs/proc/base.c: [RLIMIT_RSS] = {"Max resident set", "bytes"}, fs/proc/task_mmu.c: "VmRSS:\t%8lu kB\n" fs/proc/task_mmu.c: * Proportional Set Size(PSS): my share of RSS. And 30 seconds of checking shows the first two to be rlimits not actual values, so you're interested in the third. Look at the source of fs/proc/task_mmu.c at function task_mem(). Getting a valid pointer to a 'struct mm_struct *' is left as an exercise for the student. :) The bigger question is, of course, what you intend to try to *do* with that info from inside a kernel module. Practical experience with several years of kernel newbies shows that 90% of the time, the idea is misguided and there's a better way to achieve the actual goal...
I want process related information like rss.(we can get this by doing cat /proc/<pid>status) Is there a way to get this from >> within a kernel module? I checked out the sources on /proc filesystem. They all show how to create, read, write a new
proc file. But I want to read from an existing proc file specifically /proc/<pid>/status
Have a look here: https://gist.github.com/Aruna-Hewapathirane /f41e7291e15bae6062a1 does exactly what your trying to do.
Can anyone please point me to either resources or guide me to accomplish the task of reading information from
/proc/files...
I would suggest you go through the source code for PS : http://procps. sourceforge.net/ and carefully study how things are done. This will also help: https://www.kernel.org/doc/Documentation/filesystems/ proc.txt That being said I have to agree fully with Valdis the best way is always to grep through the kernel that way you get to see code that is being used and how things are done. With all due respect to all the more experienced and knowledgeable folks on the list I had great difficulty when I first started out accessing information and I have no wish to see anyone who is fascinated by the kernel and wants to mess with the internals be it for fun as a hobby to learn or for serious patching and contributing to the kernel walk away frustrated like I did so many times. ( I always walked right back though... :) Yes Valdis you can give me a hard time on IRC when I next show up :) Aruna ( Ravali, please heed Valdis's advice that is the best way. I just pointed you in the right direction with a bit more code to play with but try to do your own digging first through the kernel and good luck ! )
Thank you so much Valdis and Aruna. Probably, the problem was I tried to search more on internet rather than kernel source and after 3 hours(I know it is long), I understood that I am nowhere going in the right direction. I have the mm_struct * with me. grepping is more useful. Thanks again to all the people who are supporting kernel newbies :) On Mon, Aug 25, 2014 at 10:12 PM, Aruna Hewapathirane < aruna.hewapathirane@gmail.com> wrote:
I want process related information like rss.(we can get this by doing cat /proc/<pid>status) Is there a way to get this from >> within a kernel module? I checked out the sources on /proc filesystem. They all show how to create, read, write a new
proc file. But I want to read from an existing proc file specifically /proc/<pid>/status
Have a look here: https://gist.github.com/Aruna-Hewapathirane /f41e7291e15bae6062a1 does exactly what your trying to do.
Can anyone please point me to either resources or guide me to accomplish the task of reading information from
/proc/files...
I would suggest you go through the source code for PS : http://procps. sourceforge.net/ and carefully study how things are done.
This will also help: https://www.kernel.org/doc/Documentation/filesystems/ proc.txt
That being said I have to agree fully with Valdis the best way is always to grep through the kernel that way you get to see code that is being used and how things are done.
With all due respect to all the more experienced and knowledgeable folks on the list I had great difficulty when I first started out accessing information and I have no wish to see anyone who is fascinated by the kernel and wants to mess with the internals be it for fun as a hobby to learn or for serious patching and contributing to the kernel walk away frustrated like I did so many times. ( I always walked right back though... :)
Yes Valdis you can give me a hard time on IRC when I next show up :)
Aruna ( Ravali, please heed Valdis's advice that is the best way. I just pointed you in the right direction with a bit more code to play with but try to do your own digging first through the kernel and good luck ! )
Hi, I tried to do the following. Calculate rss using virtual address to physical page mappings. But *the value calculated using this method is greater than the value reported by get_mm_rss*, the api which reports the process rss in /proc/pis/status. The way I tried to calculate rss is to count pages whose virtual to physical mappings are valid and for which page is present. I am doing this on the entire va space so that I can check every page if it is present and increment a counter. The problem is that when i looked inside get_mm_rss function, it returns get_mm_counter(mm, MM_FILEPAGES) + get_mm_counter(mm, MM_ANONPAGES); I thought that anonymous and file pages cover all vm areas like stack, heap, code, data etc., but it does not look like. So, what is it that I am missing. Note: The value calculated by my module is approximately same as the sum of rss reported by /proc/pid/smaps for all vmas. Thanks On Mon, Aug 25, 2014 at 11:07 PM, ravali pullela <rpravali069@gmail.com> wrote:
Thank you so much Valdis and Aruna. Probably, the problem was I tried to search more on internet rather than kernel source and after 3 hours(I know it is long), I understood that I am nowhere going in the right direction.
I have the mm_struct * with me. grepping is more useful.
Thanks again to all the people who are supporting kernel newbies :)
On Mon, Aug 25, 2014 at 10:12 PM, Aruna Hewapathirane < aruna.hewapathirane@gmail.com> wrote:
I want process related information like rss.(we can get this by doing cat /proc/<pid>status) Is there a way to get this from >> within a kernel module? I checked out the sources on /proc filesystem. They all show how to create, read, write a new
proc file. But I want to read from an existing proc file specifically /proc/<pid>/status
Have a look here: https://gist.github.com/Aruna-Hewapathirane /f41e7291e15bae6062a1 does exactly what your trying to do.
Can anyone please point me to either resources or guide me to accomplish the task of reading information from
/proc/files...
I would suggest you go through the source code for PS : http://procps. sourceforge.net/ and carefully study how things are done.
This will also help: https://www.kernel.org/doc/Documentation/filesystems /proc.txt
That being said I have to agree fully with Valdis the best way is always to grep through the kernel that way you get to see code that is being used and how things are done.
With all due respect to all the more experienced and knowledgeable folks on the list I had great difficulty when I first started out accessing information and I have no wish to see anyone who is fascinated by the kernel and wants to mess with the internals be it for fun as a hobby to learn or for serious patching and contributing to the kernel walk away frustrated like I did so many times. ( I always walked right back though... :)
Yes Valdis you can give me a hard time on IRC when I next show up :)
Aruna ( Ravali, please heed Valdis's advice that is the best way. I just pointed you in the right direction with a bit more code to play with but try to do your own digging first through the kernel and good luck ! )
On Sat, 30 Aug 2014 01:21:12 +0530, ravali pullela said:
The problem is that when i looked inside get_mm_rss function, it returns get_mm_counter(mm, MM_FILEPAGES) + get_mm_counter(mm, MM_ANONPAGES);
I thought that anonymous and file pages cover all vm areas like stack, heap, code, data etc., but it does not look like. So, what is it that I am missing.
Perhaps nothing. Perhaps everything. Perhaps RSS isn't even the right knob to be looking at. There's different ways to calculate RSS, and which one you use depends on what question you're trying to answer, and what problem you're trying to solve. Which is why your method agrees with what summing /proc/self/smaps has, but doesn't match what get_mm_counter() is telling you. In particular, things like shared pages need different handling based on what the problem is - if N processes share the page, does each process get charged 1 per page, or 1/N per page? So what problem are you trying to solve by knowing the RSS of a process, and why does a module care? (Apologies if I missed the post)
I am trying to calculate rss just to get an understanding and insights into handling memory in kernel. On Sat, Aug 30, 2014 at 1:53 AM, <Valdis.Kletnieks@vt.edu> wrote:
On Sat, 30 Aug 2014 01:21:12 +0530, ravali pullela said:
The problem is that when i looked inside get_mm_rss function, it returns get_mm_counter(mm, MM_FILEPAGES) + get_mm_counter(mm, MM_ANONPAGES);
I thought that anonymous and file pages cover all vm areas like stack, heap, code, data etc., but it does not look like. So, what is it that I am missing.
Perhaps nothing. Perhaps everything. Perhaps RSS isn't even the right knob to be looking at.
There's different ways to calculate RSS, and which one you use depends on what question you're trying to answer, and what problem you're trying to solve. Which is why your method agrees with what summing /proc/self/smaps has, but doesn't match what get_mm_counter() is telling you.
In particular, things like shared pages need different handling based on what the problem is - if N processes share the page, does each process get charged 1 per page, or 1/N per page?
So what problem are you trying to solve by knowing the RSS of a process, and why does a module care? (Apologies if I missed the post)
participants (3)
-
Aruna Hewapathirane -
ravali pullela -
Valdis.Kletnieks@vt.edu