diff --git a/trunk/src/app/srs_app_log.cpp b/trunk/src/app/srs_app_log.cpp index 0faef4fa69..6e1a62e011 100644 --- a/trunk/src/app/srs_app_log.cpp +++ b/trunk/src/app/srs_app_log.cpp @@ -39,26 +39,27 @@ #include // the max size of a line of log. -#define LOG_MAX_SIZE 8192 +int LOG_MAX_SIZE = 8192; // the tail append to each log. #define LOG_TAIL '\n' // reserved for the end of log data, it must be strlen(LOG_TAIL) #define LOG_TAIL_SIZE 1 +// Thread local log cache. +__thread char* _srs_log_data = NULL; + SrsFileLog::SrsFileLog() { level = SrsLogLevelTrace; log_to_file_tank = false; utc = false; - log_data = new char[LOG_MAX_SIZE]; writer_ = NULL; } SrsFileLog::~SrsFileLog() { - srs_freepa(log_data); } // @remark Note that we should never write logs, because log is not ready not. @@ -96,17 +97,17 @@ void SrsFileLog::verbose(const char* tag, SrsContextId context_id, const char* f } int size = 0; - if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Verb", &size)) { + if (!srs_log_header(_srs_log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Verb", &size)) { return; } va_list ap; va_start(ap, fmt); // we reserved 1 bytes for the new line. - size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); + size += vsnprintf(_srs_log_data + size, LOG_MAX_SIZE - size, fmt, ap); va_end(ap); - write_log(log_data, size, SrsLogLevelVerbose); + write_log(_srs_log_data, size, SrsLogLevelVerbose); } void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt, ...) @@ -116,17 +117,17 @@ void SrsFileLog::info(const char* tag, SrsContextId context_id, const char* fmt, } int size = 0; - if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Debug", &size)) { + if (!srs_log_header(_srs_log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Debug", &size)) { return; } va_list ap; va_start(ap, fmt); // we reserved 1 bytes for the new line. - size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); + size += vsnprintf(_srs_log_data + size, LOG_MAX_SIZE - size, fmt, ap); va_end(ap); - write_log(log_data, size, SrsLogLevelInfo); + write_log(_srs_log_data, size, SrsLogLevelInfo); } void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt, ...) @@ -136,17 +137,17 @@ void SrsFileLog::trace(const char* tag, SrsContextId context_id, const char* fmt } int size = 0; - if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Trace", &size)) { + if (!srs_log_header(_srs_log_data, LOG_MAX_SIZE, utc, false, tag, context_id, "Trace", &size)) { return; } va_list ap; va_start(ap, fmt); // we reserved 1 bytes for the new line. - size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); + size += vsnprintf(_srs_log_data + size, LOG_MAX_SIZE - size, fmt, ap); va_end(ap); - write_log(log_data, size, SrsLogLevelTrace); + write_log(_srs_log_data, size, SrsLogLevelTrace); } void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt, ...) @@ -156,17 +157,17 @@ void SrsFileLog::warn(const char* tag, SrsContextId context_id, const char* fmt, } int size = 0; - if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Warn", &size)) { + if (!srs_log_header(_srs_log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Warn", &size)) { return; } va_list ap; va_start(ap, fmt); // we reserved 1 bytes for the new line. - size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); + size += vsnprintf(_srs_log_data + size, LOG_MAX_SIZE - size, fmt, ap); va_end(ap); - write_log(log_data, size, SrsLogLevelWarn); + write_log(_srs_log_data, size, SrsLogLevelWarn); } void SrsFileLog::error(const char* tag, SrsContextId context_id, const char* fmt, ...) @@ -176,23 +177,23 @@ void SrsFileLog::error(const char* tag, SrsContextId context_id, const char* fmt } int size = 0; - if (!srs_log_header(log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Error", &size)) { + if (!srs_log_header(_srs_log_data, LOG_MAX_SIZE, utc, true, tag, context_id, "Error", &size)) { return; } va_list ap; va_start(ap, fmt); // we reserved 1 bytes for the new line. - size += vsnprintf(log_data + size, LOG_MAX_SIZE - size, fmt, ap); + size += vsnprintf(_srs_log_data + size, LOG_MAX_SIZE - size, fmt, ap); va_end(ap); // add strerror() to error msg. // Check size to avoid security issue https://github.com/ossrs/srs/issues/1229 if (errno != 0 && size < LOG_MAX_SIZE) { - size += snprintf(log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno)); + size += snprintf(_srs_log_data + size, LOG_MAX_SIZE - size, "(%s)", strerror(errno)); } - write_log(log_data, size, SrsLogLevelError); + write_log(_srs_log_data, size, SrsLogLevelError); } void SrsFileLog::write_log(char *str_log, int size, int level) diff --git a/trunk/src/app/srs_app_log.hpp b/trunk/src/app/srs_app_log.hpp index 14c8ea73b1..f349b9e3ed 100644 --- a/trunk/src/app/srs_app_log.hpp +++ b/trunk/src/app/srs_app_log.hpp @@ -48,8 +48,6 @@ class SrsAsyncFileWriter; class SrsFileLog : public ISrsLog, public ISrsReloadHandler { private: - // Shared cache for each line of log. - char* log_data; // Async file writer. SrsAsyncFileWriter* writer_; private: diff --git a/trunk/src/app/srs_app_threads.cpp b/trunk/src/app/srs_app_threads.cpp index 327b894c0b..c69f4cd09a 100644 --- a/trunk/src/app/srs_app_threads.cpp +++ b/trunk/src/app/srs_app_threads.cpp @@ -145,6 +145,18 @@ SrsThreadPool::~SrsThreadPool() srs_freep(lock_); } +// Thread local log cache. +extern const int LOG_MAX_SIZE; +extern __thread char* _srs_log_data; + +// Setup the thread-local variables, MUST call when each thread starting. +void SrsThreadPool::setup() +{ + // Initialize the log shared buffer for threads. + srs_assert(!_srs_log_data); + _srs_log_data = new char[LOG_MAX_SIZE]; +} + srs_error_t SrsThreadPool::initialize() { srs_error_t err = srs_success; @@ -288,6 +300,9 @@ void SrsThreadPool::stop() void* SrsThreadPool::start(void* arg) { + // Initialize thread-local variables. + SrsThreadPool::setup(); + srs_error_t err = srs_success; SrsThreadEntry* entry = (SrsThreadEntry*)arg; @@ -300,11 +315,13 @@ void* SrsThreadPool::start(void* arg) r0 = pthread_setaffinity_np(pthread_self(), sizeof(entry->cpuset), &entry->cpuset); } r1 = pthread_getaffinity_np(pthread_self(), sizeof(entry->cpuset2), &entry->cpuset2); +#else + pthread_setname_np(entry->name.c_str()); #endif - srs_trace("Thread #%d: run with label=%s, name=%s, cpuset=%d/%d-0x%" PRIx64 "/%d-0x%" PRIx64, entry->num, - entry->label.c_str(), entry->name.c_str(), entry->cpuset_ok, r0, srs_covert_cpuset(entry->cpuset), - r1, srs_covert_cpuset(entry->cpuset2)); + srs_trace("Thread #%d: run with entry=%p, label=%s, name=%s, cpuset=%d/%d-0x%" PRIx64 "/%d-0x%" PRIx64, + entry->num, entry, entry->label.c_str(), entry->name.c_str(), entry->cpuset_ok, + r0, srs_covert_cpuset(entry->cpuset), r1, srs_covert_cpuset(entry->cpuset2)); if ((err = entry->start(entry->arg)) != srs_success) { entry->err = err; diff --git a/trunk/src/app/srs_app_threads.hpp b/trunk/src/app/srs_app_threads.hpp index 05403ff191..07d97235a0 100644 --- a/trunk/src/app/srs_app_threads.hpp +++ b/trunk/src/app/srs_app_threads.hpp @@ -117,6 +117,8 @@ class SrsThreadPool SrsThreadPool(); virtual ~SrsThreadPool(); public: + // Setup the thread-local variables. + static void setup(); // Initialize the thread pool. srs_error_t initialize(); // Execute start function with label in thread. diff --git a/trunk/src/main/srs_main_server.cpp b/trunk/src/main/srs_main_server.cpp index 45dc2d9fb2..e916e9e1b1 100644 --- a/trunk/src/main/srs_main_server.cpp +++ b/trunk/src/main/srs_main_server.cpp @@ -224,6 +224,9 @@ srs_error_t do_main(int argc, char** argv) } int main(int argc, char** argv) { + // Initialize thread-local variables. + SrsThreadPool::setup(); + // For background context id. _srs_context->set_id(_srs_context->generate_id()); diff --git a/trunk/src/utest/srs_utest.cpp b/trunk/src/utest/srs_utest.cpp index 4110aa6092..632a6ca45f 100644 --- a/trunk/src/utest/srs_utest.cpp +++ b/trunk/src/utest/srs_utest.cpp @@ -29,6 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. #include #include #include +#include #include using namespace std; @@ -70,7 +71,11 @@ srs_error_t prepare_main() { // We could do something in the main of utest. // Copy from gtest-1.6.0/src/gtest_main.cc -GTEST_API_ int main(int argc, char **argv) { +GTEST_API_ int main(int argc, char **argv) +{ + // Initialize thread-local variables. + SrsThreadPool::setup(); + srs_error_t err = srs_success; if ((err = prepare_main()) != srs_success) {