diff options
Diffstat (limited to 'services')
-rw-r--r-- | services/camera/libcameraservice/CameraService.h | 2 | ||||
-rw-r--r-- | services/camera/libcameraservice/utils/RingBuffer.h | 361 | ||||
-rw-r--r-- | services/mediaresourcemanager/ResourceManagerService.cpp | 4 | ||||
-rw-r--r-- | services/mediaresourcemanager/ServiceLog.cpp | 29 | ||||
-rw-r--r-- | services/mediaresourcemanager/ServiceLog.h | 10 | ||||
-rw-r--r-- | services/mediaresourcemanager/test/ServiceLog_test.cpp | 53 |
6 files changed, 62 insertions, 397 deletions
diff --git a/services/camera/libcameraservice/CameraService.h b/services/camera/libcameraservice/CameraService.h index 502fcfa..31edf44 100644 --- a/services/camera/libcameraservice/CameraService.h +++ b/services/camera/libcameraservice/CameraService.h @@ -38,9 +38,9 @@ #include "CameraFlashlight.h" #include "common/CameraModule.h" +#include "media/RingBuffer.h" #include "utils/AutoConditionLock.h" #include "utils/ClientManager.h" -#include "utils/RingBuffer.h" #include <set> #include <string> diff --git a/services/camera/libcameraservice/utils/RingBuffer.h b/services/camera/libcameraservice/utils/RingBuffer.h deleted file mode 100644 index df7c00e..0000000 --- a/services/camera/libcameraservice/utils/RingBuffer.h +++ /dev/null @@ -1,361 +0,0 @@ -/* - * Copyright (C) 2015 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 ANDROID_SERVICE_UTILS_RING_BUFFER_H -#define ANDROID_SERVICE_UTILS_RING_BUFFER_H - -#include <utils/Log.h> -#include <cutils/compiler.h> - -#include <iterator> -#include <utility> -#include <vector> - -namespace android { - -/** - * A RingBuffer class that maintains an array of objects that can grow up to a certain size. - * Elements added to the RingBuffer are inserted in the logical front of the buffer, and - * invalidate all current iterators for that RingBuffer object. - */ -template <class T> -class RingBuffer final { -public: - - /** - * Construct a RingBuffer that can grow up to the given length. - */ - RingBuffer(size_t length); - - /** - * Forward iterator to this class. Implements an std:forward_iterator. - */ - class iterator : public std::iterator<std::forward_iterator_tag, T> { - public: - iterator(T* ptr, size_t size, size_t pos, size_t ctr); - - iterator& operator++(); - - iterator operator++(int); - - bool operator==(const iterator& rhs); - - bool operator!=(const iterator& rhs); - - T& operator*(); - - T* operator->(); - - private: - T* mPtr; - size_t mSize; - size_t mPos; - size_t mCtr; - }; - - /** - * Constant forward iterator to this class. Implements an std:forward_iterator. - */ - class const_iterator : public std::iterator<std::forward_iterator_tag, T> { - public: - const_iterator(const T* ptr, size_t size, size_t pos, size_t ctr); - - const_iterator& operator++(); - - const_iterator operator++(int); - - bool operator==(const const_iterator& rhs); - - bool operator!=(const const_iterator& rhs); - - const T& operator*(); - - const T* operator->(); - - private: - const T* mPtr; - size_t mSize; - size_t mPos; - size_t mCtr; - }; - - /** - * Adds item to the front of this RingBuffer. If the RingBuffer is at its maximum length, - * this will result in the last element being replaced (this is done using the element's - * assignment operator). - * - * All current iterators are invalidated. - */ - void add(const T& item); - - /** - * Moves item to the front of this RingBuffer. Following a call to this, item should no - * longer be used. If the RingBuffer is at its maximum length, this will result in the - * last element being replaced (this is done using the element's assignment operator). - * - * All current iterators are invalidated. - */ - void add(T&& item); - - /** - * Construct item in-place in the front of this RingBuffer using the given arguments. If - * the RingBuffer is at its maximum length, this will result in the last element being - * replaced (this is done using the element's assignment operator). - * - * All current iterators are invalidated. - */ - template <class... Args> - void emplace(Args&&... args); - - /** - * Get an iterator to the front of this RingBuffer. - */ - iterator begin(); - - /** - * Get an iterator to the end of this RingBuffer. - */ - iterator end(); - - /** - * Get a const_iterator to the front of this RingBuffer. - */ - const_iterator begin() const; - - /** - * Get a const_iterator to the end of this RingBuffer. - */ - const_iterator end() const; - - /** - * Return a reference to the element at a given index. If the index is out of range for - * this ringbuffer, [0, size), the behavior for this is undefined. - */ - T& operator[](size_t index); - - /** - * Return a const reference to the element at a given index. If the index is out of range - * for this ringbuffer, [0, size), the behavior for this is undefined. - */ - const T& operator[](size_t index) const; - - /** - * Return the current size of this RingBuffer. - */ - size_t size() const; - - /** - * Remove all elements from this RingBuffer and set the size to 0. - */ - void clear(); - -private: - size_t mFrontIdx; - size_t mMaxBufferSize; - std::vector<T> mBuffer; -}; // class RingBuffer - - -template <class T> -RingBuffer<T>::RingBuffer(size_t length) : mFrontIdx{0}, mMaxBufferSize{length} {} - -template <class T> -RingBuffer<T>::iterator::iterator(T* ptr, size_t size, size_t pos, size_t ctr) : - mPtr{ptr}, mSize{size}, mPos{pos}, mCtr{ctr} {} - -template <class T> -typename RingBuffer<T>::iterator& RingBuffer<T>::iterator::operator++() { - ++mCtr; - - if (CC_UNLIKELY(mCtr == mSize)) { - mPos = mSize; - return *this; - } - - mPos = ((CC_UNLIKELY(mPos == 0)) ? mSize - 1 : mPos - 1); - return *this; -} - -template <class T> -typename RingBuffer<T>::iterator RingBuffer<T>::iterator::operator++(int) { - iterator tmp{mPtr, mSize, mPos, mCtr}; - ++(*this); - return tmp; -} - -template <class T> -bool RingBuffer<T>::iterator::operator==(const iterator& rhs) { - return (mPtr + mPos) == (rhs.mPtr + rhs.mPos); -} - -template <class T> -bool RingBuffer<T>::iterator::operator!=(const iterator& rhs) { - return (mPtr + mPos) != (rhs.mPtr + rhs.mPos); -} - -template <class T> -T& RingBuffer<T>::iterator::operator*() { - return *(mPtr + mPos); -} - -template <class T> -T* RingBuffer<T>::iterator::operator->() { - return mPtr + mPos; -} - -template <class T> -RingBuffer<T>::const_iterator::const_iterator(const T* ptr, size_t size, size_t pos, size_t ctr) : - mPtr{ptr}, mSize{size}, mPos{pos}, mCtr{ctr} {} - -template <class T> -typename RingBuffer<T>::const_iterator& RingBuffer<T>::const_iterator::operator++() { - ++mCtr; - - if (CC_UNLIKELY(mCtr == mSize)) { - mPos = mSize; - return *this; - } - - mPos = ((CC_UNLIKELY(mPos == 0)) ? mSize - 1 : mPos - 1); - return *this; -} - -template <class T> -typename RingBuffer<T>::const_iterator RingBuffer<T>::const_iterator::operator++(int) { - const_iterator tmp{mPtr, mSize, mPos, mCtr}; - ++(*this); - return tmp; -} - -template <class T> -bool RingBuffer<T>::const_iterator::operator==(const const_iterator& rhs) { - return (mPtr + mPos) == (rhs.mPtr + rhs.mPos); -} - -template <class T> -bool RingBuffer<T>::const_iterator::operator!=(const const_iterator& rhs) { - return (mPtr + mPos) != (rhs.mPtr + rhs.mPos); -} - -template <class T> -const T& RingBuffer<T>::const_iterator::operator*() { - return *(mPtr + mPos); -} - -template <class T> -const T* RingBuffer<T>::const_iterator::operator->() { - return mPtr + mPos; -} - -template <class T> -void RingBuffer<T>::add(const T& item) { - if (mBuffer.size() < mMaxBufferSize) { - mBuffer.push_back(item); - mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); - return; - } - - mBuffer[mFrontIdx] = item; - mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); -} - -template <class T> -void RingBuffer<T>::add(T&& item) { - if (mBuffer.size() != mMaxBufferSize) { - mBuffer.push_back(std::forward<T>(item)); - mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); - return; - } - - // Only works for types with move assignment operator - mBuffer[mFrontIdx] = std::forward<T>(item); - mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); -} - -template <class T> -template <class... Args> -void RingBuffer<T>::emplace(Args&&... args) { - if (mBuffer.size() != mMaxBufferSize) { - mBuffer.emplace_back(std::forward<Args>(args)...); - mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); - return; - } - - // Only works for types with move assignment operator - mBuffer[mFrontIdx] = T(std::forward<Args>(args)...); - mFrontIdx = ((mFrontIdx + 1) % mMaxBufferSize); -} - -template <class T> -typename RingBuffer<T>::iterator RingBuffer<T>::begin() { - size_t tmp = (mBuffer.size() == 0) ? 0 : mBuffer.size() - 1; - return iterator(mBuffer.data(), mBuffer.size(), (mFrontIdx == 0) ? tmp : mFrontIdx - 1, 0); -} - -template <class T> -typename RingBuffer<T>::iterator RingBuffer<T>::end() { - size_t s = mBuffer.size(); - return iterator(mBuffer.data(), s, s, s); -} - -template <class T> -typename RingBuffer<T>::const_iterator RingBuffer<T>::begin() const { - size_t tmp = (mBuffer.size() == 0) ? 0 : mBuffer.size() - 1; - return const_iterator(mBuffer.data(), mBuffer.size(), - (mFrontIdx == 0) ? tmp : mFrontIdx - 1, 0); -} - -template <class T> -typename RingBuffer<T>::const_iterator RingBuffer<T>::end() const { - size_t s = mBuffer.size(); - return const_iterator(mBuffer.data(), s, s, s); -} - -template <class T> -T& RingBuffer<T>::operator[](size_t index) { - LOG_ALWAYS_FATAL_IF(index >= mBuffer.size(), "Index %zu out of bounds, size is %zu.", - index, mBuffer.size()); - size_t pos = (index >= mFrontIdx) ? - mBuffer.size() - 1 - (index - mFrontIdx) : mFrontIdx - 1 - index; - return mBuffer[pos]; -} - -template <class T> -const T& RingBuffer<T>::operator[](size_t index) const { - LOG_ALWAYS_FATAL_IF(index >= mBuffer.size(), "Index %zu out of bounds, size is %zu.", - index, mBuffer.size()); - size_t pos = (index >= mFrontIdx) ? - mBuffer.size() - 1 - (index - mFrontIdx) : mFrontIdx - 1 - index; - return mBuffer[pos]; -} - -template <class T> -size_t RingBuffer<T>::size() const { - return mBuffer.size(); -} - -template <class T> -void RingBuffer<T>::clear() { - mBuffer.clear(); - mFrontIdx = 0; -} - -}; // namespace android - -#endif // ANDROID_SERVICE_UTILS_RING_BUFFER_H - - diff --git a/services/mediaresourcemanager/ResourceManagerService.cpp b/services/mediaresourcemanager/ResourceManagerService.cpp index 3c093f9..e2b6695 100644 --- a/services/mediaresourcemanager/ResourceManagerService.cpp +++ b/services/mediaresourcemanager/ResourceManagerService.cpp @@ -126,8 +126,8 @@ status_t ResourceManagerService::dump(int fd, const Vector<String16>& /* args */ } } } - result.append(" Logs:\n"); - result.append(mServiceLog->toString()); + result.append(" Events logs (most recent at top):\n"); + result.append(mServiceLog->toString(" " /* linePrefix */)); write(fd, result.string(), result.size()); return OK; diff --git a/services/mediaresourcemanager/ServiceLog.cpp b/services/mediaresourcemanager/ServiceLog.cpp index be7b308..791e797 100644 --- a/services/mediaresourcemanager/ServiceLog.cpp +++ b/services/mediaresourcemanager/ServiceLog.cpp @@ -27,28 +27,37 @@ static const size_t kDefaultMaxNum = 100; namespace android { -ServiceLog::ServiceLog() : mMaxNum(kDefaultMaxNum) {} -ServiceLog::ServiceLog(size_t maxNum) : mMaxNum(maxNum) {} +ServiceLog::ServiceLog() : mMaxNum(kDefaultMaxNum), mLogs(mMaxNum) {} +ServiceLog::ServiceLog(size_t maxNum) : mMaxNum(maxNum), mLogs(mMaxNum) {} void ServiceLog::add(const String8 &log) { Mutex::Autolock lock(mLock); time_t now = time(0); char buf[64]; strftime(buf, sizeof(buf), "%m-%d %T", localtime(&now)); - String8 formattedLog = String8::format("%s %s", buf, log.string()); - if (mLogs.add(formattedLog) == mMaxNum) { - mLogs.removeAt(0); - } + mLogs.add(String8::format("%s %s", buf, log.string())); } -String8 ServiceLog::toString() const { +String8 ServiceLog::toString(const char *linePrefix) const { Mutex::Autolock lock(mLock); String8 result; - for (size_t i = 0; i < mLogs.size(); ++i) { - result.append(mLogs[i]); - result.append("\n"); + for (const auto& log : mLogs) { + addLine(log.string(), linePrefix, &result); + } + if (mLogs.size() == mMaxNum) { + addLine("...", linePrefix, &result); + } else if (mLogs.size() == 0) { + addLine("[no events yet]", linePrefix, &result); } return result; } +void ServiceLog::addLine(const char *log, const char *prefix, String8 *result) const { + if (prefix != NULL) { + result->append(prefix); + } + result->append(log); + result->append("\n"); +} + } // namespace android diff --git a/services/mediaresourcemanager/ServiceLog.h b/services/mediaresourcemanager/ServiceLog.h index 14814ff..a6f16eb 100644 --- a/services/mediaresourcemanager/ServiceLog.h +++ b/services/mediaresourcemanager/ServiceLog.h @@ -23,6 +23,8 @@ #include <utils/threads.h> #include <utils/Vector.h> +#include "media/RingBuffer.h" + namespace android { class ServiceLog : public RefBase { @@ -31,12 +33,14 @@ public: ServiceLog(size_t maxNum); void add(const String8 &log); - String8 toString() const; + String8 toString(const char *linePrefix = NULL) const; private: - int mMaxNum; + size_t mMaxNum; mutable Mutex mLock; - Vector<String8> mLogs; + RingBuffer<String8> mLogs; + + void addLine(const char *log, const char *prefix, String8 *result) const; }; // ---------------------------------------------------------------------------- diff --git a/services/mediaresourcemanager/test/ServiceLog_test.cpp b/services/mediaresourcemanager/test/ServiceLog_test.cpp index 6ddcb87..9172499 100644 --- a/services/mediaresourcemanager/test/ServiceLog_test.cpp +++ b/services/mediaresourcemanager/test/ServiceLog_test.cpp @@ -34,35 +34,48 @@ protected: }; TEST_F(ServiceLogTest, addThenToString) { + String8 logString; + mServiceLog->add(String8("log1")); - EXPECT_TRUE(mServiceLog->toString().contains("log1")); - ALOGV("toString:\n%s", mServiceLog->toString().string()); + logString = mServiceLog->toString(); + EXPECT_TRUE(logString.contains("log1")); + ALOGV("toString:\n%s", logString.string()); + + static const char kTestLogPrefix[] = "testlogprefix: "; + logString = mServiceLog->toString(kTestLogPrefix); + EXPECT_TRUE(logString.contains(kTestLogPrefix)); + EXPECT_TRUE(logString.contains("log1")); + ALOGV("toString:\n%s", logString.string()); mServiceLog->add(String8("log2")); - EXPECT_TRUE(mServiceLog->toString().contains("log1")); - EXPECT_TRUE(mServiceLog->toString().contains("log2")); - ALOGV("toString:\n%s", mServiceLog->toString().string()); + logString = mServiceLog->toString(); + EXPECT_TRUE(logString.contains("log1")); + EXPECT_TRUE(logString.contains("log2")); + ALOGV("toString:\n%s", logString.string()); mServiceLog->add(String8("log3")); - EXPECT_TRUE(mServiceLog->toString().contains("log1")); - EXPECT_TRUE(mServiceLog->toString().contains("log2")); - EXPECT_TRUE(mServiceLog->toString().contains("log3")); - ALOGV("toString:\n%s", mServiceLog->toString().string()); + logString = mServiceLog->toString(); + EXPECT_TRUE(logString.contains("log1")); + EXPECT_TRUE(logString.contains("log2")); + EXPECT_TRUE(logString.contains("log3")); + ALOGV("toString:\n%s", logString.string()); mServiceLog->add(String8("log4")); - EXPECT_FALSE(mServiceLog->toString().contains("log1")); - EXPECT_TRUE(mServiceLog->toString().contains("log2")); - EXPECT_TRUE(mServiceLog->toString().contains("log3")); - EXPECT_TRUE(mServiceLog->toString().contains("log4")); - ALOGV("toString:\n%s", mServiceLog->toString().string()); + logString = mServiceLog->toString(); + EXPECT_FALSE(logString.contains("log1")); + EXPECT_TRUE(logString.contains("log2")); + EXPECT_TRUE(logString.contains("log3")); + EXPECT_TRUE(logString.contains("log4")); + ALOGV("toString:\n%s", logString.string()); mServiceLog->add(String8("log5")); - EXPECT_FALSE(mServiceLog->toString().contains("log1")); - EXPECT_FALSE(mServiceLog->toString().contains("log2")); - EXPECT_TRUE(mServiceLog->toString().contains("log3")); - EXPECT_TRUE(mServiceLog->toString().contains("log4")); - EXPECT_TRUE(mServiceLog->toString().contains("log5")); - ALOGV("toString:\n%s", mServiceLog->toString().string()); + logString = mServiceLog->toString(); + EXPECT_FALSE(logString.contains("log1")); + EXPECT_FALSE(logString.contains("log2")); + EXPECT_TRUE(logString.contains("log3")); + EXPECT_TRUE(logString.contains("log4")); + EXPECT_TRUE(logString.contains("log5")); + ALOGV("toString:\n%s", logString.string()); } } // namespace android |