]>
Commit | Line | Data |
---|---|---|
1 | # Erebus IRC bot - Author: Erebus Team | |
2 | # vim: fileencoding=utf-8 | |
3 | # resource-usage module | |
4 | # This file is released into the public domain; see http://unlicense.org/ | |
5 | ||
6 | # module info | |
7 | modinfo = { | |
8 | 'author': 'Erebus Team', | |
9 | 'license': 'public domain', | |
10 | 'compatible': [0], | |
11 | 'depends': [], | |
12 | 'softdeps': ['help'], | |
13 | } | |
14 | ||
15 | # preamble | |
16 | import modlib | |
17 | lib = modlib.modlib(__name__) | |
18 | modstart = lib.modstart | |
19 | modstop = lib.modstop | |
20 | ||
21 | # module code | |
22 | import resource, time | |
23 | ||
24 | @lib.hook(needchan=False, wantchan=True, glevel=lib.MANAGER) | |
25 | @lib.help(None, "show RAM usage") | |
26 | def ram(bot, user, chan, realtarget, *args): | |
27 | if chan is not None: replyto = chan | |
28 | else: replyto = user | |
29 | ||
30 | try: | |
31 | res = resource.getrusage(resource.RUSAGE_BOTH) | |
32 | except: | |
33 | res = resource.getrusage(resource.RUSAGE_SELF) | |
34 | ||
35 | bot.fastmsg(replyto, "Memory usage (MiB): %r" % (res.ru_maxrss/1024.0)) | |
36 | ||
37 | @lib.hook(needchan=False, wantchan=True, glevel=lib.MANAGER) | |
38 | @lib.help(None, "show resource usage") | |
39 | def resources(bot, user, chan, realtarget, *args): | |
40 | if chan is not None: replyto = chan | |
41 | else: replyto = user | |
42 | ||
43 | uptime = time.time() - bot.parent.starttime | |
44 | m, s = divmod(uptime, 60) | |
45 | h, m = divmod(m, 60) | |
46 | d, h = divmod(h, 24) | |
47 | try: | |
48 | res = resource.getrusage(resource.RUSAGE_BOTH) | |
49 | except: | |
50 | res = resource.getrusage(resource.RUSAGE_SELF) | |
51 | ||
52 | bot.slowmsg(replyto, "Resource usage:") | |
53 | for i, v in ( | |
54 | ('uptime (s)', "%d (%d days %02d:%02d:%02d)" % (uptime, d, h, m, s)), | |
55 | ('utime (s)', res.ru_utime), | |
56 | ('stime (s)', res.ru_stime), | |
57 | ('memory (MiB)', (res.ru_maxrss/1024.0)), | |
58 | ('I/O (blocks)', res.ru_inblock+res.ru_oublock), | |
59 | ('page faults', res.ru_majflt), | |
60 | ('signals', res.ru_nsignals), | |
61 | ('context switches (voluntary)', res.ru_nvcsw), | |
62 | ('context switches (involuntary)', res.ru_nivcsw), | |
63 | ): | |
64 | bot.slowmsg(replyto, "- %s: %s" % (i, v)) | |
65 | bot.slowmsg(replyto, "EOL.") |