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