OLD | NEW |
(Empty) | |
| 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 |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "components/browser_watcher/watcher_metrics_provider_win.h" |
| 6 |
| 7 #include <vector> |
| 8 |
| 9 #include "base/metrics/sparse_histogram.h" |
| 10 #include "base/process/process_handle.h" |
| 11 #include "base/strings/string_number_conversions.h" |
| 12 #include "base/strings/string_piece.h" |
| 13 #include "base/win/registry.h" |
| 14 |
| 15 namespace browser_watcher { |
| 16 |
| 17 namespace { |
| 18 |
| 19 void CompileAsserts() { |
| 20 // 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 |
| 22 // the high bit set on Windows, so there's no danger of overflow if this is |
| 23 // done consistently. |
| 24 COMPILE_ASSERT(sizeof(DWORD) == sizeof(int), |
| 25 process_ids_have_outgrown_an_int); |
| 26 } |
| 27 |
| 28 // This function does soft matching on the PID recorded in the key only. |
| 29 // Due to PID reuse, the possibility exists that the process that's now live |
| 30 // with the given PID is not the same process the data was recorded for. |
| 31 // This doesn't matter for the purpose, as eventually the data will be |
| 32 // scavenged and reported. |
| 33 bool IsDeadProcess(base::StringPiece16 key_name) { |
| 34 // Truncate the input string to the first occurrence of '-', if one exists. |
| 35 size_t num_end = key_name.find(L'-'); |
| 36 if (num_end != base::StringPiece16::npos) |
| 37 key_name = key_name.substr(0, num_end); |
| 38 |
| 39 // Convert to the numeric PID. |
| 40 int pid = 0; |
| 41 if (!base::StringToInt(key_name, &pid) || pid == 0) |
| 42 return true; |
| 43 |
| 44 // This is a very inexpensive check for the common case of our own PID. |
| 45 if (static_cast<base::ProcessId>(pid) == base::GetCurrentProcId()) |
| 46 return false; |
| 47 |
| 48 // The process is not our own - see whether a process with this PID exists. |
| 49 // This is more expensive than the above check, but should also be very rare, |
| 50 // as this only happens more than once for a given PID if a user is running |
| 51 // multiple Chrome instances concurrently. |
| 52 base::ProcessHandle process = base::kNullProcessHandle; |
| 53 if (base::OpenProcessHandle(static_cast<base::ProcessId>(pid), &process)) { |
| 54 base::CloseProcessHandle(process); |
| 55 |
| 56 // The fact that it was possible to open the process says it's live. |
| 57 return false; |
| 58 } |
| 59 |
| 60 return true; |
| 61 } |
| 62 |
| 63 } // namespace |
| 64 |
| 65 const char WatcherMetricsProviderWin::kBrowserExitCodeHistogramName[] = |
| 66 "Stability.BrowserExitCodes"; |
| 67 |
| 68 WatcherMetricsProviderWin::WatcherMetricsProviderWin( |
| 69 const base::char16* registry_path) : registry_path_(registry_path) { |
| 70 } |
| 71 |
| 72 WatcherMetricsProviderWin::~WatcherMetricsProviderWin() { |
| 73 } |
| 74 |
| 75 void WatcherMetricsProviderWin::ProvideStabilityMetrics( |
| 76 metrics::SystemProfileProto* /* system_profile_proto */) { |
| 77 // 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 |
| 79 // 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 |
| 81 // necessary to implement some form of global locking, which is not worth it |
| 82 // here. |
| 83 base::win::RegKey regkey(HKEY_CURRENT_USER, |
| 84 registry_path_.c_str(), |
| 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 } |
| 117 |
| 118 } // namespace browser_watcher |
OLD | NEW |