~christopher-hunt08/maus/maus_integrated_kalman

« back to all changes in this revision

Viewing changes to tests/integration/test_load/process_monitor.py

  • Committer: Durga Rajaram
  • Date: 2014-07-16 15:13:05 UTC
  • mfrom: (659.1.92 cand)
  • Revision ID: durga@fnal.gov-20140716151305-q27rv1y9p03v9lks
Tags: MAUS-v0.9, MAUS-v0.9.0
MAUS-v0.9.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
31
31
 
32
32
    @returns dict of {"pid":pid, "memory":mem_usage} or None on failure
33
33
    """
34
 
    columns = "%cpu,%mem,etime"
 
34
    columns = "%cpu,%mem,etime,sz,rsz,vsz"
35
35
    ps_out = os.popen('ps -p %d -o %s' % (pid, columns)).readlines()
36
36
    if len(ps_out) == 2:
37
37
        out_list = ps_out[1].rstrip().split()
58
58
    xboa.Common.make_root_legend(canvas, graph_dict.values())
59
59
    return canvas, graph_dict
60
60
 
 
61
def monitor_step(delta_t, mem_list, graph_dict):
 
62
    """
 
63
    Make a single step in the monitoring; append output to graph
 
64
    """
 
65
    mem_list_step = []
 
66
    for i, item in enumerate(mem_list[-1]):
 
67
        pid = int(item["pid"])
 
68
        mem = print_mem_usage(pid)
 
69
        mem_p = float(mem["%mem"])
 
70
        index = graph_dict[pid].GetN()
 
71
        if mem and (mem_p > 1e-9 or index == 1):
 
72
            mem_list_step.append(mem)
 
73
            graph_dict[pid].Expand(index+1)
 
74
            graph_dict[pid].SetPoint(index,
 
75
                                    delta_t.seconds,
 
76
                                    mem_p)
 
77
        graph_dict[pid].SetLineStyle(i+1)
 
78
        graph_dict[pid].Draw()
 
79
    return mem_list_step
 
80
 
61
81
def main(pid_list, title, wait_time, max_time):
62
82
    """
63
83
    Iterate over list of pids; while pids exist, print cpu usage etc
69
89
    start = datetime.datetime.now()
70
90
    mem_list = [[{"pid":pid} for pid in pid_list]]
71
91
    canvas, graph_dict = make_graphs(pid_list, wait_time, max_time)
72
 
    index = 0
73
92
    delta = datetime.timedelta.max
74
93
    if max_time:
75
94
        delta = datetime.timedelta(0, max_time)
76
 
    while len(mem_list[-1]) > 0 and \
77
 
          (max_time and datetime.datetime.now()-start < delta):
78
 
        index += 1
 
95
    delta_t = datetime.datetime.now()-start
 
96
    while len(mem_list[-1]) > 0 and delta_t < delta:
79
97
        time.sleep(wait_time)
80
 
        mem_list_step = []
81
 
        for i, item in enumerate(mem_list[-1]):
82
 
            pid = int(item["pid"])
83
 
            mem = print_mem_usage(pid)
84
 
            if mem:
85
 
                mem_list_step.append(mem)
86
 
                graph_dict[pid].Expand(index)
87
 
                graph_dict[pid].SetPoint(index,
88
 
                    (datetime.datetime.now()-start).seconds, float(mem["%mem"]))
89
 
            graph_dict[pid].SetLineStyle(i+1)
90
 
            graph_dict[pid].Draw()
91
 
            print "draw pid", pid
 
98
        mem_list_step = monitor_step(delta_t, mem_list, graph_dict)
92
99
        mem_list.append(mem_list_step)
93
100
        print mem_list[-1]
94
101
        canvas.Update()
95
102
        canvas.Print(str(title)+"_resource_usage.root")
96
103
        canvas.Print(str(title)+"_resource_usage.png")
97
104
        canvas.Print(str(title)+"_resource_usage.eps")
 
105
        delta_t = datetime.datetime.now()-start
98
106
    return mem_list
99
107
 
100
108
if __name__ == "__main__":