1

I am trying to achieve CPU and Memory usage information of current process, inside a system call. I can get current process name, pid and uid by using :

current->comm //process name
current->pid //process id
current_uid() //uid

but that seems to be all.(I am using kernel 3.2.0-24-generic)

As I have seen from Memory usage of current process in C, reading(vfs_read) and parsing /proc/pid/status seems to be the only option to get memory and cpu usage.

Is there a better way to obtain this information, or am I on the right track?

I also test my code as a kernel module first, since both system calls and kernel modules are running in kernel space. Is that also bad approach?

Community
  • 1
  • 1
  • There are many other interesting files under `/proc/` in particular `/proc/self/stat` and `/proc/self/maps` and `/proc/self/statm` etc; you could also use `clock_gettime` or `getrusage` or `times` or `gettimeofday` system calls ... – Basile Starynkevitch May 26 '12 at 13:50
  • Thanks for answers, stat, maps and statm will be helpful if i try to parse values.But i could not manage to use getrusage since I do not have sys/resource.h and can not find it anywhere. –  May 26 '12 at 14:10
  • You probably need to install appropriate developer packages, like `libc6-dev` or something similar [details are distribution specific], to get `` header file. – Basile Starynkevitch May 26 '12 at 14:16

1 Answers1

0

current->mm is the place where all memory information is stored.
current->mm->mmap is a list of memory mappings for the process, so you can iterate it and see what you find there.

current->utime and current->stime may be useful for getting CPU information.

ugoren
  • 16,023
  • 3
  • 35
  • 65