reverie/reverie-examples/chrome-trace/global_state.rs
Vladimir Makaev 03cbd6044d update hermetic_infra/** files with correct license header - 1/x
Summary:
Followed guide here https://www.internalfb.com/intern/wiki/Linting/License_Lint/ to add fbcode/hermetic_infra/** code to license linter. As we have parts of our code shipped as Open Source it's important to get this automated

This diff is updating existing file's licenses to not get conflict after lint rule enablement

Reviewed By: jasonwhite

Differential Revision: D40674080

fbshipit-source-id: da6ecac036f8964619cf7912058f3a911558e7b1
2022-10-26 12:18:14 -07:00

70 lines
1.6 KiB
Rust

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
* All rights reserved.
*
* This source code is licensed under the BSD-style license found in the
* LICENSE file in the root directory of this source tree.
*/
use std::io;
use std::path::PathBuf;
use std::sync::Mutex;
use std::time::SystemTime;
use reverie::GlobalTool;
use reverie::Pid;
use serde::Deserialize;
use serde::Serialize;
use crate::event::ThreadExit;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Program {
/// The path to the program.
name: PathBuf,
/// The program arguments.
args: Vec<String>,
}
#[derive(Debug)]
pub struct GlobalState {
epoch: SystemTime,
events: Mutex<Vec<ThreadExit>>,
}
impl Default for GlobalState {
fn default() -> Self {
Self {
epoch: SystemTime::now(),
events: Default::default(),
}
}
}
#[reverie::global_tool]
impl GlobalTool for GlobalState {
type Request = ThreadExit;
type Response = ();
async fn receive_rpc(&self, _pid: Pid, event: ThreadExit) {
let mut events = self.events.lock().unwrap();
events.push(event);
}
}
impl GlobalState {
/// Writes out a chrome trace file to the given writer.
pub fn chrome_trace<W: io::Write>(&self, writer: &mut W) -> serde_json::Result<()> {
let events = self.events.lock().unwrap();
let mut json: Vec<serde_json::Value> = Vec::new();
for event in events.iter() {
event.trace_event(self.epoch, &mut json);
}
let json = serde_json::Value::Array(json);
serde_json::to_writer(writer, &json)
}
}