<commit_before># Running Perfetto

In order to run Perfetto and get a meaningful trace you need to build
(see [build instructions](build_instructions.md)) and run the following:

`traced`: The unprivileged trace daemon that owns the log buffers and maintains
a registry of Producers and Consumers connected.

`traced_probes`: The privileged daemon that has access to the Kernel tracefs
(typically mounted under `/sys/kernel/debug/tracing`), can drive
[Ftrace](https://source.android.com/devices/tech/debug/ftrace) and writes its
output into `traced`.

`perfetto`: A command line utility client that drive the trace and save back
the results (either to a file or to [Android's Dropbox][dropbox])


## Instructions:
```
# TODO(primiano): this is temporary until we fix SELinux policies.
adb shell su root setenforce 0

adb shell su root start traced
adb shell su root start traced_probes
```

If this works you will see something like:

```
$ adb logcat -s perfetto
perfetto: service.cc:45 Started traced, listening on /dev/socket/traced_producer /dev/socket/traced_consumer
perfetto: probes.cc:25 Starting /system/bin/traced_probes service
perfetto: probes_producer.cc:32 Connected to the service
```

At which point you can grab a trace by doing:

```
$ adb shell perfetto --config :test --out /data/local/tmp/trace
```

or to save it to [Android's Dropbox][dropbox]:

```
$ adb shell perfetto --config :test --dropbox perfetto
```

`--config :test` uses a hard-coded test trace config. It is possible to pass
an arbitrary trace config by doing the following:
```
cat > /tmp/config.txpb <<EOF
# This is a text-encoded protobuf for /protos/perfetto/config/trace_config.proto
duration_ms: 2000
buffers {
  size_kb: 1024
  optimize_for: ONE_SHOT_READ
  fill_policy: RING_BUFFER
}
data_sources {
  config {
    name: "com.google.perfetto.ftrace"
    target_buffer: 0
    ftrace_config {
      event_names: "sched_switch"
    }
  }
}
EOF

protoc=$(pwd)/out/android/gcc_like_host/protoc

$protoc --encode=perfetto.protos.TraceConfig \
        -I$(pwd)/external/perfetto/protos \
        $(pwd)/external/perfetto/protos/perfetto/config/trace_config.proto \
        < /tmp/config.txpb \
        > /tmp/config.pb

adb push /tmp/config.pb /data/local/tmp/
adb shell perfetto -c /data/local/tmp/config.pb -o /data/local/tmp/trace.pb
adb pull /data/local/tmp/trace.pb /tmp/
out/android/trace_to_text systrace < /tmp/trace.pb > /tmp/trace.json

# The file can now be viewed in chrome://tracing
```

[dropbox]: https://developer.android.com/reference/android/os/DropBoxManager.html<commit_msg>Update docs am: e3724161c2 am: 260f8851cc am: 6b107693dc<commit_after># Running Perfetto

In order to run Perfetto and get a meaningful trace you need to build
(see [build instructions](build_instructions.md)) and run the following:

`traced`: The unprivileged trace daemon that owns the log buffers and maintains
a registry of Producers and Consumers connected.

`traced_probes`: The privileged daemon that has access to the Kernel tracefs
(typically mounted under `/sys/kernel/debug/tracing`), can drive
[Ftrace](https://source.android.com/devices/tech/debug/ftrace) and writes its
output into `traced`.

`perfetto`: A command line utility client that drive the trace and save back
the results (either to a file or to [Android's Dropbox][dropbox])


## Instructions:
Make sure that Perfetto daemons (`traced` / `traced_probes`) are started.
They are enabled by default on Pixel and Pixel 2 (walleye, taimen, marlin,
sailfish). On other devices start them manually by doing:
```
adb shell setprop persist.traced.enable 1
```

If this works you will see something like:

```
$ adb logcat -s perfetto
perfetto: service.cc:45 Started traced, listening on /dev/socket/traced_producer /dev/socket/traced_consumer
perfetto: probes.cc:25 Starting /system/bin/traced_probes service
perfetto: probes_producer.cc:32 Connected to the service
```

At which point you can grab a trace by doing:

```
$ adb shell perfetto --config :test --out /data/misc/perfetto-traces/trace
```

Note: If the output file is not under `/data/misc/perfetto-traces`, it might
fail due to SELinux (shell is allowed to read that directory).

Alternatively, it can be saved to [Android's Dropbox][dropbox]
(to test the uploader):

```
$ adb shell perfetto --config :test --dropbox perfetto
```

### Trace config
`--config :test` uses a hard-coded test trace config. It is possible to pass
an arbitrary trace config by doing the following:
```
cat > /tmp/config.txpb <<EOF
# This is a text-encoded protobuf for /protos/perfetto/config/trace_config.proto
duration_ms: 10000

buffers {
  size_kb: 10240
}

data_sources {
  config {
    name: "com.google.perfetto.ftrace"
    target_buffer: 0
    ftrace_config {
      buffer_size_kb: 40 # Kernel ftrace buffer size.
      ftrace_events: "sched_switch"
      ftrace_events: "print"
    }
  }
}

data_sources {
  config {
    name: "com.google.perfetto.process_stats"
    target_buffer: 0
  }
}
EOF

protoc=$(pwd)/out/android/gcc_like_host/protoc

$protoc --encode=perfetto.protos.TraceConfig \
        -I$(pwd)/external/perfetto/protos \
        $(pwd)/external/perfetto/protos/perfetto/config/perfetto_config.proto \
        < /tmp/config.txpb \
        > /tmp/config.pb

cat /tmp/config.pb | adb shell perfetto -c - -o /data/misc/perfetto-traces/trace.pb
adb pull /data/misc/perfetto-traces/trace.pb /tmp/
out/android/trace_to_text systrace < /tmp/trace.pb > /tmp/trace.json

# The file can now be viewed in chrome://tracing
```

[dropbox]: https://developer.android.com/reference/android/os/DropBoxManager.html
{'fraction_non_alphanumeric': 0.10617416131334761, 'fraction_numerical': 0.010706638115631691, 'mean_word_length': 4.0134168157423975, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 9, 'https://': 4, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1325278', 'n_tokens_mistral': 1921, 'n_tokens_neox': 1826, 'n_words': 583}