| OLD | NEW |
| (Empty) |
| 1 // Copyright 2006-2009 Google Inc. | |
| 2 // | |
| 3 // Licensed under the Apache License, Version 2.0 (the "License"); | |
| 4 // you may not use this file except in compliance with the License. | |
| 5 // You may obtain a copy of the License at | |
| 6 // | |
| 7 // http://www.apache.org/licenses/LICENSE-2.0 | |
| 8 // | |
| 9 // Unless required by applicable law or agreed to in writing, software | |
| 10 // distributed under the License is distributed on an "AS IS" BASIS, | |
| 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | |
| 12 // See the License for the specific language governing permissions and | |
| 13 // limitations under the License. | |
| 14 // ======================================================================== | |
| 15 // | |
| 16 // Implementation of Win32 metrics aggregator. | |
| 17 #include "aggregator-win32.h" | |
| 18 #include "const-win32.h" | |
| 19 #include "util-win32.h" | |
| 20 | |
| 21 namespace stats_report { | |
| 22 | |
| 23 MetricsAggregatorWin32::MetricsAggregatorWin32(MetricCollection &coll, | |
| 24 const wchar_t *key_name) | |
| 25 : MetricsAggregator(coll), | |
| 26 is_machine_(false) { | |
| 27 DCHECK(NULL != key_name); | |
| 28 | |
| 29 key_name_.Format(kStatsKeyFormatString, key_name); | |
| 30 } | |
| 31 | |
| 32 MetricsAggregatorWin32::MetricsAggregatorWin32(MetricCollection &coll, | |
| 33 const wchar_t *key_name, | |
| 34 bool is_machine) | |
| 35 : MetricsAggregator(coll), | |
| 36 is_machine_(is_machine) { | |
| 37 DCHECK(NULL != key_name); | |
| 38 | |
| 39 key_name_.Format(kStatsKeyFormatString, key_name); | |
| 40 } | |
| 41 | |
| 42 MetricsAggregatorWin32::~MetricsAggregatorWin32() { | |
| 43 } | |
| 44 | |
| 45 bool MetricsAggregatorWin32::StartAggregation() { | |
| 46 DCHECK(NULL == key_.m_hKey); | |
| 47 | |
| 48 HKEY parent_key = is_machine_ ? HKEY_LOCAL_MACHINE : HKEY_CURRENT_USER; | |
| 49 LONG err = key_.Create(parent_key, key_name_); | |
| 50 if (err != ERROR_SUCCESS) | |
| 51 return false; | |
| 52 | |
| 53 return true; | |
| 54 } | |
| 55 | |
| 56 void MetricsAggregatorWin32::EndAggregation() { | |
| 57 count_key_.Close(); | |
| 58 timing_key_.Close(); | |
| 59 integer_key_.Close(); | |
| 60 bool_key_.Close(); | |
| 61 | |
| 62 key_.Close(); | |
| 63 } | |
| 64 | |
| 65 bool MetricsAggregatorWin32::EnsureKey(const wchar_t *name, CRegKey *key) { | |
| 66 if (NULL != key->m_hKey) | |
| 67 return true; | |
| 68 | |
| 69 LONG err = key->Create(key_, name); | |
| 70 if (ERROR_SUCCESS != err) { | |
| 71 DCHECK(NULL == key->m_hKey); | |
| 72 // TODO(omaha): log? | |
| 73 return false; | |
| 74 } | |
| 75 | |
| 76 return true; | |
| 77 } | |
| 78 | |
| 79 void MetricsAggregatorWin32::Aggregate(CountMetric &metric) { | |
| 80 // do as little as possible if no value | |
| 81 int64 value = metric.Reset(); | |
| 82 if (0 == value) | |
| 83 return; | |
| 84 | |
| 85 if (!EnsureKey(kCountsKeyName, &count_key_)) | |
| 86 return; | |
| 87 | |
| 88 CString name(metric.name()); | |
| 89 int64 reg_value = 0; | |
| 90 if (!GetData(count_key_, name, ®_value)) { | |
| 91 // TODO(omaha): clean up?? | |
| 92 } | |
| 93 reg_value += value; | |
| 94 | |
| 95 LONG err = count_key_.SetBinaryValue(name, ®_value, sizeof(reg_value)); | |
| 96 } | |
| 97 | |
| 98 void MetricsAggregatorWin32::Aggregate(TimingMetric &metric) { | |
| 99 // do as little as possible if no value | |
| 100 TimingMetric::TimingData value = metric.Reset(); | |
| 101 if (0 == value.count) | |
| 102 return; | |
| 103 | |
| 104 if (!EnsureKey(kTimingsKeyName, &timing_key_)) | |
| 105 return; | |
| 106 | |
| 107 CString name(metric.name()); | |
| 108 TimingMetric::TimingData reg_value; | |
| 109 if (!GetData(timing_key_, name, ®_value)) { | |
| 110 memcpy(®_value, &value, sizeof(value)); | |
| 111 } else { | |
| 112 reg_value.count += value.count; | |
| 113 reg_value.sum += value.sum; | |
| 114 reg_value.minimum = std::min(reg_value.minimum, value.minimum); | |
| 115 reg_value.maximum = std::max(reg_value.maximum, value.maximum); | |
| 116 } | |
| 117 | |
| 118 LONG err = timing_key_.SetBinaryValue(name, ®_value, sizeof(reg_value)); | |
| 119 } | |
| 120 | |
| 121 void MetricsAggregatorWin32::Aggregate(IntegerMetric &metric) { | |
| 122 // do as little as possible if no value | |
| 123 int64 value = metric.value(); | |
| 124 if (0 == value) | |
| 125 return; | |
| 126 | |
| 127 if (!EnsureKey(kIntegersKeyName, &integer_key_)) | |
| 128 return; | |
| 129 | |
| 130 LONG err = integer_key_.SetBinaryValue(CString(metric.name()), | |
| 131 &value, sizeof(value)); | |
| 132 } | |
| 133 | |
| 134 void MetricsAggregatorWin32::Aggregate(BoolMetric &metric) { | |
| 135 // do as little as possible if no value | |
| 136 int32 value = metric.Reset(); | |
| 137 if (BoolMetric::kBoolUnset == value) | |
| 138 return; | |
| 139 | |
| 140 if (!EnsureKey(kBooleansKeyName, &bool_key_)) | |
| 141 return; | |
| 142 | |
| 143 LONG err = bool_key_.SetBinaryValue(CString(metric.name()), | |
| 144 &value, sizeof(value)); | |
| 145 } | |
| 146 | |
| 147 } // namespace stats_report | |
| OLD | NEW |