| 1 |
# Introduction |
| 2 |
|
| 3 |
This directory contains SystemZ deflate hardware acceleration support. |
| 4 |
It can be enabled using the following build commands: |
| 5 |
|
| 6 |
$ ./configure --with-dfltcc-deflate --with-dfltcc-inflate |
| 7 |
$ make |
| 8 |
|
| 9 |
or |
| 10 |
|
| 11 |
$ cmake -DWITH_DFLTCC_DEFLATE=1 -DWITH_DFLTCC_INFLATE=1 . |
| 12 |
$ make |
| 13 |
|
| 14 |
When built like this, zlib-ng would compress using hardware on level 1, |
| 15 |
and using software on all other levels. Decompression will always happen |
| 16 |
in hardware. In order to enable hardware compression for levels 1-6 |
| 17 |
(i.e. to make it used by default) one could add |
| 18 |
`-DDFLTCC_LEVEL_MASK=0x7e` to CFLAGS when building zlib-ng. |
| 19 |
|
| 20 |
SystemZ deflate hardware acceleration is available on [IBM z15]( |
| 21 |
https://www.ibm.com/products/z15) and newer machines under the name [ |
| 22 |
"Integrated Accelerator for zEnterprise Data Compression"]( |
| 23 |
https://www.ibm.com/support/z-content-solutions/compression/). The |
| 24 |
programming interface to it is a machine instruction called DEFLATE |
| 25 |
CONVERSION CALL (DFLTCC). It is documented in Chapter 26 of [Principles |
| 26 |
of Operation](https://publibfp.dhe.ibm.com/epubs/pdf/a227832c.pdf). Both |
| 27 |
the code and the rest of this document refer to this feature simply as |
| 28 |
"DFLTCC". |
| 29 |
|
| 30 |
# Performance |
| 31 |
|
| 32 |
Performance figures are published [here]( |
| 33 |
https://github.com/iii-i/zlib-ng/wiki/Performance-with-dfltcc-patch-applied-and-dfltcc-support-built-on-dfltcc-enabled-machine |
| 34 |
). The compression speed-up can be as high as 110x and the decompression |
| 35 |
speed-up can be as high as 15x. |
| 36 |
|
| 37 |
# Limitations |
| 38 |
|
| 39 |
Two DFLTCC compression calls with identical inputs are not guaranteed to |
| 40 |
produce identical outputs. Therefore care should be taken when using |
| 41 |
hardware compression when reproducible results are desired. In |
| 42 |
particular, zlib-ng-specific `zng_deflateSetParams` call allows setting |
| 43 |
`Z_DEFLATE_REPRODUCIBLE` parameter, which disables DFLTCC support for a |
| 44 |
particular stream. |
| 45 |
|
| 46 |
DFLTCC does not support every single zlib-ng feature, in particular: |
| 47 |
|
| 48 |
* `inflate(Z_BLOCK)` and `inflate(Z_TREES)` |
| 49 |
* `inflateMark()` |
| 50 |
* `inflatePrime()` |
| 51 |
* `inflateSyncPoint()` |
| 52 |
|
| 53 |
When used, these functions will either switch to software, or, in case |
| 54 |
this is not possible, gracefully fail. |
| 55 |
|
| 56 |
# Code structure |
| 57 |
|
| 58 |
All SystemZ-specific code lives in `arch/s390` directory and is |
| 59 |
integrated with the rest of zlib-ng using hook macros. |
| 60 |
|
| 61 |
## Hook macros |
| 62 |
|
| 63 |
DFLTCC takes as arguments a parameter block, an input buffer, an output |
| 64 |
buffer and a window. `ZALLOC_DEFLATE_STATE()`, `ZALLOC_INFLATE_STATE()`, |
| 65 |
`ZFREE_STATE()`, `ZCOPY_DEFLATE_STATE()`, `ZCOPY_INFLATE_STATE()`, |
| 66 |
`ZALLOC_WINDOW()`, `ZCOPY_WINDOW()` and `TRY_FREE_WINDOW()` macros encapsulate |
| 67 |
allocation details for the parameter block (which is allocated alongside |
| 68 |
zlib-ng state) and the window (which must be page-aligned and large enough). |
| 69 |
|
| 70 |
Software and hardware window formats do not match, therefore, |
| 71 |
`deflateSetDictionary()`, `deflateGetDictionary()`, `inflateSetDictionary()` |
| 72 |
and `inflateGetDictionary()` need special handling, which is triggered using |
| 73 |
`DEFLATE_SET_DICTIONARY_HOOK()`, `DEFLATE_GET_DICTIONARY_HOOK()`, |
| 74 |
`INFLATE_SET_DICTIONARY_HOOK()` and `INFLATE_GET_DICTIONARY_HOOK()` macros. |
| 75 |
|
| 76 |
`deflateResetKeep()` and `inflateResetKeep()` update the DFLTCC |
| 77 |
parameter block using `DEFLATE_RESET_KEEP_HOOK()` and |
| 78 |
`INFLATE_RESET_KEEP_HOOK()` macros. |
| 79 |
|
| 80 |
`INFLATE_PRIME_HOOK()`, `INFLATE_MARK_HOOK()` and |
| 81 |
`INFLATE_SYNC_POINT_HOOK()` macros make the respective unsupported |
| 82 |
calls gracefully fail. |
| 83 |
|
| 84 |
`DEFLATE_PARAMS_HOOK()` implements switching between hardware and |
| 85 |
software compression mid-stream using `deflateParams()`. Switching |
| 86 |
normally entails flushing the current block, which might not be possible |
| 87 |
in low memory situations. `deflateParams()` uses `DEFLATE_DONE()` hook |
| 88 |
in order to detect and gracefully handle such situations. |
| 89 |
|
| 90 |
The algorithm implemented in hardware has different compression ratio |
| 91 |
than the one implemented in software. `DEFLATE_BOUND_ADJUST_COMPLEN()` |
| 92 |
and `DEFLATE_NEED_CONSERVATIVE_BOUND()` macros make `deflateBound()` |
| 93 |
return the correct results for the hardware implementation. |
| 94 |
|
| 95 |
Actual compression and decompression are handled by `DEFLATE_HOOK()` and |
| 96 |
`INFLATE_TYPEDO_HOOK()` macros. Since inflation with DFLTCC manages the |
| 97 |
window on its own, calling `updatewindow()` is suppressed using |
| 98 |
`INFLATE_NEED_UPDATEWINDOW()` macro. |
| 99 |
|
| 100 |
In addition to compression, DFLTCC computes CRC-32 and Adler-32 |
| 101 |
checksums, therefore, whenever it's used, software checksumming is |
| 102 |
suppressed using `DEFLATE_NEED_CHECKSUM()` and `INFLATE_NEED_CHECKSUM()` |
| 103 |
macros. |
| 104 |
|
| 105 |
While software always produces reproducible compression results, this |
| 106 |
is not the case for DFLTCC. Therefore, zlib-ng users are given the |
| 107 |
ability to specify whether or not reproducible compression results |
| 108 |
are required. While it is always possible to specify this setting |
| 109 |
before the compression begins, it is not always possible to do so in |
| 110 |
the middle of a deflate stream - the exact conditions for that are |
| 111 |
determined by `DEFLATE_CAN_SET_REPRODUCIBLE()` macro. |
| 112 |
|
| 113 |
## SystemZ-specific code |
| 114 |
|
| 115 |
When zlib-ng is built with DFLTCC, the hooks described above are |
| 116 |
converted to calls to functions, which are implemented in |
| 117 |
`arch/s390/dfltcc_*` files. The functions can be grouped in three broad |
| 118 |
categories: |
| 119 |
|
| 120 |
* Base DFLTCC support, e.g. wrapping the machine instruction - |
| 121 |
`dfltcc()` and allocating aligned memory - `dfltcc_alloc_state()`. |
| 122 |
* Translating between software and hardware data formats, e.g. |
| 123 |
`dfltcc_deflate_set_dictionary()`. |
| 124 |
* Translating between software and hardware state machines, e.g. |
| 125 |
`dfltcc_deflate()` and `dfltcc_inflate()`. |
| 126 |
|
| 127 |
The functions from the first two categories are fairly simple, however, |
| 128 |
various quirks in both software and hardware state machines make the |
| 129 |
functions from the third category quite complicated. |
| 130 |
|
| 131 |
### `dfltcc_deflate()` function |
| 132 |
|
| 133 |
This function is called by `deflate()` and has the following |
| 134 |
responsibilities: |
| 135 |
|
| 136 |
* Checking whether DFLTCC can be used with the current stream. If this |
| 137 |
is not the case, then it returns `0`, making `deflate()` use some |
| 138 |
other function in order to compress in software. Otherwise it returns |
| 139 |
`1`. |
| 140 |
* Block management and Huffman table generation. DFLTCC ends blocks only |
| 141 |
when explicitly instructed to do so by the software. Furthermore, |
| 142 |
whether to use fixed or dynamic Huffman tables must also be determined |
| 143 |
by the software. Since looking at data in order to gather statistics |
| 144 |
would negate performance benefits, the following approach is used: the |
| 145 |
first `DFLTCC_FIRST_FHT_BLOCK_SIZE` bytes are placed into a fixed |
| 146 |
block, and every next `DFLTCC_BLOCK_SIZE` bytes are placed into |
| 147 |
dynamic blocks. |
| 148 |
* Writing EOBS. Block Closing Control bit in the parameter block |
| 149 |
instructs DFLTCC to write EOBS, however, certain conditions need to be |
| 150 |
met: input data length must be non-zero or Continuation Flag must be |
| 151 |
set. To put this in simpler terms, DFLTCC will silently refuse to |
| 152 |
write EOBS if this is the only thing that it is asked to do. Since the |
| 153 |
code has to be able to emit EOBS in software anyway, in order to avoid |
| 154 |
tricky corner cases Block Closing Control is never used. Whether to |
| 155 |
write EOBS is instead controlled by `soft_bcc` variable. |
| 156 |
* Triggering block post-processing. Depending on flush mode, `deflate()` |
| 157 |
must perform various additional actions when a block or a stream ends. |
| 158 |
`dfltcc_deflate()` informs `deflate()` about this using |
| 159 |
`block_state *result` parameter. |
| 160 |
* Converting software state fields into hardware parameter block fields, |
| 161 |
and vice versa. For example, `wrap` and Check Value Type or `bi_valid` |
| 162 |
and Sub-Byte Boundary. Certain fields cannot be translated and must |
| 163 |
persist untouched in the parameter block between calls, for example, |
| 164 |
Continuation Flag or Continuation State Buffer. |
| 165 |
* Handling flush modes and low-memory situations. These aspects are |
| 166 |
quite intertwined and pervasive. The general idea here is that the |
| 167 |
code must not do anything in software - whether explicitly by e.g. |
| 168 |
calling `send_eobs()`, or implicitly - by returning to `deflate()` |
| 169 |
with certain return and `*result` values, when Continuation Flag is |
| 170 |
set. |
| 171 |
* Ending streams. When a new block is started and flush mode is |
| 172 |
`Z_FINISH`, Block Header Final parameter block bit is used to mark |
| 173 |
this block as final. However, sometimes an empty final block is |
| 174 |
needed, and, unfortunately, just like with EOBS, DFLTCC will silently |
| 175 |
refuse to do this. The general idea of DFLTCC implementation is to |
| 176 |
rely as much as possible on the existing code. Here in order to do |
| 177 |
this, the code pretends that it does not support DFLTCC, which makes |
| 178 |
`deflate()` call a software compression function, which writes an |
| 179 |
empty final block. Whether this is required is controlled by |
| 180 |
`need_empty_block` variable. |
| 181 |
* Error handling. This is simply converting |
| 182 |
Operation-Ending-Supplemental Code to string. Errors can only happen |
| 183 |
due to things like memory corruption, and therefore they don't affect |
| 184 |
the `deflate()` return code. |
| 185 |
|
| 186 |
### `dfltcc_inflate()` function |
| 187 |
|
| 188 |
This function is called by `inflate()` from the `TYPEDO` state (that is, |
| 189 |
when all the metadata is parsed and the stream is positioned at the type |
| 190 |
bits of deflate block header) and it's responsible for the following: |
| 191 |
|
| 192 |
* Falling back to software when flush mode is `Z_BLOCK` or `Z_TREES`. |
| 193 |
Unfortunately, there is no way to ask DFLTCC to stop decompressing on |
| 194 |
block or tree boundary. |
| 195 |
* `inflate()` decompression loop management. This is controlled using |
| 196 |
the return value, which can be either `DFLTCC_INFLATE_BREAK` or |
| 197 |
`DFLTCC_INFLATE_CONTINUE`. |
| 198 |
* Converting software state fields into hardware parameter block fields, |
| 199 |
and vice versa. For example, `whave` and History Length or `wnext` and |
| 200 |
History Offset. |
| 201 |
* Ending streams. This instructs `inflate()` to return `Z_STREAM_END` |
| 202 |
and is controlled by `last` state field. |
| 203 |
* Error handling. Like deflate, error handling comprises |
| 204 |
Operation-Ending-Supplemental Code to string conversion. Unlike |
| 205 |
deflate, errors may happen due to bad inputs, therefore they are |
| 206 |
propagated to `inflate()` by setting `mode` field to `MEM` or `BAD`. |
| 207 |
|
| 208 |
# Testing |
| 209 |
|
| 210 |
Given complexity of DFLTCC machine instruction, it is not clear whether |
| 211 |
QEMU TCG will ever support it. At the time of writing, one has to have |
| 212 |
access to an IBM z15+ VM or LPAR in order to test DFLTCC support. Since |
| 213 |
DFLTCC is a non-privileged instruction, neither special VM/LPAR |
| 214 |
configuration nor root are required. |
| 215 |
|
| 216 |
zlib-ng CI uses an IBM-provided z15 self-hosted builder for the DFLTCC |
| 217 |
testing. There are no IBM Z builds of GitHub Actions runner, and |
| 218 |
stable qemu-user has problems with .NET apps, so the builder runs the |
| 219 |
x86_64 runner version with qemu-user built from the master branch. |
| 220 |
|
| 221 |
## Configuring the builder. |
| 222 |
|
| 223 |
### Install prerequisites. |
| 224 |
|
| 225 |
``` |
| 226 |
$ sudo dnf install docker |
| 227 |
``` |
| 228 |
|
| 229 |
### Add services. |
| 230 |
|
| 231 |
``` |
| 232 |
$ sudo cp self-hosted-builder/*.service /etc/systemd/system/ |
| 233 |
$ sudo systemctl daemon-reload |
| 234 |
``` |
| 235 |
|
| 236 |
### Create a config file. |
| 237 |
|
| 238 |
``` |
| 239 |
$ sudo tee /etc/actions-runner |
| 240 |
repo=<owner>/<name> |
| 241 |
access_token=<ghp_***> |
| 242 |
``` |
| 243 |
|
| 244 |
Access token should have the repo scope, consult |
| 245 |
https://docs.github.com/en/rest/reference/actions#create-a-registration-token-for-a-repository |
| 246 |
for details. |
| 247 |
|
| 248 |
### Autostart the x86_64 emulation support. |
| 249 |
|
| 250 |
``` |
| 251 |
$ sudo systemctl enable --now qemu-user-static |
| 252 |
``` |
| 253 |
|
| 254 |
### Autostart the runner. |
| 255 |
|
| 256 |
``` |
| 257 |
$ sudo systemctl enable --now actions-runner |
| 258 |
``` |
| 259 |
|
| 260 |
## Rebuilding the image |
| 261 |
|
| 262 |
In order to update the `iiilinuxibmcom/actions-runner` image, e.g. to get the |
| 263 |
latest OS security fixes, use the following commands: |
| 264 |
|
| 265 |
``` |
| 266 |
$ sudo docker build \ |
| 267 |
--pull \ |
| 268 |
-f self-hosted-builder/actions-runner.Dockerfile \ |
| 269 |
-t iiilinuxibmcom/actions-runner |
| 270 |
$ sudo systemctl restart actions-runner |
| 271 |
``` |
| 272 |
|
| 273 |
## Removing persistent data |
| 274 |
|
| 275 |
The `actions-runner` service stores various temporary data, such as runner |
| 276 |
registration information, work directories and logs, in the `actions-runner` |
| 277 |
volume. In order to remove it and start from scratch, e.g. when switching the |
| 278 |
runner to a different repository, use the following commands: |
| 279 |
|
| 280 |
``` |
| 281 |
$ sudo systemctl stop actions-runner |
| 282 |
$ sudo docker rm -f actions-runner |
| 283 |
$ sudo docker volume rm actions-runner |
| 284 |
``` |