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
):
71 jiffies_per_sec
= os
.sysconf(os
.sysconf_names
['SC_CLK_TCK'])
72 statfile
= "/proc/%d/stat" % pid
73 with
open(statfile
, "r") as fh
:
75 fields
= stat
.split(" ")
76 stime
= int(fields
[13])
77 utime
= int(fields
[14])
78 return TimingRecord(pid
, now
, 1000 * (stime
+ utime
) / jiffies_per_sec
)
80 def _migrate_progress(self
, vm
):
81 info
= vm
.command("query-migrate")
87 info
.get("status", "active"),
89 info
["ram"].get("transferred", 0),
90 info
["ram"].get("remaining", 0),
91 info
["ram"].get("total", 0),
92 info
["ram"].get("duplicate", 0),
93 info
["ram"].get("skipped", 0),
94 info
["ram"].get("normal", 0),
95 info
["ram"].get("normal-bytes", 0),
96 info
["ram"].get("dirty-pages-rate", 0),
97 info
["ram"].get("mbps", 0),
98 info
["ram"].get("dirty-sync-count", 0)
101 info
.get("total-time", 0),
102 info
.get("downtime", 0),
103 info
.get("expected-downtime", 0),
104 info
.get("setup-time", 0),
105 info
.get("x-cpu-throttle-percentage", 0),
108 def _migrate(self
, hardware
, scenario
, src
, dst
, connect_uri
):
111 src_pid
= src
.get_pid()
113 vcpus
= src
.command("query-cpus-fast")
116 src_threads
.append(vcpu
["thread_id"])
118 # XXX how to get dst timings on remote host ?
121 print("Sleeping %d seconds for initial guest workload run" % self
._sleep
)
122 sleep_secs
= self
._sleep
123 while sleep_secs
> 1:
124 src_qemu_time
.append(self
._cpu
_timing
(src_pid
))
125 src_vcpu_time
.extend(self
._vcpu
_timing
(src_pid
, src_threads
))
130 print("Starting migration")
131 if scenario
._auto
_converge
:
132 resp
= src
.command("migrate-set-capabilities",
134 { "capability": "auto-converge",
137 resp
= src
.command("migrate-set-parameters",
138 x_cpu_throttle_increment
=scenario
._auto
_converge
_step
)
140 if scenario
._post
_copy
:
141 resp
= src
.command("migrate-set-capabilities",
143 { "capability": "postcopy-ram",
146 resp
= dst
.command("migrate-set-capabilities",
148 { "capability": "postcopy-ram",
152 resp
= src
.command("migrate-set-parameters",
153 max_bandwidth
=scenario
._bandwidth
* 1024 * 1024)
155 resp
= src
.command("migrate-set-parameters",
156 downtime_limit
=scenario
._downtime
/ 1024.0)
158 if scenario
._compression
_mt
:
159 resp
= src
.command("migrate-set-capabilities",
161 { "capability": "compress",
164 resp
= src
.command("migrate-set-parameters",
165 compress_threads
=scenario
._compression
_mt
_threads
)
166 resp
= dst
.command("migrate-set-capabilities",
168 { "capability": "compress",
171 resp
= dst
.command("migrate-set-parameters",
172 decompress_threads
=scenario
._compression
_mt
_threads
)
174 if scenario
._compression
_xbzrle
:
175 resp
= src
.command("migrate-set-capabilities",
177 { "capability": "xbzrle",
180 resp
= dst
.command("migrate-set-capabilities",
182 { "capability": "xbzrle",
185 resp
= src
.command("migrate-set-parameters",
188 1024 * 1024 * 1024 / 100 *
189 scenario
._compression
_xbzrle
_cache
))
191 resp
= src
.command("migrate", uri
=connect_uri
)
196 progress_history
= []
204 progress
= self
._migrate
_progress
(src
)
206 src_qemu_time
.append(self
._cpu
_timing
(src_pid
))
207 src_vcpu_time
.extend(self
._vcpu
_timing
(src_pid
, src_threads
))
209 if (len(progress_history
) == 0 or
210 (progress_history
[-1]._ram
._iterations
<
211 progress
._ram
._iterations
)):
212 progress_history
.append(progress
)
214 if progress
._status
in ("completed", "failed", "cancelled"):
215 if progress
._status
== "completed" and paused
:
217 if progress_history
[-1] != progress
:
218 progress_history
.append(progress
)
220 if progress
._status
== "completed":
222 print("Sleeping %d seconds for final guest workload run" % self
._sleep
)
223 sleep_secs
= self
._sleep
224 while sleep_secs
> 1:
226 src_qemu_time
.append(self
._cpu
_timing
(src_pid
))
227 src_vcpu_time
.extend(self
._vcpu
_timing
(src_pid
, src_threads
))
230 return [progress_history
, src_qemu_time
, src_vcpu_time
]
232 if self
._verbose
and (loop
% 20) == 0:
233 print("Iter %d: remain %5dMB of %5dMB (total %5dMB @ %5dMb/sec)" % (
234 progress
._ram
._iterations
,
235 progress
._ram
._remaining
_bytes
/ (1024 * 1024),
236 progress
._ram
._total
_bytes
/ (1024 * 1024),
237 progress
._ram
._transferred
_bytes
/ (1024 * 1024),
238 progress
._ram
._transfer
_rate
_mbs
,
241 if progress
._ram
._iterations
> scenario
._max
_iters
:
243 print("No completion after %d iterations over RAM" % scenario
._max
_iters
)
244 src
.command("migrate_cancel")
247 if time
.time() > (start
+ scenario
._max
_time
):
249 print("No completion after %d seconds" % scenario
._max
_time
)
250 src
.command("migrate_cancel")
253 if (scenario
._post
_copy
and
254 progress
._ram
._iterations
>= scenario
._post
_copy
_iters
and
257 print("Switching to post-copy after %d iterations" % scenario
._post
_copy
_iters
)
258 resp
= src
.command("migrate-start-postcopy")
261 if (scenario
._pause
and
262 progress
._ram
._iterations
>= scenario
._pause
_iters
and
265 print("Pausing VM after %d iterations" % scenario
._pause
_iters
)
266 resp
= src
.command("stop")
269 def _get_common_args(self
, hardware
, tunnelled
=False):
275 "cgroup_disable=memory",
284 args
.append("ramsize=%s" % hardware
._mem
)
286 cmdline
= " ".join(args
)
288 cmdline
= "'" + cmdline
+ "'"
293 "-kernel", self
._kernel
,
294 "-initrd", self
._initrd
,
296 "-chardev", "stdio,id=cdev0",
297 "-device", "isa-serial,chardev=cdev0",
298 "-m", str((hardware
._mem
* 1024) + 512),
299 "-smp", str(hardware
._cpus
),
303 argv
.extend(["-device", "sga"])
305 if hardware
._prealloc
_pages
:
306 argv_source
+= ["-mem-path", "/dev/shm",
308 if hardware
._locked
_pages
:
309 argv_source
+= ["-overcommit", "mem-lock=on"]
310 if hardware
._huge
_pages
:
315 def _get_src_args(self
, hardware
):
316 return self
._get
_common
_args
(hardware
)
318 def _get_dst_args(self
, hardware
, uri
):
320 if self
._dst
_host
!= "localhost":
322 argv
= self
._get
_common
_args
(hardware
, tunnelled
)
323 return argv
+ ["-incoming", uri
]
326 def _get_common_wrapper(cpu_bind
, mem_bind
):
328 if len(cpu_bind
) > 0 or len(mem_bind
) > 0:
329 wrapper
.append("numactl")
331 wrapper
.append("--physcpubind=%s" % ",".join(cpu_bind
))
333 wrapper
.append("--membind=%s" % ",".join(mem_bind
))
337 def _get_src_wrapper(self
, hardware
):
338 return self
._get
_common
_wrapper
(hardware
._src
_cpu
_bind
, hardware
._src
_mem
_bind
)
340 def _get_dst_wrapper(self
, hardware
):
341 wrapper
= self
._get
_common
_wrapper
(hardware
._dst
_cpu
_bind
, hardware
._dst
_mem
_bind
)
342 if self
._dst
_host
!= "localhost":
344 "-R", "9001:localhost:9001",
345 self
._dst
_host
] + wrapper
349 def _get_timings(self
, vm
):
356 regex
= r
"[^\s]+\s\((\d+)\):\sINFO:\s(\d+)ms\scopied\s\d+\sGB\sin\s(\d+)ms"
357 matcher
= re
.compile(regex
)
359 for line
in log
.split("\n"):
360 match
= matcher
.match(line
)
362 records
.append(TimingRecord(int(match
.group(1)),
363 int(match
.group(2)) / 1000.0,
364 int(match
.group(3))))
367 def run(self
, hardware
, scenario
, result_dir
=os
.getcwd()):
368 abs_result_dir
= os
.path
.join(result_dir
, scenario
._name
)
370 if self
._transport
== "tcp":
371 uri
= "tcp:%s:9000" % self
._dst
_host
372 elif self
._transport
== "rdma":
373 uri
= "rdma:%s:9000" % self
._dst
_host
374 elif self
._transport
== "unix":
375 if self
._dst
_host
!= "localhost":
376 raise Exception("Running use unix migration transport for non-local host")
377 uri
= "unix:/var/tmp/qemu-migrate-%d.migrate" % os
.getpid()
384 if self
._dst
_host
!= "localhost":
385 dstmonaddr
= ("localhost", 9001)
387 dstmonaddr
= "/var/tmp/qemu-dst-%d-monitor.sock" % os
.getpid()
388 srcmonaddr
= "/var/tmp/qemu-src-%d-monitor.sock" % os
.getpid()
390 src
= QEMUMachine(self
._binary
,
391 args
=self
._get
_src
_args
(hardware
),
392 wrapper
=self
._get
_src
_wrapper
(hardware
),
393 name
="qemu-src-%d" % os
.getpid(),
394 monitor_address
=srcmonaddr
)
396 dst
= QEMUMachine(self
._binary
,
397 args
=self
._get
_dst
_args
(hardware
, uri
),
398 wrapper
=self
._get
_dst
_wrapper
(hardware
),
399 name
="qemu-dst-%d" % os
.getpid(),
400 monitor_address
=dstmonaddr
)
406 ret
= self
._migrate
(hardware
, scenario
, src
, dst
, uri
)
407 progress_history
= ret
[0]
408 qemu_timings
= ret
[1]
409 vcpu_timings
= ret
[2]
410 if uri
[0:5] == "unix:":
413 if os
.path
.exists(srcmonaddr
):
414 os
.remove(srcmonaddr
)
416 if self
._dst
_host
== "localhost" and os
.path
.exists(dstmonaddr
):
417 os
.remove(dstmonaddr
)
420 print("Finished migration")
425 return Report(hardware
, scenario
, progress_history
,
426 Timings(self
._get
_timings
(src
) + self
._get
_timings
(dst
)),
427 Timings(qemu_timings
),
428 Timings(vcpu_timings
),
429 self
._binary
, self
._dst
_host
, self
._kernel
,
430 self
._initrd
, self
._transport
, self
._sleep
)
431 except Exception as e
:
433 print("Failed: %s" % str(e
))