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

Commit dab324f6 authored by Tej Singh's avatar Tej Singh
Browse files

Unregistration of native pullers

Changes the API to return an int instead of a bool.
Creates API to unregister pullers.

Test: atest LibStatsPullTests
Bug: 144373250
Change-Id: I6d395237082337a22f37190343a1fc59703d39bf
parent 044669fa
Loading
Loading
Loading
Loading
+13 −3
Original line number Diff line number Diff line
@@ -23,7 +23,6 @@ extern "C" {
#endif
/*
 * Metadata for registering a stats_pull_atom_callback.
 * All fields are optional, and defaults will be used for unspecified fields.
 */
typedef struct pull_atom_metadata {
    int64_t cool_down_ns;
@@ -34,13 +33,24 @@ typedef struct pull_atom_metadata {

typedef struct pulled_stats_event_list pulled_stats_event_list;

typedef bool (*stats_pull_atom_callback_t)(int32_t atom_tag, pulled_stats_event_list* data,
                                           const void* cookie);
typedef int32_t status_pull_atom_return_t;

enum {
    STATS_PULL_SUCCESS = 0,
    STATS_PULL_SKIP = 1,
};

typedef status_pull_atom_return_t (*stats_pull_atom_callback_t)(int32_t atom_tag,
                                                                pulled_stats_event_list* data,
                                                                void* cookie);

struct stats_event* add_stats_event_to_pull_data(pulled_stats_event_list* pull_data);

void register_stats_pull_atom_callback(int32_t atom_tag, stats_pull_atom_callback_t callback,
                                       pull_atom_metadata* metadata, void* cookie);

void unregister_stats_pull_atom_callback(int32_t atom_tag);

#ifdef __cplusplus
}
#endif
+24 −7
Original line number Diff line number Diff line
@@ -42,7 +42,7 @@ static const int64_t DEFAULT_TIMEOUT_NS = 10000000000LL; // 10 seconds.

class StatsPullAtomCallbackInternal : public android::os::BnPullAtomCallback {
  public:
    StatsPullAtomCallbackInternal(const stats_pull_atom_callback_t callback, const void* cookie,
    StatsPullAtomCallbackInternal(const stats_pull_atom_callback_t callback, void* cookie,
                                  const int64_t coolDownNs, const int64_t timeoutNs,
                                  const std::vector<int32_t> additiveFields)
        : mCallback(callback),
@@ -55,7 +55,8 @@ class StatsPullAtomCallbackInternal : public android::os::BnPullAtomCallback {
            int32_t atomTag,
            const ::android::sp<::android::os::IPullAtomResultReceiver>& resultReceiver) override {
        pulled_stats_event_list statsEventList;
        bool success = mCallback(atomTag, &statsEventList, mCookie);
        int successInt = mCallback(atomTag, &statsEventList, mCookie);
        bool success = successInt == STATS_PULL_SUCCESS;

        // Convert stats_events into StatsEventParcels.
        std::vector<android::util::StatsEventParcel> parcels;
@@ -83,7 +84,7 @@ class StatsPullAtomCallbackInternal : public android::os::BnPullAtomCallback {

  private:
    const stats_pull_atom_callback_t mCallback;
    const void* mCookie;
    void* mCookie;
    const int64_t mCoolDownNs;
    const int64_t mTimeoutNs;
    const std::vector<int32_t> mAdditiveFields;
@@ -146,15 +147,31 @@ void register_stats_pull_atom_callback(int32_t atom_tag, stats_pull_atom_callbac
    }

    std::lock_guard<std::mutex> lg(pullAtomMutex);

    // Always add to the map. If statsd is dead, we will add them when it comes back.
    android::sp<StatsPullAtomCallbackInternal> callbackBinder = new StatsPullAtomCallbackInternal(
            callback, cookie, coolDownNs, timeoutNs, additiveFields);
    mPullers[atom_tag] = callbackBinder;

    const android::sp<android::os::IStatsd> statsService = getStatsServiceLocked();
    if (statsService == nullptr) {
        // Error - statsd not available
        // Statsd not available
        return;
    }

    android::sp<StatsPullAtomCallbackInternal> callbackBinder = new StatsPullAtomCallbackInternal(
            callback, cookie, coolDownNs, timeoutNs, additiveFields);
    mPullers[atom_tag] = callbackBinder;
    statsService->registerNativePullAtomCallback(atom_tag, coolDownNs, timeoutNs, additiveFields,
                                                 callbackBinder);
}

void unregister_stats_pull_atom_callback(int32_t atom_tag) {
    std::lock_guard<std::mutex> lg(pullAtomMutex);
    // Always remove the puller from our map.
    // If statsd is down, we will not register it when it comes back.
    mPullers.erase(atom_tag);
    const android::sp<android::os::IStatsd> statsService = getStatsServiceLocked();
    if (statsService == nullptr) {
        // Statsd not available
        return;
    }
    statsService->unregisterNativePullAtomCallback(atom_tag);
}