/Users/deen/code/yugabyte-db/src/yb/util/test_graph.cc
Line | Count | Source (jump to first uncovered line) |
1 | | // Licensed to the Apache Software Foundation (ASF) under one |
2 | | // or more contributor license agreements. See the NOTICE file |
3 | | // distributed with this work for additional information |
4 | | // regarding copyright ownership. The ASF licenses this file |
5 | | // to you under the Apache License, Version 2.0 (the |
6 | | // "License"); you may not use this file except in compliance |
7 | | // with the License. You may obtain a copy of the License at |
8 | | // |
9 | | // http://www.apache.org/licenses/LICENSE-2.0 |
10 | | // |
11 | | // Unless required by applicable law or agreed to in writing, |
12 | | // software distributed under the License is distributed on an |
13 | | // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
14 | | // KIND, either express or implied. See the License for the |
15 | | // specific language governing permissions and limitations |
16 | | // under the License. |
17 | | // |
18 | | // The following only applies to changes made to this file as part of YugaByte development. |
19 | | // |
20 | | // Portions Copyright (c) YugaByte, Inc. |
21 | | // |
22 | | // Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except |
23 | | // in compliance with the License. You may obtain a copy of the License at |
24 | | // |
25 | | // http://www.apache.org/licenses/LICENSE-2.0 |
26 | | // |
27 | | // Unless required by applicable law or agreed to in writing, software distributed under the License |
28 | | // is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express |
29 | | // or implied. See the License for the specific language governing permissions and limitations |
30 | | // under the License. |
31 | | // |
32 | | |
33 | | #include "yb/util/test_graph.h" |
34 | | |
35 | | #include <mutex> |
36 | | |
37 | | #include <glog/logging.h> |
38 | | |
39 | | #include "yb/gutil/ref_counted.h" |
40 | | #include "yb/gutil/stringprintf.h" |
41 | | #include "yb/gutil/walltime.h" |
42 | | #include "yb/util/locks.h" |
43 | | #include "yb/util/status.h" |
44 | | #include "yb/util/status_log.h" |
45 | | #include "yb/util/thread.h" |
46 | | |
47 | | using std::shared_ptr; |
48 | | using std::string; |
49 | | |
50 | | namespace yb { |
51 | | |
52 | 345 | void TimeSeries::AddValue(double val) { |
53 | 345 | std::lock_guard<simple_spinlock> l(lock_); |
54 | 345 | val_ += val; |
55 | 345 | } |
56 | | |
57 | 0 | void TimeSeries::SetValue(double val) { |
58 | 0 | std::lock_guard<simple_spinlock> l(lock_); |
59 | 0 | val_ = val; |
60 | 0 | } |
61 | | |
62 | 0 | double TimeSeries::value() const { |
63 | 0 | std::lock_guard<simple_spinlock> l(lock_); |
64 | 0 | return val_; |
65 | 0 | } |
66 | | |
67 | | TimeSeriesCollector::TimeSeriesCollector(std::string scope) |
68 | 6 | : scope_(std::move(scope)), exit_latch_(0), started_(false) {} |
69 | | |
70 | 6 | TimeSeriesCollector::~TimeSeriesCollector() { |
71 | 6 | if (started_) { |
72 | 6 | StopDumperThread(); |
73 | 6 | } |
74 | 6 | } |
75 | | |
76 | 12 | shared_ptr<TimeSeries> TimeSeriesCollector::GetTimeSeries(const string &key) { |
77 | 12 | MutexLock l(series_lock_); |
78 | 12 | SeriesMap::const_iterator it = series_map_.find(key); |
79 | 12 | if (it == series_map_.end()) { |
80 | 12 | shared_ptr<TimeSeries> ts(new TimeSeries()); |
81 | 12 | series_map_[key] = ts; |
82 | 12 | return ts; |
83 | 12 | } else { |
84 | 0 | return (*it).second; |
85 | 0 | } |
86 | 12 | } |
87 | | |
88 | 6 | void TimeSeriesCollector::StartDumperThread() { |
89 | 6 | LOG(INFO) << "Starting metrics dumper"; |
90 | 6 | CHECK(!started_); |
91 | 6 | exit_latch_.Reset(1); |
92 | 6 | started_ = true; |
93 | 6 | CHECK_OK(yb::Thread::Create("time series", "dumper", |
94 | 6 | &TimeSeriesCollector::DumperThread, this, &dumper_thread_)); |
95 | 6 | } |
96 | | |
97 | 6 | void TimeSeriesCollector::StopDumperThread() { |
98 | 6 | CHECK(started_); |
99 | 6 | exit_latch_.CountDown(); |
100 | 6 | CHECK_OK(ThreadJoiner(dumper_thread_.get()).Join()); |
101 | 6 | started_ = false; |
102 | 6 | } |
103 | | |
104 | 6 | void TimeSeriesCollector::DumperThread() { |
105 | 6 | CHECK(started_); |
106 | 6 | WallTime start_time = WallTime_Now(); |
107 | | |
108 | 6 | faststring metrics_str; |
109 | 6 | while (true) { |
110 | 6 | metrics_str.clear(); |
111 | 6 | metrics_str.append("metrics: "); |
112 | 6 | BuildMetricsString(WallTime_Now() - start_time, &metrics_str); |
113 | 6 | LOG(INFO) << metrics_str.ToString(); |
114 | | |
115 | | // Sleep until next dump time, or return if we should exit |
116 | 6 | if (exit_latch_.WaitFor(MonoDelta::FromMilliseconds(250))) { |
117 | 6 | return; |
118 | 6 | } |
119 | 6 | } |
120 | 6 | } |
121 | | |
122 | | void TimeSeriesCollector::BuildMetricsString( |
123 | 6 | WallTime time_since_start, faststring *dst_buf) const { |
124 | 6 | MutexLock l(series_lock_); |
125 | | |
126 | 6 | dst_buf->append(StringPrintf("{ \"scope\": \"%s\", \"time\": %.3f", |
127 | 6 | scope_.c_str(), time_since_start)); |
128 | | |
129 | 6 | for (SeriesMap::const_reference entry : series_map_) { |
130 | 0 | dst_buf->append(StringPrintf(", \"%s\": %.3f", |
131 | 0 | entry.first.c_str(), entry.second->value())); |
132 | 0 | } |
133 | 6 | dst_buf->append("}"); |
134 | 6 | } |
135 | | |
136 | | |
137 | | } // namespace yb |