| OLD | NEW |
| 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. | 1 // Copyright (c) 2010 The Chromium OS Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "counter.h" | 5 #include "counter.h" |
| 6 | 6 |
| 7 #include <sys/file.h> | 7 #include <fcntl.h> |
| 8 | 8 |
| 9 #include <base/eintr_wrapper.h> | 9 #include <base/eintr_wrapper.h> |
| 10 #include <base/logging.h> | 10 #include <base/logging.h> |
| 11 | 11 |
| 12 namespace chromeos_metrics { | 12 namespace chromeos_metrics { |
| 13 | 13 |
| 14 // TaggedCounter::Record implementation. | 14 // TaggedCounter::Record implementation. |
| 15 void TaggedCounter::Record::Init(int tag, int count) { | 15 void TaggedCounter::Record::Init(int32 tag, int32 count) { |
| 16 tag_ = tag; | 16 tag_ = tag; |
| 17 count_ = (count > 0) ? count : 0; | 17 count_ = (count > 0) ? count : 0; |
| 18 } | 18 } |
| 19 | 19 |
| 20 void TaggedCounter::Record::Add(int count) { | 20 void TaggedCounter::Record::Add(int32 count) { |
| 21 if (count <= 0) | 21 if (count <= 0) |
| 22 return; | 22 return; |
| 23 | 23 |
| 24 count_ += count; | 24 // Saturates on positive overflow. |
| 25 | 25 int64 new_count = static_cast<int64>(count_) + static_cast<int64>(count); |
| 26 // Saturates on postive overflow. | 26 if (new_count > kint32max) |
| 27 if (count_ < 0) { | 27 count_ = kint32max; |
| 28 count_ = INT_MAX; | 28 else |
| 29 } | 29 count_ = static_cast<int32>(new_count); |
| 30 } | 30 } |
| 31 | 31 |
| 32 // TaggedCounter implementation. | 32 // TaggedCounter implementation. |
| 33 TaggedCounter::TaggedCounter() | 33 TaggedCounter::TaggedCounter() |
| 34 : filename_(NULL), reporter_(NULL), reporter_handle_(NULL), | 34 : filename_(NULL), |
| 35 reporter_(NULL), |
| 36 reporter_handle_(NULL), |
| 35 record_state_(kRecordInvalid) {} | 37 record_state_(kRecordInvalid) {} |
| 36 | 38 |
| 37 TaggedCounter::~TaggedCounter() {} | 39 TaggedCounter::~TaggedCounter() {} |
| 38 | 40 |
| 39 void TaggedCounter::Init(const char* filename, | 41 void TaggedCounter::Init(const char* filename, |
| 40 Reporter reporter, void* reporter_handle) { | 42 Reporter reporter, void* reporter_handle) { |
| 41 DCHECK(filename); | 43 DCHECK(filename); |
| 42 filename_ = filename; | 44 filename_ = filename; |
| 43 reporter_ = reporter; | 45 reporter_ = reporter; |
| 44 reporter_handle_ = reporter_handle; | 46 reporter_handle_ = reporter_handle; |
| 45 record_state_ = kRecordInvalid; | 47 record_state_ = kRecordInvalid; |
| 46 } | 48 } |
| 47 | 49 |
| 48 void TaggedCounter::Update(int tag, int count) { | 50 void TaggedCounter::Update(int32 tag, int32 count) { |
| 49 UpdateInternal(tag, | 51 UpdateInternal(tag, |
| 50 count, | 52 count, |
| 51 false); // No flush. | 53 false); // No flush. |
| 52 } | 54 } |
| 53 | 55 |
| 54 void TaggedCounter::Flush() { | 56 void TaggedCounter::Flush() { |
| 55 UpdateInternal(0, // tag | 57 UpdateInternal(0, // tag |
| 56 0, // count | 58 0, // count |
| 57 true); // Do flush. | 59 true); // Do flush. |
| 58 } | 60 } |
| 59 | 61 |
| 60 void TaggedCounter::UpdateInternal(int tag, int count, bool flush) { | 62 void TaggedCounter::UpdateInternal(int32 tag, int32 count, bool flush) { |
| 61 // If there's no new data and the last record in the aggregation | 63 // If there's no new data and the last record in the aggregation |
| 62 // file is with the same tag, there's nothing to do. | 64 // file is with the same tag, there's nothing to do. |
| 63 if (!flush && count <= 0 && | 65 if (!flush && count <= 0 && |
| 64 record_state_ == kRecordValid && record_.tag() == tag) | 66 record_state_ == kRecordValid && record_.tag() == tag) |
| 65 return; | 67 return; |
| 66 | 68 |
| 67 DLOG(INFO) << "tag: " << tag << " count: " << count << " flush: " << flush; | 69 DLOG(INFO) << "tag: " << tag << " count: " << count << " flush: " << flush; |
| 68 DCHECK(filename_); | 70 DCHECK(filename_); |
| 69 | 71 |
| 70 // NOTE: The assumption is that this TaggedCounter object is the | 72 // NOTE: The assumption is that this TaggedCounter object is the |
| (...skipping 21 matching lines...) Expand all Loading... |
| 92 if (record_.count() > 0) { | 94 if (record_.count() > 0) { |
| 93 record_state_ = kRecordValid; | 95 record_state_ = kRecordValid; |
| 94 return; | 96 return; |
| 95 } | 97 } |
| 96 // This shouldn't happen normally unless somebody messed with the | 98 // This shouldn't happen normally unless somebody messed with the |
| 97 // persistent storage file. | 99 // persistent storage file. |
| 98 NOTREACHED(); | 100 NOTREACHED(); |
| 99 record_state_ = kRecordNullDirty; | 101 record_state_ = kRecordNullDirty; |
| 100 return; | 102 return; |
| 101 } | 103 } |
| 102 | |
| 103 record_state_ = kRecordNull; | 104 record_state_ = kRecordNull; |
| 104 } | 105 } |
| 105 | 106 |
| 106 void TaggedCounter::ReportRecord(int tag, bool flush) { | 107 void TaggedCounter::ReportRecord(int32 tag, bool flush) { |
| 107 // If no valid record, there's nothing to report. | 108 // If no valid record, there's nothing to report. |
| 108 if (record_state_ != kRecordValid) { | 109 if (record_state_ != kRecordValid) { |
| 109 DCHECK(record_state_ == kRecordNull); | 110 DCHECK(record_state_ == kRecordNull); |
| 110 return; | 111 return; |
| 111 } | 112 } |
| 112 | 113 |
| 113 // If the current record has the same tag as the new tag, it's not | 114 // If the current record has the same tag as the new tag, it's not |
| 114 // ready to be reported yet. | 115 // ready to be reported yet. |
| 115 if (!flush && record_.tag() == tag) | 116 if (!flush && record_.tag() == tag) |
| 116 return; | 117 return; |
| 117 | 118 |
| 118 if (reporter_) { | 119 if (reporter_) { |
| 119 reporter_(reporter_handle_, record_.tag(), record_.count()); | 120 reporter_(reporter_handle_, record_.tag(), record_.count()); |
| 120 } | 121 } |
| 121 record_state_ = kRecordNullDirty; | 122 record_state_ = kRecordNullDirty; |
| 122 } | 123 } |
| 123 | 124 |
| 124 void TaggedCounter::UpdateRecord(int tag, int count) { | 125 void TaggedCounter::UpdateRecord(int32 tag, int32 count) { |
| 125 if (count <= 0) | 126 if (count <= 0) |
| 126 return; | 127 return; |
| 127 | 128 |
| 128 switch (record_state_) { | 129 switch (record_state_) { |
| 129 case kRecordNull: | 130 case kRecordNull: |
| 130 case kRecordNullDirty: | 131 case kRecordNullDirty: |
| 131 // Current record is null, starting a new record. | 132 // Current record is null, starting a new record. |
| 132 record_.Init(tag, count); | 133 record_.Init(tag, count); |
| 133 record_state_ = kRecordValidDirty; | 134 record_state_ = kRecordValidDirty; |
| 134 break; | 135 break; |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 167 case kRecordValid: | 168 case kRecordValid: |
| 168 // Nothing to do. | 169 // Nothing to do. |
| 169 break; | 170 break; |
| 170 | 171 |
| 171 default: | 172 default: |
| 172 NOTREACHED(); | 173 NOTREACHED(); |
| 173 } | 174 } |
| 174 } | 175 } |
| 175 | 176 |
| 176 } // namespace chromeos_metrics | 177 } // namespace chromeos_metrics |
| OLD | NEW |