blob: a61f6bf87b16585a25c24c5c6af5c60794b00a43 [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);
Martijn Coenen771cc342020-02-19 23:26:56 +010078 if (IsQuotaSupported(mUuid) && cacheGid != -1) {
Risan5f308262018-10-26 12:06:58 -060079 int64_t space;
80 if ((space = GetOccupiedSpaceForGid(mUuid, cacheGid)) != -1) {
81 cacheUsed += space;
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060082 } else {
Risan5f308262018-10-26 12:06:58 -060083 return false;
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060084 }
85
Martijn Coenen771cc342020-02-19 23:26:56 +010086 if ((space = get_occupied_app_cache_space_external(mUuid, mUserId, mAppId)) != -1) {
Risan5f308262018-10-26 12:06:58 -060087 cacheUsed += space;
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060088 } else {
Risan5f308262018-10-26 12:06:58 -060089 return false;
Jeff Sharkeyd712f0c2017-05-03 11:36:42 -060090 }
91 return true;
92 } else {
93 return false;
94 }
95}
96
Jeff Sharkey88ddd942017-01-17 18:05:54 -070097void CacheTracker::loadItemsFrom(const std::string& path) {
98 FTS *fts;
99 FTSENT *p;
100 char *argv[] = { (char*) path.c_str(), nullptr };
Yi Kong954cf642018-07-17 16:16:24 -0700101 if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) {
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700102 PLOG(WARNING) << "Failed to fts_open " << path;
103 return;
104 }
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700105 while ((p = fts_read(fts)) != nullptr) {
106 if (p->fts_level == 0) continue;
107
108 // Create tracking nodes for everything we encounter
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700109 switch (p->fts_info) {
110 case FTS_D:
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700111 case FTS_DEFAULT:
112 case FTS_F:
113 case FTS_SL:
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700114 case FTS_SLNONE: {
115 auto item = std::shared_ptr<CacheItem>(new CacheItem(p));
116 p->fts_pointer = static_cast<void*>(item.get());
117 items.push_back(item);
118 }
119 }
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700120
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700121 switch (p->fts_info) {
122 case FTS_D: {
123 auto item = static_cast<CacheItem*>(p->fts_pointer);
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600124 item->group |= (getxattr(p->fts_path, kXattrCacheGroup, nullptr, 0) >= 0);
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700125 item->tombstone |= (getxattr(p->fts_path, kXattrCacheTombstone, nullptr, 0) >= 0);
126
Jeff Sharkeyed909ae2017-03-22 21:27:40 -0600127 // When group, immediately collect all files under tree
128 if (item->group) {
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700129 while ((p = fts_read(fts)) != nullptr) {
130 if (p->fts_info == FTS_DP && p->fts_level == item->level) break;
131 switch (p->fts_info) {
132 case FTS_D:
133 case FTS_DEFAULT:
134 case FTS_F:
135 case FTS_SL:
136 case FTS_SLNONE:
137 item->size += p->fts_statp->st_blocks * 512;
138 item->modified = std::max(item->modified, p->fts_statp->st_mtime);
139 }
140 }
141 }
142 }
143 }
144
145 // Bubble up modified time to parent
Luis A. Lozano682e57b2017-06-06 16:59:37 -0700146 CHECK(p != nullptr);
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700147 switch (p->fts_info) {
148 case FTS_DP:
149 case FTS_DEFAULT:
150 case FTS_F:
151 case FTS_SL:
152 case FTS_SLNONE: {
153 auto item = static_cast<CacheItem*>(p->fts_pointer);
154 auto parent = static_cast<CacheItem*>(p->fts_parent->fts_pointer);
155 if (parent) {
156 parent->modified = std::max(parent->modified, item->modified);
157 }
158 }
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700159 }
160 }
161 fts_close(fts);
162}
163
164void CacheTracker::loadItems() {
165 items.clear();
166
167 ATRACE_BEGIN("loadItems");
Chih-Hung Hsieh0e1f4ce2017-08-03 15:48:25 -0700168 for (const auto& path : mDataPaths) {
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700169 loadItemsFrom(read_path_inode(path, "cache", kXattrInodeCache));
170 loadItemsFrom(read_path_inode(path, "code_cache", kXattrInodeCodeCache));
171 }
172 ATRACE_END();
173
174 ATRACE_BEGIN("sortItems");
175 auto cmp = [](std::shared_ptr<CacheItem> left, std::shared_ptr<CacheItem> right) {
176 // TODO: sort dotfiles last
177 // TODO: sort code_cache last
178 if (left->modified != right->modified) {
179 return (left->modified > right->modified);
180 }
181 if (left->level != right->level) {
182 return (left->level < right->level);
183 }
184 return left->directory;
185 };
Jeff Sharkey871a8f22017-02-21 18:30:28 -0700186 std::stable_sort(items.begin(), items.end(), cmp);
Jeff Sharkey88ddd942017-01-17 18:05:54 -0700187 ATRACE_END();
188}
189
190void CacheTracker::ensureItems() {
191 if (mItemsLoaded) {
192 return;
193 } else {
194 loadItems();
195 mItemsLoaded = true;
196 }
197}
198
199int CacheTracker::getCacheRatio() {
200 if (cacheQuota == 0) {
201 return 0;
202 } else {
203 return (cacheUsed * 10000) / cacheQuota;
204 }
205}
206
207} // namespace installd
208} // namespace android