blob: 8b868fb584a2b69af9eb2d6e38e509e9e29e4d97 [file] [log] [blame]
Jeff Sharkey88ddd942017-01-17 18:05:54 -07001/*
2 * Copyright (C) 2017 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#define ATRACE_TAG ATRACE_TAG_PACKAGE_MANAGER
18
19#include "CacheTracker.h"
20
21#include <fts.h>
Jeff Sharkey871a8f22017-02-21 18:30:28 -070022#include <sys/xattr.h>
Jeff Sharkey88ddd942017-01-17 18:05:54 -070023#include <utils/Trace.h>
24
25#include <android-base/logging.h>
26#include <android-base/stringprintf.h>
27
Risan5f308262018-10-26 12:06:58 -060028#include "QuotaUtils.h"
Jeff Sharkey88ddd942017-01-17 18:05:54 -070029#include "utils.h"
30
31using android::base::StringPrintf;
32
33namespace android {
34namespace installd {
35
Risan5f308262018-10-26 12:06:58 -060036CacheTracker::CacheTracker(userid_t userId, appid_t appId, const std::string& uuid)
37 : cacheUsed(0),
38 cacheQuota(0),
39 mUserId(userId),
40 mAppId(appId),
41 mItemsLoaded(false),
42 mUuid(uuid) {
Jeff Sharkey88ddd942017-01-17 18:05:54 -070043}
44
45CacheTracker::~CacheTracker() {
46}
47
48std::string CacheTracker::toString() {
49 return StringPrintf("UID=%d used=%" PRId64 " quota=%" PRId64 " ratio=%d",
50 multiuser_get_uid(mUserId, mAppId), cacheUsed, cacheQuota, getCacheRatio());
51}
52
53void CacheTracker::addDataPath(const std::string& dataPath) {
54 mDataPaths.push_back(dataPath);
55}
56
57void CacheTracker::loadStats() {
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060058 ATRACE_BEGIN("loadStats quota");
59 cacheUsed = 0;
60 if (loadQuotaStats()) {
61 return;
Jeff Sharkey88ddd942017-01-17 18:05:54 -070062 }
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060063 ATRACE_END();
Jeff Sharkey88ddd942017-01-17 18:05:54 -070064
65 ATRACE_BEGIN("loadStats tree");
66 cacheUsed = 0;
Chih-Hung Hsieh0e1f4ce2017-08-03 15:48:25 -070067 for (const auto& path : mDataPaths) {
Jeff Sharkey88ddd942017-01-17 18:05:54 -070068 auto cachePath = read_path_inode(path, "cache", kXattrInodeCache);
69 auto codeCachePath = read_path_inode(path, "code_cache", kXattrInodeCodeCache);
70 calculate_tree_size(cachePath, &cacheUsed);
71 calculate_tree_size(codeCachePath, &cacheUsed);
72 }
73 ATRACE_END();
74}
75
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060076bool CacheTracker::loadQuotaStats() {
77 int cacheGid = multiuser_get_cache_gid(mUserId, mAppId);
78 int extCacheGid = multiuser_get_ext_cache_gid(mUserId, mAppId);
Risan5f308262018-10-26 12:06:58 -060079 if (IsQuotaSupported(mUuid) && cacheGid != -1 && extCacheGid != -1) {
80 int64_t space;
81 if ((space = GetOccupiedSpaceForGid(mUuid, cacheGid)) != -1) {
82 cacheUsed += space;
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060083 } else {
Risan5f308262018-10-26 12:06:58 -060084 return false;
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060085 }
86
Risan5f308262018-10-26 12:06:58 -060087 if ((space = GetOccupiedSpaceForGid(mUuid, extCacheGid)) != -1) {
88 cacheUsed += space;
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060089 } else {
Risan5f308262018-10-26 12:06:58 -060090 return false;
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060091 }
92 return true;
93 } else {
94 return false;
95 }
96}
97
Jeff Sharkey88ddd942017-01-17 18:05:54 -070098void CacheTracker::loadItemsFrom(const std::string& path) {
99 FTS *fts;
100 FTSENT *p;
101 char *argv[] = { (char*) path.c_str(), nullptr };
Yi Kong954cf642018-07-17 16:16:24 -0700102 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) {
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700103 PLOG(WARNING) << "Failed to fts_open " << path;
104 return;
105 }
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700106 while ((p = fts_read(fts)) != nullptr) {
107 if (p->fts_level == 0) continue;
108
109 // Create tracking nodes for everything we encounter
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700110 switch (p->fts_info) {
111 case FTS_D:
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700112 case FTS_DEFAULT:
113 case FTS_F:
114 case FTS_SL:
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700115 case FTS_SLNONE: {
116 auto item = std::shared_ptr<CacheItem>(new CacheItem(p));
117 p->fts_pointer = static_cast<void*>(item.get());
118 items.push_back(item);
119 }
120 }
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700121
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700122 switch (p->fts_info) {
123 case FTS_D: {
124 auto item = static_cast<CacheItem*>(p->fts_pointer);
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600125 item->group |= (getxattr(p->fts_path, kXattrCacheGroup, nullptr, 0) >= 0);
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700126 item->tombstone |= (getxattr(p->fts_path, kXattrCacheTombstone, nullptr, 0) >= 0);
127
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600128 // When group, immediately collect all files under tree
129 if (item->group) {
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700130 while ((p = fts_read(fts)) != nullptr) {
131 if (p->fts_info == FTS_DP && p->fts_level == item->level) break;
132 switch (p->fts_info) {
133 case FTS_D:
134 case FTS_DEFAULT:
135 case FTS_F:
136 case FTS_SL:
137 case FTS_SLNONE:
138 item->size += p->fts_statp->st_blocks * 512;
139 item->modified = std::max(item->modified, p->fts_statp->st_mtime);
140 }
141 }
142 }
143 }
144 }
145
146 // Bubble up modified time to parent
Luis A. Lozano682e57b2017-06-06 16:59:37 -0700147 CHECK(p != nullptr);
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700148 switch (p->fts_info) {
149 case FTS_DP:
150 case FTS_DEFAULT:
151 case FTS_F:
152 case FTS_SL:
153 case FTS_SLNONE: {
154 auto item = static_cast<CacheItem*>(p->fts_pointer);
155 auto parent = static_cast<CacheItem*>(p->fts_parent->fts_pointer);
156 if (parent) {
157 parent->modified = std::max(parent->modified, item->modified);
158 }
159 }
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700160 }
161 }
162 fts_close(fts);
163}
164
165void CacheTracker::loadItems() {
166 items.clear();
167
168 ATRACE_BEGIN("loadItems");
Chih-Hung Hsieh0e1f4ce2017-08-03 15:48:25 -0700169 for (const auto& path : mDataPaths) {
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700170 loadItemsFrom(read_path_inode(path, "cache", kXattrInodeCache));
171 loadItemsFrom(read_path_inode(path, "code_cache", kXattrInodeCodeCache));
172 }
173 ATRACE_END();
174
175 ATRACE_BEGIN("sortItems");
176 auto cmp = [](std::shared_ptr<CacheItem> left, std::shared_ptr<CacheItem> right) {
177 // TODO: sort dotfiles last
178 // TODO: sort code_cache last
179 if (left->modified != right->modified) {
180 return (left->modified > right->modified);
181 }
182 if (left->level != right->level) {
183 return (left->level < right->level);
184 }
185 return left->directory;
186 };
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700187 std::stable_sort(items.begin(), items.end(), cmp);
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700188 ATRACE_END();
189}
190
191void CacheTracker::ensureItems() {
192 if (mItemsLoaded) {
193 return;
194 } else {
195 loadItems();
196 mItemsLoaded = true;
197 }
198}
199
200int CacheTracker::getCacheRatio() {
201 if (cacheQuota == 0) {
202 return 0;
203 } else {
204 return (cacheUsed * 10000) / cacheQuota;
205 }
206}
207
208} // namespace installd
209} // namespace android