aboutsummaryrefslogtreecommitdiffstats
path: root/xpybar/config/myio.py
blob: 24753482699366c8749aeba87f1e1cd1825080df (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
# -*- python -*-
from plugins.dentrystate import DentryState
from plugins.df          import Discs
from plugins.discstats   import DiscStats
from plugins.files       import Files
from plugins.inodestate  import InodeState
from plugins.locks       import Locks
from plugins.random      import Random

from common import *

class MyIO(Entry):
    def __init__(self, *args, fs_name_map = None, fs_ignore = None, fs_type_ignore = None, **kwargs):
        self.fs_name_map = [] if fs_name_map is None else fs_name_map
        self.fs_ignore = set([] if fs_ignore is None else fs_ignore)
        self.fs_type_ignore = set(MyIO.nodev_fs() if fs_type_ignore is None else fs_type_ignore)
        self.show_value = 0
        self.show_disc_value = 0
        self.show_overall = True
        self.show_disc = 0
        self.disc_map = None
        Entry.__init__(self, *args, **kwargs)
    
    @staticmethod
    def nodev_fs():
        ret = []
        with open('/proc/filesystems', 'rb') as file:
            data = file.read()
        data = data.decode('utf-8', 'strict')[:-1].replace('\t', ' ').split('\n')
        for line in data:
            cols = line.split(' ')
            if 'nodev' in cols[:-1]:
                ret.append(cols[-1])
        ret += ['fuse.gvfsd-fuse']
        return ret
    
    def action(self, col, button, x, y):
        if button == LEFT_BUTTON:
            overall = self.show_overall
            disc_map = self.disc_map
            if overall and disc_map is not None:
                x = 0
                for text, disc in disc_map:
                    if col < x:
                        break
                    w = Bar.coloured_length(text)
                    if col < x + w:
                        if disc is not None:
                            self.show_disc = disc
                        break
                    x += w + 1
            self.show_overall = not overall
        elif button == MIDDLE_BUTTON and not self.show_overall:
            self.show_disc_value = max(self.show_disc_value - 1, 0)
        elif button == RIGHT_BUTTON and not self.show_overall:
            self.show_disc_value = min(self.show_disc_value + 1, 4)
        elif button == SCROLL_UP:
            if self.show_overall:
                self.show_value = min(self.show_value + 1, 13)
            else:
                show_disc = self.show_disc
                if isinstance(show_disc, str):
                    try:
                        show_disc = [d.filesystem for d in self.get_discs()].index(show_disc)
                    except:
                        show_disc = -1
                self.show_disc = show_disc + 1
        elif button == SCROLL_DOWN:
            if self.show_overall:
                self.show_value = max(self.show_value - 1, 0)
            else:
                show_disc = self.show_disc
                if isinstance(show_disc, str):
                    try:
                        show_disc = [d.filesystem for d in self.get_discs()].index(show_disc)
                    except:
                        show_disc = -1
                self.show_disc = max(show_disc - 1, 0)
        else:
            return
        self.invalidate()
    
    def colour(self, percent):
        colour = '39'
        if percent < 25:  colour = '32'
        if percent > 50:  colour = '33'
        if percent > 90:  colour = '31'
        if percent > 95:  colour = '39;41'
        return colour
    
    def unit(self, value):
        units = ['', 'K', 'M', 'G', 'T', 'P', 'E']
        unit = 0
        while unit + 1 < len(units) and value >= 1024:
            unit += 1
            value /= 1024
        return (value, units[unit])
    
    def si(self, value, baseunit = 0):
        unit = baseunit + 3
        units = ['n', 'µ', 'm', '', 'k', 'M', 'G', 'T', 'P', 'E']
        while unit + 1 < len(units) and value >= 1000:
            unit += 1
            value /= 1000
        return (value, units[unit])
    
    def get_discs(self):
        discs = Discs()
        fs = [discs.filesystems[f] for f in sorted(discs.filesystems.keys()) if
              f not in self.fs_ignore and
              discs.filesystems[f].fstype not in self.fs_type_ignore and
              f[0] == '/']
        return fs
    
    def function_overall(self):
        df = []
        if self.show_value == 0:
            label = 'Df'
            self.disc_map = [(label + ':', None)]
            used = 0
            available = 0
            for disc in self.get_discs():
                percent = disc.used * 100 / (disc.used + disc.available)
                text = '\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent)
                df.append(text)
                self.disc_map.append((text, disc.filesystem))
                used += disc.used
                available += disc.available
            df.append(':')
            percent = used * 100 / (used + available)
            df.append('\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent))
            
        elif self.show_value == 1:
            label = 'Df'
            self.disc_map = [(label + ':', None)]
            used = 0
            available = 0
            for disc in self.get_discs():
                percent = disc.used * 100 / (disc.used + disc.available)
                text = '\033[%sm%.1f\033[0m%s' % (self.colour(percent), *self.unit(disc.available))
                df.append(text)
                self.disc_map.append((text, disc.filesystem))
                used += disc.used
                available += disc.available
            df.append(':')
            percent = used * 100 / (used + available)
            df.append('\033[%sm%.1f\033[0m%s' % (self.colour(percent), *self.unit(available)))
            
        elif self.show_value == 2:
            label = 'Df(inodes)'
            self.disc_map = [(label + ':', None)]
            used = 0
            available = 0
            for disc in self.get_discs():
                percent = disc.iused * 100 / (disc.iused + disc.ifree)
                text = '\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent)
                df.append(text)
                self.disc_map.append((text, disc.filesystem))
                used += disc.iused
                available += disc.ifree
            df.append(':')
            percent = used * 100 / (used + available)
            df.append('\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent))
            
        elif self.show_value == 3:
            label = 'Dent'
            dentry = DentryState()
            percent = (dentry.nr_dentry - dentry.nr_unused) * 100 / dentry.nr_dentry
            df.append('\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent))
            df.append('%is' % dentry.age_limit)
            df.append('%ipages' % dentry.want_pages)
            
        elif self.show_value == 4:
            label = 'Inode'
            inode = InodeState()
            percent = (inode.nr_inodes - inode.nr_free_inodes) * 100 / inode.nr_inodes
            df.append('\033[%sm%.0f\033[0m%%%s' % (self.colour(percent), percent,
                                                   '' if inode.preshrink == 0 else ' preshrink'))
        elif self.show_value == 5:
            label = 'File'
            files = Files()
            percent = (files.nr_files - files.nr_free_files) * 100 / files.file_max
            df.append('\033[%sm%.0f\033[0m%%' % (self.colour(percent), percent))
            df.append('%i/%i/%i' % (files.nr_files - files.nr_free_files, files.nr_files, files.file_max))
            
        elif self.show_value == 6:
            label = 'Lock'
            locks = Locks().locks
            ar, aw, mr, mw = 0, 0, 0, 0
            for lock in locks:
                if lock.mandatory:
                    if lock.shared:
                        mr += 1
                    else:
                        mw += 1
                else:
                    if lock.shared:
                        ar += 1
                    else:
                        aw += 1
            df.append('%iar' % ar)
            df.append('%iaw' % aw)
            df.append('%imr' % mr)
            df.append('%imw' % mw)
            df.append(':')
            df.append('%ia' % (ar + aw))
            df.append('%im' % (mr + mw))
            df.append('%ir' % (ar + mr))
            df.append('%iw' % (aw + mw))
            df.append(':')
            df.append('%i' % (ar + aw + mr + mw))
            
        elif self.show_value == 7:
            label = 'Rand'
            random = Random()
            df.append(str(random.poolsize))
            
        elif self.show_value in (8, 11):
            label = 'Disc(r %s)' % ('sum' if self.show_value == 8 else 'avg')
            stats = DiscStats().devices.values()
            r_complete, r_merge, r_sectors, r_time = 0, 0, 0, 0
            for stat in stats:
                r_complete += stat.r_complete
                r_merge    += stat.r_merge
                r_sectors  += stat.r_sectors
                r_time     += stat.r_time
            n = 1 if self.show_value == 8 else len(stats)
            r_complete /= n
            r_merge    /= n
            r_sectors  /= n
            r_time     /= n
            df.append('%.1f%scompleted' % self.si(r_complete))
            df.append('%.1f%smerge'     % self.si(r_merge))
            df.append('%.1f%ssectors'   % self.si(r_sectors))
            df.append('%.1f%ss'         % self.si(r_time, -1))
            
        elif self.show_value in (9, 12):
            label = 'Disc(w %s)' % ('sum' if self.show_value == 9 else 'avg')
            stats = DiscStats().devices.values()
            w_complete, w_merge, w_sectors, w_time = 0, 0, 0, 0
            for stat in stats:
                w_complete += stat.w_complete
                w_merge    += stat.w_merge
                w_sectors  += stat.w_sectors
                w_time     += stat.w_time
            n = 1 if self.show_value == 9 else len(stats)
            w_complete /= n
            w_merge    /= n
            w_sectors  /= n
            w_time     /= n
            df.append('%.1f%scomplete' % self.si(w_complete))
            df.append('%.1f%smerge'    % self.si(w_merge))
            df.append('%.1f%ssectors'  % self.si(w_sectors))
            df.append('%.1f%ss'        % self.si(w_time, -1))
            
        elif self.show_value in (10, 13):
            label = 'Disc(io %s)' % ('sum' if self.show_value == 10 else 'avg')
            stats = DiscStats().devices.values()
            io_current, io_time, io_weighted_time  = 0, 0, 0
            for stat in stats:
                io_current += stat.io_current
                io_time    += stat.io_time
                io_weighted_time += stat.io_weighted_time
            n = 1 if self.show_value == 10 else len(stats)
            io_current /= n
            io_time    /= n
            io_weighted_time /= n
            df.append('%.1f%s'            % self.si(io_current))
            df.append('%.1f%ss'           % self.si(io_time, -1))
            df.append('%.1f%ss(weighted)' % self.si(io_weighted_time, -1))
        
        return '%s: %s' % (label, ' '.join(df))
    
    def function_disc(self):
        discs = self.get_discs()
        disc = self.show_disc
        if isinstance(disc, str):
            for i, d in enumerate(discs):
                if d.filesystem == disc:
                    disc = i
                    break
            if isinstance(disc, str):
                disc = 0
        elif disc >= len(discs):
            disc = len(discs) - 1
        disc = discs[disc]
        name = disc.filesystem
        name = self.fs_name_map[name] if name in self.fs_name_map else name.split('/')[-1]
        df = []
        
        if self.show_disc_value > 1:
            try:
                devices = DiscStats().devices
                device = None
                fs = os.path.realpath(disc.filesystem)
                stat = None
                for dev in devices.keys():
                    if '/dev/' + dev == fs:
                        stat = devices[dev]
                        break
                if stat is None:
                    self.show_disc_value = 0
            except:
                self.show_disc_value = 0
        
        if self.show_disc_value == 0:
            percent = disc.used * 100 / (disc.used + disc.available)
            text = '\033[%sm%.0f\033[0m%%(%.1f%sB/%.f%sB)' % (self.colour(percent), percent,
                                                              *self.unit(disc.available),
                                                              *self.unit(disc.used + disc.available))
            df.append(text)
            percent = disc.iused * 100 / (disc.iused + disc.ifree)
            text = '\033[%sm%.0f\033[0m%%(%.1f%s/%.1f%s)inodes' % (self.colour(percent), percent,
                                                                   *self.si(disc.iused), *self.si(disc.inodes))
            df.append(text)
            
        elif self.show_disc_value == 1:
            df.append(disc.fstype)
            df.append(disc.mountpoint)
            
        elif self.show_disc_value == 2:
            name += '(r)'
            df.append('%.1f%scompleted' % self.si(stat.r_complete))
            df.append('%.1f%smerge'     % self.si(stat.r_merge))
            df.append('%.1f%ssectors'   % self.si(stat.r_sectors))
            df.append('%.1f%ss'         % self.si(stat.r_time, -1))
            
        elif self.show_disc_value == 3:
            name += '(w)'
            df.append('%.1f%scomplete' % self.si(stat.w_complete))
            df.append('%.1f%smerge'    % self.si(stat.w_merge))
            df.append('%.1f%ssectors'  % self.si(stat.w_sectors))
            df.append('%.1f%ss'        % self.si(stat.w_time, -1))
            
        elif self.show_disc_value == 4:
            name += '(io)'
            df.append('%.1f%s'            % self.si(stat.io_current))
            df.append('%.1f%ss'           % self.si(stat.io_time, -1))
            df.append('%.1f%ss(weighted)' % self.si(stat.io_weighted_time, -1))
        
        return '%s: %s' % (name, ' '.join(df))
    
    def function(self):
        try:
            self.disc_map = None
            if self.show_overall:
                return self.function_overall()
            else:
                return self.function_disc()
        except Exception as e:
            return str(e)