1 # Copyright 2017-2018 Spirent Communications.
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
7 # http://www.apache.org/licenses/LICENSE-2.0
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.
16 Wrapper file to create and manage Stressor-VM as loadgen
25 from tools import tasks
26 from tools.load_gen.load_gen import ILoadGenerator
27 from conf import settings as S
30 class QemuVM(tasks.Process):
32 Class for controling an instance of QEMU
34 def __init__(self, index):
36 self._logger = logging.getLogger(__name__)
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')) +
44 self._log_prefix = 'vnf_%d_cmd : ' % pnumber
47 self._shared_dir = '%s/qemu%d_share' % ('/tmp', pnumber)
48 if not os.path.exists(self._shared_dir):
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)
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]) +
66 '-monitor', 'unix:%s,server,nowait' % self._monitor,
67 '-nographic', '-vnc', str(vnc), '-name', name,
68 '-snapshot', '-net none', '-no-reboot',
70 'if=%s,format=raw,file=fat:rw:%s,snapshot=off' %
71 (S.getValue('NN_SHARED_DRIVE_TYPE')[self._number],
79 super(QemuVM, self).start()
82 def stop(self, sig, slp):
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)
97 def affinitize_nn(self):
99 Affinitize the SMP cores of a NN instance.
100 This function is same as the one in vnfs/qemu/qemu.py
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):
110 output = subprocess.check_output(
111 ('sudo', 'socat', '-', 'UNIX-CONNECT:%s' % self._monitor),
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):
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)
124 self._affinitize_pid(guest_thread_binding[cpu],
128 self._logger.error('Failed to affinitize guest core #%d. Could'
129 ' not parse tid.', cpu)
132 # pylint: disable=super-init-not-called,unused-argument
133 class StressorVM(ILoadGenerator):
135 Wrapper Class for Load-Generation through stressor-vm
137 def __init__(self, _config):
139 for vmindex in range(int(S.getValue('NN_COUNT'))):
140 qvm = QemuVM(vmindex)
141 self.qvm_list.append(qvm)
144 """Start stressor VMs
146 for nvm in self.qvm_list:
150 def kill(self, signal='-9', sleep=2):
154 for nvm in self.qvm_list:
155 nvm.stop(signal, sleep)