tracepoint_decode 0.4.1

Rust API for decoding tracepoints
Documentation
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

//! `fmt::Display` helpers for using  tracepoint_decode types with format
//! macros like [`write!`] and [`format_args!`].

use core::fmt;
use core::fmt::Write;

use eventheader_types::*;

use crate::charconv;
use crate::enumerator;
use crate::filters;
use crate::filters::Filter;
use crate::perf_abi;
use crate::perf_event_data;
use crate::perf_event_desc;
use crate::perf_item;
use crate::perf_session;
use crate::writers;

use crate::PerfConvertOptions;
use crate::PerfMetaOptions;

/// Display implementation that JSON-escapes the provided input string.
/// This escapes control chars, quotes, and backslashes. For example,
/// the string `Hello, "world"!` would be displayed as `Hello, \"world\"!`.
pub struct JsonEscapeDisplay<'str> {
    value: &'str str,
}

impl<'str> JsonEscapeDisplay<'str> {
    /// Creates a new formatter for the specified string.
    pub fn new(value: &'str str) -> Self {
        return Self { value };
    }

    /// Writes the JSON-escaped value to the specified writer.
    pub fn write_to<W: fmt::Write + ?Sized>(&self, writer: &mut W) -> fmt::Result {
        let mut dest = filters::WriteFilter::new(writer);
        return filters::JsonEscapeFilter::new(&mut dest).write_str(self.value);
    }
}

impl<'str> fmt::Display for JsonEscapeDisplay<'str> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> fmt::Result {
        return self.write_to(f);
    }
}

/// Display implementation for string data is expected to be UTF-8. For example,
/// this may be used for the name of an EventHeader event or field.
///
/// Tries to interpret the value as UTF-8, but falls back to Latin1 if the value
/// contains non-UTF-8 sequences. This allows the value to be displayed with
/// best-effort fidelity even if the event is incorrectly-authored or corrupt.
///
/// Instances of this type are returned by methods such as
/// [`crate::EventHeaderEventInfo::name_display`] and
/// [`crate::EventHeaderItemInfo::name_display`].
#[derive(Clone, Copy, Debug)]
pub struct Utf8WithLatin1FallbackDisplay<'dat> {
    utf8_bytes: &'dat [u8],
}

impl<'dat> Utf8WithLatin1FallbackDisplay<'dat> {
    /// Creates a new formatter for the specified string data.
    ///
    /// The `utf8_bytes` value is expected to be UTF-8, but if it is not, the bytes
    /// that are not valid UTF-8 will be interpreted as Latin-1.
    pub fn new(utf8_bytes: &'dat [u8]) -> Self {
        return Self { utf8_bytes };
    }

    /// Writes the value to the specified writer.
    pub fn write_to<W: fmt::Write + ?Sized>(&self, writer: &mut W) -> fmt::Result {
        let mut dest = filters::WriteFilter::new(writer);
        return charconv::write_utf8_with_latin1_fallback_to(self.utf8_bytes, &mut dest);
    }
}

impl<'dat> fmt::Display for Utf8WithLatin1FallbackDisplay<'dat> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> fmt::Result {
        return self.write_to(f);
    }
}

/// Display implementation for the name and tag of an EventHeader field.
///
/// If the field tag is 0, writes just the field name.
/// Otherwise, writes the field name plus a suffix like ";tag=0x1234".
///
/// Tries to interpret the name as UTF-8, but falls back to Latin1 if the name
/// contains non-UTF-8 sequences. This allows the value to be displayed with
/// best-effort fidelity even if the event is incorrectly-authored or corrupt.
///
/// Instances of this type are returned by the
/// [`crate::EventHeaderItemInfo::name_and_tag_display`] method.
#[derive(Clone, Copy, Debug)]
pub struct FieldNameAndTagDisplay<'dat> {
    name_utf8_bytes: &'dat [u8],
    tag: u16,
}

impl<'dat> FieldNameAndTagDisplay<'dat> {
    /// Creates a new formatter for the specified field name and field tag.
    ///
    /// The `name_utf8_bytes` value is expected to be UTF-8, but if it is not, the bytes
    /// that are not valid UTF-8 will be interpreted as Latin-1.
    pub fn new(name_utf8_bytes: &'dat [u8], tag: u16) -> Self {
        return Self {
            name_utf8_bytes,
            tag,
        };
    }

    /// If the field tag is 0, writes just the field name.
    /// Otherwise, writes the field name plus a suffix like ";tag=0x1234".
    pub fn write_to<W: fmt::Write + ?Sized>(&self, writer: &mut W) -> fmt::Result {
        let mut dest = filters::WriteFilter::new(writer);
        charconv::write_utf8_with_latin1_fallback_to(self.name_utf8_bytes, &mut dest)?;
        if self.tag != 0 {
            return write!(dest, ";tag=0x{:X}", self.tag);
        }
        return Ok(());
    }
}

impl<'dat> fmt::Display for FieldNameAndTagDisplay<'dat> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> fmt::Result {
        return self.write_to(f);
    }
}

/// Display implementation for the identity of an EventHeader event, i.e.
/// "ProviderName:EventName".
///
/// Instances of this type are returned by the
/// [`crate::EventHeaderEventInfo::identity_display`] method.
#[derive(Clone, Copy, Debug)]
pub struct EventHeaderIdentityDisplay<'nam, 'dat> {
    provider_name: &'nam str,
    event_name_utf8_bytes: &'dat [u8],
}

impl<'nam, 'dat> EventHeaderIdentityDisplay<'nam, 'dat> {
    /// Creates a new formatter for the specified provider name and event name.
    ///
    /// The `event_name_utf8_bytes` value is expected to be UTF-8, but if it is not,
    /// the bytes that are not valid UTF-8 will be interpreted as Latin-1.
    pub fn new(provider_name: &'nam str, event_name_utf8_bytes: &'dat [u8]) -> Self {
        return Self {
            provider_name,
            event_name_utf8_bytes,
        };
    }

    /// Writes the event identity, i.e. "ProviderName:EventName"
    pub fn write_to<W: fmt::Write + ?Sized>(&self, writer: &mut W) -> fmt::Result {
        let mut dest = filters::WriteFilter::new(writer);
        dest.write_str(self.provider_name)?;
        dest.write_ascii(b':')?;
        return charconv::write_utf8_with_latin1_fallback_to(self.event_name_utf8_bytes, &mut dest);
    }
}

impl<'nam, 'dat> fmt::Display for EventHeaderIdentityDisplay<'nam, 'dat> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> fmt::Result {
        return self.write_to(f);
    }
}

/// Display implementation for the JSON-escaped identity of an EventHeader event,
/// i.e. "ProviderName:EventName".
///
/// Instances of this type are returned by the
/// [`crate::EventHeaderEventInfo::json_identity_display`] method.
#[derive(Clone, Copy, Debug)]
pub struct EventHeaderJsonIdentityDisplay<'nam, 'dat> {
    provider_name: &'nam str,
    event_name_utf8_bytes: &'dat [u8],
}

impl<'nam, 'dat> EventHeaderJsonIdentityDisplay<'nam, 'dat> {
    /// Creates a new formatter for the specified provider name and event name.
    ///
    /// The `event_name_utf8_bytes` value is expected to be UTF-8, but if it is not,
    /// the bytes that are not valid UTF-8 will be interpreted as Latin-1.
    pub fn new(provider_name: &'nam str, event_name_utf8_bytes: &'dat [u8]) -> Self {
        return Self {
            provider_name,
            event_name_utf8_bytes,
        };
    }

    /// Writes the event identity, i.e. "ProviderName:EventName"
    pub fn write_to<W: fmt::Write + ?Sized>(&self, writer: &mut W) -> fmt::Result {
        let mut dest_raw = filters::WriteFilter::new(writer);
        let mut dest = filters::JsonEscapeFilter::new(&mut dest_raw);
        dest.write_str(self.provider_name)?;
        dest.write_ascii(b':')?;
        return charconv::write_utf8_with_latin1_fallback_to(self.event_name_utf8_bytes, &mut dest);
    }
}

impl<'nam, 'dat> fmt::Display for EventHeaderJsonIdentityDisplay<'nam, 'dat> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> fmt::Result {
        return self.write_to(f);
    }
}

/// Text formatter for the value of a [`crate::PerfItemValue`].
/// This formats the value using `value.write_to()`.
pub struct PerfItemValueDisplay<'dat> {
    value: &'dat perf_item::PerfItemValue<'dat>,
    convert_options: PerfConvertOptions,
}

impl<'dat> PerfItemValueDisplay<'dat> {
    /// Creates a new formatter for the specified value.
    pub fn new(value: &'dat perf_item::PerfItemValue<'dat>) -> Self {
        return Self {
            value,
            convert_options: PerfConvertOptions::Default,
        };
    }

    /// Configures the conversion options. The default value is [`PerfConvertOptions::Default`].
    pub fn convert_options(&mut self, value: PerfConvertOptions) -> &mut Self {
        self.convert_options = value;
        return self;
    }

    /// Writes the value to the specified writer.
    pub fn write_to<W: fmt::Write + ?Sized>(&self, writer: &mut W) -> fmt::Result {
        self.value.write_to(writer, self.convert_options)
    }
}

impl<'dat> fmt::Display for PerfItemValueDisplay<'dat> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> fmt::Result {
        self.value.write_to(f, self.convert_options)
    }
}

/// JSON formatter for the value of a [`crate::PerfItemValue`].
/// This formats the value using `value.write_json_to()`.
pub struct PerfItemValueJsonDisplay<'dat> {
    value: &'dat perf_item::PerfItemValue<'dat>,
    convert_options: PerfConvertOptions,
}

impl<'dat> PerfItemValueJsonDisplay<'dat> {
    /// Creates a new formatter for the specified value.
    pub fn new(value: &'dat perf_item::PerfItemValue<'dat>) -> Self {
        return Self {
            value,
            convert_options: PerfConvertOptions::Default,
        };
    }

    /// Configures the conversion options. The default value is [`PerfConvertOptions::Default`].
    pub fn convert_options(&mut self, value: PerfConvertOptions) -> &mut Self {
        self.convert_options = value;
        return self;
    }

    /// Writes the value to the specified writer.
    pub fn write_to<W: fmt::Write + ?Sized>(&self, writer: &mut W) -> fmt::Result {
        self.value.write_json_to(writer, self.convert_options)
    }
}

impl<'dat> fmt::Display for PerfItemValueJsonDisplay<'dat> {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> fmt::Result {
        self.value.write_json_to(f, self.convert_options)
    }
}

/// Formatter for the "meta" suffix of an EventHeader event, i.e. `"level": 5, "keyword": 3`.
///
/// Instances of this type are returned by the
/// [`crate::EventHeaderEventInfo::json_meta_display`] method.
#[derive(Debug)]
pub struct EventHeaderJsonMetaDisplay<'inf> {
    eh_event_info: &'inf enumerator::EventHeaderEventInfo<'inf, 'inf>,
    sample_event_info: Option<&'inf perf_event_data::PerfSampleEventInfo<'inf>>,
    add_comma_before_first_item: bool,
    meta_options: PerfMetaOptions,
    convert_options: PerfConvertOptions,
}

impl<'inf> EventHeaderJsonMetaDisplay<'inf> {
    pub(crate) fn new(
        eh_event_info: &'inf enumerator::EventHeaderEventInfo<'inf, 'inf>,
        sample_event_info: Option<&'inf perf_event_data::PerfSampleEventInfo<'inf>>,
    ) -> Self {
        return Self {
            eh_event_info,
            sample_event_info,
            add_comma_before_first_item: false,
            meta_options: PerfMetaOptions::Default,
            convert_options: PerfConvertOptions::Default,
        };
    }

    /// Configures whether a comma will be written before the first item, e.g.
    /// `, "level": 5` (true) instead of `"level": 5` (false). The default value is false.
    ///
    /// Note that if no items are written, no comma is written regardless of this setting.
    pub fn add_comma_before_first_item(&mut self, value: bool) -> &mut Self {
        self.add_comma_before_first_item = value;
        return self;
    }

    /// Configures the items that will be included in the suffix.
    /// The default value is [`PerfMetaOptions::Default`].
    pub fn meta_options(&mut self, value: PerfMetaOptions) -> &mut Self {
        self.meta_options = value;
        return self;
    }

    /// Configures the conversion options. The default value is [`PerfConvertOptions::Default`].
    pub fn convert_options(&mut self, value: PerfConvertOptions) -> &mut Self {
        self.convert_options = value;
        return self;
    }

    /// Writes event metadata as a comma-separated list of 0 or more
    /// JSON name-value pairs, e.g. `"level": 5, "keyword": 3` (including the quotation marks).
    /// Returns true if any items were written, false if nothing was written.
    pub fn write_to<W: fmt::Write + ?Sized>(&self, w: &mut W) -> Result<bool, fmt::Error> {
        let mut any_written = if let Some(sample_event_info) = self.sample_event_info {
            sample_event_info
                .json_meta_display()
                .add_comma_before_first_item(self.add_comma_before_first_item)
                .meta_options(
                    self.meta_options
                        .and_not(PerfMetaOptions::Provider.or(PerfMetaOptions::Event)),
                )
                .convert_options(self.convert_options)
                .write_to(w)?
        } else {
            false
        };

        let mut json = writers::JsonWriter::new(
            w,
            self.convert_options,
            any_written || self.add_comma_before_first_item,
        );

        let tracepoint_name = self.eh_event_info.tracepoint_name();
        let provider_name_end = if self
            .meta_options
            .has_flag(PerfMetaOptions::Provider.or(PerfMetaOptions::Options))
        {
            // Unwrap: Shouldn't be possible to get an EventHeaderEventInfo with an invalid tracepoint name.
            tracepoint_name.rfind('_').unwrap()
        } else {
            0
        };

        if self.meta_options.has_flag(PerfMetaOptions::Provider) {
            any_written = true;
            json.write_property_name_json_safe("provider")?;
            json.write_value_quoted(|w| {
                w.write_str_with_json_escape(&tracepoint_name[..provider_name_end])
            })?;
        }

        if self.meta_options.has_flag(PerfMetaOptions::Event) {
            any_written = true;
            json.write_property_name_json_safe("event")?;
            json.write_value_quoted(|w| {
                w.write_utf8_with_json_escape(self.eh_event_info.name_bytes())
            })?;
        }

        if self.meta_options.has_flag(PerfMetaOptions::Id) && self.eh_event_info.header().id != 0 {
            any_written = true;
            json.write_property_name_json_safe("id")?;
            json.write_value(|w| w.write_display_with_no_filter(self.eh_event_info.header().id))?;
        }

        if self.meta_options.has_flag(PerfMetaOptions::Version)
            && self.eh_event_info.header().version != 0
        {
            any_written = true;
            json.write_property_name_json_safe("version")?;
            json.write_value(|w| {
                w.write_display_with_no_filter(self.eh_event_info.header().version)
            })?;
        }

        if self.meta_options.has_flag(PerfMetaOptions::Level)
            && self.eh_event_info.header().level != Level::Invalid
        {
            any_written = true;
            json.write_property_name_json_safe("level")?;
            json.write_value(|w| {
                w.write_display_with_no_filter(self.eh_event_info.header().level.as_int())
            })?;
        }

        if self.meta_options.has_flag(PerfMetaOptions::Keyword) && self.eh_event_info.keyword() != 0
        {
            any_written = true;
            json.write_property_name_json_safe("keyword")?;
            json.write_value(|w| w.write_json_hex64(self.eh_event_info.keyword()))?;
        }

        if self.meta_options.has_flag(PerfMetaOptions::Opcode)
            && self.eh_event_info.header().opcode != Opcode::Info
        {
            any_written = true;
            json.write_property_name_json_safe("opcode")?;
            json.write_value(|w| {
                w.write_display_with_no_filter(self.eh_event_info.header().opcode.as_int())
            })?;
        }

        if self.meta_options.has_flag(PerfMetaOptions::Tag) && self.eh_event_info.header().tag != 0
        {
            any_written = true;
            json.write_property_name_json_safe("tag")?;
            json.write_value(|w| w.write_json_hex32(self.eh_event_info.header().tag as u32))?;
        }

        if self.meta_options.has_flag(PerfMetaOptions::Activity)
            && self.eh_event_info.activity_id_len() >= 16
        {
            any_written = true;
            json.write_property_name_json_safe("activity")?;
            let start = self.eh_event_info.activity_id_start() as usize;
            json.write_value_quoted(|w| {
                w.write_uuid(
                    &self.eh_event_info.event_data()[start..start + 16]
                        .try_into()
                        .unwrap(),
                )
            })?;
        }

        if self.meta_options.has_flag(PerfMetaOptions::RelatedActivity)
            && self.eh_event_info.activity_id_len() >= 32
        {
            any_written = true;
            json.write_property_name_json_safe("relatedActivity")?;
            let start = self.eh_event_info.activity_id_start() as usize + 16;
            json.write_value_quoted(|w| {
                w.write_uuid(
                    &self.eh_event_info.event_data()[start..start + 16]
                        .try_into()
                        .unwrap(),
                )
            })?;
        }

        if self.meta_options.has_flag(PerfMetaOptions::Options) {
            let name_bytes = tracepoint_name.as_bytes();
            let mut pos = provider_name_end;
            while pos < name_bytes.len() {
                let ch = name_bytes[pos];
                if ch.is_ascii_uppercase() && ch != b'L' && ch != b'K' {
                    any_written = true;
                    json.write_property_name_json_safe("options")?;
                    json.write_value_quoted(|w| {
                        w.write_str_with_no_filter(&tracepoint_name[pos..])
                    })?;
                    break;
                }
                pos += 1;
            }
        }

        if self.meta_options.has_flag(PerfMetaOptions::Flags) {
            any_written = true;
            json.write_property_name_json_safe("flags")?;
            json.write_value(|w| {
                w.write_json_hex32(self.eh_event_info.header().flags.as_int() as u32)
            })?;
        }

        return Ok(any_written);
    }
}

impl<'inf> fmt::Display for EventHeaderJsonMetaDisplay<'inf> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.write_to(f)?;
        return Ok(());
    }
}

/// Formatter for the "meta" suffix of an event, i.e. `"time": "...", "cpu": 3`.
///
/// Instances of this type are returned by the
/// [`crate::PerfNonSampleEventInfo::json_meta_display`] and
/// [`crate::PerfSampleEventInfo::json_meta_display`] methods.
pub struct EventInfoJsonMetaDisplay<'a> {
    session_info: &'a perf_session::PerfSessionInfo,
    event_desc: &'a perf_event_desc::PerfEventDesc,
    time: u64,
    cpu: u32,
    pid: u32,
    tid: u32,
    add_comma_before_first_item: bool,
    meta_options: PerfMetaOptions,
    convert_options: PerfConvertOptions,
}

impl<'a> EventInfoJsonMetaDisplay<'a> {
    pub(crate) const fn new(
        session_info: &'a perf_session::PerfSessionInfo,
        event_desc: &'a perf_event_desc::PerfEventDesc,
        time: u64,
        cpu: u32,
        pid: u32,
        tid: u32,
    ) -> Self {
        return Self {
            session_info,
            event_desc,
            time,
            cpu,
            pid,
            tid,
            add_comma_before_first_item: false,
            meta_options: PerfMetaOptions::Default,
            convert_options: PerfConvertOptions::Default,
        };
    }

    /// Configures whether a comma will be written before the first item, e.g.
    /// `, "cpu": 3` (true) instead of `"cpu": 3` (false). The default value is false.
    ///
    /// Note that if no items are written, no comma is written regardless of this setting.
    pub fn add_comma_before_first_item(&mut self, value: bool) -> &mut Self {
        self.add_comma_before_first_item = value;
        return self;
    }

    /// Configures the items that will be included in the suffix.
    /// The default value is [`PerfMetaOptions::Default`].
    pub fn meta_options(&mut self, value: PerfMetaOptions) -> &mut Self {
        self.meta_options = value;
        return self;
    }

    /// Configures the conversion options. The default value is [`PerfConvertOptions::Default`].
    pub fn convert_options(&mut self, value: PerfConvertOptions) -> &mut Self {
        self.convert_options = value;
        return self;
    }

    /// Writes event metadata as a comma-separated list of 0 or more
    /// JSON name-value pairs, e.g. `"level": 5, "keyword": 3` (including the quotation marks).
    /// Returns true if any items were written, false if nothing was written.
    pub fn write_to<W: fmt::Write + ?Sized>(&self, w: &mut W) -> Result<bool, fmt::Error> {
        let mut json =
            writers::JsonWriter::new(w, self.convert_options, self.add_comma_before_first_item);
        let mut any_written = false;
        let sample_type = self.event_desc.attr().sample_type;

        if sample_type.has_flag(perf_abi::PerfEventAttrSampleType::Time)
            && self.meta_options.has_flag(PerfMetaOptions::Time)
        {
            any_written = true;
            json.write_property_name_json_safe("time")?;
            if self.session_info.clock_offset_known() {
                let time_spec = self.session_info.time_to_time_spec(self.time);
                let dt = writers::date_time::DateTime::new(time_spec.seconds());
                if dt.valid() {
                    json.write_value_quoted(|w| {
                        w.write_fmt_with_no_filter(format_args!(
                            "{:04}-{:02}-{:02}T{:02}:{:02}:{:02}.{:07}Z",
                            dt.year(),
                            dt.month_of_year(),
                            dt.day_of_month(),
                            dt.hour(),
                            dt.minute(),
                            dt.second(),
                            time_spec.nanoseconds() / 100,
                        ))
                    })?;
                } else {
                    json.write_value_quoted(|w| {
                        w.write_fmt_with_no_filter(format_args!(
                            "TIME({}.{:09})",
                            time_spec.seconds(),
                            time_spec.nanoseconds()
                        ))
                    })?;
                }
            } else {
                json.write_value(|w| w.write_float64(self.time as f64 / 1000000000.0))?;
            }
        }

        if sample_type.has_flag(perf_abi::PerfEventAttrSampleType::Cpu)
            && self.meta_options.has_flag(PerfMetaOptions::Cpu)
        {
            any_written = true;
            json.write_property_name_json_safe("cpu")?;
            json.write_value(|w| w.write_display_with_no_filter(self.cpu))?;
        }

        if sample_type.has_flag(perf_abi::PerfEventAttrSampleType::Tid) {
            if self.meta_options.has_flag(PerfMetaOptions::Pid) {
                any_written = true;
                json.write_property_name_json_safe("pid")?;
                json.write_value(|w| w.write_display_with_no_filter(self.pid))?;
            }

            if self.meta_options.has_flag(PerfMetaOptions::Tid)
                && (self.pid != self.tid || !self.meta_options.has_flag(PerfMetaOptions::Pid))
            {
                any_written = true;
                json.write_property_name_json_safe("tid")?;
                json.write_value(|w| w.write_display_with_no_filter(self.tid))?;
            }
        }

        if self
            .meta_options
            .has_flag(PerfMetaOptions::Provider.or(PerfMetaOptions::Event))
        {
            let provider;
            let event;
            let desc_name = self.event_desc.name();
            match self.event_desc.format() {
                Some(format) if desc_name.is_empty() => {
                    provider = format.system_name();
                    event = format.name();
                }
                _ => {
                    let mut parts = desc_name.split(':');
                    provider = parts.next().unwrap_or("");
                    event = parts.next().unwrap_or("");
                }
            }

            if self.meta_options.has_flag(PerfMetaOptions::Provider) && !provider.is_empty() {
                any_written = true;
                json.write_property_name_json_safe("provider")?;
                json.write_value_quoted(|w| w.write_str_with_json_escape(provider))?;
            }

            if self.meta_options.has_flag(PerfMetaOptions::Event) && !event.is_empty() {
                any_written = true;
                json.write_property_name_json_safe("event")?;
                json.write_value_quoted(|w| w.write_str_with_json_escape(event))?;
            }
        }

        return Ok(any_written);
    }
}

impl<'a> fmt::Display for EventInfoJsonMetaDisplay<'a> {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.write_to(f)?;
        return Ok(());
    }
}