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
//! A library for profiling frame-based games.
//!
//! After recording a profile, you can view it in the [WhatTheFrame GUI](https://github.com/JMS55/whattheframe).
//!
//!
//! # Activating Profiling
//!
//! By default, [`Profiler::new_frame`], [`Profiler::profile_task`], and [`Profiler::end_profiling`] will compile to no-ops.
//!
//! To enable profiling, turn on the `profile` feature. You probably want to configure your game's `Cargo.toml` like so:
//! ```toml
//! [features]
//! profile = ["wtf/profile"]
//! [dependencies]
//! wtf = "1.0"
//! ```
//!
//! And then run your game like so:
//!
//! `cargo run --features profile`
//!
//!
//! # API
//!
//! The API consists of 4 functions:
//! * [`read_profile_data`] - Used to read a `.wtf` profile.
//! * [`Profiler::new_frame`] - Call at the start of your frame
//! * [`Profiler::profile_task`] - Call at the top of each scope you want to profile
//! * [`Profiler::end_profiling`] - Call _once_ at the end of your game
//!
//! Note that you _must_ assign [`Profiler::new_frame`] and [`Profiler::profile_task`] to a variable (_not_ `_`) like so:
//! ```rust
//! let _profile = Profiler::new_frame();
//! ```
//! ```rust
//! let _profile = Profiler::new_task("foo");
//! ```
//!
//! Example with `winit`:
//! ```rust
//! use std::sync::atomic::{AtomicBool, Ordering};
//! use std::sync::Arc;
//! use std::thread;
//! use winit::event::Event;
//! use wtf::Profiler;
//!
//! fn main() {
//!     let mut profiler_frame = None;
//!
//!     let thread_should_quit = Arc::new(AtomicBool::new(false));
//!     let thread = thread::spawn({
//!         let thread_should_quit = Arc::clone(&thread_should_quit);
//!
//!         move || {
//!             while !thread_should_quit.load(Ordering::SeqCst) {
//!                 {
//!                     let _profile = Profiler::profile_task("thread_part_1");
//!                     thread_task_1();
//!                     thread_task_2();
//!                     thread_task_3();
//!                 }
//!
//!                 {
//!                     let _profile = Profiler::profile_task("thread_part_2");
//!                     thread_task_4();
//!                     thread_task_5();
//!                 }
//!             }
//!         }
//!     });
//!
//!     event_loop.run(move |event, _, _| match &event {
//!         Event::NewEvents(_) => {
//!             profiler_frame = Some(Profiler::new_frame());
//!         }
//!
//!         Event::MainEventsCleared => {
//!             let record = Profiler::profile_task("update_game");
//!             update_state();
//!             update_positions();
//!             drop(record);
//!
//!             window.request_redraw();
//!         }
//!
//!         Event::RedrawEventsCleared => {
//!             let frame = profiler_frame.take();
//!             drop(frame);
//!         }
//!
//!         Event::LoopDestroyed => {
//!             thread_should_quit.store(true, Ordering::SeqCst);
//!             thread.join().unwrap();
//!
//!             Profiler::end_profiling();
//!         }
//!
//!         _ => {}
//!     });
//! }
//! ```

use serde::Deserialize;
use snap::read::FrameDecoder;
use std::io::{self, Read};
use std::time::Duration;

#[cfg(feature = "profile")]
use {
    bumpalo::Bump as Arena,
    chrono::offset::Utc,
    flume::Sender,
    once_cell::sync::Lazy,
    serde::Serialize,
    snap::write::FrameEncoder,
    std::env,
    std::fs::File,
    std::io::Write,
    std::mem,
    std::sync::Mutex,
    std::thread::{self, JoinHandle},
    std::time::Instant,
};

#[cfg(feature = "profile")]
static PROFILER: Lazy<Profiler> = Lazy::new(|| {
    let (sender, reciever) = flume::unbounded();

    let thread = thread::Builder::new()
        .name("wtf-profiler".to_string())
        .spawn(move || {
            fn create_file() -> Option<FrameEncoder<File>> {
                let program_name = env::current_exe().ok()?;
                let program_name = program_name.file_name()?.to_str()?;
                let timestamp = Utc::now().format("%F-%T");
                let file = File::create(format!("{}-{}.wtf", program_name, timestamp)).ok()?;
                Some(FrameEncoder::new(file))
            }
            let mut file = create_file().expect("WTF: Failed to create file for profile");

            #[derive(Serialize)]
            struct TaskDataS<'a> {
                name: &'a str,
                duration: Duration,
                subtasks: Vec<&'a Self>,
            }

            let mut task_arena = Arena::with_capacity(mem::size_of::<TaskDataS>() * 1000);
            let mut frame_number: usize = 0;
            let mut frame = TaskDataS {
                name: "",
                duration: Duration::default(),
                subtasks: Vec::with_capacity(10),
            };
            let mut parent_stack: Vec<*mut TaskDataS> = vec![&mut frame];

            loop {
                let msg = reciever.recv_timeout(Duration::from_millis(100));
                match msg {
                    Ok(ProfilerMessage::TaskStart { name }) => {
                        // Create a new task with a placeholder duration
                        let task = TaskDataS {
                            name,
                            duration: Duration::default(),
                            subtasks: Vec::new(),
                        };
                        let task_mut_ref: *mut TaskDataS = task_arena.alloc(task);
                        let task_ref = unsafe { &*task_mut_ref };

                        let parent = *parent_stack.last().unwrap();
                        let parent_subtasks = unsafe { &mut (*parent).subtasks };

                        // Add it to the current parent's subtasks
                        parent_subtasks.push(task_ref);

                        // Push it to the top of the parent stack
                        parent_stack.push(task_mut_ref);
                    }
                    Ok(ProfilerMessage::TaskEnd { elapsed }) => {
                        // Replace the placeholder with the real duration
                        let task = *parent_stack.last().unwrap();
                        unsafe { (*task).duration = elapsed }

                        // If only 1 task left in the parent stack (the frame), then write the frame to the file
                        // Else pop the parent stack
                        if parent_stack.len() == 1 {
                            frame_number += 1;
                            let frame_name = format!("Frame #{}", frame_number);
                            // SAFETY: frame.name is temporarily set to reference a local string
                            // It must be reset to a valid reference by the end of the scope
                            frame.name = unsafe { mem::transmute(frame_name.as_str()) };

                            bincode::serialize_into(&mut file, &frame)
                                .expect("WTF: Failed to write data to file");

                            // Reset for the next frame
                            frame.name = "";
                            frame.subtasks.clear();
                            task_arena.reset();
                        } else {
                            parent_stack.pop().unwrap();
                        }
                    }
                    _ => {
                        // Haven't recieved any data recently, check if thread should finish
                        if PROFILER
                            .thread
                            .lock()
                            .expect("WTF: Failed to acquire thread lock")
                            .is_none()
                        {
                            break;
                        }
                    }
                }
            }

            file.flush().expect("WTF: Failed to write data to file");
        })
        .expect("WTF: Failed to spawn data writing thread");
    let thread = Mutex::new(Some(thread));

    Profiler { sender, thread }
});

pub struct Profiler {
    #[cfg(feature = "profile")]
    sender: Sender<ProfilerMessage>,
    #[cfg(feature = "profile")]
    thread: Mutex<Option<JoinHandle<()>>>,
}

#[cfg(feature = "profile")]
#[doc(hidden)]
pub type ProfilingReturnType = TaskRecord;
#[cfg(not(feature = "profile"))]
#[doc(hidden)]
pub type ProfilingReturnType = ();

impl Profiler {
    #[must_use = "Must assign to a variable: \"_profile = Profiler::new_frame()\""]
    pub fn new_frame() -> ProfilingReturnType {
        #[cfg(feature = "profile")]
        TaskRecord {
            start: Instant::now(),
        }
    }

    #[must_use = "Must assign to a variable: \"_profile = Profiler::profile_task()\""]
    #[allow(unused_variables)]
    pub fn profile_task(name: &'static str) -> ProfilingReturnType {
        #[cfg(feature = "profile")]
        {
            PROFILER
                .sender
                .send(ProfilerMessage::TaskStart { name })
                .expect("WTF: Failed to send task across a thread");
            TaskRecord {
                start: Instant::now(),
            }
        }
    }

    pub fn end_profiling() {
        #[cfg(feature = "profile")]
        {
            // Take the thread handle out, indicating to the data writing thread to stop
            let mut profiler_lock = PROFILER
                .thread
                .lock()
                .expect("WTF: Failed to acquire thread lock");
            let thread = profiler_lock
                .take()
                .expect("WTF: Profiler::end_profiling() has already been called once");
            // Drop the lock so the data writing thread can acquire it
            drop(profiler_lock);
            // Wait for the data writing thread to finish
            thread
                .join()
                .expect("WTF: Failed to join data writing thread");
        }
    }
}

pub type ProfileData = Box<[TaskData]>;

pub fn read_profile_data<R: Read>(reader: R) -> Result<ProfileData, bincode::Error> {
    let mut reader = FrameDecoder::new(reader);
    let mut frames = Vec::new();
    // Keep trying to read TaskData's until there aren't any more to read
    loop {
        let frame = bincode::deserialize_from(&mut reader);
        let frame = match frame.map_err(|err| *err) {
            Ok(task) => task,
            Err(bincode::ErrorKind::Io(err)) if err.kind() == io::ErrorKind::UnexpectedEof => break,
            Err(err) => return Err(Box::new(err)),
        };
        frames.push(frame);
    }
    Ok(frames.into_boxed_slice())
}

#[derive(Clone, Deserialize)]
pub struct TaskData {
    pub name: Box<str>,
    pub duration: Duration,
    pub subtasks: Box<[Self]>,
}

#[cfg(feature = "profile")]
enum ProfilerMessage {
    TaskStart { name: &'static str },
    TaskEnd { elapsed: Duration },
}

#[cfg(feature = "profile")]
pub struct TaskRecord {
    start: Instant,
}

#[cfg(feature = "profile")]
impl Drop for TaskRecord {
    fn drop(&mut self) {
        let msg = ProfilerMessage::TaskEnd {
            elapsed: self.start.elapsed(),
        };
        PROFILER
            .sender
            .send(msg)
            .expect("WTF: Failed to send task across a thread");
    }
}