When you will use it for monitoring, of course last values you should put outside. effect is, you will get averrage load on specfied time period. Of course 1 second for sleeping is not mandatory. Of course you should use scanf or other C functions for converting any perl regexpes I've used to C source.
![utime unix command line utime unix command line](https://thumbs.dreamstime.com/z/computer-command-line-interface-cli-unix-bash-shell-web-security-computer-command-line-interface-unix-bash-shell-web-security-139056058.jpg)
I hope it will help you in any monitoring. Printf "Load is: %5.1f\%\n",($currApp-$lastApp)/($currSys-$lastSys)*$cores*100 if defined $currApp and defined $lastApp and defined $currSys and defined $lastSys ($currApp,$currSys,$cores)=GetCurrentLoads($pid)
![utime unix command line utime unix command line](https://linuxhint.com/wp-content/uploads/2016/05/sublime-text-3-editor.jpg)
My ($currApp,$currSys,$lastApp,$lastSys,$cores) #returns current process time counters or single undef if unavailable My $pid=1234 #insert here monitored process PID Then let me show my example: #!/usr/bin/perl Specially look how cpu cores are counted :D Then when some process is active working with 2 threads, cpu usage can be more than 100%. This put percents as the same way as top, scalling it to one CPU. When you want monitor specified process, usually it is done by scripting. $(CC) $(CFLAGS) systeminfo.c -o systeminfo $(CLIBS) Glibtop_get_proclist(&proclist,which,arg) ĬLIBS=-lgtop-2.0 -lgtop_sysdeps-2.0 -lgtop_common-2.0 This program is looking for CPU,Memory,Procs also u can look glibtop header there was a lot of usefull function have fun. Sys_util = 100 * (stime_after - stime_before) / (time_total_after - time_total_before) You can now calculate the CPU usage of the process over the sampling time, with: user_util = 100 * (utime_after - utime_before) / (time_total_after - time_total_before)
![utime unix command line utime unix command line](https://infosecaddicts.com/wp-content/uploads/2017/09/putty-800x384.png)
Then sleep for a second or so, and read them all again. Read both utime and stime for the process you're interested in, and read time_total from /proc/stat. You need to take the sum of the values on this line to get a time_total measure. This tells you the cumulative CPU time that's been used in various categories, in units of jiffies. You're probably after utime and/or stime. Uninterruptible wait, Z is zombie, T is traced or stopped)Ĭmin_flt number of minor faults with child'sĬmaj_flt number of major faults with child's State state (R is running, S is sleeping, D is sleeping in an These are the first few fields (from Documentation/filesystems/proc.txt in your kernel source): Table 1-3: Contents of the stat files (as of 2.6.22-rc3) You need to parse out the data from /proc//stat.