Skip to content
This repository was archived by the owner on Jul 31, 2023. It is now read-only.

Commit 63cbc76

Browse files
authored
Record the present time once in Record() and pass that value to ViewInformation::Record(). (#36)
absl::Now() is remarkably expensive, and calling it for each ViewData was responsible for 6-8% of time spent in batched recording--after these changes total time in absl::Now() is negligible. (Log recording would move this off the critical path, but this would still help during log harvesting.)
1 parent c76e8b5 commit 63cbc76

File tree

4 files changed

+23
-14
lines changed

4 files changed

+23
-14
lines changed

opencensus/stats/BUILD

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ cc_library(
105105
deps = [
106106
":core",
107107
"@com_google_absl//absl/strings",
108+
"@com_google_absl//absl/time",
108109
],
109110
)
110111

opencensus/stats/internal/recording.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414

1515
#include "opencensus/stats/recording.h"
1616

17+
#include "absl/time/clock.h"
1718
#include "opencensus/stats/internal/stats_manager.h"
1819
#include "opencensus/stats/measure.h"
1920

@@ -24,7 +25,7 @@ void Record(
2425
std::initializer_list<Measurement> measurements,
2526
std::initializer_list<std::pair<absl::string_view, absl::string_view>>
2627
tags) {
27-
StatsManager::Get()->Record(measurements, tags);
28+
StatsManager::Get()->Record(measurements, tags, absl::Now());
2829
}
2930

3031
} // namespace stats

opencensus/stats/internal/stats_manager.cc

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ int StatsManager::ViewInformation::RemoveConsumer() {
5757

5858
void StatsManager::ViewInformation::Record(
5959
double value,
60-
absl::Span<const std::pair<absl::string_view, absl::string_view>> tags) {
60+
absl::Span<const std::pair<absl::string_view, absl::string_view>> tags,
61+
absl::Time now) {
6162
mu_->AssertHeld();
6263
std::vector<std::string> tag_values(descriptor_.columns().size());
6364
for (int i = 0; i < tag_values.size(); ++i) {
@@ -69,7 +70,7 @@ void StatsManager::ViewInformation::Record(
6970
}
7071
}
7172
}
72-
data_.Add(value, tag_values, absl::Now());
73+
data_.Add(value, tag_values, now);
7374
}
7475

7576
ViewDataImpl StatsManager::ViewInformation::GetData() const {
@@ -86,10 +87,11 @@ ViewDataImpl StatsManager::ViewInformation::GetData() const {
8687

8788
void StatsManager::MeasureInformation::Record(
8889
double value,
89-
absl::Span<const std::pair<absl::string_view, absl::string_view>> tags) {
90+
absl::Span<const std::pair<absl::string_view, absl::string_view>> tags,
91+
absl::Time now) {
9092
mu_->AssertHeld();
9193
for (auto& view : views_) {
92-
view->Record(value, tags);
94+
view->Record(value, tags, now);
9395
}
9496
}
9597

@@ -132,18 +134,18 @@ StatsManager* StatsManager::Get() {
132134

133135
void StatsManager::Record(
134136
std::initializer_list<Measurement> measurements,
135-
std::initializer_list<std::pair<absl::string_view, absl::string_view>>
136-
tags) {
137+
std::initializer_list<std::pair<absl::string_view, absl::string_view>> tags,
138+
absl::Time now) {
137139
absl::MutexLock l(&mu_);
138140
for (const auto& measurement : measurements) {
139141
if (MeasureRegistryImpl::IdValid(measurement.id_)) {
140142
const uint64_t index = MeasureRegistryImpl::IdToIndex(measurement.id_);
141143
switch (MeasureRegistryImpl::IdToType(measurement.id_)) {
142144
case MeasureDescriptor::Type::kDouble:
143-
measures_[index].Record(measurement.value_double_, tags);
145+
measures_[index].Record(measurement.value_double_, tags, now);
144146
break;
145147
case MeasureDescriptor::Type::kInt64:
146-
measures_[index].Record(measurement.value_int_, tags);
148+
measures_[index].Record(measurement.value_int_, tags, now);
147149
break;
148150
}
149151
}

opencensus/stats/internal/stats_manager.h

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <memory>
1919

2020
#include "absl/synchronization/mutex.h"
21+
#include "absl/time/time.h"
2122
#include "absl/types/span.h"
2223
#include "opencensus/common/internal/stats_object.h"
2324
#include "opencensus/stats/distribution.h"
@@ -56,7 +57,8 @@ class StatsManager final {
5657
// Requires holding *mu_.
5758
void Record(
5859
double value,
59-
absl::Span<const std::pair<absl::string_view, absl::string_view>> tags);
60+
absl::Span<const std::pair<absl::string_view, absl::string_view>> tags,
61+
absl::Time now);
6062

6163
// Retrieves a copy of the data.
6264
ViewDataImpl GetData() const LOCKS_EXCLUDED(*mu_);
@@ -85,7 +87,8 @@ class StatsManager final {
8587
void Record(
8688
std::initializer_list<Measurement> measurements,
8789
std::initializer_list<std::pair<absl::string_view, absl::string_view>>
88-
tags) LOCKS_EXCLUDED(mu_);
90+
tags,
91+
absl::Time now) LOCKS_EXCLUDED(mu_);
8992

9093
// Adds a measure--this is necessary for views to be added under that measure.
9194
template <typename MeasureT>
@@ -106,11 +109,13 @@ class StatsManager final {
106109
public:
107110
explicit MeasureInformation(absl::Mutex* mu) : mu_(mu) {}
108111

109-
// records 'value' against all views tracking 'measure'. Presently only
110-
// supports doubles; recorded ints are converted to doubles internally.
112+
// records 'value' against all views tracking 'measure' at time 'now'.
113+
// Presently only supports doubles; recorded ints are converted to doubles
114+
// internally.
111115
void Record(
112116
double value,
113-
absl::Span<const std::pair<absl::string_view, absl::string_view>> tags);
117+
absl::Span<const std::pair<absl::string_view, absl::string_view>> tags,
118+
absl::Time now);
114119

115120
ViewInformation* AddConsumer(const ViewDescriptor& descriptor);
116121
void RemoveView(const ViewInformation* handle);

0 commit comments

Comments
 (0)