summaryrefslogtreecommitdiffstats
path: root/tools/profiler/rust-api/src/marker/schema.rs
blob: 9368582f1113282dd9465b73af71cfeb01ec0bf0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

//! [`MarkerSchema`] and other enums that will be used by `MarkerSchema`.

use crate::gecko_bindings::{bindings, structs::mozilla};
use std::mem::MaybeUninit;
use std::ops::DerefMut;
use std::os::raw::c_char;
use std::pin::Pin;

/// Marker locations to be displayed in the profiler front-end.
pub type Location = mozilla::MarkerSchema_Location;

/// Formats of marker properties for profiler front-end.
pub type Format = mozilla::MarkerSchema_Format;

/// Whether it's searchable or not in the profiler front-end.
pub type Searchable = mozilla::MarkerSchema_Searchable;

/// This object collects all the information necessary to stream the JSON schema
/// that informs the front-end how to display a type of markers.
/// It will be created and populated in `marker_type_display()` functions in each
/// marker type definition, see add/set functions.
///
/// It's a RAII object that constructs and destroys a C++ MarkerSchema object
/// pointed to a specified reference.
pub struct MarkerSchema {
    pub(crate) pin: Pin<Box<MaybeUninit<mozilla::MarkerSchema>>>,
}

impl MarkerSchema {
    // Initialize a marker schema with the given `Location`s.
    pub fn new(locations: &[Location]) -> Self {
        let mut marker_schema = Box::pin(std::mem::MaybeUninit::<mozilla::MarkerSchema>::uninit());

        unsafe {
            bindings::gecko_profiler_construct_marker_schema(
                marker_schema.deref_mut().as_mut_ptr(),
                locations.as_ptr(),
                locations.len(),
            );
        }
        MarkerSchema { pin: marker_schema }
    }

    /// Marker schema for types that have special frontend handling.
    /// Nothing else should be set in this case.
    pub fn new_with_special_frontend_location() -> Self {
        let mut marker_schema = Box::pin(std::mem::MaybeUninit::<mozilla::MarkerSchema>::uninit());
        unsafe {
            bindings::gecko_profiler_construct_marker_schema_with_special_front_end_location(
                marker_schema.deref_mut().as_mut_ptr(),
            );
        }
        MarkerSchema { pin: marker_schema }
    }

    /// Optional label in the marker chart.
    /// If not provided, the marker "name" will be used. The given string
    /// can contain element keys in braces to include data elements streamed by
    /// `stream_json_marker_data()`. E.g.: "This is {marker.data.text}"
    pub fn set_chart_label(&mut self, label: &str) -> &mut Self {
        unsafe {
            bindings::gecko_profiler_marker_schema_set_chart_label(
                self.pin.deref_mut().as_mut_ptr(),
                label.as_ptr() as *const c_char,
                label.len(),
            );
        }
        self
    }

    /// Optional label in the marker chart tooltip.
    /// If not provided, the marker "name" will be used. The given string
    /// can contain element keys in braces to include data elements streamed by
    /// `stream_json_marker_data()`. E.g.: "This is {marker.data.text}"
    pub fn set_tooltip_label(&mut self, label: &str) -> &mut Self {
        unsafe {
            bindings::gecko_profiler_marker_schema_set_tooltip_label(
                self.pin.deref_mut().as_mut_ptr(),
                label.as_ptr() as *const c_char,
                label.len(),
            );
        }
        self
    }

    /// Optional label in the marker table.
    /// If not provided, the marker "name" will be used. The given string
    /// can contain element keys in braces to include data elements streamed by
    /// `stream_json_marker_data()`. E.g.: "This is {marker.data.text}"
    pub fn set_table_label(&mut self, label: &str) -> &mut Self {
        unsafe {
            bindings::gecko_profiler_marker_schema_set_table_label(
                self.pin.deref_mut().as_mut_ptr(),
                label.as_ptr() as *const c_char,
                label.len(),
            );
        }
        self
    }

    /// Set all marker chart / marker tooltip / marker table labels with the same text.
    /// Same as the individual methods, the given string can contain element keys
    /// in braces to include data elements streamed by `stream_json_marker_data()`.
    /// E.g.: "This is {marker.data.text}"
    pub fn set_all_labels(&mut self, label: &str) -> &mut Self {
        unsafe {
            bindings::gecko_profiler_marker_schema_set_all_labels(
                self.pin.deref_mut().as_mut_ptr(),
                label.as_ptr() as *const c_char,
                label.len(),
            );
        }
        self
    }

    // Each data element that is streamed by `stream_json_marker_data()` can be
    // displayed as indicated by using one of the `add_...` function below.
    // Each `add...` will add a line in the full marker description. Parameters:
    // - `key`: Element property name as streamed by `stream_json_marker_data()`.
    // - `label`: Optional label. Defaults to the key name.
    // - `format`: How to format the data element value, see `Format` above.
    // - `searchable`: Optional, indicates if the value is used in searches,
    //   defaults to false.

    /// Add a key / format row for the marker data element.
    /// - `key`: Element property name as streamed by `stream_json_marker_data()`.
    /// - `format`: How to format the data element value, see `Format` above.
    pub fn add_key_format(&mut self, key: &str, format: Format) -> &mut Self {
        unsafe {
            bindings::gecko_profiler_marker_schema_add_key_format(
                self.pin.deref_mut().as_mut_ptr(),
                key.as_ptr() as *const c_char,
                key.len(),
                format,
            );
        }
        self
    }

    /// Add a key / label / format row for the marker data element.
    /// - `key`: Element property name as streamed by `stream_json_marker_data()`.
    /// - `label`: Optional label. Defaults to the key name.
    /// - `format`: How to format the data element value, see `Format` above.
    pub fn add_key_label_format(&mut self, key: &str, label: &str, format: Format) -> &mut Self {
        unsafe {
            bindings::gecko_profiler_marker_schema_add_key_label_format(
                self.pin.deref_mut().as_mut_ptr(),
                key.as_ptr() as *const c_char,
                key.len(),
                label.as_ptr() as *const c_char,
                label.len(),
                format,
            );
        }
        self
    }

    /// Add a key / format / searchable row for the marker data element.
    /// - `key`: Element property name as streamed by `stream_json_marker_data()`.
    /// - `format`: How to format the data element value, see `Format` above.
    pub fn add_key_format_searchable(
        &mut self,
        key: &str,
        format: Format,
        searchable: Searchable,
    ) -> &mut Self {
        unsafe {
            bindings::gecko_profiler_marker_schema_add_key_format_searchable(
                self.pin.deref_mut().as_mut_ptr(),
                key.as_ptr() as *const c_char,
                key.len(),
                format,
                searchable,
            );
        }
        self
    }

    /// Add a key / label / format / searchable row for the marker data element.
    /// - `key`: Element property name as streamed by `stream_json_marker_data()`.
    /// - `label`: Optional label. Defaults to the key name.
    /// - `format`: How to format the data element value, see `Format` above.
    /// - `searchable`: Optional, indicates if the value is used in searches,
    ///   defaults to false.
    pub fn add_key_label_format_searchable(
        &mut self,
        key: &str,
        label: &str,
        format: Format,
        searchable: Searchable,
    ) -> &mut Self {
        unsafe {
            bindings::gecko_profiler_marker_schema_add_key_label_format_searchable(
                self.pin.deref_mut().as_mut_ptr(),
                key.as_ptr() as *const c_char,
                key.len(),
                label.as_ptr() as *const c_char,
                label.len(),
                format,
                searchable,
            );
        }
        self
    }

    /// Add a key / value static row.
    /// - `key`: Element property name as streamed by `stream_json_marker_data()`.
    /// - `value`: Static value to display.
    pub fn add_static_label_value(&mut self, label: &str, value: &str) -> &mut Self {
        unsafe {
            bindings::gecko_profiler_marker_schema_add_static_label_value(
                self.pin.deref_mut().as_mut_ptr(),
                label.as_ptr() as *const c_char,
                label.len(),
                value.as_ptr() as *const c_char,
                value.len(),
            );
        }
        self
    }
}

impl Drop for MarkerSchema {
    fn drop(&mut self) {
        unsafe {
            bindings::gecko_profiler_destruct_marker_schema(self.pin.deref_mut().as_mut_ptr());
        }
    }
}