summaryrefslogtreecommitdiffstats
path: root/scripts/instperf
blob: f0f53cc9e842830a88c4d9d50c217e42461da088 (plain)
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
#!/usr/bin/python

import logging
from string import split
from subprocess import Popen, PIPE
import time

# grab the top five memory consuming processes, returning them in a string of
# of the format:
#
# name1:kB1,name2:kB2,...,name5:kB5
def topProcesses():
    output = Popen(["ps", "-eo", "comm,rss", "--sort", "-rss", "--no-headers"], stdout=PIPE).communicate()[0]
    top5 = output.split("\n")[:5]
    return ",".join(map(lambda (a,b): a+":"+b, map(split, top5)))

def logit():
    buffers = 0
    cached = 0
    memTotal = 0
    memFree = 0
    swapTotal = 0
    swapFree = 0

    global mem_logger

    with open("/proc/meminfo") as f:
        for line in f.readlines():
            if line.startswith("Buffers:"):
                buffers = line.split()[1]
            elif line.startswith("Cached:"):
                cached = line.split()[1]
            elif line.startswith("MemTotal:"):
                memTotal = line.split()[1]
            elif line.startswith("MemFree:"):
                memFree = line.split()[1]
            elif line.startswith("SwapTotal:"):
                swapTotal = line.split()[1]
            elif line.startswith("SwapFree:"):
                swapFree = line.split()[1]

    d = {"memUsed": int(memTotal)-int(memFree)-int(cached)-int(buffers),
         "swapUsed": int(swapTotal)-int(swapFree),
         "procs": topProcesses()}

    mem_logger.debug("%(memUsed)d %(swapUsed)d %(procs)s" % d)

mem_logger = logging.getLogger("memLog")
mem_logger.setLevel(logging.DEBUG)

handler = logging.FileHandler("/tmp/memory.dat")
handler.setLevel(logging.DEBUG)
handler.setFormatter(logging.Formatter("%(asctime)s %(message)s", "%H:%M:%S"))
mem_logger.addHandler(handler)

while True:
    logit()
    time.sleep(1)