Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 6a5c9436 authored by Tej Singh's avatar Tej Singh
Browse files

Statsd implementation of puller API

Internal implementation of the puller API. Registers pullers by putting
them in the kAllPullAtomInfo map. Implements the actual pull,
with condition variables to timeout.

Lastly, keys the kAllPullAtom info by a PullerKey, which is a uid and
atom id. However, the uid is just set to a default of -1 for now. I will
work the security implementation in a follow up CL.
Test: builds, boots
Test: I will write unit tests in the future. It's very difficult to
write any without StatsEvent being completed.

Change-Id: Id602dd297b6ba7df811e2d5ab2e77efc0684e418
parent e9513c5e
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -69,9 +69,11 @@ cc_defaults {
        "src/external/GpuStatsPuller.cpp",
        "src/external/Perfetto.cpp",
        "src/external/PowerStatsPuller.cpp",
        "src/external/PullResultReceiver.cpp",
        "src/external/puller_util.cpp",
        "src/external/ResourceHealthManagerPuller.cpp",
        "src/external/StatsCallbackPuller.cpp",
        "src/external/StatsCallbackPullerDeprecated.cpp",
        "src/external/StatsCompanionServicePuller.cpp",
        "src/external/StatsPuller.cpp",
        "src/external/StatsPullerManager.cpp",
+4 −0
Original line number Diff line number Diff line
@@ -1292,7 +1292,11 @@ Status StatsService::registerPullerCallback(int32_t atomTag,
Status StatsService::registerPullAtomCallback(int32_t uid, int32_t atomTag, int64_t coolDownNs,
                                    int64_t timeoutNs, const std::vector<int32_t>& additiveFields,
                                    const sp<android::os::IPullAtomCallback>& pullerCallback) {
    ENFORCE_UID(AID_SYSTEM);

    VLOG("StatsService::registerPuller called.");
    mPullerManager->RegisterPullAtomCallback(uid, atomTag, coolDownNs, timeoutNs, additiveFields,
                                             pullerCallback);
    return Status::ok();
}

+43 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "PullResultReceiver.h"

using namespace android::binder;
using namespace android::util;
using namespace std;

namespace android {
namespace os {
namespace statsd {

PullResultReceiver::PullResultReceiver(
        std::function<void(int32_t, bool, const vector<android::util::StatsEvent>&)> pullFinishCb)
    : pullFinishCallback(std::move(pullFinishCb)) {
}

Status PullResultReceiver::pullFinished(int32_t atomTag, bool success,
                                        const vector<StatsEvent>& output) {
    pullFinishCallback(atomTag, success, output);
    return Status::ok();
}

PullResultReceiver::~PullResultReceiver() {
}

}  // namespace statsd
}  // namespace os
}  // namespace android
 No newline at end of file
+43 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2019 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <android/os/BnPullAtomResultReceiver.h>

using namespace std;

namespace android {
namespace os {
namespace statsd {

class PullResultReceiver : public BnPullAtomResultReceiver {
public:
    PullResultReceiver(function<void(int32_t, bool, const vector<android::util::StatsEvent>&)>
                               pullFinishCallback);
    ~PullResultReceiver();

    /**
     * Binder call for finishing a pull.
     */
    binder::Status pullFinished(int32_t atomTag, bool success,
                                        const vector<android::util::StatsEvent>& output) override;

private:
    function<void(int32_t, bool, const vector<android::util::StatsEvent>&)> pullFinishCallback;
};

}  // namespace statsd
}  // namespace os
}  // namespace android
+58 −15
Original line number Diff line number Diff line
@@ -17,20 +17,26 @@
#define DEBUG false  // STOPSHIP if true
#include "Log.h"

#include <android/os/IStatsPullerCallback.h>

#include "StatsCallbackPuller.h"

#include <android/os/IPullAtomCallback.h>
#include <android/util/StatsEvent.h>

#include "PullResultReceiver.h"
#include "StatsPullerManager.h"
#include "logd/LogEvent.h"
#include "stats_log_util.h"

using namespace android::binder;
using namespace android::util;
using namespace std;

namespace android {
namespace os {
namespace statsd {

StatsCallbackPuller::StatsCallbackPuller(int tagId, const sp<IStatsPullerCallback>& callback) :
        StatsPuller(tagId), mCallback(callback) {
StatsCallbackPuller::StatsCallbackPuller(int tagId, const sp<IPullAtomCallback>& callback)
    : StatsPuller(tagId), mCallback(callback) {
    VLOG("StatsCallbackPuller created for tag %d", tagId);
}

@@ -40,20 +46,57 @@ bool StatsCallbackPuller::PullInternal(vector<shared_ptr<LogEvent>>* data) {
        ALOGW("No callback registered");
        return false;
    }
    int64_t wallClockTimeNs = getWallClockNs();
    int64_t elapsedTimeNs = getElapsedRealtimeNs();
    vector<StatsLogEventWrapper> returned_value;
    Status status = mCallback->pullData(mTagId, elapsedTimeNs, wallClockTimeNs, &returned_value);

    // Shared variables needed in the result receiver.
    shared_ptr<mutex> cv_mutex = make_shared<mutex>();
    shared_ptr<condition_variable> cv = make_shared<condition_variable>();
    shared_ptr<bool> pullFinish = make_shared<bool>(false);
    shared_ptr<bool> pullSuccess = make_shared<bool>(false);
    shared_ptr<vector<shared_ptr<LogEvent>>> sharedData =
            make_shared<vector<shared_ptr<LogEvent>>>();

    sp<PullResultReceiver> resultReceiver = new PullResultReceiver(
            [cv_mutex, cv, pullFinish, pullSuccess, sharedData](
                    int32_t atomTag, bool success, const vector<StatsEvent>& output) {
                // This is the result of the pull, executing in a statsd binder thread.
                // The pull could have taken a long time, and we should only modify
                // data (the output param) if the pointer is in scope and the pull did not time out.
                {
                    lock_guard<mutex> lk(*cv_mutex);
                    // TODO: fill the shared vector of LogEvents once StatsEvent is complete.
                    *pullSuccess = success;
                    *pullFinish = true;
                }
                cv->notify_one();
            });

    // Initiate the pull.
    Status status = mCallback->onPullAtom(mTagId, resultReceiver);
    if (!status.isOk()) {
        ALOGW("StatsCallbackPuller::pull failed for %d", mTagId);
        return false;
    }
    data->clear();
    for (const StatsLogEventWrapper& it: returned_value) {
        LogEvent::createLogEvents(it, *data);

    {
        unique_lock<mutex> unique_lk(*cv_mutex);
        int64_t pullTimeoutNs =
                StatsPullerManager::kAllPullAtomInfo.at({.atomTag = mTagId}).pullTimeoutNs;
        // Wait until the pull finishes, or until the pull timeout.
        cv->wait_for(unique_lk, chrono::nanoseconds(pullTimeoutNs),
                     [pullFinish] { return *pullFinish; });
        if (!*pullFinish) {
            // Note: The parent stats puller will also note that there was a timeout and that the
            // cache should be cleared. Once we migrate all pullers to this callback, we could
            // consolidate the logic.
            return true;
        } else {
            // Only copy the data if we did not timeout and the pull was successful.
            if (pullSuccess) {
                *data = std::move(*sharedData);
            }
            VLOG("StatsCallbackPuller::pull succeeded for %d", mTagId);
    return true;
            return *pullSuccess;
        }
    }
}

}  // namespace statsd
Loading