blob: e2a4b040306620839c8412b99e11dfcbe2a7439d [file] [log] [blame]
James Hawkinsabd73e62016-01-19 15:10:38 -08001/*
2 * Copyright (C) 2016 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 "boot_event_record_store.h"
18
19#include <dirent.h>
20#include <fcntl.h>
21#include <sys/stat.h>
22#include <utime.h>
James Hawkinse78ea772017-03-24 11:43:02 -070023
24#include <chrono>
James Hawkinsabd73e62016-01-19 15:10:38 -080025#include <cstdlib>
James Hawkins6f282992016-03-22 14:13:06 -070026#include <string>
James Hawkins5cd4bc22016-01-22 11:07:51 -080027#include <utility>
James Hawkinse78ea772017-03-24 11:43:02 -070028
29#include <android-base/chrono_utils.h>
James Hawkinseabe08b2016-01-19 16:54:35 -080030#include <android-base/file.h>
31#include <android-base/logging.h>
James Hawkins4dded612016-07-28 11:50:23 -070032#include <android-base/parseint.h>
James Hawkinsabd73e62016-01-19 15:10:38 -080033
34namespace {
35
36const char BOOTSTAT_DATA_DIR[] = "/data/misc/bootstat/";
37
38// Given a boot even record file at |path|, extracts the event's relative time
39// from the record into |uptime|.
40bool ParseRecordEventTime(const std::string& path, int32_t* uptime) {
41 DCHECK_NE(static_cast<int32_t*>(nullptr), uptime);
42
43 struct stat file_stat;
44 if (stat(path.c_str(), &file_stat) == -1) {
45 PLOG(ERROR) << "Failed to read " << path;
46 return false;
47 }
48
49 *uptime = file_stat.st_mtime;
James Hawkins6f282992016-03-22 14:13:06 -070050
James Hawkinsabd73e62016-01-19 15:10:38 -080051 return true;
52}
53
54} // namespace
55
56BootEventRecordStore::BootEventRecordStore() {
57 SetStorePath(BOOTSTAT_DATA_DIR);
58}
59
James Hawkins500d7152016-02-16 15:05:54 -080060void BootEventRecordStore::AddBootEvent(const std::string& event) {
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -070061 auto uptime = std::chrono::duration_cast<std::chrono::seconds>(
62 android::base::boot_clock::now().time_since_epoch());
63 AddBootEventWithValue(event, uptime.count());
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080064}
65
66// The implementation of AddBootEventValue makes use of the mtime file
67// attribute to store the value associated with a boot event in order to
68// optimize on-disk size requirements and small-file thrashing.
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -070069void BootEventRecordStore::AddBootEventWithValue(const std::string& event, int32_t value) {
James Hawkins500d7152016-02-16 15:05:54 -080070 std::string record_path = GetBootEventPath(event);
James Hawkins6f282992016-03-22 14:13:06 -070071 int record_fd = creat(record_path.c_str(), S_IRUSR | S_IWUSR);
72 if (record_fd == -1) {
James Hawkinsabd73e62016-01-19 15:10:38 -080073 PLOG(ERROR) << "Failed to create " << record_path;
James Hawkins6f282992016-03-22 14:13:06 -070074 return;
75 }
76
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080077 // Fill out the stat structure for |record_path| in order to get the atime to
78 // set in the utime() call.
James Hawkinsabd73e62016-01-19 15:10:38 -080079 struct stat file_stat;
80 if (stat(record_path.c_str(), &file_stat) == -1) {
81 PLOG(ERROR) << "Failed to read " << record_path;
James Hawkins6f282992016-03-22 14:13:06 -070082 close(record_fd);
83 return;
James Hawkinsabd73e62016-01-19 15:10:38 -080084 }
85
James Hawkinsa4a1a4a2016-02-09 15:32:38 -080086 // Set the |modtime| of the file to store the value of the boot event while
87 // preserving the |actime| (as read by stat).
88 struct utimbuf times = {/* actime */ file_stat.st_atime, /* modtime */ value};
James Hawkinsabd73e62016-01-19 15:10:38 -080089 if (utime(record_path.c_str(), &times) == -1) {
90 PLOG(ERROR) << "Failed to set mtime for " << record_path;
James Hawkins6f282992016-03-22 14:13:06 -070091 close(record_fd);
92 return;
James Hawkinsabd73e62016-01-19 15:10:38 -080093 }
James Hawkins6f282992016-03-22 14:13:06 -070094
95 close(record_fd);
James Hawkinsabd73e62016-01-19 15:10:38 -080096}
97
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -070098bool BootEventRecordStore::GetBootEvent(const std::string& event, BootEventRecord* record) const {
James Hawkins500d7152016-02-16 15:05:54 -080099 CHECK_NE(static_cast<BootEventRecord*>(nullptr), record);
100 CHECK(!event.empty());
101
102 const std::string record_path = GetBootEventPath(event);
103 int32_t uptime;
104 if (!ParseRecordEventTime(record_path, &uptime)) {
105 LOG(ERROR) << "Failed to parse boot time record: " << record_path;
106 return false;
107 }
108
109 *record = std::make_pair(event, uptime);
110 return true;
111}
112
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700113std::vector<BootEventRecordStore::BootEventRecord> BootEventRecordStore::GetAllBootEvents() const {
James Hawkinsabd73e62016-01-19 15:10:38 -0800114 std::vector<BootEventRecord> events;
115
116 std::unique_ptr<DIR, decltype(&closedir)> dir(opendir(store_path_.c_str()), closedir);
117
118 // This case could happen due to external manipulation of the filesystem,
119 // so crash out if the record store doesn't exist.
120 CHECK_NE(static_cast<DIR*>(nullptr), dir.get());
121
122 struct dirent* entry;
123 while ((entry = readdir(dir.get())) != NULL) {
124 // Only parse regular files.
125 if (entry->d_type != DT_REG) {
126 continue;
127 }
128
129 const std::string event = entry->d_name;
James Hawkins500d7152016-02-16 15:05:54 -0800130 BootEventRecord record;
131 if (!GetBootEvent(event, &record)) {
132 LOG(ERROR) << "Failed to parse boot time event: " << event;
James Hawkinsabd73e62016-01-19 15:10:38 -0800133 continue;
134 }
135
James Hawkins500d7152016-02-16 15:05:54 -0800136 events.push_back(record);
James Hawkinsabd73e62016-01-19 15:10:38 -0800137 }
138
139 return events;
140}
141
142void BootEventRecordStore::SetStorePath(const std::string& path) {
143 DCHECK_EQ('/', path.back());
144 store_path_ = path;
145}
146
Mark Salyzyn14b1e6d2017-09-18 10:41:14 -0700147std::string BootEventRecordStore::GetBootEventPath(const std::string& event) const {
James Hawkinsabd73e62016-01-19 15:10:38 -0800148 DCHECK_EQ('/', store_path_.back());
149 return store_path_ + event;
150}