Yao Chen | 482d272 | 2017-09-12 13:25:43 -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 | #include <android/os/DropBoxManager.h> |
| 17 | #include <android-base/file.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 18 | #include <androidfw/ZipUtils.h> |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 19 | |
| 20 | #include "DropboxReader.h" |
| 21 | |
| 22 | using android::sp; |
| 23 | using android::String16; |
| 24 | using android::binder::Status; |
| 25 | using android::base::unique_fd; |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 26 | using android::os::statsd::EventMetricData; |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 27 | using android::os::DropBoxManager; |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 28 | using android::ZipUtils; |
| 29 | using std::vector; |
| 30 | |
| 31 | status_t DropboxReader::readStatsLogs(FILE* out, const string& tag, long msec) { |
| 32 | sp<DropBoxManager> dropbox = new DropBoxManager(); |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 33 | StatsLogReport logReport; |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 34 | |
| 35 | long timestamp = msec; |
| 36 | // instead of while(true), put a hard limit 1000. Dropbox won't have more than 1000 files. |
| 37 | for(int i = 0; i < 1000; i++ ) { |
| 38 | DropBoxManager::Entry entry; |
| 39 | Status status = dropbox->getNextEntry(String16(tag.c_str()), |
| 40 | timestamp, &entry); |
| 41 | if (!status.isOk()) { |
| 42 | ALOGD("No more entries, or failed to read. We can't tell unfortunately."); |
| 43 | return android::OK; |
| 44 | } |
| 45 | |
| 46 | const unique_fd& fd = entry.getFd(); |
| 47 | |
| 48 | // use this timestamp for next query. |
| 49 | timestamp = entry.getTimestamp(); |
| 50 | |
| 51 | if (entry.getFlags() & DropBoxManager::IS_GZIPPED) { |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 52 | if (!parseFromGzipFile(fd, logReport)) { |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 53 | // Failed to parse from the file. Continue to fetch the next entry. |
| 54 | continue; |
| 55 | } |
| 56 | } else { |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 57 | if (!parseFromFile(fd, logReport)) { |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 58 | // Failed to parse from the file. Continue to fetch the next entry. |
| 59 | continue; |
| 60 | } |
| 61 | } |
| 62 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 63 | printLog(out, logReport); |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 64 | } |
| 65 | return android::OK; |
| 66 | } |
| 67 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 68 | bool DropboxReader::parseFromGzipFile(const unique_fd& fd, StatsLogReport& logReport) { |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 69 | FILE *file = fdopen(fd, "r"); |
| 70 | bool result = false; |
| 71 | bool scanResult; |
| 72 | int method; |
| 73 | long compressedLen; |
| 74 | long uncompressedLen; |
| 75 | unsigned long crc32; |
| 76 | scanResult = ZipUtils::examineGzip(file, &method, &uncompressedLen, |
| 77 | &compressedLen, &crc32); |
| 78 | if (scanResult && method == kCompressDeflated) { |
| 79 | vector<uint8_t> buf(uncompressedLen); |
| 80 | if (ZipUtils::inflateToBuffer(file, &buf[0], uncompressedLen, compressedLen)) { |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 81 | if (logReport.ParseFromArray(&buf[0], uncompressedLen)) { |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 82 | result = true; |
| 83 | } |
| 84 | } |
| 85 | } else { |
| 86 | ALOGE("This isn't a valid deflated gzip file"); |
| 87 | } |
| 88 | fclose(file); |
| 89 | return result; |
| 90 | } |
| 91 | |
| 92 | // parse a non zipped file. |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 93 | bool DropboxReader::parseFromFile(const unique_fd& fd, StatsLogReport& logReport) { |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 94 | string content; |
| 95 | if (!android::base::ReadFdToString(fd, &content)) { |
| 96 | ALOGE("Failed to read file"); |
| 97 | return false; |
| 98 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 99 | if (!logReport.ParseFromString(content)) { |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 100 | ALOGE("failed to parse log entry from data"); |
| 101 | return false; |
| 102 | } |
| 103 | return true; |
| 104 | } |
| 105 | |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 106 | void DropboxReader::printLog(FILE* out, const StatsLogReport& logReport) { |
| 107 | fprintf(out, "start_time_msec=%lld, end_time_msec=%lld, ", |
| 108 | logReport.start_report_millis(), logReport.end_report_millis()); |
| 109 | for (int i = 0; i < logReport.event_metrics().data_size(); i++) { |
| 110 | EventMetricData eventMetricData = logReport.event_metrics().data(i); |
| 111 | for (int j = 0; j < eventMetricData.key_value_pair_size(); j++) { |
| 112 | fprintf(out, "key=%d, ", eventMetricData.key_value_pair(j).key()); |
| 113 | fprintf(out, "value_str=%s ", eventMetricData.key_value_pair(j).value_str().c_str()); |
| 114 | fprintf(out, "value_int=%lld ", eventMetricData.key_value_pair(j).value_int()); |
| 115 | fprintf(out, "value_float=%f ", eventMetricData.key_value_pair(j).value_float()); |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 116 | } |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 117 | } |
yro | 00698da | 2017-09-15 10:06:40 -0700 | [diff] [blame] | 118 | fprintf(out, "\n"); |
Yao Chen | 482d272 | 2017-09-12 13:25:43 -0700 | [diff] [blame] | 119 | } |