cido 0.2.0

Core traits and implementations for indexing with cido
Documentation
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
//! This should be made into it's own crate or use something that is already public
use core::future::Future;
use std::{
  cell::{Cell, RefCell},
  collections::HashMap,
  pin::Pin,
  task::{Context, Poll},
  time::{Duration, Instant},
};

use arrayvec::ArrayVec;
use serde::{Deserialize, Serialize};

pub(crate) const METRICS_DEPTH: usize = 8;

#[derive(PartialEq, Eq, Hash, Clone, Debug, PartialOrd, Ord, Serialize)]
#[serde(transparent)]
pub struct MetricsDepthArrayVec {
  inner: ArrayVec<&'static str, METRICS_DEPTH>,
}

impl MetricsDepthArrayVec {
  #[inline]
  pub fn into_inner(self) -> ArrayVec<&'static str, METRICS_DEPTH> {
    self.inner
  }
}

impl std::ops::Deref for MetricsDepthArrayVec {
  type Target = ArrayVec<&'static str, METRICS_DEPTH>;

  fn deref(&self) -> &Self::Target {
    &self.inner
  }
}

impl std::ops::DerefMut for MetricsDepthArrayVec {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.inner
  }
}

impl From<ArrayVec<&'static str, METRICS_DEPTH>> for MetricsDepthArrayVec {
  fn from(v: ArrayVec<&'static str, METRICS_DEPTH>) -> Self {
    Self { inner: v }
  }
}

impl From<MetricsDepthArrayVec> for ArrayVec<&'static str, METRICS_DEPTH> {
  fn from(v: MetricsDepthArrayVec) -> Self {
    v.inner
  }
}

#[derive(Serialize, Clone, Debug, Default)]
#[serde(transparent)]
pub struct MetricMap {
  pub inner: HashMap<&'static str, FutureMetrics>,
}

impl std::ops::Deref for MetricMap {
  type Target = HashMap<&'static str, FutureMetrics>;

  fn deref(&self) -> &Self::Target {
    &self.inner
  }
}

impl std::ops::DerefMut for MetricMap {
  fn deref_mut(&mut self) -> &mut Self::Target {
    &mut self.inner
  }
}

impl MetricMap {
  fn with_capacity(cap: usize) -> Self {
    Self {
      inner: HashMap::with_capacity(cap),
    }
  }
}

pub type MetricStackMap = HashMap<MetricsDepthArrayVec, FutureMetrics>;

pub fn take_metrics() -> (Option<MetricMap>, Option<MetricStackMap>) {
  STACK.with(|s| {
    let stack = s.inner.borrow();
    if stack.len() > 1 {
      panic!("take_metrics should only be called from a MeterRoot future");
    }
  });
  let metrics = METRICS.with(|m| {
    let mut map = m.borrow_mut();
    let mut new_map = Some(MetricMap::with_capacity(
      map.as_ref().map_or(0, |x| x.len()) + 8,
    ));
    std::mem::swap(&mut *map, &mut new_map);
    new_map
  });

  let metrics_stack = METRICS_STACK.with(|m| {
    let mut map = m.borrow_mut();
    let mut new_map = Some(HashMap::with_capacity(
      map.as_ref().map_or(0, HashMap::len) + 8,
    ));
    std::mem::swap(&mut *map, &mut new_map);
    new_map
  });

  (metrics, metrics_stack)
}

/// Allows for measuring execution time for each future individually and within a call stack
///
/// We don't expose these metrics yet, but will in the future
///
/// This trait should be extracted into it's own library or we need to migrate to a library
/// that provides something similar
pub trait FutureMeter<'a>: Sized {
  /// Add an entry in the call stack for this future
  fn meter(self, name: &'static str) -> Meter<Self> {
    self.meter_with_logging(name, Duration::ZERO)
  }

  /// Similar to `meter_with_logging` but specifies interval in milliseconds
  fn meter_ms(self, name: &'static str, log_interval_ms: u64) -> Meter<Self> {
    self.meter_with_logging(name, Duration::from_millis(log_interval_ms))
  }

  /// Add an entry in the call stack for this future and log when it has been running for more than
  /// `log_interval` and when it finishes after `log_interval`
  fn meter_with_logging(self, name: &'static str, log_interval: Duration) -> Meter<Self> {
    let now = Instant::now();
    // let name = ArrayString::from(&name[..NAME_LEN]).unwrap();
    Meter {
      new: true,
      name,
      created: now,
      last_logged: now,
      log_interval,
      metrics: FutureMetrics::new(now),
      f: self,
    }
  }

  /// Like `meter` but creates a root that other futures can use
  ///
  /// Multiple roots are ok to use, but creating a new one will disassociate the existing call stack
  ///
  /// This needs to be used for all futures that are at the base of the call stack that will be measured.
  fn meter_root(self, name: &'static str) -> MeterRoot<Self> {
    self.meter_root_with_logging(name, Duration::ZERO)
  }

  /// Like `meter_ms` but creates a root that other futures can use
  ///
  /// Multiple roots are ok to use, but creating a new one will disassociate the existing call stack
  ///
  /// This needs to be used for all futures that are at the base of the call stack that will be measured.
  fn meter_root_ms(self, name: &'static str, log_interval_ms: u64) -> MeterRoot<Self> {
    self.meter_root_with_logging(name, Duration::from_millis(log_interval_ms))
  }

  /// Like `meter_with_logging` but creates a root that other futures can use
  ///
  /// Multiple roots are ok to use, but creating a new one will disassociate the existing call stack
  ///
  /// This needs to be used for all futures that are at the base of the call stack that will be measured.
  fn meter_root_with_logging(self, name: &'static str, log_interval: Duration) -> MeterRoot<Self> {
    MeterRoot {
      metrics_stack: Some(HashMap::with_capacity(32)),
      metrics: Some(MetricMap::with_capacity(32)),
      has_own_metrics: true,
      inner: self.meter_with_logging(name, log_interval),
    }
  }
}

impl<'a, F: Future + Sized> FutureMeter<'a> for F {}

pub struct MeterRoot<F> {
  metrics_stack: Option<MetricStackMap>,
  metrics: Option<MetricMap>,
  has_own_metrics: bool,
  inner: Meter<F>,
}

pub struct MeterRootOutput<O> {
  metrics_stack: MetricStackMap,
  metrics: MetricMap,
  inner: O,
}

impl<O> MeterRootOutput<O> {
  /// Use this if you do not want to aggregate values into the existing MeterRoot
  pub fn into_inner(self) -> O {
    self.inner
  }
  /// Use this if you do want to aggregate values into the existing MeterRoot from this one
  pub fn aggregate_values(self) -> O {
    METRICS.with(|m| {
      let mut ref_mut = m.borrow_mut();
      let Some(map) = ref_mut.as_mut() else {
        return;
      };
      for (key, val) in self.metrics.inner {
        match map.entry(key) {
          std::collections::hash_map::Entry::Occupied(o) => {
            o.into_mut().sum(val);
          }
          std::collections::hash_map::Entry::Vacant(v) => {
            v.insert(val);
          }
        }
      }
    });
    METRICS_STACK.with(|m| {
      let mut ref_mut = m.borrow_mut();
      let Some(map) = ref_mut.as_mut() else {
        return;
      };
      for (key, val) in self.metrics_stack {
        match map.entry(key) {
          std::collections::hash_map::Entry::Occupied(o) => {
            o.into_mut().sum(val);
          }
          std::collections::hash_map::Entry::Vacant(v) => {
            v.insert(val);
          }
        }
      }
    });
    self.inner
  }
}

pub struct Meter<F> {
  new: bool,
  name: &'static str,
  created: Instant,
  last_logged: Instant,
  log_interval: Duration,
  metrics: FutureMetrics,
  f: F,
}

#[serde_with::serde_as]
#[derive(Copy, Clone, Debug, Serialize, Deserialize)]
pub struct FutureMetrics {
  entered: u64,
  called: u64,
  #[serde(skip)]
  recent_aggregation: Option<Instant>,
  #[serde_as(as = "serde_with::DurationNanoSeconds")]
  processing: Duration,
  #[serde_as(as = "serde_with::DurationNanoSeconds")]
  total: Duration,
}

impl FutureMetrics {
  fn new(start: Instant) -> Self {
    Self {
      entered: 0,
      called: 0,
      recent_aggregation: Some(start),
      processing: Duration::ZERO,
      total: Duration::ZERO,
    }
  }
  pub fn total(&self) -> Duration {
    self.total
  }
  fn aggregate(&mut self, now: Instant, processing_start: Instant, is_ready: bool, new: bool) {
    self.entered += 1;
    self.called += u64::from(new);
    self.processing += now - processing_start;
    self.total += now - self.recent_aggregation.unwrap_or(processing_start);
    self.recent_aggregation = (!is_ready).then_some(now);
  }
  fn sum(&mut self, other: Self) {
    self.entered += other.entered;
    self.called += other.called;
    self.recent_aggregation = self.recent_aggregation.max(other.recent_aggregation);
    self.total += other.total;
    self.processing += other.processing;
  }
}

impl core::fmt::Display for FutureMetrics {
  fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    let Self {
      entered,
      called,
      processing,
      total,
      recent_aggregation: _,
    } = *self;
    write!(
      f,
      "called: {called:9}, entered: {entered:9}, processing: {processing:?}, total: {total:?}"
    )
  }
}

impl<F> MeterRoot<F> {
  fn swap(&mut self, assert: bool) {
    METRICS.with(|m| {
      let mut map = m.borrow_mut();
      if assert {
        debug_assert!(
          self.metrics.is_some(),
          "MeterRoot should have metrics when not being polled"
        );
      }
      std::mem::swap(&mut self.metrics, &mut *map);
    });
    METRICS_STACK.with(|m| {
      let mut map = m.borrow_mut();
      if assert {
        debug_assert!(
          self.metrics_stack.is_some(),
          "MeterRoot should have metrics_stack when not being polled"
        );
      }
      std::mem::swap(&mut self.metrics_stack, &mut *map);
    });
    self.has_own_metrics = !self.has_own_metrics;
  }
}

impl<F> Drop for MeterRoot<F> {
  fn drop(&mut self) {
    if !self.has_own_metrics {
      self.swap(false);
    }
  }
}

impl<F: Future> Future for MeterRoot<F> {
  type Output = MeterRootOutput<<F as Future>::Output>;

  fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
    let this = unsafe { self.get_unchecked_mut() };
    POLL_START.with(|p| p.set(Instant::now()));
    this.swap(true);
    let f = unsafe { Pin::new_unchecked(&mut this.inner) };
    let poll = f.poll(cx);
    // cleanup no matter what the result of poll is.
    this.swap(false);
    poll.map(|o| MeterRootOutput {
      metrics_stack: this.metrics_stack.take().unwrap_or_default(),
      metrics: this.metrics.take().unwrap_or_default(),
      inner: o,
    })
  }
}

impl<F: Future> Future for Meter<F> {
  type Output = <F as Future>::Output;
  fn poll(self: Pin<&mut Self>, cx: &mut Context<'_>) -> Poll<Self::Output> {
    // Safety: we never move this
    let this = unsafe { self.get_unchecked_mut() };
    let new = this.new;
    this.new = false;
    let guard = STACK.with(|s| s.push(this.name));
    // Safety: we never move f
    let f = unsafe { Pin::new_unchecked(&mut this.f) };
    match f.poll(cx) {
      Poll::Pending => {
        STACK.with(|stack| {
          if stack.inner.borrow().len() == guard.index + 1 {
            // this is the one initiating the pending call, so set the instant
            PENDING_RETURNED.with(|i| i.set(Instant::now()))
          }
        });
        let now = PENDING_RETURNED.with(Cell::get);
        let poll_start = POLL_START.with(Cell::get).max(this.created);
        this.metrics.aggregate(now, poll_start, false, new);
        if this.log_interval > Duration::ZERO && now - this.last_logged >= this.log_interval {
          this.last_logged = now;
          STACK.with(|stack| {
            let stack = stack.inner.borrow();
            tracing::trace!(
              stack = ?stack.as_slice(),
              total = ?this.metrics.total,
              processing = ?this.metrics.processing,
              entered = %this.metrics.entered,
              "Future working",
            );
          })
        }
        METRICS.with(|m| {
          let mut map = m.borrow_mut();
          let Some(map) = map.as_mut() else {
            return;
          };
          let metrics = map
            .entry(this.name)
            .or_insert_with(|| FutureMetrics::new(this.created));
          metrics.aggregate(now, poll_start, false, new);
        });
        // skip root
        if guard.index > 0 {
          METRICS_STACK.with(|m| {
            let mut map = m.borrow_mut();
            let Some(map) = map.as_mut() else {
              return;
            };
            STACK.with(|stack| {
              let stack = stack.inner.borrow();
              let array = stack[stack.len().saturating_sub(METRICS_DEPTH)..]
                .iter()
                .copied()
                .collect::<ArrayVec<_, METRICS_DEPTH>>();
              let metrics = map
                .entry(array.into())
                .or_insert_with(|| FutureMetrics::new(this.created));
              metrics.aggregate(now, poll_start, false, new);
            });
          });
        }
        Poll::Pending
      }
      Poll::Ready(o) => {
        let now = Instant::now();
        let poll_start = this.created.max(POLL_START.with(Cell::get));
        this.metrics.aggregate(now, poll_start, true, new);
        if now - this.last_logged > this.log_interval {
          STACK.with(|stack| {
            let stack = stack.inner.borrow();
            tracing::debug!(
              stack = ?stack.as_slice(),
              total = ?this.metrics.total,
              processing = ?this.metrics.processing,
              entered = %this.metrics.entered,
              "Future complete",
            );
          });
        }
        METRICS.with(|m| {
          let mut map = m.borrow_mut();
          let Some(map) = map.as_mut() else {
            return;
          };
          let metrics = map
            .entry(this.name)
            .or_insert_with(|| FutureMetrics::new(this.created));
          metrics.aggregate(now, poll_start, true, new);
        });
        // skip root
        if guard.index > 0 {
          METRICS_STACK.with(|m| {
            let mut map = m.borrow_mut();
            let Some(map) = map.as_mut() else {
              return;
            };
            STACK.with(|stack| {
              let stack = stack.inner.borrow();
              let array = stack[stack.len().saturating_sub(METRICS_DEPTH)..]
                .iter()
                .copied()
                .collect::<ArrayVec<_, METRICS_DEPTH>>();
              let metrics = map
                .entry(array.into())
                .or_insert_with(|| FutureMetrics::new(this.created));
              metrics.aggregate(now, poll_start, true, new);
            });
          });
        }
        Poll::Ready(o)
      }
    }
  }
}

thread_local! {
  static STACK: OwnedStack<&'static str> = OwnedStack::with_capacity(20);
  static POLL_START: Cell<Instant> = Cell::new(Instant::now());
  static PENDING_RETURNED: Cell<Instant> = Cell::new(Instant::now());
  static METRICS_STACK: RefCell<Option<MetricStackMap>> = const { RefCell::new(None) };
  static METRICS: RefCell<Option<MetricMap>> = const { RefCell::new(None) };
}

pub struct OwnedStackGuard<T> {
  index: usize,
  _marker: std::marker::PhantomData<fn() -> T>,
}

impl<T> Drop for OwnedStackGuard<T> {
  fn drop(&mut self) {
    STACK.with(|stack| {
      let mut stack = stack.inner.borrow_mut();
      assert_eq!(stack.len(), self.index + 1);
      let _ = stack.pop();
    });
  }
}

#[derive(Default, Debug)]
#[must_use]
pub struct OwnedStack<T> {
  inner: RefCell<Vec<T>>,
}

impl<T> OwnedStack<T> {
  pub fn with_capacity(capacity: usize) -> Self {
    Self {
      inner: RefCell::new(Vec::with_capacity(capacity)),
    }
  }
  pub fn push(&self, t: T) -> OwnedStackGuard<T> {
    let mut inner = self.inner.borrow_mut();
    let index = inner.len();
    inner.push(t);
    OwnedStackGuard {
      index,
      _marker: std::marker::PhantomData,
    }
  }
}