| 1 | -- flaw, a Lua OO management framework for Awesome WM widgets. |
| 2 | -- Copyright (C) 2009,2010,2011 David Soulayrol <david.soulayrol AT gmail DOT net> |
| 3 | |
| 4 | -- This program is free software: you can redistribute it and/or modify |
| 5 | -- it under the terms of the GNU General Public License as published by |
| 6 | -- the Free Software Foundation, either version 3 of the License, or |
| 7 | -- (at your option) any later version. |
| 8 | |
| 9 | -- This program is distributed in the hope that it will be useful, |
| 10 | -- but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | -- GNU General Public License for more details. |
| 13 | |
| 14 | -- You should have received a copy of the GNU General Public License |
| 15 | -- along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 16 | |
| 17 | |
| 18 | -- Grab environment. |
| 19 | local io = io |
| 20 | local string = string |
| 21 | |
| 22 | local beautiful = require('beautiful') |
| 23 | local naughty = require('naughty') |
| 24 | |
| 25 | local flaw = { |
| 26 | helper = require('flaw.helper'), |
| 27 | gadget = require('flaw.gadget'), |
| 28 | provider = require('flaw.provider') |
| 29 | } |
| 30 | |
| 31 | |
| 32 | --- Cpu information. |
| 33 | -- |
| 34 | -- <p>This module contains a provider for cpu information and three |
| 35 | -- gadgets: a text gadget, an icon gadget and a graph gadget.</p> |
| 36 | -- |
| 37 | -- <h2>Gadgets</h2> |
| 38 | -- |
| 39 | -- <p>The ID of the gadgets designates the CPU slot to be monitored, |
| 40 | -- in the <code>/proc/stat</code> file, as indicated in the provider's |
| 41 | -- documentation below. CPU gadgets provide no custom parameters. See |
| 42 | -- the <a href="flaw.gadget.html">gadget</a> module documentation to |
| 43 | -- learn about standard gadgets parameters.</p> |
| 44 | -- |
| 45 | -- <h3>Icon Gadget</h3> |
| 46 | -- |
| 47 | -- <p>The CPU icon gadget can be instantiated by indexing the gadget |
| 48 | -- module with <code>icon.cpu</code>. Here is an exemple which assumes |
| 49 | -- the CPU icon path is stored in a <b>beautiful</b> property.</p> |
| 50 | -- |
| 51 | -- <div class='example'> |
| 52 | -- g = flaw.gadget.icon.cpu(<br/> |
| 53 | -- 'cpu', {}, { image = image(beautiful.cpu_icon) })<br/> |
| 54 | -- </div> |
| 55 | -- |
| 56 | -- <h3>Text Gadget</h3> |
| 57 | -- |
| 58 | -- <p>The CPU text gadget can be instantiated by indexing the gadget |
| 59 | -- module with <code>text.cpu</code>. By default, the gadget pattern |
| 60 | -- is <code>$load_user/$load_sum</code>. See the provider's |
| 61 | -- documentation below to learn about the available variables.</p> |
| 62 | -- |
| 63 | -- <div class='example'> |
| 64 | -- g = flaw.gadget.text.cpu('cpu') |
| 65 | -- </div> |
| 66 | -- |
| 67 | -- <h3>Graph Gadget</h3> |
| 68 | -- |
| 69 | -- <p>The CPU graph gadget can be instantiated by indexing the gadget |
| 70 | -- module with <code>graph.cpu</code>. By default, the chart displayed |
| 71 | -- by the gadget shows the evolution of <code>load_sum</code>. See the |
| 72 | -- provider's documentation below to learn about the available |
| 73 | -- variables.</p> |
| 74 | -- |
| 75 | -- <div class='example'> |
| 76 | -- g = flaw.gadget.graph.cpu(<br/> |
| 77 | -- 'cpu', {}, { width = 60, height = 18 }) |
| 78 | -- </div> |
| 79 | -- |
| 80 | -- <h2>Provider</h2> |
| 81 | -- |
| 82 | -- <p>The cpu provider loads its information from |
| 83 | -- <code>/proc/stat/</code> and considers the line beginning with |
| 84 | -- <code>cpu</code>. On multi-core machines, multiple |
| 85 | -- <code>cpuNN</code> lines follow the one which begins with only |
| 86 | -- <code>cpu</code>. The complete file format is explained at <a |
| 87 | -- href="http://www.linuxhowtos.org/System/procstat.htm">http://www.linuxhowtos.org/System/procstat.htm</a></p> |
| 88 | -- |
| 89 | -- <p>The cpu provider computed data is composed of the following |
| 90 | -- fields.</p> |
| 91 | -- |
| 92 | -- <ul> |
| 93 | -- |
| 94 | -- <li><code>load_user</code> |
| 95 | -- |
| 96 | -- <p>The percentage of time the CPU has spent performing normal |
| 97 | -- processing in user mode.</p></li> |
| 98 | -- |
| 99 | -- <li><code>load_nice</code> |
| 100 | -- |
| 101 | -- <p>The percentage of time the CPU has spent performing niced |
| 102 | -- processing in user mode.</p></li> |
| 103 | -- |
| 104 | -- <li><code>load_sum</code> |
| 105 | -- |
| 106 | -- <p>The percentage of time the CPU has spent performing any task in |
| 107 | -- any mode.</p></li> |
| 108 | -- |
| 109 | -- </ul> |
| 110 | -- |
| 111 | -- The provider data also stores the raw values read from the lines |
| 112 | -- parsed in <code>/proc/stat</code>. This information can be found in |
| 113 | -- <code>raw_sum</code>, <code>raw_user</code>, <code>raw_nice</code>, |
| 114 | -- <code>raw_idle</code>, and is used to compute percentage values. |
| 115 | -- |
| 116 | -- |
| 117 | -- @author David Soulayrol <david.soulayrol AT gmail DOT com> |
| 118 | -- @copyright 2009,2010,2011 David Soulayrol |
| 119 | module('flaw.cpu') |
| 120 | |
| 121 | --- The cpu provider prototype. |
| 122 | -- |
| 123 | -- <p>The CPU provider type is set to |
| 124 | -- <code>cpu._NAME</code>.</p> |
| 125 | -- |
| 126 | -- @class table |
| 127 | -- @name CPUProvider |
| 128 | CPUProvider = flaw.provider.CyclicProvider:new{ type = _NAME, data = {} } |
| 129 | |
| 130 | --- Callback for provider refresh. |
| 131 | function CPUProvider:do_refresh() |
| 132 | local file = io.open('/proc/stat') |
| 133 | local line = '' |
| 134 | |
| 135 | local id, user, nice, system, idle, diff |
| 136 | |
| 137 | while line ~= nil do |
| 138 | line = file:read() |
| 139 | |
| 140 | if line ~=nil then |
| 141 | id, user, nice, system, idle = string.match( |
| 142 | line, '(%w+)%s+(%d+)%s+(%d+)%s+(%d+)%s+(%d+)') |
| 143 | |
| 144 | if id ~=nil and string.find(id, 'cpu') ~= nil then |
| 145 | |
| 146 | if self.data[id] == nil then |
| 147 | self.data[id] = { |
| 148 | raw_user = 0, raw_nice = 0, raw_idle = 0, raw_sum = 0, |
| 149 | load_user = 0, load_nice = 0, load_sum = 0 } |
| 150 | end |
| 151 | |
| 152 | local cpu_sum = user + nice + system + idle |
| 153 | diff = cpu_sum - self.data[id].raw_sum |
| 154 | |
| 155 | -- The diff should always be positive. If it is not the case, |
| 156 | -- the load is too heavy for the system to refresh data. In |
| 157 | -- this case, keep a 100 USER_HZ default value. |
| 158 | self.data[id].load_user = 100 |
| 159 | self.data[id].load_nice = 100 |
| 160 | self.data[id].load_sum = 100 |
| 161 | if diff > 0 then |
| 162 | self.data[id].load_user = |
| 163 | 100 * (user - self.data[id].raw_user) / diff |
| 164 | self.data[id].load_nice = |
| 165 | 100 * (nice - self.data[id].raw_nice) / diff |
| 166 | self.data[id].load_sum = |
| 167 | 100 - 100 * (idle - self.data[id].raw_idle) / diff |
| 168 | end |
| 169 | |
| 170 | self.data[id].raw_sum = cpu_sum |
| 171 | self.data[id].raw_user = user |
| 172 | self.data[id].raw_nice = nice |
| 173 | self.data[id].raw_idle = idle |
| 174 | end |
| 175 | end |
| 176 | end |
| 177 | io.close(file); |
| 178 | end |
| 179 | |
| 180 | --- A factory for CPU provider. |
| 181 | -- |
| 182 | -- <p>Only one provider is built to store all the CPU information. Its |
| 183 | -- data is a map indexed by CPU slot.</p> |
| 184 | -- |
| 185 | -- @return a brand new CPU provider, or the existing one if found in |
| 186 | -- the providers cache. |
| 187 | function provider_factory() |
| 188 | local p = flaw.provider.get(_NAME, '') |
| 189 | -- Create the provider if necessary. |
| 190 | if p == nil then |
| 191 | p = CPUProvider:new{ id = '' } |
| 192 | flaw.provider.add(p) |
| 193 | end |
| 194 | return p |
| 195 | end |
| 196 | |
| 197 | -- A Text gadget for cpu status display. |
| 198 | flaw.gadget.register.text(_M, { delay = 1, pattern = '$load_user/$load_sum' }) |
| 199 | |
| 200 | -- A graph gadget for cpu load display. |
| 201 | flaw.gadget.register.graph(_M, { delay = 1, values = { 'load_sum' } }) |
| 202 | |
| 203 | -- An icon gadget for cpu status display. |
| 204 | flaw.gadget.register.icon(_M, { delay = 1 }) |