| 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 | |
| Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 17 | #define DEBUG false |
| 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 | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 23 | #include "../stats_log_util.h" |
| 24 | #include "../statscompanion_util.h" |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 25 | #include "StatsCompanionServicePuller.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 | |
| Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 46 | void StatsCompanionServicePuller::SetStatsCompanionService( |
| 47 | sp<IStatsCompanionService> statsCompanionService) { |
| 48 | AutoMutex _l(mStatsCompanionServiceLock); |
| 49 | sp<IStatsCompanionService> tmpForLock = mStatsCompanionService; |
| 50 | mStatsCompanionService = statsCompanionService; |
| 51 | } |
| 52 | |
| Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 53 | bool StatsCompanionServicePuller::PullInternal(vector<shared_ptr<LogEvent> >* data) { |
| Chenjie Yu | aa5b201 | 2018-03-21 13:53:15 -0700 | [diff] [blame] | 54 | sp<IStatsCompanionService> statsCompanionServiceCopy = mStatsCompanionService; |
| 55 | if (statsCompanionServiceCopy != nullptr) { |
| 56 | vector<StatsLogEventWrapper> returned_value; |
| 57 | Status status = statsCompanionServiceCopy->pullData(mTagId, &returned_value); |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 58 | if (!status.isOk()) { |
| Chenjie Yu | b038b70 | 2017-12-18 15:15:34 -0800 | [diff] [blame] | 59 | ALOGW("error pulling for %d", mTagId); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 60 | return false; |
| 61 | } |
| 62 | data->clear(); |
| Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 63 | int32_t timestampSec = getWallClockSec(); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 64 | for (const StatsLogEventWrapper& it : returned_value) { |
| 65 | log_msg tmp; |
| 66 | tmp.entry_v1.len = it.bytes.size(); |
| 67 | // Manually set the header size to 28 bytes to match the pushed log events. |
| 68 | tmp.entry.hdr_size = kLogMsgHeaderSize; |
| Yangster-mac | 330af58 | 2018-02-08 15:24:38 -0800 | [diff] [blame] | 69 | tmp.entry_v1.sec = timestampSec; |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 70 | // And set the received bytes starting after the 28 bytes reserved for header. |
| 71 | std::copy(it.bytes.begin(), it.bytes.end(), tmp.buf + kLogMsgHeaderSize); |
| 72 | data->push_back(make_shared<LogEvent>(tmp)); |
| Joe Onorato | 9fc9edf | 2017-10-15 20:08:52 -0700 | [diff] [blame] | 73 | } |
| Tej Singh | 484524a | 2018-02-01 15:10:05 -0800 | [diff] [blame] | 74 | VLOG("StatsCompanionServicePuller::pull succeeded for %d", mTagId); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 75 | return true; |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 76 | } else { |
| 77 | ALOGW("statsCompanion not found!"); |
| Chenjie Yu | 5305e1d | 2017-10-31 13:49:36 -0700 | [diff] [blame] | 78 | return false; |
| Chenjie Yu | 1a317ba | 2017-10-05 16:05:32 -0700 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
| 82 | } // namespace statsd |
| 83 | } // namespace os |
| 84 | } // namespace android |