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