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