| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 1 | /* | 
|  | 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> | 
|  | 22 | #include <sys/quota.h> | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 23 | #include <sys/xattr.h> | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 24 | #include <utils/Trace.h> | 
|  | 25 |  | 
|  | 26 | #include <android-base/logging.h> | 
|  | 27 | #include <android-base/stringprintf.h> | 
|  | 28 |  | 
|  | 29 | #include "utils.h" | 
|  | 30 |  | 
|  | 31 | using android::base::StringPrintf; | 
|  | 32 |  | 
|  | 33 | namespace android { | 
|  | 34 | namespace installd { | 
|  | 35 |  | 
|  | 36 | CacheTracker::CacheTracker(userid_t userId, appid_t appId, const std::string& quotaDevice) : | 
|  | 37 | cacheUsed(0), cacheQuota(0), mUserId(userId), mAppId(appId), mQuotaDevice(quotaDevice), | 
|  | 38 | mItemsLoaded(false) { | 
|  | 39 | } | 
|  | 40 |  | 
|  | 41 | CacheTracker::~CacheTracker() { | 
|  | 42 | } | 
|  | 43 |  | 
|  | 44 | std::string CacheTracker::toString() { | 
|  | 45 | return StringPrintf("UID=%d used=%" PRId64 " quota=%" PRId64 " ratio=%d", | 
|  | 46 | multiuser_get_uid(mUserId, mAppId), cacheUsed, cacheQuota, getCacheRatio()); | 
|  | 47 | } | 
|  | 48 |  | 
|  | 49 | void CacheTracker::addDataPath(const std::string& dataPath) { | 
|  | 50 | mDataPaths.push_back(dataPath); | 
|  | 51 | } | 
|  | 52 |  | 
|  | 53 | void CacheTracker::loadStats() { | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 54 | ATRACE_BEGIN("loadStats quota"); | 
|  | 55 | cacheUsed = 0; | 
|  | 56 | if (loadQuotaStats()) { | 
|  | 57 | return; | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 58 | } | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 59 | ATRACE_END(); | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 60 |  | 
|  | 61 | ATRACE_BEGIN("loadStats tree"); | 
|  | 62 | cacheUsed = 0; | 
| Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 63 | for (const auto& path : mDataPaths) { | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 64 | auto cachePath = read_path_inode(path, "cache", kXattrInodeCache); | 
|  | 65 | auto codeCachePath = read_path_inode(path, "code_cache", kXattrInodeCodeCache); | 
|  | 66 | calculate_tree_size(cachePath, &cacheUsed); | 
|  | 67 | calculate_tree_size(codeCachePath, &cacheUsed); | 
|  | 68 | } | 
|  | 69 | ATRACE_END(); | 
|  | 70 | } | 
|  | 71 |  | 
| Jeff Sharkey | d712f0c | 2017-05-03 11:36:42 -0600 | [diff] [blame] | 72 | bool CacheTracker::loadQuotaStats() { | 
|  | 73 | int cacheGid = multiuser_get_cache_gid(mUserId, mAppId); | 
|  | 74 | int extCacheGid = multiuser_get_ext_cache_gid(mUserId, mAppId); | 
|  | 75 | if (!mQuotaDevice.empty() && cacheGid != -1 && extCacheGid != -1) { | 
|  | 76 | struct dqblk dq; | 
|  | 77 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), mQuotaDevice.c_str(), cacheGid, | 
|  | 78 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 79 | if (errno != ESRCH) { | 
|  | 80 | PLOG(ERROR) << "Failed to quotactl " << mQuotaDevice << " for GID " << cacheGid; | 
|  | 81 | } | 
|  | 82 | return false; | 
|  | 83 | } else { | 
|  | 84 | cacheUsed += dq.dqb_curspace; | 
|  | 85 | } | 
|  | 86 |  | 
|  | 87 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), mQuotaDevice.c_str(), extCacheGid, | 
|  | 88 | reinterpret_cast<char*>(&dq)) != 0) { | 
|  | 89 | if (errno != ESRCH) { | 
|  | 90 | PLOG(ERROR) << "Failed to quotactl " << mQuotaDevice << " for GID " << cacheGid; | 
|  | 91 | } | 
|  | 92 | return false; | 
|  | 93 | } else { | 
|  | 94 | cacheUsed += dq.dqb_curspace; | 
|  | 95 | } | 
|  | 96 | return true; | 
|  | 97 | } else { | 
|  | 98 | return false; | 
|  | 99 | } | 
|  | 100 | } | 
|  | 101 |  | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 102 | void CacheTracker::loadItemsFrom(const std::string& path) { | 
|  | 103 | FTS *fts; | 
|  | 104 | FTSENT *p; | 
|  | 105 | char *argv[] = { (char*) path.c_str(), nullptr }; | 
| Yi Kong | e7bf377 | 2018-07-17 16:16:24 -0700 | [diff] [blame] | 106 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, nullptr))) { | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 107 | PLOG(WARNING) << "Failed to fts_open " << path; | 
|  | 108 | return; | 
|  | 109 | } | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 110 | while ((p = fts_read(fts)) != nullptr) { | 
|  | 111 | if (p->fts_level == 0) continue; | 
|  | 112 |  | 
|  | 113 | // Create tracking nodes for everything we encounter | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 114 | switch (p->fts_info) { | 
|  | 115 | case FTS_D: | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 116 | case FTS_DEFAULT: | 
|  | 117 | case FTS_F: | 
|  | 118 | case FTS_SL: | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 119 | case FTS_SLNONE: { | 
|  | 120 | auto item = std::shared_ptr<CacheItem>(new CacheItem(p)); | 
|  | 121 | p->fts_pointer = static_cast<void*>(item.get()); | 
|  | 122 | items.push_back(item); | 
|  | 123 | } | 
|  | 124 | } | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 125 |  | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 126 | switch (p->fts_info) { | 
|  | 127 | case FTS_D: { | 
|  | 128 | auto item = static_cast<CacheItem*>(p->fts_pointer); | 
| Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 129 | item->group |= (getxattr(p->fts_path, kXattrCacheGroup, nullptr, 0) >= 0); | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 130 | item->tombstone |= (getxattr(p->fts_path, kXattrCacheTombstone, nullptr, 0) >= 0); | 
|  | 131 |  | 
| Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame] | 132 | // When group, immediately collect all files under tree | 
|  | 133 | if (item->group) { | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 134 | while ((p = fts_read(fts)) != nullptr) { | 
|  | 135 | if (p->fts_info == FTS_DP && p->fts_level == item->level) break; | 
|  | 136 | switch (p->fts_info) { | 
|  | 137 | case FTS_D: | 
|  | 138 | case FTS_DEFAULT: | 
|  | 139 | case FTS_F: | 
|  | 140 | case FTS_SL: | 
|  | 141 | case FTS_SLNONE: | 
|  | 142 | item->size += p->fts_statp->st_blocks * 512; | 
|  | 143 | item->modified = std::max(item->modified, p->fts_statp->st_mtime); | 
|  | 144 | } | 
|  | 145 | } | 
|  | 146 | } | 
|  | 147 | } | 
|  | 148 | } | 
|  | 149 |  | 
|  | 150 | // Bubble up modified time to parent | 
| Luis A. Lozano | 88a69de | 2017-06-06 16:59:37 -0700 | [diff] [blame] | 151 | CHECK(p != nullptr); | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 152 | switch (p->fts_info) { | 
|  | 153 | case FTS_DP: | 
|  | 154 | case FTS_DEFAULT: | 
|  | 155 | case FTS_F: | 
|  | 156 | case FTS_SL: | 
|  | 157 | case FTS_SLNONE: { | 
|  | 158 | auto item = static_cast<CacheItem*>(p->fts_pointer); | 
|  | 159 | auto parent = static_cast<CacheItem*>(p->fts_parent->fts_pointer); | 
|  | 160 | if (parent) { | 
|  | 161 | parent->modified = std::max(parent->modified, item->modified); | 
|  | 162 | } | 
|  | 163 | } | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 164 | } | 
|  | 165 | } | 
|  | 166 | fts_close(fts); | 
|  | 167 | } | 
|  | 168 |  | 
|  | 169 | void CacheTracker::loadItems() { | 
|  | 170 | items.clear(); | 
|  | 171 |  | 
|  | 172 | ATRACE_BEGIN("loadItems"); | 
| Chih-Hung Hsieh | cb057c2 | 2017-08-03 15:48:25 -0700 | [diff] [blame] | 173 | for (const auto& path : mDataPaths) { | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 174 | loadItemsFrom(read_path_inode(path, "cache", kXattrInodeCache)); | 
|  | 175 | loadItemsFrom(read_path_inode(path, "code_cache", kXattrInodeCodeCache)); | 
|  | 176 | } | 
|  | 177 | ATRACE_END(); | 
|  | 178 |  | 
|  | 179 | ATRACE_BEGIN("sortItems"); | 
|  | 180 | auto cmp = [](std::shared_ptr<CacheItem> left, std::shared_ptr<CacheItem> right) { | 
|  | 181 | // TODO: sort dotfiles last | 
|  | 182 | // TODO: sort code_cache last | 
|  | 183 | if (left->modified != right->modified) { | 
|  | 184 | return (left->modified > right->modified); | 
|  | 185 | } | 
|  | 186 | if (left->level != right->level) { | 
|  | 187 | return (left->level < right->level); | 
|  | 188 | } | 
|  | 189 | return left->directory; | 
|  | 190 | }; | 
| Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 191 | std::stable_sort(items.begin(), items.end(), cmp); | 
| Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 192 | ATRACE_END(); | 
|  | 193 | } | 
|  | 194 |  | 
|  | 195 | void CacheTracker::ensureItems() { | 
|  | 196 | if (mItemsLoaded) { | 
|  | 197 | return; | 
|  | 198 | } else { | 
|  | 199 | loadItems(); | 
|  | 200 | mItemsLoaded = true; | 
|  | 201 | } | 
|  | 202 | } | 
|  | 203 |  | 
|  | 204 | int CacheTracker::getCacheRatio() { | 
|  | 205 | if (cacheQuota == 0) { | 
|  | 206 | return 0; | 
|  | 207 | } else { | 
|  | 208 | return (cacheUsed * 10000) / cacheQuota; | 
|  | 209 | } | 
|  | 210 | } | 
|  | 211 |  | 
|  | 212 | }  // namespace installd | 
|  | 213 | }  // namespace android |