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
// Copyright 2018-2024 the Deno authors. All rights reserved. MIT license.
use super::op_driver::OpDriver;
use super::op_driver::OpInflightStats;
use super::ContextState;
use crate::OpId;
use crate::OpState;
use crate::PromiseId;
use crate::ResourceId;
use bit_set::BitSet;
use serde::Serialize;
use serde::Serializer;
use std::cell::Cell;
use std::cell::RefCell;
use std::collections::BTreeMap;
use std::fmt::Display;
use std::ops::Deref;
use std::rc::Rc;

type ActivityId = usize;

/// Fast, const no-trace collection of hashes.
const NO_TRACES: [BTreeMap<ActivityId, Rc<str>>;
  RuntimeActivityType::MAX_TYPE as usize] = [
  BTreeMap::new(),
  BTreeMap::new(),
  BTreeMap::new(),
  BTreeMap::new(),
];

#[derive(Default)]
pub struct RuntimeActivityTraces {
  enabled: Cell<bool>,
  traces: RefCell<
    [BTreeMap<ActivityId, Rc<str>>; RuntimeActivityType::MAX_TYPE as usize],
  >,
}

impl RuntimeActivityTraces {
  pub(crate) fn set_enabled(&self, enabled: bool) {
    self.enabled.set(enabled);
    if !enabled {
      *self.traces.borrow_mut() = Default::default();
    }
  }

  pub(crate) fn submit(
    &self,
    activity_type: RuntimeActivityType,
    id: ActivityId,
    trace: &str,
  ) {
    debug_assert_ne!(
      activity_type,
      RuntimeActivityType::Interval,
      "Use Timer for for timers and intervals"
    );
    self.traces.borrow_mut()[activity_type as usize].insert(id, trace.into());
  }

  pub(crate) fn complete(
    &self,
    activity_type: RuntimeActivityType,
    id: ActivityId,
  ) {
    self.traces.borrow_mut()[activity_type as usize].remove(&id);
  }

  pub fn is_enabled(&self) -> bool {
    self.enabled.get()
  }

  pub fn count(&self) -> usize {
    self.traces.borrow().len()
  }

  pub fn get_all(
    &self,
    mut f: impl FnMut(RuntimeActivityType, ActivityId, &str),
  ) {
    let traces = self.traces.borrow();
    for i in 0..RuntimeActivityType::MAX_TYPE {
      for (key, value) in traces[i as usize].iter() {
        f(RuntimeActivityType::from_u8(i), *key, value.as_ref())
      }
    }
  }

  pub fn capture(
    &self,
  ) -> [BTreeMap<ActivityId, Rc<str>>; RuntimeActivityType::MAX_TYPE as usize]
  {
    if self.is_enabled() {
      self.traces.borrow().clone()
    } else {
      NO_TRACES
    }
  }

  pub fn get<T>(
    &self,
    activity_type: RuntimeActivityType,
    id: ActivityId,
    f: impl FnOnce(Option<&str>) -> T,
  ) -> T {
    f(self.traces.borrow()[activity_type as u8 as usize]
      .get(&id)
      .map(|x| x.as_ref()))
  }
}

#[derive(Clone)]
pub struct RuntimeActivityStatsFactory {
  pub(super) context_state: Rc<ContextState>,
  pub(super) op_state: Rc<RefCell<OpState>>,
}

/// Selects the statistics that you are interested in capturing.
#[derive(Clone, Default, PartialEq, Eq)]
pub struct RuntimeActivityStatsFilter {
  include_timers: bool,
  include_ops: bool,
  include_resources: bool,
  op_filter: BitSet,
}

impl RuntimeActivityStatsFilter {
  pub fn all() -> Self {
    RuntimeActivityStatsFilter {
      include_ops: true,
      include_resources: true,
      include_timers: true,
      op_filter: BitSet::default(),
    }
  }

  pub fn with_ops(mut self) -> Self {
    self.include_ops = true;
    self
  }

  pub fn with_resources(mut self) -> Self {
    self.include_resources = true;
    self
  }

  pub fn with_timers(mut self) -> Self {
    self.include_timers = true;
    self
  }

  pub fn omit_op(mut self, op: OpId) -> Self {
    self.op_filter.insert(op as _);
    self
  }

  pub fn is_empty(&self) -> bool {
    // This ensures we don't miss a newly-added field in the empty comparison
    let Self {
      include_ops,
      include_resources,
      include_timers,
      op_filter: _,
    } = self;
    !(*include_ops) && !(*include_resources) && !(*include_timers)
  }
}

impl RuntimeActivityStatsFactory {
  /// Capture the current runtime activity.
  pub fn capture(
    self,
    filter: &RuntimeActivityStatsFilter,
  ) -> RuntimeActivityStats {
    let resources = if filter.include_resources {
      let res = &self.op_state.borrow().resource_table;
      let mut resources = ResourceOpenStats {
        resources: Vec::with_capacity(res.len()),
      };
      for resource in res.names() {
        resources
          .resources
          .push((resource.0, resource.1.to_string()))
      }
      resources
    } else {
      ResourceOpenStats::default()
    };

    let timers = if filter.include_timers {
      let timer_count = self.context_state.timers.len();
      let mut timers = TimerStats {
        timers: Vec::with_capacity(timer_count),
        repeats: BitSet::with_capacity(timer_count),
      };
      for (timer_id, repeats) in &self.context_state.timers.iter() {
        if repeats {
          timers.repeats.insert(timers.timers.len());
        }
        timers.timers.push(timer_id as usize);
      }
      timers
    } else {
      TimerStats::default()
    };

    let (ops, activity_traces) = if filter.include_ops {
      let ops = self.context_state.pending_ops.stats(&filter.op_filter);
      let activity_traces = self.context_state.activity_traces.capture();
      (ops, activity_traces)
    } else {
      (Default::default(), Default::default())
    };

    RuntimeActivityStats {
      context_state: self.context_state.clone(),
      ops,
      activity_traces,
      resources,
      timers,
    }
  }
}

#[derive(Default)]
pub struct ResourceOpenStats {
  pub(super) resources: Vec<(u32, String)>,
}

#[derive(Default)]
pub struct TimerStats {
  pub(super) timers: Vec<usize>,
  /// `repeats` is a bitset that reports whether a given index in the ID array
  /// is an interval (if true) or a timer (if false).
  pub(super) repeats: BitSet,
}

/// Information about in-flight ops, open resources, active timers and other runtime-specific
/// data that can be used for test sanitization.
pub struct RuntimeActivityStats {
  context_state: Rc<ContextState>,
  pub(super) ops: OpInflightStats,
  pub(super) activity_traces: [BTreeMap<ActivityId, Rc<str>>; 4],
  pub(super) resources: ResourceOpenStats,
  pub(super) timers: TimerStats,
}

/// Contains a runtime activity (op, timer, resource, etc.) stack trace.
#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
#[repr(transparent)]
pub struct RuntimeActivityTrace(Rc<str>);

impl Serialize for RuntimeActivityTrace {
  fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
  where
    S: Serializer,
  {
    self.0.as_ref().serialize(serializer)
  }
}

impl Display for RuntimeActivityTrace {
  fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
    f.write_str(self.0.as_ref())
  }
}

impl Deref for RuntimeActivityTrace {
  type Target = str;
  fn deref(&self) -> &Self::Target {
    self.0.as_ref()
  }
}

impl From<&Rc<str>> for RuntimeActivityTrace {
  fn from(value: &Rc<str>) -> Self {
    Self(value.clone())
  }
}

/// The type of runtime activity being tracked.
#[derive(Debug, Serialize)]
pub enum RuntimeActivity {
  /// An async op, including the promise ID and op name, with an optional trace.
  AsyncOp(PromiseId, Option<RuntimeActivityTrace>, &'static str),
  /// A resource, including the resource ID and name, with an optional trace.
  Resource(ResourceId, Option<RuntimeActivityTrace>, String),
  /// A timer, including the timer ID, with an optional trace.
  Timer(usize, Option<RuntimeActivityTrace>),
  /// An interval, including the interval ID, with an optional trace.
  Interval(usize, Option<RuntimeActivityTrace>),
}

impl RuntimeActivity {
  pub fn activity(&self) -> RuntimeActivityType {
    match self {
      Self::AsyncOp(..) => RuntimeActivityType::AsyncOp,
      Self::Resource(..) => RuntimeActivityType::Resource,
      Self::Timer(..) => RuntimeActivityType::Timer,
      Self::Interval(..) => RuntimeActivityType::Interval,
    }
  }
}

/// A data-less discriminant for [`RuntimeActivity`].
#[derive(
  Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize,
)]
#[repr(u8)]
pub enum RuntimeActivityType {
  AsyncOp,
  Resource,
  Timer,
  Interval,
}

impl RuntimeActivityType {
  const MAX_TYPE: u8 = 4;

  pub(crate) fn from_u8(value: u8) -> Self {
    match value {
      0 => Self::AsyncOp,
      1 => Self::Resource,
      2 => Self::Timer,
      3 => Self::Interval,
      _ => unreachable!(),
    }
  }
}

impl RuntimeActivityStats {
  fn trace_for(
    &self,
    activity_type: RuntimeActivityType,
    id: ActivityId,
  ) -> Option<RuntimeActivityTrace> {
    debug_assert_ne!(
      activity_type,
      RuntimeActivityType::Interval,
      "Use Timer for for timers and intervals"
    );
    self.activity_traces[activity_type as u8 as usize]
      .get(&id)
      .map(|x| x.into())
  }

  /// Capture the data within this [`RuntimeActivityStats`] as a [`RuntimeActivitySnapshot`]
  /// with details of activity.
  pub fn dump(&self) -> RuntimeActivitySnapshot {
    let has_traces = !self.activity_traces.is_empty();
    let mut v = Vec::with_capacity(
      self.ops.ops.len()
        + self.resources.resources.len()
        + self.timers.timers.len(),
    );
    let ops = &self.context_state.op_ctxs;
    if has_traces {
      for op in self.ops.ops.iter() {
        v.push(RuntimeActivity::AsyncOp(
          op.0,
          self.trace_for(RuntimeActivityType::AsyncOp, op.0 as _),
          ops[op.1 as usize].decl.name,
        ));
      }
    } else {
      for op in self.ops.ops.iter() {
        v.push(RuntimeActivity::AsyncOp(
          op.0,
          None,
          ops[op.1 as usize].decl.name,
        ));
      }
    }
    for resource in self.resources.resources.iter() {
      v.push(RuntimeActivity::Resource(
        resource.0,
        None,
        resource.1.clone(),
      ))
    }
    if has_traces {
      for i in 0..self.timers.timers.len() {
        let id = self.timers.timers[i];
        if self.timers.repeats.contains(i) {
          v.push(RuntimeActivity::Interval(
            id,
            self.trace_for(RuntimeActivityType::Timer, id),
          ));
        } else {
          v.push(RuntimeActivity::Timer(
            id,
            self.trace_for(RuntimeActivityType::Timer, id),
          ));
        }
      }
    } else {
      for i in 0..self.timers.timers.len() {
        if self.timers.repeats.contains(i) {
          v.push(RuntimeActivity::Interval(self.timers.timers[i], None));
        } else {
          v.push(RuntimeActivity::Timer(self.timers.timers[i], None));
        }
      }
    }
    RuntimeActivitySnapshot { active: v }
  }

  pub fn diff(before: &Self, after: &Self) -> RuntimeActivityDiff {
    let mut appeared = vec![];
    let mut disappeared = vec![];
    let ops = &before.context_state.op_ctxs;

    let mut a = BitSet::new();
    for op in after.ops.ops.iter() {
      a.insert(op.0 as usize);
    }
    for op in before.ops.ops.iter() {
      if a.remove(op.0 as usize) {
        // continuing op
      } else {
        // before, but not after
        disappeared.push(RuntimeActivity::AsyncOp(
          op.0,
          before.trace_for(RuntimeActivityType::AsyncOp, op.0 as _),
          ops[op.1 as usize].decl.name,
        ));
      }
    }
    for op in after.ops.ops.iter() {
      if a.contains(op.0 as usize) {
        // after but not before
        appeared.push(RuntimeActivity::AsyncOp(
          op.0,
          after.trace_for(RuntimeActivityType::AsyncOp, op.0 as _),
          ops[op.1 as usize].decl.name,
        ));
      }
    }

    let mut a = BitSet::new();
    for op in after.resources.resources.iter() {
      a.insert(op.0 as usize);
    }
    for op in before.resources.resources.iter() {
      if a.remove(op.0 as usize) {
        // continuing op
      } else {
        // before, but not after
        disappeared.push(RuntimeActivity::Resource(op.0, None, op.1.clone()));
      }
    }
    for op in after.resources.resources.iter() {
      if a.contains(op.0 as usize) {
        // after but not before
        appeared.push(RuntimeActivity::Resource(op.0, None, op.1.clone()));
      }
    }

    let mut a = BitSet::new();
    for timer in after.timers.timers.iter() {
      a.insert(*timer);
    }
    for index in 0..before.timers.timers.len() {
      let timer = before.timers.timers[index];
      if a.remove(timer) {
        // continuing op
      } else {
        // before, but not after
        if before.timers.repeats.contains(index) {
          disappeared.push(RuntimeActivity::Interval(
            timer,
            before.trace_for(RuntimeActivityType::Timer, timer),
          ));
        } else {
          disappeared.push(RuntimeActivity::Timer(
            timer,
            before.trace_for(RuntimeActivityType::Timer, timer),
          ));
        }
      }
    }
    for index in 0..after.timers.timers.len() {
      let timer = after.timers.timers[index];
      if a.contains(timer) {
        // after but not before
        if after.timers.repeats.contains(index) {
          appeared.push(RuntimeActivity::Interval(
            timer,
            after.trace_for(RuntimeActivityType::Timer, timer),
          ));
        } else {
          appeared.push(RuntimeActivity::Timer(
            timer,
            after.trace_for(RuntimeActivityType::Timer, timer),
          ));
        }
      }
    }

    RuntimeActivityDiff {
      appeared,
      disappeared,
    }
  }
}

#[derive(Debug, Serialize)]
pub struct RuntimeActivityDiff {
  pub appeared: Vec<RuntimeActivity>,
  pub disappeared: Vec<RuntimeActivity>,
}

impl RuntimeActivityDiff {
  pub fn is_empty(&self) -> bool {
    self.appeared.is_empty() && self.disappeared.is_empty()
  }
}

#[derive(Debug, Serialize)]
pub struct RuntimeActivitySnapshot {
  pub active: Vec<RuntimeActivity>,
}