From: Zack Rusin Date: Wed, 24 Aug 2011 03:02:10 +0000 (-0400) Subject: Merge branch 'master' into compression X-Git-Url: https://git.cworth.org/git?a=commitdiff_plain;h=93dfad1dc7e20d694e2c8b63515bff8ae91f3700;hp=-c;p=apitrace Merge branch 'master' into compression Conflicts: trace_writer.cpp --- 93dfad1dc7e20d694e2c8b63515bff8ae91f3700 diff --combined trace_writer.cpp index 7511d31,7fcd0e6..057e395 --- a/trace_writer.cpp +++ b/trace_writer.cpp @@@ -30,9 -30,10 +30,9 @@@ #include #include -#include - #include "os.hpp" #include "trace_writer.hpp" +#include "trace_file.hpp" #include "trace_format.hpp" @@@ -40,29 -41,30 +40,29 @@@ namespace Trace Writer::Writer() : - g_gzFile(NULL), call_no(0) { + m_file = new Trace::SnappyFile; close(); -}; +} -Writer::~Writer() { +Writer::~Writer() +{ close(); -}; + delete m_file; + m_file = NULL; +} void Writer::close(void) { - if (g_gzFile != NULL) { - gzclose(g_gzFile); - g_gzFile = NULL; - } + m_file->close(); } bool Writer::open(const char *filename) { close(); - g_gzFile = gzopen(filename, "wb"); - if (!g_gzFile) { + if (!m_file->open(filename, File::Write)) { return false; } @@@ -77,51 -79,12 +77,9 @@@ return true; } - void - Writer::open(void) { - - static unsigned dwCounter = 0; - - const char *szExtension = "trace"; - char szFileName[PATH_MAX]; - const char *lpFileName; - - lpFileName = getenv("TRACE_FILE"); - if (lpFileName) { - strncpy(szFileName, lpFileName, PATH_MAX); - } - else { - char szProcessName[PATH_MAX]; - char szCurrentDir[PATH_MAX]; - OS::GetProcessName(szProcessName, PATH_MAX); - OS::GetCurrentDir(szCurrentDir, PATH_MAX); - - for (;;) { - FILE *file; - - if (dwCounter) - snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension); - else - snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension); - - file = fopen(szFileName, "rb"); - if (file == NULL) - break; - - fclose(file); - - ++dwCounter; - } - } - - OS::DebugMessage("apitrace: tracing to %s\n", szFileName); - - open(szFileName); - } - void inline Writer::_write(const void *sBuffer, size_t dwBytesToWrite) { - if (g_gzFile == NULL) - return; - - gzwrite(g_gzFile, sBuffer, dwBytesToWrite); + m_file->write(sBuffer, dwBytesToWrite); } void inline @@@ -177,12 -140,6 +135,6 @@@ inline bool lookup(std::vector &m } unsigned Writer::beginEnter(const FunctionSig *sig) { - OS::AcquireMutex(); - - if (!m_file->isOpened()) { - open(); - } - _writeByte(Trace::EVENT_ENTER); _writeUInt(sig->id); if (!lookup(functions, sig->id)) { @@@ -199,19 -156,15 +151,15 @@@ void Writer::endEnter(void) { _writeByte(Trace::CALL_END); - OS::ReleaseMutex(); } void Writer::beginLeave(unsigned call) { - OS::AcquireMutex(); _writeByte(Trace::EVENT_LEAVE); _writeUInt(call); } void Writer::endLeave(void) { _writeByte(Trace::CALL_END); - m_file->flush(); - OS::ReleaseMutex(); } void Writer::beginArg(unsigned index) { @@@ -350,5 -303,76 +298,75 @@@ void Writer::writeOpaque(const void *ad _writeUInt((size_t)addr); } + + void + LocalWriter::open(void) { + + static unsigned dwCounter = 0; + + const char *szExtension = "trace"; + char szFileName[PATH_MAX]; + const char *lpFileName; + + lpFileName = getenv("TRACE_FILE"); + if (lpFileName) { + strncpy(szFileName, lpFileName, PATH_MAX); + } + else { + char szProcessName[PATH_MAX]; + char szCurrentDir[PATH_MAX]; + OS::GetProcessName(szProcessName, PATH_MAX); + OS::GetCurrentDir(szCurrentDir, PATH_MAX); + + for (;;) { + FILE *file; + + if (dwCounter) + snprintf(szFileName, PATH_MAX, "%s%c%s.%u.%s", szCurrentDir, PATH_SEP, szProcessName, dwCounter, szExtension); + else + snprintf(szFileName, PATH_MAX, "%s%c%s.%s", szCurrentDir, PATH_SEP, szProcessName, szExtension); + + file = fopen(szFileName, "rb"); + if (file == NULL) + break; + + fclose(file); + + ++dwCounter; + } + } + + OS::DebugMessage("apitrace: tracing to %s\n", szFileName); + + Writer::open(szFileName); + } + + unsigned LocalWriter::beginEnter(const FunctionSig *sig) { + OS::AcquireMutex(); + - if (!g_gzFile) { ++ if (!m_file->isOpened()) { + open(); + } + + return Writer::beginEnter(sig); + } + + void LocalWriter::endEnter(void) { + Writer::endEnter(); - gzflush(g_gzFile, Z_SYNC_FLUSH); + OS::ReleaseMutex(); + } + + void LocalWriter::beginLeave(unsigned call) { + OS::AcquireMutex(); + Writer::beginLeave(call); + } + + void LocalWriter::endLeave(void) { + Writer::endLeave(); - gzflush(g_gzFile, Z_SYNC_FLUSH); ++ m_file->flush(); + OS::ReleaseMutex(); + } + + } /* namespace Trace */ diff --combined trace_writer.hpp index eb81f23,20a1530..4d46f73 --- a/trace_writer.hpp +++ b/trace_writer.hpp @@@ -39,11 -39,10 +39,11 @@@ namespace Trace { + class File; class Writer { protected: - void *g_gzFile; + File *m_file; unsigned call_no; std::vector functions; @@@ -55,7 -54,6 +55,6 @@@ Writer(); ~Writer(); - void open(void); bool open(const char *filename); void close(void); @@@ -105,6 -103,27 +104,27 @@@ void inline _writeString(const char *str); }; + + /** + * A specialized Writer class, mean to trace the current process. + * + * In particular: + * - it creates a trace file based on the current process name + * - uses mutexes to allow tracing from multiple threades + * - flushes the output to ensure the last call is traced in event of + * abnormal termination + */ + class LocalWriter : public Writer { + protected: + public: + void open(void); + + unsigned beginEnter(const FunctionSig *sig); + void endEnter(void); + + void beginLeave(unsigned call); + void endLeave(void); + }; } #endif /* _TRACE_WRITER_HPP_ */