Skip to main content

storage_trace/
lib.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#[cfg(feature = "tracing")]
6pub use fuchsia_trace::{
7    Id, Scope, TraceFutureExt, counter, duration, flow_begin, flow_end, flow_step, instant,
8    trace_future_args,
9};
10
11#[cfg(not(feature = "tracing"))]
12mod noop;
13#[cfg(not(feature = "tracing"))]
14pub mod __backend {
15    pub use crate::noop::*;
16}
17#[cfg(not(feature = "tracing"))]
18pub use crate::noop::{Id, Scope, TraceFutureExt};
19
20#[cfg(test)]
21mod tests {
22    use crate::*;
23
24    #[fuchsia::test]
25    fn test_duration() {
26        let trace_only_var = 6;
27        duration!("category", "name");
28        duration!("category", "name", "arg" => 5);
29        duration!("category", "name", "arg" => 5, "arg2" => trace_only_var);
30    }
31
32    #[fuchsia::test]
33    fn test_instant() {
34        let trace_only_var = 6;
35        instant!("category", "name", Scope::Thread);
36        instant!("category", "name", Scope::Thread, "arg" => 5);
37        instant!("category", "name", Scope::Thread, "arg" => 5, "arg2" => trace_only_var);
38    }
39
40    #[fuchsia::test]
41    fn test_flow_begin() {
42        let trace_only_var = 6;
43        let flow_id: Id = u64::from(Id::new()).into();
44        flow_begin!("category", "name", flow_id);
45        flow_begin!("category", "name", flow_id, "arg" => 5);
46        flow_begin!("category", "name", flow_id, "arg" => 5, "arg2" => trace_only_var);
47    }
48
49    #[fuchsia::test]
50    fn test_flow_step() {
51        let trace_only_var = 6;
52        let flow_id: Id = 5u64.into();
53        flow_step!("category", "name", flow_id);
54        flow_step!("category", "name", flow_id, "arg" => 5);
55        flow_step!("category", "name", flow_id, "arg" => 5, "arg2" => trace_only_var);
56    }
57
58    #[fuchsia::test]
59    fn test_flow_end() {
60        let trace_only_var = 6;
61        let flow_id: Id = 5u64.into();
62        flow_end!("category", "name", flow_id);
63        flow_end!("category", "name", flow_id, "arg" => 5);
64        flow_end!("category", "name", flow_id, "arg" => 5, "arg2" => trace_only_var);
65    }
66
67    #[fuchsia::test]
68    async fn test_trace_future() {
69        let value = async move { 5 }.trace(trace_future_args!("category", "name")).await;
70        assert_eq!(value, 5);
71
72        let value =
73            async move { 5 }.trace(trace_future_args!("category", "name", "arg1" => 6)).await;
74        assert_eq!(value, 5);
75
76        let trace_only_var = 7;
77        let value = async move { 5 }
78            .trace(trace_future_args!("category", "name", "arg1" => 6, "ar2" => trace_only_var))
79            .await;
80        assert_eq!(value, 5);
81    }
82
83    #[fuchsia::test]
84    fn test_arg_types() {
85        duration!("category", "name", "bool" => true);
86        duration!("category", "name", "i32" => 5i32, "u32" => 5u32);
87        duration!("category", "name", "i64" => 5i64, "u64" => 5u64);
88        duration!("category", "name", "isize" => 5isize, "usize" => 5usize);
89        duration!("category", "name", "f64" => 5f64);
90
91        let owned_str = "test-str".to_owned();
92        duration!("category", "name", "str" => owned_str.as_str());
93
94        let mut value = 5u64;
95        duration!("category", "name", "const-ptr" => &value as *const u64);
96        duration!("category", "name", "mut-ptr" => &mut value as *mut u64);
97    }
98
99    #[fuchsia::test]
100    fn test_counter() {
101        counter!("category", "name", 1, "a" => 10);
102        counter!("category", "name", 1, "a" => 10, "b" => 20);
103    }
104
105    #[fuchsia::test]
106    fn test_id_traits() {
107        use std::collections::HashSet;
108
109        let id1 = Id::new();
110        let id2 = Id::from(42u64);
111        let id3 = id2;
112        assert_eq!(id2, id3);
113        assert!(id1 <= id2 || id2 <= id1);
114        assert!(!format!("{id1:?}").is_empty());
115
116        let mut set = HashSet::new();
117        set.insert(id2);
118        assert!(set.contains(&id3));
119    }
120
121    #[cfg(not(feature = "tracing"))]
122    #[fuchsia::test]
123    fn test_noop_backend() {
124        use crate::__backend::ArgValue;
125
126        let id = Id::new();
127        let raw: u64 = id.into();
128        assert_eq!(raw, 0);
129
130        __backend::use_duration_args("category", "name");
131        __backend::use_instant_args("category", "name", Scope::Process);
132        __backend::use_instant_args("category", "name", Scope::Global);
133        __backend::use_flow_args("category", "name", id);
134        __backend::use_counter_args("category", "name", 1);
135
136        let mut val = 42u64;
137        let s = "hello";
138        __backend::use_args(&[
139            <()>::of("unit", ()),
140            bool::of("bool", true),
141            i32::of("i32", -1),
142            u32::of("u32", 1),
143            i64::of("i64", -2),
144            u64::of("u64", 2),
145            isize::of("isize", -3),
146            usize::of("usize", 3),
147            f64::of("f64", 1.5),
148            <&str>::of("str", s),
149            <*const u64>::of("cptr", &val as *const u64),
150            <*mut u64>::of("mptr", &mut val as *mut u64),
151        ]);
152    }
153}