1 | //===----------------------------------------------------------------------===// |
2 | // DuckDB |
3 | // |
4 | // duckdb/common/cycle_counter.hpp |
5 | // |
6 | // |
7 | //===----------------------------------------------------------------------===// |
8 | |
9 | #pragma once |
10 | |
11 | #include "duckdb/common/helper.hpp" |
12 | #include "duckdb/common/chrono.hpp" |
13 | |
14 | namespace duckdb { |
15 | |
16 | //! The cycle counter can be used to measure elapsed cycles for a function, expression and ... |
17 | //! Optimized by sampling mechanism. Once per 100 times. |
18 | //! //Todo Can be optimized further by calling RDTSC once per sample |
19 | class CycleCounter { |
20 | friend struct ExpressionInfo; |
21 | friend struct ExpressionRootInfo; |
22 | static constexpr int SAMPLING_RATE = 50; |
23 | |
24 | public: |
25 | CycleCounter() { |
26 | } |
27 | // Next_sample determines if a sample needs to be taken, if so start the profiler |
28 | void BeginSample() { |
29 | if (current_count >= next_sample) { |
30 | tmp = Tick(); |
31 | } |
32 | } |
33 | |
34 | // End the sample |
35 | void EndSample(int chunk_size) { |
36 | if (current_count >= next_sample) { |
37 | time += Tick() - tmp; |
38 | } |
39 | if (current_count >= next_sample) { |
40 | next_sample = SAMPLING_RATE; |
41 | ++sample_count; |
42 | sample_tuples_count += chunk_size; |
43 | current_count = 0; |
44 | } else { |
45 | ++current_count; |
46 | } |
47 | tuples_count += chunk_size; |
48 | } |
49 | |
50 | private: |
51 | uint64_t Tick() const; |
52 | // current number on RDT register |
53 | uint64_t tmp; |
54 | // Elapsed cycles |
55 | uint64_t time = 0; |
56 | //! Count the number of time the executor called since last sampling |
57 | uint64_t current_count = 0; |
58 | //! Show the next sample |
59 | uint64_t next_sample = 0; |
60 | //! Count the number of samples |
61 | uint64_t sample_count = 0; |
62 | //! Count the number of tuples sampled |
63 | uint64_t sample_tuples_count = 0; |
64 | //! Count the number of ALL tuples |
65 | uint64_t tuples_count = 0; |
66 | }; |
67 | |
68 | } // namespace duckdb |
69 | |