| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // This file defines a service that collects information about the user | 5 // This file defines a service that collects information about the user |
| 6 // experience in order to help improve future versions of the app. | 6 // experience in order to help improve future versions of the app. |
| 7 | 7 |
| 8 #ifndef COMPONENTS_METRICS_METRICS_SERVICE_H_ | 8 #ifndef COMPONENTS_METRICS_METRICS_SERVICE_H_ |
| 9 #define COMPONENTS_METRICS_METRICS_SERVICE_H_ | 9 #define COMPONENTS_METRICS_METRICS_SERVICE_H_ |
| 10 | 10 |
| 11 #include <map> | 11 #include <map> |
| 12 #include <string> | 12 #include <string> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/basictypes.h" | 15 #include "base/basictypes.h" |
| 16 #include "base/gtest_prod_util.h" | 16 #include "base/gtest_prod_util.h" |
| 17 #include "base/memory/scoped_ptr.h" | 17 #include "base/memory/scoped_ptr.h" |
| 18 #include "base/memory/scoped_vector.h" | 18 #include "base/memory/scoped_vector.h" |
| 19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
| 20 #include "base/metrics/field_trial.h" | 20 #include "base/metrics/field_trial.h" |
| 21 #include "base/metrics/histogram_flattener.h" | 21 #include "base/metrics/histogram_flattener.h" |
| 22 #include "base/metrics/histogram_snapshot_manager.h" | 22 #include "base/metrics/histogram_snapshot_manager.h" |
| 23 #include "base/metrics/persistent_memory_allocator.h" |
| 23 #include "base/metrics/user_metrics.h" | 24 #include "base/metrics/user_metrics.h" |
| 24 #include "base/observer_list.h" | 25 #include "base/observer_list.h" |
| 25 #include "base/time/time.h" | 26 #include "base/time/time.h" |
| 26 #include "components/metrics/clean_exit_beacon.h" | 27 #include "components/metrics/clean_exit_beacon.h" |
| 27 #include "components/metrics/metrics_log.h" | 28 #include "components/metrics/metrics_log.h" |
| 28 #include "components/metrics/metrics_log_manager.h" | 29 #include "components/metrics/metrics_log_manager.h" |
| 29 #include "components/metrics/metrics_provider.h" | 30 #include "components/metrics/metrics_provider.h" |
| 30 #include "components/metrics/net/network_metrics_provider.h" | 31 #include "components/metrics/net/network_metrics_provider.h" |
| 31 #include "components/variations/synthetic_trials.h" | 32 #include "components/variations/synthetic_trials.h" |
| 32 | 33 |
| 33 class PrefService; | 34 class PrefService; |
| 34 class PrefRegistrySimple; | 35 class PrefRegistrySimple; |
| 35 | 36 |
| 36 namespace base { | 37 namespace base { |
| 37 class DictionaryValue; | 38 class DictionaryValue; |
| 38 class HistogramSamples; | 39 class HistogramSamples; |
| 39 class PrefService; | 40 class PrefService; |
| 41 class SharedMemoryAllocator; |
| 40 } | 42 } |
| 41 | 43 |
| 42 namespace variations { | 44 namespace variations { |
| 43 struct ActiveGroupId; | 45 struct ActiveGroupId; |
| 44 } | 46 } |
| 45 | 47 |
| 46 namespace net { | 48 namespace net { |
| 47 class URLFetcher; | 49 class URLFetcher; |
| 48 } | 50 } |
| 49 | 51 |
| (...skipping 154 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 204 // should not be called more than once. | 206 // should not be called more than once. |
| 205 void CheckForClonedInstall( | 207 void CheckForClonedInstall( |
| 206 scoped_refptr<base::SingleThreadTaskRunner> task_runner); | 208 scoped_refptr<base::SingleThreadTaskRunner> task_runner); |
| 207 | 209 |
| 208 // Clears the stability metrics that are saved in local state. | 210 // Clears the stability metrics that are saved in local state. |
| 209 void ClearSavedStabilityMetrics(); | 211 void ClearSavedStabilityMetrics(); |
| 210 | 212 |
| 211 // Pushes a log that has been generated by an external component. | 213 // Pushes a log that has been generated by an external component. |
| 212 void PushExternalLog(const std::string& log); | 214 void PushExternalLog(const std::string& log); |
| 213 | 215 |
| 216 // Add a persistent memory segment that contains histograms. Ownership |
| 217 // of this object remains with the caller; it must be removed from here |
| 218 // before being destroyed. |
| 219 void AddPersistentMemorySegment(base::PersistentMemoryAllocator* alloc); |
| 220 |
| 221 // Remove a persistent memory segment. This should be done only after |
| 222 // a final reporting has been done to avoid data loss and incorrect |
| 223 // reporting. |
| 224 void RemovePersistentMemorySegment(base::PersistentMemoryAllocator* alloc); |
| 225 |
| 214 protected: | 226 protected: |
| 215 // Exposed for testing. | 227 // Exposed for testing. |
| 216 MetricsLogManager* log_manager() { return &log_manager_; } | 228 MetricsLogManager* log_manager() { return &log_manager_; } |
| 217 | 229 |
| 218 private: | 230 private: |
| 219 friend class MetricsServiceAccessor; | 231 friend class MetricsServiceAccessor; |
| 220 | 232 |
| 221 // The MetricsService has a lifecycle that is stored as a state. | 233 // The MetricsService has a lifecycle that is stored as a state. |
| 222 // See metrics_service.cc for description of this lifecycle. | 234 // See metrics_service.cc for description of this lifecycle. |
| 223 enum State { | 235 enum State { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 234 | 246 |
| 235 // The current state of recording for the MetricsService. The state is UNSET | 247 // The current state of recording for the MetricsService. The state is UNSET |
| 236 // until set to something else, at which point it remains INACTIVE or ACTIVE | 248 // until set to something else, at which point it remains INACTIVE or ACTIVE |
| 237 // for the lifetime of the object. | 249 // for the lifetime of the object. |
| 238 enum RecordingState { | 250 enum RecordingState { |
| 239 INACTIVE, | 251 INACTIVE, |
| 240 ACTIVE, | 252 ACTIVE, |
| 241 UNSET | 253 UNSET |
| 242 }; | 254 }; |
| 243 | 255 |
| 256 typedef std::set<base::PersistentMemoryAllocator*> AllocatorSet; |
| 244 typedef std::vector<variations::SyntheticTrialGroup> SyntheticTrialGroups; | 257 typedef std::vector<variations::SyntheticTrialGroup> SyntheticTrialGroups; |
| 245 | 258 |
| 246 // Registers a field trial name and group to be used to annotate a UMA report | 259 // Registers a field trial name and group to be used to annotate a UMA report |
| 247 // with a particular Chrome configuration state. A UMA report will be | 260 // with a particular Chrome configuration state. A UMA report will be |
| 248 // annotated with this trial group if and only if all events in the report | 261 // annotated with this trial group if and only if all events in the report |
| 249 // were created after the trial is registered. Only one group name may be | 262 // were created after the trial is registered. Only one group name may be |
| 250 // registered at a time for a given trial_name. Only the last group name that | 263 // registered at a time for a given trial_name. Only the last group name that |
| 251 // is registered for a given trial name will be recorded. The values passed | 264 // is registered for a given trial name will be recorded. The values passed |
| 252 // in must not correspond to any real field trial in the code. | 265 // in must not correspond to any real field trial in the code. |
| 253 void RegisterSyntheticFieldTrial( | 266 void RegisterSyntheticFieldTrial( |
| 254 const variations::SyntheticTrialGroup& trial_group); | 267 const variations::SyntheticTrialGroup& trial_group); |
| 255 | 268 |
| 269 |
| 270 // A class for iterating over the histograms in persistent memory segments. |
| 271 // This is implemented as an STL-compatible iterator so it can be used in |
| 272 // places that accept histograms both from here and from std containers. |
| 273 class PersistentHistogramIterator { |
| 274 // This class provides a reference-counted pointer to a histogram which |
| 275 // is necessary for histogram objects that are dynamically generated |
| 276 // (generally pointing to persistent memory) and to be owned by an STL |
| 277 // iterator which, by necessity, must be copyable. |
| 278 class HistogramPointer : public base::RefCounted<HistogramPointer> { |
| 279 public: |
| 280 HistogramPointer(base::HistogramBase* h) : histogram_(h) {} |
| 281 base::HistogramBase* get() { return histogram_.get(); } |
| 282 |
| 283 private: |
| 284 scoped_ptr<base::HistogramBase> histogram_; |
| 285 }; |
| 286 |
| 287 public: |
| 288 PersistentHistogramIterator(AllocatorSet& allocators, |
| 289 AllocatorSet::iterator pos); |
| 290 |
| 291 PersistentHistogramIterator& operator++(); |
| 292 PersistentHistogramIterator operator++(int) { |
| 293 PersistentHistogramIterator tmp(*this); |
| 294 operator++(); |
| 295 return tmp; |
| 296 } |
| 297 |
| 298 bool operator==(const PersistentHistogramIterator& rhs) const { |
| 299 return allocator_iter_ == rhs.allocator_iter_ && |
| 300 histogram_iter_ == rhs.histogram_iter_; |
| 301 } |
| 302 bool operator!=(const PersistentHistogramIterator& rhs) const { |
| 303 return allocator_iter_ != rhs.allocator_iter_ || |
| 304 histogram_iter_ != rhs.histogram_iter_; |
| 305 } |
| 306 base::HistogramBase* operator*() { |
| 307 return current_histogram_->get(); |
| 308 } |
| 309 |
| 310 private: |
| 311 AllocatorSet& allocators_; |
| 312 AllocatorSet::iterator allocator_iter_; |
| 313 base::PersistentMemoryAllocator::Iterator histogram_iter_; |
| 314 |
| 315 // STL iterators must be copyable so a regular scoped_ptr is not |
| 316 // sufficient as it doesn't allow such. A ref-counted one is needed. |
| 317 scoped_refptr<HistogramPointer> current_histogram_; |
| 318 }; |
| 319 |
| 320 |
| 256 // Calls into the client to initialize some system profile metrics. | 321 // Calls into the client to initialize some system profile metrics. |
| 257 void StartInitTask(); | 322 void StartInitTask(); |
| 258 | 323 |
| 259 // Callback that moves the state to INIT_TASK_DONE. When this is called, the | 324 // Callback that moves the state to INIT_TASK_DONE. When this is called, the |
| 260 // state should be INIT_TASK_SCHEDULED. | 325 // state should be INIT_TASK_SCHEDULED. |
| 261 void FinishedInitTask(); | 326 void FinishedInitTask(); |
| 262 | 327 |
| 263 void OnUserAction(const std::string& action); | 328 void OnUserAction(const std::string& action); |
| 264 | 329 |
| 265 // Get the amount of uptime since this process started and since the last | 330 // Get the amount of uptime since this process started and since the last |
| (...skipping 118 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 384 void RecordCurrentHistograms(); | 449 void RecordCurrentHistograms(); |
| 385 | 450 |
| 386 // Record complete list of stability histograms into the current log, | 451 // Record complete list of stability histograms into the current log, |
| 387 // i.e., histograms with the |kUmaStabilityHistogramFlag| flag set. | 452 // i.e., histograms with the |kUmaStabilityHistogramFlag| flag set. |
| 388 void RecordCurrentStabilityHistograms(); | 453 void RecordCurrentStabilityHistograms(); |
| 389 | 454 |
| 390 // Skips staged upload and discards the log. Used in case of unsuccessful | 455 // Skips staged upload and discards the log. Used in case of unsuccessful |
| 391 // upload or intentional sampling of logs. | 456 // upload or intentional sampling of logs. |
| 392 void SkipAndDiscardUpload(); | 457 void SkipAndDiscardUpload(); |
| 393 | 458 |
| 459 // Begin and End iterators for going through all the metrics under management. |
| 460 PersistentHistogramIterator persistent_begin(); |
| 461 PersistentHistogramIterator persistent_end(); |
| 462 |
| 394 // Manager for the various in-flight logs. | 463 // Manager for the various in-flight logs. |
| 395 MetricsLogManager log_manager_; | 464 MetricsLogManager log_manager_; |
| 396 | 465 |
| 397 // |histogram_snapshot_manager_| prepares histogram deltas for transmission. | 466 // |histogram_snapshot_manager_| prepares histogram deltas for transmission. |
| 398 base::HistogramSnapshotManager histogram_snapshot_manager_; | 467 base::HistogramSnapshotManager histogram_snapshot_manager_; |
| 399 | 468 |
| 400 // Used to manage various metrics reporting state prefs, such as client id, | 469 // Used to manage various metrics reporting state prefs, such as client id, |
| 401 // low entropy source and whether metrics reporting is enabled. Weak pointer. | 470 // low entropy source and whether metrics reporting is enabled. Weak pointer. |
| 402 MetricsStateManager* const state_manager_; | 471 MetricsStateManager* const state_manager_; |
| 403 | 472 |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 466 static ExecutionPhase execution_phase_; | 535 static ExecutionPhase execution_phase_; |
| 467 | 536 |
| 468 // Reduntant marker to check that we completed our shutdown, and set the | 537 // Reduntant marker to check that we completed our shutdown, and set the |
| 469 // exited-cleanly bit in the prefs. | 538 // exited-cleanly bit in the prefs. |
| 470 static ShutdownCleanliness clean_shutdown_status_; | 539 static ShutdownCleanliness clean_shutdown_status_; |
| 471 | 540 |
| 472 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess); | 541 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess); |
| 473 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, | 542 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, |
| 474 PermutedEntropyCacheClearedWhenLowEntropyReset); | 543 PermutedEntropyCacheClearedWhenLowEntropyReset); |
| 475 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial); | 544 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial); |
| 545 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, MultiplePersistentAllocators); |
| 476 | 546 |
| 477 // Weak pointers factory used to post task on different threads. All weak | 547 // Weak pointers factory used to post task on different threads. All weak |
| 478 // pointers managed by this factory have the same lifetime as MetricsService. | 548 // pointers managed by this factory have the same lifetime as MetricsService. |
| 479 base::WeakPtrFactory<MetricsService> self_ptr_factory_; | 549 base::WeakPtrFactory<MetricsService> self_ptr_factory_; |
| 480 | 550 |
| 481 // Weak pointers factory used for saving state. All weak pointers managed by | 551 // Weak pointers factory used for saving state. All weak pointers managed by |
| 482 // this factory are invalidated in ScheduleNextStateSave. | 552 // this factory are invalidated in ScheduleNextStateSave. |
| 483 base::WeakPtrFactory<MetricsService> state_saver_factory_; | 553 base::WeakPtrFactory<MetricsService> state_saver_factory_; |
| 484 | 554 |
| 555 // Persistent memory segments that contain histograms created elsewhere. |
| 556 AllocatorSet allocators_; |
| 557 |
| 485 DISALLOW_COPY_AND_ASSIGN(MetricsService); | 558 DISALLOW_COPY_AND_ASSIGN(MetricsService); |
| 486 }; | 559 }; |
| 487 | 560 |
| 488 } // namespace metrics | 561 } // namespace metrics |
| 489 | 562 |
| 490 #endif // COMPONENTS_METRICS_METRICS_SERVICE_H_ | 563 #endif // COMPONENTS_METRICS_METRICS_SERVICE_H_ |
| OLD | NEW |