简体   繁体   中英

I want to get execution time from task_struct

In a module I am accessing the task_struct and returning with stime+utime. I want to convert it to milliseconds. In what format stime and utime will be present in task_struct. I can also access it from /proc//stat. Are the both unit are different.

Advanced Unix Programming by Marc Rochkind covers this topic to some degree (page 55-ish, if I remember correctly). Pardon me if I paraphrase what he states better.

utime represents user time, and is the time spent executing instructions. It is CPU time only and doesn't include time spent waiting to run.

stime is the CPU time spent executing system calls on behalf of the process.

the units are in clock ticks.

clock ticks per second can be determined with the sysconf system call.

I hope this helps.

Given a task_struct , the total on-cpu time in nanonseconds is stored in task->se.sum_exec_runtime .

This does not appear to add up to task->utime+task->stime unless first adjusted via task_cputime_adjusted(task, &utime, &stime) .

For anyone else trying to implement this kind of functionality, I highly recommend reading proc(5) . In this case, searching for utime leads you to /proc/[pid]/stat (or proc/self/stat ) which provides utime in its 14th column. The implementation can be found in fs/proc/array.c which is where I found the call to task_cputime_adjusted . Verify module output against eg /proc/[pid]/stat | awk '{ print $14 } /proc/[pid]/stat | awk '{ print $14 } for utime .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM