blob: 92db68477dbdbc19c819773d1f3a2306e183b659 [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
22#include <android/os/IPullAtomCallback.h>
23#include <android/util/StatsEvent.h>
24
25#include "PullResultReceiver.h"
26#include "StatsPullerManager.h"
Tej Singha0c89dd2019-01-25 16:39:18 -080027#include "logd/LogEvent.h"
28#include "stats_log_util.h"
29
30using namespace android::binder;
Tej Singh6a5c9432019-10-11 11:07:06 -070031using namespace android::util;
32using namespace std;
Tej Singha0c89dd2019-01-25 16:39:18 -080033
34namespace android {
35namespace os {
36namespace statsd {
37
Tej Singh6a5c9432019-10-11 11:07:06 -070038StatsCallbackPuller::StatsCallbackPuller(int tagId, const sp<IPullAtomCallback>& callback)
39 : StatsPuller(tagId), mCallback(callback) {
40 VLOG("StatsCallbackPuller created for tag %d", tagId);
Tej Singha0c89dd2019-01-25 16:39:18 -080041}
42
43bool StatsCallbackPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
44 VLOG("StatsCallbackPuller called for tag %d", mTagId)
45 if(mCallback == nullptr) {
46 ALOGW("No callback registered");
47 return false;
48 }
Tej Singh6a5c9432019-10-11 11:07:06 -070049
50 // Shared variables needed in the result receiver.
51 shared_ptr<mutex> cv_mutex = make_shared<mutex>();
52 shared_ptr<condition_variable> cv = make_shared<condition_variable>();
53 shared_ptr<bool> pullFinish = make_shared<bool>(false);
54 shared_ptr<bool> pullSuccess = make_shared<bool>(false);
55 shared_ptr<vector<shared_ptr<LogEvent>>> sharedData =
56 make_shared<vector<shared_ptr<LogEvent>>>();
57
58 sp<PullResultReceiver> resultReceiver = new PullResultReceiver(
59 [cv_mutex, cv, pullFinish, pullSuccess, sharedData](
60 int32_t atomTag, bool success, const vector<StatsEvent>& output) {
61 // This is the result of the pull, executing in a statsd binder thread.
62 // The pull could have taken a long time, and we should only modify
63 // data (the output param) if the pointer is in scope and the pull did not time out.
64 {
65 lock_guard<mutex> lk(*cv_mutex);
66 // TODO: fill the shared vector of LogEvents once StatsEvent is complete.
67 *pullSuccess = success;
68 *pullFinish = true;
69 }
70 cv->notify_one();
71 });
72
73 // Initiate the pull.
74 Status status = mCallback->onPullAtom(mTagId, resultReceiver);
Tej Singha0c89dd2019-01-25 16:39:18 -080075 if (!status.isOk()) {
Tej Singha0c89dd2019-01-25 16:39:18 -080076 return false;
77 }
Tej Singh6a5c9432019-10-11 11:07:06 -070078
79 {
80 unique_lock<mutex> unique_lk(*cv_mutex);
81 int64_t pullTimeoutNs =
82 StatsPullerManager::kAllPullAtomInfo.at({.atomTag = mTagId}).pullTimeoutNs;
83 // Wait until the pull finishes, or until the pull timeout.
84 cv->wait_for(unique_lk, chrono::nanoseconds(pullTimeoutNs),
85 [pullFinish] { return *pullFinish; });
86 if (!*pullFinish) {
87 // Note: The parent stats puller will also note that there was a timeout and that the
88 // cache should be cleared. Once we migrate all pullers to this callback, we could
89 // consolidate the logic.
90 return true;
91 } else {
92 // Only copy the data if we did not timeout and the pull was successful.
93 if (pullSuccess) {
94 *data = std::move(*sharedData);
95 }
96 VLOG("StatsCallbackPuller::pull succeeded for %d", mTagId);
97 return *pullSuccess;
98 }
Tej Singha0c89dd2019-01-25 16:39:18 -080099 }
Tej Singha0c89dd2019-01-25 16:39:18 -0800100}
101
102} // namespace statsd
103} // namespace os
104} // namespace android