Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2014 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2014 The Chromium 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 "components/browser_watcher/watcher_metrics_provider_win.h" | 5 #include "components/browser_watcher/watcher_metrics_provider_win.h" |
| 6 | 6 |
| 7 #include <limits> | |
| 7 #include <vector> | 8 #include <vector> |
| 8 | 9 |
| 9 #include "base/metrics/sparse_histogram.h" | 10 #include "base/metrics/sparse_histogram.h" |
| 10 #include "base/process/process_handle.h" | 11 #include "base/process/process_handle.h" |
| 11 #include "base/strings/string_number_conversions.h" | 12 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
| 14 #include "base/strings/utf_string_conversions.h" | |
| 13 #include "base/win/registry.h" | 15 #include "base/win/registry.h" |
| 14 | 16 |
| 15 namespace browser_watcher { | 17 namespace browser_watcher { |
| 16 | 18 |
| 17 namespace { | 19 namespace { |
| 18 | 20 |
| 19 void CompileAsserts() { | 21 void CompileAsserts() { |
| 20 // Process ID APIs on Windows talk in DWORDs, whereas for string formatting | 22 // Process ID APIs on Windows talk in DWORDs, whereas for string formatting |
| 21 // and parsing, this code uses int. In practice there are no process IDs with | 23 // and parsing, this code uses int. In practice there are no process IDs with |
| 22 // the high bit set on Windows, so there's no danger of overflow if this is | 24 // the high bit set on Windows, so there's no danger of overflow if this is |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 53 if (base::OpenProcessHandle(static_cast<base::ProcessId>(pid), &process)) { | 55 if (base::OpenProcessHandle(static_cast<base::ProcessId>(pid), &process)) { |
| 54 base::CloseProcessHandle(process); | 56 base::CloseProcessHandle(process); |
| 55 | 57 |
| 56 // The fact that it was possible to open the process says it's live. | 58 // The fact that it was possible to open the process says it's live. |
| 57 return false; | 59 return false; |
| 58 } | 60 } |
| 59 | 61 |
| 60 return true; | 62 return true; |
| 61 } | 63 } |
| 62 | 64 |
| 65 void RecordExitCodes(const base::string16& registry_path) { | |
| 66 base::win::RegKey regkey(HKEY_CURRENT_USER, | |
| 67 registry_path.c_str(), | |
| 68 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
| 69 if (!regkey.Valid()) | |
| 70 return; | |
| 71 | |
| 72 size_t num = regkey.GetValueCount(); | |
| 73 if (num == 0) | |
| 74 return; | |
| 75 std::vector<base::string16> to_delete; | |
| 76 | |
| 77 // Record the exit codes in a sparse stability histogram, as the range of | |
| 78 // values used to report failures is large. | |
| 79 base::HistogramBase* exit_code_histogram = | |
| 80 base::SparseHistogram::FactoryGet( | |
| 81 WatcherMetricsProviderWin::kBrowserExitCodeHistogramName, | |
| 82 base::HistogramBase::kUmaStabilityHistogramFlag); | |
| 83 | |
| 84 for (size_t i = 0; i < num; ++i) { | |
| 85 base::string16 name; | |
| 86 if (regkey.GetValueNameAt(static_cast<int>(i), &name) == ERROR_SUCCESS) { | |
| 87 DWORD exit_code = 0; | |
| 88 if (regkey.ReadValueDW(name.c_str(), &exit_code) == ERROR_SUCCESS) { | |
| 89 // Do not report exit codes for processes that are still live, | |
| 90 // notably for our own process. | |
| 91 if (exit_code != STILL_ACTIVE || IsDeadProcess(name)) { | |
| 92 to_delete.push_back(name); | |
| 93 exit_code_histogram->Add(exit_code); | |
| 94 } | |
| 95 } | |
| 96 } | |
| 97 } | |
| 98 | |
| 99 // Delete the values reported above. | |
| 100 for (size_t i = 0; i < to_delete.size(); ++i) | |
| 101 regkey.DeleteValue(to_delete[i].c_str()); | |
| 102 } | |
| 103 | |
| 104 void RecordSingleExitFunnel(base::win::RegKey* parent_key, | |
| 105 const base::char16* name) { | |
| 106 DCHECK(parent_key); | |
| 107 DCHECK(name); | |
| 108 | |
| 109 base::win::RegKey regkey(parent_key->Handle(), name, KEY_READ | KEY_WRITE); | |
| 110 if (!regkey.Valid()) | |
| 111 return; | |
| 112 | |
| 113 // Exit early if no work to do. | |
| 114 size_t num = regkey.GetValueCount(); | |
| 115 if (num == 0) | |
| 116 return; | |
| 117 | |
| 118 // Enumerate the recorded events for this process for processing. | |
| 119 std::vector<std::pair<base::string16, int64>> events; | |
| 120 int64 min_time = std::numeric_limits<int64>::max(); | |
|
erikwright (departed)
2014/12/11 15:00:46
Everything in this method needs to be extracted ou
Sigurður Ásgeirsson
2014/12/12 16:21:40
Maybe so, though the tests also want to verify tha
| |
| 121 for (size_t i = 0; i < num; ++i) { | |
| 122 base::string16 event_name; | |
| 123 LONG res = regkey.GetValueNameAt(static_cast<int>(i), &event_name); | |
| 124 if (res == ERROR_SUCCESS) { | |
| 125 int64 event_time = 0; | |
| 126 res = regkey.ReadInt64(event_name.c_str(), &event_time); | |
| 127 if (res == ERROR_SUCCESS) { | |
| 128 events.push_back(std::make_pair(event_name, event_time)); | |
| 129 min_time = std::min(min_time, event_time); | |
| 130 } | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 // Attempt to delete the values before reporting anything. | |
| 135 // Exit if this fails to make sure there is no double-reporting on e.g. | |
| 136 // permission problems or other corruption. | |
| 137 for (size_t i = 0; i < events.size(); ++i) { | |
| 138 const base::string16& event_name = events[i].first; | |
| 139 LONG res = regkey.DeleteValue(event_name.c_str()); | |
| 140 if (res != ERROR_SUCCESS) { | |
| 141 LOG(ERROR) << "Failed to delete value " << event_name; | |
| 142 return; | |
| 143 } | |
| 144 } | |
| 145 | |
| 146 // Record the exit funnel event times in a sparse stability histogram. | |
| 147 for (size_t i = 0; i < events.size(); ++i) { | |
| 148 std::string histogram_name( | |
| 149 WatcherMetricsProviderWin::kExitFunnelHistogramPrefix); | |
| 150 histogram_name.append(base::WideToUTF8(events[i].first)); | |
| 151 base::TimeDelta event_time = | |
| 152 base::Time::FromInternalValue(events[i].second) - | |
| 153 base::Time::FromInternalValue(min_time); | |
| 154 base::HistogramBase* histogram = | |
| 155 base::SparseHistogram::FactoryGet( | |
| 156 histogram_name.c_str(), | |
| 157 base::HistogramBase::kUmaStabilityHistogramFlag); | |
| 158 | |
| 159 // Record the time rounded up to the nearest millisecond. | |
| 160 histogram->Add(event_time.InMillisecondsRoundedUp()); | |
| 161 } | |
| 162 } | |
| 163 | |
| 164 void RecordExitFunnels(const base::string16& registry_path) { | |
| 165 base::win::RegistryKeyIterator it(HKEY_CURRENT_USER, registry_path.c_str()); | |
| 166 if (!it.Valid()) | |
| 167 return; | |
| 168 | |
| 169 // Exit early if no work to do. | |
| 170 if (it.SubkeyCount() == 0) | |
| 171 return; | |
| 172 | |
| 173 // Open the key we use for deletion preemptively to prevent reporting | |
| 174 // multiple times on permission problems. | |
| 175 base::win::RegKey key(HKEY_CURRENT_USER, | |
| 176 registry_path.c_str(), | |
| 177 KEY_QUERY_VALUE); | |
| 178 if (!key.Valid()) { | |
| 179 LOG(ERROR) << "Failed to open " << registry_path << " for writing."; | |
| 180 return; | |
| 181 } | |
| 182 | |
| 183 std::vector<base::string16> to_delete; | |
| 184 for (; it.Valid(); ++it) { | |
| 185 RecordSingleExitFunnel(&key, it.Name()); | |
| 186 to_delete.push_back(it.Name()); | |
| 187 } | |
| 188 | |
| 189 for (size_t i = 0; i < to_delete.size(); ++i) { | |
| 190 LONG res = key.DeleteEmptyKey(to_delete[i].c_str()); | |
| 191 if (res != ERROR_SUCCESS) | |
| 192 LOG(ERROR) << "Failed to delete key " << to_delete[i]; | |
| 193 } | |
| 194 } | |
| 195 | |
| 63 } // namespace | 196 } // namespace |
| 64 | 197 |
| 65 const char WatcherMetricsProviderWin::kBrowserExitCodeHistogramName[] = | 198 const char WatcherMetricsProviderWin::kBrowserExitCodeHistogramName[] = |
| 66 "Stability.BrowserExitCodes"; | 199 "Stability.BrowserExitCodes"; |
| 200 const char WatcherMetricsProviderWin::kExitFunnelHistogramPrefix[] = | |
| 201 "Stability.ExitFunnel."; | |
| 67 | 202 |
| 68 WatcherMetricsProviderWin::WatcherMetricsProviderWin( | 203 WatcherMetricsProviderWin::WatcherMetricsProviderWin( |
| 69 const base::char16* registry_path) : registry_path_(registry_path) { | 204 const base::char16* registry_path) : registry_path_(registry_path) { |
| 70 } | 205 } |
| 71 | 206 |
| 72 WatcherMetricsProviderWin::~WatcherMetricsProviderWin() { | 207 WatcherMetricsProviderWin::~WatcherMetricsProviderWin() { |
| 73 } | 208 } |
| 74 | 209 |
| 75 void WatcherMetricsProviderWin::ProvideStabilityMetrics( | 210 void WatcherMetricsProviderWin::ProvideStabilityMetrics( |
| 76 metrics::SystemProfileProto* /* system_profile_proto */) { | 211 metrics::SystemProfileProto* /* system_profile_proto */) { |
| 77 // Note that if there are multiple instances of Chrome running in the same | 212 // Note that if there are multiple instances of Chrome running in the same |
| 78 // user account, there's a small race that will double-report the exit codes | 213 // user account, there's a small race that will double-report the exit codes |
| 79 // from both/multiple instances. This ought to be vanishingly rare and will | 214 // from both/multiple instances. This ought to be vanishingly rare and will |
| 80 // only manifest as low-level "random" noise. To work around this it would be | 215 // only manifest as low-level "random" noise. To work around this it would be |
| 81 // necessary to implement some form of global locking, which is not worth it | 216 // necessary to implement some form of global locking, which is not worth it |
| 82 // here. | 217 // here. |
| 83 base::win::RegKey regkey(HKEY_CURRENT_USER, | 218 RecordExitCodes(registry_path_); |
| 84 registry_path_.c_str(), | 219 RecordExitFunnels(registry_path_); |
| 85 KEY_QUERY_VALUE | KEY_SET_VALUE); | |
| 86 | |
| 87 size_t num = regkey.GetValueCount(); | |
| 88 if (num != 0) { | |
| 89 std::vector<base::string16> to_delete; | |
| 90 | |
| 91 // Record the exit codes in a sparse stability histogram, as the range of | |
| 92 // values used to report failures is large. | |
| 93 base::HistogramBase* exit_code_histogram = | |
| 94 base::SparseHistogram::FactoryGet(kBrowserExitCodeHistogramName, | |
| 95 base::HistogramBase::kUmaStabilityHistogramFlag); | |
| 96 | |
| 97 for (size_t i = 0; i < num; ++i) { | |
| 98 base::string16 name; | |
| 99 if (regkey.GetValueNameAt(static_cast<int>(i), &name) == ERROR_SUCCESS) { | |
| 100 DWORD exit_code = 0; | |
| 101 if (regkey.ReadValueDW(name.c_str(), &exit_code) == ERROR_SUCCESS) { | |
| 102 // Do not report exit codes for processes that are still live, | |
| 103 // notably for our own process. | |
| 104 if (exit_code != STILL_ACTIVE || IsDeadProcess(name)) { | |
| 105 to_delete.push_back(name); | |
| 106 exit_code_histogram->Add(exit_code); | |
| 107 } | |
| 108 } | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 // Delete the values reported above. | |
| 113 for (size_t i = 0; i < to_delete.size(); ++i) | |
| 114 regkey.DeleteValue(to_delete[i].c_str()); | |
| 115 } | |
| 116 } | 220 } |
| 117 | 221 |
| 118 } // namespace browser_watcher | 222 } // namespace browser_watcher |
| OLD | NEW |