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

Commit 88fc522d authored by Avichal Rakesh's avatar Avichal Rakesh
Browse files

cameraservice: Add logId to correlate CameraActionEvents

Cameraservice currently triggers logging of CameraActionEvents (through
CameraServiceProxy) which are logged as independent event. For data
analysis purposes, it would be helpful to have one set of
OPEN-n*SESSION-CLOSE events correlate with each other.

To help with that, an mLogId field was added in CameraSessionStats which
will hold a string identifier. This identifier will be randomly
generated for OPEN events and reused for corresponding
ACTIVE, IDLE, and CLOSE events.

Bug: 271297241
Test: Manually tested that statsd gets the expected logId for camera
      sessions.
Change-Id: Ia26f2cd5d75bdeb15737e85d7df1a46b75993712
parent 91920264
Loading
Loading
Loading
Loading
+16 −1
Original line number Original line Diff line number Diff line
@@ -271,6 +271,7 @@ CameraSessionStats::CameraSessionStats() :
        mApiLevel(0),
        mApiLevel(0),
        mIsNdk(false),
        mIsNdk(false),
        mLatencyMs(-1),
        mLatencyMs(-1),
        mLogId(0),
        mMaxPreviewFps(0),
        mMaxPreviewFps(0),
        mSessionType(0),
        mSessionType(0),
        mInternalReconfigure(0),
        mInternalReconfigure(0),
@@ -281,7 +282,7 @@ CameraSessionStats::CameraSessionStats() :


CameraSessionStats::CameraSessionStats(const String16& cameraId,
CameraSessionStats::CameraSessionStats(const String16& cameraId,
        int facing, int newCameraState, const String16& clientName,
        int facing, int newCameraState, const String16& clientName,
        int apiLevel, bool isNdk, int32_t latencyMs) :
        int apiLevel, bool isNdk, int32_t latencyMs, int64_t logId) :
                mCameraId(cameraId),
                mCameraId(cameraId),
                mFacing(facing),
                mFacing(facing),
                mNewCameraState(newCameraState),
                mNewCameraState(newCameraState),
@@ -289,6 +290,7 @@ CameraSessionStats::CameraSessionStats(const String16& cameraId,
                mApiLevel(apiLevel),
                mApiLevel(apiLevel),
                mIsNdk(isNdk),
                mIsNdk(isNdk),
                mLatencyMs(latencyMs),
                mLatencyMs(latencyMs),
                mLogId(logId),
                mMaxPreviewFps(0),
                mMaxPreviewFps(0),
                mSessionType(0),
                mSessionType(0),
                mInternalReconfigure(0),
                mInternalReconfigure(0),
@@ -347,6 +349,12 @@ status_t CameraSessionStats::readFromParcel(const android::Parcel* parcel) {
        return err;
        return err;
    }
    }


    int64_t logId;
    if ((err = parcel->readInt64(&logId)) != OK) {
        ALOGE("%s: Failed to read log ID from parcel", __FUNCTION__);
        return err;
    }

    float maxPreviewFps;
    float maxPreviewFps;
    if ((err = parcel->readFloat(&maxPreviewFps)) != OK) {
    if ((err = parcel->readFloat(&maxPreviewFps)) != OK) {
        ALOGE("%s: Failed to read maxPreviewFps from parcel", __FUNCTION__);
        ALOGE("%s: Failed to read maxPreviewFps from parcel", __FUNCTION__);
@@ -408,6 +416,7 @@ status_t CameraSessionStats::readFromParcel(const android::Parcel* parcel) {
    mApiLevel = apiLevel;
    mApiLevel = apiLevel;
    mIsNdk = isNdk;
    mIsNdk = isNdk;
    mLatencyMs = latencyMs;
    mLatencyMs = latencyMs;
    mLogId = logId;
    mMaxPreviewFps = maxPreviewFps;
    mMaxPreviewFps = maxPreviewFps;
    mSessionType = sessionType;
    mSessionType = sessionType;
    mInternalReconfigure = internalReconfigure;
    mInternalReconfigure = internalReconfigure;
@@ -464,6 +473,11 @@ status_t CameraSessionStats::writeToParcel(android::Parcel* parcel) const {
        return err;
        return err;
    }
    }


    if ((err = parcel->writeInt64(mLogId)) != OK) {
        ALOGE("%s: Failed to write log ID!", __FUNCTION__);
        return err;
    }

    if ((err = parcel->writeFloat(mMaxPreviewFps)) != OK) {
    if ((err = parcel->writeFloat(mMaxPreviewFps)) != OK) {
        ALOGE("%s: Failed to write maxPreviewFps!", __FUNCTION__);
        ALOGE("%s: Failed to write maxPreviewFps!", __FUNCTION__);
        return err;
        return err;
@@ -508,6 +522,7 @@ status_t CameraSessionStats::writeToParcel(android::Parcel* parcel) const {
        ALOGE("%s: Failed to write video stabilization mode!", __FUNCTION__);
        ALOGE("%s: Failed to write video stabilization mode!", __FUNCTION__);
        return err;
        return err;
    }
    }

    return OK;
    return OK;
}
}


+18 −1
Original line number Original line Diff line number Diff line
@@ -128,6 +128,22 @@ public:
    bool mIsNdk;
    bool mIsNdk;
    // latency in ms for camera open, close, or session creation.
    // latency in ms for camera open, close, or session creation.
    int mLatencyMs;
    int mLatencyMs;

    /*
     * A randomly generated identifier to map the open/active/idle/close stats to each other after
     * being logged. Every 'open' event will have a newly generated id which will be logged with
     * active/idle/closed that correspond to the particular 'open' event.
     *
     * This ID is not meant to be globally unique forever. Probabilistically, this ID can be
     * safely considered unique across all logs from one android build for 48 to 72 hours from
     * its generation. Chances of identifier collisions are significant past a week or two.
     *
     * NOTE: There are no guarantees that the identifiers will be unique. The probability of
     * collision within a short timeframe is low, but any system consuming these identifiers at
     * scale should handle identifier collisions, potentially even from the same device.
     */
    int64_t mLogId;

    float mMaxPreviewFps;
    float mMaxPreviewFps;


    // Session info and statistics
    // Session info and statistics
@@ -146,7 +162,8 @@ public:
    // Constructors
    // Constructors
    CameraSessionStats();
    CameraSessionStats();
    CameraSessionStats(const String16& cameraId, int facing, int newCameraState,
    CameraSessionStats(const String16& cameraId, int facing, int newCameraState,
            const String16& clientName, int apiLevel, bool isNdk, int32_t latencyMs);
                       const String16& clientName, int apiLevel, bool isNdk, int32_t latencyMs,
                       int64_t logId);


    virtual status_t readFromParcel(const android::Parcel* parcel) override;
    virtual status_t readFromParcel(const android::Parcel* parcel) override;
    virtual status_t writeToParcel(android::Parcel* parcel) const override;
    virtual status_t writeToParcel(android::Parcel* parcel) const override;
+6 −1
Original line number Original line Diff line number Diff line
@@ -131,8 +131,13 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
            parcelCamSessionStats.writeInt32(latencyMs);
            parcelCamSessionStats.writeInt32(latencyMs);
        }
        }


        int64_t logId = fdp.ConsumeIntegral<int64_t>();
        if (fdp.ConsumeBool()) {
            parcelCamSessionStats.writeInt64(logId);
        }

        cameraSessionStats = new CameraSessionStats(cameraId, facing, newCameraState, clientName,
        cameraSessionStats = new CameraSessionStats(cameraId, facing, newCameraState, clientName,
                                                    apiLevel, isNdk, latencyMs);
                                                    apiLevel, isNdk, latencyMs, logId);
    }
    }


    if (fdp.ConsumeBool()) {
    if (fdp.ConsumeBool()) {
+19 −4
Original line number Original line Diff line number Diff line
@@ -248,9 +248,12 @@ void CameraServiceProxyWrapper::logOpen(const String8& id, int facing,
            apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
            apiLevel = CameraSessionStats::CAMERA_API_LEVEL_2;
        }
        }


        sessionStats = std::make_shared<CameraSessionStatsWrapper>(String16(id), facing,
        // Generate a new log ID for open events
                CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
        int64_t logId = generateLogId(mRandomDevice);
                apiLevel, isNdk, latencyMs);

        sessionStats = std::make_shared<CameraSessionStatsWrapper>(
                String16(id), facing, CameraSessionStats::CAMERA_STATE_OPEN, clientPackageName,
                apiLevel, isNdk, latencyMs, logId);
        mSessionStatsMap.emplace(id, sessionStats);
        mSessionStatsMap.emplace(id, sessionStats);
        ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
        ALOGV("%s: Adding id %s", __FUNCTION__, id.c_str());
    }
    }
@@ -300,4 +303,16 @@ bool CameraServiceProxyWrapper::isCameraDisabled(int userId) {
    return ret;
    return ret;
}
}


}; // namespace android
int64_t CameraServiceProxyWrapper::generateLogId(std::random_device& randomDevice) {
    int64_t ret = 0;
    do {
        // std::random_device generates 32 bits per call, so we call it twice
        ret = randomDevice();
        ret = ret << 32;
        ret = ret | randomDevice();
    } while (ret == 0); // 0 is not a valid identifier

    return ret;
}

}  // namespace android
+14 −5
Original line number Original line Diff line number Diff line
@@ -24,6 +24,7 @@
#include <utils/String16.h>
#include <utils/String16.h>
#include <utils/StrongPointer.h>
#include <utils/StrongPointer.h>
#include <utils/Timers.h>
#include <utils/Timers.h>
#include <random>


#include <camera/CameraSessionStats.h>
#include <camera/CameraSessionStats.h>


@@ -49,9 +50,10 @@ private:


      public:
      public:
        CameraSessionStatsWrapper(const String16& cameraId, int facing, int newCameraState,
        CameraSessionStatsWrapper(const String16& cameraId, int facing, int newCameraState,
                const String16& clientName, int apiLevel, bool isNdk, int32_t latencyMs) :
                                  const String16& clientName, int apiLevel, bool isNdk,
            mSessionStats(cameraId, facing, newCameraState, clientName, apiLevel, isNdk, latencyMs)
                                  int32_t latencyMs, int64_t logId)
            { }
            : mSessionStats(cameraId, facing, newCameraState, clientName, apiLevel, isNdk,
                            latencyMs, logId) {}


        void onOpen(sp<hardware::ICameraServiceProxy>& proxyBinder);
        void onOpen(sp<hardware::ICameraServiceProxy>& proxyBinder);
        void onClose(sp<hardware::ICameraServiceProxy>& proxyBinder, int32_t latencyMs,
        void onClose(sp<hardware::ICameraServiceProxy>& proxyBinder, int32_t latencyMs,
@@ -69,8 +71,15 @@ private:
    // Map from camera id to the camera's session statistics
    // Map from camera id to the camera's session statistics
    std::map<String8, std::shared_ptr<CameraSessionStatsWrapper>> mSessionStatsMap;
    std::map<String8, std::shared_ptr<CameraSessionStatsWrapper>> mSessionStatsMap;


    std::random_device mRandomDevice;  // pulls 32-bit random numbers from /dev/urandom

    sp<hardware::ICameraServiceProxy> getCameraServiceProxy();
    sp<hardware::ICameraServiceProxy> getCameraServiceProxy();


    // Returns a randomly generated ID that is suitable for logging the event. A new identifier
    // should only be generated for an open event. All other events for the cameraId should use the
    // ID generated for the open event associated with them.
    static int64_t generateLogId(std::random_device& randomDevice);

public:
public:
    CameraServiceProxyWrapper(sp<hardware::ICameraServiceProxy> serviceProxy = nullptr) :
    CameraServiceProxyWrapper(sp<hardware::ICameraServiceProxy> serviceProxy = nullptr) :
            mCameraServiceProxy(serviceProxy)
            mCameraServiceProxy(serviceProxy)