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