Skip to content

Commit

Permalink
Threads: Set the threads name display in top.
Browse files Browse the repository at this point in the history
  • Loading branch information
winlinvip committed Apr 26, 2021
1 parent 1c6e941 commit fa4c9f9
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_rtc_server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,7 @@ srs_error_t SrsRtcServer::on_timer(srs_utime_t interval, srs_utime_t tick)
rnk_desc = buf;
}

// TODO: FIXME: Should move to Hybrid server stat.
string loss_desc;
if (_srs_pps_rloss->r10s() || _srs_pps_sloss->r10s()) {
snprintf(buf, sizeof(buf), ", loss=(r:%d,s:%d)", _srs_pps_rloss->r10s(), _srs_pps_sloss->r10s());
Expand Down
24 changes: 20 additions & 4 deletions trunk/src/app/srs_app_threads.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ SrsThreadPool::SrsThreadPool()
entry->start = NULL;
entry->arg = NULL;
entry->num = 1;
entry->trd = pthread_self();

char buf[256];
snprintf(buf, sizeof(buf), "srs-master-%d", entry->num);
entry->name = buf;
}

// TODO: FIMXE: If free the pool, we should stop all threads.
Expand All @@ -129,8 +134,8 @@ srs_error_t SrsThreadPool::initialize()

interval_ = _srs_config->get_threads_interval();
bool async_srtp = _srs_config->get_threads_async_srtp();
srs_trace("Thread #%d(%s): init interval=%dms, async_srtp=%d",
entry_->num, entry_->label.c_str(), srsu2msi(interval_), async_srtp);
srs_trace("Thread #%d(%s): init name=%s, interval=%dms, async_srtp=%d",
entry_->num, entry_->label.c_str(), entry_->name.c_str(), srsu2msi(interval_), async_srtp);

return err;
}
Expand Down Expand Up @@ -158,6 +163,10 @@ srs_error_t SrsThreadPool::execute(string label, srs_error_t (*start)(void* arg)
static int num = entry_->num + 1;
entry->num = num++;

char buf[256];
snprintf(buf, sizeof(buf), "srs-%s-%d", entry->label.c_str(), entry->num);
entry->name = buf;

// https://man7.org/linux/man-pages/man3/pthread_create.3.html
pthread_t trd;
int r0 = pthread_create(&trd, NULL, SrsThreadPool::start, entry);
Expand Down Expand Up @@ -205,7 +214,7 @@ srs_error_t SrsThreadPool::run()
sync_desc = buf;
}

srs_trace("Thread: cycle threads=%d%s%s", (int)threads_.size(),
srs_trace("Thread: %s cycle threads=%d%s%s", entry_->name.c_str(), (int)threads_.size(),
async_logs.c_str(), sync_desc.c_str());
}

Expand All @@ -222,7 +231,14 @@ void* SrsThreadPool::start(void* arg)
srs_error_t err = srs_success;

SrsThreadEntry* entry = (SrsThreadEntry*)arg;
srs_trace("Thread #%d(%s): run", entry->num, entry->label.c_str());

#ifndef SRS_OSX
// https://man7.org/linux/man-pages/man3/pthread_setname_np.3.html
pthread_setname_np(entry->trd, entry->name.c_str());
#endif

srs_trace("Thread #%d: run with label=%s, name=%s", entry->num,
entry->label.c_str(), entry->name.c_str());

if ((err = entry->start(entry->arg)) != srs_success) {
entry->err = err;
Expand Down
1 change: 1 addition & 0 deletions trunk/src/app/srs_app_threads.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ class SrsThreadEntry
public:
SrsThreadPool* pool;
std::string label;
std::string name;
srs_error_t (*start)(void* arg);
void* arg;
int num;
Expand Down

0 comments on commit fa4c9f9

Please sign in to comment.