Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 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 LOG_TAG "storaged" |
| 18 | |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 20 | #include <string.h> |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 21 | #include <sys/statvfs.h> |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 22 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 23 | #include <numeric> |
| 24 | |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 25 | #include <android-base/file.h> |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 26 | #include <android-base/parseint.h> |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 27 | #include <android-base/logging.h> |
Jin Qian | b90f1ae | 2017-03-21 16:57:44 -0700 | [diff] [blame] | 28 | #include <android-base/strings.h> |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 29 | #include <log/log_event_list.h> |
| 30 | |
| 31 | #include "storaged.h" |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 32 | #include "storaged_info.h" |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 33 | |
| 34 | using namespace std; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 35 | using namespace chrono; |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 36 | using namespace android::base; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 37 | using namespace storaged_proto; |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 38 | |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 39 | const string emmc_info_t::emmc_sysfs = "/sys/bus/mmc/devices/mmc0:0001/"; |
| 40 | const string emmc_info_t::emmc_debugfs = "/d/mmc0/mmc0:0001/ext_csd"; |
| 41 | const char* emmc_info_t::emmc_ver_str[9] = { |
| 42 | "4.0", "4.1", "4.2", "4.3", "Obsolete", "4.41", "4.5", "5.0", "5.1" |
| 43 | }; |
Jin Qian | b90f1ae | 2017-03-21 16:57:44 -0700 | [diff] [blame] | 44 | |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 45 | const string ufs_info_t::health_file = "/sys/devices/soc/624000.ufshc/health"; |
| 46 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 47 | namespace { |
| 48 | |
| 49 | bool FileExists(const std::string& filename) |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 50 | { |
| 51 | struct stat buffer; |
| 52 | return stat(filename.c_str(), &buffer) == 0; |
| 53 | } |
| 54 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 55 | } // namespace |
| 56 | |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 57 | storage_info_t* storage_info_t::get_storage_info() |
| 58 | { |
| 59 | if (FileExists(emmc_info_t::emmc_sysfs) || |
| 60 | FileExists(emmc_info_t::emmc_debugfs)) { |
| 61 | return new emmc_info_t; |
| 62 | } |
| 63 | if (FileExists(ufs_info_t::health_file)) { |
| 64 | return new ufs_info_t; |
| 65 | } |
| 66 | return new storage_info_t; |
| 67 | } |
| 68 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 69 | void storage_info_t::init(const IOPerfHistory& perf_history) |
| 70 | { |
| 71 | if (!perf_history.has_day_start_sec() || |
| 72 | perf_history.daily_perf_size() > (int)daily_perf.size() || |
| 73 | perf_history.weekly_perf_size() > (int)weekly_perf.size()) { |
| 74 | LOG_TO(SYSTEM, ERROR) << "Invalid IOPerfHistory proto"; |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | day_start_tp = {}; |
| 79 | day_start_tp += seconds(perf_history.day_start_sec()); |
| 80 | |
| 81 | nr_samples = perf_history.nr_samples(); |
| 82 | for (auto bw : perf_history.recent_perf()) { |
| 83 | recent_perf.push_back(bw); |
| 84 | } |
| 85 | |
| 86 | nr_days = perf_history.nr_days(); |
| 87 | int i = 0; |
| 88 | for (auto bw : perf_history.daily_perf()) { |
| 89 | daily_perf[i++] = bw; |
| 90 | } |
| 91 | |
| 92 | nr_weeks = perf_history.nr_weeks(); |
| 93 | i = 0; |
| 94 | for (auto bw : perf_history.weekly_perf()) { |
| 95 | weekly_perf[i++] = bw; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | void storage_info_t::refresh(IOPerfHistory* perf_history) |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 100 | { |
| 101 | struct statvfs buf; |
| 102 | if (statvfs(userdata_path.c_str(), &buf) != 0) { |
| 103 | PLOG_TO(SYSTEM, WARNING) << "Failed to get userdata info"; |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | userdata_total_kb = buf.f_bsize * buf.f_blocks >> 10; |
| 108 | userdata_free_kb = buf.f_bfree * buf.f_blocks >> 10; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 109 | |
| 110 | unique_ptr<lock_t> lock(new lock_t(&si_lock)); |
| 111 | |
| 112 | perf_history->Clear(); |
| 113 | perf_history->set_day_start_sec( |
| 114 | duration_cast<seconds>(day_start_tp.time_since_epoch()).count()); |
| 115 | for (const uint32_t& bw : recent_perf) { |
| 116 | perf_history->add_recent_perf(bw); |
| 117 | } |
| 118 | perf_history->set_nr_samples(nr_samples); |
| 119 | for (const uint32_t& bw : daily_perf) { |
| 120 | perf_history->add_daily_perf(bw); |
| 121 | } |
| 122 | perf_history->set_nr_days(nr_days); |
| 123 | for (const uint32_t& bw : weekly_perf) { |
| 124 | perf_history->add_weekly_perf(bw); |
| 125 | } |
| 126 | perf_history->set_nr_weeks(nr_weeks); |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 129 | void storage_info_t::publish() |
| 130 | { |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 131 | android_log_event_list(EVENTLOGTAG_EMMCINFO) |
| 132 | << version << eol << lifetime_a << lifetime_b |
| 133 | << LOG_ID_EVENTS; |
| 134 | } |
| 135 | |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 136 | void storage_info_t::update_perf_history(uint32_t bw, |
| 137 | const time_point<system_clock>& tp) |
| 138 | { |
| 139 | unique_ptr<lock_t> lock(new lock_t(&si_lock)); |
| 140 | |
| 141 | if (tp > day_start_tp && |
| 142 | duration_cast<seconds>(tp - day_start_tp).count() < DAY_TO_SEC) { |
| 143 | if (nr_samples >= recent_perf.size()) { |
| 144 | recent_perf.push_back(bw); |
| 145 | } else { |
| 146 | recent_perf[nr_samples] = bw; |
| 147 | } |
| 148 | nr_samples++; |
| 149 | return; |
| 150 | } |
| 151 | |
| 152 | recent_perf.erase(recent_perf.begin() + nr_samples, |
| 153 | recent_perf.end()); |
| 154 | |
| 155 | uint32_t daily_avg_bw = accumulate(recent_perf.begin(), |
| 156 | recent_perf.begin() + nr_samples, 0) / nr_samples; |
| 157 | |
| 158 | day_start_tp = tp - seconds(duration_cast<seconds>( |
| 159 | tp.time_since_epoch()).count() % DAY_TO_SEC); |
| 160 | |
| 161 | nr_samples = 0; |
| 162 | if (recent_perf.empty()) |
| 163 | recent_perf.resize(1); |
| 164 | recent_perf[nr_samples++] = bw; |
| 165 | |
| 166 | if (nr_days < WEEK_TO_DAYS) { |
| 167 | daily_perf[nr_days++] = daily_avg_bw; |
| 168 | return; |
| 169 | } |
| 170 | |
| 171 | uint32_t week_avg_bw = accumulate(daily_perf.begin(), |
| 172 | daily_perf.begin() + nr_days, 0) / nr_days; |
| 173 | |
| 174 | nr_days = 0; |
| 175 | daily_perf[nr_days++] = daily_avg_bw; |
| 176 | |
| 177 | if (nr_weeks >= YEAR_TO_WEEKS) { |
| 178 | nr_weeks = 0; |
| 179 | } |
| 180 | weekly_perf[nr_weeks++] = week_avg_bw; |
| 181 | } |
| 182 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 183 | vector<int> storage_info_t::get_perf_history() |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 184 | { |
| 185 | unique_ptr<lock_t> lock(new lock_t(&si_lock)); |
| 186 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 187 | vector<int> ret(3 + recent_perf.size() + daily_perf.size() + weekly_perf.size()); |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 188 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 189 | ret[0] = recent_perf.size(); |
| 190 | ret[1] = daily_perf.size(); |
| 191 | ret[2] = weekly_perf.size(); |
| 192 | |
| 193 | int start = 3; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 194 | for (size_t i = 0; i < recent_perf.size(); i++) { |
| 195 | int idx = (recent_perf.size() + nr_samples - 1 - i) % recent_perf.size(); |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 196 | ret[start + i] = recent_perf[idx]; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 199 | start += recent_perf.size(); |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 200 | for (size_t i = 0; i < daily_perf.size(); i++) { |
| 201 | int idx = (daily_perf.size() + nr_days - 1 - i) % daily_perf.size(); |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 202 | ret[start + i] = daily_perf[idx]; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 203 | } |
| 204 | |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 205 | start += daily_perf.size(); |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 206 | for (size_t i = 0; i < weekly_perf.size(); i++) { |
| 207 | int idx = (weekly_perf.size() + nr_weeks - 1 - i) % weekly_perf.size(); |
Jin Qian | b049d18 | 2017-10-12 17:02:17 -0700 | [diff] [blame^] | 208 | ret[start + i] = weekly_perf[idx]; |
Jin Qian | d691d6e | 2017-09-28 16:02:22 -0700 | [diff] [blame] | 209 | } |
| 210 | |
| 211 | return ret; |
| 212 | } |
| 213 | |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 214 | void emmc_info_t::report() |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 215 | { |
| 216 | if (!report_sysfs() && !report_debugfs()) |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 217 | return; |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 218 | |
| 219 | publish(); |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | bool emmc_info_t::report_sysfs() |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 223 | { |
| 224 | string buffer; |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 225 | uint16_t rev = 0; |
| 226 | |
| 227 | if (!ReadFileToString(emmc_sysfs + "rev", &buffer)) { |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 228 | return false; |
| 229 | } |
| 230 | |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 231 | if (sscanf(buffer.c_str(), "0x%hx", &rev) < 1 || |
| 232 | rev < 7 || rev > ARRAY_SIZE(emmc_ver_str)) { |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 233 | return false; |
| 234 | } |
| 235 | |
| 236 | version = "emmc "; |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 237 | version += emmc_ver_str[rev]; |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 238 | |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 239 | if (!ReadFileToString(emmc_sysfs + "pre_eol_info", &buffer)) { |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 240 | return false; |
| 241 | } |
| 242 | |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 243 | if (sscanf(buffer.c_str(), "%hx", &eol) < 1 || eol == 0) { |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 244 | return false; |
| 245 | } |
| 246 | |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 247 | if (!ReadFileToString(emmc_sysfs + "life_time", &buffer)) { |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 248 | return false; |
| 249 | } |
| 250 | |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 251 | if (sscanf(buffer.c_str(), "0x%hx 0x%hx", &lifetime_a, &lifetime_b) < 2 || |
| 252 | (lifetime_a == 0 && lifetime_b == 0)) { |
Jin Qian | 4fc338e | 2017-03-15 19:03:06 -0700 | [diff] [blame] | 253 | return false; |
| 254 | } |
| 255 | |
| 256 | return true; |
| 257 | } |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 258 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 259 | namespace { |
| 260 | |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 261 | const size_t EXT_CSD_FILE_MIN_SIZE = 1024; |
| 262 | /* 2 characters in string for each byte */ |
| 263 | const size_t EXT_CSD_REV_IDX = 192 * 2; |
| 264 | const size_t EXT_PRE_EOL_INFO_IDX = 267 * 2; |
| 265 | const size_t EXT_DEVICE_LIFE_TIME_EST_A_IDX = 268 * 2; |
| 266 | const size_t EXT_DEVICE_LIFE_TIME_EST_B_IDX = 269 * 2; |
| 267 | |
Jin Qian | 65dea71 | 2017-08-29 16:48:20 -0700 | [diff] [blame] | 268 | } // namespace |
| 269 | |
Jin Qian | 726339c | 2017-03-17 16:36:59 -0700 | [diff] [blame] | 270 | bool emmc_info_t::report_debugfs() |
| 271 | { |
| 272 | string buffer; |
| 273 | uint16_t rev = 0; |
| 274 | |
| 275 | if (!ReadFileToString(emmc_debugfs, &buffer) || |
| 276 | buffer.length() < (size_t)EXT_CSD_FILE_MIN_SIZE) { |
| 277 | return false; |
| 278 | } |
| 279 | |
| 280 | string str = buffer.substr(EXT_CSD_REV_IDX, 2); |
| 281 | if (!ParseUint(str, &rev) || |
| 282 | rev < 7 || rev > ARRAY_SIZE(emmc_ver_str)) { |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | version = "emmc "; |
| 287 | version += emmc_ver_str[rev]; |
| 288 | |
| 289 | str = buffer.substr(EXT_PRE_EOL_INFO_IDX, 2); |
| 290 | if (!ParseUint(str, &eol)) { |
| 291 | return false; |
| 292 | } |
| 293 | |
| 294 | str = buffer.substr(EXT_DEVICE_LIFE_TIME_EST_A_IDX, 2); |
| 295 | if (!ParseUint(str, &lifetime_a)) { |
| 296 | return false; |
| 297 | } |
| 298 | |
| 299 | str = buffer.substr(EXT_DEVICE_LIFE_TIME_EST_B_IDX, 2); |
| 300 | if (!ParseUint(str, &lifetime_b)) { |
| 301 | return false; |
| 302 | } |
| 303 | |
| 304 | return true; |
Jin Qian | b90f1ae | 2017-03-21 16:57:44 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 307 | void ufs_info_t::report() |
Jin Qian | b90f1ae | 2017-03-21 16:57:44 -0700 | [diff] [blame] | 308 | { |
| 309 | string buffer; |
| 310 | if (!ReadFileToString(health_file, &buffer)) { |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 311 | return; |
Jin Qian | b90f1ae | 2017-03-21 16:57:44 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | vector<string> lines = Split(buffer, "\n"); |
| 315 | if (lines.empty()) { |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 316 | return; |
Jin Qian | b90f1ae | 2017-03-21 16:57:44 -0700 | [diff] [blame] | 317 | } |
| 318 | |
| 319 | char rev[8]; |
| 320 | if (sscanf(lines[0].c_str(), "ufs version: 0x%7s\n", rev) < 1) { |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 321 | return; |
Jin Qian | b90f1ae | 2017-03-21 16:57:44 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | version = "ufs " + string(rev); |
| 325 | |
| 326 | for (size_t i = 1; i < lines.size(); i++) { |
| 327 | char token[32]; |
| 328 | uint16_t val; |
| 329 | int ret; |
| 330 | if ((ret = sscanf(lines[i].c_str(), |
| 331 | "Health Descriptor[Byte offset 0x%*d]: %31s = 0x%hx", |
| 332 | token, &val)) < 2) { |
| 333 | continue; |
| 334 | } |
| 335 | |
| 336 | if (string(token) == "bPreEOLInfo") { |
| 337 | eol = val; |
| 338 | } else if (string(token) == "bDeviceLifeTimeEstA") { |
| 339 | lifetime_a = val; |
| 340 | } else if (string(token) == "bDeviceLifeTimeEstB") { |
| 341 | lifetime_b = val; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | if (eol == 0 || (lifetime_a == 0 && lifetime_b == 0)) { |
Jin Qian | 8847c62 | 2017-07-17 15:06:11 -0700 | [diff] [blame] | 346 | return; |
Jin Qian | b90f1ae | 2017-03-21 16:57:44 -0700 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | publish(); |
Jin Qian | b90f1ae | 2017-03-21 16:57:44 -0700 | [diff] [blame] | 350 | } |
| 351 | |