1 /*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "collectors/disk_usage_collector.h"
18
19 #include <base/bind.h>
20 #include <base/bind_helpers.h>
21 #include <base/message_loop/message_loop.h>
22 #include <sys/statvfs.h>
23
24 #include "metrics/metrics_library.h"
25
26 namespace {
27
28 const char kDiskUsageMB[] = "Platform.DataPartitionUsed.MB";
29 const char kDiskUsagePercent[] = "Platform.DataPartitionUsed.Percent";
30 const char kDataPartitionPath[] = "/data";
31
32 // Collect every 15 minutes.
33 const int kDiskUsageCollectorIntervalSeconds = 900;
34
35 } // namespace
36
DiskUsageCollector(MetricsLibraryInterface * metrics_library)37 DiskUsageCollector::DiskUsageCollector(
38 MetricsLibraryInterface* metrics_library) {
39 collect_interval_ = base::TimeDelta::FromSeconds(
40 kDiskUsageCollectorIntervalSeconds);
41 CHECK(metrics_library);
42 metrics_lib_ = metrics_library;
43 }
44
Collect()45 void DiskUsageCollector::Collect() {
46 struct statvfs buf;
47 int result = statvfs(kDataPartitionPath, &buf);
48 if (result != 0) {
49 PLOG(ERROR) << "Failed to check the available space in "
50 << kDataPartitionPath;
51 return;
52 }
53
54 unsigned long total_space = buf.f_blocks * buf.f_bsize;
55 unsigned long used_space = (buf.f_blocks - buf.f_bfree) * buf.f_bsize;
56 int percent_used = (used_space * 100) / total_space;
57
58 metrics_lib_->SendToUMA(kDiskUsageMB,
59 used_space / (1024 * 1024),
60 0,
61 1024, // up to 1 GB.
62 100);
63 metrics_lib_->SendEnumToUMA(kDiskUsagePercent, percent_used, 101);
64 }
65
CollectCallback()66 void DiskUsageCollector::CollectCallback() {
67 Collect();
68 Schedule();
69 }
70
Schedule()71 void DiskUsageCollector::Schedule() {
72 base::MessageLoop::current()->PostDelayedTask(FROM_HERE,
73 base::Bind(&DiskUsageCollector::CollectCallback, base::Unretained(this)),
74 collect_interval_);
75 }
76