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