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> |
| 23 | #include <utils/Trace.h> |
| 24 | |
| 25 | #include <android-base/logging.h> |
| 26 | #include <android-base/stringprintf.h> |
| 27 | |
| 28 | #include "utils.h" |
| 29 | |
| 30 | using android::base::StringPrintf; |
| 31 | |
| 32 | namespace android { |
| 33 | namespace installd { |
| 34 | |
| 35 | CacheTracker::CacheTracker(userid_t userId, appid_t appId, const std::string& quotaDevice) : |
| 36 | cacheUsed(0), cacheQuota(0), mUserId(userId), mAppId(appId), mQuotaDevice(quotaDevice), |
| 37 | mItemsLoaded(false) { |
| 38 | } |
| 39 | |
| 40 | CacheTracker::~CacheTracker() { |
| 41 | } |
| 42 | |
| 43 | std::string CacheTracker::toString() { |
| 44 | return StringPrintf("UID=%d used=%" PRId64 " quota=%" PRId64 " ratio=%d", |
| 45 | multiuser_get_uid(mUserId, mAppId), cacheUsed, cacheQuota, getCacheRatio()); |
| 46 | } |
| 47 | |
| 48 | void CacheTracker::addDataPath(const std::string& dataPath) { |
| 49 | mDataPaths.push_back(dataPath); |
| 50 | } |
| 51 | |
| 52 | void CacheTracker::loadStats() { |
| 53 | int cacheGid = multiuser_get_cache_gid(mUserId, mAppId); |
| 54 | if (cacheGid != -1 && !mQuotaDevice.empty()) { |
| 55 | ATRACE_BEGIN("loadStats quota"); |
| 56 | struct dqblk dq; |
| 57 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), mQuotaDevice.c_str(), cacheGid, |
| 58 | reinterpret_cast<char*>(&dq)) != 0) { |
| 59 | ATRACE_END(); |
| 60 | if (errno != ESRCH) { |
| 61 | PLOG(ERROR) << "Failed to quotactl " << mQuotaDevice << " for GID " << cacheGid; |
| 62 | } |
| 63 | } else { |
| 64 | cacheUsed = dq.dqb_curspace; |
| 65 | ATRACE_END(); |
| 66 | return; |
| 67 | } |
| 68 | } |
| 69 | |
| 70 | ATRACE_BEGIN("loadStats tree"); |
| 71 | cacheUsed = 0; |
| 72 | for (auto path : mDataPaths) { |
| 73 | auto cachePath = read_path_inode(path, "cache", kXattrInodeCache); |
| 74 | auto codeCachePath = read_path_inode(path, "code_cache", kXattrInodeCodeCache); |
| 75 | calculate_tree_size(cachePath, &cacheUsed); |
| 76 | calculate_tree_size(codeCachePath, &cacheUsed); |
| 77 | } |
| 78 | ATRACE_END(); |
| 79 | } |
| 80 | |
| 81 | void CacheTracker::loadItemsFrom(const std::string& path) { |
| 82 | FTS *fts; |
| 83 | FTSENT *p; |
| 84 | char *argv[] = { (char*) path.c_str(), nullptr }; |
| 85 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_XDEV, NULL))) { |
| 86 | PLOG(WARNING) << "Failed to fts_open " << path; |
| 87 | return; |
| 88 | } |
| 89 | // TODO: add support for "user.atomic" and "user.tombstone" xattrs |
| 90 | while ((p = fts_read(fts)) != NULL) { |
| 91 | switch (p->fts_info) { |
| 92 | case FTS_D: |
| 93 | // Track the newest mtime of anything inside so we consider |
| 94 | // deleting the directory last |
| 95 | p->fts_number = p->fts_statp->st_mtime; |
| 96 | break; |
| 97 | case FTS_DP: |
| 98 | p->fts_statp->st_mtime = p->fts_number; |
| 99 | |
| 100 | // Ignore the actual top-level cache directories |
| 101 | if (p->fts_level == 0) break; |
| 102 | case FTS_DEFAULT: |
| 103 | case FTS_F: |
| 104 | case FTS_SL: |
| 105 | case FTS_SLNONE: |
| 106 | // TODO: optimize path memory footprint |
| 107 | items.push_back(std::shared_ptr<CacheItem>(new CacheItem(nullptr, p))); |
| 108 | |
| 109 | // Track the newest modified item under this tree |
| 110 | p->fts_parent->fts_number = |
| 111 | std::max(p->fts_parent->fts_number, p->fts_statp->st_mtime); |
| 112 | break; |
| 113 | } |
| 114 | } |
| 115 | fts_close(fts); |
| 116 | } |
| 117 | |
| 118 | void CacheTracker::loadItems() { |
| 119 | items.clear(); |
| 120 | |
| 121 | ATRACE_BEGIN("loadItems"); |
| 122 | for (auto path : mDataPaths) { |
| 123 | loadItemsFrom(read_path_inode(path, "cache", kXattrInodeCache)); |
| 124 | loadItemsFrom(read_path_inode(path, "code_cache", kXattrInodeCodeCache)); |
| 125 | } |
| 126 | ATRACE_END(); |
| 127 | |
| 128 | ATRACE_BEGIN("sortItems"); |
| 129 | auto cmp = [](std::shared_ptr<CacheItem> left, std::shared_ptr<CacheItem> right) { |
| 130 | // TODO: sort dotfiles last |
| 131 | // TODO: sort code_cache last |
| 132 | if (left->modified != right->modified) { |
| 133 | return (left->modified > right->modified); |
| 134 | } |
| 135 | if (left->level != right->level) { |
| 136 | return (left->level < right->level); |
| 137 | } |
| 138 | return left->directory; |
| 139 | }; |
| 140 | std::sort(items.begin(), items.end(), cmp); |
| 141 | ATRACE_END(); |
| 142 | } |
| 143 | |
| 144 | void CacheTracker::ensureItems() { |
| 145 | if (mItemsLoaded) { |
| 146 | return; |
| 147 | } else { |
| 148 | loadItems(); |
| 149 | mItemsLoaded = true; |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | int CacheTracker::getCacheRatio() { |
| 154 | if (cacheQuota == 0) { |
| 155 | return 0; |
| 156 | } else { |
| 157 | return (cacheUsed * 10000) / cacheQuota; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | } // namespace installd |
| 162 | } // namespace android |