loadgen: Affinitize Stressor-VM Threads.
[vswitchperf.git] / tools / load_gen / stressorvm / stressor_vm.py
1 # Copyright 2017-2018 Spirent Communications.
2 #
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
6 #
7 #   http://www.apache.org/licenses/LICENSE-2.0
8 #
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
14
15 """
16 Wrapper file to create and manage Stressor-VM as loadgen
17 """
18
19 import locale
20 import logging
21 import os
22 import re
23 import subprocess
24 import time
25 from tools import tasks
26 from tools.load_gen.load_gen import ILoadGenerator
27 from conf import settings as S
28
29
30 class QemuVM(tasks.Process):
31     """
32     Class for controling an instance of QEMU
33     """
34     def __init__(self, index):
35         self._running = False
36         self._logger = logging.getLogger(__name__)
37         self._number = index
38         pnumber = int(S.getValue('NN_BASE_VNC_PORT')) + self._number
39         cpumask = ",".join(S.getValue('NN_CORE_BINDING')[self._number])
40         self._monitor = '%s/vm%dmonitor' % ('/tmp', pnumber)
41         self._logfile = (os.path.join(S.getValue('LOG_DIR'),
42                                       S.getValue('NN_LOG_FILE')) +
43                          str(self._number))
44         self._log_prefix = 'vnf_%d_cmd : ' % pnumber
45         name = 'NN%d' % index
46         vnc = ':%d' % pnumber
47         self._shared_dir = '%s/qemu%d_share' % ('/tmp', pnumber)
48         if not os.path.exists(self._shared_dir):
49             try:
50                 os.makedirs(self._shared_dir)
51             except OSError as exp:
52                 raise OSError("Failed to create shared directory %s: %s" %
53                               self._shared_dir, exp)
54
55         self.nics_nr = S.getValue('NN_NICS_NR')[self._number]
56         self.image = S.getValue('NN_IMAGE')[self._number]
57         self._cmd = ['sudo', '-E', 'taskset', '-c', cpumask,
58                      S.getValue('TOOLS')['qemu-system'],
59                      '-m', S.getValue('NN_MEMORY')[self._number],
60                      '-smp', S.getValue('NN_SMP')[self._number],
61                      '-cpu', 'host,migratable=off',
62                      '-drive', 'if={},file='.format(
63                          S.getValue('NN_BOOT_DRIVE_TYPE')[self._number]) +
64                      self.image, '-boot',
65                      'c', '--enable-kvm',
66                      '-monitor', 'unix:%s,server,nowait' % self._monitor,
67                      '-nographic', '-vnc', str(vnc), '-name', name,
68                      '-snapshot', '-net none', '-no-reboot',
69                      '-drive',
70                      'if=%s,format=raw,file=fat:rw:%s,snapshot=off' %
71                      (S.getValue('NN_SHARED_DRIVE_TYPE')[self._number],
72                       self._shared_dir)
73                     ]
74
75     def start(self):
76         """
77         Start QEMU instance
78         """
79         super(QemuVM, self).start()
80         self._running = True
81
82     def stop(self, sig, slp):
83         """
84         Stops VNF instance.
85         """
86         if self._running:
87             self._logger.info('Killing VNF...')
88             # force termination of VNF and wait to terminate; It will avoid
89             # sporadic reboot of host.
90             super(QemuVM, self).kill(signal=sig, sleep=slp)
91         # remove shared dir if it exists to avoid issues with file consistency
92         if os.path.exists(self._shared_dir):
93             tasks.run_task(['rm', '-f', '-r', self._shared_dir], self._logger,
94                            'Removing content of shared directory...', True)
95         self._running = False
96
97     def affinitize_nn(self):
98         """
99         Affinitize the SMP cores of a NN instance.
100         This function is same as the one in vnfs/qemu/qemu.py
101
102         :returns: None
103         """
104         thread_id = (r'.* CPU #%d: .* thread_id=(\d+)')
105         cur_locale = locale.getdefaultlocale()[1]
106         proc = subprocess.Popen(
107             ('echo', 'info cpus'), stdout=subprocess.PIPE)
108         while not os.path.exists(self._monitor):
109             time.sleep(1)
110         output = subprocess.check_output(
111             ('sudo', 'socat', '-', 'UNIX-CONNECT:%s' % self._monitor),
112             stdin=proc.stdout)
113         proc.wait()
114
115         # calculate the number of CPUs specified by NN_SMP
116         cpu_nr = int(S.getValue('NN_SMP')[self._number])
117         # pin each NN's core to host core based on configured BINDING
118         for cpu in range(0, cpu_nr):
119             match = None
120             guest_thread_binding = S.getValue('NN_CORE_BINDING')[self._number]
121             for line in output.decode(cur_locale).split('\n'):
122                 match = re.search(thread_id % cpu, line)
123                 if match:
124                     self._affinitize_pid(guest_thread_binding[cpu],
125                                          match.group(1))
126                     break
127             if not match:
128                 self._logger.error('Failed to affinitize guest core #%d. Could'
129                                    ' not parse tid.', cpu)
130
131
132 # pylint: disable=super-init-not-called,unused-argument
133 class StressorVM(ILoadGenerator):
134     """
135     Wrapper Class for Load-Generation through stressor-vm
136     """
137     def __init__(self, _config):
138         self.qvm_list = []
139         for vmindex in range(int(S.getValue('NN_COUNT'))):
140             qvm = QemuVM(vmindex)
141             self.qvm_list.append(qvm)
142
143     def start(self):
144         """Start stressor VMs
145         """
146         for nvm in self.qvm_list:
147             nvm.start()
148             nvm.affinitize_nn()
149
150     def kill(self, signal='-9', sleep=2):
151         """
152         Stop Stressor VMs
153         """
154         for nvm in self.qvm_list:
155             nvm.stop(signal, sleep)