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
//! `coarse-prof` allows you to hierarchically measure the time that blocks in
//! your program take, enabling you to get an intuition of where most time is
//! spent. This can be useful for game development, where you have a bunch of
//! things that need to run in every frame, such as physics, rendering,
//! networking and so on, and you may wish to identify the hot spots, so that
//! you know whether and what to optimize.
//!
//! `coarse-prof`'s implementation has been inspired by
//! [hprof](https://github.com/cmr/hprof).
//! In contrast to `hprof`, which resets measurements after each frame, this
//! library tracks averages over multiple frames. Also, `coarse-prof` provides
//! the macro [`profile`](macro.profile.html) for profiling a scope, so that
//! users do not have to assign a name to scope guards.
//!
//! # Example
//!
//! ```
//! use std::thread::sleep;
//! use std::time::Duration;
//!
//! use coarse_prof::profile;
//!
//! fn render() {
//!     profile!("render");
//!
//!     // So slow!
//!     sleep(Duration::from_millis(10));
//! }
//!
//! // Our game's main loop
//! let num_frames = 100;
//! for i in 0..num_frames {
//!     profile!("frame");
//!
//!     // Physics don't run every frame
//!     if i % 10 == 0 {
//!         profile!("physics");
//!         sleep(Duration::from_millis(2));
//!         
//!         {
//!             profile!("collisions");
//!             sleep(Duration::from_millis(1));
//!         }
//!     }
//!     
//!     render();
//! }
//!
//! // Print the profiling results.
//! coarse_prof::write(&mut std::io::stdout()).unwrap();
//! ```
//!
//! Example output:
//! ```text
//! frame: 100.00%, 10.40ms/call @ 96.17Hz
//!   physics: 3.04%, 3.16ms/call @ 9.62Hz
//!     collisions: 33.85%, 1.07ms/call @ 9.62Hz
//!   render: 96.84%, 10.07ms/call @ 96.17Hz
//! ```

use std::cell::RefCell;
use std::io;
use std::rc::Rc;
use std::time::{Duration, Instant};

use floating_duration::TimeAsFloat;

thread_local!(
    /// Global thread-local instance of the profiler.
    pub static PROFILER: RefCell<Profiler> = RefCell::new(Profiler::new())
);

/// Print profiling scope tree.
///
/// Example output:
/// ```text
/// frame: 100.00%, 10.40ms/call @ 96.17Hz
///   physics: 3.04%, 3.16ms/call @ 9.62Hz
///     collisions: 33.85%, 1.07ms/call @ 9.62Hz
///   render: 96.84%, 10.07ms/call @ 96.17Hz
/// ```
///
/// Percentages represent the amount of time taken relative to the parent node.
///
/// Frequencies are computed with respect to the total amount of time spent in
/// root nodes. Thus, if you have multiple root nodes and they do not cover
/// all code that runs in your program, the printed frequencies will be
/// overestimated.
pub fn write<W: io::Write>(out: &mut W) -> io::Result<()> {
    PROFILER.with(|p| p.borrow().write(out))
}

/// Reset profiling information.
pub fn reset() {
    PROFILER.with(|p| p.borrow_mut().reset());
}

/// Manually enter a scope.
///
/// The returned instance of [`Guard`](struct.Guard.html) should be dropped
/// when leaving the scope.
///
/// Usually, it is more convenient to use the macro
/// [`profile`](macro.profile.html) for including a scope in profiling, but in
/// some special cases explicit entering/leaving can make sense.
pub fn enter(name: &'static str) -> Guard {
    PROFILER.with(|p| p.borrow_mut().enter(name))
}

/// Use this macro to add the current scope to profiling. In effect, the time
/// taken from entering to leaving the scope will be measured.
///
/// Internally, the scope is inserted in the scope tree of the global
/// thread-local [`PROFILER`](constant.PROFILER.html).
///
/// # Example
///
/// The following example will profile the scope `"foo"`, which has the scope
/// `"bar"` as a child.
///
/// ```
/// use coarse_prof::profile;
///
/// {
///     profile!("foo");
///
///     {
///         profile!("bar");
///         // ... do something ...
///     }
///
///     // ... do some more ...
/// }
/// ```
#[macro_export]
macro_rules! profile {
    ($name:expr) => {
        let _guard = $crate::PROFILER.with(|p| p.borrow_mut().enter($name));
    };
}

/// Internal representation of scopes as a tree.
struct Scope {
    /// Name of the scope.
    name: &'static str,

    /// Parent scope in the tree. Root scopes have no parent.
    pred: Option<Rc<RefCell<Scope>>>,

    /// Child scopes in the tree.
    succs: Vec<Rc<RefCell<Scope>>>,

    /// How often has this scope been visited?
    num_calls: usize,

    /// In total, how much time has been spent in this scope?
    duration_sum: Duration,
}

impl Scope {
    fn new(name: &'static str, pred: Option<Rc<RefCell<Scope>>>) -> Scope {
        Scope {
            name,
            pred,
            succs: Vec::new(),
            num_calls: 0,
            duration_sum: Duration::new(0, 0),
        }
    }

    /// Enter this scope. Returns a `Guard` instance that should be dropped
    /// when leaving the scope.
    fn enter(&mut self) -> Guard {
        self.num_calls += 1;
        Guard::enter()
    }

    /// Leave this scope. Called automatically by the `Guard` instance.
    fn leave(&mut self, duration: Duration) {
        let duration_sum = self.duration_sum.checked_add(duration);

        // Even though this is extremely unlikely, let's not panic on overflow.
        self.duration_sum = duration_sum.unwrap_or(Duration::from_millis(0));
    }

    fn write_recursive<W: io::Write>(
        &self,
        out: &mut W,
        total_duration: Duration,
        depth: usize,
    ) -> io::Result<()> {
        let total_duration_secs = total_duration.as_fractional_secs();
        let duration_sum_secs = self.duration_sum.as_fractional_secs();
        let pred_sum_secs = self.pred.clone().map_or(total_duration_secs, |pred| {
            pred.borrow().duration_sum.as_fractional_secs()
        });

        let percent = duration_sum_secs / pred_sum_secs * 100.0;

        // Write self
        for _ in 0..depth {
            write!(out, "  ")?;
        }
        writeln!(
            out,
            "{}: {:3.2}%, {:>4.2}ms/call @ {:.2}Hz",
            self.name,
            percent,
            duration_sum_secs * 1000.0 / (self.num_calls as f64),
            self.num_calls as f64 / total_duration_secs,
        )?;

        // Write children
        for succ in &self.succs {
            succ.borrow()
                .write_recursive(out, total_duration, depth + 1)?;
        }

        Ok(())
    }
}

/// A guard that is created when entering a scope and dropped when leaving it.
pub struct Guard {
    enter_time: Instant,
}

impl Guard {
    fn enter() -> Self {
        Self {
            enter_time: Instant::now(),
        }
    }
}

impl Drop for Guard {
    fn drop(&mut self) {
        let duration = self.enter_time.elapsed();
        PROFILER.with(|p| p.borrow_mut().leave(duration));
    }
}

/// A `Profiler` stores the scope tree and keeps track of the currently active
/// scope.
///
/// Note that there is a global thread-local instance of `Profiler` in
/// [`PROFILER`](constant.PROFILER.html), so it is not possible to manually
/// create an instance of `Profiler`.
pub struct Profiler {
    roots: Vec<Rc<RefCell<Scope>>>,
    current: Option<Rc<RefCell<Scope>>>,
}

impl Profiler {
    fn new() -> Profiler {
        Profiler {
            roots: Vec::new(),
            current: None,
        }
    }

    /// Enter a scope. Returns a [`Guard`](struct.Guard.html) that should be
    /// dropped upon leaving the scope.
    ///
    /// Usually, this method will be called by the
    /// [`profile`](macro.profile.html) macro, so it does not need to be used
    /// directly.
    pub fn enter(&mut self, name: &'static str) -> Guard {
        // Check if we have already registered `name` at the current point in
        // the tree.
        let succ = if let Some(current) = self.current.as_ref() {
            // We are currently in some scope.
            let existing_succ = current
                .borrow()
                .succs
                .iter()
                .find(|succ| succ.borrow().name == name)
                .cloned();

            existing_succ.unwrap_or_else(|| {
                // Add new successor node to the current node.
                let new_scope = Scope::new(name, Some(current.clone()));
                let succ = Rc::new(RefCell::new(new_scope));

                current.borrow_mut().succs.push(succ.clone());

                succ
            })
        } else {
            // We are currently not within any scope. Check if `name` already
            // is a root.
            let existing_root = self
                .roots
                .iter()
                .find(|root| root.borrow().name == name)
                .cloned();

            existing_root.unwrap_or_else(|| {
                // Add a new root node.
                let new_scope = Scope::new(name, None);
                let succ = Rc::new(RefCell::new(new_scope));

                self.roots.push(succ.clone());

                succ
            })
        };

        let guard = succ.borrow_mut().enter();

        self.current = Some(succ);

        guard
    }

    /// Completely reset profiling data.
    fn reset(&mut self) {
        self.roots.clear();

        // Note that we could now still be anywhere in the previous profiling
        // tree, so we can not simply reset `self.current`. However, as the
        // frame comes to an end we will eventually leave a root node, at which
        // point `self.current` will be set to `None`.
    }

    /// Leave the current scope.
    fn leave(&mut self, duration: Duration) {
        self.current = if let Some(current) = self.current.as_ref() {
            current.borrow_mut().leave(duration);

            // Set current scope back to the parent node (if any).
            current.borrow().pred.as_ref().cloned()
        } else {
            // This should not happen with proper usage.
            log::error!("Called coarse_prof::leave() while not in any scope");

            None
        };
    }

    fn write<W: io::Write>(&self, out: &mut W) -> io::Result<()> {
        let total_duration = self
            .roots
            .iter()
            .map(|root| root.borrow().duration_sum)
            .sum();

        for root in self.roots.iter() {
            root.borrow().write_recursive(out, total_duration, 0)?;
        }

        out.flush()
    }
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_multiple_roots() {
        super::reset();

        for i in 0..=5 {
            if i == 5 {
                profile!("a");
            }
            {
                profile!("b");
            }
        }

        super::PROFILER.with(|p| {
            let p = p.borrow();

            assert_eq!(p.roots.len(), 2);

            for root in p.roots.iter() {
                assert!(root.borrow().pred.is_none());
                assert!(root.borrow().succs.is_empty());
            }

            assert_eq!(p.roots[0].borrow().name, "b");
            assert_eq!(p.roots[1].borrow().name, "a");

            assert_eq!(p.roots[0].borrow().num_calls, 6);
            assert_eq!(p.roots[1].borrow().num_calls, 1);
        });
    }

    #[test]
    fn test_succ_reuse() {
        use std::ptr;

        super::reset();

        for i in 0..=5 {
            profile!("a");
            if i > 2 {
                profile!("b");
            }
        }

        assert_eq!(super::PROFILER.with(|p| p.borrow().roots.len()), 1);

        super::PROFILER.with(|p| {
            let p = p.borrow();

            assert_eq!(p.roots.len(), 1);

            let root = p.roots[0].borrow();
            assert_eq!(root.name, "a");
            assert!(root.pred.is_none());
            assert_eq!(root.succs.len(), 1);
            assert_eq!(root.num_calls, 6);

            let succ = root.succs[0].borrow();
            assert_eq!(succ.name, "b");
            assert!(ptr::eq(
                succ.pred.as_ref().unwrap().as_ref(),
                p.roots[0].as_ref()
            ));
            assert!(succ.succs.is_empty());
            assert_eq!(succ.num_calls, 3);
        });
    }

    #[test]
    fn test_reset_during_frame() {
        super::reset();

        for i in 0..=5 {
            profile!("a");
            profile!("b");
            {
                profile!("c");
                if i == 5 {
                    super::reset();
                }

                assert!(super::PROFILER.with(|p| p.borrow().current.is_some()));

                profile!("d");
            }
        }

        super::PROFILER.with(|p| {
            let p = p.borrow();

            assert!(p.roots.is_empty());
            assert!(p.current.is_none());
        });
    }
}