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""); |
| 306 | local_state->RegisterStringPref(prefs::kMetricsClientIDTimestamp, L"0"); |
| 307 | local_state->RegisterStringPref(prefs::kStabilityLaunchTimeSec, L"0"); |
| 308 | local_state->RegisterStringPref(prefs::kStabilityLastTimestampSec, L"0"); |
| 309 | local_state->RegisterStringPref(prefs::kStabilityUptimeSec, L"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); |
| 339 | } |
| 340 | |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 341 | // static |
| 342 | void MetricsService::DiscardOldStabilityStats(PrefService* local_state) { |
| 343 | local_state->SetBoolean(prefs::kStabilityExitedCleanly, true); |
| 344 | |
| 345 | local_state->SetInteger(prefs::kStabilityIncompleteSessionEndCount, 0); |
| 346 | local_state->SetInteger(prefs::kStabilityBreakpadRegistrationSuccess, 0); |
| 347 | local_state->SetInteger(prefs::kStabilityBreakpadRegistrationFail, 0); |
| 348 | local_state->SetInteger(prefs::kStabilityDebuggerPresent, 0); |
| 349 | local_state->SetInteger(prefs::kStabilityDebuggerNotPresent, 0); |
| 350 | |
| 351 | local_state->SetInteger(prefs::kStabilityLaunchCount, 0); |
| 352 | local_state->SetInteger(prefs::kStabilityCrashCount, 0); |
| 353 | |
| 354 | local_state->SetInteger(prefs::kStabilityPageLoadCount, 0); |
| 355 | local_state->SetInteger(prefs::kStabilityRendererCrashCount, 0); |
| 356 | local_state->SetInteger(prefs::kStabilityRendererHangCount, 0); |
| 357 | |
| 358 | local_state->SetInteger(prefs::kSecurityRendererOnSboxDesktop, 0); |
| 359 | local_state->SetInteger(prefs::kSecurityRendererOnDefaultDesktop, 0); |
| 360 | |
| 361 | local_state->SetString(prefs::kStabilityUptimeSec, L"0"); |
| 362 | |
| 363 | local_state->ClearPref(prefs::kStabilityPluginStats); |
| 364 | } |
| 365 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 366 | MetricsService::MetricsService() |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 367 | : recording_active_(false), |
| 368 | reporting_active_(false), |
| 369 | user_permits_upload_(false), |
| 370 | server_permits_upload_(true), |
| 371 | state_(INITIALIZED), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | pending_log_(NULL), |
| 373 | pending_log_text_(""), |
| 374 | current_fetch_(NULL), |
| 375 | current_log_(NULL), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 376 | idle_since_last_transmission_(false), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | next_window_id_(0), |
[email protected] | 40bcc30 | 2009-03-02 20:50:39 | [diff] [blame] | 378 | ALLOW_THIS_IN_INITIALIZER_LIST(log_sender_factory_(this)), |
| 379 | ALLOW_THIS_IN_INITIALIZER_LIST(state_saver_factory_(this)), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 380 | logged_samples_(), |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 381 | interlog_duration_(TimeDelta::FromSeconds(kInitialInterlogDuration)), |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 382 | log_event_limit_(kInitialEventLimit), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 383 | timer_pending_(false) { |
| 384 | DCHECK(IsSingleThreaded()); |
| 385 | InitializeMetricsState(); |
| 386 | } |
| 387 | |
| 388 | MetricsService::~MetricsService() { |
| 389 | SetRecording(false); |
[email protected] | d8bc79bf | 2009-01-28 01:17:58 | [diff] [blame] | 390 | if (pending_log_) { |
| 391 | delete pending_log_; |
| 392 | pending_log_ = NULL; |
| 393 | } |
| 394 | if (current_log_) { |
| 395 | delete current_log_; |
| 396 | current_log_ = NULL; |
| 397 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 398 | } |
| 399 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 400 | void MetricsService::SetUserPermitsUpload(bool enabled) { |
| 401 | HandleIdleSinceLastTransmission(false); |
| 402 | user_permits_upload_ = enabled; |
| 403 | } |
| 404 | |
| 405 | void MetricsService::Start() { |
| 406 | SetRecording(true); |
| 407 | SetReporting(true); |
| 408 | } |
| 409 | |
| 410 | void MetricsService::StartRecordingOnly() { |
| 411 | SetRecording(true); |
| 412 | SetReporting(false); |
| 413 | } |
| 414 | |
| 415 | void MetricsService::Stop() { |
| 416 | SetReporting(false); |
| 417 | SetRecording(false); |
| 418 | } |
| 419 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 420 | void MetricsService::SetRecording(bool enabled) { |
| 421 | DCHECK(IsSingleThreaded()); |
| 422 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 423 | if (enabled == recording_active_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 424 | return; |
| 425 | |
| 426 | if (enabled) { |
| 427 | StartRecording(); |
| 428 | ListenerRegistration(true); |
| 429 | } else { |
| 430 | // Turn off all observers. |
| 431 | ListenerRegistration(false); |
| 432 | PushPendingLogsToUnsentLists(); |
| 433 | DCHECK(!pending_log()); |
| 434 | if (state_ > INITIAL_LOG_READY && unsent_logs()) |
| 435 | state_ = SEND_OLD_INITIAL_LOGS; |
| 436 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 437 | recording_active_ = enabled; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 438 | } |
| 439 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 440 | bool MetricsService::recording_active() const { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 441 | DCHECK(IsSingleThreaded()); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 442 | return recording_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 443 | } |
| 444 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 445 | void MetricsService::SetReporting(bool enable) { |
| 446 | if (reporting_active_ != enable) { |
| 447 | reporting_active_ = enable; |
| 448 | if (reporting_active_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 449 | StartLogTransmissionTimer(); |
| 450 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | bool MetricsService::reporting_active() const { |
| 454 | DCHECK(IsSingleThreaded()); |
| 455 | return reporting_active_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 456 | } |
| 457 | |
| 458 | void MetricsService::Observe(NotificationType type, |
| 459 | const NotificationSource& source, |
| 460 | const NotificationDetails& details) { |
| 461 | DCHECK(current_log_); |
| 462 | DCHECK(IsSingleThreaded()); |
| 463 | |
| 464 | if (!CanLogNotification(type, source, details)) |
| 465 | return; |
| 466 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 467 | switch (type.value) { |
| 468 | case NotificationType::USER_ACTION: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 469 | current_log_->RecordUserAction(*Details<const wchar_t*>(details).ptr()); |
| 470 | break; |
| 471 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 472 | case NotificationType::BROWSER_OPENED: |
| 473 | case NotificationType::BROWSER_CLOSED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 474 | LogWindowChange(type, source, details); |
| 475 | break; |
| 476 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 477 | case NotificationType::TAB_PARENTED: |
| 478 | case NotificationType::TAB_CLOSING: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 479 | LogWindowChange(type, source, details); |
| 480 | break; |
| 481 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 482 | case NotificationType::LOAD_STOP: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 483 | LogLoadComplete(type, source, details); |
| 484 | break; |
| 485 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 486 | case NotificationType::LOAD_START: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 487 | LogLoadStarted(); |
| 488 | break; |
| 489 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 490 | case NotificationType::RENDERER_PROCESS_TERMINATED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 491 | if (!*Details<bool>(details).ptr()) |
| 492 | LogRendererCrash(); |
| 493 | break; |
| 494 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 495 | case NotificationType::RENDERER_PROCESS_HANG: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 496 | LogRendererHang(); |
| 497 | break; |
| 498 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 499 | case NotificationType::RENDERER_PROCESS_IN_SBOX: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 500 | LogRendererInSandbox(*Details<bool>(details).ptr()); |
| 501 | break; |
| 502 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 503 | case NotificationType::CHILD_PROCESS_HOST_CONNECTED: |
| 504 | case NotificationType::CHILD_PROCESS_CRASHED: |
| 505 | case NotificationType::CHILD_INSTANCE_CREATED: |
| 506 | LogChildProcessChange(type, source, details); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 507 | break; |
| 508 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 509 | case NotificationType::TEMPLATE_URL_MODEL_LOADED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 510 | LogKeywords(Source<TemplateURLModel>(source).ptr()); |
| 511 | break; |
| 512 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 513 | case NotificationType::OMNIBOX_OPENED_URL: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 514 | current_log_->RecordOmniboxOpenedURL( |
| 515 | *Details<AutocompleteLog>(details).ptr()); |
| 516 | break; |
| 517 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 518 | case NotificationType::BOOKMARK_MODEL_LOADED: |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 519 | LogBookmarks(Source<Profile>(source)->GetBookmarkModel()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 520 | break; |
| 521 | |
| 522 | default: |
| 523 | NOTREACHED(); |
| 524 | break; |
| 525 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 526 | |
| 527 | HandleIdleSinceLastTransmission(false); |
| 528 | |
| 529 | if (current_log_) |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 530 | DLOG(INFO) << "METRICS: NUMBER OF EVENTS = " << current_log_->num_events(); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | void MetricsService::HandleIdleSinceLastTransmission(bool in_idle) { |
| 534 | // If there wasn't a lot of action, maybe the computer was asleep, in which |
| 535 | // case, the log transmissions should have stopped. Here we start them up |
| 536 | // again. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 537 | if (!in_idle && idle_since_last_transmission_) |
| 538 | StartLogTransmissionTimer(); |
| 539 | idle_since_last_transmission_ = in_idle; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 540 | } |
| 541 | |
| 542 | void MetricsService::RecordCleanShutdown() { |
| 543 | RecordBooleanPrefValue(prefs::kStabilityExitedCleanly, true); |
| 544 | } |
| 545 | |
| 546 | void MetricsService::RecordStartOfSessionEnd() { |
| 547 | RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, false); |
| 548 | } |
| 549 | |
| 550 | void MetricsService::RecordCompletedSessionEnd() { |
| 551 | RecordBooleanPrefValue(prefs::kStabilitySessionEndCompleted, true); |
| 552 | } |
| 553 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 554 | void MetricsService:: RecordBreakpadRegistration(bool success) { |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 555 | if (!success) |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 556 | IncrementPrefValue(prefs::kStabilityBreakpadRegistrationFail); |
| 557 | else |
| 558 | IncrementPrefValue(prefs::kStabilityBreakpadRegistrationSuccess); |
| 559 | } |
| 560 | |
| 561 | void MetricsService::RecordBreakpadHasDebugger(bool has_debugger) { |
| 562 | if (!has_debugger) |
| 563 | IncrementPrefValue(prefs::kStabilityDebuggerNotPresent); |
| 564 | else |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 565 | IncrementPrefValue(prefs::kStabilityDebuggerPresent); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 566 | } |
| 567 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 568 | //------------------------------------------------------------------------------ |
| 569 | // private methods |
| 570 | //------------------------------------------------------------------------------ |
| 571 | |
| 572 | |
| 573 | //------------------------------------------------------------------------------ |
| 574 | // Initialization methods |
| 575 | |
| 576 | void MetricsService::InitializeMetricsState() { |
| 577 | PrefService* pref = g_browser_process->local_state(); |
| 578 | DCHECK(pref); |
| 579 | |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 580 | if (WideToUTF8(pref->GetString(prefs::kStabilityStatsVersion)) != |
| 581 | MetricsLog::GetVersionString()) { |
| 582 | // This is a new version, so we don't want to confuse the stats about the |
| 583 | // old version with info that we upload. |
| 584 | DiscardOldStabilityStats(pref); |
| 585 | pref->SetString(prefs::kStabilityStatsVersion, |
| 586 | UTF8ToWide(MetricsLog::GetVersionString())); |
| 587 | } |
| 588 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 589 | client_id_ = WideToUTF8(pref->GetString(prefs::kMetricsClientID)); |
| 590 | if (client_id_.empty()) { |
| 591 | client_id_ = GenerateClientID(); |
| 592 | pref->SetString(prefs::kMetricsClientID, UTF8ToWide(client_id_)); |
| 593 | |
| 594 | // Might as well make a note of how long this ID has existed |
| 595 | pref->SetString(prefs::kMetricsClientIDTimestamp, |
| 596 | Int64ToWString(Time::Now().ToTimeT())); |
| 597 | } |
| 598 | |
| 599 | // Update session ID |
| 600 | session_id_ = pref->GetInteger(prefs::kMetricsSessionID); |
| 601 | ++session_id_; |
| 602 | pref->SetInteger(prefs::kMetricsSessionID, session_id_); |
| 603 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 604 | // Stability bookkeeping |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 605 | IncrementPrefValue(prefs::kStabilityLaunchCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 606 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 607 | if (!pref->GetBoolean(prefs::kStabilityExitedCleanly)) { |
| 608 | IncrementPrefValue(prefs::kStabilityCrashCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 609 | } |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 610 | |
| 611 | // This will be set to 'true' if we exit cleanly. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 612 | pref->SetBoolean(prefs::kStabilityExitedCleanly, false); |
| 613 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 614 | if (!pref->GetBoolean(prefs::kStabilitySessionEndCompleted)) { |
| 615 | IncrementPrefValue(prefs::kStabilityIncompleteSessionEndCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 616 | } |
| 617 | // This is marked false when we get a WM_ENDSESSION. |
| 618 | pref->SetBoolean(prefs::kStabilitySessionEndCompleted, true); |
| 619 | |
[email protected] | 41fb1d7 | 2009-02-28 01:01:50 | [diff] [blame] | 620 | int64 last_start_time = StringToInt64( |
| 621 | WideToUTF16Hack(pref->GetString(prefs::kStabilityLaunchTimeSec))); |
| 622 | int64 last_end_time = StringToInt64( |
| 623 | WideToUTF16Hack(pref->GetString(prefs::kStabilityLastTimestampSec))); |
| 624 | int64 uptime = StringToInt64( |
| 625 | WideToUTF16Hack(pref->GetString(prefs::kStabilityUptimeSec))); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 626 | |
| 627 | if (last_start_time && last_end_time) { |
| 628 | // TODO(JAR): Exclude sleep time. ... which must be gathered in UI loop. |
| 629 | uptime += last_end_time - last_start_time; |
| 630 | pref->SetString(prefs::kStabilityUptimeSec, Int64ToWString(uptime)); |
| 631 | } |
| 632 | pref->SetString(prefs::kStabilityLaunchTimeSec, |
| 633 | Int64ToWString(Time::Now().ToTimeT())); |
| 634 | |
| 635 | // Save profile metrics. |
| 636 | PrefService* prefs = g_browser_process->local_state(); |
| 637 | if (prefs) { |
| 638 | // Remove the current dictionary and store it for use when sending data to |
| 639 | // server. By removing the value we prune potentially dead profiles |
| 640 | // (and keys). All valid values are added back once services startup. |
| 641 | const DictionaryValue* profile_dictionary = |
| 642 | prefs->GetDictionary(prefs::kProfileMetrics); |
| 643 | if (profile_dictionary) { |
| 644 | // Do a deep copy of profile_dictionary since ClearPref will delete it. |
| 645 | profile_dictionary_.reset(static_cast<DictionaryValue*>( |
| 646 | profile_dictionary->DeepCopy())); |
| 647 | prefs->ClearPref(prefs::kProfileMetrics); |
| 648 | } |
| 649 | } |
| 650 | |
| 651 | // Kick off the process of saving the state (so the uptime numbers keep |
| 652 | // getting updated) every n minutes. |
| 653 | ScheduleNextStateSave(); |
| 654 | } |
| 655 | |
| 656 | void MetricsService::OnGetPluginListTaskComplete() { |
| 657 | DCHECK(state_ == PLUGIN_LIST_REQUESTED); |
| 658 | if (state_ == PLUGIN_LIST_REQUESTED) |
| 659 | state_ = PLUGIN_LIST_ARRIVED; |
| 660 | } |
| 661 | |
| 662 | std::string MetricsService::GenerateClientID() { |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 663 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 664 | const int kGUIDSize = 39; |
| 665 | |
| 666 | GUID guid; |
| 667 | HRESULT guid_result = CoCreateGuid(&guid); |
| 668 | DCHECK(SUCCEEDED(guid_result)); |
| 669 | |
| 670 | std::wstring guid_string; |
| 671 | int result = StringFromGUID2(guid, |
| 672 | WriteInto(&guid_string, kGUIDSize), kGUIDSize); |
| 673 | DCHECK(result == kGUIDSize); |
| 674 | |
| 675 | return WideToUTF8(guid_string.substr(1, guid_string.length() - 2)); |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 676 | #else |
| 677 | // TODO(port): Implement for Mac and linux. |
[email protected] | f5d0e15 | 2009-02-19 21:54:37 | [diff] [blame] | 678 | // Rather than actually implementing a random source, might this be a good |
| 679 | // time to implement http://code.google.com/p/chromium/issues/detail?id=2278 |
| 680 | // ? I think so! |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 681 | NOTIMPLEMENTED(); |
| 682 | return std::string(); |
| 683 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | |
| 687 | //------------------------------------------------------------------------------ |
| 688 | // State save methods |
| 689 | |
| 690 | void MetricsService::ScheduleNextStateSave() { |
| 691 | state_saver_factory_.RevokeAll(); |
| 692 | |
| 693 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 694 | state_saver_factory_.NewRunnableMethod(&MetricsService::SaveLocalState), |
| 695 | kSaveStateInterval * 1000); |
| 696 | } |
| 697 | |
| 698 | void MetricsService::SaveLocalState() { |
| 699 | PrefService* pref = g_browser_process->local_state(); |
| 700 | if (!pref) { |
| 701 | NOTREACHED(); |
| 702 | return; |
| 703 | } |
| 704 | |
| 705 | RecordCurrentState(pref); |
| 706 | pref->ScheduleSavePersistentPrefs(g_browser_process->file_thread()); |
| 707 | |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 708 | // TODO(jar): Does this run down the batteries???? |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 709 | ScheduleNextStateSave(); |
| 710 | } |
| 711 | |
| 712 | |
| 713 | //------------------------------------------------------------------------------ |
| 714 | // Recording control methods |
| 715 | |
| 716 | void MetricsService::StartRecording() { |
| 717 | if (current_log_) |
| 718 | return; |
| 719 | |
| 720 | current_log_ = new MetricsLog(client_id_, session_id_); |
| 721 | if (state_ == INITIALIZED) { |
| 722 | // We only need to schedule that run once. |
| 723 | state_ = PLUGIN_LIST_REQUESTED; |
| 724 | |
| 725 | // Make sure the plugin list is loaded before the inital log is sent, so |
| 726 | // that the main thread isn't blocked generating the list. |
| 727 | g_browser_process->file_thread()->message_loop()->PostDelayedTask(FROM_HERE, |
| 728 | new GetPluginListTask(MessageLoop::current()), |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 729 | kInitialInterlogDuration * 1000 / 2); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 730 | } |
| 731 | } |
| 732 | |
| 733 | void MetricsService::StopRecording(MetricsLog** log) { |
| 734 | if (!current_log_) |
| 735 | return; |
| 736 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 737 | // TODO(jar): Integrate bounds on log recording more consistently, so that we |
| 738 | // can stop recording logs that are too big much sooner. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 739 | if (current_log_->num_events() > log_event_limit_) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 740 | UMA_HISTOGRAM_COUNTS("UMA.Discarded Log Events", |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 741 | current_log_->num_events()); |
| 742 | current_log_->CloseLog(); |
| 743 | delete current_log_; |
[email protected] | 29463878 | 2008-09-24 00:22:41 | [diff] [blame] | 744 | current_log_ = NULL; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 745 | StartRecording(); // Start trivial log to hold our histograms. |
| 746 | } |
| 747 | |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 748 | // Put incremental data (histogram deltas, and realtime stats deltas) at the |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 749 | // end of all log transmissions (initial log handles this separately). |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 750 | // Don't bother if we're going to discard current_log_. |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 751 | if (log) { |
[email protected] | c96d5309 | 2009-02-24 01:25:06 | [diff] [blame] | 752 | current_log_->RecordIncrementalStabilityElements(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 753 | RecordCurrentHistograms(); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 754 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 755 | |
| 756 | current_log_->CloseLog(); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 757 | if (log) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 758 | *log = current_log_; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 759 | else |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 760 | delete current_log_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 761 | current_log_ = NULL; |
| 762 | } |
| 763 | |
| 764 | void MetricsService::ListenerRegistration(bool start_listening) { |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 765 | AddOrRemoveObserver(this, NotificationType::BROWSER_OPENED, start_listening); |
| 766 | AddOrRemoveObserver(this, NotificationType::BROWSER_CLOSED, start_listening); |
| 767 | AddOrRemoveObserver(this, NotificationType::USER_ACTION, start_listening); |
| 768 | AddOrRemoveObserver(this, NotificationType::TAB_PARENTED, start_listening); |
| 769 | AddOrRemoveObserver(this, NotificationType::TAB_CLOSING, start_listening); |
| 770 | AddOrRemoveObserver(this, NotificationType::LOAD_START, start_listening); |
| 771 | AddOrRemoveObserver(this, NotificationType::LOAD_STOP, start_listening); |
| 772 | AddOrRemoveObserver(this, NotificationType::RENDERER_PROCESS_IN_SBOX, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 773 | start_listening); |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 774 | AddOrRemoveObserver(this, NotificationType::RENDERER_PROCESS_TERMINATED, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 775 | start_listening); |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 776 | AddOrRemoveObserver(this, NotificationType::RENDERER_PROCESS_HANG, |
| 777 | start_listening); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 778 | AddOrRemoveObserver(this, NotificationType::CHILD_PROCESS_HOST_CONNECTED, |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 779 | start_listening); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 780 | AddOrRemoveObserver(this, NotificationType::CHILD_INSTANCE_CREATED, |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 781 | start_listening); |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 782 | AddOrRemoveObserver(this, NotificationType::CHILD_PROCESS_CRASHED, |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 783 | start_listening); |
| 784 | AddOrRemoveObserver(this, NotificationType::TEMPLATE_URL_MODEL_LOADED, |
| 785 | start_listening); |
| 786 | AddOrRemoveObserver(this, NotificationType::OMNIBOX_OPENED_URL, |
| 787 | start_listening); |
| 788 | AddOrRemoveObserver(this, NotificationType::BOOKMARK_MODEL_LOADED, |
| 789 | start_listening); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 790 | } |
| 791 | |
| 792 | // static |
| 793 | void MetricsService::AddOrRemoveObserver(NotificationObserver* observer, |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 794 | NotificationType type, |
| 795 | bool is_add) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 796 | NotificationService* service = NotificationService::current(); |
| 797 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 798 | if (is_add) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 799 | service->AddObserver(observer, type, NotificationService::AllSources()); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 800 | else |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 801 | service->RemoveObserver(observer, type, NotificationService::AllSources()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 802 | } |
| 803 | |
| 804 | void MetricsService::PushPendingLogsToUnsentLists() { |
| 805 | if (state_ < INITIAL_LOG_READY) |
[email protected] | 28ab7f9 | 2009-01-06 21:39:04 | [diff] [blame] | 806 | 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] | 807 | |
| 808 | if (pending_log()) { |
| 809 | PreparePendingLogText(); |
| 810 | if (state_ == INITIAL_LOG_READY) { |
| 811 | // We may race here, and send second copy of initial log later. |
| 812 | unsent_initial_logs_.push_back(pending_log_text_); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 813 | state_ = SEND_OLD_INITIAL_LOGS; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 814 | } else { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 815 | // TODO(jar): Verify correctness in other states, including sending unsent |
[email protected] | 541f7792 | 2009-02-23 21:14:38 | [diff] [blame] | 816 | // initial logs. |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 817 | PushPendingLogTextToUnsentOngoingLogs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 818 | } |
| 819 | DiscardPendingLog(); |
| 820 | } |
| 821 | DCHECK(!pending_log()); |
| 822 | StopRecording(&pending_log_); |
| 823 | PreparePendingLogText(); |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 824 | PushPendingLogTextToUnsentOngoingLogs(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 825 | DiscardPendingLog(); |
| 826 | StoreUnsentLogs(); |
| 827 | } |
| 828 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 829 | void MetricsService::PushPendingLogTextToUnsentOngoingLogs() { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 830 | // If UMA response told us not to upload, there's no need to save the pending |
| 831 | // log. It wasn't supposed to be uploaded anyway. |
| 832 | if (!server_permits_upload_) |
| 833 | return; |
| 834 | |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 835 | if (pending_log_text_.length() > |
| 836 | static_cast<size_t>(kUploadLogAvoidRetransmitSize)) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 837 | UMA_HISTOGRAM_COUNTS("UMA.Large Accumulated Log Not Persisted", |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 838 | static_cast<int>(pending_log_text_.length())); |
| 839 | return; |
| 840 | } |
| 841 | unsent_ongoing_logs_.push_back(pending_log_text_); |
| 842 | } |
| 843 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 844 | //------------------------------------------------------------------------------ |
| 845 | // Transmission of logs methods |
| 846 | |
| 847 | void MetricsService::StartLogTransmissionTimer() { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 848 | // If we're not reporting, there's no point in starting a log transmission |
| 849 | // timer. |
| 850 | if (!reporting_active()) |
| 851 | return; |
| 852 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 853 | if (!current_log_) |
| 854 | return; // Recorder is shutdown. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 855 | |
| 856 | // If there is already a timer running, we leave it running. |
| 857 | // If timer_pending is true because the fetch is waiting for a response, |
| 858 | // we return for now and let the response handler start the timer. |
| 859 | if (timer_pending_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 860 | return; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 861 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 862 | // Before starting the timer, set timer_pending_ to true. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 863 | timer_pending_ = true; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 864 | |
| 865 | // Right before the UMA transmission gets started, there's one more thing we'd |
| 866 | // like to record: the histogram of memory usage, so we spawn a task to |
| 867 | // collect the memory details and when that task is finished, we arrange for |
| 868 | // TryToStartTransmission to take over. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 869 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 870 | log_sender_factory_. |
| 871 | NewRunnableMethod(&MetricsService::CollectMemoryDetails), |
| 872 | static_cast<int>(interlog_duration_.InMilliseconds())); |
| 873 | } |
| 874 | |
| 875 | void MetricsService::TryToStartTransmission() { |
| 876 | DCHECK(IsSingleThreaded()); |
| 877 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 878 | // This function should only be called via timer, so timer_pending_ |
| 879 | // should be true. |
| 880 | DCHECK(timer_pending_); |
| 881 | timer_pending_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 882 | |
| 883 | DCHECK(!current_fetch_.get()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 884 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 885 | // If we're getting no notifications, then the log won't have much in it, and |
| 886 | // it's possible the computer is about to go to sleep, so don't upload and |
| 887 | // don't restart the transmission timer. |
| 888 | if (idle_since_last_transmission_) |
| 889 | return; |
| 890 | |
| 891 | // If somehow there is a fetch in progress, we return setting timer_pending_ |
| 892 | // to true and hope things work out. |
| 893 | if (current_fetch_.get()) { |
| 894 | timer_pending_ = true; |
| 895 | return; |
| 896 | } |
| 897 | |
| 898 | // If uploads are forbidden by UMA response, there's no point in keeping |
| 899 | // the current_log_, and the more often we delete it, the less likely it is |
| 900 | // to expand forever. |
| 901 | if (!server_permits_upload_ && current_log_) { |
| 902 | StopRecording(NULL); |
| 903 | StartRecording(); |
| 904 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 905 | |
| 906 | if (!current_log_) |
| 907 | return; // Logging was disabled. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 908 | if (!reporting_active()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 909 | return; // Don't do work if we're not going to send anything now. |
| 910 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 911 | MakePendingLog(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 912 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 913 | // MakePendingLog should have put something in the pending log, if it didn't, |
| 914 | // we start the timer again, return and hope things work out. |
| 915 | if (!pending_log()) { |
| 916 | StartLogTransmissionTimer(); |
| 917 | return; |
| 918 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 919 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 920 | // If we're not supposed to upload any UMA data because the response or the |
| 921 | // user said so, cancel the upload at this point, but start the timer. |
| 922 | if (!TransmissionPermitted()) { |
| 923 | DiscardPendingLog(); |
| 924 | StartLogTransmissionTimer(); |
| 925 | return; |
| 926 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 927 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 928 | PrepareFetchWithPendingLog(); |
| 929 | |
| 930 | if (!current_fetch_.get()) { |
| 931 | // Compression failed, and log discarded :-/. |
| 932 | DiscardPendingLog(); |
| 933 | StartLogTransmissionTimer(); // Maybe we'll do better next time |
| 934 | // TODO(jar): If compression failed, we should have created a tiny log and |
| 935 | // compressed that, so that we can signal that we're losing logs. |
| 936 | return; |
| 937 | } |
| 938 | |
| 939 | DCHECK(!timer_pending_); |
| 940 | |
| 941 | // The URL fetch is a like timer in that after a while we get called back |
| 942 | // so we set timer_pending_ true just as we start the url fetch. |
| 943 | timer_pending_ = true; |
| 944 | current_fetch_->Start(); |
| 945 | |
| 946 | HandleIdleSinceLastTransmission(true); |
| 947 | } |
| 948 | |
| 949 | |
| 950 | void MetricsService::MakePendingLog() { |
| 951 | if (pending_log()) |
| 952 | return; |
| 953 | |
| 954 | switch (state_) { |
| 955 | case INITIALIZED: |
| 956 | case PLUGIN_LIST_REQUESTED: // We should be further along by now. |
| 957 | DCHECK(false); |
| 958 | return; |
| 959 | |
| 960 | case PLUGIN_LIST_ARRIVED: |
| 961 | // We need to wait for the initial log to be ready before sending |
| 962 | // anything, because the server will tell us whether it wants to hear |
| 963 | // from us. |
| 964 | PrepareInitialLog(); |
| 965 | DCHECK(state_ == PLUGIN_LIST_ARRIVED); |
| 966 | RecallUnsentLogs(); |
| 967 | state_ = INITIAL_LOG_READY; |
| 968 | break; |
| 969 | |
| 970 | case SEND_OLD_INITIAL_LOGS: |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 971 | if (!unsent_initial_logs_.empty()) { |
| 972 | pending_log_text_ = unsent_initial_logs_.back(); |
| 973 | break; |
| 974 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 975 | state_ = SENDING_OLD_LOGS; |
| 976 | // Fall through. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 977 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 978 | case SENDING_OLD_LOGS: |
| 979 | if (!unsent_ongoing_logs_.empty()) { |
| 980 | pending_log_text_ = unsent_ongoing_logs_.back(); |
| 981 | break; |
| 982 | } |
| 983 | state_ = SENDING_CURRENT_LOGS; |
| 984 | // Fall through. |
| 985 | |
| 986 | case SENDING_CURRENT_LOGS: |
| 987 | StopRecording(&pending_log_); |
| 988 | StartRecording(); |
| 989 | break; |
| 990 | |
| 991 | default: |
| 992 | DCHECK(false); |
| 993 | return; |
| 994 | } |
| 995 | |
| 996 | DCHECK(pending_log()); |
| 997 | } |
| 998 | |
| 999 | bool MetricsService::TransmissionPermitted() const { |
| 1000 | // If the user forbids uploading that's they're business, and we don't upload |
| 1001 | // anything. If the server forbids uploading, that's our business, so we take |
| 1002 | // that to mean it forbids current logs, but we still send up the inital logs |
| 1003 | // and any old logs. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1004 | if (!user_permits_upload_) |
| 1005 | return false; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1006 | if (server_permits_upload_) |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1007 | return true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1008 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1009 | switch (state_) { |
| 1010 | case INITIAL_LOG_READY: |
| 1011 | case SEND_OLD_INITIAL_LOGS: |
| 1012 | case SENDING_OLD_LOGS: |
| 1013 | return true; |
| 1014 | |
| 1015 | case SENDING_CURRENT_LOGS: |
| 1016 | default: |
| 1017 | return false; |
[email protected] | 8c8824b | 2008-09-20 01:55:50 | [diff] [blame] | 1018 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1019 | } |
| 1020 | |
| 1021 | void MetricsService::CollectMemoryDetails() { |
| 1022 | Task* task = log_sender_factory_. |
| 1023 | NewRunnableMethod(&MetricsService::TryToStartTransmission); |
| 1024 | MetricsMemoryDetails* details = new MetricsMemoryDetails(task); |
| 1025 | details->StartFetch(); |
| 1026 | |
| 1027 | // Collect WebCore cache information to put into a histogram. |
| 1028 | for (RenderProcessHost::iterator it = RenderProcessHost::begin(); |
| 1029 | it != RenderProcessHost::end(); ++it) { |
| 1030 | it->second->Send(new ViewMsg_GetCacheResourceStats()); |
| 1031 | } |
| 1032 | } |
| 1033 | |
| 1034 | void MetricsService::PrepareInitialLog() { |
| 1035 | DCHECK(state_ == PLUGIN_LIST_ARRIVED); |
| 1036 | std::vector<WebPluginInfo> plugins; |
| 1037 | PluginService::GetInstance()->GetPlugins(false, &plugins); |
| 1038 | |
| 1039 | MetricsLog* log = new MetricsLog(client_id_, session_id_); |
| 1040 | log->RecordEnvironment(plugins, profile_dictionary_.get()); |
| 1041 | |
| 1042 | // Histograms only get written to current_log_, so setup for the write. |
| 1043 | MetricsLog* save_log = current_log_; |
| 1044 | current_log_ = log; |
| 1045 | RecordCurrentHistograms(); // Into current_log_... which is really log. |
| 1046 | current_log_ = save_log; |
| 1047 | |
| 1048 | log->CloseLog(); |
| 1049 | DCHECK(!pending_log()); |
| 1050 | pending_log_ = log; |
| 1051 | } |
| 1052 | |
| 1053 | void MetricsService::RecallUnsentLogs() { |
| 1054 | DCHECK(unsent_initial_logs_.empty()); |
| 1055 | DCHECK(unsent_ongoing_logs_.empty()); |
| 1056 | |
| 1057 | PrefService* local_state = g_browser_process->local_state(); |
| 1058 | DCHECK(local_state); |
| 1059 | |
| 1060 | ListValue* unsent_initial_logs = local_state->GetMutableList( |
| 1061 | prefs::kMetricsInitialLogs); |
| 1062 | for (ListValue::iterator it = unsent_initial_logs->begin(); |
| 1063 | it != unsent_initial_logs->end(); ++it) { |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 1064 | std::string log; |
| 1065 | (*it)->GetAsString(&log); |
| 1066 | unsent_initial_logs_.push_back(log); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1067 | } |
| 1068 | |
| 1069 | ListValue* unsent_ongoing_logs = local_state->GetMutableList( |
| 1070 | prefs::kMetricsOngoingLogs); |
| 1071 | for (ListValue::iterator it = unsent_ongoing_logs->begin(); |
| 1072 | it != unsent_ongoing_logs->end(); ++it) { |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 1073 | std::string log; |
| 1074 | (*it)->GetAsString(&log); |
| 1075 | unsent_ongoing_logs_.push_back(log); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1076 | } |
| 1077 | } |
| 1078 | |
| 1079 | void MetricsService::StoreUnsentLogs() { |
| 1080 | if (state_ < INITIAL_LOG_READY) |
| 1081 | return; // We never Recalled the prior unsent logs. |
| 1082 | |
| 1083 | PrefService* local_state = g_browser_process->local_state(); |
| 1084 | DCHECK(local_state); |
| 1085 | |
| 1086 | ListValue* unsent_initial_logs = local_state->GetMutableList( |
| 1087 | prefs::kMetricsInitialLogs); |
| 1088 | unsent_initial_logs->Clear(); |
| 1089 | size_t start = 0; |
| 1090 | if (unsent_initial_logs_.size() > kMaxInitialLogsPersisted) |
| 1091 | start = unsent_initial_logs_.size() - kMaxInitialLogsPersisted; |
| 1092 | for (size_t i = start; i < unsent_initial_logs_.size(); ++i) |
| 1093 | unsent_initial_logs->Append( |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 1094 | Value::CreateStringValue(unsent_initial_logs_[i])); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1095 | |
| 1096 | ListValue* unsent_ongoing_logs = local_state->GetMutableList( |
| 1097 | prefs::kMetricsOngoingLogs); |
| 1098 | unsent_ongoing_logs->Clear(); |
| 1099 | start = 0; |
| 1100 | if (unsent_ongoing_logs_.size() > kMaxOngoingLogsPersisted) |
| 1101 | start = unsent_ongoing_logs_.size() - kMaxOngoingLogsPersisted; |
| 1102 | for (size_t i = start; i < unsent_ongoing_logs_.size(); ++i) |
| 1103 | unsent_ongoing_logs->Append( |
[email protected] | 5e324b7 | 2008-12-18 00:07:59 | [diff] [blame] | 1104 | Value::CreateStringValue(unsent_ongoing_logs_[i])); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1105 | } |
| 1106 | |
| 1107 | void MetricsService::PreparePendingLogText() { |
| 1108 | DCHECK(pending_log()); |
| 1109 | if (!pending_log_text_.empty()) |
| 1110 | return; |
| 1111 | int original_size = pending_log_->GetEncodedLogSize(); |
| 1112 | pending_log_->GetEncodedLog(WriteInto(&pending_log_text_, original_size), |
| 1113 | original_size); |
| 1114 | } |
| 1115 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1116 | void MetricsService::PrepareFetchWithPendingLog() { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1117 | DCHECK(pending_log()); |
| 1118 | DCHECK(!current_fetch_.get()); |
| 1119 | PreparePendingLogText(); |
| 1120 | DCHECK(!pending_log_text_.empty()); |
| 1121 | |
| 1122 | // Allow security conscious users to see all metrics logs that we send. |
| 1123 | LOG(INFO) << "METRICS LOG: " << pending_log_text_; |
| 1124 | |
| 1125 | std::string compressed_log; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1126 | if (!Bzip2Compress(pending_log_text_, &compressed_log)) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1127 | NOTREACHED() << "Failed to compress log for transmission."; |
| 1128 | DiscardPendingLog(); |
| 1129 | StartLogTransmissionTimer(); // Maybe we'll do better on next log :-/. |
| 1130 | return; |
| 1131 | } |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1132 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1133 | current_fetch_.reset(new URLFetcher(GURL(kMetricsURL), URLFetcher::POST, |
| 1134 | this)); |
| 1135 | current_fetch_->set_request_context(Profile::GetDefaultRequestContext()); |
| 1136 | current_fetch_->set_upload_data(kMetricsType, compressed_log); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1137 | } |
| 1138 | |
| 1139 | void MetricsService::DiscardPendingLog() { |
| 1140 | if (pending_log_) { // Shutdown might have deleted it! |
| 1141 | delete pending_log_; |
| 1142 | pending_log_ = NULL; |
| 1143 | } |
| 1144 | pending_log_text_.clear(); |
| 1145 | } |
| 1146 | |
| 1147 | // This implementation is based on the Firefox MetricsService implementation. |
| 1148 | bool MetricsService::Bzip2Compress(const std::string& input, |
| 1149 | std::string* output) { |
| 1150 | bz_stream stream = {0}; |
| 1151 | // As long as our input is smaller than the bzip2 block size, we should get |
| 1152 | // the best compression. For example, if your input was 250k, using a block |
| 1153 | // size of 300k or 500k should result in the same compression ratio. Since |
| 1154 | // our data should be under 100k, using the minimum block size of 100k should |
| 1155 | // allocate less temporary memory, but result in the same compression ratio. |
| 1156 | int result = BZ2_bzCompressInit(&stream, |
| 1157 | 1, // 100k (min) block size |
| 1158 | 0, // quiet |
| 1159 | 0); // default "work factor" |
| 1160 | if (result != BZ_OK) { // out of memory? |
| 1161 | return false; |
| 1162 | } |
| 1163 | |
| 1164 | output->clear(); |
| 1165 | |
| 1166 | stream.next_in = const_cast<char*>(input.data()); |
| 1167 | stream.avail_in = static_cast<int>(input.size()); |
| 1168 | // NOTE: we don't need a BZ_RUN phase since our input buffer contains |
| 1169 | // the entire input |
| 1170 | do { |
| 1171 | output->resize(output->size() + 1024); |
| 1172 | stream.next_out = &((*output)[stream.total_out_lo32]); |
| 1173 | stream.avail_out = static_cast<int>(output->size()) - stream.total_out_lo32; |
| 1174 | result = BZ2_bzCompress(&stream, BZ_FINISH); |
| 1175 | } while (result == BZ_FINISH_OK); |
| 1176 | if (result != BZ_STREAM_END) // unknown failure? |
| 1177 | return false; |
| 1178 | result = BZ2_bzCompressEnd(&stream); |
| 1179 | DCHECK(result == BZ_OK); |
| 1180 | |
| 1181 | output->resize(stream.total_out_lo32); |
| 1182 | |
| 1183 | return true; |
| 1184 | } |
| 1185 | |
| 1186 | static const char* StatusToString(const URLRequestStatus& status) { |
| 1187 | switch (status.status()) { |
| 1188 | case URLRequestStatus::SUCCESS: |
| 1189 | return "SUCCESS"; |
| 1190 | |
| 1191 | case URLRequestStatus::IO_PENDING: |
| 1192 | return "IO_PENDING"; |
| 1193 | |
| 1194 | case URLRequestStatus::HANDLED_EXTERNALLY: |
| 1195 | return "HANDLED_EXTERNALLY"; |
| 1196 | |
| 1197 | case URLRequestStatus::CANCELED: |
| 1198 | return "CANCELED"; |
| 1199 | |
| 1200 | case URLRequestStatus::FAILED: |
| 1201 | return "FAILED"; |
| 1202 | |
| 1203 | default: |
| 1204 | NOTREACHED(); |
| 1205 | return "Unknown"; |
| 1206 | } |
| 1207 | } |
| 1208 | |
| 1209 | void MetricsService::OnURLFetchComplete(const URLFetcher* source, |
| 1210 | const GURL& url, |
| 1211 | const URLRequestStatus& status, |
| 1212 | int response_code, |
| 1213 | const ResponseCookies& cookies, |
| 1214 | const std::string& data) { |
| 1215 | DCHECK(timer_pending_); |
| 1216 | timer_pending_ = false; |
| 1217 | DCHECK(current_fetch_.get()); |
| 1218 | current_fetch_.reset(NULL); // We're not allowed to re-use it. |
| 1219 | |
| 1220 | // Confirm send so that we can move on. |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1221 | LOG(INFO) << "METRICS RESPONSE CODE: " << response_code << " status=" << |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1222 | StatusToString(status); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1223 | |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1224 | // Provide boolean for error recovery (allow us to ignore response_code). |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1225 | bool discard_log = false; |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1226 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1227 | if (response_code != 200 && |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1228 | pending_log_text_.length() > |
| 1229 | static_cast<size_t>(kUploadLogAvoidRetransmitSize)) { |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1230 | UMA_HISTOGRAM_COUNTS("UMA.Large Rejected Log was Discarded", |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1231 | static_cast<int>(pending_log_text_.length())); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1232 | discard_log = true; |
| 1233 | } else if (response_code == 400) { |
| 1234 | // Bad syntax. Retransmission won't work. |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1235 | UMA_HISTOGRAM_COUNTS("UMA.Unacceptable_Log_Discarded", state_); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1236 | discard_log = true; |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1237 | } |
| 1238 | |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1239 | if (response_code != 200 && !discard_log) { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1240 | LOG(INFO) << "METRICS: transmission attempt returned a failure code: " |
| 1241 | << response_code << ". Verify network connectivity"; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1242 | HandleBadResponseCode(); |
[email protected] | 0eb34fee | 2009-01-21 08:04:38 | [diff] [blame] | 1243 | } else { // Successful receipt (or we are discarding log). |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1244 | LOG(INFO) << "METRICS RESPONSE DATA: " << data; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1245 | switch (state_) { |
| 1246 | case INITIAL_LOG_READY: |
| 1247 | state_ = SEND_OLD_INITIAL_LOGS; |
| 1248 | break; |
| 1249 | |
| 1250 | case SEND_OLD_INITIAL_LOGS: |
| 1251 | DCHECK(!unsent_initial_logs_.empty()); |
| 1252 | unsent_initial_logs_.pop_back(); |
| 1253 | StoreUnsentLogs(); |
| 1254 | break; |
| 1255 | |
| 1256 | case SENDING_OLD_LOGS: |
| 1257 | DCHECK(!unsent_ongoing_logs_.empty()); |
| 1258 | unsent_ongoing_logs_.pop_back(); |
| 1259 | StoreUnsentLogs(); |
| 1260 | break; |
| 1261 | |
| 1262 | case SENDING_CURRENT_LOGS: |
| 1263 | break; |
| 1264 | |
| 1265 | default: |
| 1266 | DCHECK(false); |
| 1267 | break; |
| 1268 | } |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1269 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1270 | DiscardPendingLog(); |
[email protected] | 29be9255 | 2008-08-07 22:49:27 | [diff] [blame] | 1271 | // Since we sent a log, make sure our in-memory state is recorded to disk. |
| 1272 | PrefService* local_state = g_browser_process->local_state(); |
| 1273 | DCHECK(local_state); |
| 1274 | if (local_state) |
| 1275 | local_state->ScheduleSavePersistentPrefs( |
| 1276 | g_browser_process->file_thread()); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1277 | |
[email protected] | 147bbc0b | 2009-01-06 19:37:40 | [diff] [blame] | 1278 | // Provide a default (free of exponetial backoff, other varances) in case |
| 1279 | // the server does not specify a value. |
| 1280 | interlog_duration_ = TimeDelta::FromSeconds(kMinSecondsPerLog); |
| 1281 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1282 | GetSettingsFromResponseData(data); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1283 | // Override server specified interlog delay if there are unsent logs to |
[email protected] | 29be9255 | 2008-08-07 22:49:27 | [diff] [blame] | 1284 | // transmit. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1285 | if (unsent_logs()) { |
| 1286 | DCHECK(state_ < SENDING_CURRENT_LOGS); |
| 1287 | interlog_duration_ = TimeDelta::FromSeconds(kUnsentLogDelay); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1288 | } |
| 1289 | } |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1290 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1291 | StartLogTransmissionTimer(); |
| 1292 | } |
| 1293 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1294 | void MetricsService::HandleBadResponseCode() { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1295 | LOG(INFO) << "Verify your metrics logs are formatted correctly. " |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1296 | "Verify server is active at " << kMetricsURL; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1297 | if (!pending_log()) { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1298 | LOG(INFO) << "METRICS: Recorder shutdown during log transmission."; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1299 | } else { |
| 1300 | // Send progressively less frequently. |
| 1301 | DCHECK(kBackoff > 1.0); |
| 1302 | interlog_duration_ = TimeDelta::FromMicroseconds( |
| 1303 | static_cast<int64>(kBackoff * interlog_duration_.InMicroseconds())); |
| 1304 | |
| 1305 | if (kMaxBackoff * TimeDelta::FromSeconds(kMinSecondsPerLog) < |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1306 | interlog_duration_) { |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1307 | interlog_duration_ = kMaxBackoff * |
| 1308 | TimeDelta::FromSeconds(kMinSecondsPerLog); |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1309 | } |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1310 | |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1311 | LOG(INFO) << "METRICS: transmission retry being scheduled in " << |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1312 | interlog_duration_.InSeconds() << " seconds for " << |
| 1313 | pending_log_text_; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1314 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1315 | } |
| 1316 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1317 | void MetricsService::GetSettingsFromResponseData(const std::string& data) { |
| 1318 | // We assume that the file is structured as a block opened by <response> |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1319 | // and that inside response, there is a block opened by tag <chrome_config> |
| 1320 | // other tags are ignored for now except the content of <chrome_config>. |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1321 | LOG(INFO) << "METRICS: getting settings from response data: " << data; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1322 | |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1323 | int data_size = static_cast<int>(data.size()); |
| 1324 | if (data_size < 0) { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1325 | LOG(INFO) << "METRICS: server response data bad size: " << data_size << |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1326 | "; aborting extraction of settings"; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1327 | return; |
| 1328 | } |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1329 | xmlDocPtr doc = xmlReadMemory(data.c_str(), data_size, "", NULL, 0); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1330 | DCHECK(doc); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1331 | // If the document is malformed, we just use the settings that were there. |
| 1332 | if (!doc) { |
[email protected] | 281d288 | 2009-01-20 20:32:42 | [diff] [blame] | 1333 | LOG(INFO) << "METRICS: reading xml from server response data failed"; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1334 | return; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1335 | } |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1336 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1337 | xmlNodePtr top_node = xmlDocGetRootElement(doc), chrome_config_node = NULL; |
| 1338 | // Here, we find the chrome_config node by name. |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1339 | for (xmlNodePtr p = top_node->children; p; p = p->next) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1340 | if (xmlStrEqual(p->name, BAD_CAST "chrome_config")) { |
| 1341 | chrome_config_node = p; |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1342 | break; |
| 1343 | } |
| 1344 | } |
| 1345 | // If the server data is formatted wrong and there is no |
| 1346 | // config node where we expect, we just drop out. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1347 | if (chrome_config_node != NULL) |
| 1348 | GetSettingsFromChromeConfigNode(chrome_config_node); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1349 | xmlFreeDoc(doc); |
| 1350 | } |
| 1351 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1352 | void MetricsService::GetSettingsFromChromeConfigNode( |
| 1353 | xmlNodePtr chrome_config_node) { |
| 1354 | // Iterate through all children of the config node. |
| 1355 | for (xmlNodePtr current_node = chrome_config_node->children; |
| 1356 | current_node; |
| 1357 | current_node = current_node->next) { |
| 1358 | // If we find the upload tag, we appeal to another function |
| 1359 | // GetSettingsFromUploadNode to read all the data in it. |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1360 | if (xmlStrEqual(current_node->name, BAD_CAST "upload")) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1361 | GetSettingsFromUploadNode(current_node); |
[email protected] | 252873ef | 2008-08-04 21:59:45 | [diff] [blame] | 1362 | continue; |
| 1363 | } |
| 1364 | } |
| 1365 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1366 | |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1367 | void MetricsService::InheritedProperties::OverwriteWhereNeeded( |
| 1368 | xmlNodePtr node) { |
| 1369 | xmlChar* salt_value = xmlGetProp(node, BAD_CAST "salt"); |
| 1370 | if (salt_value) // If the property isn't there, xmlGetProp returns NULL. |
| 1371 | salt = atoi(reinterpret_cast<char*>(salt_value)); |
| 1372 | // If the property isn't there, we keep the value the property had before |
| 1373 | |
| 1374 | xmlChar* denominator_value = xmlGetProp(node, BAD_CAST "denominator"); |
| 1375 | if (denominator_value) |
| 1376 | denominator = atoi(reinterpret_cast<char*>(denominator_value)); |
| 1377 | } |
| 1378 | |
| 1379 | void MetricsService::GetSettingsFromUploadNode(xmlNodePtr upload_node) { |
| 1380 | InheritedProperties props; |
| 1381 | GetSettingsFromUploadNodeRecursive(upload_node, props, "", true); |
| 1382 | } |
| 1383 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1384 | void MetricsService::GetSettingsFromUploadNodeRecursive( |
| 1385 | xmlNodePtr node, |
| 1386 | InheritedProperties props, |
| 1387 | std::string path_prefix, |
| 1388 | bool uploadOn) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1389 | props.OverwriteWhereNeeded(node); |
| 1390 | |
| 1391 | // The bool uploadOn is set to true if the data represented by current |
| 1392 | // node should be uploaded. This gets inherited in the tree; the children |
| 1393 | // of a node that has already been rejected for upload get rejected for |
| 1394 | // upload. |
| 1395 | uploadOn = uploadOn && NodeProbabilityTest(node, props); |
| 1396 | |
| 1397 | // The path is a / separated list of the node names ancestral to the current |
| 1398 | // one. So, if you want to check if the current node has a certain name, |
| 1399 | // compare to name. If you want to check if it is a certan tag at a certain |
| 1400 | // place in the tree, compare to the whole path. |
| 1401 | std::string name = std::string(reinterpret_cast<const char*>(node->name)); |
| 1402 | std::string path = path_prefix + "/" + name; |
| 1403 | |
| 1404 | if (path == "/upload") { |
| 1405 | xmlChar* upload_interval_val = xmlGetProp(node, BAD_CAST "interval"); |
| 1406 | if (upload_interval_val) { |
| 1407 | interlog_duration_ = TimeDelta::FromSeconds( |
| 1408 | atoi(reinterpret_cast<char*>(upload_interval_val))); |
| 1409 | } |
| 1410 | |
| 1411 | server_permits_upload_ = uploadOn; |
| 1412 | } |
| 1413 | if (path == "/upload/logs") { |
| 1414 | xmlChar* log_event_limit_val = xmlGetProp(node, BAD_CAST "event_limit"); |
| 1415 | if (log_event_limit_val) |
| 1416 | log_event_limit_ = atoi(reinterpret_cast<char*>(log_event_limit_val)); |
| 1417 | } |
| 1418 | if (name == "histogram") { |
| 1419 | xmlChar* type_value = xmlGetProp(node, BAD_CAST "type"); |
| 1420 | if (type_value) { |
| 1421 | std::string type = (reinterpret_cast<char*>(type_value)); |
| 1422 | if (uploadOn) |
| 1423 | histograms_to_upload_.insert(type); |
| 1424 | else |
| 1425 | histograms_to_omit_.insert(type); |
| 1426 | } |
| 1427 | } |
| 1428 | if (name == "log") { |
| 1429 | xmlChar* type_value = xmlGetProp(node, BAD_CAST "type"); |
| 1430 | if (type_value) { |
| 1431 | std::string type = (reinterpret_cast<char*>(type_value)); |
| 1432 | if (uploadOn) |
| 1433 | logs_to_upload_.insert(type); |
| 1434 | else |
| 1435 | logs_to_omit_.insert(type); |
| 1436 | } |
| 1437 | } |
| 1438 | |
| 1439 | // Recursive call. If the node is a leaf i.e. if it ends in a "/>", then it |
| 1440 | // doesn't have children, so node->children is NULL, and this loop doesn't |
| 1441 | // call (that's how the recursion ends). |
| 1442 | for (xmlNodePtr child_node = node->children; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1443 | child_node; |
| 1444 | child_node = child_node->next) { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1445 | GetSettingsFromUploadNodeRecursive(child_node, props, path, uploadOn); |
| 1446 | } |
| 1447 | } |
| 1448 | |
| 1449 | bool MetricsService::NodeProbabilityTest(xmlNodePtr node, |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1450 | InheritedProperties props) const { |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1451 | // Default value of probability on any node is 1, but recall that |
| 1452 | // its parents can already have been rejected for upload. |
| 1453 | double probability = 1; |
| 1454 | |
| 1455 | // If a probability is specified in the node, we use it instead. |
| 1456 | xmlChar* probability_value = xmlGetProp(node, BAD_CAST "probability"); |
| 1457 | if (probability_value) |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 1458 | probability = atoi(reinterpret_cast<char*>(probability_value)); |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1459 | |
| 1460 | return ProbabilityTest(probability, props.salt, props.denominator); |
| 1461 | } |
| 1462 | |
| 1463 | bool MetricsService::ProbabilityTest(double probability, |
| 1464 | int salt, |
| 1465 | int denominator) const { |
| 1466 | // Okay, first we figure out how many of the digits of the |
| 1467 | // client_id_ we need in order to make a nice pseudorandomish |
| 1468 | // number in the range [0,denominator). Too many digits is |
| 1469 | // fine. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1470 | |
| 1471 | // n is the length of the client_id_ string |
| 1472 | size_t n = client_id_.size(); |
| 1473 | |
| 1474 | // idnumber is a positive integer generated from the client_id_. |
| 1475 | // It plus salt is going to give us our pseudorandom number. |
| 1476 | int idnumber = 0; |
| 1477 | const char* client_id_c_str = client_id_.c_str(); |
| 1478 | |
| 1479 | // Here we hash the relevant digits of the client_id_ |
| 1480 | // string somehow to get a big integer idnumber (could be negative |
| 1481 | // from wraparound) |
| 1482 | int big = 1; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1483 | for (size_t j = n - 1; j >= 0; --j) { |
| 1484 | idnumber += static_cast<int>(client_id_c_str[j]) * big; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1485 | big *= 10; |
| 1486 | } |
| 1487 | |
| 1488 | // Mod id number by denominator making sure to get a non-negative |
| 1489 | // answer. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1490 | idnumber = ((idnumber % denominator) + denominator) % denominator; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1491 | |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1492 | // ((idnumber + salt) % denominator) / denominator is in the range [0,1] |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1493 | // if it's less than probability we call that an affirmative coin |
| 1494 | // toss. |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1495 | return static_cast<double>((idnumber + salt) % denominator) < |
| 1496 | probability * denominator; |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1497 | } |
| 1498 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1499 | void MetricsService::LogWindowChange(NotificationType type, |
| 1500 | const NotificationSource& source, |
| 1501 | const NotificationDetails& details) { |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1502 | int controller_id = -1; |
| 1503 | uintptr_t window_or_tab = source.map_key(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1504 | MetricsLog::WindowEventType window_type; |
| 1505 | |
| 1506 | // Note: since we stop all logging when a single OTR session is active, it is |
| 1507 | // possible that we start getting notifications about a window that we don't |
| 1508 | // know about. |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1509 | if (window_map_.find(window_or_tab) == window_map_.end()) { |
| 1510 | controller_id = next_window_id_++; |
| 1511 | window_map_[window_or_tab] = controller_id; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1512 | } else { |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1513 | controller_id = window_map_[window_or_tab]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1514 | } |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1515 | DCHECK(controller_id != -1); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1516 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1517 | switch (type.value) { |
| 1518 | case NotificationType::TAB_PARENTED: |
| 1519 | case NotificationType::BROWSER_OPENED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1520 | window_type = MetricsLog::WINDOW_CREATE; |
| 1521 | break; |
| 1522 | |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1523 | case NotificationType::TAB_CLOSING: |
| 1524 | case NotificationType::BROWSER_CLOSED: |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1525 | window_map_.erase(window_map_.find(window_or_tab)); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1526 | window_type = MetricsLog::WINDOW_DESTROY; |
| 1527 | break; |
| 1528 | |
| 1529 | default: |
| 1530 | NOTREACHED(); |
[email protected] | 68d74f0 | 2009-02-13 01:36:50 | [diff] [blame] | 1531 | return; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1532 | } |
| 1533 | |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1534 | // TODO(brettw) we should have some kind of ID for the parent. |
| 1535 | current_log_->RecordWindowEvent(window_type, controller_id, 0); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1536 | } |
| 1537 | |
| 1538 | void MetricsService::LogLoadComplete(NotificationType type, |
| 1539 | const NotificationSource& source, |
| 1540 | const NotificationDetails& details) { |
| 1541 | if (details == NotificationService::NoDetails()) |
| 1542 | return; |
| 1543 | |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1544 | // TODO(jar): There is a bug causing this to be called too many times, and |
| 1545 | // the log overflows. For now, we won't record these events. |
[email protected] | 553dba6 | 2009-02-24 19:08:23 | [diff] [blame] | 1546 | UMA_HISTOGRAM_COUNTS("UMA.LogLoadComplete called", 1); |
[email protected] | 68475e60 | 2008-08-22 03:21:15 | [diff] [blame] | 1547 | return; |
| 1548 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1549 | const Details<LoadNotificationDetails> load_details(details); |
[email protected] | 534e54b | 2008-08-13 15:40:09 | [diff] [blame] | 1550 | int controller_id = window_map_[details.map_key()]; |
| 1551 | current_log_->RecordLoadEvent(controller_id, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1552 | load_details->url(), |
| 1553 | load_details->origin(), |
| 1554 | load_details->session_index(), |
| 1555 | load_details->load_time()); |
| 1556 | } |
| 1557 | |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1558 | void MetricsService::IncrementPrefValue(const wchar_t* path) { |
| 1559 | PrefService* pref = g_browser_process->local_state(); |
| 1560 | DCHECK(pref); |
| 1561 | int value = pref->GetInteger(path); |
| 1562 | pref->SetInteger(path, value + 1); |
| 1563 | } |
| 1564 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1565 | void MetricsService::LogLoadStarted() { |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1566 | IncrementPrefValue(prefs::kStabilityPageLoadCount); |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 1567 | // We need to save the prefs, as page load count is a critical stat, and it |
| 1568 | // might be lost due to a crash :-(. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1569 | } |
| 1570 | |
| 1571 | void MetricsService::LogRendererInSandbox(bool on_sandbox_desktop) { |
| 1572 | PrefService* prefs = g_browser_process->local_state(); |
| 1573 | DCHECK(prefs); |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1574 | if (on_sandbox_desktop) |
| 1575 | IncrementPrefValue(prefs::kSecurityRendererOnSboxDesktop); |
| 1576 | else |
| 1577 | IncrementPrefValue(prefs::kSecurityRendererOnDefaultDesktop); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1578 | } |
| 1579 | |
| 1580 | void MetricsService::LogRendererCrash() { |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1581 | IncrementPrefValue(prefs::kStabilityRendererCrashCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | void MetricsService::LogRendererHang() { |
[email protected] | e73c0197 | 2008-08-13 00:18:24 | [diff] [blame] | 1585 | IncrementPrefValue(prefs::kStabilityRendererHangCount); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1586 | } |
| 1587 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1588 | void MetricsService::LogChildProcessChange( |
| 1589 | NotificationType type, |
| 1590 | const NotificationSource& source, |
| 1591 | const NotificationDetails& details) { |
| 1592 | const std::wstring& child_name = |
| 1593 | Details<ChildProcessInfo>(details)->name(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1594 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1595 | if (child_process_stats_buffer_.find(child_name) == |
| 1596 | child_process_stats_buffer_.end()) { |
| 1597 | child_process_stats_buffer_[child_name] = ChildProcessStats(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1598 | } |
| 1599 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1600 | ChildProcessStats& stats = child_process_stats_buffer_[child_name]; |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1601 | switch (type.value) { |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1602 | case NotificationType::CHILD_PROCESS_HOST_CONNECTED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1603 | stats.process_launches++; |
| 1604 | break; |
| 1605 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1606 | case NotificationType::CHILD_INSTANCE_CREATED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1607 | stats.instances++; |
| 1608 | break; |
| 1609 | |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1610 | case NotificationType::CHILD_PROCESS_CRASHED: |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1611 | stats.process_crashes++; |
| 1612 | break; |
| 1613 | |
| 1614 | default: |
[email protected] | bfd04a6 | 2009-02-01 18:16:56 | [diff] [blame] | 1615 | NOTREACHED() << "Unexpected notification type " << type.value; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1616 | return; |
| 1617 | } |
| 1618 | } |
| 1619 | |
| 1620 | // Recursively counts the number of bookmarks and folders in node. |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 1621 | static void CountBookmarks(BookmarkNode* node, int* bookmarks, int* folders) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1622 | if (node->GetType() == history::StarredEntry::URL) |
| 1623 | (*bookmarks)++; |
| 1624 | else |
| 1625 | (*folders)++; |
| 1626 | for (int i = 0; i < node->GetChildCount(); ++i) |
| 1627 | CountBookmarks(node->GetChild(i), bookmarks, folders); |
| 1628 | } |
| 1629 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 1630 | void MetricsService::LogBookmarks(BookmarkNode* node, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1631 | const wchar_t* num_bookmarks_key, |
| 1632 | const wchar_t* num_folders_key) { |
| 1633 | DCHECK(node); |
| 1634 | int num_bookmarks = 0; |
| 1635 | int num_folders = 0; |
| 1636 | CountBookmarks(node, &num_bookmarks, &num_folders); |
| 1637 | num_folders--; // Don't include the root folder in the count. |
| 1638 | |
| 1639 | PrefService* pref = g_browser_process->local_state(); |
| 1640 | DCHECK(pref); |
| 1641 | pref->SetInteger(num_bookmarks_key, num_bookmarks); |
| 1642 | pref->SetInteger(num_folders_key, num_folders); |
| 1643 | } |
| 1644 | |
[email protected] | d8e41ed | 2008-09-11 15:22:32 | [diff] [blame] | 1645 | void MetricsService::LogBookmarks(BookmarkModel* model) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1646 | DCHECK(model); |
| 1647 | LogBookmarks(model->GetBookmarkBarNode(), |
| 1648 | prefs::kNumBookmarksOnBookmarkBar, |
| 1649 | prefs::kNumFoldersOnBookmarkBar); |
| 1650 | LogBookmarks(model->other_node(), |
| 1651 | prefs::kNumBookmarksInOtherBookmarkFolder, |
| 1652 | prefs::kNumFoldersInOtherBookmarkFolder); |
| 1653 | ScheduleNextStateSave(); |
| 1654 | } |
| 1655 | |
| 1656 | void MetricsService::LogKeywords(const TemplateURLModel* url_model) { |
| 1657 | DCHECK(url_model); |
| 1658 | |
| 1659 | PrefService* pref = g_browser_process->local_state(); |
| 1660 | DCHECK(pref); |
| 1661 | pref->SetInteger(prefs::kNumKeywords, |
| 1662 | static_cast<int>(url_model->GetTemplateURLs().size())); |
| 1663 | ScheduleNextStateSave(); |
| 1664 | } |
| 1665 | |
| 1666 | void MetricsService::RecordPluginChanges(PrefService* pref) { |
| 1667 | ListValue* plugins = pref->GetMutableList(prefs::kStabilityPluginStats); |
| 1668 | DCHECK(plugins); |
| 1669 | |
| 1670 | for (ListValue::iterator value_iter = plugins->begin(); |
| 1671 | value_iter != plugins->end(); ++value_iter) { |
| 1672 | if (!(*value_iter)->IsType(Value::TYPE_DICTIONARY)) { |
| 1673 | NOTREACHED(); |
| 1674 | continue; |
| 1675 | } |
| 1676 | |
| 1677 | DictionaryValue* plugin_dict = static_cast<DictionaryValue*>(*value_iter); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1678 | std::wstring plugin_name; |
| 1679 | plugin_dict->GetString(prefs::kStabilityPluginName, &plugin_name); |
[email protected] | 6470ee8f | 2009-03-03 20:46:40 | [diff] [blame] | 1680 | if (plugin_name.empty()) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1681 | NOTREACHED(); |
| 1682 | continue; |
| 1683 | } |
| 1684 | |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1685 | if (child_process_stats_buffer_.find(plugin_name) == |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1686 | child_process_stats_buffer_.end()) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1687 | continue; |
| 1688 | |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1689 | ChildProcessStats stats = child_process_stats_buffer_[plugin_name]; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1690 | if (stats.process_launches) { |
| 1691 | int launches = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1692 | plugin_dict->GetInteger(prefs::kStabilityPluginLaunches, &launches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1693 | launches += stats.process_launches; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1694 | plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, launches); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1695 | } |
| 1696 | if (stats.process_crashes) { |
| 1697 | int crashes = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1698 | plugin_dict->GetInteger(prefs::kStabilityPluginCrashes, &crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1699 | crashes += stats.process_crashes; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1700 | plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, crashes); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1701 | } |
| 1702 | if (stats.instances) { |
| 1703 | int instances = 0; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1704 | plugin_dict->GetInteger(prefs::kStabilityPluginInstances, &instances); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1705 | instances += stats.instances; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1706 | plugin_dict->SetInteger(prefs::kStabilityPluginInstances, instances); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1707 | } |
| 1708 | |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1709 | child_process_stats_buffer_.erase(plugin_name); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1710 | } |
| 1711 | |
| 1712 | // Now go through and add dictionaries for plugins that didn't already have |
| 1713 | // reports in Local State. |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1714 | for (std::map<std::wstring, ChildProcessStats>::iterator cache_iter = |
| 1715 | child_process_stats_buffer_.begin(); |
| 1716 | cache_iter != child_process_stats_buffer_.end(); ++cache_iter) { |
| 1717 | std::wstring plugin_name = cache_iter->first; |
| 1718 | ChildProcessStats stats = cache_iter->second; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1719 | DictionaryValue* plugin_dict = new DictionaryValue; |
| 1720 | |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1721 | plugin_dict->SetString(prefs::kStabilityPluginName, plugin_name); |
| 1722 | plugin_dict->SetInteger(prefs::kStabilityPluginLaunches, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1723 | stats.process_launches); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1724 | plugin_dict->SetInteger(prefs::kStabilityPluginCrashes, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1725 | stats.process_crashes); |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1726 | plugin_dict->SetInteger(prefs::kStabilityPluginInstances, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1727 | stats.instances); |
| 1728 | plugins->Append(plugin_dict); |
| 1729 | } |
[email protected] | a27a938 | 2009-02-11 23:55:10 | [diff] [blame] | 1730 | child_process_stats_buffer_.clear(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1731 | } |
| 1732 | |
| 1733 | bool MetricsService::CanLogNotification(NotificationType type, |
| 1734 | const NotificationSource& source, |
| 1735 | const NotificationDetails& details) { |
| 1736 | // We simply don't log anything to UMA if there is a single off the record |
| 1737 | // session visible. The problem is that we always notify using the orginal |
| 1738 | // profile in order to simplify notification processing. |
| 1739 | return !BrowserList::IsOffTheRecordSessionActive(); |
| 1740 | } |
| 1741 | |
| 1742 | void MetricsService::RecordBooleanPrefValue(const wchar_t* path, bool value) { |
| 1743 | DCHECK(IsSingleThreaded()); |
| 1744 | |
| 1745 | PrefService* pref = g_browser_process->local_state(); |
| 1746 | DCHECK(pref); |
| 1747 | |
| 1748 | pref->SetBoolean(path, value); |
| 1749 | RecordCurrentState(pref); |
| 1750 | } |
| 1751 | |
| 1752 | void MetricsService::RecordCurrentState(PrefService* pref) { |
| 1753 | pref->SetString(prefs::kStabilityLastTimestampSec, |
| 1754 | Int64ToWString(Time::Now().ToTimeT())); |
| 1755 | |
| 1756 | RecordPluginChanges(pref); |
| 1757 | } |
| 1758 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 1759 | void MetricsService::CollectRendererHistograms() { |
| 1760 | for (RenderProcessHost::iterator it = RenderProcessHost::begin(); |
| 1761 | it != RenderProcessHost::end(); ++it) { |
| 1762 | it->second->Send(new ViewMsg_GetRendererHistograms()); |
| 1763 | } |
| 1764 | } |
| 1765 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1766 | void MetricsService::RecordCurrentHistograms() { |
| 1767 | DCHECK(current_log_); |
| 1768 | |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 1769 | CollectRendererHistograms(); |
| 1770 | |
| 1771 | // TODO(raman): Delay the metrics collection activities until we get all the |
| 1772 | // updates from the renderers, or we time out (1 second? 3 seconds?). |
| 1773 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1774 | StatisticsRecorder::Histograms histograms; |
| 1775 | StatisticsRecorder::GetHistograms(&histograms); |
| 1776 | for (StatisticsRecorder::Histograms::iterator it = histograms.begin(); |
| 1777 | histograms.end() != it; |
[email protected] | cac7884 | 2008-11-27 01:02:20 | [diff] [blame] | 1778 | ++it) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1779 | if ((*it)->flags() & kUmaTargetedHistogramFlag) |
[email protected] | 0b33f80b | 2008-12-17 21:34:36 | [diff] [blame] | 1780 | // TODO(petersont): Only record historgrams if they are not precluded by |
| 1781 | // the UMA response data. |
[email protected] | d01b873 | 2008-10-16 02:18:07 | [diff] [blame] | 1782 | // Bug http://code.google.com/p/chromium/issues/detail?id=2739. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1783 | RecordHistogram(**it); |
| 1784 | } |
| 1785 | } |
| 1786 | |
| 1787 | void MetricsService::RecordHistogram(const Histogram& histogram) { |
| 1788 | // Get up-to-date snapshot of sample stats. |
| 1789 | Histogram::SampleSet snapshot; |
| 1790 | histogram.SnapshotSample(&snapshot); |
| 1791 | |
| 1792 | const std::string& histogram_name = histogram.histogram_name(); |
| 1793 | |
| 1794 | // Find the already sent stats, or create an empty set. |
| 1795 | LoggedSampleMap::iterator it = logged_samples_.find(histogram_name); |
| 1796 | Histogram::SampleSet* already_logged; |
| 1797 | if (logged_samples_.end() == it) { |
| 1798 | // Add new entry |
| 1799 | already_logged = &logged_samples_[histogram.histogram_name()]; |
| 1800 | already_logged->Resize(histogram); // Complete initialization. |
| 1801 | } else { |
| 1802 | already_logged = &(it->second); |
| 1803 | // Deduct any stats we've already logged from our snapshot. |
| 1804 | snapshot.Subtract(*already_logged); |
| 1805 | } |
| 1806 | |
| 1807 | // snapshot now contains only a delta to what we've already_logged. |
| 1808 | |
| 1809 | if (snapshot.TotalCount() > 0) { |
| 1810 | current_log_->RecordHistogramDelta(histogram, snapshot); |
| 1811 | // Add new data into our running total. |
| 1812 | already_logged->Add(snapshot); |
| 1813 | } |
| 1814 | } |
| 1815 | |
| 1816 | void MetricsService::AddProfileMetric(Profile* profile, |
| 1817 | const std::wstring& key, |
| 1818 | int value) { |
| 1819 | // Restriction of types is needed for writing values. See |
| 1820 | // MetricsLog::WriteProfileMetrics. |
| 1821 | DCHECK(profile && !key.empty()); |
| 1822 | PrefService* prefs = g_browser_process->local_state(); |
| 1823 | DCHECK(prefs); |
| 1824 | |
| 1825 | // Key is stored in prefs, which interpret '.'s as paths. As such, key |
| 1826 | // shouldn't have any '.'s in it. |
| 1827 | DCHECK(key.find(L'.') == std::wstring::npos); |
| 1828 | // The id is most likely an email address. We shouldn't send it to the server. |
| 1829 | const std::wstring id_hash = |
| 1830 | UTF8ToWide(MetricsLog::CreateBase64Hash(WideToUTF8(profile->GetID()))); |
| 1831 | DCHECK(id_hash.find('.') == std::string::npos); |
| 1832 | |
| 1833 | DictionaryValue* prof_prefs = prefs->GetMutableDictionary( |
| 1834 | prefs::kProfileMetrics); |
| 1835 | DCHECK(prof_prefs); |
| 1836 | const std::wstring pref_key = std::wstring(prefs::kProfilePrefix) + id_hash + |
| 1837 | L"." + key; |
[email protected] | 8e50b60 | 2009-03-03 22:59:43 | [diff] [blame^] | 1838 | prof_prefs->SetInteger(pref_key.c_str(), value); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | static bool IsSingleThreaded() { |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1842 | static PlatformThreadId thread_id = 0; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1843 | if (!thread_id) |
[email protected] | dc6f496 | 2009-02-13 01:25:50 | [diff] [blame] | 1844 | thread_id = PlatformThread::CurrentId(); |
| 1845 | return PlatformThread::CurrentId() == thread_id; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 1846 | } |