OpenHarmony htrace file parser & cold-start analyzer
Project description
ohos-htrace
Pure-Python parser & cold-start analyzer for OpenHarmony
.htracetrace files.
No external binaries needed โ replaces the C++ trace_streamer tool with a native Python implementation that parses .htrace protobuf data directly into an in-memory SQLite database.
โจ Features
- ๐ Pure Python โ no
streamer.exedependency, works on any platform - โก Fast โ parses 120MB htrace in ~3 seconds
- ๐ฏ Cold-start analysis โ automatic detection of app launch timing
- ๐ Keyword breakdown โ measure time spent in specific functions
- ๐ง CLI & API โ use from command line or import in your Python code
- ๐ฆ pip installable โ
pip install ohos-htrace
๐ฆ Installation
pip install ohos-htrace
Or install from source:
git clone https://github.com/p2003722/ohos-htrace.git
cd ohos-htrace
pip install -e .
๐ Quick Start
Command Line
# Basic cold-start analysis
ohos-htrace trace.htrace com.example.app
# With keyword duration breakdown
ohos-htrace trace.htrace aweme -k AppSpawn,LaunchAbility,LoadModule
# JSON output (for scripting)
ohos-htrace trace.htrace aweme --json
# Verbose output with top-10 functions
ohos-htrace trace.htrace aweme -k AppSpawn,LaunchAbility -v
Example output:
Parsing trace.htrace (118.7 MB) ...
Done in 3.6s
Process: ss.hm.ugc.aweme (pid=4575)
Cold Start: 2566.08 ms
Callstack: 69,933 nodes
Depth-0: 16,916 blocks
Max Gap: 27.9 ms
Frames: 0
Keyword Durations:
AppSpawn: 19.12ms (5 hits)
LaunchAbility: 3175.94ms (2 hits)
LoadModule: 1164.25ms (3 hits)
Python API
import ohos_htrace
# One-shot analysis
result = ohos_htrace.parse_and_analyze(
"trace.htrace",
"aweme",
keywords=["AppSpawn", "LaunchAbility", "LoadModule"],
)
print(f"Cold start: {result['cold_start_ms']:.2f}ms")
print(f"Max gap: {result['max_gap_ms']:.1f}ms")
for kw, info in result["keyword_durations"]["by_keyword"].items():
print(f" {kw}: {info['total_ms']:.2f}ms ({info['count']} hits)")
Low-Level API
import ohos_htrace
# Step 1: Parse htrace โ in-memory SQLite
conn = ohos_htrace.parse_to_memory_db("trace.htrace")
# Step 2: Query like a normal database
cur = conn.cursor()
cur.execute("SELECT name, pid FROM process")
for name, pid in cur.fetchall():
print(f" {name} (pid={pid})")
# Step 3: Use analysis functions
procs = ohos_htrace.find_matching_processes(conn, "aweme")
for proc in procs:
tree = ohos_htrace.get_process_callstack(conn, proc["id"])
cold_ms, gaps = ohos_htrace.compute_cold_start_time(tree)
print(f" {proc['name']}: {cold_ms:.2f}ms")
conn.close()
๐ Database Schema
The in-memory database mirrors the C++ trace_streamer output:
| Table | Columns | Description |
|---|---|---|
process |
id, name, pid | Process list |
thread |
id, ipid, name, tid | Thread list (ipid โ process.id) |
callstack |
id, callid, parent_id, name, depth, ts, dur | Function call stack (callid โ thread.id) |
frame_slice |
id, vsync, ts, dur, type_desc, flag, ipid | Frame rendering data |
๐ How It Works
.htrace file
โ
โโ 1024-byte Header (magic: OHOSPROF)
โ
โโ Segments: [4-byte length][protobuf payload]
โ
โโ ProfilerPluginData
โ
โโ ftrace-plugin โ TracePluginResult
โ
โโ ftrace_cpu_detail[].event[]
โ โโ print_format (B|E|S|F events โ callstack)
โ โโ task_rename_format (โ process names)
โ โโ sched_switch_format (โ thread names)
โ
โโ comm_dict[] (โ thread name resolution)
Key implementation details:
- tgid extraction from buf โ Uses the tgid from
B|<tgid>|<name>(notevent.tgid), matching C++ streamer behavior. This is critical for correct process attribution afterfork(). - Event sorting โ Events from different CPU cores are sorted by
(tgid, pid, ts)before B/E stack matching, preventing cross-core timing artifacts. - Async events โ Supports
S|F(async start/finish) events with cookie-based pairing.
๐ Comparison with trace_streamer (C++)
Tested on a 118.7MB htrace file:
| Metric | trace_streamer | ohos-htrace | Match |
|---|---|---|---|
| Cold Start | 2566.08 ms | 2566.08 ms | โ |
| Max Gap | 27.9 ms | 27.9 ms | โ |
| AppSpawn | 19.12ms (5) | 19.12ms (5) | โ |
| LaunchAbility | 3175.94ms (2) | 3175.94ms (2) | โ |
| JsAbilityStage | 1575.63ms (5) | 1575.63ms (5) | โ |
| LoadModule | 1164.25ms (3) | 1164.25ms (3) | โ |
| Parse Time | 4.6s | 3.6s | โก faster |
| Callstack | 71,114 | 69,933 | ~98% (binder events) |
Remaining differences: binder transaction events (~1,181 entries) and frame_slice data (from hidump-plugin, not yet parsed). These do not affect cold-start analysis accuracy.
๐ Requirements
- Python โฅ 3.10
- protobuf โฅ 4.21.0
๐ License
Apache License 2.0
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file ohos_htrace-0.1.1.tar.gz.
File metadata
- Download URL: ohos_htrace-0.1.1.tar.gz
- Upload date:
- Size: 47.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5105e9a1d8c870124431945409d47bb8222b67b08ca2a9fb77f75c1f99ed573
|
|
| MD5 |
2ad12938bd044a41e1210889e2f9b2ba
|
|
| BLAKE2b-256 |
e0f66f506a7022ecb6ebc8e6e912c76ff46ebebabc6b71a9be543489be25093e
|
Provenance
The following attestation bundles were made for ohos_htrace-0.1.1.tar.gz:
Publisher:
publish.yml on p2003722/ohos-htrace
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ohos_htrace-0.1.1.tar.gz -
Subject digest:
a5105e9a1d8c870124431945409d47bb8222b67b08ca2a9fb77f75c1f99ed573 - Sigstore transparency entry: 1438992448
- Sigstore integration time:
-
Permalink:
p2003722/ohos-htrace@407d3a0828503efc5b1907c66b180e3668d5af29 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/p2003722
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@407d3a0828503efc5b1907c66b180e3668d5af29 -
Trigger Event:
push
-
Statement type:
File details
Details for the file ohos_htrace-0.1.1-py3-none-any.whl.
File metadata
- Download URL: ohos_htrace-0.1.1-py3-none-any.whl
- Upload date:
- Size: 71.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7f20039133c6418bce79655187eb49312e63212bc8492a86171be84db46cc571
|
|
| MD5 |
180989e43e01c3559d907413aa9fbb9d
|
|
| BLAKE2b-256 |
20a7bd62f6d80983f3f39ea154adaf72ee940f0eaf5c89e02d84a3139f5926e1
|
Provenance
The following attestation bundles were made for ohos_htrace-0.1.1-py3-none-any.whl:
Publisher:
publish.yml on p2003722/ohos-htrace
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
ohos_htrace-0.1.1-py3-none-any.whl -
Subject digest:
7f20039133c6418bce79655187eb49312e63212bc8492a86171be84db46cc571 - Sigstore transparency entry: 1438992467
- Sigstore integration time:
-
Permalink:
p2003722/ohos-htrace@407d3a0828503efc5b1907c66b180e3668d5af29 -
Branch / Tag:
refs/tags/v0.1.1 - Owner: https://github.com/p2003722
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish.yml@407d3a0828503efc5b1907c66b180e3668d5af29 -
Trigger Event:
push
-
Statement type: