[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 1 | // Copyright (c) 2010 The Chromium Authors. All rights reserved. |
2 | // Use of this source code is governed by a BSD-style license that can be | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 3 | // found in the LICENSE file. |
4 | |||||
5 | #ifndef CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_ | ||||
6 | #define CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_ | ||||
7 | |||||
[email protected] | f412499 | 2013-06-20 03:34:17 | [diff] [blame] | 8 | #include <string> |
9 | |||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 10 | #include "base/compiler_specific.h" |
[email protected] | 3c1ec41 | 2010-06-02 15:22:56 | [diff] [blame] | 11 | #include "base/gtest_prod_util.h" |
avi | 8a07d5389 | 2015-12-24 22:13:53 | [diff] [blame] | 12 | #include "base/macros.h" |
[email protected] | c694427 | 2012-01-06 22:12:28 | [diff] [blame] | 13 | #include "base/memory/ref_counted.h" |
Daniel Erat | a3d4e9c3 | 2018-09-05 20:36:13 | [diff] [blame] | 14 | #include "base/time/time.h" |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 15 | |
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 16 | namespace metrics { |
17 | class MetricSample; | ||||
18 | } // namespace metrics | ||||
19 | |||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 20 | namespace chromeos { |
21 | |||||
22 | // ExternalMetrics is a service that Chrome offers to Chrome OS to upload | ||||
23 | // metrics to the UMA server on its behalf. Chrome periodically reads the | ||||
24 | // content of a well-know file, and parses it into name-value pairs, each | ||||
25 | // representing a Chrome OS metrics event. The events are logged using the | ||||
26 | // normal UMA mechanism. The file is then truncated to zero size. Chrome uses | ||||
27 | // flock() to synchronize accesses to the file. | ||||
28 | class ExternalMetrics : public base::RefCountedThreadSafe<ExternalMetrics> { | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 29 | public: |
[email protected] | 196ff47 | 2011-01-21 19:25:27 | [diff] [blame] | 30 | ExternalMetrics(); |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 31 | |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 32 | // Begins the external data collection. This service is started and stopped |
33 | // by the chrome metrics service. Calls to RecordAction originate in the | ||||
hashimoto | d562a72 | 2015-04-10 13:44:27 | [diff] [blame] | 34 | // blocking pool but are executed in the UI thread. |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 35 | void Start(); |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 36 | |
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 37 | // Creates an ExternalMetrics instance reading from |filename| for testing |
38 | // purpose. | ||||
39 | static scoped_refptr<ExternalMetrics> CreateForTesting( | ||||
40 | const std::string& filename); | ||||
41 | |||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 42 | private: |
[email protected] | 9faa02dd | 2013-07-03 04:34:13 | [diff] [blame] | 43 | friend class base::RefCountedThreadSafe<ExternalMetrics>; |
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 44 | friend class ExternalMetricsTest; |
[email protected] | 9faa02dd | 2013-07-03 04:34:13 | [diff] [blame] | 45 | |
Daniel Erat | a3d4e9c3 | 2018-09-05 20:36:13 | [diff] [blame] | 46 | FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, CustomInterval); |
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 47 | FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, HandleMissingFile); |
Daniel Erat | a3d4e9c3 | 2018-09-05 20:36:13 | [diff] [blame] | 48 | FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, CanReceiveHistogram); |
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 49 | FRIEND_TEST_ALL_PREFIXES(ExternalMetricsTest, |
50 | IncorrectHistogramsAreDiscarded); | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 51 | |
52 | // The max length of a message (name-value pair, plus header) | ||||
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 53 | static const int kMetricsMessageMaxLength = 1024; // be generous |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 54 | |
[email protected] | 41baad0 | 2011-05-15 20:37:46 | [diff] [blame] | 55 | ~ExternalMetrics(); |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 56 | |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 57 | // Passes an action event to the UMA service on the UI thread. |
isherman | de9efab | 2015-03-31 00:01:25 | [diff] [blame] | 58 | void RecordActionUI(const std::string& action_string); |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 59 | |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 60 | // Passes an action event to the UMA service. |
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 61 | void RecordAction(const std::string& action_name); |
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 62 | |
[email protected] | c1834a9 | 2011-01-21 18:21:03 | [diff] [blame] | 63 | // Records an external crash of the given string description to |
64 | // UMA service on the UI thread. | ||||
65 | void RecordCrashUI(const std::string& crash_kind); | ||||
66 | |||||
67 | // Records an external crash of the given string description. | ||||
68 | void RecordCrash(const std::string& crash_kind); | ||||
69 | |||||
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 70 | // Records an histogram. |sample| is expected to be an histogram. |
71 | void RecordHistogram(const metrics::MetricSample& sample); | ||||
[email protected] | 29cf1677 | 2010-04-21 15:13:47 | [diff] [blame] | 72 | |
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 73 | // Records a sparse histogram. |sample| is expected to be a sparse histogram. |
74 | void RecordSparseHistogram(const metrics::MetricSample& sample); | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 75 | |
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 76 | // Records a linear histogram. |sample| is expected to be a linear histogram. |
77 | void RecordLinearHistogram(const metrics::MetricSample& sample); | ||||
[email protected] | 85bbf09 | 2013-05-07 08:23:40 | [diff] [blame] | 78 | |
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 79 | // Collects external events from metrics log file. This is run at periodic |
80 | // intervals. | ||||
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 81 | // |
82 | // Returns the number of events collected. | ||||
83 | int CollectEvents(); | ||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 84 | |
85 | // Calls CollectEvents and reschedules a future collection. | ||||
86 | void CollectEventsAndReschedule(); | ||||
87 | |||||
88 | // Schedules a metrics event collection in the future. | ||||
89 | void ScheduleCollector(); | ||||
90 | |||||
[email protected] | d5985de | 2014-05-28 15:24:48 | [diff] [blame] | 91 | // File used by libmetrics to send metrics to Chrome. |
92 | std::string uma_events_file_; | ||||
[email protected] | 9faa02dd | 2013-07-03 04:34:13 | [diff] [blame] | 93 | |
Daniel Erat | a3d4e9c3 | 2018-09-05 20:36:13 | [diff] [blame] | 94 | // Interval between metrics being read from |uma_events_file_|. |
95 | base::TimeDelta collection_interval_; | ||||
96 | |||||
[email protected] | 5ccaa41 | 2009-11-13 22:00:16 | [diff] [blame] | 97 | DISALLOW_COPY_AND_ASSIGN(ExternalMetrics); |
98 | }; | ||||
99 | |||||
100 | } // namespace chromeos | ||||
101 | |||||
102 | #endif // CHROME_BROWSER_CHROMEOS_EXTERNAL_METRICS_H_ |