]>
git.proxmox.com Git - mirror_qemu.git/blob - tests/migration/guestperf/engine.py
1 from __future__
import print_function
3 # Migration test main engine
5 # Copyright (c) 2016 Red Hat, Inc.
7 # This library is free software; you can redistribute it and/or
8 # modify it under the terms of the GNU Lesser General Public
9 # License as published by the Free Software Foundation; either
10 # version 2 of the License, or (at your option) any later version.
12 # This library is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 # Lesser General Public License for more details.
17 # You should have received a copy of the GNU Lesser General Public
18 # License along with this library; if not, see <http://www.gnu.org/licenses/>.
27 from guestperf
.progress
import Progress
, ProgressStats
28 from guestperf
.report
import Report
29 from guestperf
.timings
import TimingRecord
, Timings
31 sys
.path
.append(os
.path
.join(os
.path
.dirname(__file__
),
32 '..', '..', '..', 'python'))
33 from qemu
.machine
import QEMUMachine
38 def __init__(self
, binary
, dst_host
, kernel
, initrd
, transport
="tcp",
39 sleep
=15, verbose
=False, debug
=False):
41 self
._binary
= binary
# Path to QEMU binary
42 self
._dst
_host
= dst_host
# Hostname of target host
43 self
._kernel
= kernel
# Path to kernel image
44 self
._initrd
= initrd
# Path to stress initrd
45 self
._transport
= transport
# 'unix' or 'tcp' or 'rdma'
47 self
._verbose
= verbose
53 def _vcpu_timing(self
, pid
, tid_list
):
57 jiffies_per_sec
= os
.sysconf(os
.sysconf_names
['SC_CLK_TCK'])
59 statfile
= "/proc/%d/task/%d/stat" % (pid
, tid
)
60 with
open(statfile
, "r") as fh
:
62 fields
= stat
.split(" ")
63 stime
= int(fields
[13])
64 utime
= int(fields
[14])
65 records
.append(TimingRecord(tid
, now
, 1000 * (stime
+ utime
) / jiffies_per_sec
))
68 def _cpu_timing(self
, pid
):
72 jiffies_per_sec
= os
.sysconf(os
.sysconf_names
['SC_CLK_TCK'])
73 statfile
= "/proc/%d/stat" % pid
74 with
open(statfile
, "r") as fh
:
76 fields
= stat
.split(" ")
77 stime
= int(fields
[13])
78 utime
= int(fields
[14])
79 return TimingRecord(pid
, now
, 1000 * (stime
+ utime
) / jiffies_per_sec
)
81 def _migrate_progress(self
, vm
):
82 info
= vm
.command("query-migrate")
88 info
.get("status", "active"),
90 info
["ram"].get("transferred", 0),
91 info
["ram"].get("remaining", 0),
92 info
["ram"].get("total", 0),
93 info
["ram"].get("duplicate", 0),
94 info
["ram"].get("skipped", 0),
95 info
["ram"].get("normal", 0),
96 info
["ram"].get("normal-bytes", 0),
97 info
["ram"].get("dirty-pages-rate", 0),
98 info
["ram"].get("mbps", 0),
99 info
["ram"].get("dirty-sync-count", 0)
102 info
.get("total-time", 0),
103 info
.get("downtime", 0),
104 info
.get("expected-downtime", 0),
105 info
.get("setup-time", 0),
106 info
.get("x-cpu-throttle-percentage", 0),
109 def _migrate(self
, hardware
, scenario
, src
, dst
, connect_uri
):
112 src_pid
= src
.get_pid()
114 vcpus
= src
.command("query-cpus")
117 src_threads
.append(vcpu
["thread_id"])
119 # XXX how to get dst timings on remote host ?
122 print("Sleeping %d seconds for initial guest workload run" % self
._sleep
)
123 sleep_secs
= self
._sleep
124 while sleep_secs
> 1:
125 src_qemu_time
.append(self
._cpu
_timing
(src_pid
))
126 src_vcpu_time
.extend(self
._vcpu
_timing
(src_pid
, src_threads
))
131 print("Starting migration")
132 if scenario
._auto
_converge
:
133 resp
= src
.command("migrate-set-capabilities",
135 { "capability": "auto-converge",
138 resp
= src
.command("migrate-set-parameters",
139 x_cpu_throttle_increment
=scenario
._auto
_converge
_step
)
141 if scenario
._post
_copy
:
142 resp
= src
.command("migrate-set-capabilities",
144 { "capability": "postcopy-ram",
147 resp
= dst
.command("migrate-set-capabilities",
149 { "capability": "postcopy-ram",
153 resp
= src
.command("migrate_set_speed",
154 value
=scenario
._bandwidth
* 1024 * 1024)
156 resp
= src
.command("migrate_set_downtime",
157 value
=scenario
._downtime
/ 1024.0)
159 if scenario
._compression
_mt
:
160 resp
= src
.command("migrate-set-capabilities",
162 { "capability": "compress",
165 resp
= src
.command("migrate-set-parameters",
166 compress_threads
=scenario
._compression
_mt
_threads
)
167 resp
= dst
.command("migrate-set-capabilities",
169 { "capability": "compress",
172 resp
= dst
.command("migrate-set-parameters",
173 decompress_threads
=scenario
._compression
_mt
_threads
)
175 if scenario
._compression
_xbzrle
:
176 resp
= src
.command("migrate-set-capabilities",
178 { "capability": "xbzrle",
181 resp
= dst
.command("migrate-set-capabilities",
183 { "capability": "xbzrle",
186 resp
= src
.command("migrate-set-cache-size",
187 value
=(hardware
._mem
* 1024 * 1024 * 1024 / 100 *
188 scenario
._compression
_xbzrle
_cache
))
190 resp
= src
.command("migrate", uri
=connect_uri
)
195 progress_history
= []
203 progress
= self
._migrate
_progress
(src
)
205 src_qemu_time
.append(self
._cpu
_timing
(src_pid
))
206 src_vcpu_time
.extend(self
._vcpu
_timing
(src_pid
, src_threads
))
208 if (len(progress_history
) == 0 or
209 (progress_history
[-1]._ram
._iterations
<
210 progress
._ram
._iterations
)):
211 progress_history
.append(progress
)
213 if progress
._status
in ("completed", "failed", "cancelled"):
214 if progress
._status
== "completed" and paused
:
216 if progress_history
[-1] != progress
:
217 progress_history
.append(progress
)
219 if progress
._status
== "completed":
221 print("Sleeping %d seconds for final guest workload run" % self
._sleep
)
222 sleep_secs
= self
._sleep
223 while sleep_secs
> 1:
225 src_qemu_time
.append(self
._cpu
_timing
(src_pid
))
226 src_vcpu_time
.extend(self
._vcpu
_timing
(src_pid
, src_threads
))
229 return [progress_history
, src_qemu_time
, src_vcpu_time
]
231 if self
._verbose
and (loop
% 20) == 0:
232 print("Iter %d: remain %5dMB of %5dMB (total %5dMB @ %5dMb/sec)" % (
233 progress
._ram
._iterations
,
234 progress
._ram
._remaining
_bytes
/ (1024 * 1024),
235 progress
._ram
._total
_bytes
/ (1024 * 1024),
236 progress
._ram
._transferred
_bytes
/ (1024 * 1024),
237 progress
._ram
._transfer
_rate
_mbs
,
240 if progress
._ram
._iterations
> scenario
._max
_iters
:
242 print("No completion after %d iterations over RAM" % scenario
._max
_iters
)
243 src
.command("migrate_cancel")
246 if time
.time() > (start
+ scenario
._max
_time
):
248 print("No completion after %d seconds" % scenario
._max
_time
)
249 src
.command("migrate_cancel")
252 if (scenario
._post
_copy
and
253 progress
._ram
._iterations
>= scenario
._post
_copy
_iters
and
256 print("Switching to post-copy after %d iterations" % scenario
._post
_copy
_iters
)
257 resp
= src
.command("migrate-start-postcopy")
260 if (scenario
._pause
and
261 progress
._ram
._iterations
>= scenario
._pause
_iters
and
264 print("Pausing VM after %d iterations" % scenario
._pause
_iters
)
265 resp
= src
.command("stop")
268 def _get_common_args(self
, hardware
, tunnelled
=False):
274 "cgroup_disable=memory",
283 args
.append("ramsize=%s" % hardware
._mem
)
285 cmdline
= " ".join(args
)
287 cmdline
= "'" + cmdline
+ "'"
290 "-machine", "accel=kvm",
292 "-kernel", self
._kernel
,
293 "-initrd", self
._initrd
,
295 "-chardev", "stdio,id=cdev0",
296 "-device", "isa-serial,chardev=cdev0",
297 "-m", str((hardware
._mem
* 1024) + 512),
298 "-smp", str(hardware
._cpus
),
302 argv
.extend(["-device", "sga"])
304 if hardware
._prealloc
_pages
:
305 argv_source
+= ["-mem-path", "/dev/shm",
307 if hardware
._locked
_pages
:
308 argv_source
+= ["-realtime", "mlock=on"]
309 if hardware
._huge
_pages
:
314 def _get_src_args(self
, hardware
):
315 return self
._get
_common
_args
(hardware
)
317 def _get_dst_args(self
, hardware
, uri
):
319 if self
._dst
_host
!= "localhost":
321 argv
= self
._get
_common
_args
(hardware
, tunnelled
)
322 return argv
+ ["-incoming", uri
]
325 def _get_common_wrapper(cpu_bind
, mem_bind
):
327 if len(cpu_bind
) > 0 or len(mem_bind
) > 0:
328 wrapper
.append("numactl")
330 wrapper
.append("--physcpubind=%s" % ",".join(cpu_bind
))
332 wrapper
.append("--membind=%s" % ",".join(mem_bind
))
336 def _get_src_wrapper(self
, hardware
):
337 return self
._get
_common
_wrapper
(hardware
._src
_cpu
_bind
, hardware
._src
_mem
_bind
)
339 def _get_dst_wrapper(self
, hardware
):
340 wrapper
= self
._get
_common
_wrapper
(hardware
._dst
_cpu
_bind
, hardware
._dst
_mem
_bind
)
341 if self
._dst
_host
!= "localhost":
343 "-R", "9001:localhost:9001",
344 self
._dst
_host
] + wrapper
348 def _get_timings(self
, vm
):
355 regex
= r
"[^\s]+\s\((\d+)\):\sINFO:\s(\d+)ms\scopied\s\d+\sGB\sin\s(\d+)ms"
356 matcher
= re
.compile(regex
)
358 for line
in log
.split("\n"):
359 match
= matcher
.match(line
)
361 records
.append(TimingRecord(int(match
.group(1)),
362 int(match
.group(2)) / 1000.0,
363 int(match
.group(3))))
366 def run(self
, hardware
, scenario
, result_dir
=os
.getcwd()):
367 abs_result_dir
= os
.path
.join(result_dir
, scenario
._name
)
369 if self
._transport
== "tcp":
370 uri
= "tcp:%s:9000" % self
._dst
_host
371 elif self
._transport
== "rdma":
372 uri
= "rdma:%s:9000" % self
._dst
_host
373 elif self
._transport
== "unix":
374 if self
._dst
_host
!= "localhost":
375 raise Exception("Running use unix migration transport for non-local host")
376 uri
= "unix:/var/tmp/qemu-migrate-%d.migrate" % os
.getpid()
383 if self
._dst
_host
!= "localhost":
384 dstmonaddr
= ("localhost", 9001)
386 dstmonaddr
= "/var/tmp/qemu-dst-%d-monitor.sock" % os
.getpid()
387 srcmonaddr
= "/var/tmp/qemu-src-%d-monitor.sock" % os
.getpid()
389 src
= QEMUMachine(self
._binary
,
390 args
=self
._get
_src
_args
(hardware
),
391 wrapper
=self
._get
_src
_wrapper
(hardware
),
392 name
="qemu-src-%d" % os
.getpid(),
393 monitor_address
=srcmonaddr
)
395 dst
= QEMUMachine(self
._binary
,
396 args
=self
._get
_dst
_args
(hardware
, uri
),
397 wrapper
=self
._get
_dst
_wrapper
(hardware
),
398 name
="qemu-dst-%d" % os
.getpid(),
399 monitor_address
=dstmonaddr
)
405 ret
= self
._migrate
(hardware
, scenario
, src
, dst
, uri
)
406 progress_history
= ret
[0]
407 qemu_timings
= ret
[1]
408 vcpu_timings
= ret
[2]
409 if uri
[0:5] == "unix:":
412 print("Finished migration")
417 return Report(hardware
, scenario
, progress_history
,
418 Timings(self
._get
_timings
(src
) + self
._get
_timings
(dst
)),
419 Timings(qemu_timings
),
420 Timings(vcpu_timings
),
421 self
._binary
, self
._dst
_host
, self
._kernel
,
422 self
._initrd
, self
._transport
, self
._sleep
)
423 except Exception as e
:
425 print("Failed: %s" % str(e
))