1 // Copyright (C) 2023 The Android Open Source Project
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 //! Usage sample for libatrace_rust.
16
17 use std::thread::JoinHandle;
18
19 use atrace::AtraceTag;
20
spawn_async_event() -> JoinHandle<()>21 fn spawn_async_event() -> JoinHandle<()> {
22 // Unlike normal events, async events don't need to be nested.
23 // You need to use the same name and cookie (the last arg) to close the event.
24 // The cookie must be unique on the name level.
25 let unique_cookie = 12345;
26 atrace::atrace_async_begin(AtraceTag::App, "Async task", unique_cookie);
27 std::thread::spawn(move || {
28 std::thread::sleep(std::time::Duration::from_millis(500));
29 atrace::atrace_async_end(AtraceTag::App, "Async task", unique_cookie);
30 })
31 }
32
spawn_async_event_with_track() -> JoinHandle<()>33 fn spawn_async_event_with_track() -> JoinHandle<()> {
34 // Same as `atrace_async_begin` but per track.
35 // Track name (not event name) and cookie are used to close the event.
36 // The cookie must be unique on the track level.
37 let unique_cookie = 12345;
38 atrace::atrace_async_for_track_begin(
39 AtraceTag::App,
40 "Async track",
41 "Task with track",
42 unique_cookie,
43 );
44 std::thread::spawn(move || {
45 std::thread::sleep(std::time::Duration::from_millis(600));
46 atrace::atrace_async_for_track_end(AtraceTag::App, "Async track", unique_cookie);
47 })
48 }
49
spawn_counter_thread() -> JoinHandle<()>50 fn spawn_counter_thread() -> JoinHandle<()> {
51 std::thread::spawn(|| {
52 for i in 1..=10 {
53 std::thread::sleep(std::time::Duration::from_millis(100));
54 // Counter events are available for int and int64 to trace values.
55 atrace::atrace_int(AtraceTag::App, "Count of i", i);
56 }
57 })
58 }
59
main()60 fn main() {
61 // This macro will create a scoped event with the function name used as the event name.
62 atrace::trace_method!(AtraceTag::App);
63
64 // The scoped event will be ended when the returned guard is dropped.
65 let _scoped_event = atrace::begin_scoped_event(AtraceTag::App, "Example main");
66
67 // Methods starting with atrace_* are direct wrappers of libcutils methods.
68 let enabled_tags = atrace::atrace_get_enabled_tags();
69 println!("Enabled tags: {:?}", enabled_tags);
70
71 println!("Spawning async trace events");
72 let async_event_handler = spawn_async_event();
73 let async_event_with_track_handler = spawn_async_event_with_track();
74 let counter_thread_handler = spawn_counter_thread();
75
76 // Instant events have no duration and don't need to be closed.
77 atrace::atrace_instant(AtraceTag::App, "Instant event");
78
79 println!("Calling atrace_begin and sleeping for 1 sec...");
80 // If you begin an event you need to close it with the same tag. If you're calling begin
81 // manually make sure you have a matching end. Or just use a scoped event.
82 atrace::atrace_begin(AtraceTag::App, "Hello tracing!");
83 std::thread::sleep(std::time::Duration::from_secs(1));
84 atrace::atrace_end(AtraceTag::App);
85
86 println!("Joining async events...");
87 async_event_handler.join().unwrap();
88 async_event_with_track_handler.join().unwrap();
89 counter_thread_handler.join().unwrap();
90
91 println!("Done!");
92 }
93