From e47010130f950a87456a99e7fbff8011c5d1dd36 Mon Sep 17 00:00:00 2001 From: Andre Puschmann Date: Mon, 27 May 2019 12:09:56 +0200 Subject: [PATCH] extend thread class to set unique thread name in ctor this will also extend all classes that use srslte::thread to specify the name of the thread in the ctor as well as to set the name of the worker threads in the thread pool the thread name will be displayed in gdb. --- lib/include/srslte/common/logger_file.h | 3 +- lib/include/srslte/common/metrics_hub.h | 5 +-- lib/include/srslte/common/threads.h | 42 ++++++++++++++++--------- lib/src/common/logger_file.cc | 15 +++++---- lib/src/common/thread_pool.cc | 4 ++- lib/test/upper/rlc_am_test.cc | 2 +- lib/test/upper/rlc_stress_test.cc | 40 +++++++++++++---------- srsenb/hdr/mac/mac.h | 2 +- srsenb/hdr/phy/prach_worker.h | 12 +++++-- srsenb/hdr/upper/gtpu.h | 2 +- srsenb/hdr/upper/rrc.h | 4 +-- srsenb/hdr/upper/s1ap.h | 2 ++ srsenb/src/mac/mac.cc | 3 +- srsenb/src/phy/txrx.cc | 3 +- srsenb/src/upper/gtpu.cc | 2 +- srsenb/src/upper/rrc.cc | 2 +- srsenb/src/upper/s1ap.cc | 11 +++++++ srsepc/src/mbms-gw/mbms-gw.cc | 4 +-- srsepc/src/mme/mme.cc | 2 +- srsepc/src/spgw/spgw.cc | 2 +- srsue/src/phy/async_scell_recv.cc | 2 +- srsue/src/phy/phy.cc | 2 +- srsue/src/phy/sync.cc | 4 +-- srsue/src/stack/mac/mac.cc | 4 +-- srsue/src/stack/rrc/rrc.cc | 3 +- srsue/src/stack/upper/gw.cc | 3 +- 26 files changed, 110 insertions(+), 70 deletions(-) diff --git a/lib/include/srslte/common/logger_file.h b/lib/include/srslte/common/logger_file.h index 80c20585e..17f08ef5e 100644 --- a/lib/include/srslte/common/logger_file.h +++ b/lib/include/srslte/common/logger_file.h @@ -63,8 +63,7 @@ private: std::string filename; pthread_cond_t not_empty; pthread_mutex_t mutex; - pthread_t thread; - std::deque buffer; + std::deque buffer; }; } // namespace srslte diff --git a/lib/include/srslte/common/metrics_hub.h b/lib/include/srslte/common/metrics_hub.h index 149fadd8f..47b45881f 100644 --- a/lib/include/srslte/common/metrics_hub.h +++ b/lib/include/srslte/common/metrics_hub.h @@ -54,8 +54,9 @@ template class metrics_hub : public periodic_thread { public: - metrics_hub() : m(nullptr), sleep_start(std::chrono::steady_clock::now()) {} - bool init(metrics_interface *m_, float report_period_secs_=1.0) { + metrics_hub() : m(nullptr), sleep_start(std::chrono::steady_clock::now()), periodic_thread("METRICS_HUB") {} + bool init(metrics_interface* m_, float report_period_secs_ = 1.0) + { m = m_; // Start with user-default priority start_periodic(report_period_secs_*1e6, -2); diff --git a/lib/include/srslte/common/threads.h b/lib/include/srslte/common/threads.h index 689182aa5..7313ed1d1 100644 --- a/lib/include/srslte/common/threads.h +++ b/lib/include/srslte/common/threads.h @@ -39,28 +39,32 @@ void threads_print_self(); #ifdef __cplusplus -} - + } + +#include + #ifndef SRSLTE_THREADS_H #define SRSLTE_THREADS_H class thread { public: - thread() { - _thread = 0; - } + thread(const std::string& name_) : _thread(0), name(name_) {} bool start(int prio = -1) { return threads_new_rt_prio(&_thread, thread_function_entry, this, prio); } bool start_cpu(int prio, int cpu) { return threads_new_rt_cpu(&_thread, thread_function_entry, this, cpu, prio); } - bool start_cpu_mask(int prio, int mask){ - return threads_new_rt_mask(&_thread, thread_function_entry, this, mask, prio); -} - void print_priority() { - threads_print_self(); + bool start_cpu_mask(int prio, int mask) + { + return threads_new_rt_mask(&_thread, thread_function_entry, this, mask, prio); + } + void print_priority() { threads_print_self(); } + void set_name(const std::string& name_) + { + name = name_; + pthread_setname_np(pthread_self(), name.c_str()); } void wait_thread_finish() { pthread_join(_thread, NULL); @@ -68,16 +72,25 @@ public: void thread_cancel() { pthread_cancel(_thread); } + protected: - virtual void run_thread() = 0; + virtual void run_thread() = 0; + private: - static void *thread_function_entry(void *_this) { ((thread*) _this)->run_thread(); return NULL; } - pthread_t _thread; + static void* thread_function_entry(void* _this) + { + pthread_setname_np(pthread_self(), ((thread*)_this)->name.c_str()); + ((thread*)_this)->run_thread(); + return NULL; + } + pthread_t _thread; + std::string name; }; class periodic_thread : public thread { public: + periodic_thread(const std::string name_) : thread(name_) {} void start_periodic(int period_us_, int priority = -1) { run_enable = true; period_us = period_us_; @@ -156,5 +169,4 @@ private: #endif // SRSLTE_THREADS_H -#endif // __cplusplus - +#endif // __cplusplus \ No newline at end of file diff --git a/lib/src/common/logger_file.cc b/lib/src/common/logger_file.cc index 5b22fbae8..ec248231b 100644 --- a/lib/src/common/logger_file.cc +++ b/lib/src/common/logger_file.cc @@ -27,12 +27,11 @@ using namespace std; namespace srslte{ -logger_file::logger_file() - :logfile(NULL) - ,is_running(false) - ,cur_length(0) - ,max_length(0) -{} +logger_file::logger_file() : logfile(NULL), is_running(false), cur_length(0), max_length(0), thread("LOGGER_FILE") +{ + pthread_mutex_init(&mutex, NULL); + pthread_cond_init(¬_empty, NULL); +} logger_file::~logger_file() { if(is_running) { @@ -46,9 +45,9 @@ logger_file::~logger_file() { if (logfile) { fclose(logfile); } - pthread_mutex_destroy(&mutex); - pthread_cond_destroy(¬_empty); } + pthread_mutex_destroy(&mutex); + pthread_cond_destroy(¬_empty); } void logger_file::init(std::string file, int max_length_) { diff --git a/lib/src/common/thread_pool.cc b/lib/src/common/thread_pool.cc index f48b1cb27..1eddae834 100644 --- a/lib/src/common/thread_pool.cc +++ b/lib/src/common/thread_pool.cc @@ -30,12 +30,13 @@ namespace srslte { -thread_pool::worker::worker() : my_id(0), running(false), my_parent(NULL) {} +thread_pool::worker::worker() : my_id(0), running(false), my_parent(NULL), thread("THREAD_POOL_WORKER") {} void thread_pool::worker::setup(uint32_t id, thread_pool *parent, uint32_t prio, uint32_t mask) { my_id = id; my_parent = parent; + if(mask == 255) { start(prio); @@ -49,6 +50,7 @@ void thread_pool::worker::setup(uint32_t id, thread_pool *parent, uint32_t prio, void thread_pool::worker::run_thread() { + set_name(std::string("WORKER") + std::to_string(my_id)); running = true; while(running) { wait_to_start(); diff --git a/lib/test/upper/rlc_am_test.cc b/lib/test/upper/rlc_am_test.cc index 21ff3474d..9fddd52c2 100644 --- a/lib/test/upper/rlc_am_test.cc +++ b/lib/test/upper/rlc_am_test.cc @@ -89,7 +89,7 @@ public: class ul_writer : public thread { public: - ul_writer(rlc_am* rlc_) : rlc(rlc_), running(false) {} + ul_writer(rlc_am* rlc_) : rlc(rlc_), running(false), thread("UL_WRITER") {} ~ul_writer() { stop(); } void stop() { diff --git a/lib/test/upper/rlc_stress_test.cc b/lib/test/upper/rlc_stress_test.cc index a52478a70..67843c670 100644 --- a/lib/test/upper/rlc_stress_test.cc +++ b/lib/test/upper/rlc_stress_test.cc @@ -109,15 +109,20 @@ class mac_dummy ,public thread { public: - mac_dummy(rlc_interface_mac *rlc1_, rlc_interface_mac *rlc2_, stress_test_args_t args_, uint32_t lcid_, rlc_pcap* pcap_ = NULL) - :timers(8) - ,run_enable(true) - ,rlc1(rlc1_) - ,rlc2(rlc2_) - ,args(args_) - ,pcap(pcap_) - ,lcid(lcid_) - ,log("MAC ") + mac_dummy(rlc_interface_mac* rlc1_, + rlc_interface_mac* rlc2_, + stress_test_args_t args_, + uint32_t lcid_, + rlc_pcap* pcap_ = NULL) : + timers(8), + run_enable(true), + rlc1(rlc1_), + rlc2(rlc2_), + args(args_), + pcap(pcap_), + lcid(lcid_), + log("MAC "), + thread("MAC_DUMMY") { log.set_level(static_cast(args.log_level)); log.set_hex_limit(LOG_HEX_LIMIT); @@ -209,14 +214,15 @@ class rlc_tester ,public thread { public: - rlc_tester(rlc_interface_pdcp *rlc_, std::string name_, stress_test_args_t args_, uint32_t lcid_) - :log("Testr") - ,rlc(rlc_) - ,run_enable(true) - ,rx_pdus() - ,name(name_) - ,args(args_) - ,lcid(lcid_) + rlc_tester(rlc_interface_pdcp* rlc_, std::string name_, stress_test_args_t args_, uint32_t lcid_) : + log("Testr"), + rlc(rlc_), + run_enable(true), + rx_pdus(), + name(name_), + args(args_), + lcid(lcid_), + thread("RLC_TESTER") { log.set_level(srslte::LOG_LEVEL_ERROR); log.set_hex_limit(LOG_HEX_LIMIT); diff --git a/srsenb/hdr/mac/mac.h b/srsenb/hdr/mac/mac.h index 16cedfb94..a00453e0a 100644 --- a/srsenb/hdr/mac/mac.h +++ b/srsenb/hdr/mac/mac.h @@ -196,7 +196,7 @@ private: /* Class to run upper-layer timers with normal priority */ class timer_thread : public thread { public: - timer_thread(srslte::timers *t) : ttisync(10240),timers(t),running(false) {start();} + timer_thread(srslte::timers* t) : ttisync(10240), timers(t), running(false), thread("MAC_TIMER") { start(); } void tti_clock(); void stop(); private: diff --git a/srsenb/hdr/phy/prach_worker.h b/srsenb/hdr/phy/prach_worker.h index 33bf9a70c..8388a98c1 100644 --- a/srsenb/hdr/phy/prach_worker.h +++ b/srsenb/hdr/phy/prach_worker.h @@ -33,8 +33,16 @@ namespace srsenb { class prach_worker : thread { public: - prach_worker() : initiated(false), prach_nof_det(0), max_prach_offset_us(0), buffer_pool(8), - running(false), nof_sf(0), sf_cnt(0) { + prach_worker() : + initiated(false), + prach_nof_det(0), + max_prach_offset_us(0), + buffer_pool(8), + running(false), + nof_sf(0), + sf_cnt(0), + thread("PRACH_WORKER") + { log_h = NULL; mac = NULL; bzero(&prach, sizeof(srslte_prach_t)); diff --git a/srsenb/hdr/upper/gtpu.h b/srsenb/hdr/upper/gtpu.h index c71dadf00..154803ce7 100644 --- a/srsenb/hdr/upper/gtpu.h +++ b/srsenb/hdr/upper/gtpu.h @@ -71,7 +71,7 @@ private: // Class to create class mch_thread : public thread { public: - mch_thread() : initiated(false), running(false), run_enable(false), pool(NULL), lcid_counter(0) {} + mch_thread() : initiated(false), running(false), run_enable(false), pool(NULL), lcid_counter(0), thread("MCH") {} bool init(std::string m1u_multiaddr_, std::string m1u_if_addr_, pdcp_interface_gtpu *pdcp_, srslte::log *gtpu_log_); void stop(); private: diff --git a/srsenb/hdr/upper/rrc.h b/srsenb/hdr/upper/rrc.h index 33a19a666..2a46f5c17 100644 --- a/srsenb/hdr/upper/rrc.h +++ b/srsenb/hdr/upper/rrc.h @@ -106,8 +106,8 @@ class rrc : public rrc_interface_pdcp, public thread { public: - - rrc() : act_monitor(this), cnotifier(NULL), running(false), nof_si_messages(0) { + rrc() : act_monitor(this), cnotifier(NULL), running(false), nof_si_messages(0), thread("RRC") + { users.clear(); pending_paging.clear(); diff --git a/srsenb/hdr/upper/s1ap.h b/srsenb/hdr/upper/s1ap.h index 260c88881..1abfdf03f 100644 --- a/srsenb/hdr/upper/s1ap.h +++ b/srsenb/hdr/upper/s1ap.h @@ -61,6 +61,8 @@ class s1ap ,public thread { public: + s1ap(); + ~s1ap(); bool init(s1ap_args_t args_, rrc_interface_s1ap *rrc_, srslte::log *s1ap_log_); void stop(); void get_metrics(s1ap_metrics_t &m); diff --git a/srsenb/src/mac/mac.cc b/srsenb/src/mac/mac.cc index a9d410830..daed71f9e 100644 --- a/srsenb/src/mac/mac.cc +++ b/srsenb/src/mac/mac.cc @@ -901,7 +901,8 @@ void mac::timer_thread::tti_clock() * DEMUX unit * *******************************************************/ -mac::pdu_process::pdu_process(pdu_process_handler *h) : running(false) { +mac::pdu_process::pdu_process(pdu_process_handler* h) : running(false), thread("MAC_PDU_PROCESS") +{ handler = h; pthread_mutex_init(&mutex, NULL); pthread_cond_init(&cvar, NULL); diff --git a/srsenb/src/phy/txrx.cc b/srsenb/src/phy/txrx.cc index afbe67392..dcb966365 100644 --- a/srsenb/src/phy/txrx.cc +++ b/srsenb/src/phy/txrx.cc @@ -37,7 +37,8 @@ using namespace std; namespace srsenb { -txrx::txrx() : tx_worker_cnt(0), nof_workers(0), tti(0) { +txrx::txrx() : tx_worker_cnt(0), nof_workers(0), tti(0), thread("TXRX") +{ running = false; radio_h = NULL; log_h = NULL; diff --git a/srsenb/src/upper/gtpu.cc b/srsenb/src/upper/gtpu.cc index b6ddc669c..3cc70d5c2 100644 --- a/srsenb/src/upper/gtpu.cc +++ b/srsenb/src/upper/gtpu.cc @@ -28,7 +28,7 @@ using namespace srslte; namespace srsenb { -gtpu::gtpu():mchthread() +gtpu::gtpu() : mchthread(), thread("GTPU") { pdcp = NULL; gtpu_log = NULL; diff --git a/srsenb/src/upper/rrc.cc b/srsenb/src/upper/rrc.cc index 991ae4290..1f038b8c7 100644 --- a/srsenb/src/upper/rrc.cc +++ b/srsenb/src/upper/rrc.cc @@ -865,7 +865,7 @@ void rrc::run_thread() Activity monitor class *******************************************************************************/ -rrc::activity_monitor::activity_monitor(rrc* parent_) +rrc::activity_monitor::activity_monitor(rrc* parent_) : thread("RRC_ACTIVITY_MONITOR") { running = true; parent = parent_; diff --git a/srsenb/src/upper/s1ap.cc b/srsenb/src/upper/s1ap.cc index 266838947..4eaea0cc2 100644 --- a/srsenb/src/upper/s1ap.cc +++ b/srsenb/src/upper/s1ap.cc @@ -38,6 +38,17 @@ using srslte::uint32_to_uint8; namespace srsenb{ +s1ap::s1ap() : + thread("S1AP"), + rrc(nullptr), + s1ap_log(nullptr), + pool(nullptr), + mme_connected(false), + running(false), + next_eNB_UE_S1AP_ID(1), + next_ue_stream_id(1){}; +s1ap::~s1ap(){}; + bool s1ap::init(s1ap_args_t args_, rrc_interface_s1ap *rrc_, srslte::log *s1ap_log_) { rrc = rrc_; diff --git a/srsepc/src/mbms-gw/mbms-gw.cc b/srsepc/src/mbms-gw/mbms-gw.cc index 46ae9f71c..8273d5ecb 100644 --- a/srsepc/src/mbms-gw/mbms-gw.cc +++ b/srsepc/src/mbms-gw/mbms-gw.cc @@ -39,9 +39,7 @@ pthread_mutex_t mbms_gw_instance_mutex = PTHREAD_MUTEX_INITIALIZER; const uint16_t MBMS_GW_BUFFER_SIZE = 2500; -mbms_gw::mbms_gw(): - m_running(false), - m_sgi_mb_up(false) +mbms_gw::mbms_gw() : m_running(false), m_sgi_mb_up(false), thread("MBMS_GW") { return; } diff --git a/srsepc/src/mme/mme.cc b/srsepc/src/mme/mme.cc index 266165cf4..6f480d5ef 100644 --- a/srsepc/src/mme/mme.cc +++ b/srsepc/src/mme/mme.cc @@ -31,7 +31,7 @@ namespace srsepc { mme* mme::m_instance = NULL; pthread_mutex_t mme_instance_mutex = PTHREAD_MUTEX_INITIALIZER; -mme::mme() : m_running(false) +mme::mme() : m_running(false), thread("MME") { m_pool = srslte::byte_buffer_pool::get_instance(); return; diff --git a/srsepc/src/spgw/spgw.cc b/srsepc/src/spgw/spgw.cc index 1da9dbf37..439152e41 100644 --- a/srsepc/src/spgw/spgw.cc +++ b/srsepc/src/spgw/spgw.cc @@ -31,7 +31,7 @@ namespace srsepc { spgw* spgw::m_instance = NULL; pthread_mutex_t spgw_instance_mutex = PTHREAD_MUTEX_INITIALIZER; -spgw::spgw() : m_running(false) +spgw::spgw() : m_running(false), thread("SPGW") { m_gtpc = new spgw::gtpc; m_gtpu = new spgw::gtpu; diff --git a/srsue/src/phy/async_scell_recv.cc b/srsue/src/phy/async_scell_recv.cc index 0a5ba389c..1d2eaae6b 100644 --- a/srsue/src/phy/async_scell_recv.cc +++ b/srsue/src/phy/async_scell_recv.cc @@ -45,7 +45,7 @@ namespace srsue { -async_scell_recv::async_scell_recv() : thread() +async_scell_recv::async_scell_recv() : thread("ASYNC_SCELL_RECV") { initiated = false; buffer_write_idx = 0; diff --git a/srsue/src/phy/phy.cc b/srsue/src/phy/phy.cc index 4279b16f2..32d807705 100644 --- a/srsue/src/phy/phy.cc +++ b/srsue/src/phy/phy.cc @@ -42,7 +42,7 @@ using namespace asn1::rrc; namespace srsue { -phy::phy() : workers_pool(MAX_WORKERS), workers(0), common(MAX_WORKERS), scell_sync() +phy::phy() : workers_pool(MAX_WORKERS), workers(0), common(MAX_WORKERS), scell_sync(), thread("PHY") { tdd_config = {}; prach_cfg = {}; diff --git a/srsue/src/phy/sync.cc b/srsue/src/phy/sync.cc index 6decf7161..a1f78d6e0 100644 --- a/srsue/src/phy/sync.cc +++ b/srsue/src/phy/sync.cc @@ -42,7 +42,7 @@ double callback_set_rx_gain(void *h, double gain) { return ((sync*)h)->set_rx_gain(gain); } -sync::sync() +sync::sync() : thread("SYNC") { cellsearch_earfcn_index = 0; current_sflen = 0; @@ -1665,7 +1665,7 @@ int sync::meas_stop(uint32_t earfcn, int pci) return -1; } -sync::intra_measure::intra_measure() : scell() +sync::intra_measure::intra_measure() : scell(), thread("SYNC_INTRA_MEASURE") { rrc = NULL; diff --git a/srsue/src/stack/mac/mac.cc b/srsue/src/stack/mac/mac.cc index 6d3ef6b6c..9fac29aaa 100644 --- a/srsue/src/stack/mac/mac.cc +++ b/srsue/src/stack/mac/mac.cc @@ -37,7 +37,7 @@ using namespace asn1::rrc; namespace srsue { -mac::mac() : timers(64), pdu_process_thread(&demux_unit), mch_msg(10), running(false), pcap(nullptr) +mac::mac() : timers(64), pdu_process_thread(&demux_unit), mch_msg(10), running(false), pcap(nullptr), thread("MAC") { // Create PCell HARQ entities auto ul = ul_harq_entity_ptr(new ul_harq_entity()); @@ -646,7 +646,7 @@ uint32_t mac::timer_get_unique_id() * DEMUX unit * *******************************************************/ -mac::pdu_process::pdu_process(demux *demux_unit_) +mac::pdu_process::pdu_process(demux* demux_unit_) : thread("MAC_PDU_PROCESS") { demux_unit = demux_unit_; pthread_mutex_init(&mutex, NULL); diff --git a/srsue/src/stack/rrc/rrc.cc b/srsue/src/stack/rrc/rrc.cc index 455ac5ee6..5c41bc8bd 100644 --- a/srsue/src/stack/rrc/rrc.cc +++ b/srsue/src/stack/rrc/rrc.cc @@ -51,7 +51,8 @@ rrc::rrc() : last_state(RRC_STATE_CONNECTED), drb_up(false), rlc_flush_timeout(2000), - rlc_flush_counter(0) + rlc_flush_counter(0), + thread("RRC") { n310_cnt = 0; n311_cnt = 0; diff --git a/srsue/src/stack/upper/gw.cc b/srsue/src/stack/upper/gw.cc index ed109d54f..e0f3f6369 100644 --- a/srsue/src/stack/upper/gw.cc +++ b/srsue/src/stack/upper/gw.cc @@ -69,8 +69,7 @@ namespace srsue { #define DRB1_LCID 3 -gw::gw() - :if_up(false) +gw::gw() : if_up(false), thread("GW") { current_ip_addr = 0; }