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