aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/libs/grpc/src/core/lib/debug/event_log.cc
blob: 323ad560ca39966b1b71db785c64db42264388a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
// Copyright 2022 gRPC authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <grpc/support/port_platform.h>

#include "src/core/lib/debug/event_log.h"

#include <algorithm>
#include <atomic>

#include "y_absl/strings/str_cat.h"
#include "y_absl/strings/str_join.h"

#include <grpc/support/log.h>

namespace grpc_core {

std::atomic<EventLog*> EventLog::g_instance_{nullptr};

EventLog::~EventLog() {
  GPR_ASSERT(g_instance_.load(std::memory_order_acquire) != this);
}

void EventLog::BeginCollection() {
  for (auto& fragment : fragments_) {
    MutexLock lock(&fragment.mu);
    fragment.entries.clear();
  }
  collection_begin_ = gpr_get_cycle_counter();
  g_instance_.store(this, std::memory_order_release);
  Append("logging", 1);
}

std::vector<EventLog::Entry> EventLog::EndCollection(
    y_absl::Span<const y_absl::string_view> wanted_events) {
  Append("logging", -1);
  g_instance_.store(nullptr, std::memory_order_release);
  std::vector<Entry> result;
  for (auto& fragment : fragments_) {
    MutexLock lock(&fragment.mu);
    for (const auto& entry : fragment.entries) {
      if (std::find(wanted_events.begin(), wanted_events.end(), entry.event) !=
          wanted_events.end()) {
        result.push_back(entry);
      }
    }
    fragment.entries.clear();
  }
  std::stable_sort(
      result.begin(), result.end(),
      [](const Entry& a, const Entry& b) { return a.when < b.when; });
  return result;
}

void EventLog::AppendInternal(y_absl::string_view event, int64_t delta) {
  auto& fragment = fragments_.this_cpu();
  MutexLock lock(&fragment.mu);
  fragment.entries.push_back({gpr_get_cycle_counter(), event, delta});
}

TString EventLog::EndCollectionAndReportCsv(
    y_absl::Span<const y_absl::string_view> columns) {
  auto events = EndCollection(columns);
  std::vector<int64_t> values(columns.size(), 0);
  TString result =
      y_absl::StrCat("timestamp,", y_absl::StrJoin(columns, ","), "\n");
  for (const auto& entry : events) {
    auto idx = std::find(columns.begin(), columns.end(), entry.event) -
               columns.begin();
    values[idx] += entry.delta;
    y_absl::StrAppend(&result, entry.when - collection_begin_, ",",
                    y_absl::StrJoin(values, ","), "\n");
  }
  return result;
}

}  // namespace grpc_core