Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(70)

Side by Side Diff: components/metrics/metrics_service.h

Issue 1425533011: Support "shared" histograms between processes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@shmem-alloc
Patch Set: reorganized Factory class to be cleaner Created 5 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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/active_field_trials.h" 32 #include "components/variations/active_field_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 196 matching lines...) Expand 10 before | Expand all | Expand 10 after
246 // should not be called more than once. 248 // should not be called more than once.
247 void CheckForClonedInstall( 249 void CheckForClonedInstall(
248 scoped_refptr<base::SingleThreadTaskRunner> task_runner); 250 scoped_refptr<base::SingleThreadTaskRunner> task_runner);
249 251
250 // Clears the stability metrics that are saved in local state. 252 // Clears the stability metrics that are saved in local state.
251 void ClearSavedStabilityMetrics(); 253 void ClearSavedStabilityMetrics();
252 254
253 // Pushes a log that has been generated by an external component. 255 // Pushes a log that has been generated by an external component.
254 void PushExternalLog(const std::string& log); 256 void PushExternalLog(const std::string& log);
255 257
258 // Add a persistent memory segment that contains histograms. Ownership
259 // of this object remains with the caller; it must be removed from here
260 // before being destroyed.
261 void AddPersistentMemorySegment(base::PersistentMemoryAllocator* alloc);
262
263 // Remove a persistent memory segment. This should be done only after
264 // a final reporting has been done to avoid data loss and incorrect
265 // reporting.
266 void RemovePersistentMemorySegment(base::PersistentMemoryAllocator* alloc);
267
256 protected: 268 protected:
257 // Exposed for testing. 269 // Exposed for testing.
258 MetricsLogManager* log_manager() { return &log_manager_; } 270 MetricsLogManager* log_manager() { return &log_manager_; }
259 271
260 private: 272 private:
261 // The MetricsService has a lifecycle that is stored as a state. 273 // The MetricsService has a lifecycle that is stored as a state.
262 // See metrics_service.cc for description of this lifecycle. 274 // See metrics_service.cc for description of this lifecycle.
263 enum State { 275 enum State {
264 INITIALIZED, // Constructor was called. 276 INITIALIZED, // Constructor was called.
265 INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish. 277 INIT_TASK_SCHEDULED, // Waiting for deferred init tasks to finish.
266 INIT_TASK_DONE, // Waiting for timer to send initial log. 278 INIT_TASK_DONE, // Waiting for timer to send initial log.
267 SENDING_LOGS, // Sending logs an creating new ones when we run out. 279 SENDING_LOGS, // Sending logs an creating new ones when we run out.
268 }; 280 };
269 281
270 enum ShutdownCleanliness { 282 enum ShutdownCleanliness {
271 CLEANLY_SHUTDOWN = 0xdeadbeef, 283 CLEANLY_SHUTDOWN = 0xdeadbeef,
272 NEED_TO_SHUTDOWN = ~CLEANLY_SHUTDOWN 284 NEED_TO_SHUTDOWN = ~CLEANLY_SHUTDOWN
273 }; 285 };
274 286
275 // The current state of recording for the MetricsService. The state is UNSET 287 // The current state of recording for the MetricsService. The state is UNSET
276 // until set to something else, at which point it remains INACTIVE or ACTIVE 288 // until set to something else, at which point it remains INACTIVE or ACTIVE
277 // for the lifetime of the object. 289 // for the lifetime of the object.
278 enum RecordingState { 290 enum RecordingState {
279 INACTIVE, 291 INACTIVE,
280 ACTIVE, 292 ACTIVE,
281 UNSET 293 UNSET
282 }; 294 };
283 295
296 typedef std::set<base::PersistentMemoryAllocator*> AllocatorSet;
297
298 // A class for iterating over the histograms in persistent memory segments.
299 // This is implemented as an STL-compatible iterator so it can be used in
300 // places that accept histograms both from here and from std containers.
301 class PersistentHistogramIterator {
302 // This class provides a reference-counted pointer to a histogram which
303 // is necessary for histogram objects that are dynamically generated
304 // (generally pointing to persistent memory) and to be owned by an STL
305 // iterator which, by necessity, must be copyable.
306 class HistogramPointer : public base::RefCounted<HistogramPointer> {
307 public:
308 HistogramPointer(base::HistogramBase* h) : histogram_(h) {}
309 base::HistogramBase* get() { return histogram_.get(); }
310
311 private:
312 scoped_ptr<base::HistogramBase> histogram_;
313 };
314
315 public:
316 PersistentHistogramIterator(AllocatorSet& allocators,
317 AllocatorSet::iterator pos);
318
319 PersistentHistogramIterator& operator++();
320 PersistentHistogramIterator operator++(int) {
321 PersistentHistogramIterator tmp(*this);
322 operator++();
323 return tmp;
324 }
325
326 bool operator==(const PersistentHistogramIterator& rhs) const {
327 return allocator_iter_ == rhs.allocator_iter_ &&
328 histogram_iter_ == rhs.histogram_iter_;
329 }
330 bool operator!=(const PersistentHistogramIterator& rhs) const {
331 return allocator_iter_ != rhs.allocator_iter_ ||
332 histogram_iter_ != rhs.histogram_iter_;
333 }
334 base::HistogramBase* operator*() {
335 return current_histogram_->get();
336 }
337
338 private:
339 AllocatorSet& allocators_;
340 AllocatorSet::iterator allocator_iter_;
341 base::PersistentMemoryAllocator::Iterator histogram_iter_;
342
343 // STL iterators must be copyable so a regular scoped_ptr is not
344 // sufficient as it doesn't allow such. A ref-counted one is needed.
345 scoped_refptr<HistogramPointer> current_histogram_;
346 };
347
284 typedef std::vector<SyntheticTrialGroup> SyntheticTrialGroups; 348 typedef std::vector<SyntheticTrialGroup> SyntheticTrialGroups;
285 349
286 // Calls into the client to initialize some system profile metrics. 350 // Calls into the client to initialize some system profile metrics.
287 void StartInitTask(); 351 void StartInitTask();
288 352
289 // Callback that moves the state to INIT_TASK_DONE. When this is called, the 353 // Callback that moves the state to INIT_TASK_DONE. When this is called, the
290 // state should be INIT_TASK_SCHEDULED. 354 // state should be INIT_TASK_SCHEDULED.
291 void FinishedInitTask(); 355 void FinishedInitTask();
292 356
293 void OnUserAction(const std::string& action); 357 void OnUserAction(const std::string& action);
(...skipping 120 matching lines...) Expand 10 before | Expand all | Expand 10 after
414 void RecordCurrentHistograms(); 478 void RecordCurrentHistograms();
415 479
416 // Record complete list of stability histograms into the current log, 480 // Record complete list of stability histograms into the current log,
417 // i.e., histograms with the |kUmaStabilityHistogramFlag| flag set. 481 // i.e., histograms with the |kUmaStabilityHistogramFlag| flag set.
418 void RecordCurrentStabilityHistograms(); 482 void RecordCurrentStabilityHistograms();
419 483
420 // Skips staged upload and discards the log. Used in case of unsuccessful 484 // Skips staged upload and discards the log. Used in case of unsuccessful
421 // upload or intentional sampling of logs. 485 // upload or intentional sampling of logs.
422 void SkipAndDiscardUpload(); 486 void SkipAndDiscardUpload();
423 487
488 // Begin and End iterators for going through all the metrics under management.
489 PersistentHistogramIterator persistent_begin();
490 PersistentHistogramIterator persistent_end();
491
424 // Manager for the various in-flight logs. 492 // Manager for the various in-flight logs.
425 MetricsLogManager log_manager_; 493 MetricsLogManager log_manager_;
426 494
427 // |histogram_snapshot_manager_| prepares histogram deltas for transmission. 495 // |histogram_snapshot_manager_| prepares histogram deltas for transmission.
428 base::HistogramSnapshotManager histogram_snapshot_manager_; 496 base::HistogramSnapshotManager histogram_snapshot_manager_;
429 497
430 // Used to manage various metrics reporting state prefs, such as client id, 498 // Used to manage various metrics reporting state prefs, such as client id,
431 // low entropy source and whether metrics reporting is enabled. Weak pointer. 499 // low entropy source and whether metrics reporting is enabled. Weak pointer.
432 MetricsStateManager* const state_manager_; 500 MetricsStateManager* const state_manager_;
433 501
(...skipping 61 matching lines...) Expand 10 before | Expand all | Expand 10 after
495 static ExecutionPhase execution_phase_; 563 static ExecutionPhase execution_phase_;
496 564
497 // Reduntant marker to check that we completed our shutdown, and set the 565 // Reduntant marker to check that we completed our shutdown, and set the
498 // exited-cleanly bit in the prefs. 566 // exited-cleanly bit in the prefs.
499 static ShutdownCleanliness clean_shutdown_status_; 567 static ShutdownCleanliness clean_shutdown_status_;
500 568
501 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess); 569 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, IsPluginProcess);
502 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, 570 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest,
503 PermutedEntropyCacheClearedWhenLowEntropyReset); 571 PermutedEntropyCacheClearedWhenLowEntropyReset);
504 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial); 572 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, RegisterSyntheticTrial);
573 FRIEND_TEST_ALL_PREFIXES(MetricsServiceTest, MultiplePersistentAllocators);
505 574
506 // Weak pointers factory used to post task on different threads. All weak 575 // Weak pointers factory used to post task on different threads. All weak
507 // pointers managed by this factory have the same lifetime as MetricsService. 576 // pointers managed by this factory have the same lifetime as MetricsService.
508 base::WeakPtrFactory<MetricsService> self_ptr_factory_; 577 base::WeakPtrFactory<MetricsService> self_ptr_factory_;
509 578
510 // Weak pointers factory used for saving state. All weak pointers managed by 579 // Weak pointers factory used for saving state. All weak pointers managed by
511 // this factory are invalidated in ScheduleNextStateSave. 580 // this factory are invalidated in ScheduleNextStateSave.
512 base::WeakPtrFactory<MetricsService> state_saver_factory_; 581 base::WeakPtrFactory<MetricsService> state_saver_factory_;
513 582
583 // Persistent memory segments that contain histograms created elsewhere.
584 AllocatorSet allocators_;
585
514 DISALLOW_COPY_AND_ASSIGN(MetricsService); 586 DISALLOW_COPY_AND_ASSIGN(MetricsService);
515 }; 587 };
516 588
517 } // namespace metrics 589 } // namespace metrics
518 590
519 #endif // COMPONENTS_METRICS_METRICS_SERVICE_H_ 591 #endif // COMPONENTS_METRICS_METRICS_SERVICE_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698