Andrew Savage | ed58f02 | 2022-08-12 09:14:48 -0700 | [diff] [blame] | 1 | # Evergreen Binary Compression |
| 2 | |
| 3 | ## What is Evergreen Binary Compression? |
| 4 | |
| 5 | Evergreen Binary Compression is a feature that reduces the amount of space used |
| 6 | on-device to store Cobalt Core binaries. The binaries are stored compressed, |
| 7 | using the |
| 8 | [LZ4 Frame Format](https://github.com/lz4/lz4/blob/dev/doc/lz4_Frame_format.md), |
| 9 | and decompressed when they are loaded and run. This optional feature is off by |
| 10 | default but partners can enable it by following the instructions below. |
| 11 | |
| 12 | ## Storage Savings |
| 13 | |
| 14 | Across all reference devices tested, including Raspberry Pi 2, we have seen |
| 15 | compression ratios just above 2.0. We therefore expect that partners can halve |
| 16 | the space used for Cobalt binary storage by enabling the feature for all |
| 17 | installation slots: the system image slot and the writable slots. |
| 18 | |
| 19 | ## Caveats |
| 20 | |
| 21 | ### Performance Costs |
| 22 | |
| 23 | Because the Cobalt Core shared library must be decompressed before it's loaded, |
| 24 | there is necessarily some effect on startup latency and CPU memory usage. Based |
| 25 | on our analysis across a range of devices these effects are relatively small. |
| 26 | |
| 27 | For startup latency, we measured an increase in `libcobalt` load time in the |
| 28 | hundreds of milliseconds for the low-powered Raspberry Pi 2 and in the tens of |
| 29 | milliseconds for more representative, higher-powered devices. This increase is |
| 30 | small in the context of overall app load time. |
| 31 | |
| 32 | For CPU memory usage, `libcobalt.lz4` is decompressed to an in-memory ELF file |
| 33 | before the program is loaded. We therefore expect CPU memory usage to |
| 34 | temporarily increase by roughly the size of the uncompressed `libcobalt` and |
| 35 | then to return to a normal level once the library is loaded. And this is exactly |
| 36 | what we've seen in our analysis. |
| 37 | |
| 38 | ### Incompatibilities |
| 39 | |
| 40 | This feature is incompatible with the Memory Mapped file feature that is |
| 41 | controlled by the `--loader_use_mmap_file` switch. With compression, we lose the |
| 42 | required 1:1 mapping between the file and virtual memory. So, |
| 43 | `--loader_use_mmap_file` should not be set if compression is enabled for any |
| 44 | installation slots (next section). |
| 45 | |
| 46 | ## Enabling Binary Compression |
| 47 | |
| 48 | Separate steps are required in order for a partner to enable compression for a) |
| 49 | the system image slot and b) (Evergreen-Full only) the 2+ writable slots on a |
| 50 | device. |
| 51 | |
| 52 | Compression for the system image slot is enabled by installing `libcobalt.lz4` |
| 53 | instead of `libcobalt.so` in the read-only system image slot (i.e., SLOT_0). |
| 54 | Starting with a to be determined Cobalt 23 release, the open-source releases on |
| 55 | [GitHub](https://github.com/youtube/cobalt/releases) include separate CRX |
| 56 | packages, denoted by a "compressed" suffix, that contain the pre-built and |
| 57 | LZ4-compressed Cobalt Core binary. |
| 58 | |
| 59 | Compression for the writable slots is enabled by configuring Cobalt to launch |
| 60 | with the `--use_compressed_updates` flag: |
| 61 | |
| 62 | ``` |
| 63 | $ ./loader_app --use_compressed_updates |
| 64 | ``` |
| 65 | |
| 66 | This flag instructs the Cobalt Updater to request, download, and install |
| 67 | packages containing compressed Cobalt Core binaries from Google Update and |
| 68 | Google Downloads. As a result, the device ends up with `libcobalt.lz4` instead |
| 69 | of `libcobalt.so` in the relevant slot after its next update. |
| 70 | |
| 71 | Note that the system image slot is independent from the writable slots with |
| 72 | respect to the compression feature: the feature can be enabled for the system |
| 73 | image slot but disabled for the writable slots, or vice versa. However, we |
| 74 | expect that partners will typically enable the feature for all slots in order to |
| 75 | take full advantage of the storage reduction. |
| 76 | |
| 77 | ## Disabling Binary Compression |
| 78 | |
| 79 | The compression feature is turned off by default. Once enabled, though, it can |
| 80 | be disabled by undoing the steps required for enablement. |
| 81 | |
| 82 | Compression for the system image slot is disabled by installing the uncompressed |
| 83 | `libcobalt.so` instead of `libcobalt.lz4` in the read-only system image slot. |
| 84 | |
| 85 | Compression for the writable slots is disabled by configuring Cobalt to launch |
| 86 | **without** the `--use_compressed_updates` flag. The Cobalt Updater will then be |
| 87 | instructed to request, download, and install packages containing the |
| 88 | uncompressed Cobalt Core binaries. |
| 89 | |
| 90 | Note that disabling compression for the writable slots is eventual in the sense |
| 91 | that the compressed Cobalt Core binaries in these slots will remain and continue |
| 92 | to be used until newer versions of Cobalt Core are available on Google Update |
| 93 | and Google Downloads. |
| 94 | |
| 95 | ## FAQ |
| 96 | |
| 97 | ### Should multiple apps that share binaries also share compression settings? |
| 98 | |
| 99 | The [Cobalt Evergreen Overview doc](cobalt_evergreen_overview.md) describes |
| 100 | multi-app support, where multiple Cobalt-based applications share Cobalt Core |
| 101 | binaries (i.e., they share the same installation slot(s)). When multi-app |
| 102 | support is used then, yes, the apps should also share compression settings. |
| 103 | |
| 104 | For the system image slot, this means that either `libcobalt.lz4` or |
| 105 | `libcobalt.so` is installed in the one, shared slot. |
| 106 | |
| 107 | For the writable slots, this means that the loader app is either launched with |
| 108 | `--use_compressed_updates` set for all apps or launched without it for all apps. |
| 109 | Otherwise, the different apps would compete with respect to whether the writable |
| 110 | slots are upgraded to compressed or uncompressed binaries. |