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 #include <grpc/support/port_platform.h>
16
17 #include "src/core/lib/debug/event_log.h"
18
19 #include <algorithm>
20 #include <atomic>
21
22 #include "absl/strings/str_cat.h"
23 #include "absl/strings/str_join.h"
24
25 #include <grpc/support/log.h>
26
27 namespace grpc_core {
28
29 std::atomic<EventLog*> EventLog::g_instance_{nullptr};
30
~EventLog()31 EventLog::~EventLog() {
32 GPR_ASSERT(g_instance_.load(std::memory_order_acquire) != this);
33 }
34
BeginCollection()35 void EventLog::BeginCollection() {
36 for (auto& fragment : fragments_) {
37 MutexLock lock(&fragment.mu);
38 fragment.entries.clear();
39 }
40 collection_begin_ = gpr_get_cycle_counter();
41 g_instance_.store(this, std::memory_order_release);
42 Append("logging", 1);
43 }
44
EndCollection(absl::Span<const absl::string_view> wanted_events)45 std::vector<EventLog::Entry> EventLog::EndCollection(
46 absl::Span<const absl::string_view> wanted_events) {
47 Append("logging", -1);
48 g_instance_.store(nullptr, std::memory_order_release);
49 std::vector<Entry> result;
50 for (auto& fragment : fragments_) {
51 MutexLock lock(&fragment.mu);
52 for (const auto& entry : fragment.entries) {
53 if (std::find(wanted_events.begin(), wanted_events.end(), entry.event) !=
54 wanted_events.end()) {
55 result.push_back(entry);
56 }
57 }
58 fragment.entries.clear();
59 }
60 std::stable_sort(
61 result.begin(), result.end(),
62 [](const Entry& a, const Entry& b) { return a.when < b.when; });
63 return result;
64 }
65
AppendInternal(absl::string_view event,int64_t delta)66 void EventLog::AppendInternal(absl::string_view event, int64_t delta) {
67 auto& fragment = fragments_.this_cpu();
68 MutexLock lock(&fragment.mu);
69 fragment.entries.push_back({gpr_get_cycle_counter(), event, delta});
70 }
71
EndCollectionAndReportCsv(absl::Span<const absl::string_view> columns)72 std::string EventLog::EndCollectionAndReportCsv(
73 absl::Span<const absl::string_view> columns) {
74 auto events = EndCollection(columns);
75 std::vector<int64_t> values(columns.size(), 0);
76 std::string result =
77 absl::StrCat("timestamp,", absl::StrJoin(columns, ","), "\n");
78 for (const auto& entry : events) {
79 auto idx = std::find(columns.begin(), columns.end(), entry.event) -
80 columns.begin();
81 values[idx] += entry.delta;
82 absl::StrAppend(&result, entry.when - collection_begin_, ",",
83 absl::StrJoin(values, ","), "\n");
84 }
85 return result;
86 }
87
88 } // namespace grpc_core
89