Merge "Change PTL informatin in INFO"
[bottlenecks.git] / testsuites / vstf / vstf_scripts / 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
24     def __init__(self):
25         self.sar_cmd_str = "sar -u %(interval)s"
26         self.child_process = {}
27
28     def start(self, interval=2):
29         cmd = self.sar_cmd_str % {'interval': interval}
30         child = my_popen(
31             cmd.split(),
32             stdout=subprocess.PIPE,
33             stderr=subprocess.PIPE)
34         time.sleep(1)
35         if child.poll() is not None:
36             print child.poll()
37             raise Exception("start vnstat error, vnstat is not running")
38         self.child_process[child.pid] = child
39         return child.pid
40
41     def stop(self, pid):
42         assert pid in self.child_process
43         os.kill(pid, SIGINT)
44         process = self.child_process.pop(pid)
45         out = process.stdout.read()
46         process.wait()
47         data = {'raw_data': out, 'tool': 'sar', 'type': 'cpu'}
48         cpu_info = collect.Collect().collect_host_info()[1]
49         cpu_num = cpu_info['CPU INFO']['CPU(s)']
50         cpu_mhz = cpu_info['CPU INFO']['CPU MHz']
51         data.update({'cpu_num': float(cpu_num), 'cpu_mhz': float(cpu_mhz)})
52         return data
53
54     def process(self, raw):
55         lines = raw.splitlines()
56         # print lines
57         head = lines[2].split()[3:]
58         average = lines[-1].split()[2:]
59         data = {}
60         for h, d in zip(head, average):
61             data[h.strip('%')] = float(d)
62         cpu_num = check_output(
63             'cat /proc/cpuinfo  | grep processor | wc -l',
64             shell=True).strip()
65         data.update({'cpu_num': int(cpu_num)})
66         return data
67
68     def clean(self):
69         for _, process in self.child_process.items():
70             process.kill()
71             process.wait()
72         self.child_process = {}
73         return True
74
75     def force_clean(self):
76         LOG.info("%s %s start", self.__class__, self.force_clean.__name__)
77         kill_by_name("sar")
78         self.child_process = {}
79         return True
80
81 if __name__ == '__main__':
82     logging.basicConfig(level=logging.DEBUG)
83     q = Sar()
84     pid = q.start()
85     time.sleep(10)
86     raw = q.stop(pid)
87     print raw
88     print q.process(raw['raw_data'])