Get started with pw_tokenizer#
pw_tokenizer: Compress strings to shrink logs by +75%
Overview#
There are two sides to pw_tokenizer, which we call tokenization and
detokenization.
Tokenization converts string literals in the source code to binary tokens at compile time. If the string has printf-style arguments, these are encoded to compact binary form at runtime.
Detokenization converts tokenized strings back to the original human-readable strings.
Here’s an overview of what happens when pw_tokenizer is used:
During compilation, the
pw_tokenizermodule hashes string literals to generate stable 32-bit tokens.The tokenization macro removes these strings by declaring them in an ELF section that is excluded from the final binary.
After compilation, strings are extracted from the ELF to build a database of tokenized strings for use by the detokenizer. The ELF file may also be used directly.
During operation, the device encodes the string token and its arguments, if any.
The encoded tokenized strings are sent off-device or stored.
Off-device, the detokenizer tools use the token database to decode the strings to human-readable form.
Integrating with Bazel / GN / CMake projects#
Integrating pw_tokenizer requires a few steps beyond building the code. This
section describes one way pw_tokenizer might be integrated with a project.
These steps can be adapted as needed.
Add
pw_tokenizerto your build. Build files for GN, CMake, and Bazel are provided. For Make or other build systems, add the files specified in the BUILD.gn’spw_tokenizertarget to the build.Use the tokenization macros in your code. See Tokenization.
Ensure the
.pw_tokenizer.*sections are included in your output ELF file:In GN and CMake, this is done automatically.
In Bazel, include
"@pigweed//pw_tokenizer:linker_script"in thedepsof your main binary rule (assuming you’re already overriding the default linker script).If your binary does not use a custom linker script, you can pass
add_tokenizer_sections_to_default_script.ldto the linker which will augment the default linker script (rather than override it).Alternatively, include
pw_tokenizer/pw_tokenizer_linker_sections.ldfrom your project’s linker script by depending on"@pigweed//pw_tokenizer:pw_tokenizer_linker_sections_ld".
Compile your code to produce an ELF file.
Run
database.py createon the ELF file to generate a CSV token database. See Managing token databases.Commit the token database to your repository. See notes in Database management.
Integrate a
database.py addcommand to your build to automatically update the committed token database. In GN, use thepw_tokenizer_databasetemplate to do this. See Update a database.Integrate
detokenize.pyor the C++ detokenization library with your tools to decode tokenized logs. See Detokenization.
Using with Zephyr#
When building pw_tokenizer with Zephyr, 3 Kconfigs can be used currently:
CONFIG_PIGWEED_TOKENIZERwill automatically linkpw_tokenizeras well as any dependencies.CONFIG_PIGWEED_TOKENIZER_BASE64will automatically linkpw_tokenizer.base64as well as any dependencies.CONFIG_PIGWEED_DETOKENIZERwill automatically linkpw_tokenizer.decoderas well as any dependencies.
Once enabled, the tokenizer headers can be included like any Zephyr headers:
#include <pw_tokenizer/tokenize.h>
Note
Zephyr handles the additional linker sections via
pw_tokenizer_zephyr.ld which is added to the end of the linker file
via a call to zephyr_linker_sources(SECTIONS ...).