1 | #include <Common/ProfileEvents.h> |
2 | #include <Common/CurrentThread.h> |
3 | #include <Common/typeid_cast.h> |
4 | #include <Columns/ColumnArray.h> |
5 | |
6 | /// Available events. Add something here as you wish. |
7 | #define APPLY_FOR_EVENTS(M) \ |
8 | M(Query, "Number of queries to be interpreted and potentially executed. Does not include queries that failed to parse or were rejected due to AST size limits, quota limits or limits on the number of simultaneously running queries. May include internal queries initiated by ClickHouse itself. Does not count subqueries.") \ |
9 | M(SelectQuery, "Same as Query, but only for SELECT queries.") \ |
10 | M(InsertQuery, "Same as Query, but only for INSERT queries.") \ |
11 | M(FileOpen, "Number of files opened.") \ |
12 | M(Seek, "Number of times the 'lseek' function was called.") \ |
13 | M(ReadBufferFromFileDescriptorRead, "Number of reads (read/pread) from a file descriptor. Does not include sockets.") \ |
14 | M(ReadBufferFromFileDescriptorReadFailed, "Number of times the read (read/pread) from a file descriptor have failed.") \ |
15 | M(ReadBufferFromFileDescriptorReadBytes, "Number of bytes read from file descriptors. If the file is compressed, this will show the compressed data size.") \ |
16 | M(WriteBufferFromFileDescriptorWrite, "Number of writes (write/pwrite) to a file descriptor. Does not include sockets.") \ |
17 | M(WriteBufferFromFileDescriptorWriteFailed, "Number of times the write (write/pwrite) to a file descriptor have failed.") \ |
18 | M(WriteBufferFromFileDescriptorWriteBytes, "Number of bytes written to file descriptors. If the file is compressed, this will show compressed data size.") \ |
19 | M(ReadBufferAIORead, "") \ |
20 | M(ReadBufferAIOReadBytes, "") \ |
21 | M(WriteBufferAIOWrite, "") \ |
22 | M(WriteBufferAIOWriteBytes, "") \ |
23 | M(ReadCompressedBytes, "") \ |
24 | M(CompressedReadBufferBlocks, "") \ |
25 | M(CompressedReadBufferBytes, "") \ |
26 | M(UncompressedCacheHits, "") \ |
27 | M(UncompressedCacheMisses, "") \ |
28 | M(UncompressedCacheWeightLost, "") \ |
29 | M(IOBufferAllocs, "") \ |
30 | M(IOBufferAllocBytes, "") \ |
31 | M(ArenaAllocChunks, "") \ |
32 | M(ArenaAllocBytes, "") \ |
33 | M(FunctionExecute, "") \ |
34 | M(TableFunctionExecute, "") \ |
35 | M(MarkCacheHits, "") \ |
36 | M(MarkCacheMisses, "") \ |
37 | M(CreatedReadBufferOrdinary, "") \ |
38 | M(CreatedReadBufferAIO, "") \ |
39 | M(CreatedReadBufferAIOFailed, "") \ |
40 | M(CreatedWriteBufferOrdinary, "") \ |
41 | M(CreatedWriteBufferAIO, "") \ |
42 | M(CreatedWriteBufferAIOFailed, "") \ |
43 | M(DiskReadElapsedMicroseconds, "Total time spent waiting for read syscall. This include reads from page cache.") \ |
44 | M(DiskWriteElapsedMicroseconds, "Total time spent waiting for write syscall. This include writes to page cache.") \ |
45 | M(NetworkReceiveElapsedMicroseconds, "") \ |
46 | M(NetworkSendElapsedMicroseconds, "") \ |
47 | M(ThrottlerSleepMicroseconds, "Total time a query was sleeping to conform the 'max_network_bandwidth' setting.") \ |
48 | \ |
49 | M(QueryMaskingRulesMatch, "Number of times query masking rules was successfully matched.") \ |
50 | \ |
51 | M(ReplicatedPartFetches, "Number of times a data part was downloaded from replica of a ReplicatedMergeTree table.") \ |
52 | M(ReplicatedPartFailedFetches, "") \ |
53 | M(ObsoleteReplicatedParts, "") \ |
54 | M(ReplicatedPartMerges, "") \ |
55 | M(ReplicatedPartFetchesOfMerged, "Number of times we prefer to download already merged part from replica of ReplicatedMergeTree table instead of performing a merge ourself (usually we prefer doing a merge ourself to save network traffic). This happens when we have not all source parts to perform a merge or when the data part is old enough.") \ |
56 | M(ReplicatedPartMutations, "") \ |
57 | M(ReplicatedPartChecks, "") \ |
58 | M(ReplicatedPartChecksFailed, "") \ |
59 | M(ReplicatedDataLoss, "Number of times a data part that we wanted doesn't exist on any replica (even on replicas that are offline right now). That data parts are definitely lost. This is normal due to asynchronous replication (if quorum inserts were not enabled), when the replica on which the data part was written was failed and when it became online after fail it doesn't contain that data part.") \ |
60 | \ |
61 | M(InsertedRows, "Number of rows INSERTed to all tables.") \ |
62 | M(InsertedBytes, "Number of bytes (uncompressed; for columns as they stored in memory) INSERTed to all tables.") \ |
63 | M(DelayedInserts, "Number of times the INSERT of a block to a MergeTree table was throttled due to high number of active data parts for partition.") \ |
64 | M(RejectedInserts, "Number of times the INSERT of a block to a MergeTree table was rejected with 'Too many parts' exception due to high number of active data parts for partition.") \ |
65 | M(DelayedInsertsMilliseconds, "Total number of milliseconds spent while the INSERT of a block to a MergeTree table was throttled due to high number of active data parts for partition.") \ |
66 | M(DuplicatedInsertedBlocks, "Number of times the INSERTed block to a ReplicatedMergeTree table was deduplicated.") \ |
67 | \ |
68 | M(ZooKeeperInit, "") \ |
69 | M(ZooKeeperTransactions, "") \ |
70 | M(ZooKeeperList, "") \ |
71 | M(ZooKeeperCreate, "") \ |
72 | M(ZooKeeperRemove, "") \ |
73 | M(ZooKeeperExists, "") \ |
74 | M(ZooKeeperGet, "") \ |
75 | M(ZooKeeperSet, "") \ |
76 | M(ZooKeeperMulti, "") \ |
77 | M(ZooKeeperCheck, "") \ |
78 | M(ZooKeeperClose, "") \ |
79 | M(ZooKeeperWatchResponse, "") \ |
80 | M(ZooKeeperUserExceptions, "") \ |
81 | M(ZooKeeperHardwareExceptions, "") \ |
82 | M(ZooKeeperOtherExceptions, "") \ |
83 | M(ZooKeeperWaitMicroseconds, "") \ |
84 | M(ZooKeeperBytesSent, "") \ |
85 | M(ZooKeeperBytesReceived, "") \ |
86 | \ |
87 | M(DistributedConnectionFailTry, "") \ |
88 | M(DistributedConnectionMissingTable, "") \ |
89 | M(DistributedConnectionStaleReplica, "") \ |
90 | M(DistributedConnectionFailAtAll, "") \ |
91 | \ |
92 | M(CompileAttempt, "Number of times a compilation of generated C++ code was initiated.") \ |
93 | M(CompileSuccess, "Number of times a compilation of generated C++ code was successful.") \ |
94 | \ |
95 | M(CompileFunction, "Number of times a compilation of generated LLVM code (to create fused function for complex expressions) was initiated.") \ |
96 | M(CompiledFunctionExecute, "Number of times a compiled function was executed.") \ |
97 | M(CompileExpressionsMicroseconds, "Total time spent for compilation of expressions to LLVM code.") \ |
98 | M(CompileExpressionsBytes, "Number of bytes used for expressions compilation.") \ |
99 | \ |
100 | M(ExternalSortWritePart, "") \ |
101 | M(ExternalSortMerge, "") \ |
102 | M(ExternalAggregationWritePart, "") \ |
103 | M(ExternalAggregationMerge, "") \ |
104 | M(ExternalAggregationCompressedBytes, "") \ |
105 | M(ExternalAggregationUncompressedBytes, "") \ |
106 | \ |
107 | M(SlowRead, "Number of reads from a file that were slow. This indicate system overload. Thresholds are controlled by read_backoff_* settings.") \ |
108 | M(ReadBackoff, "Number of times the number of query processing threads was lowered due to slow reads.") \ |
109 | \ |
110 | M(ReplicaYieldLeadership, "Number of times Replicated table was yielded its leadership due to large replication lag relative to other replicas.") \ |
111 | M(ReplicaPartialShutdown, "") \ |
112 | \ |
113 | M(SelectedParts, "Number of data parts selected to read from a MergeTree table.") \ |
114 | M(SelectedRanges, "Number of (non-adjacent) ranges in all data parts selected to read from a MergeTree table.") \ |
115 | M(SelectedMarks, "Number of marks (index granules) selected to read from a MergeTree table.") \ |
116 | \ |
117 | M(Merge, "Number of launched background merges.") \ |
118 | M(MergedRows, "Rows read for background merges. This is the number of rows before merge.") \ |
119 | M(MergedUncompressedBytes, "Uncompressed bytes (for columns as they stored in memory) that was read for background merges. This is the number before merge.") \ |
120 | M(MergesTimeMilliseconds, "Total time spent for background merges.")\ |
121 | \ |
122 | M(MergeTreeDataWriterRows, "Number of rows INSERTed to MergeTree tables.") \ |
123 | M(MergeTreeDataWriterUncompressedBytes, "Uncompressed bytes (for columns as they stored in memory) INSERTed to MergeTree tables.") \ |
124 | M(MergeTreeDataWriterCompressedBytes, "Bytes written to filesystem for data INSERTed to MergeTree tables.") \ |
125 | M(MergeTreeDataWriterBlocks, "Number of blocks INSERTed to MergeTree tables. Each block forms a data part of level zero.") \ |
126 | M(MergeTreeDataWriterBlocksAlreadySorted, "Number of blocks INSERTed to MergeTree tables that appeared to be already sorted.") \ |
127 | \ |
128 | M(CannotRemoveEphemeralNode, "Number of times an error happened while trying to remove ephemeral node. This is not an issue, because our implementation of ZooKeeper library guarantee that the session will expire and the node will be removed.") \ |
129 | M(LeaderElectionAcquiredLeadership, "Number of times a ReplicatedMergeTree table became a leader. Leader replica is responsible for assigning merges, cleaning old blocks for deduplications and a few more bookkeeping tasks.") \ |
130 | \ |
131 | M(RegexpCreated, "Compiled regular expressions. Identical regular expressions compiled just once and cached forever.") \ |
132 | M(ContextLock, "Number of times the lock of Context was acquired or tried to acquire. This is global lock.") \ |
133 | \ |
134 | M(StorageBufferFlush, "") \ |
135 | M(StorageBufferErrorOnFlush, "") \ |
136 | M(StorageBufferPassedAllMinThresholds, "") \ |
137 | M(StorageBufferPassedTimeMaxThreshold, "") \ |
138 | M(StorageBufferPassedRowsMaxThreshold, "") \ |
139 | M(StorageBufferPassedBytesMaxThreshold, "") \ |
140 | \ |
141 | M(DictCacheKeysRequested, "") \ |
142 | M(DictCacheKeysRequestedMiss, "") \ |
143 | M(DictCacheKeysRequestedFound, "") \ |
144 | M(DictCacheKeysExpired, "") \ |
145 | M(DictCacheKeysNotFound, "") \ |
146 | M(DictCacheKeysHit, "") \ |
147 | M(DictCacheRequestTimeNs, "") \ |
148 | M(DictCacheRequests, "") \ |
149 | M(DictCacheLockWriteNs, "") \ |
150 | M(DictCacheLockReadNs, "") \ |
151 | \ |
152 | M(DistributedSyncInsertionTimeoutExceeded, "") \ |
153 | M(DataAfterMergeDiffersFromReplica, "") \ |
154 | M(DataAfterMutationDiffersFromReplica, "") \ |
155 | M(PolygonsAddedToPool, "") \ |
156 | M(PolygonsInPoolAllocatedBytes, "") \ |
157 | M(RWLockAcquiredReadLocks, "") \ |
158 | M(RWLockAcquiredWriteLocks, "") \ |
159 | M(RWLockReadersWaitMilliseconds, "") \ |
160 | M(RWLockWritersWaitMilliseconds, "") \ |
161 | M(NetworkErrors, "") \ |
162 | \ |
163 | M(RealTimeMicroseconds, "Total (wall clock) time spent in processing (queries and other tasks) threads (not that this is a sum).") \ |
164 | M(UserTimeMicroseconds, "Total time spent in processing (queries and other tasks) threads executing CPU instructions in user space. This include time CPU pipeline was stalled due to cache misses, branch mispredictions, hyper-threading, etc.") \ |
165 | M(SystemTimeMicroseconds, "Total time spent in processing (queries and other tasks) threads executing CPU instructions in OS kernel space. This include time CPU pipeline was stalled due to cache misses, branch mispredictions, hyper-threading, etc.") \ |
166 | M(SoftPageFaults, "") \ |
167 | M(HardPageFaults, "") \ |
168 | M(VoluntaryContextSwitches, "") \ |
169 | M(InvoluntaryContextSwitches, "") \ |
170 | \ |
171 | M(OSIOWaitMicroseconds, "Total time a thread spent waiting for a result of IO operation, from the OS point of view. This is real IO that doesn't include page cache.") \ |
172 | M(OSCPUWaitMicroseconds, "Total time a thread was ready for execution but waiting to be scheduled by OS, from the OS point of view.") \ |
173 | M(OSCPUVirtualTimeMicroseconds, "CPU time spent seen by OS. Does not include involuntary waits due to virtualization.") \ |
174 | M(OSReadBytes, "Number of bytes read from disks or block devices. Doesn't include bytes read from page cache. May include excessive data due to block size, readahead, etc.") \ |
175 | M(OSWriteBytes, "Number of bytes written to disks or block devices. Doesn't include bytes that are in page cache dirty pages. May not include data that was written by OS asynchronously.") \ |
176 | M(OSReadChars, "Number of bytes read from filesystem, including page cache.") \ |
177 | M(OSWriteChars, "Number of bytes written to filesystem, including page cache.") \ |
178 | M(CreatedHTTPConnections, "Total amount of created HTTP connections (closed or opened).") \ |
179 | \ |
180 | M(CannotWriteToWriteBufferDiscard, "Number of stack traces dropped by query profiler or signal handler because pipe is full or cannot write to pipe.") \ |
181 | M(QueryProfilerSignalOverruns, "Number of times we drop processing of a signal due to overrun plus the number of signals that OS has not delivered due to overrun.") \ |
182 | |
183 | namespace ProfileEvents |
184 | { |
185 | |
186 | #define M(NAME, DOCUMENTATION) extern const Event NAME = __COUNTER__; |
187 | APPLY_FOR_EVENTS(M) |
188 | #undef M |
189 | constexpr Event END = __COUNTER__; |
190 | |
191 | /// Global variable, initialized by zeros. |
192 | Counter global_counters_array[END] {}; |
193 | /// Initialize global counters statically |
194 | Counters global_counters(global_counters_array); |
195 | |
196 | const Event Counters::num_counters = END; |
197 | |
198 | |
199 | Counters::Counters(VariableContext level_, Counters * parent_) |
200 | : counters_holder(new Counter[num_counters] {}), |
201 | parent(parent_), |
202 | level(level_) |
203 | { |
204 | counters = counters_holder.get(); |
205 | } |
206 | |
207 | void Counters::resetCounters() |
208 | { |
209 | if (counters) |
210 | { |
211 | for (Event i = 0; i < num_counters; ++i) |
212 | counters[i].store(0, std::memory_order_relaxed); |
213 | } |
214 | } |
215 | |
216 | void Counters::reset() |
217 | { |
218 | parent = nullptr; |
219 | resetCounters(); |
220 | } |
221 | |
222 | Counters Counters::getPartiallyAtomicSnapshot() const |
223 | { |
224 | Counters res(VariableContext::Snapshot, nullptr); |
225 | for (Event i = 0; i < num_counters; ++i) |
226 | res.counters[i].store(counters[i].load(std::memory_order_relaxed), std::memory_order_relaxed); |
227 | return res; |
228 | } |
229 | |
230 | const char * getName(Event event) |
231 | { |
232 | static const char * strings[] = |
233 | { |
234 | #define M(NAME, DOCUMENTATION) #NAME, |
235 | APPLY_FOR_EVENTS(M) |
236 | #undef M |
237 | }; |
238 | |
239 | return strings[event]; |
240 | } |
241 | |
242 | const char * getDocumentation(Event event) |
243 | { |
244 | static const char * strings[] = |
245 | { |
246 | #define M(NAME, DOCUMENTATION) DOCUMENTATION, |
247 | APPLY_FOR_EVENTS(M) |
248 | #undef M |
249 | }; |
250 | |
251 | return strings[event]; |
252 | } |
253 | |
254 | |
255 | Event end() { return END; } |
256 | |
257 | |
258 | void increment(Event event, Count amount) |
259 | { |
260 | DB::CurrentThread::getProfileEvents().increment(event, amount); |
261 | } |
262 | |
263 | } |
264 | |
265 | #undef APPLY_FOR_EVENTS |
266 | |