Debian Codegraph @

src/core/lib/debug/event_log.h

raw
1 // Copyright 2022 gRPC authors.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 #ifndef GRPC_CORE_LIB_DEBUG_EVENT_LOG_H
16 #define GRPC_CORE_LIB_DEBUG_EVENT_LOG_H
17
18 #include <grpc/support/port_platform.h>
19
20 #include <stdint.h>
21
22 #include <atomic>
23 #include <string>
24 #include <vector>
25
26 #include "absl/base/thread_annotations.h"
27 #include "absl/strings/string_view.h"
28 #include "absl/types/span.h"
29
30 #include "src/core/lib/gpr/time_precise.h"
31 #include "src/core/lib/gprpp/per_cpu.h"
32 #include "src/core/lib/gprpp/sync.h"
33
34 namespace grpc_core {
35
36 // Debug utility to collect a burst of events and then later log them as a
37 // detailed sequence.
38 // Collects (timestamp, counter-name, delta) and gives back a csv with
39 // timestamps and accumulated values for each counter in separate columns.
40 class EventLog {
41 public:
42 EventLog() = default;
43 ~EventLog();
44
45 EventLog(const EventLog&) = delete;
46 EventLog& operator=(const EventLog&) = delete;
47
48 void BeginCollection();
49 std::string EndCollectionAndReportCsv(
50 absl::Span<const absl::string_view> columns);
51
52 static void Append(absl::string_view event, int64_t delta) {
53 EventLog* log = g_instance_.load(std::memory_order_acquire);
54 if (log == nullptr) return;
55 log->AppendInternal(event, delta);
56 }
57
58 private:
59 struct Entry {
60 gpr_cycle_counter when;
61 absl::string_view event;
62 int64_t delta;
63 };
64
65 struct Fragment {
66 Mutex mu;
67 std::vector<Entry> entries ABSL_GUARDED_BY(mu);
68 };
69
70 void AppendInternal(absl::string_view event, int64_t delta);
71 std::vector<Entry> EndCollection(
72 absl::Span<const absl::string_view> wanted_events);
73
74 PerCpu<Fragment> fragments_;
75 gpr_cycle_counter collection_begin_;
76 static std::atomic<EventLog*> g_instance_;
77 };
78
79 } // namespace grpc_core
80
81 #endif // GRPC_CORE_LIB_DEBUG_EVENT_LOG_H