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