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
use serde_json::to_writer;
use std::{
    thread,
    io::Write,
    sync::{
        Mutex,
        mpsc::{channel, Receiver, Sender},
        atomic::{AtomicUsize, Ordering},
    },
};
use crate::{
    local::{Local, LOCAL},
    Args,
    Base,
    Event,
};

lazy_static! {
    pub static ref GLOBAL: Mutex<Global> = Mutex::new(Global::new());
}

#[derive(Clone)]
struct Thread {
    name: String,
    pid: usize,
    sort_index: Option<usize>,
}

pub struct Global {
    tx: Sender<Event>,
    rx: Receiver<Event>,
    threads: Vec<Thread>,
    skip: AtomicUsize,
}

impl Global {
    fn new() -> Self {
        let (tx, rx) = channel();
        Self {
            tx, rx,
            threads: Vec::new(),
            skip: AtomicUsize::new(0),
        }
    }

    pub fn create_sender(&self) -> Sender<Event> {
        self.tx.clone()
    }

    pub fn register_thread(&mut self, pid: usize, sort_index: Option<usize>) {
        let id = self.threads.len();
        let current = thread::current();
        let tid = current.id();
        let name = current.name()
            .map(|s| s.to_string())
            .unwrap_or_else(|| format!("<unnamed-{}-{:?}>", id, tid));

        let tid = self.threads.len();
        self.threads.push(Thread {
            name, sort_index, pid,
        });

        LOCAL.with(|local| {
            assert!(local.borrow().is_none());
            *local.borrow_mut() = Some(Local::new(tid, pid, self.tx.clone()));
        });
    }

    pub fn write_profile<W: Write>(&self, mut w: W) {
        // Stop reading samples that are written after
        // write_profile_json() is called.

        self.tx.send(Event::Barrier).ok();

        let skip = self.skip.swap(self.threads.len(), Ordering::Relaxed);

        let names = self.threads.iter()
            .skip(skip)
            .cloned()
            .enumerate()
            .map(|(tid, th)| Event::Meta {
                base: Base {
                    name: "thread_name".into(),
                    tid,
                    pid: th.pid,
                    cat: None,
                    args: Args::Name { name: th.name.into() },
                    cname: None,
                },
            });

        let sort_index = self.threads.iter()
            .skip(skip)
            .cloned()
            .enumerate()
            .filter_map(|(i, th)| th.sort_index.map(|idx| (i, idx, th.pid)))
            .map(|(tid, sort_index, pid)| Event::Meta {
                base: Base {
                    name: "thread_sort_index".into(),
                    tid,
                    pid,
                    cat: None,
                    args: Args::SortIndex { sort_index },
                    cname: None,
                },
            });

        let iter = names
            .chain(sort_index)
            .chain(self.rx.try_iter().take_while(|e| !e.is_barrier()));

        for e in iter {
            to_writer(&mut w, &e).unwrap();
            w.write_all(b",\n").unwrap();
        }

        /*
        while let Ok(event) = self.samples.1.try_recv() {
            if event.t0 > start_time {
                break;
            }

            let t0 = event.t0 / 1000;
            let t1 = event.t1 / 1000;

            /*
            data.push(json!({
                "pid": 0,
                "tid": sample.tid.0,
                "name": sample.name,
                "ph": "B",
                "ts": t0,
                "args": json!({
                    "module": sample.location.module,
                    "file": sample.location.file,
                    "line": sample.location.line,
                })
            }));

            data.push(json!({
                "pid": 0,
                "tid": sample.tid.0,
                "ph": "E",
                "ts": t1
            }));
            */
        }
        */
    }
}