license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 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. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
| 5 | |
| 6 | |
| 7 | //------------------------------------------------------------------------------ |
| 8 | // Description of the life cycle of a instance of MetricsService. |
| 9 | // |
| 10 | // OVERVIEW |
| 11 | // |
| 12 | // A MetricsService instance is typically created at application startup. It |
| 13 | // is the central controller for the acquisition of log data, and the automatic |
| 14 | // transmission of that log data to an external server. Its major job is to |
| 15 | // manage logs, grouping them for transmission, and transmitting them. As part |
| 16 | // of its grouping, MS finalizes logs by including some just-in-time gathered |
| 17 | // memory statistics, snapshotting the current stats of numerous histograms, |
| 18 | // closing the logs, translating to XML text, and compressing the results for |
| 19 | // transmission. Transmission includes submitting a compressed log as data in a |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 20 | // URL-post, and retransmitting (or retaining at process termination) if the |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 21 | // attempted transmission failed. Retention across process terminations is done |
| 22 | // using the the PrefServices facilities. The format for the retained |
| 23 | // logs (ones that never got transmitted) is always the uncompressed textual |
| 24 | // representation. |
| 25 | // |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 26 | // Logs fall into one of two categories: "initial logs," and "ongoing logs." |
| 27 | // There is at most one initial log sent for each complete run of the chromium |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | // product (from startup, to browser shutdown). An initial log is generally |
| 29 | // transmitted some short time (1 minute?) after startup, and includes stats |
| 30 | // such as recent crash info, the number and types of plugins, etc. The |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 31 | // external server's response to the initial log conceptually tells this MS if |
| 32 | // it should continue transmitting logs (during this session). The server |
| 33 | // response can actually be much more detailed, and always includes (at a |
| 34 | // minimum) how often additional ongoing logs should be sent. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 35 | // |
| 36 | // After the above initial log, a series of ongoing logs will be transmitted. |
| 37 | // The first ongoing log actually begins to accumulate information stating when |
| 38 | // the MS was first constructed. Note that even though the initial log is |
| 39 | // commonly sent a full minute after startup, the initial log does not include |
| 40 | // much in the way of user stats. The most common interlog period (delay) |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 41 | // is 20 minutes. That time period starts when the first user action causes a |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 42 | // logging event. This means that if there is no user action, there may be long |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 43 | // periods without any (ongoing) log transmissions. Ongoing logs typically |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 44 | // contain very detailed records of user activities (ex: opened tab, closed |
| 45 | // tab, fetched URL, maximized window, etc.) In addition, just before an |
| 46 | // ongoing log is closed out, a call is made to gather memory statistics. Those |
| 47 | // memory statistics are deposited into a histogram, and the log finalization |
| 48 | // code is then called. In the finalization, a call to a Histogram server |
| 49 | // acquires a list of all local histograms that have been flagged for upload |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 50 | // to the UMA server. The finalization also acquires a the most recent number |
| 51 | // of page loads, along with any counts of renderer or plugin crashes. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 52 | // |
| 53 | // When the browser shuts down, there will typically be a fragment of an ongoing |
| 54 | // log that has not yet been transmitted. At shutdown time, that fragment |
| 55 | // is closed (including snapshotting histograms), and converted to text. Note |
| 56 | // that memory stats are not gathered during shutdown, as gathering *might* be |
| 57 | // too time consuming. The textual representation of the fragment of the |
| 58 | // ongoing log is then stored persistently as a string in the PrefServices, for |
| 59 | // potential transmission during a future run of the product. |
| 60 | // |
| 61 | // There are two slightly abnormal shutdown conditions. There is a |
| 62 | // "disconnected scenario," and a "really fast startup and shutdown" scenario. |
| 63 | // In the "never connected" situation, the user has (during the running of the |
| 64 | // process) never established an internet connection. As a result, attempts to |
| 65 | // transmit the initial log have failed, and a lot(?) of data has accumulated in |
| 66 | // the ongoing log (which didn't yet get closed, because there was never even a |
| 67 | // contemplation of sending it). There is also a kindred "lost connection" |
| 68 | // situation, where a loss of connection prevented an ongoing log from being |
| 69 | // transmitted, and a (still open) log was stuck accumulating a lot(?) of data, |
| 70 | // while the earlier log retried its transmission. In both of these |
| 71 | // disconnected situations, two logs need to be, and are, persistently stored |
| 72 | // for future transmission. |
| 73 | // |
| 74 | // The other unusual shutdown condition, termed "really fast startup and |
| 75 | // shutdown," involves the deliberate user termination of the process before |
| 76 | // the initial log is even formed or transmitted. In that situation, no logging |
| 77 | // is done, but the historical crash statistics remain (unlogged) for inclusion |
| 78 | // in a future run's initial log. (i.e., we don't lose crash stats). |
| 79 | // |
| 80 | // With the above overview, we can now describe the state machine's various |
| 81 | // stats, based on the State enum specified in the state_ member. Those states |
| 82 | // are: |
| 83 | // |
| 84 | // INITIALIZED, // Constructor was called. |
[email protected] | 28ab7f9 | 2009-01-06 21:39:04 | [diff] [blame] | 85 | // PLUGIN_LIST_REQUESTED, // Waiting for plugin list to be loaded. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 86 | // PLUGIN_LIST_ARRIVED, // Waiting for timer to send initial log. |
| 87 | // INITIAL_LOG_READY, // Initial log generated, and waiting for reply. |
| 88 | // SEND_OLD_INITIAL_LOGS, // Sending unsent logs from previous session. |
| 89 | // SENDING_OLD_LOGS, // Sending unsent logs from previous session. |
| 90 | // SENDING_CURRENT_LOGS, // Sending standard current logs as they accrue. |
| 91 | // |
| 92 | // In more detail, we have: |
| 93 | // |
| 94 | // INITIALIZED, // Constructor was called. |
| 95 | // The MS has been constructed, but has taken no actions to compose the |
| 96 | // initial log. |
| 97 | // |
[email protected] | 28ab7f9 | 2009-01-06 21:39:04 | [diff] [blame] | 98 | // PLUGIN_LIST_REQUESTED, // Waiting for plugin list to be loaded. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 99 | // Typically about 30 seconds after startup, a task is sent to a second thread |
| 100 | // to get the list of plugins. That task will (when complete) make an async |
| 101 | // callback (via a Task) to indicate the completion. |
| 102 | // |
| 103 | // PLUGIN_LIST_ARRIVED, // Waiting for timer to send initial log. |
| 104 | // The callback has arrived, and it is now possible for an initial log to be |
| 105 | // created. This callback typically arrives back less than one second after |
| 106 | // the task is dispatched. |
| 107 | // |
| 108 | // INITIAL_LOG_READY, // Initial log generated, and waiting for reply. |
| 109 | // This state is entered only after an initial log has been composed, and |
| 110 | // prepared for transmission. It is also the case that any previously unsent |
| 111 | // logs have been loaded into instance variables for possible transmission. |
| 112 | // |
| 113 | // SEND_OLD_INITIAL_LOGS, // Sending unsent logs from previous session. |
| 114 | // This state indicates that the initial log for this session has been |
| 115 | // successfully sent and it is now time to send any "initial logs" that were |
| 116 | // saved from previous sessions. Most commonly, there are none, but all old |
| 117 | // logs that were "initial logs" must be sent before this state is exited. |
| 118 | // |
| 119 | // SENDING_OLD_LOGS, // Sending unsent logs from previous session. |
| 120 | // This state indicates that there are no more unsent initial logs, and now any |
| 121 | // ongoing logs from previous sessions should be transmitted. All such logs |
| 122 | // will be transmitted before exiting this state, and proceeding with ongoing |
| 123 | // logs from the current session (see next state). |
| 124 | // |
| 125 | // SENDING_CURRENT_LOGS, // Sending standard current logs as they accrue. |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 126 | // Current logs are being accumulated. Typically every 20 minutes a log is |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 127 | // closed and finalized for transmission, at the same time as a new log is |
| 128 | // started. |
| 129 | // |
| 130 | // The progression through the above states is simple, and sequential, in the |
| 131 | // most common use cases. States proceed from INITIAL to SENDING_CURRENT_LOGS, |
| 132 | // and remain in the latter until shutdown. |
| 133 | // |
| 134 | // The one unusual case is when the user asks that we stop logging. When that |
| 135 | // happens, any pending (transmission in progress) log is pushed into the list |
| 136 | // of old unsent logs (the appropriate list, depending on whether it is an |
| 137 | // initial log, or an ongoing log). An addition, any log that is currently |
| 138 | // accumulating is also finalized, and pushed into the unsent log list. With |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 139 | // those pushes performed, we regress back to the SEND_OLD_INITIAL_LOGS state in |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | // case the user enables log recording again during this session. This way |
| 141 | // anything we have "pushed back" will be sent automatically if/when we progress |
| 142 | // back to SENDING_CURRENT_LOG state. |
| 143 | // |
| 144 | // Also note that whenever the member variables containing unsent logs are |
| 145 | // modified (i.e., when we send an old log), we mirror the list of logs into |
| 146 | // the PrefServices. This ensures that IF we crash, we won't start up and |
| 147 | // retransmit our old logs again. |
| 148 | // |
| 149 | // Due to race conditions, it is always possible that a log file could be sent |
| 150 | // twice. For example, if a log file is sent, but not yet acknowledged by |
| 151 | // the external server, and the user shuts down, then a copy of the log may be |
| 152 | // saved for re-transmission. These duplicates could be filtered out server |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 153 | // side, but are not expected to be a significant problem. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | // |
| 155 | // |
| 156 | //------------------------------------------------------------------------------ |
| 157 | |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 158 | #include "chrome/browser/metrics/metrics_service.h" |
| 159 | |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 160 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 161 | #include <windows.h> |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 162 | #include <objbase.h> |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 163 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 164 | |
[email protected] | 690a99c | 2009-01-06 16:48:45 | [diff] [blame] | 165 | #include "base/file_path.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 166 | #include "base/histogram.h" |
| 167 | #include "base/path_service.h" |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 168 | #include "base/platform_thread.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 169 | #include "base/string_util.h" |
| 170 | #include "base/task.h" |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 171 | #include "chrome/browser/bookmarks/bookmark_model.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 172 | #include "chrome/browser/browser.h" |
| 173 | #include "chrome/browser/browser_list.h" |
| 174 | #include "chrome/browser/browser_process.h" |
| 175 | #include "chrome/browser/load_notification_details.h" |
| 176 | #include "chrome/browser/memory_details.h" |
[email protected] | fd49e2d | 2009-02-20 17:21:30 | [diff] [blame] | 177 | #include "chrome/browser/plugin_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 178 | #include "chrome/browser/profile.h" |
[email protected] | 8c8657d6 | 2009-01-16 18:31:26 | [diff] [blame] | 179 | #include "chrome/browser/renderer_host/render_process_host.h" |
[email protected] | d54e03a5 | 2009-01-16 00:31:04 | [diff] [blame] | 180 | #include "chrome/browser/search_engines/template_url.h" |
| 181 | #include "chrome/browser/search_engines/template_url_model.h" |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 182 | #include "chrome/common/child_process_info.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 183 | #include "chrome/common/chrome_paths.h" |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 184 | #include "chrome/common/libxml_utils.h" |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 185 | #include "chrome/common/notification_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 186 | #include "chrome/common/pref_names.h" |
| 187 | #include "chrome/common/pref_service.h" |
[email protected] | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 188 | #include "chrome/common/render_messages.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | #include "googleurl/src/gurl.h" |
| 190 | #include "net/base/load_flags.h" |
| 191 | #include "third_party/bzip2/bzlib.h" |
| 192 | |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 193 | #if defined(OS_POSIX) |
| 194 | // TODO(port): Move these headers above as they are ported. |
| 195 | #include "chrome/common/temp_scaffolding_stubs.h" |
| 196 | #else |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 197 | #include "chrome/installer/util/google_update_settings.h" |
| 198 | #endif |
| 199 | |
[email protected] | e1acf6f | 2008-10-27 20:43:33 | [diff] [blame] | 200 | using base::Time; |
| 201 | using base::TimeDelta; |
| 202 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 203 | // Check to see that we're being called on only one thread. |
| 204 | static bool IsSingleThreaded(); |
| 205 | |
| 206 | static const char kMetricsURL[] = |
[email protected] | 0acdfc4 | 2009-01-30 01:13:22 | [diff] [blame] | 207 | "https://clients4.google.com/firefox/metrics/collect"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 208 | |
| 209 | static const char kMetricsType[] = "application/vnd.mozilla.metrics.bz2"; |
| 210 | |
| 211 | // The delay, in seconds, after startup before sending the first log message. |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 212 | static const int kInitialInterlogDuration = 60; // one minute |
| 213 | |
| 214 | // The default maximum number of events in a log uploaded to the UMA server. |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 215 | static const int kInitialEventLimit = 2400; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 216 | |
| 217 | // If an upload fails, and the transmission was over this byte count, then we |
| 218 | // will discard the log, and not try to retransmit it. We also don't persist |
| 219 | // the log to the prefs for transmission during the next chrome session if this |
| 220 | // limit is exceeded. |
| 221 | static const int kUploadLogAvoidRetransmitSize = 50000; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 222 | |
| 223 | // When we have logs from previous Chrome sessions to send, how long should we |
| 224 | // delay (in seconds) between each log transmission. |
| 225 | static const int kUnsentLogDelay = 15; // 15 seconds |
| 226 | |
| 227 | // Minimum time a log typically exists before sending, in seconds. |
| 228 | // This number is supplied by the server, but until we parse it out of a server |
| 229 | // response, we use this duration to specify how long we should wait before |
| 230 | // sending the next log. If the channel is busy, such as when there is a |
| 231 | // failure during an attempt to transmit a previous log, then a log may wait |
| 232 | // (and continue to accrue now log entries) for a much greater period of time. |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 233 | static const int kMinSecondsPerLog = 20 * 60; // Twenty minutes. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 234 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 235 | // When we don't succeed at transmitting a log to a server, we progressively |
| 236 | // wait longer and longer before sending the next log. This backoff process |
| 237 | // help reduce load on the server, and makes the amount of backoff vary between |
| 238 | // clients so that a collision (server overload?) on retransmit is less likely. |
| 239 | // The following is the constant we use to expand that inter-log duration. |
| 240 | static const double kBackoff = 1.1; |
| 241 | // We limit the maximum backoff to be no greater than some multiple of the |
| 242 | // default kMinSecondsPerLog. The following is that maximum ratio. |
| 243 | static const int kMaxBackoff = 10; |
| 244 | |
| 245 | // Interval, in seconds, between state saves. |
| 246 | static const int kSaveStateInterval = 5 * 60; // five minutes |
| 247 | |
| 248 | // The number of "initial" logs we're willing to save, and hope to send during |
| 249 | // a future Chrome session. Initial logs contain crash stats, and are pretty |
| 250 | // small. |
| 251 | static const size_t kMaxInitialLogsPersisted = 20; |
| 252 | |
| 253 | // The number of ongoing logs we're willing to save persistently, and hope to |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 254 | // send during a this or future sessions. Note that each log may be pretty |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 255 | // large, as presumably the related "initial" log wasn't sent (probably nothing |
| 256 | // was, as the user was probably off-line). As a result, the log probably kept |
| 257 | // accumulating while the "initial" log was stalled (pending_), and couldn't be |
| 258 | // sent. As a result, we don't want to save too many of these mega-logs. |
| 259 | // A "standard shutdown" will create a small log, including just the data that |
| 260 | // was not yet been transmitted, and that is normal (to have exactly one |
| 261 | // ongoing_log_ at startup). |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 262 | static const size_t kMaxOngoingLogsPersisted = 8; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 263 | |
| 264 | |
| 265 | // Handles asynchronous fetching of memory details. |
| 266 | // Will run the provided task after finished. |
| 267 | class MetricsMemoryDetails : public MemoryDetails { |
| 268 | public: |
| 269 | explicit MetricsMemoryDetails(Task* completion) : completion_(completion) {} |
| 270 | |
| 271 | virtual void OnDetailsAvailable() { |
| 272 | MessageLoop::current()->PostTask(FROM_HERE, completion_); |
| 273 | } |
| 274 | |
| 275 | private: |
| 276 | Task* completion_; |
| 277 | DISALLOW_EVIL_CONSTRUCTORS(MetricsMemoryDetails); |
| 278 | }; |
| 279 | |
| 280 | class MetricsService::GetPluginListTaskComplete : public Task { |
| 281 | virtual void Run() { |
| 282 | g_browser_process->metrics_service()->OnGetPluginListTaskComplete(); |
| 283 | } |
| 284 | }; |
| 285 | |
| 286 | class MetricsService::GetPluginListTask : public Task { |
| 287 | public: |
| 288 | explicit GetPluginListTask(MessageLoop* callback_loop) |
| 289 | : callback_loop_(callback_loop) {} |
| 290 | |
| 291 | virtual void Run() { |
| 292 | std::vector<WebPluginInfo> plugins; |
| 293 | PluginService::GetInstance()->GetPlugins(false, &plugins); |
| 294 | |
| 295 | callback_loop_->PostTask(FROM_HERE, new GetPluginListTaskComplete()); |
| 296 | } |
| 297 | |
| 298 | private: |
| 299 | MessageLoop* callback_loop_; |
| 300 | }; |
| 301 | |
| 302 | // static |
| 303 | void MetricsService::RegisterPrefs(PrefService* local_state) { |
| 304 | DCHECK(IsSingleThreaded()); |
| 305 | local_state->RegisterStringPref(prefs::kMetricsClientID, L""); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 306 | local_state->RegisterInt64Pref(prefs::kMetricsClientIDTimestamp, 0); |
| 307 | local_state->RegisterInt64Pref(prefs::kStabilityLaunchTimeSec, 0); |
| 308 | local_state->RegisterInt64Pref(prefs::kStabilityLastTimestampSec, 0); |
| 309 | local_state->RegisterInt64Pref(prefs::kStabilityUptimeSec, 0); |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 310 | local_state->RegisterStringPref(prefs::kStabilityStatsVersion, L""); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 311 | local_state->RegisterBooleanPref(prefs::kStabilityExitedCleanly, true); |
| 312 | local_state->RegisterBooleanPref(prefs::kStabilitySessionEndCompleted, true); |
| 313 | local_state->RegisterIntegerPref(prefs::kMetricsSessionID, -1); |
| 314 | local_state->RegisterIntegerPref(prefs::kStabilityLaunchCount, 0); |
| 315 | local_state->RegisterIntegerPref(prefs::kStabilityCrashCount, 0); |
| 316 | local_state->RegisterIntegerPref(prefs::kStabilityIncompleteSessionEndCount, |
| 317 | 0); |
| 318 | local_state->RegisterIntegerPref(prefs::kStabilityPageLoadCount, 0); |
| 319 | local_state->RegisterIntegerPref(prefs::kSecurityRendererOnSboxDesktop, 0); |
| 320 | local_state->RegisterIntegerPref(prefs::kSecurityRendererOnDefaultDesktop, 0); |
| 321 | local_state->RegisterIntegerPref(prefs::kStabilityRendererCrashCount, 0); |
| 322 | local_state->RegisterIntegerPref(prefs::kStabilityRendererHangCount, 0); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 323 | local_state->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationFail, |
| 324 | 0); |
| 325 | local_state->RegisterIntegerPref(prefs::kStabilityBreakpadRegistrationSuccess, |
| 326 | 0); |
| 327 | local_state->RegisterIntegerPref(prefs::kStabilityDebuggerPresent, 0); |
| 328 | local_state->RegisterIntegerPref(prefs::kStabilityDebuggerNotPresent, 0); |
| 329 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 330 | local_state->RegisterDictionaryPref(prefs::kProfileMetrics); |
| 331 | local_state->RegisterIntegerPref(prefs::kNumBookmarksOnBookmarkBar, 0); |
| 332 | local_state->RegisterIntegerPref(prefs::kNumFoldersOnBookmarkBar, 0); |
| 333 | local_state->RegisterIntegerPref(prefs::kNumBookmarksInOtherBookmarkFolder, |
| 334 | 0); |
| 335 | local_state->RegisterIntegerPref(prefs::kNumFoldersInOtherBookmarkFolder, 0); |
| 336 | local_state->RegisterIntegerPref(prefs::kNumKeywords, 0); |
| 337 | local_state->RegisterListPref(prefs::kMetricsInitialLogs); |
| 338 | local_state->RegisterListPref(prefs::kMetricsOngoingLogs); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 339 | |
| 340 | local_state->RegisterInt64Pref(prefs::kUninstallMetricsPageLoadCount, 0); |
| 341 | local_state->RegisterInt64Pref(prefs::kUninstallLaunchCount, 0); |
| 342 | local_state->RegisterInt64Pref(prefs::kUninstallMetricsUptimeSec, 0); |
| 343 | local_state->RegisterInt64Pref(prefs::kUninstallLastLaunchTimeSec, 0); |
| 344 | local_state->RegisterInt64Pref(prefs::kUninstallLastObservedRunTimeSec, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 345 | } |
| 346 | |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 347 | // static |
| 348 | void MetricsService::DiscardOldStabilityStats(PrefService* local_state) { |
| 349 | local_state->SetBoolean(prefs::kStabilityExitedCleanly, true); |
| 350 | |
| 351 | local_state->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
| 352 | local_state->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0); |
| 353 | local_state->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0); |
| 354 | local_state->SetInteger(prefs::kStabilityDebuggerPresent, 0); |
| 355 | local_state->SetInteger(prefs::kStabilityDebuggerNotPresent, 0); |
| 356 | |
| 357 | local_state->SetInteger(prefs::kStabilityLaunchCount, 0); |
| 358 | local_state->SetInteger(prefs::kStabilityCrashCount, 0); |
| 359 | |
| 360 | local_state->SetInteger(prefs::kStabilityPageLoadCount, 0); |
| 361 | local_state->SetInteger(prefs::kStabilityRendererCrashCount, 0); |
| 362 | local_state->SetInteger(prefs::kStabilityRendererHangCount, 0); |
| 363 | |
| 364 | local_state->SetInteger(prefs::kSecurityRendererOnSboxDesktop, 0); |
| 365 | local_state->SetInteger(prefs::kSecurityRendererOnDefaultDesktop, 0); |
| 366 | |
| 367 | local_state->SetString(prefs::kStabilityUptimeSec, L"0"); |
| 368 | |
| 369 | local_state->ClearPref(prefs::kStabilityPluginStats); |
| 370 | } |
| 371 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | MetricsService::MetricsService() |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 373 | : recording_active_(false), |
| 374 | reporting_active_(false), |
| 375 | user_permits_upload_(false), |
| 376 | server_permits_upload_(true), |
| 377 | state_(INITIALIZED), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 378 | pending_log_(NULL), |
| 379 | pending_log_text_(""), |
| 380 | current_fetch_(NULL), |
| 381 | current_log_(NULL), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 382 | idle_since_last_transmission_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 383 | next_window_id_(0), |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 384 | ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_factory_(this)), |
| 385 | ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_factory_(this)), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 386 | logged_samples_(), |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 387 | interlog_duration_(TimeDelta::FromSeconds(kInitialInterlogDuration)), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 388 | log_event_limit_(kInitialEventLimit), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 389 | timer_pending_(false) { |
| 390 | DCHECK(IsSingleThreaded()); |
| 391 | InitializeMetricsState(); |
| 392 | } |
| 393 | |
| 394 | MetricsService::~MetricsService() { |
| 395 | SetRecording(false); |
[email protected] | d8bc79bf | 2009-01-28 01:17:58 | [diff] [blame] | 396 | if (pending_log_) { |
| 397 | delete pending_log_; |
| 398 | pending_log_ = NULL; |
| 399 | } |
| 400 | if (current_log_) { |
| 401 | delete current_log_; |
| 402 | current_log_ = NULL; |
| 403 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 404 | } |
| 405 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 406 | void MetricsService::SetUserPermitsUpload(bool enabled) { |
| 407 | HandleIdleSinceLastTransmission(false); |
| 408 | user_permits_upload_ = enabled; |
| 409 | } |
| 410 | |
| 411 | void MetricsService::Start() { |
| 412 | SetRecording(true); |
| 413 | SetReporting(true); |
| 414 | } |
| 415 | |
| 416 | void MetricsService::StartRecordingOnly() { |
| 417 | SetRecording(true); |
| 418 | SetReporting(false); |
| 419 | } |
| 420 | |
| 421 | void MetricsService::Stop() { |
| 422 | SetReporting(false); |
| 423 | SetRecording(false); |
| 424 | } |
| 425 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 426 | void MetricsService::SetRecording(bool enabled) { |
| 427 | DCHECK(IsSingleThreaded()); |
| 428 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 429 | if (enabled == recording_active_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 430 | return; |
| 431 | |
| 432 | if (enabled) { |
[email protected] | b0c819f | 2009-03-08 04:52:15 | [diff] [blame] | 433 | if (client_id_.empty()) { |
| 434 | PrefService* pref = g_browser_process->local_state(); |
| 435 | DCHECK(pref); |
| 436 | client_id_ = WideToUTF8(pref->GetString(prefs::kMetricsClientID)); |
| 437 | if (client_id_.empty()) { |
| 438 | client_id_ = GenerateClientID(); |
| 439 | pref->SetString(prefs::kMetricsClientID, UTF8ToWide(client_id_)); |
| 440 | |
| 441 | // Might as well make a note of how long this ID has existed |
| 442 | pref->SetString(prefs::kMetricsClientIDTimestamp, |
| 443 | Int64ToWString(Time::Now().ToTimeT())); |
| 444 | } |
| 445 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 446 | StartRecording(); |
| 447 | ListenerRegistration(true); |
| 448 | } else { |
| 449 | // Turn off all observers. |
| 450 | ListenerRegistration(false); |
| 451 | PushPendingLogsToUnsentLists(); |
| 452 | DCHECK(!pending_log()); |
| 453 | if (state_ > INITIAL_LOG_READY && unsent_logs()) |
| 454 | state_ = SEND_OLD_INITIAL_LOGS; |
| 455 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 456 | recording_active_ = enabled; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 457 | } |
| 458 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 459 | bool MetricsService::recording_active() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 460 | DCHECK(IsSingleThreaded()); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 461 | return recording_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 462 | } |
| 463 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 464 | void MetricsService::SetReporting(bool enable) { |
| 465 | if (reporting_active_ != enable) { |
| 466 | reporting_active_ = enable; |
| 467 | if (reporting_active_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 468 | StartLogTransmissionTimer(); |
| 469 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 470 | } |
| 471 | |
| 472 | bool MetricsService::reporting_active() const { |
| 473 | DCHECK(IsSingleThreaded()); |
| 474 | return reporting_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | void MetricsService::Observe(NotificationType type, |
| 478 | const NotificationSource& source, |
| 479 | const NotificationDetails& details) { |
| 480 | DCHECK(current_log_); |
| 481 | DCHECK(IsSingleThreaded()); |
| 482 | |
| 483 | if (!CanLogNotification(type, source, details)) |
| 484 | return; |
| 485 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 486 | switch (type.value) { |
| 487 | case NotificationType::USER_ACTION: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 488 | current_log_->RecordUserAction(*Details<const wchar_t*>(details).ptr()); |
| 489 | break; |
| 490 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 491 | case NotificationType::BROWSER_OPENED: |
| 492 | case NotificationType::BROWSER_CLOSED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 493 | LogWindowChange(type, source, details); |
| 494 | break; |
| 495 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 496 | case NotificationType::TAB_PARENTED: |
| 497 | case NotificationType::TAB_CLOSING: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 498 | LogWindowChange(type, source, details); |
| 499 | break; |
| 500 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 501 | case NotificationType::LOAD_STOP: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 502 | LogLoadComplete(type, source, details); |
| 503 | break; |
| 504 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 505 | case NotificationType::LOAD_START: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 506 | LogLoadStarted(); |
| 507 | break; |
| 508 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 509 | case NotificationType::RENDERER_PROCESS_TERMINATED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | if (!*Details<bool>(details).ptr()) |
| 511 | LogRendererCrash(); |
| 512 | break; |
| 513 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 514 | case NotificationType::RENDERER_PROCESS_HANG: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 515 | LogRendererHang(); |
| 516 | break; |
| 517 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 518 | case NotificationType::RENDERER_PROCESS_IN_SBOX: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 519 | LogRendererInSandbox(*Details<bool>(details).ptr()); |
| 520 | break; |
| 521 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 522 | case NotificationType::CHILD_PROCESS_HOST_CONNECTED: |
| 523 | case NotificationType::CHILD_PROCESS_CRASHED: |
| 524 | case NotificationType::CHILD_INSTANCE_CREATED: |
| 525 | LogChildProcessChange(type, source, details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 526 | break; |
| 527 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 528 | case NotificationType::TEMPLATE_URL_MODEL_LOADED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 529 | LogKeywords(Source<TemplateURLModel>(source).ptr()); |
| 530 | break; |
| 531 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 532 | case NotificationType::OMNIBOX_OPENED_URL: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 533 | current_log_->RecordOmniboxOpenedURL( |
| 534 | *Details<AutocompleteLog>(details).ptr()); |
| 535 | break; |
| 536 | |
[email protected] | b61236c6 | 2009-04-09 22:43:55 | [diff] [blame^] | 537 | case NotificationType::BOOKMARK_MODEL_LOADED: { |
| 538 | Profile* p = Source<Profile>(source).ptr(); |
| 539 | if (p) |
| 540 | LogBookmarks(p->GetBookmarkModel()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 541 | break; |
[email protected] | b61236c6 | 2009-04-09 22:43:55 | [diff] [blame^] | 542 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 543 | default: |
| 544 | NOTREACHED(); |
| 545 | break; |
| 546 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 547 | |
| 548 | HandleIdleSinceLastTransmission(false); |
| 549 | |
| 550 | if (current_log_) |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 551 | DLOG(INFO) << "METRICS: NUMBER OF EVENTS = " << current_log_->num_events(); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 552 | } |
| 553 | |
| 554 | void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) { |
| 555 | // If there wasn't a lot of action, maybe the computer was asleep, in which |
| 556 | // case, the log transmissions should have stopped. Here we start them up |
| 557 | // again. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 558 | if (!in_idle && idle_since_last_transmission_) |
| 559 | StartLogTransmissionTimer(); |
| 560 | idle_since_last_transmission_ = in_idle; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 561 | } |
| 562 | |
| 563 | void MetricsService::RecordCleanShutdown() { |
| 564 | RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); |
| 565 | } |
| 566 | |
| 567 | void MetricsService::RecordStartOfSessionEnd() { |
| 568 | RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, false); |
| 569 | } |
| 570 | |
| 571 | void MetricsService::RecordCompletedSessionEnd() { |
| 572 | RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, true); |
| 573 | } |
| 574 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 575 | void MetricsService:: RecordBreakpadRegistration(bool success) { |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 576 | if (!success) |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 577 | IncrementPrefValue(prefs::kStabilityBreakpadRegistrationFail); |
| 578 | else |
| 579 | IncrementPrefValue(prefs::kStabilityBreakpadRegistrationSuccess); |
| 580 | } |
| 581 | |
| 582 | void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) { |
| 583 | if (!has_debugger) |
| 584 | IncrementPrefValue(prefs::kStabilityDebuggerNotPresent); |
| 585 | else |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 586 | IncrementPrefValue(prefs::kStabilityDebuggerPresent); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 587 | } |
| 588 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 589 | //------------------------------------------------------------------------------ |
| 590 | // private methods |
| 591 | //------------------------------------------------------------------------------ |
| 592 | |
| 593 | |
| 594 | //------------------------------------------------------------------------------ |
| 595 | // Initialization methods |
| 596 | |
| 597 | void MetricsService::InitializeMetricsState() { |
| 598 | PrefService* pref = g_browser_process->local_state(); |
| 599 | DCHECK(pref); |
| 600 | |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 601 | if (WideToUTF8(pref->GetString(prefs::kStabilityStatsVersion)) != |
| 602 | MetricsLog::GetVersionString()) { |
| 603 | // This is a new version, so we don't want to confuse the stats about the |
| 604 | // old version with info that we upload. |
| 605 | DiscardOldStabilityStats(pref); |
| 606 | pref->SetString(prefs::kStabilityStatsVersion, |
| 607 | UTF8ToWide(MetricsLog::GetVersionString())); |
| 608 | } |
| 609 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 610 | // Update session ID |
| 611 | session_id_ = pref->GetInteger(prefs::kMetricsSessionID); |
| 612 | ++session_id_; |
| 613 | pref->SetInteger(prefs::kMetricsSessionID, session_id_); |
| 614 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 615 | // Stability bookkeeping |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 616 | IncrementPrefValue(prefs::kStabilityLaunchCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 617 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 618 | if (!pref->GetBoolean(prefs::kStabilityExitedCleanly)) { |
| 619 | IncrementPrefValue(prefs::kStabilityCrashCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 620 | } |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 621 | |
| 622 | // This will be set to 'true' if we exit cleanly. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 623 | pref->SetBoolean(prefs::kStabilityExitedCleanly, false); |
| 624 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 625 | if (!pref->GetBoolean(prefs::kStabilitySessionEndCompleted)) { |
| 626 | IncrementPrefValue(prefs::kStabilityIncompleteSessionEndCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 627 | } |
| 628 | // This is marked false when we get a WM_ENDSESSION. |
| 629 | pref->SetBoolean(prefs::kStabilitySessionEndCompleted, true); |
| 630 | |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 631 | int64 last_start_time = pref->GetInt64(prefs::kStabilityLaunchTimeSec); |
| 632 | int64 last_end_time = pref->GetInt64(prefs::kStabilityLastTimestampSec); |
| 633 | int64 uptime = pref->GetInt64(prefs::kStabilityUptimeSec); |
| 634 | |
| 635 | // Same idea as uptime, except this one never gets reset and is used at |
| 636 | // uninstallation. |
| 637 | int64 uninstall_metrics_uptime = |
| 638 | pref->GetInt64(prefs::kUninstallMetricsUptimeSec); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 639 | |
| 640 | if (last_start_time && last_end_time) { |
| 641 | // TODO(JAR): Exclude sleep time. ... which must be gathered in UI loop. |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 642 | int64 uptime_increment = last_end_time - last_start_time; |
| 643 | uptime += uptime_increment; |
| 644 | pref->SetInt64(prefs::kStabilityUptimeSec, uptime); |
| 645 | |
| 646 | uninstall_metrics_uptime += uptime_increment; |
| 647 | pref->SetInt64(prefs::kUninstallMetricsUptimeSec, |
| 648 | uninstall_metrics_uptime); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 649 | } |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 650 | pref->SetInt64(prefs::kStabilityLaunchTimeSec, Time::Now().ToTimeT()); |
| 651 | |
| 652 | // Bookkeeping for the uninstall metrics. |
| 653 | IncrementLongPrefsValue(prefs::kUninstallLaunchCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 654 | |
| 655 | // Save profile metrics. |
| 656 | PrefService* prefs = g_browser_process->local_state(); |
| 657 | if (prefs) { |
| 658 | // Remove the current dictionary and store it for use when sending data to |
| 659 | // server. By removing the value we prune potentially dead profiles |
| 660 | // (and keys). All valid values are added back once services startup. |
| 661 | const DictionaryValue* profile_dictionary = |
| 662 | prefs->GetDictionary(prefs::kProfileMetrics); |
| 663 | if (profile_dictionary) { |
| 664 | // Do a deep copy of profile_dictionary since ClearPref will delete it. |
| 665 | profile_dictionary_.reset(static_cast<DictionaryValue*>( |
| 666 | profile_dictionary->DeepCopy())); |
| 667 | prefs->ClearPref(prefs::kProfileMetrics); |
| 668 | } |
| 669 | } |
| 670 | |
| 671 | // Kick off the process of saving the state (so the uptime numbers keep |
| 672 | // getting updated) every n minutes. |
| 673 | ScheduleNextStateSave(); |
| 674 | } |
| 675 | |
| 676 | void MetricsService::OnGetPluginListTaskComplete() { |
| 677 | DCHECK(state_ == PLUGIN_LIST_REQUESTED); |
| 678 | if (state_ == PLUGIN_LIST_REQUESTED) |
| 679 | state_ = PLUGIN_LIST_ARRIVED; |
| 680 | } |
| 681 | |
| 682 | std::string MetricsService::GenerateClientID() { |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 683 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 684 | const int kGUIDSize = 39; |
| 685 | |
| 686 | GUID guid; |
| 687 | HRESULT guid_result = CoCreateGuid(&guid); |
| 688 | DCHECK(SUCCEEDED(guid_result)); |
| 689 | |
| 690 | std::wstring guid_string; |
| 691 | int result = StringFromGUID2(guid, |
| 692 | WriteInto(&guid_string, kGUIDSize), kGUIDSize); |
| 693 | DCHECK(result == kGUIDSize); |
| 694 | |
| 695 | return WideToUTF8(guid_string.substr(1, guid_string.length() - 2)); |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 696 | #else |
| 697 | // TODO(port): Implement for Mac and linux. |
[email protected] | f5d0e15 | 2009-02-19 21:54:37 | [diff] [blame] | 698 | // Rather than actually implementing a random source, might this be a good |
| 699 | // time to implement http://code.google.com/p/chromium/issues/detail?id=2278 |
| 700 | // ? I think so! |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 701 | NOTIMPLEMENTED(); |
| 702 | return std::string(); |
| 703 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | |
| 707 | //------------------------------------------------------------------------------ |
| 708 | // State save methods |
| 709 | |
| 710 | void MetricsService::ScheduleNextStateSave() { |
| 711 | state_saver_factory_.RevokeAll(); |
| 712 | |
| 713 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 714 | state_saver_factory_.NewRunnableMethod(&MetricsService::SaveLocalState), |
| 715 | kSaveStateInterval * 1000); |
| 716 | } |
| 717 | |
| 718 | void MetricsService::SaveLocalState() { |
| 719 | PrefService* pref = g_browser_process->local_state(); |
| 720 | if (!pref) { |
| 721 | NOTREACHED(); |
| 722 | return; |
| 723 | } |
| 724 | |
| 725 | RecordCurrentState(pref); |
| 726 | pref->ScheduleSavePersistentPrefs(g_browser_process->file_thread()); |
| 727 | |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 728 | // TODO(jar): Does this run down the batteries???? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 729 | ScheduleNextStateSave(); |
| 730 | } |
| 731 | |
| 732 | |
| 733 | //------------------------------------------------------------------------------ |
| 734 | // Recording control methods |
| 735 | |
| 736 | void MetricsService::StartRecording() { |
| 737 | if (current_log_) |
| 738 | return; |
| 739 | |
| 740 | current_log_ = new MetricsLog(client_id_, session_id_); |
| 741 | if (state_ == INITIALIZED) { |
| 742 | // We only need to schedule that run once. |
| 743 | state_ = PLUGIN_LIST_REQUESTED; |
| 744 | |
| 745 | // Make sure the plugin list is loaded before the inital log is sent, so |
| 746 | // that the main thread isn't blocked generating the list. |
| 747 | g_browser_process->file_thread()->message_loop()->PostDelayedTask(FROM_HERE, |
| 748 | new GetPluginListTask(MessageLoop::current()), |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 749 | kInitialInterlogDuration * 1000 / 2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 750 | } |
| 751 | } |
| 752 | |
| 753 | void MetricsService::StopRecording(MetricsLog** log) { |
| 754 | if (!current_log_) |
| 755 | return; |
| 756 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 757 | // TODO(jar): Integrate bounds on log recording more consistently, so that we |
| 758 | // can stop recording logs that are too big much sooner. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 759 | if (current_log_->num_events() > log_event_limit_) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 760 | UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 761 | current_log_->num_events()); |
| 762 | current_log_->CloseLog(); |
| 763 | delete current_log_; |
[email protected] | 29463878 | 2008-09-24 00:22:41 | [diff] [blame] | 764 | current_log_ = NULL; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 765 | StartRecording(); // Start trivial log to hold our histograms. |
| 766 | } |
| 767 | |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 768 | // Put incremental data (histogram deltas, and realtime stats deltas) at the |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 769 | // end of all log transmissions (initial log handles this separately). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 770 | // Don't bother if we're going to discard current_log_. |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 771 | if (log) { |
[email protected] | c96d5309 | 2009-02-24 01:25:06 | [diff] [blame] | 772 | current_log_->RecordIncrementalStabilityElements(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 773 | RecordCurrentHistograms(); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 774 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 775 | |
| 776 | current_log_->CloseLog(); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 777 | if (log) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 778 | *log = current_log_; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 779 | else |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 780 | delete current_log_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 781 | current_log_ = NULL; |
| 782 | } |
| 783 | |
| 784 | void MetricsService::ListenerRegistration(bool start_listening) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 785 | AddOrRemoveObserver(this, NotificationType::BROWSER_OPENED, start_listening); |
| 786 | AddOrRemoveObserver(this, NotificationType::BROWSER_CLOSED, start_listening); |
| 787 | AddOrRemoveObserver(this, NotificationType::USER_ACTION, start_listening); |
| 788 | AddOrRemoveObserver(this, NotificationType::TAB_PARENTED, start_listening); |
| 789 | AddOrRemoveObserver(this, NotificationType::TAB_CLOSING, start_listening); |
| 790 | AddOrRemoveObserver(this, NotificationType::LOAD_START, start_listening); |
| 791 | AddOrRemoveObserver(this, NotificationType::LOAD_STOP, start_listening); |
| 792 | AddOrRemoveObserver(this, NotificationType::RENDERER_PROCESS_IN_SBOX, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 793 | start_listening); |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 794 | AddOrRemoveObserver(this, NotificationType::RENDERER_PROCESS_TERMINATED, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 795 | start_listening); |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 796 | AddOrRemoveObserver(this, NotificationType::RENDERER_PROCESS_HANG, |
| 797 | start_listening); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 798 | AddOrRemoveObserver(this, NotificationType::CHILD_PROCESS_HOST_CONNECTED, |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 799 | start_listening); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 800 | AddOrRemoveObserver(this, NotificationType::CHILD_INSTANCE_CREATED, |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 801 | start_listening); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 802 | AddOrRemoveObserver(this, NotificationType::CHILD_PROCESS_CRASHED, |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 803 | start_listening); |
| 804 | AddOrRemoveObserver(this, NotificationType::TEMPLATE_URL_MODEL_LOADED, |
| 805 | start_listening); |
| 806 | AddOrRemoveObserver(this, NotificationType::OMNIBOX_OPENED_URL, |
| 807 | start_listening); |
| 808 | AddOrRemoveObserver(this, NotificationType::BOOKMARK_MODEL_LOADED, |
| 809 | start_listening); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // static |
| 813 | void MetricsService::AddOrRemoveObserver(NotificationObserver* observer, |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 814 | NotificationType type, |
| 815 | bool is_add) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 816 | NotificationService* service = NotificationService::current(); |
| 817 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 818 | if (is_add) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 819 | service->AddObserver(observer, type, NotificationService::AllSources()); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 820 | else |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 821 | service->RemoveObserver(observer, type, NotificationService::AllSources()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 822 | } |
| 823 | |
| 824 | void MetricsService::PushPendingLogsToUnsentLists() { |
| 825 | if (state_ < INITIAL_LOG_READY) |
[email protected] | 28ab7f9 | 2009-01-06 21:39:04 | [diff] [blame] | 826 | return; // We didn't and still don't have time to get plugin list etc. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 827 | |
| 828 | if (pending_log()) { |
| 829 | PreparePendingLogText(); |
| 830 | if (state_ == INITIAL_LOG_READY) { |
| 831 | // We may race here, and send second copy of initial log later. |
| 832 | unsent_initial_logs_.push_back(pending_log_text_); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 833 | state_ = SEND_OLD_INITIAL_LOGS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 834 | } else { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 835 | // TODO(jar): Verify correctness in other states, including sending unsent |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 836 | // initial logs. |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 837 | PushPendingLogTextToUnsentOngoingLogs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 838 | } |
| 839 | DiscardPendingLog(); |
| 840 | } |
| 841 | DCHECK(!pending_log()); |
| 842 | StopRecording(&pending_log_); |
| 843 | PreparePendingLogText(); |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 844 | PushPendingLogTextToUnsentOngoingLogs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 845 | DiscardPendingLog(); |
| 846 | StoreUnsentLogs(); |
| 847 | } |
| 848 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 849 | void MetricsService::PushPendingLogTextToUnsentOngoingLogs() { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 850 | // If UMA response told us not to upload, there's no need to save the pending |
| 851 | // log. It wasn't supposed to be uploaded anyway. |
| 852 | if (!server_permits_upload_) |
| 853 | return; |
| 854 | |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 855 | if (pending_log_text_.length() > |
| 856 | static_cast<size_t>(kUploadLogAvoidRetransmitSize)) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 857 | UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 858 | static_cast<int>(pending_log_text_.length())); |
| 859 | return; |
| 860 | } |
| 861 | unsent_ongoing_logs_.push_back(pending_log_text_); |
| 862 | } |
| 863 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 864 | //------------------------------------------------------------------------------ |
| 865 | // Transmission of logs methods |
| 866 | |
| 867 | void MetricsService::StartLogTransmissionTimer() { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 868 | // If we're not reporting, there's no point in starting a log transmission |
| 869 | // timer. |
| 870 | if (!reporting_active()) |
| 871 | return; |
| 872 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 873 | if (!current_log_) |
| 874 | return; // Recorder is shutdown. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 875 | |
| 876 | // If there is already a timer running, we leave it running. |
| 877 | // If timer_pending is true because the fetch is waiting for a response, |
| 878 | // we return for now and let the response handler start the timer. |
| 879 | if (timer_pending_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 880 | return; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 881 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 882 | // Before starting the timer, set timer_pending_ to true. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 883 | timer_pending_ = true; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 884 | |
| 885 | // Right before the UMA transmission gets started, there's one more thing we'd |
| 886 | // like to record: the histogram of memory usage, so we spawn a task to |
| 887 | // collect the memory details and when that task is finished, we arrange for |
| 888 | // TryToStartTransmission to take over. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 889 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 890 | log_sender_factory_. |
| 891 | NewRunnableMethod(&MetricsService::CollectMemoryDetails), |
| 892 | static_cast<int>(interlog_duration_.InMilliseconds())); |
| 893 | } |
| 894 | |
| 895 | void MetricsService::TryToStartTransmission() { |
| 896 | DCHECK(IsSingleThreaded()); |
| 897 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 898 | // This function should only be called via timer, so timer_pending_ |
| 899 | // should be true. |
| 900 | DCHECK(timer_pending_); |
| 901 | timer_pending_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 902 | |
| 903 | DCHECK(!current_fetch_.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 904 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 905 | // If we're getting no notifications, then the log won't have much in it, and |
| 906 | // it's possible the computer is about to go to sleep, so don't upload and |
| 907 | // don't restart the transmission timer. |
| 908 | if (idle_since_last_transmission_) |
| 909 | return; |
| 910 | |
| 911 | // If somehow there is a fetch in progress, we return setting timer_pending_ |
| 912 | // to true and hope things work out. |
| 913 | if (current_fetch_.get()) { |
| 914 | timer_pending_ = true; |
| 915 | return; |
| 916 | } |
| 917 | |
| 918 | // If uploads are forbidden by UMA response, there's no point in keeping |
| 919 | // the current_log_, and the more often we delete it, the less likely it is |
| 920 | // to expand forever. |
| 921 | if (!server_permits_upload_ && current_log_) { |
| 922 | StopRecording(NULL); |
| 923 | StartRecording(); |
| 924 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 925 | |
| 926 | if (!current_log_) |
| 927 | return; // Logging was disabled. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 928 | if (!reporting_active()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 929 | return; // Don't do work if we're not going to send anything now. |
| 930 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 931 | MakePendingLog(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 932 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 933 | // MakePendingLog should have put something in the pending log, if it didn't, |
| 934 | // we start the timer again, return and hope things work out. |
| 935 | if (!pending_log()) { |
| 936 | StartLogTransmissionTimer(); |
| 937 | return; |
| 938 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 939 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 940 | // If we're not supposed to upload any UMA data because the response or the |
| 941 | // user said so, cancel the upload at this point, but start the timer. |
| 942 | if (!TransmissionPermitted()) { |
| 943 | DiscardPendingLog(); |
| 944 | StartLogTransmissionTimer(); |
| 945 | return; |
| 946 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 947 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 948 | PrepareFetchWithPendingLog(); |
| 949 | |
| 950 | if (!current_fetch_.get()) { |
| 951 | // Compression failed, and log discarded :-/. |
| 952 | DiscardPendingLog(); |
| 953 | StartLogTransmissionTimer(); // Maybe we'll do better next time |
| 954 | // TODO(jar): If compression failed, we should have created a tiny log and |
| 955 | // compressed that, so that we can signal that we're losing logs. |
| 956 | return; |
| 957 | } |
| 958 | |
| 959 | DCHECK(!timer_pending_); |
| 960 | |
| 961 | // The URL fetch is a like timer in that after a while we get called back |
| 962 | // so we set timer_pending_ true just as we start the url fetch. |
| 963 | timer_pending_ = true; |
| 964 | current_fetch_->Start(); |
| 965 | |
| 966 | HandleIdleSinceLastTransmission(true); |
| 967 | } |
| 968 | |
| 969 | |
| 970 | void MetricsService::MakePendingLog() { |
| 971 | if (pending_log()) |
| 972 | return; |
| 973 | |
| 974 | switch (state_) { |
| 975 | case INITIALIZED: |
| 976 | case PLUGIN_LIST_REQUESTED: // We should be further along by now. |
| 977 | DCHECK(false); |
| 978 | return; |
| 979 | |
| 980 | case PLUGIN_LIST_ARRIVED: |
| 981 | // We need to wait for the initial log to be ready before sending |
| 982 | // anything, because the server will tell us whether it wants to hear |
| 983 | // from us. |
| 984 | PrepareInitialLog(); |
| 985 | DCHECK(state_ == PLUGIN_LIST_ARRIVED); |
| 986 | RecallUnsentLogs(); |
| 987 | state_ = INITIAL_LOG_READY; |
| 988 | break; |
| 989 | |
| 990 | case SEND_OLD_INITIAL_LOGS: |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 991 | if (!unsent_initial_logs_.empty()) { |
| 992 | pending_log_text_ = unsent_initial_logs_.back(); |
| 993 | break; |
| 994 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 995 | state_ = SENDING_OLD_LOGS; |
| 996 | // Fall through. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 997 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 998 | case SENDING_OLD_LOGS: |
| 999 | if (!unsent_ongoing_logs_.empty()) { |
| 1000 | pending_log_text_ = unsent_ongoing_logs_.back(); |
| 1001 | break; |
| 1002 | } |
| 1003 | state_ = SENDING_CURRENT_LOGS; |
| 1004 | // Fall through. |
| 1005 | |
| 1006 | case SENDING_CURRENT_LOGS: |
| 1007 | StopRecording(&pending_log_); |
| 1008 | StartRecording(); |
| 1009 | break; |
| 1010 | |
| 1011 | default: |
| 1012 | DCHECK(false); |
| 1013 | return; |
| 1014 | } |
| 1015 | |
| 1016 | DCHECK(pending_log()); |
| 1017 | } |
| 1018 | |
| 1019 | bool MetricsService::TransmissionPermitted() const { |
| 1020 | // If the user forbids uploading that's they're business, and we don't upload |
| 1021 | // anything. If the server forbids uploading, that's our business, so we take |
| 1022 | // that to mean it forbids current logs, but we still send up the inital logs |
| 1023 | // and any old logs. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1024 | if (!user_permits_upload_) |
| 1025 | return false; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1026 | if (server_permits_upload_) |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1027 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1028 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1029 | switch (state_) { |
| 1030 | case INITIAL_LOG_READY: |
| 1031 | case SEND_OLD_INITIAL_LOGS: |
| 1032 | case SENDING_OLD_LOGS: |
| 1033 | return true; |
| 1034 | |
| 1035 | case SENDING_CURRENT_LOGS: |
| 1036 | default: |
| 1037 | return false; |
[email protected] | 8c8824b | 2008-09-20 01:55:50 | [diff] [blame] | 1038 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1039 | } |
| 1040 | |
| 1041 | void MetricsService::CollectMemoryDetails() { |
| 1042 | Task* task = log_sender_factory_. |
| 1043 | NewRunnableMethod(&MetricsService::TryToStartTransmission); |
| 1044 | MetricsMemoryDetails* details = new MetricsMemoryDetails(task); |
| 1045 | details->StartFetch(); |
| 1046 | |
| 1047 | // Collect WebCore cache information to put into a histogram. |
| 1048 | for (RenderProcessHost::iterator it = RenderProcessHost::begin(); |
| 1049 | it != RenderProcessHost::end(); ++it) { |
| 1050 | it->second->Send(new ViewMsg_GetCacheResourceStats()); |
| 1051 | } |
| 1052 | } |
| 1053 | |
| 1054 | void MetricsService::PrepareInitialLog() { |
| 1055 | DCHECK(state_ == PLUGIN_LIST_ARRIVED); |
| 1056 | std::vector<WebPluginInfo> plugins; |
| 1057 | PluginService::GetInstance()->GetPlugins(false, &plugins); |
| 1058 | |
| 1059 | MetricsLog* log = new MetricsLog(client_id_, session_id_); |
| 1060 | log->RecordEnvironment(plugins, profile_dictionary_.get()); |
| 1061 | |
| 1062 | // Histograms only get written to current_log_, so setup for the write. |
| 1063 | MetricsLog* save_log = current_log_; |
| 1064 | current_log_ = log; |
| 1065 | RecordCurrentHistograms(); // Into current_log_... which is really log. |
| 1066 | current_log_ = save_log; |
| 1067 | |
| 1068 | log->CloseLog(); |
| 1069 | DCHECK(!pending_log()); |
| 1070 | pending_log_ = log; |
| 1071 | } |
| 1072 | |
| 1073 | void MetricsService::RecallUnsentLogs() { |
| 1074 | DCHECK(unsent_initial_logs_.empty()); |
| 1075 | DCHECK(unsent_ongoing_logs_.empty()); |
| 1076 | |
| 1077 | PrefService* local_state = g_browser_process->local_state(); |
| 1078 | DCHECK(local_state); |
| 1079 | |
| 1080 | ListValue* unsent_initial_logs = local_state->GetMutableList( |
| 1081 | prefs::kMetricsInitialLogs); |
| 1082 | for (ListValue::iterator it = unsent_initial_logs->begin(); |
| 1083 | it != unsent_initial_logs->end(); ++it) { |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 1084 | std::string log; |
| 1085 | (*it)->GetAsString(&log); |
| 1086 | unsent_initial_logs_.push_back(log); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | ListValue* unsent_ongoing_logs = local_state->GetMutableList( |
| 1090 | prefs::kMetricsOngoingLogs); |
| 1091 | for (ListValue::iterator it = unsent_ongoing_logs->begin(); |
| 1092 | it != unsent_ongoing_logs->end(); ++it) { |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 1093 | std::string log; |
| 1094 | (*it)->GetAsString(&log); |
| 1095 | unsent_ongoing_logs_.push_back(log); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1096 | } |
| 1097 | } |
| 1098 | |
| 1099 | void MetricsService::StoreUnsentLogs() { |
| 1100 | if (state_ < INITIAL_LOG_READY) |
| 1101 | return; // We never Recalled the prior unsent logs. |
| 1102 | |
| 1103 | PrefService* local_state = g_browser_process->local_state(); |
| 1104 | DCHECK(local_state); |
| 1105 | |
| 1106 | ListValue* unsent_initial_logs = local_state->GetMutableList( |
| 1107 | prefs::kMetricsInitialLogs); |
| 1108 | unsent_initial_logs->Clear(); |
| 1109 | size_t start = 0; |
| 1110 | if (unsent_initial_logs_.size() > kMaxInitialLogsPersisted) |
| 1111 | start = unsent_initial_logs_.size() - kMaxInitialLogsPersisted; |
| 1112 | for (size_t i = start; i < unsent_initial_logs_.size(); ++i) |
| 1113 | unsent_initial_logs->Append( |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 1114 | Value::CreateStringValue(unsent_initial_logs_[i])); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1115 | |
| 1116 | ListValue* unsent_ongoing_logs = local_state->GetMutableList( |
| 1117 | prefs::kMetricsOngoingLogs); |
| 1118 | unsent_ongoing_logs->Clear(); |
| 1119 | start = 0; |
| 1120 | if (unsent_ongoing_logs_.size() > kMaxOngoingLogsPersisted) |
| 1121 | start = unsent_ongoing_logs_.size() - kMaxOngoingLogsPersisted; |
| 1122 | for (size_t i = start; i < unsent_ongoing_logs_.size(); ++i) |
| 1123 | unsent_ongoing_logs->Append( |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 1124 | Value::CreateStringValue(unsent_ongoing_logs_[i])); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1125 | } |
| 1126 | |
| 1127 | void MetricsService::PreparePendingLogText() { |
| 1128 | DCHECK(pending_log()); |
| 1129 | if (!pending_log_text_.empty()) |
| 1130 | return; |
| 1131 | int original_size = pending_log_->GetEncodedLogSize(); |
| 1132 | pending_log_->GetEncodedLog(WriteInto(&pending_log_text_, original_size), |
| 1133 | original_size); |
| 1134 | } |
| 1135 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1136 | void MetricsService::PrepareFetchWithPendingLog() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1137 | DCHECK(pending_log()); |
| 1138 | DCHECK(!current_fetch_.get()); |
| 1139 | PreparePendingLogText(); |
| 1140 | DCHECK(!pending_log_text_.empty()); |
| 1141 | |
| 1142 | // Allow security conscious users to see all metrics logs that we send. |
| 1143 | LOG(INFO) << "METRICS LOG: " << pending_log_text_; |
| 1144 | |
| 1145 | std::string compressed_log; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1146 | if (!Bzip2Compress(pending_log_text_, &compressed_log)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1147 | NOTREACHED() << "Failed to compress log for transmission."; |
| 1148 | DiscardPendingLog(); |
| 1149 | StartLogTransmissionTimer(); // Maybe we'll do better on next log :-/. |
| 1150 | return; |
| 1151 | } |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1152 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1153 | current_fetch_.reset(new URLFetcher(GURL(kMetricsURL), URLFetcher::POST, |
| 1154 | this)); |
| 1155 | current_fetch_->set_request_context(Profile::GetDefaultRequestContext()); |
| 1156 | current_fetch_->set_upload_data(kMetricsType, compressed_log); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1157 | } |
| 1158 | |
| 1159 | void MetricsService::DiscardPendingLog() { |
| 1160 | if (pending_log_) { // Shutdown might have deleted it! |
| 1161 | delete pending_log_; |
| 1162 | pending_log_ = NULL; |
| 1163 | } |
| 1164 | pending_log_text_.clear(); |
| 1165 | } |
| 1166 | |
| 1167 | // This implementation is based on the Firefox MetricsService implementation. |
| 1168 | bool MetricsService::Bzip2Compress(const std::string& input, |
| 1169 | std::string* output) { |
| 1170 | bz_stream stream = {0}; |
| 1171 | // As long as our input is smaller than the bzip2 block size, we should get |
| 1172 | // the best compression. For example, if your input was 250k, using a block |
| 1173 | // size of 300k or 500k should result in the same compression ratio. Since |
| 1174 | // our data should be under 100k, using the minimum block size of 100k should |
| 1175 | // allocate less temporary memory, but result in the same compression ratio. |
| 1176 | int result = BZ2_bzCompressInit(&stream, |
| 1177 | 1, // 100k (min) block size |
| 1178 | 0, // quiet |
| 1179 | 0); // default "work factor" |
| 1180 | if (result != BZ_OK) { // out of memory? |
| 1181 | return false; |
| 1182 | } |
| 1183 | |
| 1184 | output->clear(); |
| 1185 | |
| 1186 | stream.next_in = const_cast<char*>(input.data()); |
| 1187 | stream.avail_in = static_cast<int>(input.size()); |
| 1188 | // NOTE: we don't need a BZ_RUN phase since our input buffer contains |
| 1189 | // the entire input |
| 1190 | do { |
| 1191 | output->resize(output->size() + 1024); |
| 1192 | stream.next_out = &((*output)[stream.total_out_lo32]); |
| 1193 | stream.avail_out = static_cast<int>(output->size()) - stream.total_out_lo32; |
| 1194 | result = BZ2_bzCompress(&stream, BZ_FINISH); |
| 1195 | } while (result == BZ_FINISH_OK); |
| 1196 | if (result != BZ_STREAM_END) // unknown failure? |
| 1197 | return false; |
| 1198 | result = BZ2_bzCompressEnd(&stream); |
| 1199 | DCHECK(result == BZ_OK); |
| 1200 | |
| 1201 | output->resize(stream.total_out_lo32); |
| 1202 | |
| 1203 | return true; |
| 1204 | } |
| 1205 | |
| 1206 | static const char* StatusToString(const URLRequestStatus& status) { |
| 1207 | switch (status.status()) { |
| 1208 | case URLRequestStatus::SUCCESS: |
| 1209 | return "SUCCESS"; |
| 1210 | |
| 1211 | case URLRequestStatus::IO_PENDING: |
| 1212 | return "IO_PENDING"; |
| 1213 | |
| 1214 | case URLRequestStatus::HANDLED_EXTERNALLY: |
| 1215 | return "HANDLED_EXTERNALLY"; |
| 1216 | |
| 1217 | case URLRequestStatus::CANCELED: |
| 1218 | return "CANCELED"; |
| 1219 | |
| 1220 | case URLRequestStatus::FAILED: |
| 1221 | return "FAILED"; |
| 1222 | |
| 1223 | default: |
| 1224 | NOTREACHED(); |
| 1225 | return "Unknown"; |
| 1226 | } |
| 1227 | } |
| 1228 | |
| 1229 | void MetricsService::OnURLFetchComplete(const URLFetcher* source, |
| 1230 | const GURL& url, |
| 1231 | const URLRequestStatus& status, |
| 1232 | int response_code, |
| 1233 | const ResponseCookies& cookies, |
| 1234 | const std::string& data) { |
| 1235 | DCHECK(timer_pending_); |
| 1236 | timer_pending_ = false; |
| 1237 | DCHECK(current_fetch_.get()); |
| 1238 | current_fetch_.reset(NULL); // We're not allowed to re-use it. |
| 1239 | |
| 1240 | // Confirm send so that we can move on. |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1241 | LOG(INFO) << "METRICS RESPONSE CODE: " << response_code << " status=" << |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1242 | StatusToString(status); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1243 | |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1244 | // Provide boolean for error recovery (allow us to ignore response_code). |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1245 | bool discard_log = false; |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1246 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1247 | if (response_code != 200 && |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1248 | pending_log_text_.length() > |
| 1249 | static_cast<size_t>(kUploadLogAvoidRetransmitSize)) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1250 | UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded", |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1251 | static_cast<int>(pending_log_text_.length())); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1252 | discard_log = true; |
| 1253 | } else if (response_code == 400) { |
| 1254 | // Bad syntax. Retransmission won't work. |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1255 | UMA_HISTOGRAM_COUNTS("UMA.Unacceptable_Log_Discarded", state_); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1256 | discard_log = true; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1257 | } |
| 1258 | |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1259 | if (response_code != 200 && !discard_log) { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1260 | LOG(INFO) << "METRICS: transmission attempt returned a failure code: " |
| 1261 | << response_code << ". Verify network connectivity"; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1262 | HandleBadResponseCode(); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1263 | } else { // Successful receipt (or we are discarding log). |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1264 | LOG(INFO) << "METRICS RESPONSE DATA: " << data; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1265 | switch (state_) { |
| 1266 | case INITIAL_LOG_READY: |
| 1267 | state_ = SEND_OLD_INITIAL_LOGS; |
| 1268 | break; |
| 1269 | |
| 1270 | case SEND_OLD_INITIAL_LOGS: |
| 1271 | DCHECK(!unsent_initial_logs_.empty()); |
| 1272 | unsent_initial_logs_.pop_back(); |
| 1273 | StoreUnsentLogs(); |
| 1274 | break; |
| 1275 | |
| 1276 | case SENDING_OLD_LOGS: |
| 1277 | DCHECK(!unsent_ongoing_logs_.empty()); |
| 1278 | unsent_ongoing_logs_.pop_back(); |
| 1279 | StoreUnsentLogs(); |
| 1280 | break; |
| 1281 | |
| 1282 | case SENDING_CURRENT_LOGS: |
| 1283 | break; |
| 1284 | |
| 1285 | default: |
| 1286 | DCHECK(false); |
| 1287 | break; |
| 1288 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1289 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1290 | DiscardPendingLog(); |
[email protected] | 29be9255 | 2008-08-07 22:49:27 | [diff] [blame] | 1291 | // Since we sent a log, make sure our in-memory state is recorded to disk. |
| 1292 | PrefService* local_state = g_browser_process->local_state(); |
| 1293 | DCHECK(local_state); |
| 1294 | if (local_state) |
| 1295 | local_state->ScheduleSavePersistentPrefs( |
| 1296 | g_browser_process->file_thread()); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1297 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 1298 | // Provide a default (free of exponetial backoff, other varances) in case |
| 1299 | // the server does not specify a value. |
| 1300 | interlog_duration_ = TimeDelta::FromSeconds(kMinSecondsPerLog); |
| 1301 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1302 | GetSettingsFromResponseData(data); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1303 | // Override server specified interlog delay if there are unsent logs to |
[email protected] | 29be9255 | 2008-08-07 22:49:27 | [diff] [blame] | 1304 | // transmit. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1305 | if (unsent_logs()) { |
| 1306 | DCHECK(state_ < SENDING_CURRENT_LOGS); |
| 1307 | interlog_duration_ = TimeDelta::FromSeconds(kUnsentLogDelay); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1308 | } |
| 1309 | } |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1310 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1311 | StartLogTransmissionTimer(); |
| 1312 | } |
| 1313 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1314 | void MetricsService::HandleBadResponseCode() { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1315 | LOG(INFO) << "Verify your metrics logs are formatted correctly. " |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1316 | "Verify server is active at " << kMetricsURL; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1317 | if (!pending_log()) { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1318 | LOG(INFO) << "METRICS: Recorder shutdown during log transmission."; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1319 | } else { |
| 1320 | // Send progressively less frequently. |
| 1321 | DCHECK(kBackoff > 1.0); |
| 1322 | interlog_duration_ = TimeDelta::FromMicroseconds( |
| 1323 | static_cast<int64>(kBackoff * interlog_duration_.InMicroseconds())); |
| 1324 | |
| 1325 | if (kMaxBackoff * TimeDelta::FromSeconds(kMinSecondsPerLog) < |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1326 | interlog_duration_) { |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1327 | interlog_duration_ = kMaxBackoff * |
| 1328 | TimeDelta::FromSeconds(kMinSecondsPerLog); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1329 | } |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1330 | |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1331 | LOG(INFO) << "METRICS: transmission retry being scheduled in " << |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1332 | interlog_duration_.InSeconds() << " seconds for " << |
| 1333 | pending_log_text_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1334 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1335 | } |
| 1336 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1337 | void MetricsService::GetSettingsFromResponseData(const std::string& data) { |
| 1338 | // We assume that the file is structured as a block opened by <response> |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1339 | // and that inside response, there is a block opened by tag <chrome_config> |
| 1340 | // other tags are ignored for now except the content of <chrome_config>. |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1341 | LOG(INFO) << "METRICS: getting settings from response data: " << data; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1342 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1343 | int data_size = static_cast<int>(data.size()); |
| 1344 | if (data_size < 0) { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1345 | LOG(INFO) << "METRICS: server response data bad size: " << data_size << |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1346 | "; aborting extraction of settings"; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1347 | return; |
| 1348 | } |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1349 | xmlDocPtr doc = xmlReadMemory(data.c_str(), data_size, "", NULL, 0); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1350 | DCHECK(doc); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1351 | // If the document is malformed, we just use the settings that were there. |
| 1352 | if (!doc) { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1353 | LOG(INFO) << "METRICS: reading xml from server response data failed"; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1354 | return; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1355 | } |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1356 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1357 | xmlNodePtr top_node = xmlDocGetRootElement(doc), chrome_config_node = NULL; |
| 1358 | // Here, we find the chrome_config node by name. |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1359 | for (xmlNodePtr p = top_node->children; p; p = p->next) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1360 | if (xmlStrEqual(p->name, BAD_CAST "chrome_config")) { |
| 1361 | chrome_config_node = p; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1362 | break; |
| 1363 | } |
| 1364 | } |
| 1365 | // If the server data is formatted wrong and there is no |
| 1366 | // config node where we expect, we just drop out. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1367 | if (chrome_config_node != NULL) |
| 1368 | GetSettingsFromChromeConfigNode(chrome_config_node); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1369 | xmlFreeDoc(doc); |
| 1370 | } |
| 1371 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1372 | void MetricsService::GetSettingsFromChromeConfigNode( |
| 1373 | xmlNodePtr chrome_config_node) { |
| 1374 | // Iterate through all children of the config node. |
| 1375 | for (xmlNodePtr current_node = chrome_config_node->children; |
| 1376 | current_node; |
| 1377 | current_node = current_node->next) { |
| 1378 | // If we find the upload tag, we appeal to another function |
| 1379 | // GetSettingsFromUploadNode to read all the data in it. |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1380 | if (xmlStrEqual(current_node->name, BAD_CAST "upload")) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1381 | GetSettingsFromUploadNode(current_node); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1382 | continue; |
| 1383 | } |
| 1384 | } |
| 1385 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1386 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1387 | void MetricsService::InheritedProperties::OverwriteWhereNeeded( |
| 1388 | xmlNodePtr node) { |
| 1389 | xmlChar* salt_value = xmlGetProp(node, BAD_CAST "salt"); |
| 1390 | if (salt_value) // If the property isn't there, xmlGetProp returns NULL. |
| 1391 | salt = atoi(reinterpret_cast<char*>(salt_value)); |
| 1392 | // If the property isn't there, we keep the value the property had before |
| 1393 | |
| 1394 | xmlChar* denominator_value = xmlGetProp(node, BAD_CAST "denominator"); |
| 1395 | if (denominator_value) |
| 1396 | denominator = atoi(reinterpret_cast<char*>(denominator_value)); |
| 1397 | } |
| 1398 | |
| 1399 | void MetricsService::GetSettingsFromUploadNode(xmlNodePtr upload_node) { |
| 1400 | InheritedProperties props; |
| 1401 | GetSettingsFromUploadNodeRecursive(upload_node, props, "", true); |
| 1402 | } |
| 1403 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1404 | void MetricsService::GetSettingsFromUploadNodeRecursive( |
| 1405 | xmlNodePtr node, |
| 1406 | InheritedProperties props, |
| 1407 | std::string path_prefix, |
| 1408 | bool uploadOn) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1409 | props.OverwriteWhereNeeded(node); |
| 1410 | |
| 1411 | // The bool uploadOn is set to true if the data represented by current |
| 1412 | // node should be uploaded. This gets inherited in the tree; the children |
| 1413 | // of a node that has already been rejected for upload get rejected for |
| 1414 | // upload. |
| 1415 | uploadOn = uploadOn && NodeProbabilityTest(node, props); |
| 1416 | |
| 1417 | // The path is a / separated list of the node names ancestral to the current |
| 1418 | // one. So, if you want to check if the current node has a certain name, |
| 1419 | // compare to name. If you want to check if it is a certan tag at a certain |
| 1420 | // place in the tree, compare to the whole path. |
| 1421 | std::string name = std::string(reinterpret_cast<const char*>(node->name)); |
| 1422 | std::string path = path_prefix + "/" + name; |
| 1423 | |
| 1424 | if (path == "/upload") { |
| 1425 | xmlChar* upload_interval_val = xmlGetProp(node, BAD_CAST "interval"); |
| 1426 | if (upload_interval_val) { |
| 1427 | interlog_duration_ = TimeDelta::FromSeconds( |
| 1428 | atoi(reinterpret_cast<char*>(upload_interval_val))); |
| 1429 | } |
| 1430 | |
| 1431 | server_permits_upload_ = uploadOn; |
| 1432 | } |
| 1433 | if (path == "/upload/logs") { |
| 1434 | xmlChar* log_event_limit_val = xmlGetProp(node, BAD_CAST "event_limit"); |
| 1435 | if (log_event_limit_val) |
| 1436 | log_event_limit_ = atoi(reinterpret_cast<char*>(log_event_limit_val)); |
| 1437 | } |
| 1438 | if (name == "histogram") { |
| 1439 | xmlChar* type_value = xmlGetProp(node, BAD_CAST "type"); |
| 1440 | if (type_value) { |
| 1441 | std::string type = (reinterpret_cast<char*>(type_value)); |
| 1442 | if (uploadOn) |
| 1443 | histograms_to_upload_.insert(type); |
| 1444 | else |
| 1445 | histograms_to_omit_.insert(type); |
| 1446 | } |
| 1447 | } |
| 1448 | if (name == "log") { |
| 1449 | xmlChar* type_value = xmlGetProp(node, BAD_CAST "type"); |
| 1450 | if (type_value) { |
| 1451 | std::string type = (reinterpret_cast<char*>(type_value)); |
| 1452 | if (uploadOn) |
| 1453 | logs_to_upload_.insert(type); |
| 1454 | else |
| 1455 | logs_to_omit_.insert(type); |
| 1456 | } |
| 1457 | } |
| 1458 | |
| 1459 | // Recursive call. If the node is a leaf i.e. if it ends in a "/>", then it |
| 1460 | // doesn't have children, so node->children is NULL, and this loop doesn't |
| 1461 | // call (that's how the recursion ends). |
| 1462 | for (xmlNodePtr child_node = node->children; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1463 | child_node; |
| 1464 | child_node = child_node->next) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1465 | GetSettingsFromUploadNodeRecursive(child_node, props, path, uploadOn); |
| 1466 | } |
| 1467 | } |
| 1468 | |
| 1469 | bool MetricsService::NodeProbabilityTest(xmlNodePtr node, |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1470 | InheritedProperties props) const { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1471 | // Default value of probability on any node is 1, but recall that |
| 1472 | // its parents can already have been rejected for upload. |
| 1473 | double probability = 1; |
| 1474 | |
| 1475 | // If a probability is specified in the node, we use it instead. |
| 1476 | xmlChar* probability_value = xmlGetProp(node, BAD_CAST "probability"); |
| 1477 | if (probability_value) |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 1478 | probability = atoi(reinterpret_cast<char*>(probability_value)); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1479 | |
| 1480 | return ProbabilityTest(probability, props.salt, props.denominator); |
| 1481 | } |
| 1482 | |
| 1483 | bool MetricsService::ProbabilityTest(double probability, |
| 1484 | int salt, |
| 1485 | int denominator) const { |
| 1486 | // Okay, first we figure out how many of the digits of the |
| 1487 | // client_id_ we need in order to make a nice pseudorandomish |
| 1488 | // number in the range [0,denominator). Too many digits is |
| 1489 | // fine. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1490 | |
| 1491 | // n is the length of the client_id_ string |
| 1492 | size_t n = client_id_.size(); |
| 1493 | |
| 1494 | // idnumber is a positive integer generated from the client_id_. |
| 1495 | // It plus salt is going to give us our pseudorandom number. |
| 1496 | int idnumber = 0; |
| 1497 | const char* client_id_c_str = client_id_.c_str(); |
| 1498 | |
| 1499 | // Here we hash the relevant digits of the client_id_ |
| 1500 | // string somehow to get a big integer idnumber (could be negative |
| 1501 | // from wraparound) |
| 1502 | int big = 1; |
[email protected] | 5ed7334 | 2009-03-18 17:39:43 | [diff] [blame] | 1503 | int last_pos = n - 1; |
| 1504 | for (size_t j = 0; j < n; ++j) { |
| 1505 | idnumber += static_cast<int>(client_id_c_str[last_pos - j]) * big; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1506 | big *= 10; |
| 1507 | } |
| 1508 | |
| 1509 | // Mod id number by denominator making sure to get a non-negative |
| 1510 | // answer. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1511 | idnumber = ((idnumber % denominator) + denominator) % denominator; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1512 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1513 | // ((idnumber + salt) % denominator) / denominator is in the range [0,1] |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1514 | // if it's less than probability we call that an affirmative coin |
| 1515 | // toss. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1516 | return static_cast<double>((idnumber + salt) % denominator) < |
| 1517 | probability * denominator; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1518 | } |
| 1519 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1520 | void MetricsService::LogWindowChange(NotificationType type, |
| 1521 | const NotificationSource& source, |
| 1522 | const NotificationDetails& details) { |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1523 | int controller_id = -1; |
| 1524 | uintptr_t window_or_tab = source.map_key(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1525 | MetricsLog::WindowEventType window_type; |
| 1526 | |
| 1527 | // Note: since we stop all logging when a single OTR session is active, it is |
| 1528 | // possible that we start getting notifications about a window that we don't |
| 1529 | // know about. |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1530 | if (window_map_.find(window_or_tab) == window_map_.end()) { |
| 1531 | controller_id = next_window_id_++; |
| 1532 | window_map_[window_or_tab] = controller_id; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1533 | } else { |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1534 | controller_id = window_map_[window_or_tab]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1535 | } |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1536 | DCHECK(controller_id != -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1537 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1538 | switch (type.value) { |
| 1539 | case NotificationType::TAB_PARENTED: |
| 1540 | case NotificationType::BROWSER_OPENED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1541 | window_type = MetricsLog::WINDOW_CREATE; |
| 1542 | break; |
| 1543 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1544 | case NotificationType::TAB_CLOSING: |
| 1545 | case NotificationType::BROWSER_CLOSED: |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1546 | window_map_.erase(window_map_.find(window_or_tab)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1547 | window_type = MetricsLog::WINDOW_DESTROY; |
| 1548 | break; |
| 1549 | |
| 1550 | default: |
| 1551 | NOTREACHED(); |
[email protected] | 68d74f0 | 2009-02-13 01:36:50 | [diff] [blame] | 1552 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1553 | } |
| 1554 | |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1555 | // TODO(brettw) we should have some kind of ID for the parent. |
| 1556 | current_log_->RecordWindowEvent(window_type, controller_id, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1557 | } |
| 1558 | |
| 1559 | void MetricsService::LogLoadComplete(NotificationType type, |
| 1560 | const NotificationSource& source, |
| 1561 | const NotificationDetails& details) { |
| 1562 | if (details == NotificationService::NoDetails()) |
| 1563 | return; |
| 1564 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1565 | // TODO(jar): There is a bug causing this to be called too many times, and |
| 1566 | // the log overflows. For now, we won't record these events. |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1567 | UMA_HISTOGRAM_COUNTS("UMA.LogLoadComplete called", 1); |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1568 | return; |
| 1569 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1570 | const Details<LoadNotificationDetails> load_details(details); |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1571 | int controller_id = window_map_[details.map_key()]; |
| 1572 | current_log_->RecordLoadEvent(controller_id, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1573 | load_details->url(), |
| 1574 | load_details->origin(), |
| 1575 | load_details->session_index(), |
| 1576 | load_details->load_time()); |
| 1577 | } |
| 1578 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1579 | void MetricsService::IncrementPrefValue(const wchar_t* path) { |
| 1580 | PrefService* pref = g_browser_process->local_state(); |
| 1581 | DCHECK(pref); |
| 1582 | int value = pref->GetInteger(path); |
| 1583 | pref->SetInteger(path, value + 1); |
| 1584 | } |
| 1585 | |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1586 | void MetricsService::IncrementLongPrefsValue(const wchar_t* path) { |
| 1587 | PrefService* pref = g_browser_process->local_state(); |
| 1588 | DCHECK(pref); |
| 1589 | int64 value = pref->GetInt64(path); |
| 1590 | pref->SetInt64(path, value+1); |
| 1591 | } |
| 1592 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1593 | void MetricsService::LogLoadStarted() { |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1594 | IncrementPrefValue(prefs::kStabilityPageLoadCount); |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1595 | IncrementLongPrefsValue(prefs::kUninstallMetricsPageLoadCount); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 1596 | // We need to save the prefs, as page load count is a critical stat, and it |
| 1597 | // might be lost due to a crash :-(. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1598 | } |
| 1599 | |
| 1600 | void MetricsService::LogRendererInSandbox(bool on_sandbox_desktop) { |
| 1601 | PrefService* prefs = g_browser_process->local_state(); |
| 1602 | DCHECK(prefs); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1603 | if (on_sandbox_desktop) |
| 1604 | IncrementPrefValue(prefs::kSecurityRendererOnSboxDesktop); |
| 1605 | else |
| 1606 | IncrementPrefValue(prefs::kSecurityRendererOnDefaultDesktop); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1607 | } |
| 1608 | |
| 1609 | void MetricsService::LogRendererCrash() { |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1610 | IncrementPrefValue(prefs::kStabilityRendererCrashCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1611 | } |
| 1612 | |
| 1613 | void MetricsService::LogRendererHang() { |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1614 | IncrementPrefValue(prefs::kStabilityRendererHangCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1615 | } |
| 1616 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1617 | void MetricsService::LogChildProcessChange( |
| 1618 | NotificationType type, |
| 1619 | const NotificationSource& source, |
| 1620 | const NotificationDetails& details) { |
| 1621 | const std::wstring& child_name = |
| 1622 | Details<ChildProcessInfo>(details)->name(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1623 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1624 | if (child_process_stats_buffer_.find(child_name) == |
| 1625 | child_process_stats_buffer_.end()) { |
| 1626 | child_process_stats_buffer_[child_name] = ChildProcessStats(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1627 | } |
| 1628 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1629 | ChildProcessStats& stats = child_process_stats_buffer_[child_name]; |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1630 | switch (type.value) { |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1631 | case NotificationType::CHILD_PROCESS_HOST_CONNECTED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1632 | stats.process_launches++; |
| 1633 | break; |
| 1634 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1635 | case NotificationType::CHILD_INSTANCE_CREATED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1636 | stats.instances++; |
| 1637 | break; |
| 1638 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1639 | case NotificationType::CHILD_PROCESS_CRASHED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1640 | stats.process_crashes++; |
| 1641 | break; |
| 1642 | |
| 1643 | default: |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1644 | NOTREACHED() << "Unexpected notification type " << type.value; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1645 | return; |
| 1646 | } |
| 1647 | } |
| 1648 | |
| 1649 | // Recursively counts the number of bookmarks and folders in node. |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 1650 | static void CountBookmarks(BookmarkNode* node, int* bookmarks, int* folders) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1651 | if (node->GetType() == history::StarredEntry::URL) |
| 1652 | (*bookmarks)++; |
| 1653 | else |
| 1654 | (*folders)++; |
| 1655 | for (int i = 0; i < node->GetChildCount(); ++i) |
| 1656 | CountBookmarks(node->GetChild(i), bookmarks, folders); |
| 1657 | } |
| 1658 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 1659 | void MetricsService::LogBookmarks(BookmarkNode* node, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1660 | const wchar_t* num_bookmarks_key, |
| 1661 | const wchar_t* num_folders_key) { |
| 1662 | DCHECK(node); |
| 1663 | int num_bookmarks = 0; |
| 1664 | int num_folders = 0; |
| 1665 | CountBookmarks(node, &num_bookmarks, &num_folders); |
| 1666 | num_folders--; // Don't include the root folder in the count. |
| 1667 | |
| 1668 | PrefService* pref = g_browser_process->local_state(); |
| 1669 | DCHECK(pref); |
| 1670 | pref->SetInteger(num_bookmarks_key, num_bookmarks); |
| 1671 | pref->SetInteger(num_folders_key, num_folders); |
| 1672 | } |
| 1673 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 1674 | void MetricsService::LogBookmarks(BookmarkModel* model) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1675 | DCHECK(model); |
| 1676 | LogBookmarks(model->GetBookmarkBarNode(), |
| 1677 | prefs::kNumBookmarksOnBookmarkBar, |
| 1678 | prefs::kNumFoldersOnBookmarkBar); |
| 1679 | LogBookmarks(model->other_node(), |
| 1680 | prefs::kNumBookmarksInOtherBookmarkFolder, |
| 1681 | prefs::kNumFoldersInOtherBookmarkFolder); |
| 1682 | ScheduleNextStateSave(); |
| 1683 | } |
| 1684 | |
| 1685 | void MetricsService::LogKeywords(const TemplateURLModel* url_model) { |
| 1686 | DCHECK(url_model); |
| 1687 | |
| 1688 | PrefService* pref = g_browser_process->local_state(); |
| 1689 | DCHECK(pref); |
| 1690 | pref->SetInteger(prefs::kNumKeywords, |
| 1691 | static_cast<int>(url_model->GetTemplateURLs().size())); |
| 1692 | ScheduleNextStateSave(); |
| 1693 | } |
| 1694 | |
| 1695 | void MetricsService::RecordPluginChanges(PrefService* pref) { |
| 1696 | ListValue* plugins = pref->GetMutableList(prefs::kStabilityPluginStats); |
| 1697 | DCHECK(plugins); |
| 1698 | |
| 1699 | for (ListValue::iterator value_iter = plugins->begin(); |
| 1700 | value_iter != plugins->end(); ++value_iter) { |
| 1701 | if (!(*value_iter)->IsType(Value::TYPE_DICTIONARY)) { |
| 1702 | NOTREACHED(); |
| 1703 | continue; |
| 1704 | } |
| 1705 | |
| 1706 | DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*value_iter); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1707 | std::wstring plugin_name; |
| 1708 | plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name); |
[email protected] | 6470ee8f | 2009-03-03 20:46:40 | [diff] [blame] | 1709 | if (plugin_name.empty()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1710 | NOTREACHED(); |
| 1711 | continue; |
| 1712 | } |
| 1713 | |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1714 | if (child_process_stats_buffer_.find(plugin_name) == |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1715 | child_process_stats_buffer_.end()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1716 | continue; |
| 1717 | |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1718 | ChildProcessStats stats = child_process_stats_buffer_[plugin_name]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1719 | if (stats.process_launches) { |
| 1720 | int launches = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1721 | plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1722 | launches += stats.process_launches; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1723 | plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, launches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1724 | } |
| 1725 | if (stats.process_crashes) { |
| 1726 | int crashes = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1727 | plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1728 | crashes += stats.process_crashes; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1729 | plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1730 | } |
| 1731 | if (stats.instances) { |
| 1732 | int instances = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1733 | plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1734 | instances += stats.instances; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1735 | plugin_dict->SetInteger(prefs::kStabilityPluginInstances, instances); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1736 | } |
| 1737 | |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1738 | child_process_stats_buffer_.erase(plugin_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1739 | } |
| 1740 | |
| 1741 | // Now go through and add dictionaries for plugins that didn't already have |
| 1742 | // reports in Local State. |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1743 | for (std::map<std::wstring, ChildProcessStats>::iterator cache_iter = |
| 1744 | child_process_stats_buffer_.begin(); |
| 1745 | cache_iter != child_process_stats_buffer_.end(); ++cache_iter) { |
| 1746 | std::wstring plugin_name = cache_iter->first; |
| 1747 | ChildProcessStats stats = cache_iter->second; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1748 | DictionaryValue* plugin_dict = new DictionaryValue; |
| 1749 | |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1750 | plugin_dict->SetString(prefs::kStabilityPluginName, plugin_name); |
| 1751 | plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1752 | stats.process_launches); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1753 | plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1754 | stats.process_crashes); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1755 | plugin_dict->SetInteger(prefs::kStabilityPluginInstances, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1756 | stats.instances); |
| 1757 | plugins->Append(plugin_dict); |
| 1758 | } |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1759 | child_process_stats_buffer_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1760 | } |
| 1761 | |
| 1762 | bool MetricsService::CanLogNotification(NotificationType type, |
| 1763 | const NotificationSource& source, |
| 1764 | const NotificationDetails& details) { |
| 1765 | // We simply don't log anything to UMA if there is a single off the record |
| 1766 | // session visible. The problem is that we always notify using the orginal |
| 1767 | // profile in order to simplify notification processing. |
| 1768 | return !BrowserList::IsOffTheRecordSessionActive(); |
| 1769 | } |
| 1770 | |
| 1771 | void MetricsService::RecordBooleanPrefValue(const wchar_t* path, bool value) { |
| 1772 | DCHECK(IsSingleThreaded()); |
| 1773 | |
| 1774 | PrefService* pref = g_browser_process->local_state(); |
| 1775 | DCHECK(pref); |
| 1776 | |
| 1777 | pref->SetBoolean(path, value); |
| 1778 | RecordCurrentState(pref); |
| 1779 | } |
| 1780 | |
| 1781 | void MetricsService::RecordCurrentState(PrefService* pref) { |
[email protected] | 0bb1a62 | 2009-03-04 03:22:32 | [diff] [blame] | 1782 | pref->SetInt64(prefs::kStabilityLastTimestampSec, Time::Now().ToTimeT()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1783 | |
| 1784 | RecordPluginChanges(pref); |
| 1785 | } |
| 1786 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 1787 | void MetricsService::CollectRendererHistograms() { |
| 1788 | for (RenderProcessHost::iterator it = RenderProcessHost::begin(); |
| 1789 | it != RenderProcessHost::end(); ++it) { |
| 1790 | it->second->Send(new ViewMsg_GetRendererHistograms()); |
| 1791 | } |
| 1792 | } |
| 1793 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1794 | void MetricsService::RecordCurrentHistograms() { |
| 1795 | DCHECK(current_log_); |
| 1796 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 1797 | CollectRendererHistograms(); |
| 1798 | |
| 1799 | // TODO(raman): Delay the metrics collection activities until we get all the |
| 1800 | // updates from the renderers, or we time out (1 second? 3 seconds?). |
| 1801 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1802 | StatisticsRecorder::Histograms histograms; |
| 1803 | StatisticsRecorder::GetHistograms(&histograms); |
| 1804 | for (StatisticsRecorder::Histograms::iterator it = histograms.begin(); |
| 1805 | histograms.end() != it; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1806 | ++it) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1807 | if ((*it)->flags() & kUmaTargetedHistogramFlag) |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 1808 | // TODO(petersont): Only record historgrams if they are not precluded by |
| 1809 | // the UMA response data. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1810 | // Bug http://code.google.com/p/chromium/issues/detail?id=2739. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1811 | RecordHistogram(**it); |
| 1812 | } |
| 1813 | } |
| 1814 | |
| 1815 | void MetricsService::RecordHistogram(const Histogram& histogram) { |
| 1816 | // Get up-to-date snapshot of sample stats. |
| 1817 | Histogram::SampleSet snapshot; |
| 1818 | histogram.SnapshotSample(&snapshot); |
| 1819 | |
| 1820 | const std::string& histogram_name = histogram.histogram_name(); |
| 1821 | |
| 1822 | // Find the already sent stats, or create an empty set. |
| 1823 | LoggedSampleMap::iterator it = logged_samples_.find(histogram_name); |
| 1824 | Histogram::SampleSet* already_logged; |
| 1825 | if (logged_samples_.end() == it) { |
| 1826 | // Add new entry |
| 1827 | already_logged = &logged_samples_[histogram.histogram_name()]; |
| 1828 | already_logged->Resize(histogram); // Complete initialization. |
| 1829 | } else { |
| 1830 | already_logged = &(it->second); |
| 1831 | // Deduct any stats we've already logged from our snapshot. |
| 1832 | snapshot.Subtract(*already_logged); |
| 1833 | } |
| 1834 | |
| 1835 | // snapshot now contains only a delta to what we've already_logged. |
| 1836 | |
| 1837 | if (snapshot.TotalCount() > 0) { |
| 1838 | current_log_->RecordHistogramDelta(histogram, snapshot); |
| 1839 | // Add new data into our running total. |
| 1840 | already_logged->Add(snapshot); |
| 1841 | } |
| 1842 | } |
| 1843 | |
| 1844 | void MetricsService::AddProfileMetric(Profile* profile, |
| 1845 | const std::wstring& key, |
| 1846 | int value) { |
| 1847 | // Restriction of types is needed for writing values. See |
| 1848 | // MetricsLog::WriteProfileMetrics. |
| 1849 | DCHECK(profile && !key.empty()); |
| 1850 | PrefService* prefs = g_browser_process->local_state(); |
| 1851 | DCHECK(prefs); |
| 1852 | |
| 1853 | // Key is stored in prefs, which interpret '.'s as paths. As such, key |
| 1854 | // shouldn't have any '.'s in it. |
| 1855 | DCHECK(key.find(L'.') == std::wstring::npos); |
| 1856 | // The id is most likely an email address. We shouldn't send it to the server. |
| 1857 | const std::wstring id_hash = |
| 1858 | UTF8ToWide(MetricsLog::CreateBase64Hash(WideToUTF8(profile->GetID()))); |
| 1859 | DCHECK(id_hash.find('.') == std::string::npos); |
| 1860 | |
| 1861 | DictionaryValue* prof_prefs = prefs->GetMutableDictionary( |
| 1862 | prefs::kProfileMetrics); |
| 1863 | DCHECK(prof_prefs); |
| 1864 | const std::wstring pref_key = std::wstring(prefs::kProfilePrefix) + id_hash + |
| 1865 | L"." + key; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame] | 1866 | prof_prefs->SetInteger(pref_key.c_str(), value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1867 | } |
| 1868 | |
| 1869 | static bool IsSingleThreaded() { |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1870 | static PlatformThreadId thread_id = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1871 | if (!thread_id) |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1872 | thread_id = PlatformThread::CurrentId(); |
| 1873 | return PlatformThread::CurrentId() == thread_id; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1874 | } |