本讲是Android Camera性能分析专题的第29讲,我们来优化Camera LatencyHistogram,包括如下内容:
- 直方图统计时添加平均值、最大值、最小值、Camera ID
- 为Stream直方图添加Size和Name
资源 | 描述 |
---|---|
在线课程 | 极客笔记在线课程 |
知识星球 | 星球名称:深入浅出Android Camera 星球ID: 17296815 |
极客笔记圈 |
直方图统计时添加平均值、最大值、最小值、Camera ID
代码见视频讲解,运行结果如下:
为Stream直方图添加Size和Name
代码见视频讲解,运行结果如下:
代码修改点
diff --git a/services/camera/libcameraservice/Android.bp b/services/camera/libcameraservice/Android.bp
index 981c569..dc02162 100644
--- a/services/camera/libcameraservice/Android.bp
+++ b/services/camera/libcameraservice/Android.bp
@@ -196,6 +196,7 @@ cc_library_shared {
"-Wextra",
"-Werror",
"-Wno-ignored-qualifiers",
+ "-DDeepinout",
],
}
diff --git a/services/camera/libcameraservice/api2/CameraDeviceClient.cpp b/services/camera/libcameraservice/api2/CameraDeviceClient.cpp
index 5e91501..554e267 100644
--- a/services/camera/libcameraservice/api2/CameraDeviceClient.cpp
+++ b/services/camera/libcameraservice/api2/CameraDeviceClient.cpp
@@ -958,12 +958,14 @@ binder::Status CameraDeviceClient::createStream(
static_cast(outputConfiguration.getRotation()),
&streamId, physicalCameraId, streamInfo.sensorPixelModesUsed, &surfaceIds,
outputConfiguration.getSurfaceSetID(), isShared, isMultiResolution);
+
if (err == OK) {
Mutex::Autolock l(mCompositeLock);
mCompositeStreamMap.add(IInterface::asBinder(surfaces[0]->getIGraphicBufferProducer()),
compositeStream);
}
} else {
+
err = mDevice->createStream(surfaces, deferredConsumer, streamInfo.width,
streamInfo.height, streamInfo.format, streamInfo.dataSpace,
static_cast(outputConfiguration.getRotation()),
diff --git a/services/camera/libcameraservice/device3/Camera3Device.cpp b/services/camera/libcameraservice/device3/Camera3Device.cpp
index 7c2f34f..e4361ac 100644
--- a/services/camera/libcameraservice/device3/Camera3Device.cpp
+++ b/services/camera/libcameraservice/device3/Camera3Device.cpp
@@ -1139,6 +1139,9 @@ status_t Camera3Device::createStream(const std::vector>& consumers,
isMultiResolution, dynamicRangeProfile, streamUseCase, mDeviceTimeBaseIsRealtime,
timestampBase, mirrorMode);
}
+#ifdef Deepinout
+ newStream->mLogicalCameraId = mId;
+#endif
size_t consumerCount = consumers.size();
for (size_t i = 0; i < consumerCount; i++) {
diff --git a/services/camera/libcameraservice/device3/Camera3OutputStream.cpp b/services/camera/libcameraservice/device3/Camera3OutputStream.cpp
index 1e20ee0..204947c 100644
--- a/services/camera/libcameraservice/device3/Camera3OutputStream.cpp
+++ b/services/camera/libcameraservice/device3/Camera3OutputStream.cpp
@@ -71,6 +71,9 @@ Camera3OutputStream::Camera3OutputStream(int id,
mDropBuffers(false),
mMirrorMode(mirrorMode),
mDequeueBufferLatency(kDequeueLatencyBinSize),
+#ifdef Deepinout
+ mQueueBufferIntervalHistogram(kQueueBufferIntervalBinSize),
+#endif
mIPCTransport(transport) {
if (mConsumer == NULL) {
@@ -104,6 +107,9 @@ Camera3OutputStream::Camera3OutputStream(int id,
mDropBuffers(false),
mMirrorMode(mirrorMode),
mDequeueBufferLatency(kDequeueLatencyBinSize),
+#ifdef Deepinout
+ mQueueBufferIntervalHistogram(kQueueBufferIntervalBinSize),
+#endif
mIPCTransport(transport) {
if (format != HAL_PIXEL_FORMAT_BLOB && format != HAL_PIXEL_FORMAT_RAW_OPAQUE) {
@@ -144,6 +150,9 @@ Camera3OutputStream::Camera3OutputStream(int id,
mDropBuffers(false),
mMirrorMode(mirrorMode),
mDequeueBufferLatency(kDequeueLatencyBinSize),
+#ifdef Deepinout
+ mQueueBufferIntervalHistogram(kQueueBufferIntervalBinSize),
+#endif
mIPCTransport(transport) {
// Deferred consumer only support preview surface format now.
if (format != HAL_PIXEL_FORMAT_IMPLEMENTATION_DEFINED) {
@@ -192,6 +201,9 @@ Camera3OutputStream::Camera3OutputStream(int id, camera_stream_type_t type,
mDropBuffers(false),
mMirrorMode(mirrorMode),
mDequeueBufferLatency(kDequeueLatencyBinSize),
+#ifdef Deepinout
+ mQueueBufferIntervalHistogram(kQueueBufferIntervalBinSize),
+#endif
mIPCTransport(transport) {
bool needsReleaseNotify = setId > CAMERA3_STREAM_SET_ID_INVALID;
@@ -312,6 +324,17 @@ status_t Camera3OutputStream::returnBufferLocked(
mLastTimestamp = timestamp;
mFrameCount++;
+#ifdef Deepinout
+ nsecs_t currentQueueBufferTime = systemTime(SYSTEM_TIME_MONOTONIC);
+ if (mLastQueueBufferTime > 0) {
+ mQueueBufferIntervalHistogram.add(mLastQueueBufferTime, currentQueueBufferTime);
+ char traceLog[48];
+ snprintf(traceLog, sizeof(traceLog), "queueBufferInterval-%d", mId);
+ ATRACE_ASYNC_BEGIN(traceLog, mId);
+ ATRACE_ASYNC_END(traceLog, mId);
+ }
+ mLastQueueBufferTime = currentQueueBufferTime;
+#endif
return OK;
}
@@ -521,6 +544,10 @@ void Camera3OutputStream::dump(int fd, const Vector &args) const {
mDequeueBufferLatency.dump(fd,
" DequeueBuffer latency histogram:");
+#ifdef Deepinout
+ mQueueBufferIntervalHistogram.dump(fd,
+ " QueueBufferInterval histogram:");
+#endif
}
status_t Camera3OutputStream::setTransform(int transform, bool mayChangeMirror) {
@@ -1015,8 +1042,25 @@ status_t Camera3OutputStream::disconnectLocked() {
mState = (mState == STATE_IN_RECONFIG) ? STATE_IN_CONFIG
: STATE_CONSTRUCTED;
-
+#ifndef Deepinout
mDequeueBufferLatency.log("Stream %d dequeueBuffer latency histogram", mId);
+#else
+ mDequeueBufferLatency.log("CamId(L:P):(%s:%s) Stream %d-%s-%dx%d dequeueBuffer latency histogram",
+ mLogicalCameraId.string(),
+ getPhysicalCameraId().string(),
+ mId,
+ mConsumerName.string(),
+ camera_stream::width,
+ camera_stream::height);
+ mQueueBufferIntervalHistogram.log("CamId(L:P):(%s:%s) Stream %d-%s-%dx%d queueBuffer interval histogram",
+ mLogicalCameraId.string(),
+ getPhysicalCameraId().string(),
+ mId,
+ mConsumerName.string(),
+ camera_stream::width,
+ camera_stream::height);
+ mQueueBufferIntervalHistogram.reset();
+#endif
mDequeueBufferLatency.reset();
return OK;
}
diff --git a/services/camera/libcameraservice/device3/Camera3OutputStream.h b/services/camera/libcameraservice/device3/Camera3OutputStream.h
index e8065ce..05831ec 100644
--- a/services/camera/libcameraservice/device3/Camera3OutputStream.h
+++ b/services/camera/libcameraservice/device3/Camera3OutputStream.h
@@ -408,6 +408,11 @@ class Camera3OutputStream :
static const int32_t kDequeueLatencyBinSize = 5; // in ms
CameraLatencyHistogram mDequeueBufferLatency;
+#ifdef Deepinout
+ nsecs_t mLastQueueBufferTime = 0;
+ static const int32_t kQueueBufferIntervalBinSize = 35; // in ms
+ CameraLatencyHistogram mQueueBufferIntervalHistogram;
+#endif
IPCTransport mIPCTransport = IPCTransport::INVALID;
int mImageDumpMask = 0;
diff --git a/services/camera/libcameraservice/device3/Camera3Stream.h b/services/camera/libcameraservice/device3/Camera3Stream.h
index d429e6c..f7784bd 100644
--- a/services/camera/libcameraservice/device3/Camera3Stream.h
+++ b/services/camera/libcameraservice/device3/Camera3Stream.h
@@ -469,6 +469,10 @@ class Camera3Stream :
void fireBufferRequestForFrameNumber(uint64_t frameNumber,
const CameraMetadata& settings) override;
+#ifdef Deepinout
+ String8 mLogicalCameraId;
+#endif
+
protected:
const int mId;
/**
diff --git a/services/camera/libcameraservice/utils/LatencyHistogram.cpp b/services/camera/libcameraservice/utils/LatencyHistogram.cpp
index e2bdc43..a48ada7 100644
--- a/services/camera/libcameraservice/utils/LatencyHistogram.cpp
+++ b/services/camera/libcameraservice/utils/LatencyHistogram.cpp
@@ -27,7 +27,14 @@ CameraLatencyHistogram::CameraLatencyHistogram(int32_t binSizeMs, int32_t binCou
mBinSizeMs(binSizeMs),
mBinCount(binCount),
mBins(binCount),
+#ifndef Deepinout
mTotalCount(0) {
+#else
+ mTotoalDuration(0),
+ mMaxDuration(0),
+ mMinDuration(0),
+ mTotalCount(0) {
+#endif
}
void CameraLatencyHistogram::add(nsecs_t start, nsecs_t end) {
@@ -43,6 +50,15 @@ void CameraLatencyHistogram::add(nsecs_t start, nsecs_t end) {
mBins[binIndex]++;
mTotalCount++;
+#ifdef Deepinout
+ mTotoalDuration += duration;
+ if (mMaxDuration == 0 || mMaxDuration < duration) {
+ mMaxDuration = duration;
+ }
+ if (mMinDuration ==0 || mMinDuration > duration) {
+ mMinDuration = duration;
+ }
+#endif
}
void CameraLatencyHistogram::reset() {
@@ -56,7 +72,16 @@ void CameraLatencyHistogram::dump(int fd, const char* name) const {
}
String8 lines;
+#ifndef Deepinout
lines.appendFormat("%s (%" PRId64 ") samples\n", name, mTotalCount);
+#else
+ lines.appendFormat("%s (%" PRId64 ") samples , avg:max:min(%.2f ms:%.2f ms:%.2f):\n",
+ name,
+ mTotalCount,
+ (float)(mTotoalDuration / 1e6) / mTotalCount,
+ (float)(mMaxDuration / 1e6),
+ (float)(mMinDuration / 1e6));
+#endif
String8 lineBins, lineBinCounts;
formatHistogramText(lineBins, lineBinCounts);
@@ -77,7 +102,16 @@ void CameraLatencyHistogram::log(const char* fmt, ...) {
va_list args;
va_start(args, fmt);
String8 histogramName = String8::formatV(fmt, args);
+#ifndef Deepinout
ALOGI("%s (%" PRId64 ") samples:", histogramName.string(), mTotalCount);
+#else
+ ALOGI("%s (%" PRId64 ") samples, avg:max:min(%.2f ms:%.2f ms:%.2f):",
+ histogramName.string(),
+ mTotalCount,
+ (float)(mTotoalDuration / 1e6) / mTotalCount,
+ (float)(mMaxDuration / 1e6),
+ (float)(mMinDuration / 1e6));
+#endif
va_end(args);
String8 lineBins, lineBinCounts;
diff --git a/services/camera/libcameraservice/utils/LatencyHistogram.h b/services/camera/libcameraservice/utils/LatencyHistogram.h
index bfd9b1b..d0dcbf7 100644
--- a/services/camera/libcameraservice/utils/LatencyHistogram.h
+++ b/services/camera/libcameraservice/utils/LatencyHistogram.h
@@ -38,6 +38,11 @@ private:
int32_t mBinSizeMs;
int32_t mBinCount;
std::vector mBins;
+#ifdef Deepinout
+ nsecs_t mTotoalDuration;
+ nsecs_t mMaxDuration;
+ nsecs_t mMinDuration;
+#endif
uint64_t mTotalCount;
void formatHistogramText(String8& lineBins, String8& lineBinCounts) const;