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() { |
| 54 | int cacheGid = multiuser_get_cache_gid(mUserId, mAppId); |
| 55 | if (cacheGid != -1 && !mQuotaDevice.empty()) { |
| 56 | ATRACE_BEGIN("loadStats quota"); |
| 57 | struct dqblk dq; |
| 58 | if (quotactl(QCMD(Q_GETQUOTA, GRPQUOTA), mQuotaDevice.c_str(), cacheGid, |
| 59 | reinterpret_cast<char*>(&dq)) != 0) { |
| 60 | ATRACE_END(); |
| 61 | if (errno != ESRCH) { |
| 62 | PLOG(ERROR) << "Failed to quotactl " << mQuotaDevice << " for GID " << cacheGid; |
| 63 | } |
| 64 | } else { |
| 65 | cacheUsed = dq.dqb_curspace; |
| 66 | ATRACE_END(); |
| 67 | return; |
| 68 | } |
| 69 | } |
| 70 | |
| 71 | ATRACE_BEGIN("loadStats tree"); |
| 72 | cacheUsed = 0; |
| 73 | for (auto path : mDataPaths) { |
| 74 | auto cachePath = read_path_inode(path, "cache", kXattrInodeCache); |
| 75 | auto codeCachePath = read_path_inode(path, "code_cache", kXattrInodeCodeCache); |
| 76 | calculate_tree_size(cachePath, &cacheUsed); |
| 77 | calculate_tree_size(codeCachePath, &cacheUsed); |
| 78 | } |
| 79 | ATRACE_END(); |
| 80 | } |
| 81 | |
| 82 | void CacheTracker::loadItemsFrom(const std::string& path) { |
| 83 | FTS *fts; |
| 84 | FTSENT *p; |
| 85 | char *argv[] = { (char*) path.c_str(), nullptr }; |
Jeff Sharkey | b26786d | 2017-03-11 19:40:29 -0700 | [diff] [blame] | 86 | if (!(fts = fts_open(argv, FTS_PHYSICAL | FTS_NOCHDIR | FTS_XDEV, NULL))) { |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 87 | PLOG(WARNING) << "Failed to fts_open " << path; |
| 88 | return; |
| 89 | } |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 90 | while ((p = fts_read(fts)) != nullptr) { |
| 91 | if (p->fts_level == 0) continue; |
| 92 | |
| 93 | // Create tracking nodes for everything we encounter |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 94 | switch (p->fts_info) { |
| 95 | case FTS_D: |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 96 | case FTS_DEFAULT: |
| 97 | case FTS_F: |
| 98 | case FTS_SL: |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 99 | case FTS_SLNONE: { |
| 100 | auto item = std::shared_ptr<CacheItem>(new CacheItem(p)); |
| 101 | p->fts_pointer = static_cast<void*>(item.get()); |
| 102 | items.push_back(item); |
| 103 | } |
| 104 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 105 | |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 106 | switch (p->fts_info) { |
| 107 | case FTS_D: { |
| 108 | auto item = static_cast<CacheItem*>(p->fts_pointer); |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame^] | 109 | item->group |= (getxattr(p->fts_path, kXattrCacheGroup, nullptr, 0) >= 0); |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 110 | item->tombstone |= (getxattr(p->fts_path, kXattrCacheTombstone, nullptr, 0) >= 0); |
| 111 | |
Jeff Sharkey | ed909ae | 2017-03-22 21:27:40 -0600 | [diff] [blame^] | 112 | // When group, immediately collect all files under tree |
| 113 | if (item->group) { |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 114 | while ((p = fts_read(fts)) != nullptr) { |
| 115 | if (p->fts_info == FTS_DP && p->fts_level == item->level) break; |
| 116 | switch (p->fts_info) { |
| 117 | case FTS_D: |
| 118 | case FTS_DEFAULT: |
| 119 | case FTS_F: |
| 120 | case FTS_SL: |
| 121 | case FTS_SLNONE: |
| 122 | item->size += p->fts_statp->st_blocks * 512; |
| 123 | item->modified = std::max(item->modified, p->fts_statp->st_mtime); |
| 124 | } |
| 125 | } |
| 126 | } |
| 127 | } |
| 128 | } |
| 129 | |
| 130 | // Bubble up modified time to parent |
| 131 | switch (p->fts_info) { |
| 132 | case FTS_DP: |
| 133 | case FTS_DEFAULT: |
| 134 | case FTS_F: |
| 135 | case FTS_SL: |
| 136 | case FTS_SLNONE: { |
| 137 | auto item = static_cast<CacheItem*>(p->fts_pointer); |
| 138 | auto parent = static_cast<CacheItem*>(p->fts_parent->fts_pointer); |
| 139 | if (parent) { |
| 140 | parent->modified = std::max(parent->modified, item->modified); |
| 141 | } |
| 142 | } |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 143 | } |
| 144 | } |
| 145 | fts_close(fts); |
| 146 | } |
| 147 | |
| 148 | void CacheTracker::loadItems() { |
| 149 | items.clear(); |
| 150 | |
| 151 | ATRACE_BEGIN("loadItems"); |
| 152 | for (auto path : mDataPaths) { |
| 153 | loadItemsFrom(read_path_inode(path, "cache", kXattrInodeCache)); |
| 154 | loadItemsFrom(read_path_inode(path, "code_cache", kXattrInodeCodeCache)); |
| 155 | } |
| 156 | ATRACE_END(); |
| 157 | |
| 158 | ATRACE_BEGIN("sortItems"); |
| 159 | auto cmp = [](std::shared_ptr<CacheItem> left, std::shared_ptr<CacheItem> right) { |
| 160 | // TODO: sort dotfiles last |
| 161 | // TODO: sort code_cache last |
| 162 | if (left->modified != right->modified) { |
| 163 | return (left->modified > right->modified); |
| 164 | } |
| 165 | if (left->level != right->level) { |
| 166 | return (left->level < right->level); |
| 167 | } |
| 168 | return left->directory; |
| 169 | }; |
Jeff Sharkey | 871a8f2 | 2017-02-21 18:30:28 -0700 | [diff] [blame] | 170 | std::stable_sort(items.begin(), items.end(), cmp); |
Jeff Sharkey | 88ddd94 | 2017-01-17 18:05:54 -0700 | [diff] [blame] | 171 | ATRACE_END(); |
| 172 | } |
| 173 | |
| 174 | void CacheTracker::ensureItems() { |
| 175 | if (mItemsLoaded) { |
| 176 | return; |
| 177 | } else { |
| 178 | loadItems(); |
| 179 | mItemsLoaded = true; |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | int CacheTracker::getCacheRatio() { |
| 184 | if (cacheQuota == 0) { |
| 185 | return 0; |
| 186 | } else { |
| 187 | return (cacheUsed * 10000) / cacheQuota; |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | } // namespace installd |
| 192 | } // namespace android |