>> main >> Tips & Articles >> Linux Tips & Tricks >> Getting CPU load percentage from software

 

Getting CPU load percentage from software

CPU load can be seen from the terminal using the top command, however sometimes you only want to see the load or on embedded system you may not have the top command or you may not have the ncurses library needed for the top command.
Sometimes you might also want to measure CPU load grammatically (from your application).
The CPU time statistics are stored in /proc/stat, so you can read this file and calculate the load.
The first line of /proc/stat contains CPU times for all CPU's in the system.
This line contains 4 numbers in units of jiffies (usually 1/100 of second).
The numbers are: user, nice, system, idle
  • user - Time spent in user space (for all processes)
  • nice - Time spent in niced tasks (tasks with positive nice value)
  • system - Time spent in Kernel space
  • idle - Time the processor was not busy with any running process.
Summing up all these times gives you the system uptime in jiffies, this is what the uptime command do.
If we divide the idle time in total time we get the percentage of time the processor was idle.
We can get the load percentage by subtracting this number from 100.
num_rows == 0) return; /* end of recursive call */ while($line = $result->fetch_assoc()) { $n = $line['num']; $query = "DELETE FROM comments WHERE num=$n"; $newresult = DoQuery($query, __LINE__); RecursiveDelete($n); } } function GetTimeStr($hour, $min, $sec, $month, $day, $year) { global $timeoffset; $time = mktime($hour, $min, $sec, $month, $day, $year, 0); $time += ($timeoffset * 3600); /* add time offset in seconds */ $str = date("d/m/Y H:i:s", $time); return $str; } /* | AddLinks: | Replace URL's in string with links */ function AddLinks($string) { $string = preg_replace("/(^|[^=\"\/])\b((\w+:\/\/|www\.)[^\s<]+)". "((\W+|\b)([\s<]|$))/i", "$1$2$4", $string); return preg_replace("/href=\"www/i", "href=\"http://www", $string); // $txt = preg_replace( "/(?\s]+)/i", "\\0", $txt ); // " } /* | Replace special codes in messages */ function SpecialCodes($str) { $str = stripslashes($str); $str = str_replace('<', '<', $str); $str = str_replace('|code|', '

', $str); $str = str_replace('|CODE|', '

', $str); $str = str_replace('|ECODE|', '

', $str); $str = str_replace('|ecode|', '

', $str); $str = str_replace('|b|', '', $str); $str = str_replace('|B|', '', $str); $str = str_replace('|eb|', '', $str); $str = str_replace('|EB|', '', $str); $str = str_replace('|u|', '', $str); $str = str_replace('|U|', '', $str); $str = str_replace('|eu|', '', $str); $str = str_replace('|EU|', '', $str); $str = str_replace("\n", "
", $str); $str = AddLinks($str); return $str; } /* | DisplayContents: | Show a string of section body, parsing dot commands | currently only one command is supported | .C that will start or stop code section */ function DisplayContents($contents) { $incode = 0; $contents = SpecialCodes($contents); $a = explode('
', $contents); foreach($a as $val) { if($val[0] == '.') { /* this is a command */ if($val[1] == 'C') { if(!$incode) { print "
\n"; $incode++; } else { print "
\n"; $incode--; } } } else { $val = AddLinks($val); print "$val
\n"; } } if($incode) { while($incode) { print "
\n"; $incode--; } } } /* | DispComments: | Recursive function to display comments for specific article */ function DispComments($id, $ancestor, $level, $delprivilege) { global $commentstbl, $abspath, $l10n; global $dir, $lang; global $base; // print "dir: $dir
\n"; $query = "SELECT * FROM $commentstbl WHERE article='$id' AND ancestor='$ancestor' ORDER BY time DESC"; // print "Query: $query
\n"; $result = DoQuery($query, __LINE__); if($result->num_rows == 0) return; /* end of recursive call */ if($level == 0) print "
 
\n"; /* spacer between two threades */ while($line = $result->fetch_assoc()) { $msgnum = $line['num']; $title = SpecialCodes($line['title']); if(empty($title)) $title = _("No title"); $msguser = $line['name']; $email = $line['email']; $website = $line['website']; $comment = $line['comment']; $time = $line['time']; $i = $level * 10; if($dir == 'rtl') $style = "margin-right:${i}px"; else $style = "margin-left:${i}px"; print "
\n"; print "$title \n"; if($website) print "$msguser "; else print "$msguser \n"; list($year, $month, $day, $hour, $min, $sec) = sscanf($time, "%d-%d-%d %d:%d:%d"); $timestr = GetTimeStr($hour, $min, $sec, $month, $day, $year); print "$timestr\n"; /* Put message contents as DIV that will be displayed when clicking on message */ print "
\n"; DisplayContents($comment); /* show link to add response */ $id1 = urlencode($id); $url = "${base}articlemsg.php?id=$id1&ancestor=$msgnum&lang=$lang"; $url = urlencode($url); print "
"; print _("Add comment"); print "      \n"; if($delprivilege == 1) { $l = _("Delete comment"); $url = "?action=delcomment&id=$id1&num=$msgnum"; print "$l\n"; print "  $email\n"; } print "
\n"; print "
\n"; print "
\n"; DispComments($id, $msgnum, $level+1, $delprivilege, $dir); } } if($action == 'delcomment') { $num = $_GET['num']; $query = "DELETE FROM $commentstbl WHERE num='$num'"; $result = DoQuery($query, __LINE__); RecursiveDelete($num); $url = urldecode($id); print "\n"; print "
\n"; print _("Comment deleted"); $l = _("Click here to return"); print "
$l
\n"; print "
\n"; return; } $commenturl1 = urlencode($commenturl); $url = "${base}articlemsg.php?id=$commenturl1&ancestor=0&lang=$lang"; $url = urlencode($url); print "
"; print _("Add comment"); print "\n"; // print "

delprivilege: $delprivilege

\n"; DispComments($commenturl, 0, 0, $delprivilege); ?>