Updated 2018-08-24 15:03:51 by pooryorick

Created by CecilWesterhof.

I want to display my CPU usage. For this I need information (the first line) from /proc/stat. Here a proc that fetches this line, puts it in a easy to use dict, which also has a total added and returns it.
proc getStatCPU {} {
    set infoLst [lrange [getLineAsList /proc/stat] 1 7]
    lassign  $infoLst user nice system idle iowait irq softirq
    dict set statCPU user    $user
    dict set statCPU nice    $nice
    dict set statCPU system  $system
    dict set statCPU idle    $idle
    dict set statCPU iowait  $iowait
    dict set statCPU irq     $irq
    dict set statCPU softirq $softirq
    dict set statCPU total   [::tcl::mathop::+ {*}$infoLst]
    return $statCPU
}

For getLineAsList see Get a Certain Line From a File.

I will also post the script.

As always: comments, tips and questions are appreciated.

APN A comment about style - the braces around variable names are not required (and generally frowned upon by long-time Tclers) unless the name has "special characters" such as delimiters. Use $infoLst, $nice etc. instead.

CecilWesterhof When it is frowned upon I maybe should change it. Some bash people also frowned on it, but in bash I always use it, because it is to easy to get into a situation where it is important.

KPV Check out CPU Monitor which visually displays the CPU usage for every core.


Category CPU usage[Category System Maintenance]Category Linux