| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -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 | |
| Chenjie Yu | b3dda41 | 2017-10-24 13:41:59 -0700 | [diff] [blame] | 17 | #define DEBUG true |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 18 | #include "Log.h" |
| 19 | |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 20 | #include <android/os/IStatsCompanionService.h> |
| 21 | #include <binder/IPCThreadState.h> |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 22 | #include <private/android_filesystem_config.h> |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 23 | #include "StatsCompanionServicePuller.h" |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 24 | #include "StatsService.h" |
| Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame^] | 25 | #include "guardrail/StatsdStats.h" |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 26 | |
| 27 | using namespace android; |
| 28 | using namespace android::base; |
| 29 | using namespace android::binder; |
| 30 | using namespace android::os; |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 31 | using std::make_shared; |
| 32 | using std::shared_ptr; |
| 33 | using std::vector; |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 34 | |
| 35 | namespace android { |
| 36 | namespace os { |
| 37 | namespace statsd { |
| 38 | |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 39 | const int kLogMsgHeaderSize = 28; |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 40 | |
| 41 | // The reading and parsing are implemented in Java. It is not difficult to port over. But for now |
| 42 | // let StatsCompanionService handle that and send the data back. |
| Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame^] | 43 | StatsCompanionServicePuller::StatsCompanionServicePuller(int tagId) : StatsPuller(tagId) { |
| 44 | } |
| 45 | |
| 46 | bool StatsCompanionServicePuller::PullInternal(vector<shared_ptr<LogEvent> >* data) { |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 47 | sp<IStatsCompanionService> statsCompanion = StatsService::getStatsCompanionService(); |
| David Chen | 1481fe1 | 2017-10-16 13:16:34 -0700 | [diff] [blame] | 48 | vector<StatsLogEventWrapper> returned_value; |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 49 | if (statsCompanion != NULL) { |
| Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame^] | 50 | Status status = statsCompanion->pullData(mTagId, &returned_value); |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 51 | if (!status.isOk()) { |
| Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame^] | 52 | ALOGW("error pulling for %d", mTagId); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 53 | return false; |
| 54 | } |
| 55 | data->clear(); |
| Chenjie Yu | 6842a8c | 2017-12-05 22:34:34 -0800 | [diff] [blame] | 56 | int timestamp = time(nullptr); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 57 | for (const StatsLogEventWrapper& it : returned_value) { |
| 58 | log_msg tmp; |
| 59 | tmp.entry_v1.len = it.bytes.size(); |
| 60 | // Manually set the header size to 28 bytes to match the pushed log events. |
| 61 | tmp.entry.hdr_size = kLogMsgHeaderSize; |
| Chenjie Yu | 6842a8c | 2017-12-05 22:34:34 -0800 | [diff] [blame] | 62 | tmp.entry_v1.sec = timestamp; |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 63 | // And set the received bytes starting after the 28 bytes reserved for header. |
| 64 | std::copy(it.bytes.begin(), it.bytes.end(), tmp.buf + kLogMsgHeaderSize); |
| 65 | data->push_back(make_shared<LogEvent>(tmp)); |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 66 | } |
| Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame^] | 67 | ALOGD("StatsCompanionServicePuller::pull succeeded for %d", mTagId); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 68 | return true; |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 69 | } else { |
| 70 | ALOGW("statsCompanion not found!"); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 71 | return false; |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 72 | } |
| 73 | } |
| 74 | |
| 75 | } // namespace statsd |
| 76 | } // namespace os |
| 77 | } // namespace android |