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
use derive_more::{Add, AddAssign};
#[derive(Debug, Default, Copy, Clone, Add, AddAssign, PartialEq, Eq)]
pub struct ExecSummary {
pub time_processed_ns: usize,
pub num_produced_rows: usize,
pub num_iterations: usize,
}
pub trait ExecSummaryCollector: Send {
type DurationRecorder;
fn new(output_index: usize) -> Self
where
Self: Sized;
fn on_start_iterate(&mut self) -> Self::DurationRecorder;
fn on_finish_iterate(&mut self, dr: Self::DurationRecorder, rows: usize);
fn collect(&mut self, target: &mut [ExecSummary]);
}
pub struct ExecSummaryCollectorEnabled {
output_index: usize,
counts: ExecSummary,
}
impl ExecSummaryCollector for ExecSummaryCollectorEnabled {
type DurationRecorder = tikv_util::time::Instant;
#[inline]
fn new(output_index: usize) -> ExecSummaryCollectorEnabled {
ExecSummaryCollectorEnabled {
output_index,
counts: Default::default(),
}
}
#[inline]
fn on_start_iterate(&mut self) -> Self::DurationRecorder {
self.counts.num_iterations += 1;
tikv_util::time::Instant::now_coarse()
}
#[inline]
fn on_finish_iterate(&mut self, dr: Self::DurationRecorder, rows: usize) {
self.counts.num_produced_rows += rows;
let elapsed_time = tikv_util::time::duration_to_nanos(dr.elapsed()) as usize;
self.counts.time_processed_ns += elapsed_time;
}
#[inline]
fn collect(&mut self, target: &mut [ExecSummary]) {
let current_summary = std::mem::take(&mut self.counts);
target[self.output_index] += current_summary;
}
}
pub struct ExecSummaryCollectorDisabled;
impl ExecSummaryCollector for ExecSummaryCollectorDisabled {
type DurationRecorder = ();
#[inline]
fn new(_output_index: usize) -> ExecSummaryCollectorDisabled {
ExecSummaryCollectorDisabled
}
#[inline]
fn on_start_iterate(&mut self) -> Self::DurationRecorder {}
#[inline]
fn on_finish_iterate(&mut self, _dr: Self::DurationRecorder, _rows: usize) {}
#[inline]
fn collect(&mut self, _target: &mut [ExecSummary]) {}
}
pub struct WithSummaryCollector<C: ExecSummaryCollector, T> {
pub summary_collector: C,
pub inner: T,
}
pub struct ExecuteStats {
pub summary_per_executor: Vec<ExecSummary>,
pub scanned_rows_per_range: Vec<usize>,
}
impl ExecuteStats {
pub fn new(executors_len: usize) -> Self {
Self {
summary_per_executor: vec![ExecSummary::default(); executors_len],
scanned_rows_per_range: Vec::new(),
}
}
pub fn clear(&mut self) {
for item in self.summary_per_executor.iter_mut() {
*item = ExecSummary::default();
}
self.scanned_rows_per_range.clear();
}
}