JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / agent / perf / sar.py
1 ##############################################################################
2 # Copyright (c) 2015 Huawei Technologies Co.,Ltd and others.
3 #
4 # All rights reserved. This program and the accompanying materials
5 # are made available under the terms of the Apache License, Version 2.0
6 # which accompanies this distribution, and is available at
7 # http://www.apache.org/licenses/LICENSE-2.0
8 ##############################################################################
9
10 import subprocess
11 import logging
12 import time
13 import os
14 from signal import SIGINT
15
16 from vstf.common.utils import check_output, my_popen, kill_by_name
17 from vstf.agent.env.basic import collect
18
19 LOG = logging.getLogger(__name__)
20
21
22 class Sar(object):
23     def __init__(self):
24         self.sar_cmd_str = "sar -u %(interval)s"
25         self.child_process = {}
26
27     def start(self, interval=2):
28         cmd = self.sar_cmd_str % {'interval': interval}
29         child = my_popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE)
30         time.sleep(1)
31         if child.poll() is not None:
32             print child.poll()
33             raise Exception("start vnstat error, vnstat is not running")
34         self.child_process[child.pid] = child
35         return child.pid
36
37     def stop(self, pid):
38         assert pid in self.child_process
39         os.kill(pid, SIGINT)
40         process = self.child_process.pop(pid)
41         out = process.stdout.read()
42         process.wait()
43         data = {'raw_data': out, 'tool': 'sar', 'type': 'cpu'}
44         cpu_info = collect.Collect().collect_host_info()[1]
45         cpu_num = cpu_info['CPU INFO']['CPU(s)']
46         cpu_mhz = cpu_info['CPU INFO']['CPU MHz']
47         data.update({'cpu_num': float(cpu_num), 'cpu_mhz': float(cpu_mhz)})
48         return data
49
50     def process(self, raw):
51         lines = raw.splitlines()
52         # print lines
53         head = lines[2].split()[3:]
54         average = lines[-1].split()[2:]
55         data = {}
56         for h, d in zip(head, average):
57             data[h.strip('%')] = float(d)
58         cpu_num = check_output('cat /proc/cpuinfo  | grep processor | wc -l', shell=True).strip()
59         data.update({'cpu_num': int(cpu_num)})
60         return data
61
62     def clean(self):
63         for _, process in self.child_process.items():
64             process.kill()
65             process.wait()
66         self.child_process = {}
67         return True
68
69     def force_clean(self):
70         LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
71         kill_by_name("sar")
72         self.child_process = {}
73         return True
74
75 if __name__ == '__main__':
76     logging.basicConfig(level=logging.DEBUG)
77     q = Sar()
78     pid = q.start()
79     time.sleep(10)
80     raw = q.stop(pid)
81     print raw
82     print q.process(raw['raw_data'])