From 9a668fbda69721fb0f07d2df89ab5a80a211d36f Mon Sep 17 00:00:00 2001 From: Manisha Jajoo Date: Wed, 21 Aug 2019 19:22:00 +0530 Subject: [PATCH] Benchmark: Add common utilities Test: builds Bug: 140051680 Change-Id: Ic55a927684c39fd70a86dd9b83ca7417694a20a1 --- .../benchmark/src/native/common/Android.bp | 77 ++++++++++++++++++ .../src/native/common/BenchmarkCommon.h | 28 +++++++ .../benchmark/src/native/common/Timer.cpp | 65 +++++++++++++++ .../tests/benchmark/src/native/common/Timer.h | 81 +++++++++++++++++++ 4 files changed, 251 insertions(+) create mode 100644 media/tests/benchmark/src/native/common/Android.bp create mode 100644 media/tests/benchmark/src/native/common/BenchmarkCommon.h create mode 100644 media/tests/benchmark/src/native/common/Timer.cpp create mode 100644 media/tests/benchmark/src/native/common/Timer.h diff --git a/media/tests/benchmark/src/native/common/Android.bp b/media/tests/benchmark/src/native/common/Android.bp new file mode 100644 index 0000000000..e4f9ab696b --- /dev/null +++ b/media/tests/benchmark/src/native/common/Android.bp @@ -0,0 +1,77 @@ +/* + * 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. + */ + +cc_library_static { + name: "libbenchmark_common", + defaults: [ + "libbenchmark-defaults", + "libbenchmark_soft_sanitize_all-defaults", + ], + + srcs: [ + "Timer.cpp", + ], + + export_include_dirs: ["."], + + ldflags: ["-Wl,-Bsymbolic"] +} + +cc_defaults { + name: "libbenchmark_common-defaults", + + defaults: [ + "libbenchmark-defaults", + ], + + static_libs: [ + "libbenchmark_common", + ], +} + +cc_defaults { + name: "libbenchmark-defaults", + + header_libs: [ + "media_ndk_headers", + ], + + shared_libs: [ + "libmediandk", + "liblog", + "libutils", + ], + + cflags: [ + "-Wall", + "-Werror", + ] +} + +// public dependency for native implementation +// to be used by code under media/benchmark/* only +cc_defaults { + name: "libbenchmark_soft_sanitize_all-defaults", + + sanitize: { + misc_undefined: [ + "unsigned-integer-overflow", + "signed-integer-overflow", + ], + cfi: true, + address: true, + } +} diff --git a/media/tests/benchmark/src/native/common/BenchmarkCommon.h b/media/tests/benchmark/src/native/common/BenchmarkCommon.h new file mode 100644 index 0000000000..3ef47c2a77 --- /dev/null +++ b/media/tests/benchmark/src/native/common/BenchmarkCommon.h @@ -0,0 +1,28 @@ +/* + * 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. + */ + +#ifndef __BENCHMARK_COMMON_H__ +#define __BENCHMARK_COMMON_H__ + +#include + +#include + +constexpr uint32_t kQueueDequeueTimeoutUs = 1000; +constexpr uint32_t kMaxCSDStrlen = 16; +constexpr uint32_t kMaxBufferSize = 1024 * 1024 * 16; + +#endif // __BENCHMARK_COMMON_H__ diff --git a/media/tests/benchmark/src/native/common/Timer.cpp b/media/tests/benchmark/src/native/common/Timer.cpp new file mode 100644 index 0000000000..0487123c00 --- /dev/null +++ b/media/tests/benchmark/src/native/common/Timer.cpp @@ -0,0 +1,65 @@ +/* + * 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. + */ + +//#define LOG_NDEBUG 0 +#define LOG_TAG "Timer" + +#include +#include +#include + +#include "Timer.h" + +/** + * Dumps the stats of the operation for a given input media. + * + * \param operation describes the operation performed on the input media + * (i.e. extract/mux/decode/encode) + * \param inputReference input media + * \param duarationUs is a duration of the input media in microseconds. + */ +void Timer::dumpStatistics(std::string operation, std::string inputReference, int64_t duarationUs) { + ALOGV("In %s", __func__); + if (!mOutputTimer.size()) { + ALOGE("No output produced"); + return; + } + nsecs_t totalTimeTakenNs = getTotalTime(); + nsecs_t timeTakenPerSec = (totalTimeTakenNs * 1000000) / duarationUs; + nsecs_t timeToFirstFrameNs = *mOutputTimer.begin() - mStartTimeNs; + // get min and max output intervals. + nsecs_t intervalNs; + nsecs_t minTimeTakenNs = INT64_MAX; + nsecs_t maxTimeTakenNs = 0; + nsecs_t prevIntervalNs = mStartTimeNs; + for (int32_t idx = 0; idx < mOutputTimer.size() - 1; idx++) { + intervalNs = mOutputTimer.at(idx) - prevIntervalNs; + prevIntervalNs = mOutputTimer.at(idx); + if (minTimeTakenNs > intervalNs) minTimeTakenNs = intervalNs; + else if (maxTimeTakenNs < intervalNs) maxTimeTakenNs = intervalNs; + } + + // Print the Stats + std::cout << "Input Reference : " << inputReference << endl; + std::cout << "Setup Time in nano sec : " << mInitTimeNs << endl; + std::cout << "Average Time in nano sec : " << totalTimeTakenNs / mOutputTimer.size() << endl; + std::cout << "Time to first frame in nano sec : " << timeToFirstFrameNs << endl; + std::cout << "Time taken (in nano sec) to " << operation + << " 1 sec of content : " << timeTakenPerSec << endl; + std::cout << "Minimum Time in nano sec : " << minTimeTakenNs << endl; + std::cout << "Maximum Time in nano sec : " << maxTimeTakenNs << endl; + std::cout << "Destroy Time in nano sec : " << mDeInitTimeNs << endl; +} diff --git a/media/tests/benchmark/src/native/common/Timer.h b/media/tests/benchmark/src/native/common/Timer.h new file mode 100644 index 0000000000..92af86f71f --- /dev/null +++ b/media/tests/benchmark/src/native/common/Timer.h @@ -0,0 +1,81 @@ +/* + * 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. + */ + +#ifndef __TIMER_H__ +#define __TIMER_H__ + +#include +#include +#include +#include +#include + +using namespace std; + +class Timer { + public: + Timer() { + mInitTimeNs = 0; + mDeInitTimeNs = 0; + } + + ~Timer() { + if (!mInputTimer.empty()) mInputTimer.clear(); + if (!mOutputTimer.empty()) mOutputTimer.clear(); + } + + private: + nsecs_t mInitTimeNs; + nsecs_t mDeInitTimeNs; + nsecs_t mStartTimeNs; + std::vector mInputTimer; + std::vector mOutputTimer; + + public: + nsecs_t getCurTime() { return systemTime(CLOCK_MONOTONIC); } + + void setInitTime(nsecs_t initTime) { mInitTimeNs = initTime; } + + void setDeInitTime(nsecs_t deInitTime) { mDeInitTimeNs = deInitTime; } + + void setStartTime() { mStartTimeNs = systemTime(CLOCK_MONOTONIC); } + + void addInputTime() { mInputTimer.push_back(systemTime(CLOCK_MONOTONIC)); } + + void addOutputTime() { mOutputTimer.push_back(systemTime(CLOCK_MONOTONIC)); } + + void resetTimers() { + if (!mInputTimer.empty()) mInputTimer.clear(); + if (!mOutputTimer.empty()) mOutputTimer.clear(); + } + + std::vector getOutputTimer() { return mOutputTimer; } + + nsecs_t getInitTime() { return mInitTimeNs; } + + nsecs_t getDeInitTime() { return mDeInitTimeNs; } + + nsecs_t getTimeDiff(nsecs_t sTime, nsecs_t eTime) { return (eTime - sTime); } + + nsecs_t getTotalTime() { + if (mOutputTimer.empty()) return -1; + return (*(mOutputTimer.end() - 1) - mStartTimeNs); + } + + void dumpStatistics(std::string operation, std::string inputReference, int64_t duarationUs); +}; + +#endif // __TIMER_H__