blob: 78e6f094db7e3c630767b46b5995d87419e42d34 [file] [log] [blame]
Tej Singha0c89dd2019-01-25 16:39:18 -08001/*
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 Singha0c89dd2019-01-25 16:39:18 -080020#include "StatsCallbackPuller.h"
Tej Singh6a5c9432019-10-11 11:07:06 -070021#include "PullResultReceiver.h"
22#include "StatsPullerManager.h"
Tej Singha0c89dd2019-01-25 16:39:18 -080023#include "logd/LogEvent.h"
24#include "stats_log_util.h"
25
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080026#include <aidl/android/util/StatsEventParcel.h>
27
Tej Singh6a5c9432019-10-11 11:07:06 -070028using namespace std;
Tej Singha0c89dd2019-01-25 16:39:18 -080029
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080030using Status = ::ndk::ScopedAStatus;
31using aidl::android::util::StatsEventParcel;
32using ::ndk::SharedRefBase;
33
Tej Singha0c89dd2019-01-25 16:39:18 -080034namespace android {
35namespace os {
36namespace statsd {
37
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080038StatsCallbackPuller::StatsCallbackPuller(int tagId, const shared_ptr<IPullAtomCallback>& callback,
Tej Singh5b4951b2020-01-24 13:23:56 -080039 const int64_t coolDownNs, int64_t timeoutNs,
40 const vector<int> additiveFields)
41 : StatsPuller(tagId, coolDownNs, timeoutNs, additiveFields), mCallback(callback) {
Tej Singh6a5c9432019-10-11 11:07:06 -070042 VLOG("StatsCallbackPuller created for tag %d", tagId);
Tej Singha0c89dd2019-01-25 16:39:18 -080043}
44
45bool StatsCallbackPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
Tej Singh8f358602020-01-15 16:05:39 -080046 VLOG("StatsCallbackPuller called for tag %d", mTagId);
Tej Singha0c89dd2019-01-25 16:39:18 -080047 if(mCallback == nullptr) {
48 ALOGW("No callback registered");
49 return false;
50 }
Tej Singh6a5c9432019-10-11 11:07:06 -070051
52 // Shared variables needed in the result receiver.
53 shared_ptr<mutex> cv_mutex = make_shared<mutex>();
54 shared_ptr<condition_variable> cv = make_shared<condition_variable>();
55 shared_ptr<bool> pullFinish = make_shared<bool>(false);
56 shared_ptr<bool> pullSuccess = make_shared<bool>(false);
57 shared_ptr<vector<shared_ptr<LogEvent>>> sharedData =
58 make_shared<vector<shared_ptr<LogEvent>>>();
59
Ruchir Rastogie449b0c2020-02-10 17:40:09 -080060 shared_ptr<PullResultReceiver> resultReceiver = SharedRefBase::make<PullResultReceiver>(
Tej Singh6a5c9432019-10-11 11:07:06 -070061 [cv_mutex, cv, pullFinish, pullSuccess, sharedData](
Ruchir Rastogicd9dd942019-11-27 15:26:03 -080062 int32_t atomTag, bool success, const vector<StatsEventParcel>& output) {
Tej Singh6a5c9432019-10-11 11:07:06 -070063 // This is the result of the pull, executing in a statsd binder thread.
64 // The pull could have taken a long time, and we should only modify
65 // data (the output param) if the pointer is in scope and the pull did not time out.
66 {
67 lock_guard<mutex> lk(*cv_mutex);
Ruchir Rastogicd9dd942019-11-27 15:26:03 -080068 for (const StatsEventParcel& parcel: output) {
Ruchir Rastogidfd63d42020-02-20 17:54:13 -080069 shared_ptr<LogEvent> event = make_shared<LogEvent>(/*uid=*/-1, /*pid=*/-1);
Ruchir Rastogi07f7adb2020-04-16 17:46:12 -070070 bool valid = event->parseBuffer((uint8_t*)parcel.buffer.data(),
71 parcel.buffer.size());
72 if (valid) {
73 sharedData->push_back(event);
74 } else {
75 StatsdStats::getInstance().noteAtomError(event->GetTagId(),
76 /*pull=*/true);
77 }
Ruchir Rastogicd9dd942019-11-27 15:26:03 -080078 }
Tej Singh6a5c9432019-10-11 11:07:06 -070079 *pullSuccess = success;
80 *pullFinish = true;
81 }
82 cv->notify_one();
83 });
84
Tej Singh89817632019-12-09 16:58:08 -080085 // Initiate the pull. This is a oneway call to a different process, except
86 // in unit tests. In process calls are not oneway.
Tej Singh6a5c9432019-10-11 11:07:06 -070087 Status status = mCallback->onPullAtom(mTagId, resultReceiver);
Tej Singha0c89dd2019-01-25 16:39:18 -080088 if (!status.isOk()) {
Tej Singh873e91a2020-04-28 00:33:11 -070089 StatsdStats::getInstance().notePullBinderCallFailed(mTagId);
Tej Singha0c89dd2019-01-25 16:39:18 -080090 return false;
91 }
Tej Singh6a5c9432019-10-11 11:07:06 -070092
93 {
94 unique_lock<mutex> unique_lk(*cv_mutex);
Tej Singh6a5c9432019-10-11 11:07:06 -070095 // Wait until the pull finishes, or until the pull timeout.
Tej Singh5b4951b2020-01-24 13:23:56 -080096 cv->wait_for(unique_lk, chrono::nanoseconds(mPullTimeoutNs),
Tej Singh6a5c9432019-10-11 11:07:06 -070097 [pullFinish] { return *pullFinish; });
98 if (!*pullFinish) {
99 // Note: The parent stats puller will also note that there was a timeout and that the
100 // cache should be cleared. Once we migrate all pullers to this callback, we could
101 // consolidate the logic.
102 return true;
103 } else {
104 // Only copy the data if we did not timeout and the pull was successful.
Tej Singh89817632019-12-09 16:58:08 -0800105 if (*pullSuccess) {
Tej Singh6a5c9432019-10-11 11:07:06 -0700106 *data = std::move(*sharedData);
107 }
108 VLOG("StatsCallbackPuller::pull succeeded for %d", mTagId);
109 return *pullSuccess;
110 }
Tej Singha0c89dd2019-01-25 16:39:18 -0800111 }
Tej Singha0c89dd2019-01-25 16:39:18 -0800112}
113
114} // namespace statsd
115} // namespace os
116} // namespace android