tools: optional LZ4 compression of the libxenguest migration stream
cover tools: optional LZ4 compression of the libxenguest migration stream
Hello,
This series adds optional LZ4 compression of the REC_TYPE_PAGE_DATA records of the libxenguest migration stream, reducing the data transferred during live migration (and suspend-to-file).
It is opt-in for:
* the build: compiled in only when liblz4 is present (HAVE_LZ4), which tools/configure already autodetects
* the toolstack: via new flag XCFLAGS_COMPRESS_LZ4 (off by default)
* libxl: via new option xl migrate --compress
Example usage:
xl -vvv migrate --compress <vm> <destination host>
xc: detail: fd .., dom .., flags 5, hvm <= 5: XCFLAGS_COMPRESS_LZ4 is on
xc: debug: LZ4: 4190208 -> 20732 bytes (0.5% ratio) <= compress 4MB batch
...
Migration successful.
Motivation
==========
It's common for recent VMs to have large memory footprints. This increases the effort to stream the memory during live migration or suspend, as the streamed data hops across several steps before the VM memory reaches the intended endpoint. The stream is encrypted, transmited over the network or persisted in storage, decrypted at the endpoint etc. In each step, CPU could have been saved and I/O data paths avoided bottlenecks if the stream had been compressed as close to the origin of the data as possible. Compressing at the first opportunity, inside libxenguest:
* keeps the compressor co-located with the guest page mapping so the fast path can be made zero-copy from the guest page into the compressor,
* saves on the amount of data copied/piped across processes in dom0,
* saves on the amount of data transmitted/persisted.
The LZ4 compression has a small cost, but this cost is amortized across all the operations above, resulting in a net gain (see below). LZ4 was chosen for its very high compression/decompression throughput on commodity CPUs, low memory footprint, and the existence of a stable common upstream library (liblz4).
We found the use of compression useful in general when migrating individual VMs, as LZ4 typically halves or better the streamed data, and especially useful when:
* migrating several VMs in parallel, as the net savings in CPU and I/O data paths reduce bottlenecks in dom0.
* networks are slow, as less data is transmitted
* many pipelined processes in dom0 are used to manipulate the stream data, as the dom0 kernel avoids data copy and scheduling activities.
Wire format
===========
A new record type REC_TYPE_PAGE_DATA_LZ4 (0x00000013) is added. It shares the existing xc_sr_rec_page_data_header layout but the trailing payload is a sequence of per-page sub-blocks: for each page a uint16 little-endian length clen then clen octets of a independently compressed page, or the raw page when clen == 0.
Senders that do not opt in continue to emit REC_TYPE_PAGE_DATA exactly as before; receivers built without liblz4 produce a clear error when they meet a compressed record rather than the generic "unrecognised record" diagnostic.
Compatibility
=============
* Default behaviour is unchanged: without XCFLAGS_COMPRESS_LZ4 (or xl migrate --compress), the stream is byte-for-byte identical to before this series.
* A receiver that includes this series can decompress streams from any sender (legacy or compressed)
* If the toolstack asks for compression but libxenguest was built without liblz4, xc_domain_save() fails immediately with EOPNOTSUPP rather than silently emitting an uncompressed stream.
Testing
=======
Tested with xl migrate --compress and with a patched version of XAPI.
Measurements
============
XAPI host-evacuation scenario: 64GB VMs, dom0 with 16 vCPUs, stunnel- encrypted migration transport, where
* hosts: 2x Intel Xeon Gold 6430, 1TB DDR5-4800, NIC 100 Gbps
* VM: 12 vCPUs, 64GB RAM, Windows OS
show significant wall-clock reduction when LZ4 compression is enabled.
Test = time xe host-evacuate (for 10x VMs)evacuate_ratio = host_evacuate_time_with_LZ4 / host_evacuate_without_LZ4LZ4_ratio = transmitted_LZ4_compressed_memory / total_VM_memory
Host Evacuation: Scenario 1: (BEST CASE)
----------------------------------------
guest load:
* VMs idle just after reboot
LZ4 LZ4_ratio evacuate_time evacuate_time_ratio off - 3m18s 100% (baseline) on 1.4% 1m44s 52% when LZ4 is on
Host Evacuation: Scenario 2: (AVERAGE CASE)
-------------------------------------------
guest load:
* VMs running desktop apps in loop for 1 hour before migration
LZ4 LZ4_ratio evacuate_time evacuate_time_ratio off - 3m18s 100% (baseline) on 7.0% 2m02s 62% when LZ4 is on
Host Evacuation: Scenario 3: (WORST CASE)
-----------------------------------------
guest load:
* 100% constant CPU load quickly dirtying all memory pages
* all memory filled with random uncompressible buffers
* constant swapping to disk
LZ4 LZ4_ratio evacuate_time evacuate_time_ratio off - 23m12s 100% (baseline) on 98.8% 23m25s 101% when LZ4 is on
The scenario 3 is the expected upper bound of time when LZ4 compression is used, and it's around 101% compared to when LZ4 compression is not used. The limit of 1% maximum overhead when compression is enabled is due to:
* using a fast LZ4 compression/decompression algorithm
* if the 4MB compressed batch ratio is >97%, then send uncompressed to save the time that would be used decompressing on the receiver.
* if a whole iteration of migration achieves >90% LZ4_ratio, then disable compression for the subsequent iterations.
The thresholds are conservative: 97% per-batch compresses only when it saves over 3% (when compression is more likely to repay decompression), and 90% per-iteration disables compression for the rest of the migration once a iteration saves under 10% (effectively uncompressible). Together they cap the worst case (Scenario 3) at about 1% over uncompressed.
Patch summary
=============
1. tools/migration: introduce PAGE_DATA_LZ4 stream record type
- define REC_TYPE_PAGE_DATA_LZ4 (0x00000013)
2. tools/libs/guest: decompress PAGE_DATA_LZ4 records on restore
- empower the receiver to handle and decompress incoming LZ4 payloads
3. tools/libs/guest: compress PAGE_DATA with LZ4 on save
- add the save-side LZ4 batching and streaming path with persistent buffers
- emit REC_TYPE_PAGE_DATA_LZ4 records depending on per-batch ratio threshold (XC_SR_COMPRESS_BATCH_THRESHOLD = 97%)
4. tools/libs/guest: adaptively disable LZ4 on poor compression ratio
- downgrade ctx->compression to XC_SR_COMPRESS_NONE after an iteration whose average per-batch ratio fails the 90% threshold
5. tools/libs/guest: opt in to LZ4 save-side compression
- via XCFLAGS_COMPRESS_LZ4 flag in public API to request compression
6. tools/libxl,xl: add --compress flag to xl migrate
- wire the feature up to xl migrate --compress and libxl, so that xl users can opt in to compression during migration
Comments/feedback are welcome!
PATCH 1/6 tools/migration: introduce PAGE_DATA_LZ4 stream record type
Allocate a new migration stream record type REC_TYPE_PAGE_DATA_LZ4 (0x00000013) to transmit PAGE_DATA when payload is LZ4-compressed. The record use the same xc_sr_rec_page_data_header as REC_TYPE_PAGE_DATA but the page data section type changes from page_data to data_lz4.
--- docs/specs/libxc-migration-stream.pandoc | 71 ++++++++++++++++- tools/libs/guest/xg_sr_common.c | 4 + tools/libs/guest/xg_sr_stream_format.h | 13 ++++ tools/python/xen/migration/libxc.py | 90 ++++++++++++++++++++- tools/python/xen/migration/tests.py | 99 +++++++++++++++++++++++- 5 files changed, 271 insertions(+), 6 deletions(-)
docs/specs/libxc-migration-stream.pandoc +67 -4
@@ -3,7 +3,8 @@ Andrew Cooper <<[email protected]>> Wen Congyang <<[email protected]>> Yang Hongyang <<[email protected]>> -% Revision 3 + Marcus Granado <<[email protected]>> +% Revision 4 Introduction ============
@@ -39,8 +40,6 @@ Not Yet Included The following features are not yet fully specified and will be included in a future draft. -* Page data compression. - * ARM
@@ -233,7 +232,9 @@ type 0x00000000: END 0x00000012: X86_MSR_POLICY - 0x00000013 - 0x7FFFFFFF: Reserved for future _mandatory_ + 0x00000013: PAGE_DATA_LZ4 + + 0x00000014 - 0x7FFFFFFF: Reserved for future _mandatory_ records. 0x80000000 - 0xFFFFFFFF: Reserved for future _optional_
@@ -359,6 +360,68 @@ tail. \clearpage +PAGE_DATA_LZ4 +------------- + +A PAGE_DATA_LZ4 record carries exactly the same information as a +PAGE_DATA record, but with the page contents LZ4-compressed. The saver +may emit it in place of a PAGE_DATA record when LZ4 compression has been +requested. + + 0 1 2 3 4 5 6 7 octet + +-----------------------+-------------------------+ + | count (C) | (reserved) | + +-----------------------+-------------------------+ + | pfn[0] | + +-------------------------------------------------+ + ... + +-------------------------------------------------+ + | pfn[C-1] | + +-----------+-------------------------------------+ + | clen[0] | page_data_lz4[0]... | + +-----------+-------------------------------------+ + ... + +-----------+-------------------------------------+ + | clen[N-1] | page_data_lz4[N-1]... | + ... + +-------------------------------------------------+ + +-------------------------------------------------------------------- +Field Description +----------- -------------------------------------------------------- +count Number of pages described in this record. + +pfn An array of count PFNs and their types, with the same + layout and page types as in a PAGE_DATA record. + +data_lz4 The compressed page contents, as one sub-block per page + set as present in the pfn array, in pfn-array order (i.e. + N sub-blocks, with N as in a PAGE_DATA record: N <= C). + Each sub-block is a `uint16` little-endian length `clen` + followed by either an LZ4 block or a raw page. When + `clen > 0`, page_data_lz4 is `clen` octets of a raw LZ4 + block (per the LZ4 block format) whose decompressed output + is one page_size page. When `clen == 0`, page_data_lz4 is + page_size octets of a raw, uncompressed page. + A compressed `clen` is at most page_size - 1, occupying + only the low 12 bits; the top 4 bits are reserved for + future use and must be 0. +-------------------------------------------------------------------- + +The `count` (C) and `pfn` fields are identical in meaning and +constraints to those of a PAGE_DATA record, and N (the number of +present pages, N <= C) is as defined there. Unlike PAGE_DATA, a +PAGE_DATA_LZ4 record always has N >= 1 (at least one sub-block): +the saver emits the LZ4 variant only when there is page data to +compress, and a restoring side rejects a PAGE_DATA_LZ4 record with +N == 0. When a batch has no present pages (all pfns of invalid +types) the saver emits a plain PAGE_DATA record instead. + +PAGE_DATA_LZ4 is a _mandatory_ record: a restoring side that does not +support it must fail the migration. + +\clearpage + X86_PV_INFO -----------
tools/libs/guest/xg_sr_common.c +4 -0
@@ -39,6 +39,7 @@ static const char *const mandatory_rec_types[] = [REC_TYPE_STATIC_DATA_END] = "Static data end", [REC_TYPE_X86_CPUID_POLICY] = "x86 CPUID policy", [REC_TYPE_X86_MSR_POLICY] = "x86 MSR policy", + [REC_TYPE_PAGE_DATA_LZ4] = "Page data (lz4)", };const char *rec_type_to_str (uint32_t type)
@@ -154,6 +155,9 @@ static void __attribute__((unused)) build_assertions(void)BUILD_BUG_ON (sizeof (struct xc_sr_rec_x86_tsc_info ) !=24 );BUILD_BUG_ON (sizeof (struct xc_sr_rec_hvm_params_entry ) !=16 );BUILD_BUG_ON (sizeof (struct xc_sr_rec_hvm_params ) !=8 ); + +/* clen's low bits hold the compressed length; reserved bits stay clear */ +BUILD_BUG_ON ((PAGE_SIZE -1 ) &PAGE_DATA_LZ4_CLEN_RESERVED_MASK ); }/*
tools/libs/guest/xg_sr_stream_format.h +13 -0
@@ -76,6 +76,7 @@ struct xc_sr_rhdr#define REC_TYPE_STATIC_DATA_END 0x00000010U#define REC_TYPE_X86_CPUID_POLICY 0x00000011U#define REC_TYPE_X86_MSR_POLICY 0x00000012U +#define REC_TYPE_PAGE_DATA_LZ4 0x00000013U#define REC_TYPE_OPTIONAL 0x80000000U
@@ -90,6 +91,18 @@ struct xc_sr_rec_page_data_header#define PAGE_DATA_PFN_MASK 0x000fffffffffffffULL#define PAGE_DATA_TYPE_MASK 0xf000000000000000ULL +/* +* PAGE_DATA_LZ4 payload: one sub-block per present page (pfn order), each a +* uint16 LE length clen then clen octets. clen == PAGE_DATA_LZ4_CLEN_RAW +* means a raw PAGE_SIZE page follows. A compressed length is at most +* PAGE_SIZE - 1, so it occupies only the low 12 bits; the top 4 bits +* (PAGE_DATA_LZ4_CLEN_RESERVED_MASK) are reserved for future use and must +* be 0. +*/ +#define PAGE_DATA_LZ4_CLEN_SIZE (sizeof(uint16_t)) +#define PAGE_DATA_LZ4_CLEN_RAW 0x0000U +#define PAGE_DATA_LZ4_CLEN_RESERVED_MASK 0xF000U +/* X86_PV_INFO */ struct xc_sr_rec_x86_pv_info {
tools/python/xen/migration/libxc.py +89 -1
124 lines not on this page · raw message
tools/python/xen/migration/tests.py +98 -1
113 lines not on this page · raw message
base-commit: a7fd7d4cbd5e793d31d61c25e08526b330edd7f8
Signature · 1 line
2.43.0
Re: [PATCH v1 1/6] tools/migration: introduce PAGE_DATA_LZ4 stream record type
Le 20/07/2026 à 17:51, Marcus Granado a écrit :
Quoted 18 lines
Allocate a new migration stream record type
REC_TYPE_PAGE_DATA_LZ4(0x00000013) to transmitPAGE_DATAwhen payload is LZ4-compressed. The record use the samexc_sr_rec_page_data_headerasREC_TYPE_PAGE_DATAbut the page data section type changes frompage_datatodata_lz4.Signed-off-by Marcus Granado <[email protected]>--- docs/specs/libxc-migration-stream.pandoc | 71 ++++++++++++++++- tools/libs/guest/xg_sr_common.c | 4 + tools/libs/guest/xg_sr_stream_format.h | 13 ++++ tools/python/xen/migration/libxc.py | 90 ++++++++++++++++++++- tools/python/xen/migration/tests.py | 99 +++++++++++++++++++++++- 5 files changed, 271 insertions(+), 6 deletions(-)docs/specs/libxc-migration-stream.pandoc +0
-0
...
docs/specs/libxc-migration-stream.pandoc tail. · line 370
Quoted 8 lines
docs/specs/libxc-migration-stream.pandoc +8
-0+PAGE_DATA_LZ4 +------------- + +A PAGE_DATA_LZ4 record carries exactly the same information as a +PAGE_DATA record, but with the page contents LZ4-compressed. The saver +may emit it in place of a PAGE_DATA record when LZ4 compression has been +requested. +
Would it be preferable to make this structure more generic, i.e PAGE_DATA_COMPRESSED, as I'm not sure it's wise to restrict to only LZ4 (someone may want to add support for e.g zstd or another algorithm without having to change the format) ?
Quoted 42 lines
docs/specs/libxc-migration-stream.pandoc +42
-0+ 0 1 2 3 4 5 6 7 octet + +-----------------------+-------------------------+ + | count (C) | (reserved) | + +-----------------------+-------------------------+ + | pfn[0] | + +-------------------------------------------------+ + ... + +-------------------------------------------------+ + | pfn[C-1] | + +-----------+-------------------------------------+ + | clen[0] | page_data_lz4[0]... | + +-----------+-------------------------------------+ + ... + +-----------+-------------------------------------+ + | clen[N-1] | page_data_lz4[N-1]... | + ... + +-------------------------------------------------+ + +-------------------------------------------------------------------- +Field Description +----------- -------------------------------------------------------- +count Number of pages described in this record. + +pfn An array of count PFNs and their types, with the same + layout and page types as in a PAGE_DATA record. + +data_lz4 The compressed page contents, as one sub-block per page + set as present in the pfn array, in pfn-array order (i.e. + N sub-blocks, with N as in a PAGE_DATA record: N <= C). + Each sub-block is a `uint16` little-endian length `clen` + followed by either an LZ4 block or a raw page. When + `clen > 0`, page_data_lz4 is `clen` octets of a raw LZ4 + block (per the LZ4 block format) whose decompressed output + is one page_size page. When `clen == 0`, page_data_lz4 is + page_size octets of a raw, uncompressed page. + A compressed `clen` is at most page_size - 1, occupying + only the low 12 bits; the top 4 bits are reserved for + future use and must be 0. +-------------------------------------------------------------------- + +The `count` (C) and `pfn` fields are identical in meaning and +constraints to those of a PAGE_DATA record, and N (the number of
docs/specs/libxc-migration-stream.pandoc tail. · line 422
Quoted 10 lines
docs/specs/libxc-migration-stream.pandoc +10
-0+present pages, N <= C) is as defined there. Unlike PAGE_DATA, a +PAGE_DATA_LZ4 record always has N >= 1 (at least one sub-block): +the saver emits the LZ4 variant only when there is page data to +compress, and a restoring side rejects a PAGE_DATA_LZ4 record with +N == 0. When a batch has no present pages (all pfns of invalid +types) the saver emits a plain PAGE_DATA record instead. + +PAGE_DATA_LZ4 is a _mandatory_ record: a restoring side that does not +support it must fail the migration. +
The inline clen at the beginning of page_data_lz4 feels a bit odd to me, or at least, a bit inconsistent with how pfns are arranged. I think something like i.e
1. count and (reserved)
2. pfn[0] ... pfn[C-1]
3. clen[0] ... clen[N-1]
4. page_data_lz4[0] ... page_data_lz4[N-1]
would be better.
Though, aside that, I'm not sure having separate compressed block for each page is a good idea. Compression is more efficient when processing larger blocks, LZ4 documents a 64 KB deduplication window (of the past bytes) [1].
In my opinion, it may be preferable to have one large block with all the page datas (covering up to MAX_BATCH_SIZE=1024 pages) rather than having to uncompress individual blocks. Which in the end would remove the need to have individual clen[n].
What do you think ?
[1] https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md#compression-techniques
...
Quoted 2 lines
base-commit:
a7fd7d4cbd5e793d31d61c25e08526b330edd7f8
Teddy
Re: [PATCH v1 1/6] tools/migration: introduce PAGE_DATA_LZ4 stream record type
Quoted 16 lines · Teddy Astie
Quoted 14 lines · Marcus Granado
Allocate a new migration stream record type
REC_TYPE_PAGE_DATA_LZ4(0x00000013) to transmitPAGE_DATAwhen payload is LZ4-compressed. The record use the samexc_sr_rec_page_data_headerasREC_TYPE_PAGE_DATAbut the page data section type changes frompage_datatodata_lz4.Signed-off-by Marcus Granado <[email protected]>--- docs/specs/libxc-migration-stream.pandoc | 71 ++++++++++++++++- tools/libs/guest/xg_sr_common.c | 4 + tools/libs/guest/xg_sr_stream_format.h | 13 ++++ tools/python/xen/migration/libxc.py | 90 ++++++++++++++++++++- tools/python/xen/migration/tests.py | 99 +++++++++++++++++++++++- 5 files changed, 271 insertions(+), 6 deletions(-)
Quoted 6 lines
Quoted 4 lines
docs/specs/libxc-migration-stream.pandoc +0
-0...
Don't we need to bump the version number while we add a new mandatory record? Is there no kind of dialog about the supported version?
Quoted 15 lines
docs/specs/libxc-migration-stream.pandoc tail. · line 370
Quoted 9 lines
docs/specs/libxc-migration-stream.pandoc +8
-0+PAGE_DATA_LZ4 +------------- + +A PAGE_DATA_LZ4 record carries exactly the same information as a +PAGE_DATA record, but with the page contents LZ4-compressed. The saver +may emit it in place of a PAGE_DATA record when LZ4 compression has been +requested. +Would it be preferable to make this structure more generic, i.e
PAGE_DATA_COMPRESSED, as I'm not sure it's wise to restrict to only LZ4 (someone may want to add support for e.g zstd or another algorithm without having to change the format) ?
Why not extending the generalization compressing all payload of uncompressed packets (type+body), so to have something like
- LZ4 compressed type (uint32)
- compressed length (uint32)
- compressed data (array of bytes, length field above) Uncompressed data will contain
- type (uint32, like PAGE_DATA)
- body (array of bytes, not padded)
- padding to 8 bytes with zeroes
So we could compress any packet, not only PAGE_DATA. Yes, to support more compression types an additional "compression_type" or similar field could help. But what about additional possible options?
Quoted 69 lines
Quoted 42 lines
docs/specs/libxc-migration-stream.pandoc +42
-0+ 0 1 2 3 4 5 6 7 octet + +-----------------------+-------------------------+ + | count (C) | (reserved) | + +-----------------------+-------------------------+ + | pfn[0] | + +-------------------------------------------------+ + ... + +-------------------------------------------------+ + | pfn[C-1] | + +-----------+-------------------------------------+ + | clen[0] | page_data_lz4[0]... | + +-----------+-------------------------------------+ + ... + +-----------+-------------------------------------+ + | clen[N-1] | page_data_lz4[N-1]... | + ... + +-------------------------------------------------+ + +-------------------------------------------------------------------- +Field Description +----------- -------------------------------------------------------- +count Number of pages described in this record. + +pfn An array of count PFNs and their types, with the same + layout and page types as in a PAGE_DATA record. + +data_lz4 The compressed page contents, as one sub-block per page + set as present in the pfn array, in pfn-array order (i.e. + N sub-blocks, with N as in a PAGE_DATA record: N <= C). + Each sub-block is a `uint16` little-endian length `clen` + followed by either an LZ4 block or a raw page. When + `clen > 0`, page_data_lz4 is `clen` octets of a raw LZ4 + block (per the LZ4 block format) whose decompressed output + is one page_size page. When `clen == 0`, page_data_lz4 is + page_size octets of a raw, uncompressed page. + A compressed `clen` is at most page_size - 1, occupying + only the low 12 bits; the top 4 bits are reserved for + future use and must be 0. +-------------------------------------------------------------------- + +The `count` (C) and `pfn` fields are identical in meaning and +constraints to those of a PAGE_DATA record, and N (the number ofdocs/specs/libxc-migration-stream.pandoc tail. · line 422
Quoted 10 lines
docs/specs/libxc-migration-stream.pandoc +10
-0+present pages, N <= C) is as defined there. Unlike PAGE_DATA, a +PAGE_DATA_LZ4 record always has N >= 1 (at least one sub-block): +the saver emits the LZ4 variant only when there is page data to +compress, and a restoring side rejects a PAGE_DATA_LZ4 record with +N == 0. When a batch has no present pages (all pfns of invalid +types) the saver emits a plain PAGE_DATA record instead. + +PAGE_DATA_LZ4 is a _mandatory_ record: a restoring side that does not +support it must fail the migration. +The inline clen at the beginning of
page_data_lz4feels a bit odd to me, or at least, a bit inconsistent with how pfns are arranged. I think something like i.e1. count and (reserved)
2. pfn[0] ... pfn[C-1]
3. clen[0] ... clen[N-1]
4. page_data_lz4[0] ... page_data_lz4[N-1]would be better.
Though, aside that, I'm not sure having separate compressed block for each page is a good idea. Compression is more efficient when processing larger blocks, LZ4 documents a 64 KB deduplication window (of the past bytes) [1].
Quoted 5 lines
In my opinion, it may be preferable to have one large block with all the page datas (covering up to MAX_BATCH_SIZE=1024 pages) rather than having to uncompress individual blocks. Which in the end would remove the need to have individual clen[n].
I was thinking the same. Talking with the author today one reason is that bytes could change while compressing them (because they are shared with the guest which is running) but on following updates (dirty pages map is used) they get updated and fixed (otherwise VM will crash).
However we could copy the memory to avoid this. Considering the algorithm (in this case) has a window of 64 K we could compress and copy in 64 K chunks to reduce memory usage and increase cache (as processor one) usage (if that's an issue).
Quoted 10 lines
What do you think ?
[1]
https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md#compression-techniques
...Quoted 2 lines
base-commit:
a7fd7d4cbd5e793d31d61c25e08526b330edd7f8Teddy
Frediano
Re: [PATCH v1 1/6] tools/migration: introduce PAGE_DATA_LZ4 stream record type
Le 22/07/2026 à 21:46, Frediano Ziglio a écrit :
Quoted 54 lines · Teddy Astie
Quoted 16 lines · Marcus Granado
Quoted 14 lines
Allocate a new migration stream record type
REC_TYPE_PAGE_DATA_LZ4(0x00000013) to transmitPAGE_DATAwhen payload is LZ4-compressed. The record use the samexc_sr_rec_page_data_headerasREC_TYPE_PAGE_DATAbut the page data section type changes frompage_datatodata_lz4.Signed-off-by Marcus Granado <[email protected]>--- docs/specs/libxc-migration-stream.pandoc | 71 ++++++++++++++++- tools/libs/guest/xg_sr_common.c | 4 + tools/libs/guest/xg_sr_stream_format.h | 13 ++++ tools/python/xen/migration/libxc.py | 90 ++++++++++++++++++++- tools/python/xen/migration/tests.py | 99 +++++++++++++++++++++++- 5 files changed, 271 insertions(+), 6 deletions(-)Quoted 6 lines
Quoted 4 lines
docs/specs/libxc-migration-stream.pandoc +0
-0...
Don't we need to bump the version number while we add a new mandatory record? Is there no kind of dialog about the supported version?
Quoted 15 lines
docs/specs/libxc-migration-stream.pandoc tail. · line 370
Quoted 9 lines
docs/specs/libxc-migration-stream.pandoc +8
-0+PAGE_DATA_LZ4 +------------- + +A PAGE_DATA_LZ4 record carries exactly the same information as a +PAGE_DATA record, but with the page contents LZ4-compressed. The saver +may emit it in place of a PAGE_DATA record when LZ4 compression has been +requested. +Would it be preferable to make this structure more generic, i.e
PAGE_DATA_COMPRESSED, as I'm not sure it's wise to restrict to only LZ4 (someone may want to add support for e.g zstd or another algorithm without having to change the format) ?Why not extending the generalization compressing all payload of uncompressed packets (type+body), so to have something like
- LZ4 compressed type (uint32)
- compressed length (uint32)
- compressed data (array of bytes, length field above) Uncompressed data will contain
- type (uint32, likePAGE_DATA)
- body (array of bytes, not padded)
- padding to 8 bytes with zeroes
Quoted 5 lines
So we could compress any packet, not only
PAGE_DATA. Yes, to support more compression types an additional "compression_type" or similar field could help. But what about additional possible options?
(you should probably also add uncompressed size, so that decompressors can guard against decompression bomb blocks)
In principle, it's preferable as it decouple the compression aspect to the data itself. But it needs a quite significant refactoring to add a middle layer that "transparently" compress/decompress data to the lower layer that perform live migration logic and parse/generate the page_data object.
Quoted 75 lines
Quoted 69 lines
Quoted 42 lines
docs/specs/libxc-migration-stream.pandoc +42
-0+ 0 1 2 3 4 5 6 7 octet + +-----------------------+-------------------------+ + | count (C) | (reserved) | + +-----------------------+-------------------------+ + | pfn[0] | + +-------------------------------------------------+ + ... + +-------------------------------------------------+ + | pfn[C-1] | + +-----------+-------------------------------------+ + | clen[0] | page_data_lz4[0]... | + +-----------+-------------------------------------+ + ... + +-----------+-------------------------------------+ + | clen[N-1] | page_data_lz4[N-1]... | + ... + +-------------------------------------------------+ + +-------------------------------------------------------------------- +Field Description +----------- -------------------------------------------------------- +count Number of pages described in this record. + +pfn An array of count PFNs and their types, with the same + layout and page types as in a PAGE_DATA record. + +data_lz4 The compressed page contents, as one sub-block per page + set as present in the pfn array, in pfn-array order (i.e. + N sub-blocks, with N as in a PAGE_DATA record: N <= C). + Each sub-block is a `uint16` little-endian length `clen` + followed by either an LZ4 block or a raw page. When + `clen > 0`, page_data_lz4 is `clen` octets of a raw LZ4 + block (per the LZ4 block format) whose decompressed output + is one page_size page. When `clen == 0`, page_data_lz4 is + page_size octets of a raw, uncompressed page. + A compressed `clen` is at most page_size - 1, occupying + only the low 12 bits; the top 4 bits are reserved for + future use and must be 0. +-------------------------------------------------------------------- + +The `count` (C) and `pfn` fields are identical in meaning and +constraints to those of a PAGE_DATA record, and N (the number ofdocs/specs/libxc-migration-stream.pandoc tail. · line 422
Quoted 10 lines
docs/specs/libxc-migration-stream.pandoc +10
-0+present pages, N <= C) is as defined there. Unlike PAGE_DATA, a +PAGE_DATA_LZ4 record always has N >= 1 (at least one sub-block): +the saver emits the LZ4 variant only when there is page data to +compress, and a restoring side rejects a PAGE_DATA_LZ4 record with +N == 0. When a batch has no present pages (all pfns of invalid +types) the saver emits a plain PAGE_DATA record instead. + +PAGE_DATA_LZ4 is a _mandatory_ record: a restoring side that does not +support it must fail the migration. +The inline clen at the beginning of
page_data_lz4feels a bit odd to me, or at least, a bit inconsistent with how pfns are arranged. I think something like i.e1. count and (reserved)
2. pfn[0] ... pfn[C-1]
3. clen[0] ... clen[N-1]
4. page_data_lz4[0] ... page_data_lz4[N-1]would be better.
Though, aside that, I'm not sure having separate compressed block for each page is a good idea. Compression is more efficient when processing larger blocks, LZ4 documents a 64 KB deduplication window (of the past bytes) [1].
Quoted 5 lines
In my opinion, it may be preferable to have one large block with all the page datas (covering up to MAX_BATCH_SIZE=1024 pages) rather than having to uncompress individual blocks. Which in the end would remove the need to have individual clen[n].
Quoted 5 lines
I was thinking the same. Talking with the author today one reason is that bytes could change while compressing them (because they are shared with the guest which is running) but on following updates (dirty pages map is used) they get updated and fixed (otherwise VM will crash).
Oh, well, that's (unfortunately) actually worse; while running compression logic to live pages can cause data corruption of the end result, this also causes undefined behavior through the compiler (data race as compiler doesn't expect the pointer content to be modified from elsewhere concurrently).
We probably need to copy the memory here to stay safe. Something that could be addressed with xg_foreignmemory_copy_* proposal.
Quoted 5 lines
However we could copy the memory to avoid this. Considering the algorithm (in this case) has a window of 64 K we could compress and copy in 64 K chunks to reduce memory usage and increase cache (as processor one) usage (if that's an issue).
That could be considered, but it would make things a bit more complicated to define. I found that along LZ4, there is also LZ4F [1] which can be useful to make it streamable or chunkable in a quite standardized way. Otherwise, we would have to add some arbitrary framing each 32 KB blocks, so it will no longer be simply a LZ4 block.
But that may be too much for just chunking a 4 MB (maximum) block so I'm not really certain of what's best actually.
[1] https://github.com/lz4/lz4/blob/dev/doc/lz4_Frame_format.md
Quoted 7 lines
Quoted 7 lines
What do you think ?
[1]
https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md#compression-techniques
...Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
base-commit:
a7fd7d4cbd5e793d31d61c25e08526b330edd7f8Teddy
Frediano
Teddy
Re: [PATCH v1 1/6] tools/migration: introduce PAGE_DATA_LZ4 stream record type
Quoted 56 lines · Teddy Astie
Quoted 54 lines · Frediano Ziglio
Quoted 16 lines · Teddy Astie
Quoted 14 lines · Marcus Granado
Allocate a new migration stream record type
REC_TYPE_PAGE_DATA_LZ4(0x00000013) to transmitPAGE_DATAwhen payload is LZ4-compressed. The record use the samexc_sr_rec_page_data_headerasREC_TYPE_PAGE_DATAbut the page data section type changes frompage_datatodata_lz4.Signed-off-by Marcus Granado <[email protected]>--- docs/specs/libxc-migration-stream.pandoc | 71 ++++++++++++++++- tools/libs/guest/xg_sr_common.c | 4 + tools/libs/guest/xg_sr_stream_format.h | 13 ++++ tools/python/xen/migration/libxc.py | 90 ++++++++++++++++++++- tools/python/xen/migration/tests.py | 99 +++++++++++++++++++++++- 5 files changed, 271 insertions(+), 6 deletions(-)Quoted 6 lines
Quoted 4 lines
docs/specs/libxc-migration-stream.pandoc +0
-0...
Don't we need to bump the version number while we add a new mandatory record? Is there no kind of dialog about the supported version?
Quoted 15 lines
docs/specs/libxc-migration-stream.pandoc tail. · line 370
Quoted 9 lines
docs/specs/libxc-migration-stream.pandoc +8
-0+PAGE_DATA_LZ4 +------------- + +A PAGE_DATA_LZ4 record carries exactly the same information as a +PAGE_DATA record, but with the page contents LZ4-compressed. The saver +may emit it in place of a PAGE_DATA record when LZ4 compression has been +requested. +Would it be preferable to make this structure more generic, i.e
PAGE_DATA_COMPRESSED, as I'm not sure it's wise to restrict to only LZ4 (someone may want to add support for e.g zstd or another algorithm without having to change the format) ?Why not extending the generalization compressing all payload of uncompressed packets (type+body), so to have something like
- LZ4 compressed type (uint32)
- compressed length (uint32)
- compressed data (array of bytes, length field above) Uncompressed data will contain
- type (uint32, likePAGE_DATA)
- body (array of bytes, not padded)
- padding to 8 bytes with zeroes
Quoted 9 lines
Quoted 5 lines
So we could compress any packet, not only
PAGE_DATA. Yes, to support more compression types an additional "compression_type" or similar field could help. But what about additional possible options?(you should probably also add uncompressed size, so that decompressors can guard against decompression bomb blocks)
Yes, apparently that field disappeared between thinking and writing the reply. It's also useful for different purposes like preallocating.
Quoted 6 lines
In principle, it's preferable as it decouple the compression aspect to the data itself. But it needs a quite significant refactoring to add a middle layer that "transparently" compress/decompress data to the lower layer that perform live migration logic and parse/generate the
page_dataobject.
A similar change was done (not by me) in another project I worked on previously. I thought it was more complicated too but it turned out the change was pretty clean, not requiring much.
More or less during the process of the package you decompress the package and call the process again on the decompressed package. Similar for the sending side.
Quoted 101 lines
Quoted 75 lines
Quoted 69 lines
Quoted 42 lines
docs/specs/libxc-migration-stream.pandoc +42
-0+ 0 1 2 3 4 5 6 7 octet + +-----------------------+-------------------------+ + | count (C) | (reserved) | + +-----------------------+-------------------------+ + | pfn[0] | + +-------------------------------------------------+ + ... + +-------------------------------------------------+ + | pfn[C-1] | + +-----------+-------------------------------------+ + | clen[0] | page_data_lz4[0]... | + +-----------+-------------------------------------+ + ... + +-----------+-------------------------------------+ + | clen[N-1] | page_data_lz4[N-1]... | + ... + +-------------------------------------------------+ + +-------------------------------------------------------------------- +Field Description +----------- -------------------------------------------------------- +count Number of pages described in this record. + +pfn An array of count PFNs and their types, with the same + layout and page types as in a PAGE_DATA record. + +data_lz4 The compressed page contents, as one sub-block per page + set as present in the pfn array, in pfn-array order (i.e. + N sub-blocks, with N as in a PAGE_DATA record: N <= C). + Each sub-block is a `uint16` little-endian length `clen` + followed by either an LZ4 block or a raw page. When + `clen > 0`, page_data_lz4 is `clen` octets of a raw LZ4 + block (per the LZ4 block format) whose decompressed output + is one page_size page. When `clen == 0`, page_data_lz4 is + page_size octets of a raw, uncompressed page. + A compressed `clen` is at most page_size - 1, occupying + only the low 12 bits; the top 4 bits are reserved for + future use and must be 0. +-------------------------------------------------------------------- + +The `count` (C) and `pfn` fields are identical in meaning and +constraints to those of a PAGE_DATA record, and N (the number ofdocs/specs/libxc-migration-stream.pandoc tail. · line 422
Quoted 10 lines
docs/specs/libxc-migration-stream.pandoc +10
-0+present pages, N <= C) is as defined there. Unlike PAGE_DATA, a +PAGE_DATA_LZ4 record always has N >= 1 (at least one sub-block): +the saver emits the LZ4 variant only when there is page data to +compress, and a restoring side rejects a PAGE_DATA_LZ4 record with +N == 0. When a batch has no present pages (all pfns of invalid +types) the saver emits a plain PAGE_DATA record instead. + +PAGE_DATA_LZ4 is a _mandatory_ record: a restoring side that does not +support it must fail the migration. +The inline clen at the beginning of
page_data_lz4feels a bit odd to me, or at least, a bit inconsistent with how pfns are arranged. I think something like i.e1. count and (reserved)
2. pfn[0] ... pfn[C-1]
3. clen[0] ... clen[N-1]
4. page_data_lz4[0] ... page_data_lz4[N-1]would be better.
Though, aside that, I'm not sure having separate compressed block for each page is a good idea. Compression is more efficient when processing larger blocks, LZ4 documents a 64 KB deduplication window (of the past bytes) [1].
Quoted 5 lines
In my opinion, it may be preferable to have one large block with all the page datas (covering up to MAX_BATCH_SIZE=1024 pages) rather than having to uncompress individual blocks. Which in the end would remove the need to have individual clen[n].
Quoted 5 lines
I was thinking the same. Talking with the author today one reason is that bytes could change while compressing them (because they are shared with the guest which is running) but on following updates (dirty pages map is used) they get updated and fixed (otherwise VM will crash).
Oh, well, that's (unfortunately) actually worse; while running compression logic to live pages can cause data corruption of the end result, this also causes undefined behavior through the compiler (data race as compiler doesn't expect the pointer content to be modified from elsewhere concurrently).
We probably need to copy the memory here to stay safe. Something that could be addressed with xg_foreignmemory_copy_* proposal.
Quoted 5 lines
However we could copy the memory to avoid this. Considering the algorithm (in this case) has a window of 64 K we could compress and copy in 64 K chunks to reduce memory usage and increase cache (as processor one) usage (if that's an issue).
That could be considered, but it would make things a bit more complicated to define. I found that along LZ4, there is also LZ4F [1] which can be useful to make it streamable or chunkable in a quite standardized way. Otherwise, we would have to add some arbitrary framing each 32 KB blocks, so it will no longer be simply a LZ4 block.
Quoted 5 lines
But that may be too much for just chunking a 4 MB (maximum) block so I'm not really certain of what's best actually.
[1] https://github.com/lz4/lz4/blob/dev/doc/lz4_Frame_format.md
About compressing all together and considering also the issue of memory changing while sending I would vote to copy the memory in a temporary buffer to avoid this. One advantage is that it simplified the format. The current LZ4 implementation seems to cope with data changes but nothing guarantees it in the future, the buffer you are passing is not supposed to change while you compress it.
Quoted 15 lines
Quoted 7 lines
Quoted 7 lines
What do you think ?
[1]
https://github.com/lz4/lz4/blob/dev/doc/lz4_Block_format.md#compression-techniques
...Quoted 1 line
Quoted 6 lines
Quoted 3 lines
Quoted 1 line
base-commit:
a7fd7d4cbd5e793d31d61c25e08526b330edd7f8Teddy
Frediano
Teddy
Re: [PATCH v1 1/6] tools/migration: introduce PAGE_DATA_LZ4 stream record type
Quoted 6 lines · Frediano Ziglio
About compressing all together and considering also the issue of memory changing while sending I would vote to copy the memory in a temporary buffer to avoid this. One advantage is that it simplified the format. The current LZ4 implementation seems to cope with data changes but nothing guarantees it in the future, the buffer you are passing is not supposed to change while you compress it.
Agreed. Frediano and I discussed this further, including simplifications on the compression record for v2 so that the page data compression:
* compresses the whole batch together
* and uses a staged copy for it as you and Teddy suggested (so that we do not need to worry about mutating buffers affecting LZ4 or other algorithms).
Once there's exactly one compressed blob per record, there's no need for extra fields to describe the compressed data, as the uncompressed batch size is known (and bounded by MAX_BATCH_SIZE * page_size), and its compressed size can be inferred from the size of the emitted compressed record without a need for an explicit field for clen or a field with the number of raw compressed blobs. This means the separate PAGE_DATA_LZ4 record type is not needed, and the proposal for v2 compression record could simplify to reusing PAGE_DATA and spending one octet of its reserved word as follows:
0 1 2 3 4 5 6 7 octet
+-----------------------+-----+-------------------+
| count (C) | comp| (reserved) |
+-----------------------+-----+-------------------+
| pfn[0] |
+-------------------------------------------------+
...
+-------------------------------------------------+
| pfn[C-1] |
+-------------------------------------------------+
| page_data[0..N-1] if comp == 0 |
| or page_cdata if comp != 0 |
+-------------------------------------------------+with two new entries in the field table:
comp Compression algorithm applied to the page contents. 0
means none, and the record is exactly as it is today. 1 means LZ4 block format. Other values are reserved for other future formats like ZSTD etc. A comp != 0 can only be emitted if 0 < len(page_cdata) < N * page_size. An unknown comp must cause the receiver to fail with a "Compression algorithm <value> not handled" error.
page_cdata Present instead of page_data when comp is non-zero. A
single compressed object holding the concatenation of the N page_data entries. The receiver must verify that 0 < count <= MAX_BATCH_SIZE.
Teddy, I hope this format covers your points: it's no longer LZ4 specific, the inline clen inconsistency in libxenguest record disappears, it's one block over an immutable copy of the batch instead of one per page, the decompressed size is known up front, and the allocation derived from count is bounded on the receiver.
This simplification is also forward-compatible in two different ways: your 64KB chunking for cache locality doesn't depend on the format, as the staging copy can be done in chunks while still making a single compress call over the whole batch. And if we find benefits in splitting the payload into several compressed units, that can be implemented as a new comp value using a self-delimiting format like zstd or lz4f frames, which report the consumed bytes without a need to specify clen or extra framing fields at the record level.
Quoted 1 line · Frediano Ziglio
Don't we need to bump the version number while we add a new mandatory record?
I believe we may avoid having to do a version bump if we adopt the property that 0 < len(page_cdata) < N * page_size when comp != 0, as in this case the compressed data sent to an old receiver would fail in handle_page_data() with "PAGE_DATA record wrong size". Bumping the version would make uncompressed migrations fail if they are sent to old receivers that also understand uncompressed migration, so avoiding if possible would be good. The spec says migration tools "shall always save images using version V", so it doesn't seem like we could bump the version only when compression is on.
Quoted 1 line
Is there no kind of dialog about the supported version?
There is no in-stream negotiation, and this proposal does not add one. Compression is opt-in at the sender via xl migrate --compress, so an operator who enables it against an old receiver gets a clean failure rather than corruption.
Quoted 2 lines
Why not extending the generalization compressing all payload of uncompressed packets (type+body),
A composable wrapper would be a clean generic mechanism, but I think there are two reasons not to go that way in v2: PAGE_DATA is effectively all of the stream bytes, so compressing the other record types would not show up in a measurement. And the no-extra-fields property above depends on PAGE_DATA
specifically: a generic wrapper has no pfn array from where we can derive the uncompressed size, so it would need explicit algorithm, compressed size and uncompressed size fields on every record, which re-adds the fields that this proposal tries to avoid.
Please let me know if the ideas above capture what you had in mind in terms of suggestions to improve v1.
In particular, I wonder what the maintainers think of the use of one of the octets of the PAGE_DATA reserved field for the purpose of indicating the data is compressed, or is it preferable to use a new PAGE_DATA_COMPRESSED (0x13) type record as a mandatory record so that an old receiver fails more cleanly when it doesn't understand compression "Mandatory record <name> not handled" (caused by the specification of mandatory records) instead of with a generic error "PAGE_DATA record wrong size" (caused by the current safety implementation that rejects unexpected record sizes)?
Marcus
Re: [PATCH v1 1/6] tools/migration: introduce PAGE_DATA_LZ4 stream record type
Le 03/08/2026 à 19:23, Marcus Granado a écrit :
Quoted 56 lines · Frediano Ziglio
Quoted 6 lines
About compressing all together and considering also the issue of memory changing while sending I would vote to copy the memory in a temporary buffer to avoid this. One advantage is that it simplified the format. The current LZ4 implementation seems to cope with data changes but nothing guarantees it in the future, the buffer you are passing is not supposed to change while you compress it.
Agreed. Frediano and I discussed this further, including simplifications on the compression record for v2 so that the page data compression:
* compresses the whole batch together
* and uses a staged copy for it as you and Teddy suggested (so that we do not need to worry about mutating buffers affecting LZ4 or other algorithms).Once there's exactly one compressed blob per record, there's no need for extra fields to describe the compressed data, as the uncompressed batch size is known (and bounded by
MAX_BATCH_SIZE*page_size), and its compressed size can be inferred from the size of the emitted compressed record without a need for an explicit field for clen or a field with the number of raw compressed blobs. This means the separatePAGE_DATA_LZ4record type is not needed, and the proposal for v2 compression record could simplify to reusingPAGE_DATAand spending one octet of its reserved word as follows:0 1 2 3 4 5 6 7 octet +-----------------------+-----+-------------------+ | count (C) | comp| (reserved) | +-----------------------+-----+-------------------+ | pfn[0] | +-------------------------------------------------+ ... +-------------------------------------------------+ | pfn[C-1] | +-------------------------------------------------+ | page_data[0..N-1] if comp == 0 | | or page_cdata if comp != 0 | +-------------------------------------------------+with two new entries in the field table:
comp Compression algorithm applied to the page contents. 0
means none, and the record is exactly as it is today. 1 means LZ4 block format. Other values are reserved for other future formats like ZSTD etc. A comp != 0 can only be emitted if 0 <len(page_cdata)< N *page_size. An unknown comp must cause the receiver to fail with a "Compression algorithm <value> not handled" error.
page_cdataPresent instead ofpage_datawhen comp is non-zero. A
single compressed object holding the concatenation of the Npage_dataentries. The receiver must verify that 0 < count <=MAX_BATCH_SIZE.
Quoted 6 lines
Teddy, I hope this format covers your points: it's no longer LZ4 specific, the inline clen inconsistency in libxenguest record disappears, it's one block over an immutable copy of the batch instead of one per page, the decompressed size is known up front, and the allocation derived from count is bounded on the receiver.
Looks good to me.
Quoted 47 lines
This simplification is also forward-compatible in two different ways: your 64KB chunking for cache locality doesn't depend on the format, as the staging copy can be done in chunks while still making a single compress call over the whole batch. And if we find benefits in splitting the payload into several compressed units, that can be implemented as a new comp value using a self-delimiting format like zstd or lz4f frames, which report the consumed bytes without a need to specify clen or extra framing fields at the record level.
Quoted 1 line · Frediano Ziglio
Don't we need to bump the version number while we add a new mandatory record?
I believe we may avoid having to do a version bump if we adopt the property that 0 <
len(page_cdata)< N *page_sizewhen comp != 0, as in this case the compressed data sent to an old receiver would fail inhandle_page_data()with "PAGE_DATArecord wrong size". Bumping the version would make uncompressed migrations fail if they are sent to old receivers that also understand uncompressed migration, so avoiding if possible would be good. The spec says migration tools "shall always save images using version V", so it doesn't seem like we could bump the version only when compression is on.Quoted 1 line
Is there no kind of dialog about the supported version?
There is no in-stream negotiation, and this proposal does not add one. Compression is opt-in at the sender via xl migrate --compress, so an operator who enables it against an old receiver gets a clean failure rather than corruption.
Quoted 2 lines
Why not extending the generalization compressing all payload of uncompressed packets (type+body),
A composable wrapper would be a clean generic mechanism, but I think there are two reasons not to go that way in v2:
PAGE_DATAis effectively all of the stream bytes, so compressing the other record types would not show up in a measurement. And the no-extra-fields property above depends onPAGE_DATA
specifically: a generic wrapper has no pfn array from where we can derive the uncompressed size, so it would need explicit algorithm, compressed size and uncompressed size fields on every record, which re-adds the fields that this proposal tries to avoid.Please let me know if the ideas above capture what you had in mind in terms of suggestions to improve v1.
Quoted 9 lines
In particular, I wonder what the maintainers think of the use of one of the octets of the
PAGE_DATAreserved field for the purpose of indicating the data is compressed, or is it preferable to use a newPAGE_DATA_COMPRESSED(0x13) type record as a mandatory record so that an old receiver fails more cleanly when it doesn't understand compression "Mandatory record <name> not handled" (caused by the specification of mandatory records) instead of with a generic error "PAGE_DATArecord wrong size" (caused by the current safety implementation that rejects unexpected record sizes)?
The specification says
Quoted 1 line
Padding and reserved fields are set to zero on save and must be
ignored during restore.
Which is not really going to help if we add a new field that matters on how the content is organized.
To avoid confusion, it may be desirable to add a new type (PAGE_DATA_COMPRESSED), but I'm not fully sold on it.
Quoted 4 lines
Marcus
Teddy
Re: [PATCH v1 1/6] tools/migration: introduce PAGE_DATA_LZ4 stream record type
Quoted 124 lines · Teddy Astie
Quoted 56 lines · Marcus Granado
Quoted 6 lines · Frediano Ziglio
About compressing all together and considering also the issue of memory changing while sending I would vote to copy the memory in a temporary buffer to avoid this. One advantage is that it simplified the format. The current LZ4 implementation seems to cope with data changes but nothing guarantees it in the future, the buffer you are passing is not supposed to change while you compress it.
Agreed. Frediano and I discussed this further, including simplifications on the compression record for v2 so that the page data compression:
* compresses the whole batch together
* and uses a staged copy for it as you and Teddy suggested (so that we do not need to worry about mutating buffers affecting LZ4 or other algorithms).Once there's exactly one compressed blob per record, there's no need for extra fields to describe the compressed data, as the uncompressed batch size is known (and bounded by
MAX_BATCH_SIZE*page_size), and its compressed size can be inferred from the size of the emitted compressed record without a need for an explicit field for clen or a field with the number of raw compressed blobs. This means the separatePAGE_DATA_LZ4record type is not needed, and the proposal for v2 compression record could simplify to reusingPAGE_DATAand spending one octet of its reserved word as follows:0 1 2 3 4 5 6 7 octet +-----------------------+-----+-------------------+ | count (C) | comp| (reserved) | +-----------------------+-----+-------------------+ | pfn[0] | +-------------------------------------------------+ ... +-------------------------------------------------+ | pfn[C-1] | +-------------------------------------------------+ | page_data[0..N-1] if comp == 0 | | or page_cdata if comp != 0 | +-------------------------------------------------+with two new entries in the field table:
comp Compression algorithm applied to the page contents. 0
means none, and the record is exactly as it is today. 1 means LZ4 block format. Other values are reserved for other future formats like ZSTD etc. A comp != 0 can only be emitted if 0 <len(page_cdata)< N *page_size. An unknown comp must cause the receiver to fail with a "Compression algorithm <value> not handled" error.
page_cdataPresent instead ofpage_datawhen comp is non-zero. A
single compressed object holding the concatenation of the Npage_dataentries. The receiver must verify that 0 < count <=MAX_BATCH_SIZE.Quoted 6 lines
Teddy, I hope this format covers your points: it's no longer LZ4 specific, the inline clen inconsistency in libxenguest record disappears, it's one block over an immutable copy of the batch instead of one per page, the decompressed size is known up front, and the allocation derived from count is bounded on the receiver.
Looks good to me.
Quoted 47 lines
This simplification is also forward-compatible in two different ways: your 64KB chunking for cache locality doesn't depend on the format, as the staging copy can be done in chunks while still making a single compress call over the whole batch. And if we find benefits in splitting the payload into several compressed units, that can be implemented as a new comp value using a self-delimiting format like zstd or lz4f frames, which report the consumed bytes without a need to specify clen or extra framing fields at the record level.
Quoted 1 line · Frediano Ziglio
Don't we need to bump the version number while we add a new mandatory record?
I believe we may avoid having to do a version bump if we adopt the property that 0 <
len(page_cdata)< N *page_sizewhen comp != 0, as in this case the compressed data sent to an old receiver would fail inhandle_page_data()with "PAGE_DATArecord wrong size". Bumping the version would make uncompressed migrations fail if they are sent to old receivers that also understand uncompressed migration, so avoiding if possible would be good. The spec says migration tools "shall always save images using version V", so it doesn't seem like we could bump the version only when compression is on.Quoted 1 line
Is there no kind of dialog about the supported version?
There is no in-stream negotiation, and this proposal does not add one. Compression is opt-in at the sender via xl migrate --compress, so an operator who enables it against an old receiver gets a clean failure rather than corruption.
Quoted 2 lines
Why not extending the generalization compressing all payload of uncompressed packets (type+body),
A composable wrapper would be a clean generic mechanism, but I think there are two reasons not to go that way in v2:
PAGE_DATAis effectively all of the stream bytes, so compressing the other record types would not show up in a measurement. And the no-extra-fields property above depends onPAGE_DATA
specifically: a generic wrapper has no pfn array from where we can derive the uncompressed size, so it would need explicit algorithm, compressed size and uncompressed size fields on every record, which re-adds the fields that this proposal tries to avoid.Please let me know if the ideas above capture what you had in mind in terms of suggestions to improve v1.
Quoted 9 lines
In particular, I wonder what the maintainers think of the use of one of the octets of the
PAGE_DATAreserved field for the purpose of indicating the data is compressed, or is it preferable to use a newPAGE_DATA_COMPRESSED(0x13) type record as a mandatory record so that an old receiver fails more cleanly when it doesn't understand compression "Mandatory record <name> not handled" (caused by the specification of mandatory records) instead of with a generic error "PAGE_DATArecord wrong size" (caused by the current safety implementation that rejects unexpected record sizes)?
Quoted 8 lines
The specification says
Quoted 1 line
Padding and reserved fields are set to zero on save and must be
ignored during restore.
Which is not really going to help if we add a new field that matters on how the content is organized.
Yes, there's no check for "_res1" being 0, however there's this check:
if ( rec->length != (sizeof(*pages) +
(sizeof(uint64_t) * pages->count) +
(PAGE_SIZE * pages_of_data)) )
{
ERROR("PAGE_DATA record wrong size: length %u, expected "
"%zu + %zu + %lu", rec->length, sizeof(*pages),
(sizeof(uint64_t) * pages->count), (PAGE_SIZE * pages_of_data));
goto err;
}so, as long as we require that the compressed data is less than the uncompressed one (we should) it's fine.
Quoted 3 lines
To avoid confusion, it may be desirable to add a new type (
PAGE_DATA_COMPRESSED), but I'm not fully sold on it.
Not strong but if I could vote I would just use part of "_res1" as proposed.
Quoted 6 lines
Quoted 4 lines
Marcus
Teddy
Frediano
Re: [PATCH v1 1/6] tools/migration: introduce PAGE_DATA_LZ4 stream record type
Quoted 50 lines · Frediano Ziglio
Quoted 49 lines · Teddy Astie
Quoted 34 lines · Marcus Granado
simplify to reusing
PAGE_DATAand spending one octet of its reserved word as follows:0 1 2 3 4 5 6 7 octet +-----------------------+-----+-------------------+ | count (C) | comp| (reserved) | +-----------------------+-----+-------------------+ | pfn[0] | +-------------------------------------------------+ ... +-------------------------------------------------+ | pfn[C-1] | +-------------------------------------------------+ | page_data[0..N-1] if comp == 0 | | or page_cdata if comp != 0 | +-------------------------------------------------+with two new entries in the field table:
comp Compression algorithm applied to the page contents. 0
means none, and the record is exactly as it is today. 1 means LZ4 block format. Other values are reserved for other future formats like ZSTD etc. A comp != 0 can only be emitted if 0 <len(page_cdata)< N *page_size. An unknown comp must cause the receiver to fail with a "Compression algorithm <value> not handled" error.
page_cdataPresent instead ofpage_datawhen comp is non-zero. A
single compressed object holding the concatenation of the Npage_dataentries. The receiver must verify that 0 < count <=MAX_BATCH_SIZE.Quoted 6 lines
Teddy, I hope this format covers your points: it's no longer LZ4 specific, the inline clen inconsistency in libxenguest record disappears, it's one block over an immutable copy of the batch instead of one per page, the decompressed size is known up front, and the allocation derived from count is bounded on the receiver.
Looks good to me.
Quoted 4 lines · Frediano Ziglio
Quoted 2 lines
Don't we need to bump the version number while we add a new mandatory record?
Quoted 9 lines
Quoted 9 lines
Quoted 9 lines
I believe we may avoid having to do a version bump if we adopt the property that 0 <
len(page_cdata)< N *page_sizewhen comp != 0, as in this case the compressed data sent to an old receiver would fail inhandle_page_data()with "PAGE_DATArecord wrong size". Bumping the version would make uncompressed migrations fail if they are sent to old receivers that also understand uncompressed migration, so avoiding if possible would be good. The spec says migration tools "shall always save images using version V", so it doesn't seem like we could bump the version only when compression is on.
Thanks. I will take the discussion so far as the record format above being agreed, so the only open point is how it is carried.
Quoted 7 lines
Quoted 7 lines
The specification says
Quoted 1 line
Padding and reserved fields are set to zero on save and must be
ignored during restore.
Which is not really going to help if we add a new field that matters on how the content is organized.
Agreed that the sentence as it stands does not cover it, but I think that is a documentation problem rather than a format problem: once the octet is given a meaning by the specification it is no longer a reserved field, so the rule stops applying to it. The v2 spec patch would define comp in the PAGE_DATA field table alongside count and pfn, and leave the remaining three octets reserved and required to be zero.
To make the compatibility argument rest on the specification rather than on one implementation, I would also add these rules to it in the same patch:
* a saver may only emit comp != 0 when 0 < len(page_cdata) < N * page_size
* a restoring side must reject a PAGE_DATA record whose length is not exactly sizeof(hdr) + C * 8 + N * page_size when comp == 0
* a restoring side must reject a record with count > MAX_BATCH_SIZE, and must fail on a comp value it does not implement
The second is what handle_page_data() already does today, so writing it down in the specification is easy and turns the PAGE_DATA argument below into a guarantee rather than an observation about the current code.
Quoted 7 lines
Yes, there's no check for "
_res1" being 0, however there's this check:if ( rec->length != (sizeof(*pages) + ...so, as long as we require that the compressed data is less than the uncompressed one (we should) it's fine.
Not strong but if I could vote I would just use part of "
_res1" as proposed.
I would vote the same way. To be explicit about it: an old receiver will reject a compressed record through the length check rather than through the mandatory record rule, so the diagnostic is "PAGE_DATA record wrong size" rather than one naming the unsupported compression. I think that is an acceptable price for not adding a record type, given the strict inequality above makes the rejection guaranteed rather than incidental.
Andrew, Roger, the one open point is whether to carry this in a PAGE_DATA reserved octet or in a separate PAGE_DATA_COMPRESSED mandatory record. Unless a maintainer prefers the latter, I will implement v2 with the comp octet in PAGE_DATA and the three rules above.
Marcus
Re: [PATCH v1 1/6] tools/migration: introduce PAGE_DATA_LZ4 stream record type
In reply to Teddy AstieQuoted 13 lines · Teddy Astie
docs/specs/libxc-migration-stream.pandoc tail. · line 369
Quoted 7 lines · Marcus Granado
docs/specs/libxc-migration-stream.pandoc +7
-0+PAGE_DATA_LZ4 +------------- + +A PAGE_DATA_LZ4 record carries exactly the same information as a +PAGE_DATA record, but with the page contents LZ4-compressed. The saver +may emit it in place of a PAGE_DATA record when LZ4 compression has been +requested.Would it be preferable to make this structure more generic, i.e
PAGE_DATA_COMPRESSED, as I'm not sure it's wise to restrict to only LZ4 (someone may want to add support for e.g zstd or another algorithm without having to change the format) ?
It should be possible to create a more generic structure like PAGE_DATA_COMPRESSED instead of PAGE_DATA_LZ4, which accepts different compression algorithms in extra new fields. I suspect the challenge here is how generic to make it: I wonder if it wouldn't be equivalent and simpler to instead add new record types (PAGE_DATA_ZSTD etc) in the future when necessary, as a new algorithm needs new code on both ends anyway and reuses the same per-page framing, so a generic record would not actually avoid a code change. Perhaps we could adopt a PAGE_DATA_COMPRESSED_* family prefix for the wire format doc, and rename the algorithm type PAGE_DATA_LZ4 -> PAGE_DATA_COMPRESSED_LZ4, so that eg. in the future PAGE_DATA_COMPRESSED_ZSTD etc reuses the same family document and is only a new type id. This would be an interesting question for the maintainers of libxenguest.
Quoted 10 lines
docs/specs/libxc-migration-stream.pandoc tail. · line 405
Quoted 5 lines
docs/specs/libxc-migration-stream.pandoc +5
-0+ Each sub-block is a `uint16` little-endian length `clen` + followed by either an LZ4 block or a raw page. When + `clen > 0`, page_data_lz4 is `clen` octets of a raw LZ4 + block [...] When `clen == 0`, page_data_lz4 is + page_size octets of a raw, uncompressed page.The inline clen at the beginning of
page_data_lz4feels a bit odd to me, or at least, a bit inconsistent with how pfns are arranged. I think something like i.e
Quoted 6 lines
1. count and (reserved)
2. pfn[0] ... pfn[C-1]
3. clen[0] ... clen[N-1]
4. page_data_lz4[0] ... page_data_lz4[N-1]would be better.
Using an array clen[0..N-1] before the page_data_lz4[0..N-1] is neat and as you said follows the existing pfn[0..C-1] pattern, and I think it would be good to adopt it in v2.
Quoted 4 lines
Though, aside that, I'm not sure having separate compressed block for each page is a good idea. Compression is more efficient when processing larger blocks, LZ4 documents a 64 KB deduplication window (of the past bytes) [1].
LZ4 expects the source buffer to be immutable for the duration of the compression call. However, during live migration the guest is still running and updating its pages concurrently with the compression. This mutable source buffer can cause compressed back-references to be inconsistent in the
Quoted 1 line
4KB deduplication window (LZ4's match window reaches 64 KB, ie. up to 16
pages), resulting in corrupt decompressed pages on the destination. Corrupted dirty pages would be resent and healed in the next iteration, but corrupted clean (non-dirty) pages wouldn't. Indeed, I initially experienced with compressing the whole 4MB batch and found the guests would crash on the destination due to corrupted page contents after decompression. Compressing individual 4KB pages avoids this back-reference inconsistency issue across pages. Any inconsistency caused by a concurrent write inside an individually-compressed 4KB page is not a problem, as it's marked dirty anyway and will be resend in the next iteration, so the problem is only present when compressing together >1 page.
I also tried staging: compress from a read-only copy of the whole 4 MB batch to avoid the concurrent guest writes. That is safe, but it adds a second, slower full memory pass per batch, which I suspect is better to avoid to minimise memory bottlenecks in this high-throughput memory transfer operation. Measured on identical buffers, per-page in general costs <1% of the batch on ordinary compressible pages and is no worse on incompressible pages (it stores them raw rather than expanding them); per-batch wins when a page is duplicated within LZ4's 64 KB window, which per-page cannot dedup, and in the measured scenarios the aggregate effect on a real migration is not visible within the run-to-run variations when comparing per-batch compression vs per-page compression, so in v1 I adopted the per-4K-page compression.
Quoted 6 lines
In my opinion, it may be preferable to have one large block with all the page datas (covering up to MAX_BATCH_SIZE=1024 pages) rather than having to uncompress individual blocks. Which in the end would remove the need to have individual clen[n].
What do you think ?
Regarding using one large block (B=1), this would resurface the issue of the cross-page back references problem, as the guest keeps updating the pages while we compress them. Per-page independent blocks (B=N) make it safe to compress directly from the live mapping. Right now we have B=N (the zero-copy non-staged compression in v1), and we could extend it to allow B=1 (the cross-page compression with staging) in the future, something like:
1. count and (reserved)
2. pfn[0] ... pfn[C-1]
3. B
if B==N: zero-copy, non-staged per-page compression units if B==1: staged per-batch compression unit
4. clen[0] ... clen[B-1]
5. cdata[0] ... cdata[B-1]
If we wanted to allow a generic 1<=B<=N, for B arbitrary compression units for arbitrary blocks in each unit, then we'd probably need another array plen[0..B-1] to map a compression unit to expected decompressed cdata[0..B-1] size for the decompressor.
Re: [PATCH v1 1/6] tools/migration: introduce PAGE_DATA_LZ4 stream record type
In reply to Marcus GranadoQuoted 10 lines · Marcus Granado
docs/specs/libxc-migration-stream.pandoc +2
-0@@ -359,6 +360,68 @@ tail. \clearpage +PAGE_DATA_LZ4 +-------------
docs/specs/libxc-migration-stream.pandoc tail. · line 374
Quoted 10 lines
docs/specs/libxc-migration-stream.pandoc +10
-0+ +A PAGE_DATA_LZ4 record carries exactly the same information as a +PAGE_DATA record, but with the page contents LZ4-compressed. The saver +may emit it in place of a PAGE_DATA record when LZ4 compression has been +requested. + + 0 1 2 3 4 5 6 7 octet + +-----------------------+-------------------------+ + | count (C) | (reserved) | + +-----------------------+-------------------------+
This reserved field in the original PAGE_DATA was earmarked for compression information, but that was on the expectation that we'd be compressing the whole record in one go. We're going to need to figure out that part first.
A complication with compressing in a single block is that we end up with disjoint ranges for any PV pagetable, and for holes in HVM guests, although Frediano's foreign-copy work could be adjusted to arrange for the pagedata to be contiguous.
Another question was about the choice of algorithm. Yes we could use any algorithm, but CPU time in dom0 is at a premium and we want to favour speed over compression ratio. LZ4 does this. ZSTD might be acceptable too. The others are unlikely to be a win.
While this is going off topic, it's worth at least mentioning. Another optimisation I had in mind was to remember the last version of a page that we sent, and on subsequent iterations, XOR the current contents with the old contents. I posit that plenty of dirty pages will only have a small amount dirty compared to the previous send, meaning the XOR of the two will be mostly zeroes, and run-length encode very well. The downside is extra memory overhead on the source side, so it's not an automatic win.
~Andrew
PATCH 2/6 tools/libs/guest: decompress PAGE_DATA_LZ4 records on restore
In reply to Marcus GranadoThis patch updates the restore side of the migration stream to accept the newly defined REC_TYPE_PAGE_DATA_LZ4 record. When HAVE_LZ4 is defined, handle_data_lz4() decodes the PAGE_DATA_LZ4 record's data_lz4 payload (containing the per-page [uint16 LE clen][clen octets] entries) into ctx->restore.lz4_decomp_buf and returns it, or NULL on error.
clen is read via lz4_page_get_clen() (little-endian octet assembly instead of a pointer cast as the entries are byte-packed and therefore unaligned). Each entry's length and the running input offset are bounds-checked against the record payload, each page_data_lz4 must decompress to exactly PAGE_SIZE, and the walk must consume the payload exactly, before any byte reaches the guest. The decompressed page_data_lz4 is sent to the existing process_page_data() path of REC_TYPE_PAGE_DATA, so the rest of the restore code stays the same.
The existing process_record() routes both types of page data records to the same handle_page_data(), which is refactored to dispatch on rec->type:
* the original uncompresed REC_TYPE_PAGE_DATA case keeps its original exact-size check
* the new REC_TYPE_PAGE_DATA_LZ4 case expects variable record lengths and decompresses the expected_payload
The decompression buffer grows on demand via realloc() and is freed in cleanup(). Sizing is driven by the uncompressed byte count from the expected_payload of the header, so the buffer naturally converges to the largest batch size used during the stream.
The existing per-object 'xg_dom_bzimageloader.o: CFLAGS += $(ZLIB_CFLAGS)' is broadened to a global 'CFLAGS += $(ZLIB_CFLAGS)' (tools/configure groups liblz4's -DHAVE_LZ4 and cflags into ZLIB_CFLAGS), so HAVE_LZ4 is defined consistently for every libxenguest object that includes xg_sr_common.h.
--- tools/libs/guest/Makefile | 2 +- tools/libs/guest/xg_sr_common.h | 12 +++ tools/libs/guest/xg_sr_restore.c | 149 +++++++++++++++++++++++++++++-- 3 files changed, 154 insertions(+), 9 deletions(-)
tools/libs/guest/Makefile +1 -1
@@ -3,7 +3,7 @@ include $(XEN_ROOT)/tools/Rules.mk include Makefile.common -xg_dom_bzimageloader.o xg_dom_bzimageloader.opic: CFLAGS += $(ZLIB_CFLAGS) +CFLAGS += $(ZLIB_CFLAGS) $(LIBELF_OBJS:.o=.opic): CFLAGS += -Wno-pointer-sign
tools/libs/guest/xg_sr_common.h +12 -0
@@ -209,6 +209,12 @@ static inline int update_blob(struct xc_sr_blob *blob,return 0 ; } +/* Little-endian accessors for the PAGE_DATA_LZ4 per-page clen prefix. */ +static inline uint16_t lz4_page_get_clen (const uint8_t *p) +{ +return (uint16_t )(p[0 ] | (p[1 ] <<8 )); +} +struct xc_sr_context {xc_interface *xch;
@@ -298,6 +304,12 @@ struct xc_sr_context/* memflags to pass to xc_domain_populate_physmap{_exact}(). */ unsigned int memflags; + +#ifdef HAVE_LZ4 +/* Persistent LZ4 decompression buffer */ +void *lz4_decomp_buf; +size_t lz4_decomp_buf_size; +#endif } restore; };
tools/libs/guest/xg_sr_restore.c +141 -8
183 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 3/6 tools/libs/guest: compress PAGE_DATA with LZ4 on save
In reply to Marcus GranadoThis patch updates the save side of the migration stream to emit REC_TYPE_PAGE_DATA_LZ4 compressed records of page data batches.
The compression algorithm is selected at runtime via a new ctx->compression field of type enum xc_sr_compression. It defaults to XC_SR_COMPRESS_NONE, so the on-wire behaviour is unchanged unless compression is explicitly requested; the opt-in that selects XC_SR_COMPRESS_LZ4 is added in a later patch. The LZ4-specific code is compiled in only when libxenguest is built with liblz4 support (ie. HAVE_LZ4 is defined).
The compression state (buffers, state) is kept in the xc_sr_context struct, guarded by #ifdef HAVE_LZ4 for LZ4-specific members. setup_compression_lz4() allocates one persistent buffer:
* lz4_compress_buf: the output buffer packing the per-page compressed output (worst case: every page stored raw), with a few spare bytes so the record's 8-byte alignment padding can be folded into its tail
It is freed in cleanup_compression_lz4(). Using a persistent buffer avoids a malloc/free cycle per batch during migration.
lz4_compress_batch() compresses each present page independently. The raw page data has already been written into iov[2 ..) by write_batch() (which coalesces physically-contiguous pages), so each page is recovered by walking those iov entries in PAGE_SIZE strides. Each page is compressed with a destination capacity of PAGE_SIZE - 1, so any page that does not strictly shrink is stored raw. Then it checks a per-batch ratio threshold (XC_SR_COMPRESS_BATCH_THRESHOLD 97%): if the compressed payload is at least 97% of the original size, lz4_compress_batch() returns false and the batch is sent uncompressed as REC_TYPE_PAGE_DATA instead of REC_TYPE_PAGE_DATA_LZ4. The caller then emits the batch through the native uncompressed per-page path, which the receiver side handles natively as REC_TYPE_PAGE_DATA without any awareness of the discarded compression attempt. This avoids wasting receiver CPU on decompression for negligible size savings and handles incompressible data (eg encrypted pages) where framing overhead would otherwise make the compressed output larger than the input.
write_batch() is refactored to dispatch on ctx->compression: the iovec for writev() is sized at nr_pfns + 2 entries (2 header iovs plus up to one per present page), covering both the LZ4 path (a single payload iov, with the record's alignment padding folded into it) and the uncompressed per-page path used natively and on LZ4 decline.
--- tools/libs/guest/xg_sr_common.h | 26 ++++ tools/libs/guest/xg_sr_save.c | 244 +++++++++++++++++++++++++++++++- 2 files changed, 267 insertions(+), 3 deletions(-)
tools/libs/guest/xg_sr_common.h +26 -0
@@ -210,11 +210,26 @@ static inline int update_blob(struct xc_sr_blob *blob, }/* Little-endian accessors for the PAGE_DATA_LZ4 per-page clen prefix. */ +static inline void lz4_page_put_clen (uint8_t *p,uint16_t clen) +{ + p[0 ] = (uint8_t )(clen &0xff ); + p[1 ] = (uint8_t )((clen >>8 ) &0xff ); +} +static inline uint16_t lz4_page_get_clen (const uint8_t *p) {return (uint16_t )(p[0 ] | (p[1 ] <<8 )); } +/* Compression mode for the migration stream. */ +enum xc_sr_compression +{ +XC_SR_COMPRESS_NONE =0 , +#ifdef HAVE_LZ4 +XC_SR_COMPRESS_LZ4 , +#endif +}; +struct xc_sr_context {xc_interface *xch;
@@ -224,6 +239,9 @@ struct xc_sr_context/* Plain VM, or checkpoints over time. */ xc_stream_type_t stream_type; +/* Compression algorithm for page data records. */ +enum xc_sr_compression compression; +xc_domaininfo_t dominfo; union /* Common save or restore data. */
@@ -250,6 +268,14 @@ struct xc_sr_contextunsigned long *deferred_pages;unsigned long nr_deferred_pages;xc_hypercall_buffer_t dirty_bitmap_hbuf; + +#ifdef HAVE_LZ4 +/* +* Persistent LZ4 output buffer (avoids repeated malloc/free), +* sized MAX_BATCH_SIZE * (PAGE_SIZE + clen) + (REC_ALIGN - 1). +*/ +void *lz4_compress_buf; +#endif } save; struct /* Restore data. */
tools/libs/guest/xg_sr_save.c +241 -3
301 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 4/6 tools/libs/guest: adaptively disable LZ4 on poor compression ratio
In reply to Marcus GranadoIf a VM's memory contain mostly incompressible page content, avoid wasting CPU on both sender and receiver.
Add a per-iteration ratio tracker so that, after each precopy iteration, the average compression ratio across all batches of that iteration is computed; if it is worse than XC_SR_COMPRESS_ITER_THRESHOLD (90%), ctx->compression is downgraded to XC_SR_COMPRESS_NONE for the remainder of the migration.
--- tools/libs/guest/xg_sr_common.h | 18 ++++++ tools/libs/guest/xg_sr_save.c | 109 ++++++++++++++++++++++++++++++++ 2 files changed, 127 insertions(+)
tools/libs/guest/xg_sr_common.h +18 -0
@@ -269,6 +269,24 @@ struct xc_sr_contextunsigned long nr_deferred_pages;xc_hypercall_buffer_t dirty_bitmap_hbuf; +/* +* Compression ratio tracking per iteration. Accumulated by +* the compression function for each batch and evaluated at +* the end of every precopy iteration to decide whether to +* disable compression for the remaining iterations +*/ +uint64_t compress_raw_total;/* uncompressed, this iter */ +uint64_t compress_out_total;/* compressed, this iter */ + +/* +* Cumulative compression totals across the whole save (not +* reset per iteration). Used for the final compression summary +* logged during teardown, remain valid even if adaptive +* disabling turned compression off partway through +*/ +uint64_t compress_raw_all_total;/* uncompressed, all iters */ +uint64_t compress_out_all_total;/* compressed, all iters */ +#ifdef HAVE_LZ4 /* * Persistent LZ4 output buffer (avoids repeated malloc/free),
tools/libs/guest/xg_sr_save.c +109 -0
145 lines not on this page · raw message
Signature · 1 line
2.43.0
PATCH 5/6 tools/libs/guest: opt in to LZ4 save-side compression via XCFLAGS_COMPRESS_LZ4
In reply to Marcus GranadoThis patch introduces a new public flag XCFLAGS_COMPRESS_LZ4 (bit 2) that can be used to enable the LZ4 migration compression. xc_domain_save() now starts with ctx.compression = XC_SR_COMPRESS_NONE and only switches to XC_SR_COMPRESS_LZ4 when the caller sets the flag.
When this flag is clear, libxenguest emits the legacy REC_TYPE_PAGE_DATA records just as it did before this series.
--- tools/include/xenguest.h | 5 +++-- tools/libs/guest/xg_sr_save.c | 11 +++++++++++ 2 files changed, 14 insertions(+), 2 deletions(-)
tools/include/xenguest.h +3 -2
@@ -24,8 +24,9 @@#define XC_NUMA_NO_NODE (~0U) -#define XCFLAGS_LIVE (1 << 0) -#define XCFLAGS_DEBUG (1 << 1) +#define XCFLAGS_LIVE (1 << 0) +#define XCFLAGS_DEBUG (1 << 1) +#define XCFLAGS_COMPRESS_LZ4 (1 << 2)/* Compress PAGE_DATA with LZ4 on save */ #define X86_64_B_SIZE 64#define X86_32_B_SIZE 32
tools/libs/guest/xg_sr_save.c +11 -0
@@ -1317,6 +1317,17 @@ int xc_domain_save(xc_interface *xch, int io_fd, uint32_t dom,bool hvm; ctx.compression =XC_SR_COMPRESS_NONE ; +if ( flags &XCFLAGS_COMPRESS_LZ4 ) + { +#ifdef HAVE_LZ4 + ctx.compression =XC_SR_COMPRESS_LZ4 ; +#else +ERROR ("XCFLAGS_COMPRESS_LZ4 requested but libxenguest was built " +"without liblz4 support" ); + errno =EOPNOTSUPP ; +return -1 ; +#endif + }/* GCC 4.4 (of CentOS 6.x vintage) can' t initialise anonymous unions. */ ctx.save.callbacks = callbacks;
Signature · 1 line
2.43.0
PATCH 6/6 tools/libxl,xl: add --compress flag to xl migrate
In reply to Marcus GranadoWire the libxenguest XCFLAGS_COMPRESS_LZ4 bit added earlier in this series to libxl and xl:
* xl migrate gets a new --compress option
* libxl gets a new public flag LIBXL_SUSPEND_COMPRESS_LZ4
* LIBXL_SUSPEND_COMPRESS_LZ4 is mapped to XCFLAGS_COMPRESS_LZ4
No change to the default behaviour: without --compress, xl migrate continues to emit uncompressed PAGE_DATA records.
--- docs/man/xl.1.pod.in | 8 ++++++++ tools/include/libxl.h | 1 + tools/libs/light/libxl_dom_save.c | 4 +++- tools/libs/light/libxl_domain.c | 1 + tools/libs/light/libxl_internal.h | 1 + tools/xl/xl_cmdtable.c | 2 ++ tools/xl/xl_migrate.c | 13 ++++++++++--- 7 files changed, 26 insertions(+), 4 deletions(-)
docs/man/xl.1.pod.in +8 -0
@@ -488,6 +488,14 @@ domain. Display huge (!) amount of debug information during the migration process. +=item B<--compress> + +Compress the memory stream using LZ4 inside libxenguest before sending it +to the destination host. This can significantly reduce migration time on +slow transports, and reduce dom0 CPU pressure during host evacuations when +the migration data is subsequently encrypted or copied through pipes. +Requires libxenguest to have been built against liblz4. + =item B<-p> Leave the domain on the receive side paused after migration.
tools/include/libxl.h +1 -0
@@ -1908,6 +1908,7 @@ int libxl_domain_suspend(libxl_ctx *ctx, uint32_t domid, int fd,LIBXL_EXTERNAL_CALLERS_ONLY ;#define LIBXL_SUSPEND_DEBUG 1#define LIBXL_SUSPEND_LIVE 2 +#define LIBXL_SUSPEND_COMPRESS_LZ4 4/* * Only suspend domain, do not save its state to file, do not destroy it.
tools/libs/light/libxl_dom_save.c +3 -1
@@ -245,6 +245,7 @@ void libxl__domain_save(libxl__egc *egc, libxl__domain_save_state *dss)const libxl_domain_type type = dss->type;const int live = dss->live;const int debug = dss->debug; +const int compress_lz4 = dss->compress_lz4;const libxl_domain_remus_info *const r_info = dss->remus;libxl__srm_save_autogen_callbacks *const callbacks = &dss->sws.shs.callbacks.save.a;
@@ -269,7 +270,8 @@ void libxl__domain_save(libxl__egc *egc, libxl__domain_save_state *dss)if (rc) goto out; dss->xcflags = (live ?XCFLAGS_LIVE :0 ) - | (debug ?XCFLAGS_DEBUG :0 ); + | (debug ?XCFLAGS_DEBUG :0 ) + | (compress_lz4 ?XCFLAGS_COMPRESS_LZ4 :0 );/* Disallow saving a guest with vNUMA configured because migration * stream does not preserve node information.
tools/libs/light/libxl_domain.c +1 -0
@@ -527,6 +527,7 @@ int libxl_domain_suspend(libxl_ctx *ctx, uint32_t domid, int fd, int flags, dss->type = type; dss->live = flags &LIBXL_SUSPEND_LIVE ; dss->debug = flags &LIBXL_SUSPEND_DEBUG ; + dss->compress_lz4 = flags &LIBXL_SUSPEND_COMPRESS_LZ4 ; dss->checkpointed_stream =LIBXL_CHECKPOINTED_STREAM_NONE ; rc = libxl__fd_flags_modify_save(gc, dss->fd,
tools/libs/light/libxl_internal.h +1 -0
@@ -3598,6 +3598,7 @@ struct libxl__domain_save_state {libxl_domain_type type;int live;int debug; +int compress_lz4;int checkpointed_stream;const libxl_domain_remus_info *remus;/* private */
tools/xl/xl_cmdtable.c +2 -0
@@ -174,6 +174,8 @@ const struct cmd_spec cmd_table[] = {"-e Do not wait in the background (on <host>) for the death\n" " of the domain.\n" "--debug Print huge (!) amount of debug during the migration process.\n" +"--compress Compress the memory stream using LZ4 in libxenguest.\n" +" Requires libxenguest to be built against liblz4.\n" "-p Do not unpause domain after migrating it.\n" "-D Preserve the domain id" },
tools/xl/xl_migrate.c +10 -3
@@ -177,7 +177,7 @@ static void migrate_do_preamble(int send_fd, int recv_fd, pid_t child, }static void migrate_domain (uint32_t domid,int preserve_domid, -const char *rune,int debug, +const char *rune,int debug,int compress_lz4,const char *override_config_file) {pid_t child =-1 ;
@@ -206,6 +206,8 @@ static void migrate_domain(uint32_t domid, int preserve_domid,if (debug) flags |=LIBXL_SUSPEND_DEBUG ; +if (compress_lz4) + flags |=LIBXL_SUSPEND_COMPRESS_LZ4 ; rc =libxl_domain_suspend (ctx, domid, send_fd, flags,NULL );if (rc) { fprintf(stderr, "migration sender: libxl_domain_suspend failed"
@@ -538,10 +540,11 @@ int main_migrate(int argc, char **argv)char *rune =NULL ;char *host;int opt, daemonize =1 , monitor =1 , debug =0 , pause_after_migration =0 ; -int preserve_domid =0 ; +int preserve_domid =0 , compress_lz4 =0 ;static struct option opts[] = { {"debug" ,0 ,0 ,0x100 }, {"live" ,0 ,0 ,0x200 }, + {"compress" ,0 ,0 ,0x300 },COMMON_LONG_OPTS };
@@ -571,6 +574,9 @@ int main_migrate(int argc, char **argv)case 0x200 :/* --live */ /* ignored for compatibility with xm */ break ; +case 0x300 :/* --compress */ + compress_lz4 =1 ; +break ; } domid =find_domain (argv[optind]);
@@ -602,7 +608,8 @@ int main_migrate(int argc, char **argv) pause_after_migration ? " -p" : ""); } -migrate_domain (domid, preserve_domid, rune, debug, config_filename); +migrate_domain (domid, preserve_domid, rune, debug, compress_lz4, + config_filename);return EXIT_SUCCESS ; }
Signature · 1 line
2.43.0