Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
| 17 | #define DEBUG false // STOPSHIP if true |
| 18 | #include "Log.h" |
| 19 | |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 20 | #include "StatsCallbackPuller.h" |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 21 | |
| 22 | #include <android/os/IPullAtomCallback.h> |
Ruchir Rastogi | cd9dd94 | 2019-11-27 15:26:03 -0800 | [diff] [blame] | 23 | #include <android/util/StatsEventParcel.h> |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 24 | |
| 25 | #include "PullResultReceiver.h" |
| 26 | #include "StatsPullerManager.h" |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 27 | #include "logd/LogEvent.h" |
| 28 | #include "stats_log_util.h" |
| 29 | |
| 30 | using namespace android::binder; |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 31 | using namespace android::util; |
| 32 | using namespace std; |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 33 | |
| 34 | namespace android { |
| 35 | namespace os { |
| 36 | namespace statsd { |
| 37 | |
Tej Singh | 8981763 | 2019-12-09 16:58:08 -0800 | [diff] [blame] | 38 | StatsCallbackPuller::StatsCallbackPuller(int tagId, const sp<IPullAtomCallback>& callback, |
| 39 | int64_t timeoutNs) |
| 40 | : StatsPuller(tagId), mCallback(callback), mTimeoutNs(timeoutNs) { |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 41 | VLOG("StatsCallbackPuller created for tag %d", tagId); |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 42 | } |
| 43 | |
| 44 | bool StatsCallbackPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) { |
| 45 | VLOG("StatsCallbackPuller called for tag %d", mTagId) |
| 46 | if(mCallback == nullptr) { |
| 47 | ALOGW("No callback registered"); |
| 48 | return false; |
| 49 | } |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 50 | |
| 51 | // Shared variables needed in the result receiver. |
| 52 | shared_ptr<mutex> cv_mutex = make_shared<mutex>(); |
| 53 | shared_ptr<condition_variable> cv = make_shared<condition_variable>(); |
| 54 | shared_ptr<bool> pullFinish = make_shared<bool>(false); |
| 55 | shared_ptr<bool> pullSuccess = make_shared<bool>(false); |
| 56 | shared_ptr<vector<shared_ptr<LogEvent>>> sharedData = |
| 57 | make_shared<vector<shared_ptr<LogEvent>>>(); |
| 58 | |
| 59 | sp<PullResultReceiver> resultReceiver = new PullResultReceiver( |
| 60 | [cv_mutex, cv, pullFinish, pullSuccess, sharedData]( |
Ruchir Rastogi | cd9dd94 | 2019-11-27 15:26:03 -0800 | [diff] [blame] | 61 | int32_t atomTag, bool success, const vector<StatsEventParcel>& output) { |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 62 | // This is the result of the pull, executing in a statsd binder thread. |
| 63 | // The pull could have taken a long time, and we should only modify |
| 64 | // data (the output param) if the pointer is in scope and the pull did not time out. |
| 65 | { |
| 66 | lock_guard<mutex> lk(*cv_mutex); |
Ruchir Rastogi | cd9dd94 | 2019-11-27 15:26:03 -0800 | [diff] [blame] | 67 | for (const StatsEventParcel& parcel: output) { |
Tej Singh | 8981763 | 2019-12-09 16:58:08 -0800 | [diff] [blame] | 68 | shared_ptr<LogEvent> event = make_shared<LogEvent>( |
| 69 | const_cast<uint8_t*>(parcel.buffer.data()), parcel.buffer.size(), |
| 70 | /*uid=*/-1, /*useNewSchema=*/true); |
Ruchir Rastogi | cd9dd94 | 2019-11-27 15:26:03 -0800 | [diff] [blame] | 71 | sharedData->push_back(event); |
| 72 | } |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 73 | *pullSuccess = success; |
| 74 | *pullFinish = true; |
| 75 | } |
| 76 | cv->notify_one(); |
| 77 | }); |
| 78 | |
Tej Singh | 8981763 | 2019-12-09 16:58:08 -0800 | [diff] [blame] | 79 | // Initiate the pull. This is a oneway call to a different process, except |
| 80 | // in unit tests. In process calls are not oneway. |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 81 | Status status = mCallback->onPullAtom(mTagId, resultReceiver); |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 82 | if (!status.isOk()) { |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 83 | return false; |
| 84 | } |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 85 | |
| 86 | { |
| 87 | unique_lock<mutex> unique_lk(*cv_mutex); |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 88 | // Wait until the pull finishes, or until the pull timeout. |
Tej Singh | 8981763 | 2019-12-09 16:58:08 -0800 | [diff] [blame] | 89 | cv->wait_for(unique_lk, chrono::nanoseconds(mTimeoutNs), |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 90 | [pullFinish] { return *pullFinish; }); |
| 91 | if (!*pullFinish) { |
| 92 | // Note: The parent stats puller will also note that there was a timeout and that the |
| 93 | // cache should be cleared. Once we migrate all pullers to this callback, we could |
| 94 | // consolidate the logic. |
| 95 | return true; |
| 96 | } else { |
| 97 | // Only copy the data if we did not timeout and the pull was successful. |
Tej Singh | 8981763 | 2019-12-09 16:58:08 -0800 | [diff] [blame] | 98 | if (*pullSuccess) { |
Tej Singh | 6a5c943 | 2019-10-11 11:07:06 -0700 | [diff] [blame] | 99 | *data = std::move(*sharedData); |
| 100 | } |
| 101 | VLOG("StatsCallbackPuller::pull succeeded for %d", mTagId); |
| 102 | return *pullSuccess; |
| 103 | } |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 104 | } |
Tej Singh | a0c89dd | 2019-01-25 16:39:18 -0800 | [diff] [blame] | 105 | } |
| 106 | |
| 107 | } // namespace statsd |
| 108 | } // namespace os |
| 109 | } // namespace android |