941769a3c681cdbc6458d46dd172caaa1d2add01
[bottlenecks.git] / vstf / vstf / agent / equalizer / optimize.py
1 #!/usr/bin/python
2 import commands
3 import re
4
5
6 # import pdb
7 # pdb.set_trace()
8
9 class Optimize(object):
10     def __init__(self):
11         pass
12
13     def bind_cpu(self, cpu_range, thread):
14         flag, num = commands.getstatusoutput('taskset -pc %s %s' % (cpu_range, thread))
15         return flag
16
17     def catch_thread_info(self):
18         thread_info = {'fwd_vhost': None, 'src_recv_irq': None, 'dst_send_irq': None}
19         # top -H get the usage info
20         flag, threads_usages = commands.getstatusoutput('top -bH -n1 -c -w 2000')
21         line_array = threads_usages.split('\n')
22         # get highest vhost line
23         for line in line_array:
24             if re.search('vhost-', line) and self._check_thread_usage(line):
25                 thread_info['fwd_vhost'] = line.split()[0]
26                 break
27         # get highest irq thread as src_recv_irq thread
28         for line in line_array:
29             if re.search('irq/', line) and self._check_thread_usage(line):
30                 thread_info['src_recv_irq'] = line.split()[0]
31                 line_array.remove(line)
32                 break
33         # get the second highest irq thread as dst_send_irq
34         for line in line_array:
35             if re.search('irq/', line) and self._check_thread_usage(line):
36                 thread_info['dst_send_irq'] = line.split()[0]
37                 break
38         # check the data valid
39
40         for key in thread_info.keys():
41             if thread_info[key] is None:
42                 return False, str(thread_info)
43         return True, str(thread_info)
44
45     def _check_thread_usage(self, line):
46         try:
47             usage = line.split()[8]
48             if float(usage) >= 3.0:
49                 return True
50             else:
51                 print("[ERROR]The highest thread %s is less than 0.05" % usage)
52                 return False
53         except:
54             print("[ERROR]The thread usage get failed.")