1:tocdepth: 3 2 3.. _module-pw_tokenizer-tokenization: 4 5============ 6Tokenization 7============ 8.. pigweed-module-subpage:: 9 :name: pw_tokenizer 10 11Tokenization converts a string literal to a token. If it's a printf-style 12string, its arguments are encoded along with it. The results of tokenization can 13be sent off device or stored in place of a full string. 14 15-------- 16Concepts 17-------- 18See :ref:`module-pw_tokenizer-get-started-overview` for a high-level 19explanation of how ``pw_tokenizer`` works. 20 21Token generation: fixed length hashing at compile time 22====================================================== 23String tokens are generated using a modified version of the x65599 hash used by 24the SDBM project. All hashing is done at compile time. 25 26In C code, strings are hashed with a preprocessor macro. For compatibility with 27macros, the hash must be limited to a fixed maximum number of characters. This 28value is set by ``PW_TOKENIZER_CFG_C_HASH_LENGTH``. Increasing 29``PW_TOKENIZER_CFG_C_HASH_LENGTH`` increases the compilation time for C due to 30the complexity of the hashing macros. 31 32C++ macros use a constexpr function instead of a macro. This function works with 33any length of string and has lower compilation time impact than the C macros. 34For consistency, C++ tokenization uses the same hash algorithm, but the 35calculated values will differ between C and C++ for strings longer than 36``PW_TOKENIZER_CFG_C_HASH_LENGTH`` characters. 37 38Token encoding 39============== 40The token is a 32-bit hash calculated during compilation. The string is encoded 41little-endian with the token followed by arguments, if any. For example, the 4231-byte string ``You can go about your business.`` hashes to 0xdac9a244. 43This is encoded as 4 bytes: ``44 a2 c9 da``. 44 45Arguments are encoded as follows: 46 47* **Integers** (1--10 bytes) -- 48 `ZagZag and varint encoded <https://developers.google.com/protocol-buffers/docs/encoding#signed-integers>`_, 49 similarly to Protocol Buffers. Smaller values take fewer bytes. 50* **Floating point numbers** (4 bytes) -- Single precision floating point. 51* **Strings** (1--128 bytes) -- Length byte followed by the string contents. 52 The top bit of the length whether the string was truncated or not. The 53 remaining 7 bits encode the string length, with a maximum of 127 bytes. 54 55.. TODO(hepler): insert diagram here! 56 57.. tip:: 58 ``%s`` arguments can quickly fill a tokenization buffer. Keep ``%s`` 59 arguments short or avoid encoding them as strings (e.g. encode an enum as an 60 integer instead of a string). See also 61 :ref:`module-pw_tokenizer-nested-arguments`. 62 63.. _module-pw_tokenizer-proto: 64 65Tokenized fields in protocol buffers 66==================================== 67Text may be represented in a few different ways: 68 69- Plain ASCII or UTF-8 text (``This is plain text``) 70- Base64-encoded tokenized message (``$ibafcA==``) 71- Binary-encoded tokenized message (``89 b6 9f 70``) 72- Little-endian 32-bit integer token (``0x709fb689``) 73 74``pw_tokenizer`` provides the ``pw.tokenizer.format`` protobuf field option. 75This option may be applied to a protobuf field to indicate that it may contain a 76tokenized string. A string that is optionally tokenized is represented with a 77single ``bytes`` field annotated with ``(pw.tokenizer.format) = 78TOKENIZATION_OPTIONAL``. 79 80For example, the following protobuf has one field that may contain a tokenized 81string. 82 83.. code-block:: protobuf 84 85 import "pw_tokenizer_proto/options.proto"; 86 87 message MessageWithOptionallyTokenizedField { 88 bytes just_bytes = 1; 89 bytes maybe_tokenized = 2 [(pw.tokenizer.format) = TOKENIZATION_OPTIONAL]; 90 string just_text = 3; 91 } 92 93----------------------- 94Tokenization in C++ / C 95----------------------- 96To tokenize a string, include ``pw_tokenizer/tokenize.h`` and invoke one of the 97``PW_TOKENIZE_*`` macros. 98 99Tokenize string literals outside of expressions 100=============================================== 101``pw_tokenizer`` provides macros for tokenizing string literals with no 102arguments: 103 104* :c:macro:`PW_TOKENIZE_STRING` 105* :c:macro:`PW_TOKENIZE_STRING_DOMAIN` 106* :c:macro:`PW_TOKENIZE_STRING_MASK` 107 108The tokenization macros above cannot be used inside other expressions. 109 110.. admonition:: **Yes**: Assign :c:macro:`PW_TOKENIZE_STRING` to a ``constexpr`` variable. 111 :class: checkmark 112 113 .. code-block:: cpp 114 115 constexpr uint32_t kGlobalToken = PW_TOKENIZE_STRING("Wowee Zowee!"); 116 117 void Function() { 118 constexpr uint32_t local_token = PW_TOKENIZE_STRING("Wowee Zowee?"); 119 } 120 121.. admonition:: **No**: Use :c:macro:`PW_TOKENIZE_STRING` in another expression. 122 :class: error 123 124 .. code-block:: cpp 125 126 void BadExample() { 127 ProcessToken(PW_TOKENIZE_STRING("This won't compile!")); 128 } 129 130 Use :c:macro:`PW_TOKENIZE_STRING_EXPR` instead. 131 132Tokenize inside expressions 133=========================== 134An alternate set of macros are provided for use inside expressions. These make 135use of lambda functions, so while they can be used inside expressions, they 136require C++ and cannot be assigned to constexpr variables or be used with 137special function variables like ``__func__``. 138 139* :c:macro:`PW_TOKENIZE_STRING_EXPR` 140* :c:macro:`PW_TOKENIZE_STRING_DOMAIN_EXPR` 141* :c:macro:`PW_TOKENIZE_STRING_MASK_EXPR` 142 143.. admonition:: When to use these macros 144 145 Use :c:macro:`PW_TOKENIZE_STRING` and related macros to tokenize string 146 literals that do not need %-style arguments encoded. 147 148.. admonition:: **Yes**: Use :c:macro:`PW_TOKENIZE_STRING_EXPR` within other expressions. 149 :class: checkmark 150 151 .. code-block:: cpp 152 153 void GoodExample() { 154 ProcessToken(PW_TOKENIZE_STRING_EXPR("This will compile!")); 155 } 156 157.. admonition:: **No**: Assign :c:macro:`PW_TOKENIZE_STRING_EXPR` to a ``constexpr`` variable. 158 :class: error 159 160 .. code-block:: cpp 161 162 constexpr uint32_t wont_work = PW_TOKENIZE_STRING_EXPR("This won't compile!")); 163 164 Instead, use :c:macro:`PW_TOKENIZE_STRING` to assign to a ``constexpr`` variable. 165 166.. admonition:: **No**: Tokenize ``__func__`` in :c:macro:`PW_TOKENIZE_STRING_EXPR`. 167 :class: error 168 169 .. code-block:: cpp 170 171 void BadExample() { 172 // This compiles, but __func__ will not be the outer function's name, and 173 // there may be compiler warnings. 174 constexpr uint32_t wont_work = PW_TOKENIZE_STRING_EXPR(__func__); 175 } 176 177 Instead, use :c:macro:`PW_TOKENIZE_STRING` to tokenize ``__func__`` or similar macros. 178 179Tokenize a message with arguments to a buffer 180============================================= 181* :c:macro:`PW_TOKENIZE_TO_BUFFER` 182* :c:macro:`PW_TOKENIZE_TO_BUFFER_DOMAIN` 183* :c:macro:`PW_TOKENIZE_TO_BUFFER_MASK` 184 185.. admonition:: Why use this macro 186 187 - Encode a tokenized message for consumption within a function. 188 - Encode a tokenized message into an existing buffer. 189 190 Avoid using ``PW_TOKENIZE_TO_BUFFER`` in widely expanded macros, such as a 191 logging macro, because it will result in larger code size than passing the 192 tokenized data to a function. 193 194.. _module-pw_tokenizer-nested-arguments: 195 196Tokenize nested arguments 197========================= 198Encoding ``%s`` string arguments is inefficient, since ``%s`` strings are 199encoded 1:1, with no tokenization. Tokens can therefore be used to replace 200string arguments to tokenized format strings. 201 202* :c:macro:`PW_TOKEN_FMT` 203 204.. admonition:: Logging nested tokens 205 206 Users will typically interact with nested token arguments during logging. 207 In this case there is a slightly different interface described by 208 :ref:`module-pw_log-tokenized-args` that does not generally invoke 209 ``PW_TOKEN_FMT`` directly. 210 211The format specifier for a token is given by PRI-style macro ``PW_TOKEN_FMT()``, 212which is concatenated to the rest of the format string by the C preprocessor. 213 214.. code-block:: cpp 215 216 PW_TOKENIZE_FORMAT_STRING("margarine_domain", 217 UINT32_MAX, 218 "I can't believe it's not " PW_TOKEN_FMT() "!", 219 PW_TOKENIZE_STRING_EXPR("butter")); 220 221This feature is currently only supported by the Python detokenizer. 222 223Nested token format 224------------------- 225Nested tokens have the following format within strings: 226 227.. code-block:: 228 229 $[BASE#]TOKEN 230 231The ``$`` is a common prefix required for all nested tokens. It is possible to 232configure a different common prefix if necessary, but using the default ``$`` 233character is strongly recommended. 234 235The optional ``BASE`` defines the numeric base encoding of the token. Accepted 236values are 8, 10, 16, and 64. If the hash symbol ``#`` is used without 237specifying a number, the base is assumed to be 16. If the base option is 238omitted entirely, the base defaults to 64 for backward compatibility. All 239encodings except Base64 are not case sensitive. This may be expanded to support 240other bases in the future. 241 242Non-Base64 tokens are encoded strictly as 32-bit integers with padding. 243Base64 data may additionally encode string arguments for the detokenized token, 244and therefore does not have a maximum width. 245 246The meaning of ``TOKEN`` depends on the current phase of transformation for the 247current tokenized format string. Within the format string's entry in the token 248database, when the actual value of the token argument is not known, ``TOKEN`` is 249a printf argument specifier (e.g. ``%08x`` for a base-16 token with correct 250padding). The actual tokens that will be used as arguments have separate 251entries in the token database. 252 253After the top-level format string has been detokenized and formatted, ``TOKEN`` 254should be the value of the token argument in the specified base, with any 255necessary padding. This is the final format of a nested token if it cannot be 256tokenized. 257 258.. list-table:: Example tokens 259 :widths: 10 25 25 260 261 * - Base 262 - | Token database 263 | (within format string entry) 264 - Partially detokenized 265 * - 10 266 - ``$10#%010d`` 267 - ``$10#0086025943`` 268 * - 16 269 - ``$#%08x`` 270 - ``$#0000001A`` 271 * - 64 272 - ``%s`` 273 - ``$QA19pfEQ`` 274 275.. _module-pw_tokenizer-custom-macro: 276 277Tokenize a message with arguments in a custom macro 278=================================================== 279Projects can leverage the tokenization machinery in whichever way best suits 280their needs. The most efficient way to use ``pw_tokenizer`` is to pass tokenized 281data to a global handler function. A project's custom tokenization macro can 282handle tokenized data in a function of their choosing. The function may accept 283any arguments, but its final arguments must be: 284 285* The 32-bit token (:cpp:type:`pw_tokenizer_Token`) 286* The argument types (:cpp:type:`pw_tokenizer_ArgTypes`) 287* Variadic arguments, if any 288 289``pw_tokenizer`` provides two low-level macros to help projects create custom 290tokenization macros: 291 292* :c:macro:`PW_TOKENIZE_FORMAT_STRING` 293* :c:macro:`PW_TOKENIZER_REPLACE_FORMAT_STRING` 294 295.. caution:: 296 297 Note the spelling difference! The first macro begins with ``PW_TOKENIZE_`` 298 (no ``R``) whereas the second begins with ``PW_TOKENIZER_``. 299 300Use these macros to invoke an encoding function with the token, argument types, 301and variadic arguments. The function can then encode the tokenized message to a 302buffer using helpers in ``pw_tokenizer/encode_args.h``: 303 304.. Note: pw_tokenizer_EncodeArgs is a C function so you would expect to 305.. reference it as :c:func:`pw_tokenizer_EncodeArgs`. That doesn't work because 306.. it's defined in a header file that mixes C and C++. 307 308* :cpp:func:`pw::tokenizer::EncodeArgs` 309* :cpp:class:`pw::tokenizer::EncodedMessage` 310* :cpp:func:`pw_tokenizer_EncodeArgs` 311 312Example 313------- 314The following example implements a custom tokenization macro similar to 315:ref:`module-pw_log_tokenized`. 316 317.. code-block:: cpp 318 319 #include "pw_tokenizer/tokenize.h" 320 321 #ifndef __cplusplus 322 extern "C" { 323 #endif 324 325 void EncodeTokenizedMessage(uint32_t metadata, 326 pw_tokenizer_Token token, 327 pw_tokenizer_ArgTypes types, 328 ...); 329 330 #ifndef __cplusplus 331 } // extern "C" 332 #endif 333 334 #define PW_LOG_TOKENIZED_ENCODE_MESSAGE(metadata, format, ...) \ 335 do { \ 336 PW_TOKENIZE_FORMAT_STRING("logs", UINT32_MAX, format, __VA_ARGS__); \ 337 EncodeTokenizedMessage( \ 338 metadata, PW_TOKENIZER_REPLACE_FORMAT_STRING(__VA_ARGS__)); \ 339 } while (0) 340 341In this example, the ``EncodeTokenizedMessage`` function would handle encoding 342and processing the message. Encoding is done by the 343:cpp:class:`pw::tokenizer::EncodedMessage` class or 344:cpp:func:`pw::tokenizer::EncodeArgs` function from 345``pw_tokenizer/encode_args.h``. The encoded message can then be transmitted or 346stored as needed. 347 348.. code-block:: cpp 349 350 #include "pw_log_tokenized/log_tokenized.h" 351 #include "pw_tokenizer/encode_args.h" 352 353 void HandleTokenizedMessage(pw::log_tokenized::Metadata metadata, 354 pw::span<std::byte> message); 355 356 extern "C" void EncodeTokenizedMessage(const uint32_t metadata, 357 const pw_tokenizer_Token token, 358 const pw_tokenizer_ArgTypes types, 359 ...) { 360 va_list args; 361 va_start(args, types); 362 pw::tokenizer::EncodedMessage<kLogBufferSize> encoded_message(token, types, args); 363 va_end(args); 364 365 HandleTokenizedMessage(metadata, encoded_message); 366 } 367 368.. admonition:: Why use a custom macro 369 370 - Optimal code size. Invoking a free function with the tokenized data results 371 in the smallest possible call site. 372 - Pass additional arguments, such as metadata, with the tokenized message. 373 - Integrate ``pw_tokenizer`` with other systems. 374 375Tokenizing function names 376========================= 377The string literal tokenization functions support tokenizing string literals or 378constexpr character arrays (``constexpr const char[]``). In GCC and Clang, the 379special ``__func__`` variable and ``__PRETTY_FUNCTION__`` extension are declared 380as ``static constexpr char[]`` in C++ instead of the standard ``static const 381char[]``. This means that ``__func__`` and ``__PRETTY_FUNCTION__`` can be 382tokenized while compiling C++ with GCC or Clang. 383 384.. code-block:: cpp 385 386 // Tokenize the special function name variables. 387 constexpr uint32_t function = PW_TOKENIZE_STRING(__func__); 388 constexpr uint32_t pretty_function = PW_TOKENIZE_STRING(__PRETTY_FUNCTION__); 389 390Note that ``__func__`` and ``__PRETTY_FUNCTION__`` are not string literals. 391They are defined as static character arrays, so they cannot be implicitly 392concatentated with string literals. For example, ``printf(__func__ ": %d", 393123);`` will not compile. 394 395Calculate minimum required buffer size 396====================================== 397See :cpp:func:`pw::tokenizer::MinEncodingBufferSizeBytes`. 398 399.. _module-pw_tokenizer-base64-format: 400 401Encoding Base64 402=============== 403The tokenizer encodes messages to a compact binary representation. Applications 404may desire a textual representation of tokenized strings. This makes it easy to 405use tokenized messages alongside plain text messages, but comes at a small 406efficiency cost: encoded Base64 messages occupy about 4/3 (133%) as much memory 407as binary messages. 408 409The Base64 format is comprised of a ``$`` character followed by the 410Base64-encoded contents of the tokenized message. For example, consider 411tokenizing the string ``This is an example: %d!`` with the argument -1. The 412string's token is 0x4b016e66. 413 414.. code-block:: text 415 416 Source code: PW_LOG("This is an example: %d!", -1); 417 418 Plain text: This is an example: -1! [23 bytes] 419 420 Binary: 66 6e 01 4b 01 [ 5 bytes] 421 422 Base64: $Zm4BSwE= [ 9 bytes] 423 424To encode with the Base64 format, add a call to 425``pw::tokenizer::PrefixedBase64Encode`` or ``pw_tokenizer_PrefixedBase64Encode`` 426in the tokenizer handler function. For example, 427 428.. code-block:: cpp 429 430 void TokenizedMessageHandler(const uint8_t encoded_message[], 431 size_t size_bytes) { 432 pw::InlineBasicString base64 = pw::tokenizer::PrefixedBase64Encode( 433 pw::span(encoded_message, size_bytes)); 434 435 TransmitLogMessage(base64.data(), base64.size()); 436 } 437 438.. _module-pw_tokenizer-masks: 439 440Reduce token size with masking 441============================== 442``pw_tokenizer`` uses 32-bit tokens. On 32-bit or 64-bit architectures, using 443fewer than 32 bits does not improve runtime or code size efficiency. However, 444when tokens are packed into data structures or stored in arrays, the size of the 445token directly affects memory usage. In those cases, every bit counts, and it 446may be desireable to use fewer bits for the token. 447 448``pw_tokenizer`` allows users to provide a mask to apply to the token. This 449masked token is used in both the token database and the code. The masked token 450is not a masked version of the full 32-bit token, the masked token is the token. 451This makes it trivial to decode tokens that use fewer than 32 bits. 452 453Masking functionality is provided through the ``*_MASK`` versions of the macros: 454 455* :c:macro:`PW_TOKENIZE_STRING_MASK` 456* :c:macro:`PW_TOKENIZE_STRING_MASK_EXPR` 457* :c:macro:`PW_TOKENIZE_TO_BUFFER_MASK` 458 459For example, the following generates 16-bit tokens and packs them into an 460existing value. 461 462.. code-block:: cpp 463 464 constexpr uint32_t token = PW_TOKENIZE_STRING_MASK("domain", 0xFFFF, "Pigweed!"); 465 uint32_t packed_word = (other_bits << 16) | token; 466 467Tokens are hashes, so tokens of any size have a collision risk. The fewer bits 468used for tokens, the more likely two strings are to hash to the same token. See 469:ref:`module-pw_tokenizer-collisions`. 470 471Masked tokens without arguments may be encoded in fewer bytes. For example, the 47216-bit token ``0x1234`` may be encoded as two little-endian bytes (``34 12``) 473rather than four (``34 12 00 00``). The detokenizer tools zero-pad data smaller 474than four bytes. Tokens with arguments must always be encoded as four bytes. 475 476.. _module-pw_tokenizer-domains: 477 478Keep tokens from different sources separate with domains 479======================================================== 480``pw_tokenizer`` supports having multiple tokenization domains. Domains are a 481string label associated with each tokenized string. This allows projects to keep 482tokens from different sources separate. Potential use cases include the 483following: 484 485* Keep large sets of tokenized strings separate to avoid collisions. 486* Create a separate database for a small number of strings that use truncated 487 tokens, for example only 10 or 16 bits instead of the full 32 bits. 488 489If no domain is specified, the domain is empty (``""``). For many projects, this 490default domain is sufficient, so no additional configuration is required. 491 492.. code-block:: cpp 493 494 // Tokenizes this string to the default ("") domain. 495 PW_TOKENIZE_STRING("Hello, world!"); 496 497 // Tokenizes this string to the "my_custom_domain" domain. 498 PW_TOKENIZE_STRING_DOMAIN("my_custom_domain", "Hello, world!"); 499 500The database and detokenization command line tools default to reading from the 501default domain. The domain may be specified for ELF files by appending 502``#DOMAIN_NAME`` to the file path. Use ``#.*`` to read from all domains. For 503example, the following reads strings in ``some_domain`` from ``my_image.elf``. 504 505.. code-block:: sh 506 507 ./database.py create --database my_db.csv path/to/my_image.elf#some_domain 508 509See :ref:`module-pw_tokenizer-managing-token-databases` for information about 510the ``database.py`` command line tool. 511 512Limitations, bugs, and future work 513================================== 514 515.. _module-pw_tokenizer-gcc-template-bug: 516 517GCC bug: tokenization in template functions 518------------------------------------------- 519GCC releases prior to 14 incorrectly ignore the section attribute for template 520`functions <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70435>`_ and `variables 521<https://gcc.gnu.org/bugzilla/show_bug.cgi?id=88061>`_. The bug causes tokenized 522strings in template functions to be emitted into ``.rodata`` instead of the 523tokenized string section, so they cannot be extracted for detokenization. 524 525Fortunately, this is simple to work around in the linker script. 526``pw_tokenizer_linker_sections.ld`` includes a statement that pulls tokenized 527string entries from ``.rodata`` into the tokenized string section. See 528`b/321306079 <https://issues.pigweed.dev/issues/321306079>`_ for details. 529 530If tokenization is working, but strings in templates are not appearing in token 531databases, check the following: 532 533- The full contents of the latest version of ``pw_tokenizer_linker_sections.ld`` 534 are included with the linker script. The linker script was updated in 535 `pwrev.dev/188424 <http://pwrev.dev/188424>`_. 536- The ``-fdata-sections`` compilation option is in use. This places each 537 variable in its own section, which is necessary for pulling tokenized string 538 entries from ``.rodata`` into the proper section. 539 54064-bit tokenization 541------------------- 542The Python and C++ detokenizing libraries currently assume that strings were 543tokenized on a system with 32-bit ``long``, ``size_t``, ``intptr_t``, and 544``ptrdiff_t``. Decoding may not work correctly for these types if a 64-bit 545device performed the tokenization. 546 547Supporting detokenization of strings tokenized on 64-bit targets would be 548simple. This could be done by adding an option to switch the 32-bit types to 54964-bit. The tokenizer stores the sizes of these types in the 550``.pw_tokenizer.info`` ELF section, so the sizes of these types can be verified 551by checking the ELF file, if necessary. 552 553Tokenization in headers 554----------------------- 555Tokenizing code in header files (inline functions or templates) may trigger 556warnings such as ``-Wlto-type-mismatch`` under certain conditions. That 557is because tokenization requires declaring a character array for each tokenized 558string. If the tokenized string includes macros that change value, the size of 559this character array changes, which means the same static variable is defined 560with different sizes. It should be safe to suppress these warnings, but, when 561possible, code that tokenizes strings with macros that can change value should 562be moved to source files rather than headers. 563 564---------------------- 565Tokenization in Python 566---------------------- 567The Python ``pw_tokenizer.encode`` module has limited support for encoding 568tokenized messages with the :func:`pw_tokenizer.encode.encode_token_and_args` 569function. This function requires a string's token is already calculated. 570Typically these tokens are provided by a database, but they can be manually 571created using the tokenizer hash. 572 573:func:`pw_tokenizer.tokens.pw_tokenizer_65599_hash` is particularly useful 574for offline token database generation in cases where tokenized strings in a 575binary cannot be embedded as parsable pw_tokenizer entries. 576 577.. note:: 578 In C, the hash length of a string has a fixed limit controlled by 579 ``PW_TOKENIZER_CFG_C_HASH_LENGTH``. To match tokens produced by C (as opposed 580 to C++) code, ``pw_tokenizer_65599_hash()`` should be called with a matching 581 hash length limit. When creating an offline database, it's a good idea to 582 generate tokens for both, and merge the databases. 583 584.. _module-pw_tokenizer-cli-encoding: 585 586----------------- 587Encoding CLI tool 588----------------- 589The ``pw_tokenizer.encode`` command line tool can be used to encode 590format strings and optional arguments. 591 592.. code-block:: bash 593 594 python -m pw_tokenizer.encode [-h] FORMAT_STRING [ARG ...] 595 596Example: 597 598.. code-block:: text 599 600 $ python -m pw_tokenizer.encode "There's... %d many of %s!" 2 them 601 Raw input: "There's... %d many of %s!" % (2, 'them') 602 Formatted input: There's... 2 many of them! 603 Token: 0xb6ef8b2d 604 Encoded: b'-\x8b\xef\xb6\x04\x04them' (2d 8b ef b6 04 04 74 68 65 6d) [10 bytes] 605 Prefixed Base64: $LYvvtgQEdGhlbQ== 606 607See ``--help`` for full usage details. 608 609-------- 610Appendix 611-------- 612 613Case study 614========== 615.. note:: This section discusses the implementation, results, and lessons 616 learned from a real-world deployment of ``pw_tokenizer``. 617 618The tokenizer module was developed to bring tokenized logging to an 619in-development product. The product already had an established text-based 620logging system. Deploying tokenization was straightforward and had substantial 621benefits. 622 623Results 624------- 625* Log contents shrunk by over 50%, even with Base64 encoding. 626 627 * Significant size savings for encoded logs, even using the less-efficient 628 Base64 encoding required for compatibility with the existing log system. 629 * Freed valuable communication bandwidth. 630 * Allowed storing many more logs in crash dumps. 631 632* Substantial flash savings. 633 634 * Reduced the size firmware images by up to 18%. 635 636* Simpler logging code. 637 638 * Removed CPU-heavy ``snprintf`` calls. 639 * Removed complex code for forwarding log arguments to a low-priority task. 640 641This section describes the tokenizer deployment process and highlights key 642insights. 643 644Firmware deployment 645------------------- 646* In the project's logging macro, calls to the underlying logging function were 647 replaced with a tokenized log macro invocation. 648* The log level was passed as the payload argument to facilitate runtime log 649 level control. 650* For this project, it was necessary to encode the log messages as text. In 651 the handler function the log messages were encoded in the $-prefixed 652 :ref:`module-pw_tokenizer-base64-format`, then dispatched as normal log messages. 653* Asserts were tokenized a callback-based API that has been removed (a 654 :ref:`custom macro <module-pw_tokenizer-custom-macro>` is a better 655 alternative). 656 657.. attention:: 658 Do not encode line numbers in tokenized strings. This results in a huge 659 number of lines being added to the database, since every time code moves, 660 new strings are tokenized. If :ref:`module-pw_log_tokenized` is used, line 661 numbers are encoded in the log metadata. Line numbers may also be included by 662 by adding ``"%d"`` to the format string and passing ``__LINE__``. 663 664.. _module-pw_tokenizer-database-management: 665 666Database management 667------------------- 668* The token database was stored as a CSV file in the project's Git repo. 669* The token database was automatically updated as part of the build, and 670 developers were expected to check in the database changes alongside their code 671 changes. 672* A presubmit check verified that all strings added by a change were added to 673 the token database. 674* The token database included logs and asserts for all firmware images in the 675 project. 676* No strings were purged from the token database. 677 678.. tip:: 679 Merge conflicts may be a frequent occurrence with an in-source CSV database. 680 Use the :ref:`module-pw_tokenizer-directory-database-format` instead. 681 682Decoding tooling deployment 683--------------------------- 684* The Python detokenizer in ``pw_tokenizer`` was deployed to two places: 685 686 * Product-specific Python command line tools, using 687 ``pw_tokenizer.Detokenizer``. 688 * Standalone script for decoding prefixed Base64 tokens in files or 689 live output (e.g. from ``adb``), using ``detokenize.py``'s command line 690 interface. 691 692* The C++ detokenizer library was deployed to two Android apps with a Java 693 Native Interface (JNI) layer. 694 695 * The binary token database was included as a raw resource in the APK. 696 * In one app, the built-in token database could be overridden by copying a 697 file to the phone. 698 699.. tip:: 700 Make the tokenized logging tools simple to use for your project. 701 702 * Provide simple wrapper shell scripts that fill in arguments for the 703 project. For example, point ``detokenize.py`` to the project's token 704 databases. 705 * Use ``pw_tokenizer.AutoUpdatingDetokenizer`` to decode in 706 continuously-running tools, so that users don't have to restart the tool 707 when the token database updates. 708 * Integrate detokenization everywhere it is needed. Integrating the tools 709 takes just a few lines of code, and token databases can be embedded in APKs 710 or binaries. 711