JIRA: BOTTLENECKS-29
[bottlenecks.git] / vstf / vstf / agent / perf / utils.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 logging
11 import subprocess
12 from vstf.common.utils import check_call, check_output
13
14 LOG = logging.getLogger(__name__)
15
16
17 def get_pid_by_name(process_name):
18     out = check_output(['ps', '-A'])
19     pids = []
20     for line in out.splitlines():
21         values = line.split()
22         pid, name = values[0], values[3]
23         if process_name == name:
24             pids.append(int(pid))
25     return pids
26
27
28 def get_cpu_num():
29     cpu_num = check_output('cat /proc/cpuinfo  | grep processor | wc -l', shell=True).strip()
30     cpu_num = int(cpu_num)
31     return cpu_num
32
33
34 def get_default_threads():
35     cpu_num = get_cpu_num()
36     return 2 if cpu_num > 3 * 3 else 1
37
38
39 def modprobe_pktgen():
40     check_call('modprobe pktgen', shell=True)
41     return True
42
43
44 def iface_up(device):
45     check_call("ifconfig %s up" % device, shell=True)
46     return True