12e5b6d6dSopenharmony_ci---
22e5b6d6dSopenharmony_cilayout: default
32e5b6d6dSopenharmony_cititle: Resource and Data Tracing
42e5b6d6dSopenharmony_cinav_order: 2
52e5b6d6dSopenharmony_ciparent: ICU Data
62e5b6d6dSopenharmony_ci---
72e5b6d6dSopenharmony_ci<!--
82e5b6d6dSopenharmony_ci© 2019 and later: Unicode, Inc. and others.
92e5b6d6dSopenharmony_ciLicense & terms of use: http://www.unicode.org/copyright.html
102e5b6d6dSopenharmony_ci-->
112e5b6d6dSopenharmony_ci
122e5b6d6dSopenharmony_ci# Resource and Data Tracing
132e5b6d6dSopenharmony_ci{: .no_toc }
142e5b6d6dSopenharmony_ci
152e5b6d6dSopenharmony_ci## Contents
162e5b6d6dSopenharmony_ci{: .no_toc .text-delta }
172e5b6d6dSopenharmony_ci
182e5b6d6dSopenharmony_ci1. TOC
192e5b6d6dSopenharmony_ci{:toc}
202e5b6d6dSopenharmony_ci
212e5b6d6dSopenharmony_ci---
222e5b6d6dSopenharmony_ci
232e5b6d6dSopenharmony_ci## Overview
242e5b6d6dSopenharmony_ci
252e5b6d6dSopenharmony_ciWhen building an [ICU data filter specification](buildtool.md), it is useful to
262e5b6d6dSopenharmony_cisee what resources are being used by your application so that you can select
272e5b6d6dSopenharmony_cithose resources and discard the others. This guide describes how to use
282e5b6d6dSopenharmony_ci*utrace.h* to inspect resource access in real time in ICU4C.
292e5b6d6dSopenharmony_ci
302e5b6d6dSopenharmony_ci**Note:** This feature is only available in ICU4C at this time. If you are
312e5b6d6dSopenharmony_ciinterested in ICU4J, please see
322e5b6d6dSopenharmony_ci[ICU-20656](https://unicode-org.atlassian.net/browse/ICU-20656).
332e5b6d6dSopenharmony_ci
342e5b6d6dSopenharmony_ci## Quick Start
352e5b6d6dSopenharmony_ci
362e5b6d6dSopenharmony_ciFirst, you *must* have a copy of ICU4C configured with tracing enabled.
372e5b6d6dSopenharmony_ci
382e5b6d6dSopenharmony_ci    $ ./runConfigureICU Linux --enable-tracing
392e5b6d6dSopenharmony_ci
402e5b6d6dSopenharmony_ciThe following program prints resource and data usages to standard out:
412e5b6d6dSopenharmony_ci
422e5b6d6dSopenharmony_ci```cpp
432e5b6d6dSopenharmony_ci#include "unicode/brkiter.h"
442e5b6d6dSopenharmony_ci#include "unicode/errorcode.h"
452e5b6d6dSopenharmony_ci#include "unicode/localpointer.h"
462e5b6d6dSopenharmony_ci#include "unicode/utrace.h"
472e5b6d6dSopenharmony_ci
482e5b6d6dSopenharmony_ci#include <iostream>
492e5b6d6dSopenharmony_ci
502e5b6d6dSopenharmony_cistatic void U_CALLCONV traceData(
512e5b6d6dSopenharmony_ci        const void *context,
522e5b6d6dSopenharmony_ci        int32_t fnNumber,
532e5b6d6dSopenharmony_ci        int32_t level,
542e5b6d6dSopenharmony_ci        const char *fmt,
552e5b6d6dSopenharmony_ci        va_list args) {
562e5b6d6dSopenharmony_ci    char        buf[1000];
572e5b6d6dSopenharmony_ci    const char *fnName;
582e5b6d6dSopenharmony_ci
592e5b6d6dSopenharmony_ci    fnName = utrace_functionName(fnNumber);
602e5b6d6dSopenharmony_ci    utrace_vformat(buf, sizeof(buf), 0, fmt, args);
612e5b6d6dSopenharmony_ci    std::cout << fnName << " " << buf << std::endl;
622e5b6d6dSopenharmony_ci}
632e5b6d6dSopenharmony_ci
642e5b6d6dSopenharmony_ciint main() {
652e5b6d6dSopenharmony_ci    icu::ErrorCode status;
662e5b6d6dSopenharmony_ci
672e5b6d6dSopenharmony_ci    const void* context = nullptr;
682e5b6d6dSopenharmony_ci    utrace_setFunctions(context, nullptr, nullptr, traceData);
692e5b6d6dSopenharmony_ci    utrace_setLevel(UTRACE_VERBOSE);
702e5b6d6dSopenharmony_ci
712e5b6d6dSopenharmony_ci    // Create a new BreakIterator
722e5b6d6dSopenharmony_ci    icu::LocalPointer<icu::BreakIterator> brkitr(
732e5b6d6dSopenharmony_ci        icu::BreakIterator::createWordInstance("zh-CN", status));
742e5b6d6dSopenharmony_ci}
752e5b6d6dSopenharmony_ci```
762e5b6d6dSopenharmony_ci
772e5b6d6dSopenharmony_ciThe following output is produced from this program:
782e5b6d6dSopenharmony_ci
792e5b6d6dSopenharmony_ci    res-open icudt64l-brkitr/zh_CN.res
802e5b6d6dSopenharmony_ci    res-open icudt64l-brkitr/zh.res
812e5b6d6dSopenharmony_ci    res-open icudt64l-brkitr/root.res
822e5b6d6dSopenharmony_ci    bundle-open icudt64l-brkitr/zh.res
832e5b6d6dSopenharmony_ci    resc       (get) icudt64l-brkitr/zh.res @ /boundaries
842e5b6d6dSopenharmony_ci    resc       (get) icudt64l-brkitr/root.res @ /boundaries/word
852e5b6d6dSopenharmony_ci    resc    (string) icudt64l-brkitr/root.res @ /boundaries/word
862e5b6d6dSopenharmony_ci    file-open icudt64l-brkitr/word.brk
872e5b6d6dSopenharmony_ci
882e5b6d6dSopenharmony_ciWhat this means:
892e5b6d6dSopenharmony_ci
902e5b6d6dSopenharmony_ci1. The BreakIterator constructor opened three resource files in the locale
912e5b6d6dSopenharmony_ci   fallback chain for zh_CN. The actual bundle was opened for zh.
922e5b6d6dSopenharmony_ci2. One string was read from that resource bundle: the one at the resource path
932e5b6d6dSopenharmony_ci   "/boundaries/word" in brkitr/root.res.
942e5b6d6dSopenharmony_ci3. In addition, the binary data file brkitr/word.brk was opened.
952e5b6d6dSopenharmony_ci
962e5b6d6dSopenharmony_ciBased on that information, you can make a more informed decision when writing
972e5b6d6dSopenharmony_ciresource filter rules for this simple program.
982e5b6d6dSopenharmony_ci
992e5b6d6dSopenharmony_ci## Data Tracing API
1002e5b6d6dSopenharmony_ci
1012e5b6d6dSopenharmony_ciThe `traceData` function shown above takes five arguments. The following two
1022e5b6d6dSopenharmony_ciare most important for data tracing:
1032e5b6d6dSopenharmony_ci
1042e5b6d6dSopenharmony_ci- `fnNumber` indicates what type of data access this is.
1052e5b6d6dSopenharmony_ci- `args` contains the details on which resources were accessed.
1062e5b6d6dSopenharmony_ci
1072e5b6d6dSopenharmony_ci**Important:** When reading from `args`, the strings are valid only within the
1082e5b6d6dSopenharmony_ciscope of your `traceData` function. You should make copies of the strings if
1092e5b6d6dSopenharmony_ciyou intend to save them for further processing.
1102e5b6d6dSopenharmony_ci
1112e5b6d6dSopenharmony_ci### UTRACE_UDATA_RESOURCE
1122e5b6d6dSopenharmony_ci
1132e5b6d6dSopenharmony_ciUTRACE_UDATA_RESOURCE is used to indicate that a value inside of a resource
1142e5b6d6dSopenharmony_cibundle was read by ICU code.
1152e5b6d6dSopenharmony_ci
1162e5b6d6dSopenharmony_ciWhen `fnNumber` is `UTRACE_UDATA_RESOURCE`, there are three C-style strings in
1172e5b6d6dSopenharmony_ci`args`:
1182e5b6d6dSopenharmony_ci
1192e5b6d6dSopenharmony_ci1. Data type; not usually relevant for the purpose of resource filtering.
1202e5b6d6dSopenharmony_ci2. The internal path of the resource file from which the value was read.
1212e5b6d6dSopenharmony_ci3. The path to the value within that resource file.
1222e5b6d6dSopenharmony_ci
1232e5b6d6dSopenharmony_ciTo read each of these into different variables, you can write the code,
1242e5b6d6dSopenharmony_ci
1252e5b6d6dSopenharmony_ci```cpp
1262e5b6d6dSopenharmony_ciconst char* dataType = va_arg(args, const char*);
1272e5b6d6dSopenharmony_ciconst char* filePath = va_arg(args, const char*);
1282e5b6d6dSopenharmony_ciconst char* resPath = va_arg(args, const char*);
1292e5b6d6dSopenharmony_ci```
1302e5b6d6dSopenharmony_ci
1312e5b6d6dSopenharmony_ciAs stated above, you should copy the strings if you intend to save them. The
1322e5b6d6dSopenharmony_cipointers will not be valid after the tracing function returns.
1332e5b6d6dSopenharmony_ci
1342e5b6d6dSopenharmony_ci### UTRACE_UDATA_BUNDLE
1352e5b6d6dSopenharmony_ci
1362e5b6d6dSopenharmony_ciUTRACE_UDATA_BUNDLE is used to indicate that a resource bundle was opened by
1372e5b6d6dSopenharmony_ciICU code.
1382e5b6d6dSopenharmony_ci
1392e5b6d6dSopenharmony_ciFor the purposes of making your ICU data filter, the specific resource paths
1402e5b6d6dSopenharmony_ciprovided by UTRACE_UDATA_RESOURCE are more precise and useful.
1412e5b6d6dSopenharmony_ci
1422e5b6d6dSopenharmony_ci### UTRACE_UDATA_DATA_FILE
1432e5b6d6dSopenharmony_ci
1442e5b6d6dSopenharmony_ciUTRACE_UDATA_DATA_FILE is used to indicate that a non-resource-bundle binary
1452e5b6d6dSopenharmony_cidata file was opened by ICU code. Such files are used for break iteration,
1462e5b6d6dSopenharmony_ciconversion, confusables, and a handful of other ICU services.
1472e5b6d6dSopenharmony_ci
1482e5b6d6dSopenharmony_ci### UTRACE_UDATA_RES_FILE
1492e5b6d6dSopenharmony_ci
1502e5b6d6dSopenharmony_ciUTRACE_UDATA_RES_FILE is used to indicate that a binary resource bundle file
1512e5b6d6dSopenharmony_ciwas opened by ICU code. This can be helpful to debug locale fallbacks. This
1522e5b6d6dSopenharmony_cidiffers from UTRACE_UDATA_BUNDLE because the resource *file* is typically
1532e5b6d6dSopenharmony_ciopened only once per application runtime.
1542e5b6d6dSopenharmony_ci
1552e5b6d6dSopenharmony_ciFor the purposes of making your ICU data filter, the specific resource paths
1562e5b6d6dSopenharmony_ciprovided by UTRACE_UDATA_RESOURCE are more precise and useful.
157