Skip to content

Commit

Permalink
Threads: Use thread-local buffer for log
Browse files Browse the repository at this point in the history
1. Call SrsThreadPool::setup() in main(),or  each thread starting.
2. Initialize the thread-local object in SrsThreadPool::setup().
3. Change shared log buffer to thread-local.
  • Loading branch information
winlinvip committed Apr 26, 2021
1 parent 185359f commit 957034e
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 25 deletions.
39 changes: 20 additions & 19 deletions trunk/src/app/srs_app_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,27 @@
#include <srs_app_threads.hpp>

// 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.
Expand Down Expand Up @@ -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, ...)
Expand All @@ -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, ...)
Expand All @@ -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, ...)
Expand All @@ -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, ...)
Expand All @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions trunk/src/app/srs_app_log.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
23 changes: 20 additions & 3 deletions trunk/src/app/srs_app_threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -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;
Expand Down
2 changes: 2 additions & 0 deletions trunk/src/app/srs_app_threads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 3 additions & 0 deletions trunk/src/main/srs_main_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());

Expand Down
7 changes: 6 additions & 1 deletion trunk/src/utest/srs_utest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#include <srs_app_config.hpp>
#include <srs_app_log.hpp>
#include <srs_app_rtc_dtls.hpp>
#include <srs_app_threads.hpp>

#include <string>
using namespace std;
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 957034e

Please sign in to comment.