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_SRC_CORE_UTIL_EVENT_LOG_H 16 #define GRPC_SRC_CORE_UTIL_EVENT_LOG_H 17 18 #include <grpc/support/port_platform.h> 19 #include <stdint.h> 20 21 #include <atomic> 22 #include <string> 23 #include <vector> 24 25 #include "absl/base/thread_annotations.h" 26 #include "absl/strings/string_view.h" 27 #include "absl/types/span.h" 28 #include "src/core/util/per_cpu.h" 29 #include "src/core/util/sync.h" 30 #include "src/core/util/time_precise.h" 31 32 namespace grpc_core { 33 34 // Debug utility to collect a burst of events and then later log them as a 35 // detailed sequence. 36 // Collects (timestamp, counter-name, delta) and gives back a csv with 37 // timestamps and accumulated values for each counter in separate columns. 38 class EventLog { 39 public: 40 EventLog() = default; 41 ~EventLog(); 42 43 EventLog(const EventLog&) = delete; 44 EventLog& operator=(const EventLog&) = delete; 45 46 void BeginCollection(); 47 std::string EndCollectionAndReportCsv( 48 absl::Span<const absl::string_view> columns); 49 Append(absl::string_view event,int64_t delta)50 static void Append(absl::string_view event, int64_t delta) { 51 EventLog* log = g_instance_.load(std::memory_order_acquire); 52 if (log == nullptr) return; 53 log->AppendInternal(event, delta); 54 } 55 56 private: 57 struct Entry { 58 gpr_cycle_counter when; 59 absl::string_view event; 60 int64_t delta; 61 }; 62 63 struct Fragment { 64 Mutex mu; 65 std::vector<Entry> entries ABSL_GUARDED_BY(mu); 66 }; 67 68 void AppendInternal(absl::string_view event, int64_t delta); 69 std::vector<Entry> EndCollection( 70 absl::Span<const absl::string_view> wanted_events); 71 72 PerCpu<Fragment> fragments_{PerCpuOptions().SetCpusPerShard(2)}; 73 gpr_cycle_counter collection_begin_; 74 static std::atomic<EventLog*> g_instance_; 75 }; 76 77 } // namespace grpc_core 78 79 #endif // GRPC_SRC_CORE_UTIL_EVENT_LOG_H 80