Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(15)

Side by Side Diff: webrtc/system_wrappers/source/metrics_default.cc

Issue 2268323002: Use swap instead of copy in RtcHistogram::GetAndReset. (Closed) Base URL: https://chromium.googlesource.com/external/webrtc.git@master
Patch Set: remove Reset change Created 4 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « no previous file | webrtc/system_wrappers/source/metrics_default_unittest.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved. 1 // Copyright (c) 2014 The WebRTC project authors. All Rights Reserved.
2 // 2 //
3 // Use of this source code is governed by a BSD-style license 3 // Use of this source code is governed by a BSD-style license
4 // that can be found in the LICENSE file in the root of the source 4 // that can be found in the LICENSE file in the root of the source
5 // tree. An additional intellectual property rights grant can be found 5 // tree. An additional intellectual property rights grant can be found
6 // in the file PATENTS. All contributing project authors may 6 // in the file PATENTS. All contributing project authors may
7 // be found in the AUTHORS file in the root of the source tree. 7 // be found in the AUTHORS file in the root of the source tree.
8 // 8 //
9 9
10 #include "webrtc/system_wrappers/include/metrics_default.h" 10 #include "webrtc/system_wrappers/include/metrics_default.h"
11 11
12 #include <algorithm>
13
12 #include "webrtc/base/criticalsection.h" 14 #include "webrtc/base/criticalsection.h"
13 #include "webrtc/base/thread_annotations.h" 15 #include "webrtc/base/thread_annotations.h"
14 #include "webrtc/system_wrappers/include/metrics.h" 16 #include "webrtc/system_wrappers/include/metrics.h"
15 17
16 // Default implementation of histogram methods for WebRTC clients that do not 18 // Default implementation of histogram methods for WebRTC clients that do not
17 // want to provide their own implementation. 19 // want to provide their own implementation.
18 20
19 namespace webrtc { 21 namespace webrtc {
20 namespace metrics { 22 namespace metrics {
21 class Histogram; 23 class Histogram;
22 24
23 namespace { 25 namespace {
24 // Limit for the maximum number of sample values that can be stored. 26 // Limit for the maximum number of sample values that can be stored.
25 // TODO(asapersson): Consider using bucket count (and set up 27 // TODO(asapersson): Consider using bucket count (and set up
26 // linearly/exponentially spaced buckets) if samples are logged more frequently. 28 // linearly/exponentially spaced buckets) if samples are logged more frequently.
27 const int kMaxSampleMapSize = 300; 29 const int kMaxSampleMapSize = 300;
28 30
29 class RtcHistogram { 31 class RtcHistogram {
30 public: 32 public:
31 RtcHistogram(const std::string& name, int min, int max, int bucket_count) 33 RtcHistogram(const std::string& name, int min, int max, int bucket_count)
32 : min_(min), max_(max), info_(name, min, max, bucket_count) { 34 : min_(min), max_(max), info_(name, min, max, bucket_count) {
33 RTC_DCHECK_GT(bucket_count, 0); 35 RTC_DCHECK_GT(bucket_count, 0);
34 } 36 }
35 37
36 void Add(int sample) { 38 void Add(int sample) {
37 if (sample < min_) 39 sample = std::min(sample, max_);
38 sample = min_ - 1; // Underflow bucket. 40 sample = std::max(sample, min_ - 1); // Underflow bucket.
39 if (sample > max_)
40 sample = max_;
41 41
42 rtc::CritScope cs(&crit_); 42 rtc::CritScope cs(&crit_);
43 if (info_.samples.size() == kMaxSampleMapSize && 43 if (info_.samples.size() == kMaxSampleMapSize &&
44 info_.samples.find(sample) == info_.samples.end()) { 44 info_.samples.find(sample) == info_.samples.end()) {
45 return; 45 return;
46 } 46 }
47 ++info_.samples[sample]; 47 ++info_.samples[sample];
48 } 48 }
49 49
50 // Returns a copy (or nullptr if there are no samples) and clears samples. 50 // Returns a copy (or nullptr if there are no samples) and clears samples.
51 std::unique_ptr<SampleInfo> GetAndReset() { 51 std::unique_ptr<SampleInfo> GetAndReset() {
52 rtc::CritScope cs(&crit_); 52 rtc::CritScope cs(&crit_);
53 if (info_.samples.empty()) 53 if (info_.samples.empty())
54 return nullptr; 54 return nullptr;
55 55
56 SampleInfo* copy = 56 SampleInfo* copy =
57 new SampleInfo(info_.name, info_.min, info_.max, info_.bucket_count); 57 new SampleInfo(info_.name, info_.min, info_.max, info_.bucket_count);
58 copy->samples = info_.samples; 58
59 info_.samples.clear(); 59 std::swap(info_.samples, copy->samples);
stefan-webrtc 2016/11/28 15:15:55 Should we clear info_.samples after this?
åsapersson 2016/11/29 14:22:21 copy->samples has an empty map before the swap.
stefan-webrtc 2016/11/29 16:15:53 Acknowledged.
60
60 return std::unique_ptr<SampleInfo>(copy); 61 return std::unique_ptr<SampleInfo>(copy);
61 } 62 }
62 63
63 const std::string& name() const { return info_.name; } 64 const std::string& name() const { return info_.name; }
64 65
65 // Functions only for testing. 66 // Functions only for testing.
66 void Reset() { 67 void Reset() {
67 rtc::CritScope cs(&crit_); 68 rtc::CritScope cs(&crit_);
68 info_.samples.clear(); 69 info_.samples.clear();
69 } 70 }
(...skipping 214 matching lines...) Expand 10 before | Expand all | Expand 10 after
284 return map ? map->NumSamples(name) : 0; 285 return map ? map->NumSamples(name) : 0;
285 } 286 }
286 287
287 int MinSample(const std::string& name) { 288 int MinSample(const std::string& name) {
288 RtcHistogramMap* map = GetMap(); 289 RtcHistogramMap* map = GetMap();
289 return map ? map->MinSample(name) : -1; 290 return map ? map->MinSample(name) : -1;
290 } 291 }
291 292
292 } // namespace metrics 293 } // namespace metrics
293 } // namespace webrtc 294 } // namespace webrtc
OLDNEW
« no previous file with comments | « no previous file | webrtc/system_wrappers/source/metrics_default_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698