]>
git.proxmox.com Git - mirror_qemu.git/blob - tests/migration/guestperf/engine.py
2 # Migration test main engine
4 # Copyright (c) 2016 Red Hat, Inc.
6 # This library is free software; you can redistribute it and/or
7 # modify it under the terms of the GNU Lesser General Public
8 # License as published by the Free Software Foundation; either
9 # version 2.1 of the License, or (at your option) any later version.
11 # This library is distributed in the hope that it will be useful,
12 # but WITHOUT ANY WARRANTY; without even the implied warranty of
13 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 # Lesser General Public License for more details.
16 # You should have received a copy of the GNU Lesser General Public
17 # License along with this library; if not, see <http://www.gnu.org/licenses/>.
26 from guestperf
.progress
import Progress
, ProgressStats
27 from guestperf
.report
import Report
28 from guestperf
.timings
import TimingRecord
, Timings
30 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
),
31 '..', '..', '..', 'python'))
32 from qemu
.machine
import QEMUMachine
37 def __init__(self
, binary
, dst_host
, kernel
, initrd
, transport
="tcp",
38 sleep
=15, verbose
=False, debug
=False):
40 self
._binary
= binary
# Path to QEMU binary
41 self
._dst
_host
= dst_host
# Hostname of target host
42 self
._kernel
= kernel
# Path to kernel image
43 self
._initrd
= initrd
# Path to stress initrd
44 self
._transport
= transport
# 'unix' or 'tcp' or 'rdma'
46 self
._verbose
= verbose
52 def _vcpu_timing(self
, pid
, tid_list
):
56 jiffies_per_sec
= os
.sysconf(os
.sysconf_names
['SC_CLK_TCK'])
58 statfile
= "/proc/%d/task/%d/stat" % (pid
, tid
)
59 with
open(statfile
, "r") as fh
:
61 fields
= stat
.split(" ")
62 stime
= int(fields
[13])
63 utime
= int(fields
[14])
64 records
.append(TimingRecord(tid
, now
, 1000 * (stime
+ utime
) / jiffies_per_sec
))
67 def _cpu_timing(self
, pid
):
70 jiffies_per_sec
= os
.sysconf(os
.sysconf_names
['SC_CLK_TCK'])
71 statfile
= "/proc/%d/stat" % pid
72 with
open(statfile
, "r") as fh
:
74 fields
= stat
.split(" ")
75 stime
= int(fields
[13])
76 utime
= int(fields
[14])
77 return TimingRecord(pid
, now
, 1000 * (stime
+ utime
) / jiffies_per_sec
)
79 def _migrate_progress(self
, vm
):
80 info
= vm
.cmd("query-migrate")
86 info
.get("status", "active"),
88 info
["ram"].get("transferred", 0),
89 info
["ram"].get("remaining", 0),
90 info
["ram"].get("total", 0),
91 info
["ram"].get("duplicate", 0),
92 info
["ram"].get("skipped", 0),
93 info
["ram"].get("normal", 0),
94 info
["ram"].get("normal-bytes", 0),
95 info
["ram"].get("dirty-pages-rate", 0),
96 info
["ram"].get("mbps", 0),
97 info
["ram"].get("dirty-sync-count", 0)
100 info
.get("total-time", 0),
101 info
.get("downtime", 0),
102 info
.get("expected-downtime", 0),
103 info
.get("setup-time", 0),
104 info
.get("cpu-throttle-percentage", 0),
107 def _migrate(self
, hardware
, scenario
, src
, dst
, connect_uri
):
110 src_pid
= src
.get_pid()
112 vcpus
= src
.cmd("query-cpus-fast")
115 src_threads
.append(vcpu
["thread-id"])
117 # XXX how to get dst timings on remote host ?
120 print("Sleeping %d seconds for initial guest workload run" % self
._sleep
)
121 sleep_secs
= self
._sleep
122 while sleep_secs
> 1:
123 src_qemu_time
.append(self
._cpu
_timing
(src_pid
))
124 src_vcpu_time
.extend(self
._vcpu
_timing
(src_pid
, src_threads
))
129 print("Starting migration")
130 if scenario
._auto
_converge
:
131 resp
= src
.cmd("migrate-set-capabilities",
133 { "capability": "auto-converge",
136 resp
= src
.cmd("migrate-set-parameters",
137 cpu_throttle_increment
=scenario
._auto
_converge
_step
)
139 if scenario
._post
_copy
:
140 resp
= src
.cmd("migrate-set-capabilities",
142 { "capability": "postcopy-ram",
145 resp
= dst
.cmd("migrate-set-capabilities",
147 { "capability": "postcopy-ram",
151 resp
= src
.cmd("migrate-set-parameters",
152 max_bandwidth
=scenario
._bandwidth
* 1024 * 1024)
154 resp
= src
.cmd("migrate-set-parameters",
155 downtime_limit
=scenario
._downtime
)
157 if scenario
._compression
_mt
:
158 resp
= src
.cmd("migrate-set-capabilities",
160 { "capability": "compress",
163 resp
= src
.cmd("migrate-set-parameters",
164 compress_threads
=scenario
._compression
_mt
_threads
)
165 resp
= dst
.cmd("migrate-set-capabilities",
167 { "capability": "compress",
170 resp
= dst
.cmd("migrate-set-parameters",
171 decompress_threads
=scenario
._compression
_mt
_threads
)
173 if scenario
._compression
_xbzrle
:
174 resp
= src
.cmd("migrate-set-capabilities",
176 { "capability": "xbzrle",
179 resp
= dst
.cmd("migrate-set-capabilities",
181 { "capability": "xbzrle",
184 resp
= src
.cmd("migrate-set-parameters",
187 1024 * 1024 * 1024 / 100 *
188 scenario
._compression
_xbzrle
_cache
))
190 if scenario
._multifd
:
191 resp
= src
.cmd("migrate-set-capabilities",
193 { "capability": "multifd",
196 resp
= src
.cmd("migrate-set-parameters",
197 multifd_channels
=scenario
._multifd
_channels
)
198 resp
= dst
.cmd("migrate-set-capabilities",
200 { "capability": "multifd",
203 resp
= dst
.cmd("migrate-set-parameters",
204 multifd_channels
=scenario
._multifd
_channels
)
206 resp
= src
.cmd("migrate", uri
=connect_uri
)
211 progress_history
= []
219 progress
= self
._migrate
_progress
(src
)
221 src_qemu_time
.append(self
._cpu
_timing
(src_pid
))
222 src_vcpu_time
.extend(self
._vcpu
_timing
(src_pid
, src_threads
))
224 if (len(progress_history
) == 0 or
225 (progress_history
[-1]._ram
._iterations
<
226 progress
._ram
._iterations
)):
227 progress_history
.append(progress
)
229 if progress
._status
in ("completed", "failed", "cancelled"):
230 if progress
._status
== "completed" and paused
:
232 if progress_history
[-1] != progress
:
233 progress_history
.append(progress
)
235 if progress
._status
== "completed":
237 print("Sleeping %d seconds for final guest workload run" % self
._sleep
)
238 sleep_secs
= self
._sleep
239 while sleep_secs
> 1:
241 src_qemu_time
.append(self
._cpu
_timing
(src_pid
))
242 src_vcpu_time
.extend(self
._vcpu
_timing
(src_pid
, src_threads
))
245 return [progress_history
, src_qemu_time
, src_vcpu_time
]
247 if self
._verbose
and (loop
% 20) == 0:
248 print("Iter %d: remain %5dMB of %5dMB (total %5dMB @ %5dMb/sec)" % (
249 progress
._ram
._iterations
,
250 progress
._ram
._remaining
_bytes
/ (1024 * 1024),
251 progress
._ram
._total
_bytes
/ (1024 * 1024),
252 progress
._ram
._transferred
_bytes
/ (1024 * 1024),
253 progress
._ram
._transfer
_rate
_mbs
,
256 if progress
._ram
._iterations
> scenario
._max
_iters
:
258 print("No completion after %d iterations over RAM" % scenario
._max
_iters
)
259 src
.cmd("migrate_cancel")
262 if time
.time() > (start
+ scenario
._max
_time
):
264 print("No completion after %d seconds" % scenario
._max
_time
)
265 src
.cmd("migrate_cancel")
268 if (scenario
._post
_copy
and
269 progress
._ram
._iterations
>= scenario
._post
_copy
_iters
and
272 print("Switching to post-copy after %d iterations" % scenario
._post
_copy
_iters
)
273 resp
= src
.cmd("migrate-start-postcopy")
276 if (scenario
._pause
and
277 progress
._ram
._iterations
>= scenario
._pause
_iters
and
280 print("Pausing VM after %d iterations" % scenario
._pause
_iters
)
281 resp
= src
.cmd("stop")
284 def _is_ppc64le(self
):
285 _
, _
, _
, _
, machine
= os
.uname()
286 if machine
== "ppc64le":
290 def _get_guest_console_args(self
):
291 if self
._is
_ppc
64le
():
292 return "console=hvc0"
294 return "console=ttyS0"
296 def _get_qemu_serial_args(self
):
297 if self
._is
_ppc
64le
():
298 return ["-chardev", "stdio,id=cdev0",
299 "-device", "spapr-vty,chardev=cdev0"]
301 return ["-chardev", "stdio,id=cdev0",
302 "-device", "isa-serial,chardev=cdev0"]
304 def _get_common_args(self
, hardware
, tunnelled
=False):
310 "cgroup_disable=memory",
314 args
.append(self
._get
_guest
_console
_args
())
321 args
.append("ramsize=%s" % hardware
._mem
)
323 cmdline
= " ".join(args
)
325 cmdline
= "'" + cmdline
+ "'"
330 "-kernel", self
._kernel
,
331 "-initrd", self
._initrd
,
333 "-m", str((hardware
._mem
* 1024) + 512),
334 "-smp", str(hardware
._cpus
),
337 argv
.extend(self
._get
_qemu
_serial
_args
())
340 argv
.extend(["-machine", "graphics=off"])
342 if hardware
._prealloc
_pages
:
343 argv_source
+= ["-mem-path", "/dev/shm",
345 if hardware
._locked
_pages
:
346 argv_source
+= ["-overcommit", "mem-lock=on"]
347 if hardware
._huge
_pages
:
352 def _get_src_args(self
, hardware
):
353 return self
._get
_common
_args
(hardware
)
355 def _get_dst_args(self
, hardware
, uri
):
357 if self
._dst
_host
!= "localhost":
359 argv
= self
._get
_common
_args
(hardware
, tunnelled
)
360 return argv
+ ["-incoming", uri
]
363 def _get_common_wrapper(cpu_bind
, mem_bind
):
365 if len(cpu_bind
) > 0 or len(mem_bind
) > 0:
366 wrapper
.append("numactl")
368 wrapper
.append("--physcpubind=%s" % ",".join(cpu_bind
))
370 wrapper
.append("--membind=%s" % ",".join(mem_bind
))
374 def _get_src_wrapper(self
, hardware
):
375 return self
._get
_common
_wrapper
(hardware
._src
_cpu
_bind
, hardware
._src
_mem
_bind
)
377 def _get_dst_wrapper(self
, hardware
):
378 wrapper
= self
._get
_common
_wrapper
(hardware
._dst
_cpu
_bind
, hardware
._dst
_mem
_bind
)
379 if self
._dst
_host
!= "localhost":
381 "-R", "9001:localhost:9001",
382 self
._dst
_host
] + wrapper
386 def _get_timings(self
, vm
):
393 regex
= r
"[^\s]+\s\((\d+)\):\sINFO:\s(\d+)ms\scopied\s\d+\sGB\sin\s(\d+)ms"
394 matcher
= re
.compile(regex
)
396 for line
in log
.split("\n"):
397 match
= matcher
.match(line
)
399 records
.append(TimingRecord(int(match
.group(1)),
400 int(match
.group(2)) / 1000.0,
401 int(match
.group(3))))
404 def run(self
, hardware
, scenario
, result_dir
=os
.getcwd()):
405 abs_result_dir
= os
.path
.join(result_dir
, scenario
._name
)
407 if self
._transport
== "tcp":
408 uri
= "tcp:%s:9000" % self
._dst
_host
409 elif self
._transport
== "rdma":
410 uri
= "rdma:%s:9000" % self
._dst
_host
411 elif self
._transport
== "unix":
412 if self
._dst
_host
!= "localhost":
413 raise Exception("Running use unix migration transport for non-local host")
414 uri
= "unix:/var/tmp/qemu-migrate-%d.migrate" % os
.getpid()
421 if self
._dst
_host
!= "localhost":
422 dstmonaddr
= ("localhost", 9001)
424 dstmonaddr
= "/var/tmp/qemu-dst-%d-monitor.sock" % os
.getpid()
425 srcmonaddr
= "/var/tmp/qemu-src-%d-monitor.sock" % os
.getpid()
427 src
= QEMUMachine(self
._binary
,
428 args
=self
._get
_src
_args
(hardware
),
429 wrapper
=self
._get
_src
_wrapper
(hardware
),
430 name
="qemu-src-%d" % os
.getpid(),
431 monitor_address
=srcmonaddr
)
433 dst
= QEMUMachine(self
._binary
,
434 args
=self
._get
_dst
_args
(hardware
, uri
),
435 wrapper
=self
._get
_dst
_wrapper
(hardware
),
436 name
="qemu-dst-%d" % os
.getpid(),
437 monitor_address
=dstmonaddr
)
443 ret
= self
._migrate
(hardware
, scenario
, src
, dst
, uri
)
444 progress_history
= ret
[0]
445 qemu_timings
= ret
[1]
446 vcpu_timings
= ret
[2]
447 if uri
[0:5] == "unix:" and os
.path
.exists(uri
[5:]):
450 if os
.path
.exists(srcmonaddr
):
451 os
.remove(srcmonaddr
)
453 if self
._dst
_host
== "localhost" and os
.path
.exists(dstmonaddr
):
454 os
.remove(dstmonaddr
)
457 print("Finished migration")
462 return Report(hardware
, scenario
, progress_history
,
463 Timings(self
._get
_timings
(src
) + self
._get
_timings
(dst
)),
464 Timings(qemu_timings
),
465 Timings(vcpu_timings
),
466 self
._binary
, self
._dst
_host
, self
._kernel
,
467 self
._initrd
, self
._transport
, self
._sleep
)
468 except Exception as e
:
470 print("Failed: %s" % str(e
))