• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2021 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "components/metrics/structured/external_metrics.h"
6 
7 #include <sys/file.h>
8 
9 #include "base/containers/fixed_flat_set.h"
10 #include "base/files/file.h"
11 #include "base/files/file_enumerator.h"
12 #include "base/files/file_util.h"
13 #include "base/logging.h"
14 #include "base/strings/string_number_conversions.h"
15 #include "base/strings/string_split.h"
16 #include "base/task/sequenced_task_runner.h"
17 #include "base/task/task_traits.h"
18 #include "base/task/thread_pool.h"
19 #include "base/threading/scoped_blocking_call.h"
20 #include "components/metrics/structured/histogram_util.h"
21 #include "components/metrics/structured/storage.pb.h"
22 #include "components/metrics/structured/structured_metrics_features.h"
23 
24 namespace metrics {
25 namespace structured {
26 namespace {
27 
FilterEvents(google::protobuf::RepeatedPtrField<metrics::StructuredEventProto> * events,const base::flat_set<uint64_t> & disallowed_projects)28 void FilterEvents(
29     google::protobuf::RepeatedPtrField<metrics::StructuredEventProto>* events,
30     const base::flat_set<uint64_t>& disallowed_projects) {
31   auto it = events->begin();
32   while (it != events->end()) {
33     if (disallowed_projects.contains(it->project_name_hash())) {
34       it = events->erase(it);
35     } else {
36       ++it;
37     }
38   }
39 }
40 
41 // TODO(b/181724341): Remove this once the bluetooth metrics are fully enabled.
MaybeFilterBluetoothEvents(google::protobuf::RepeatedPtrField<metrics::StructuredEventProto> * events)42 void MaybeFilterBluetoothEvents(
43     google::protobuf::RepeatedPtrField<metrics::StructuredEventProto>* events) {
44   // Event name hashes of all bluetooth events listed in
45   // src/platform2/metrics/structured/structured.xml.
46   static constexpr auto kBluetoothEventHashes =
47       base::MakeFixedFlatSet<uint64_t>(
48           {// BluetoothAdapterStateChanged
49            UINT64_C(959829856916771459),
50            // BluetoothPairingStateChanged
51            UINT64_C(11839023048095184048),
52            // BluetoothAclConnectionStateChanged
53            UINT64_C(1880220404408566268),
54            // BluetoothProfileConnectionStateChanged
55            UINT64_C(7217682640379679663),
56            // BluetoothDeviceInfoReport
57            UINT64_C(1506471670382892394)});
58 
59   if (base::FeatureList::IsEnabled(kBluetoothSessionizedMetrics)) {
60     return;
61   }
62 
63   // Remove all bluetooth events.
64   auto it = events->begin();
65   while (it != events->end()) {
66     if (kBluetoothEventHashes.contains(it->event_name_hash())) {
67       it = events->erase(it);
68     } else {
69       ++it;
70     }
71   }
72 }
73 
FilterProto(EventsProto * proto,const base::flat_set<uint64_t> & disallowed_projects)74 bool FilterProto(EventsProto* proto,
75                  const base::flat_set<uint64_t>& disallowed_projects) {
76   FilterEvents(proto->mutable_uma_events(), disallowed_projects);
77   FilterEvents(proto->mutable_non_uma_events(), disallowed_projects);
78   return proto->uma_events_size() > 0 || proto->non_uma_events_size() > 0;
79 }
80 
ReadAndDeleteEvents(const base::FilePath & directory,const base::flat_set<uint64_t> & disallowed_projects)81 EventsProto ReadAndDeleteEvents(
82     const base::FilePath& directory,
83     const base::flat_set<uint64_t>& disallowed_projects) {
84   base::ScopedBlockingCall scoped_blocking_call(FROM_HERE,
85                                                 base::BlockingType::MAY_BLOCK);
86   EventsProto result;
87   if (!base::DirectoryExists(directory)) {
88     return result;
89   }
90 
91   base::FileEnumerator enumerator(directory, false,
92                                   base::FileEnumerator::FILES);
93   int file_counter = 0;
94 
95   for (base::FilePath path = enumerator.Next(); !path.empty();
96        path = enumerator.Next()) {
97     std::string proto_str;
98     int64_t file_size;
99     EventsProto proto;
100 
101     ++file_counter;
102 
103     // There may be too many messages in the directory to hold in-memory. This
104     // could happen if the process in which Structured metrics resides is
105     // either crash-looping or taking too long to process externally recorded
106     // events.
107     //
108     // Events will be dropped in that case so that more recent events can be
109     // processed.
110     if (file_counter > GetFileLimitPerScan()) {
111       base::DeleteFile(path);
112       continue;
113     }
114 
115     // If an event is abnormally large, ignore it to prevent OOM.
116     bool fs_ok = base::GetFileSize(path, &file_size);
117 
118     // If file size get is successful, log the file size.
119     if (fs_ok) {
120       LogEventFileSizeKB(static_cast<int>(file_size / 1024));
121     }
122 
123     if (!fs_ok || file_size > GetFileSizeByteLimit()) {
124       base::DeleteFile(path);
125       continue;
126     }
127 
128     bool read_ok = base::ReadFileToString(path, &proto_str) &&
129                    proto.ParseFromString(proto_str);
130     base::DeleteFile(path);
131 
132     if (!read_ok) {
133       continue;
134     }
135 
136     if (!FilterProto(&proto, disallowed_projects)) {
137       continue;
138     }
139 
140     // MergeFrom performs a copy that could be a move if done manually. But
141     // all the protos here are expected to be small, so let's keep it simple.
142     result.mutable_uma_events()->MergeFrom(proto.uma_events());
143     result.mutable_non_uma_events()->MergeFrom(proto.non_uma_events());
144   }
145 
146   LogNumFilesPerExternalMetricsScan(file_counter);
147 
148   MaybeFilterBluetoothEvents(result.mutable_uma_events());
149   MaybeFilterBluetoothEvents(result.mutable_non_uma_events());
150   return result;
151 }
152 
153 }  // namespace
154 
ExternalMetrics(const base::FilePath & events_directory,const base::TimeDelta & collection_interval,MetricsCollectedCallback callback)155 ExternalMetrics::ExternalMetrics(const base::FilePath& events_directory,
156                                  const base::TimeDelta& collection_interval,
157                                  MetricsCollectedCallback callback)
158     : events_directory_(events_directory),
159       collection_interval_(collection_interval),
160       callback_(std::move(callback)),
161       task_runner_(base::ThreadPool::CreateSequencedTaskRunner(
162           {base::TaskPriority::BEST_EFFORT, base::MayBlock(),
163            base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN})) {
164   ScheduleCollector();
165   CacheDisallowedProjectsSet();
166 }
167 
168 ExternalMetrics::~ExternalMetrics() = default;
169 
CollectEventsAndReschedule()170 void ExternalMetrics::CollectEventsAndReschedule() {
171   CollectEvents();
172   ScheduleCollector();
173 }
174 
ScheduleCollector()175 void ExternalMetrics::ScheduleCollector() {
176   base::SequencedTaskRunner::GetCurrentDefault()->PostDelayedTask(
177       FROM_HERE,
178       base::BindOnce(&ExternalMetrics::CollectEventsAndReschedule,
179                      weak_factory_.GetWeakPtr()),
180       collection_interval_);
181 }
182 
CollectEvents()183 void ExternalMetrics::CollectEvents() {
184   task_runner_->PostTaskAndReplyWithResult(
185       FROM_HERE,
186       base::BindOnce(&ReadAndDeleteEvents, events_directory_,
187                      disallowed_projects_),
188       base::BindOnce(callback_));
189 }
190 
CacheDisallowedProjectsSet()191 void ExternalMetrics::CacheDisallowedProjectsSet() {
192   const std::string& disallowed_list = GetDisabledProjects();
193   if (disallowed_list.empty()) {
194     return;
195   }
196 
197   for (const auto& value :
198        base::SplitString(disallowed_list, ",", base::TRIM_WHITESPACE,
199                          base::SPLIT_WANT_NONEMPTY)) {
200     uint64_t project_name_hash;
201     // Parse the string and keep only perfect conversions.
202     if (base::StringToUint64(value, &project_name_hash)) {
203       disallowed_projects_.insert(project_name_hash);
204     }
205   }
206 }
207 
AddDisallowedProjectForTest(uint64_t project_name_hash)208 void ExternalMetrics::AddDisallowedProjectForTest(uint64_t project_name_hash) {
209   disallowed_projects_.insert(project_name_hash);
210 }
211 
212 }  // namespace structured
213 }  // namespace metrics
214