| 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 | --- Memory information. |
| 33 | -- |
| 34 | -- <p>This module contains a provider for memory status and two |
| 35 | -- gadgets: a text gadget and a graph gadget.</p> |
| 36 | -- |
| 37 | -- <h2>Gadgets</h2> |
| 38 | -- |
| 39 | -- <p>The ID parameter has no meaning for the memory gadgets. Memory |
| 40 | -- gadgets provide no custom parameters. See the <a |
| 41 | -- href="flaw.gadget.html">gadget</a> module documentation to learn |
| 42 | -- about standard gadgets parameters.</p> |
| 43 | -- |
| 44 | -- <h3>Text Gadget</h3> |
| 45 | -- |
| 46 | -- <p>The memory text gadget can be instantiated by indexing the |
| 47 | -- gadget module with <code>text.memory</code>. By default, the gadget |
| 48 | -- pattern is <code>$ratio%</code>. See the provider's documentation |
| 49 | -- below to learn about the available variables.</p> |
| 50 | -- |
| 51 | -- <div class='example'> |
| 52 | -- g = flaw.gadget.text.memory('') |
| 53 | -- </div> |
| 54 | -- |
| 55 | -- <h3>Graph Gadget</h3> |
| 56 | -- |
| 57 | -- <p>The memory graph gadget can be instantiated by indexing the |
| 58 | -- gadget module with <code>graph.memory</code>. By default, the chart |
| 59 | -- displayed by the gadget shows the evolution of |
| 60 | -- <code>ratio</code>. See the provider's documentation below to learn |
| 61 | -- about the available variables.</p> |
| 62 | -- |
| 63 | -- <div class='example'> |
| 64 | -- g = flaw.gadget.graph.memory(<br/> |
| 65 | -- '', {}, { width = 60, height = 18 }) |
| 66 | -- </div> |
| 67 | -- |
| 68 | -- <h2>Provider</h2> |
| 69 | -- |
| 70 | -- <p>The memory provider loads its information from |
| 71 | -- <code>/proc/meminfo/</code>. This file format is explained, among |
| 72 | -- other places, at <a |
| 73 | -- href="http://www.redhat.com/advice/tips/meminfo.html">http://www.redhat.com/advice/tips/meminfo.html</a></p> |
| 74 | -- |
| 75 | -- <p>The memory provider computed data is composed of the following |
| 76 | -- field.</p> |
| 77 | -- |
| 78 | -- <ul> |
| 79 | -- |
| 80 | -- <li><code>ratio</code> |
| 81 | -- |
| 82 | -- <p>The percentage of memory currently used.</p></li> |
| 83 | -- |
| 84 | -- </ul> |
| 85 | -- |
| 86 | -- <p>The provider data also stores the raw values read from the |
| 87 | -- <code>/proc/meminfo</code> file. This information can be found in |
| 88 | -- <code>proc</code> and is composed of the following fields.</p> |
| 89 | -- |
| 90 | -- <ul> |
| 91 | -- |
| 92 | -- <li><code>proc.meminfo_memtotal</code> |
| 93 | -- |
| 94 | -- <p>The memory available on the system.</p></li> |
| 95 | -- |
| 96 | -- <li><code>proc.meminfo_cached</code> |
| 97 | -- |
| 98 | -- <p>The memory affected to cached data.</p></li> |
| 99 | -- |
| 100 | -- <li><code>proc.meminfo_buffers</code> |
| 101 | -- |
| 102 | -- <p>The memory affected to application buffers data.</p></li> |
| 103 | -- |
| 104 | -- <li><code>proc.meminfo_memfree</code> |
| 105 | -- |
| 106 | -- <p>The free memory.</p></li> |
| 107 | -- |
| 108 | -- </ul> |
| 109 | -- |
| 110 | -- |
| 111 | -- @author David Soulayrol <david.soulayrol AT gmail DOT com> |
| 112 | -- @copyright 2009,2010,2011 David Soulayrol |
| 113 | module('flaw.memory') |
| 114 | |
| 115 | |
| 116 | --- The memory provider prototype. |
| 117 | -- |
| 118 | -- <p>The memory provider type is set to |
| 119 | -- <code>memory._NAME</code>.</p> |
| 120 | -- |
| 121 | -- @class table |
| 122 | -- @name MemoryProvider |
| 123 | MemoryProvider = flaw.provider.CyclicProvider:new{ type = _NAME, data = {} } |
| 124 | |
| 125 | --- Callback for provider refresh. |
| 126 | function MemoryProvider:do_refresh() |
| 127 | local p = self.data.proc |
| 128 | local r = flaw.helper.file.load_state_file('/proc', 'meminfo', p) |
| 129 | |
| 130 | self.data.ratio = 0 |
| 131 | if r > 0 then |
| 132 | -- Adapt values. |
| 133 | local total = p.meminfo_memtotal:match('(%d+).*') or 0 |
| 134 | local free = p.meminfo_memfree:match('(%d+).*') or 0 |
| 135 | local buffers = p.meminfo_buffers:match('(%d+).*') or 0 |
| 136 | local cached = p.meminfo_cached:match('(%d+).*') or 0 |
| 137 | |
| 138 | if total ~= 0 then |
| 139 | self.data.ratio = flaw.helper.round( |
| 140 | 100 * (total - free - buffers - cached) / total, 2) |
| 141 | end |
| 142 | end |
| 143 | end |
| 144 | |
| 145 | --- A factory for memory providers. |
| 146 | -- |
| 147 | -- <p>Only one provider is built.</p> |
| 148 | -- |
| 149 | -- @return a brand new memory provider, or the existing one if found |
| 150 | -- in the providers cache. |
| 151 | function provider_factory() |
| 152 | local p = flaw.provider.get(_NAME, '') |
| 153 | -- Create the provider if necessary. |
| 154 | if p == nil then |
| 155 | p = MemoryProvider:new{ |
| 156 | id = '', data = { ratio = 0, proc = {} } } |
| 157 | flaw.provider.add(p) |
| 158 | end |
| 159 | return p |
| 160 | end |
| 161 | |
| 162 | |
| 163 | -- A Text gadget for memory status display. |
| 164 | flaw.gadget.register.text(_M, { pattern = '$ratio%' }) |
| 165 | |
| 166 | -- A graph gadget for memory status history. |
| 167 | flaw.gadget.register.graph(_M, { delay = 1, values = { 'ratio' } }) |