diff options
author | Lingfeng Yang <lfy@google.com> | 2016-07-13 07:31:51 -0700 |
---|---|---|
committer | Greg Hartman <ghartman@google.com> | 2018-08-23 17:24:52 -0700 |
commit | 30975cf7b31762dd7226e29c093afd4650904e3a (patch) | |
tree | 57bc0cafa69299311f41c605397a4b5270b6bdd8 | |
parent | 6156903f6ba792f7f66150e145bf8df6ab054e52 (diff) | |
download | device_generic_opengl-transport-30975cf7b31762dd7226e29c093afd4650904e3a.tar.gz device_generic_opengl-transport-30975cf7b31762dd7226e29c093afd4650904e3a.tar.bz2 device_generic_opengl-transport-30975cf7b31762dd7226e29c093afd4650904e3a.zip |
Make OpenGL logger thread safe
- our opengl command rate is now fast enough to raise STL
exception when concurrently using push_back
- also fix build error
Change-Id: Ief7c2ffb09ea0e5fec300b1e1e3f2679bbd6ad02
4 files changed, 53 insertions, 26 deletions
diff --git a/android/opengl/logger.cpp b/android/opengl/logger.cpp index 0f239f24b..175c1daf9 100644 --- a/android/opengl/logger.cpp +++ b/android/opengl/logger.cpp @@ -9,11 +9,14 @@ // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the // GNU General Public License for more details. +#include "android/opengl/logger.h" + #include "android/base/files/PathUtils.h" #include "android/base/memory/LazyInstance.h" +#include "android/base/synchronization/Lock.h" #include "android/crashreport/CrashReporter.h" -#include "android/opengl/logger.h" +#include <algorithm> #include <fstream> #include <inttypes.h> #include <stdarg.h> @@ -22,6 +25,8 @@ #include <sys/time.h> #include <vector> +using android::base::AutoLock; +using android::base::Lock; using android::base::PathUtils; using android::crashreport::CrashReporter; @@ -36,30 +41,34 @@ using android::crashreport::CrashReporter; static const int kBufferLen = 2048; +typedef std::pair<uint64_t, std::string> TimestampedLogEntry; + class OpenGLLogger { public: OpenGLLogger(); OpenGLLogger(const char* filename); // Coarse log: Call this infrequently. void writeCoarse(const char* str); + // Fine log: When we want to log very frequent events. // Fine logs can be toggled on/off. - void writeFine(const char* str); + void writeFine(uint64_t time, const char* str); void writeFineTimestamped(const char* str); + void startFineLog(); void stopFineLog(); static OpenGLLogger* get(); private: + Lock mLock; bool mFineLogActive; std::string mFileName; std::ofstream mFileHandle; std::string mFineLogFileName; std::ofstream mFineLogFileHandle; - std::vector<std::string> mFineLog; + std::vector<TimestampedLogEntry> mFineLog; DISALLOW_COPY_ASSIGN_AND_MOVE(OpenGLLogger); }; - ::android::base::LazyInstance<OpenGLLogger> sOpenGLLogger = LAZY_INSTANCE_INIT; OpenGLLogger* OpenGLLogger::get() { @@ -90,9 +99,10 @@ void OpenGLLogger::writeCoarse(const char* str) { } } -void OpenGLLogger::writeFine(const char* str) { +void OpenGLLogger::writeFine(uint64_t time, const char* str) { if (mFineLogActive) { - mFineLog.push_back(std::string(str)); + AutoLock lock(mLock); + mFineLog.emplace_back(time, str); } } @@ -114,7 +124,8 @@ void OpenGLLogger::writeFineTimestamped(const char* str) { curr_millis, curr_micros, str); - writeFine(buf); + writeFine(curr_micros + 1000ULL * curr_millis + + 1000ULL * 1000ULL * curr_secs, buf); } } @@ -123,26 +134,38 @@ void OpenGLLogger::startFineLog() { } void OpenGLLogger::stopFineLog() { - mFineLogActive = false; - // Only print message when fine-grained - // logging is turned on. - if (!mFineLog.empty()) { - fprintf(stderr, - "Writing fine-grained GL log to %s...", - mFineLogFileName.c_str()); - } - for (const auto& entry : mFineLog) { - // The fine log does not print newlines - // as it is used with the opengl debug - // printout in emugl, which adds - // newlines of its own. - mFineLogFileHandle << entry; - } - mFineLogFileHandle.close(); - if (!mFineLog.empty()) { - fprintf(stderr, "done\n"); + if (mFineLogActive) { + mFineLogActive = false; + // Only print message when fine-grained + // logging is turned on. + if (!mFineLog.empty()) { + fprintf(stderr, + "Writing fine-grained GL log to %s...", + mFineLogFileName.c_str()); + } + + // Sort log entries according to their timestamps. + // This is because the log entries might arrive + // out of order. + std::sort(mFineLog.begin(), mFineLog.end(), + [](const TimestampedLogEntry& x, + const TimestampedLogEntry& y) { + return x.first < y.first; + }); + + for (const auto& entry : mFineLog) { + // The fine log does not print newlines + // as it is used with the opengl debug + // printout in emugl, which adds + // newlines of its own. + mFineLogFileHandle << entry.second; + } + mFineLogFileHandle.close(); + if (!mFineLog.empty()) { + fprintf(stderr, "done\n"); + } + mFineLog.clear(); } - mFineLog.clear(); } // C interface diff --git a/distrib/android-emugl/host/libs/renderControl_dec/Android.mk b/distrib/android-emugl/host/libs/renderControl_dec/Android.mk index e6ea9479b..9013065c4 100644 --- a/distrib/android-emugl/host/libs/renderControl_dec/Android.mk +++ b/distrib/android-emugl/host/libs/renderControl_dec/Android.mk @@ -7,4 +7,6 @@ $(call emugl-import,libOpenglCodecCommon) $(call emugl-gen-decoder,$(LOCAL_PATH),renderControl) # For renderControl_types.h $(call emugl-export,C_INCLUDES,$(LOCAL_PATH)) + +$(call emugl-export,CFLAGS,$(EMUGL_USER_CFLAGS)) $(call emugl-end-module) diff --git a/distrib/android-emugl/host/tools/emugen/ApiGen.cpp b/distrib/android-emugl/host/tools/emugen/ApiGen.cpp index f31b098e5..4db80817b 100644 --- a/distrib/android-emugl/host/tools/emugen/ApiGen.cpp +++ b/distrib/android-emugl/host/tools/emugen/ApiGen.cpp @@ -755,6 +755,7 @@ int ApiGen::genDecoderHeader(const std::string &filename) fprintf(fp, "#include \"IOStream.h\" \n"); fprintf(fp, "#include \"%s_%s_context.h\"\n\n\n", m_basename.c_str(), sideString(SERVER_SIDE)); + fprintf(fp, "#include \"emugl/common/logging.h\"\n"); for (size_t i = 0; i < m_decoderHeaders.size(); i++) { fprintf(fp, "#include %s\n", m_decoderHeaders[i].c_str()); diff --git a/distrib/android-emugl/host/tools/emugen/tests/t.001/expected/decoder/foo_dec.h b/distrib/android-emugl/host/tools/emugen/tests/t.001/expected/decoder/foo_dec.h index 3646de53a..bf9ead7c8 100644 --- a/distrib/android-emugl/host/tools/emugen/tests/t.001/expected/decoder/foo_dec.h +++ b/distrib/android-emugl/host/tools/emugen/tests/t.001/expected/decoder/foo_dec.h @@ -8,6 +8,7 @@ #include "foo_server_context.h" +#include "emugl/common/logging.h" struct foo_decoder_context_t : public foo_server_context_t { |