Question

I have a process that spawns some other processes, I want to use the time command on a specific process and get the same output as the time command.

Is that possible and how?

Was it helpful?

Solution

I want to use the time command on a specific process and get the same output as the time command.

Probably it is enough just to use pidstat to get user and sys time:

$ pidstat -p 30122 1 4
Linux 2.6.32-431.el6.x86_64 (hostname)        05/15/2014      _x86_64_        (8 CPU)

04:42:28 PM       PID    %usr %system  %guest    %CPU   CPU  Command
04:42:29 PM     30122  706.00   16.00    0.00  722.00     3  has_serverd
04:42:30 PM     30122  714.00   12.00    0.00  726.00     3  has_serverd
04:42:31 PM     30122  714.00   14.00    0.00  728.00     3  has_serverd
04:42:32 PM     30122  708.00   16.00    0.00  724.00     3  has_serverd
Average:        30122  710.50   14.50    0.00  725.00     -  has_serverd

If not then according to strace time uses wait4 system call (http://linux.die.net/man/2/wait4) to get information about a process from the kernel. The same info returns getrusage but you cannot call it for an arbitrary process according to its documentation (http://linux.die.net/man/2/getrusage).

So, I do not know any command that will give the same output. However it is feasible to create a bash script that gets PID of the specific process and outputs something like time outpus then

This script does these steps:

1) Get the number of clock ticks per second

getconf CLK_TCK

I assume it is 100 and 1 tick is equal to 10 milliseconds.

2) Then in loop do the same sequence of commands while exists the directory /proc/YOUR-PID:

while [ -e "/proc/YOUR-PID" ];
do
  read USER_TIME SYS_TIME REAL_TIME <<< $(cat /proc/PID/stat | awk '{print $14, $15,  $22;}')
  sleep 0.1
end loop

Some explanation - according to man proc :

user time: ($14) - utime - Amount of time that this process has been scheduled in user mode, measured in clock ticks
sys time: ($15) - stime - Amount of time that this process has been scheduled in kernel mode, measured in clock ticks
starttime ($22) - The time in jiffies the process started after system boot.

3) When the process is finished get finish time

   read FINISH_TIME <<< $(cat '/proc/self/stat' | awk '{print $22;}') 

And then output:

  • the real time = ($FINISH_TIME-$REAL_TIME) * 10 - in milliseconds
  • user time: ($USER_TIME/(getconf CLK_TCK)) * 10 - in milliseconds
  • sys time: ($SYS_TIME/(getconf CLK_TCK)) * 10 - in milliseconds

I think it should give roughly the same result as time. One possible problem I see is if the process exists for a very short period of time.

This is my implementation of time:

#!/bin/bash
# Uses herestrings

print_res_jeffies()
{
  let "TIME_M=$2/60000"
  let "TIME_S=($2-$TIME_M*60000)/1000"
  let "TIME_MS=$2-$TIME_M*60000-$TIME_S*1000"
  printf "%s\t%dm%d.%03dms\n" $1 $TIME_M $TIME_S $TIME_MS
}

print_res_ticks()
{
  let "TIME_M=$2/6000"
  let "TIME_S=($2-$TIME_M*6000)/100"
  let "TIME_MS=($2-$TIME_M*6000-$TIME_S*100)*10"
  printf "%s\t%dm%d.%03dms\n" $1 $TIME_M $TIME_S $TIME_MS
}

if [ $(getconf CLK_TCK) != 100 ]; then
  exit 1;
fi

if [ $# != 1 ]; then
  exit 1;
fi

PROC_DIR="/proc/"$1

if [ ! -e $PROC_DIR ]; then
  exit 1
fi

USER_TIME=0
SYS_TIME=0
START_TIME=0

while [ -e $PROC_DIR ]; do
  read TMP_USER_TIME TMP_SYS_TIME TMP_START_TIME <<< $(cat $PROC_DIR/stat | awk '{print $14, $15,  $22;}')
  if [ -e $PROC_DIR ]; then
    USER_TIME=$TMP_USER_TIME
    SYS_TIME=$TMP_SYS_TIME
    START_TIME=$TMP_START_TIME
    sleep 0.1
  else
    break
  fi
done

read FINISH_TIME <<< $(cat '/proc/self/stat' | awk '{print $22;}')
let "REAL_TIME=($FINISH_TIME - $START_TIME)*10"
print_res_jeffies  'real' $REAL_TIME
print_res_ticks    'user' $USER_TIME
print_res_ticks    'sys' $SYS_TIME

And this is an example that compares my implementation of time and real time:

>time ./sys_intensive > /dev/null
Alarm clock

real    0m10.004s
user    0m9.883s
sys     0m0.034s

In another terminal window I run my_time.sh and give it PID:

>./my_time.sh `pidof sys_intensive`
real    0m10.010ms
user    0m9.780ms
sys     0m0.030ms
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top