Browse Source

Add support for thread names

Paul-Louis Ageneau 2 years ago
parent
commit
80d1ae90aa

+ 0 - 1
src/impl/dtlstransport.hpp

@@ -19,7 +19,6 @@
 #include <functional>
 #include <functional>
 #include <memory>
 #include <memory>
 #include <mutex>
 #include <mutex>
-#include <thread>
 
 
 namespace rtc::impl {
 namespace rtc::impl {
 
 

+ 3 - 2
src/impl/init.cpp

@@ -7,15 +7,15 @@
  */
  */
 
 
 #include "init.hpp"
 #include "init.hpp"
-#include "internals.hpp"
-
 #include "certificate.hpp"
 #include "certificate.hpp"
 #include "dtlstransport.hpp"
 #include "dtlstransport.hpp"
 #include "icetransport.hpp"
 #include "icetransport.hpp"
+#include "internals.hpp"
 #include "pollservice.hpp"
 #include "pollservice.hpp"
 #include "sctptransport.hpp"
 #include "sctptransport.hpp"
 #include "threadpool.hpp"
 #include "threadpool.hpp"
 #include "tls.hpp"
 #include "tls.hpp"
+#include "utils.hpp"
 
 
 #if RTC_ENABLE_WEBSOCKET
 #if RTC_ENABLE_WEBSOCKET
 #include "tlstransport.hpp"
 #include "tlstransport.hpp"
@@ -43,6 +43,7 @@ struct Init::TokenPayload {
 	~TokenPayload() {
 	~TokenPayload() {
 		std::thread t(
 		std::thread t(
 		    [](std::promise<void> promise) {
 		    [](std::promise<void> promise) {
+				utils::this_thread::set_name("RTC cleanup");
 			    try {
 			    try {
 				    Init::Instance().doCleanup();
 				    Init::Instance().doCleanup();
 				    promise.set_value();
 				    promise.set_value();

+ 2 - 1
src/impl/peerconnection.cpp

@@ -9,7 +9,6 @@
 
 
 #include "peerconnection.hpp"
 #include "peerconnection.hpp"
 #include "certificate.hpp"
 #include "certificate.hpp"
-#include "common.hpp"
 #include "dtlstransport.hpp"
 #include "dtlstransport.hpp"
 #include "icetransport.hpp"
 #include "icetransport.hpp"
 #include "internals.hpp"
 #include "internals.hpp"
@@ -18,6 +17,7 @@
 #include "processor.hpp"
 #include "processor.hpp"
 #include "rtp.hpp"
 #include "rtp.hpp"
 #include "sctptransport.hpp"
 #include "sctptransport.hpp"
+#include "utils.hpp"
 
 
 #if RTC_ENABLE_MEDIA
 #if RTC_ENABLE_MEDIA
 #include "dtlssrtptransport.hpp"
 #include "dtlssrtptransport.hpp"
@@ -1061,6 +1061,7 @@ void PeerConnection::processRemoteCandidate(Candidate candidate) {
 		if ((iceTransport = std::atomic_load(&mIceTransport))) {
 		if ((iceTransport = std::atomic_load(&mIceTransport))) {
 			weak_ptr<IceTransport> weakIceTransport{iceTransport};
 			weak_ptr<IceTransport> weakIceTransport{iceTransport};
 			std::thread t([weakIceTransport, candidate = std::move(candidate)]() mutable {
 			std::thread t([weakIceTransport, candidate = std::move(candidate)]() mutable {
+				utils::this_thread::set_name("RTC resolver");
 				if (candidate.resolve(Candidate::ResolveMode::Lookup))
 				if (candidate.resolve(Candidate::ResolveMode::Lookup))
 					if (auto iceTransport = weakIceTransport.lock())
 					if (auto iceTransport = weakIceTransport.lock())
 						iceTransport->addRemoteCandidate(std::move(candidate));
 						iceTransport->addRemoteCandidate(std::move(candidate));

+ 4 - 2
src/impl/pollservice.cpp

@@ -8,6 +8,7 @@
 
 
 #include "pollservice.hpp"
 #include "pollservice.hpp"
 #include "internals.hpp"
 #include "internals.hpp"
+#include "utils.hpp"
 
 
 #if RTC_ENABLE_WEBSOCKET
 #if RTC_ENABLE_WEBSOCKET
 
 
@@ -158,10 +159,11 @@ void PollService::process(std::vector<struct pollfd> &pfds) {
 }
 }
 
 
 void PollService::runLoop() {
 void PollService::runLoop() {
+	utils::this_thread::set_name("RTC poll");
+	PLOG_DEBUG << "Poll service started";
+
 	try {
 	try {
-		PLOG_DEBUG << "Poll service started";
 		assert(mSocks);
 		assert(mSocks);
-
 		std::vector<struct pollfd> pfds;
 		std::vector<struct pollfd> pfds;
 		optional<clock::time_point> next;
 		optional<clock::time_point> next;
 		while (!mStopped) {
 		while (!mStopped) {

+ 2 - 0
src/impl/threadpool.cpp

@@ -7,6 +7,7 @@
  */
  */
 
 
 #include "threadpool.hpp"
 #include "threadpool.hpp"
+#include "utils.hpp"
 
 
 namespace rtc::impl {
 namespace rtc::impl {
 
 
@@ -54,6 +55,7 @@ void ThreadPool::clear() {
 }
 }
 
 
 void ThreadPool::run() {
 void ThreadPool::run() {
+	utils::this_thread::set_name("RTC worker");
 	++mBusyWorkers;
 	++mBusyWorkers;
 	scope_guard guard([&]() { --mBusyWorkers; });
 	scope_guard guard([&]() { --mBusyWorkers; });
 	while (runOne()) {
 	while (runOne()) {

+ 31 - 0
src/impl/utils.cpp

@@ -18,6 +18,13 @@
 #include <sstream>
 #include <sstream>
 #include <thread>
 #include <thread>
 
 
+#if defined(__linux__)
+#include <sys/prctl.h> // for prctl(PR_SET_NAME)
+#endif
+#if defined(__FreeBSD__)
+#include <pthread_np.h> // for pthread_set_name_np
+#endif
+
 namespace rtc::impl::utils {
 namespace rtc::impl::utils {
 
 
 using std::to_integer;
 using std::to_integer;
@@ -179,4 +186,28 @@ std::multimap<string, string> parseHttpHeaders(const std::list<string> &lines) {
 	return headers;
 	return headers;
 }
 }
 
 
+namespace {
+
+void thread_set_name_self(const char *name) {
+#if defined(_WIN32)
+	(void)name;
+#elif defined(__linux__)
+	prctl(PR_SET_NAME, name);
+#elif defined(__APPLE__)
+	pthread_setname_np(name);
+#elif defined(__FreeBSD__)
+	pthread_set_name_np(pthread_self(), name);
+#else
+	(void)name;
+#endif
+}
+
+} // namespace
+
+namespace this_thread {
+	void set_name(const string &name) {
+		thread_set_name_self(name.c_str());
+	}
+}
+
 } // namespace rtc::impl::utils
 } // namespace rtc::impl::utils

+ 4 - 0
src/impl/utils.hpp

@@ -70,6 +70,10 @@ template <typename Generator = std::mt19937> auto random_bytes_engine() {
 	return random_engine<char_independent_bits_engine, uint8_t>();
 	return random_engine<char_independent_bits_engine, uint8_t>();
 }
 }
 
 
+namespace this_thread {
+void set_name(const string &name);
+}
+
 } // namespace rtc::impl::utils
 } // namespace rtc::impl::utils
 
 
 #endif
 #endif

+ 4 - 2
src/impl/websocketserver.cpp

@@ -12,6 +12,7 @@
 #include "common.hpp"
 #include "common.hpp"
 #include "internals.hpp"
 #include "internals.hpp"
 #include "threadpool.hpp"
 #include "threadpool.hpp"
+#include "utils.hpp"
 
 
 namespace rtc::impl {
 namespace rtc::impl {
 
 
@@ -41,8 +42,8 @@ WebSocketServer::WebSocketServer(Configuration config_)
 		}
 		}
 	}
 	}
 
 
-	const char* bindAddress = nullptr;
-	if(config.bindAddress){
+	const char *bindAddress = nullptr;
+	if (config.bindAddress) {
 		bindAddress = config.bindAddress->c_str();
 		bindAddress = config.bindAddress->c_str();
 	}
 	}
 	// Create TCP server
 	// Create TCP server
@@ -67,6 +68,7 @@ void WebSocketServer::stop() {
 }
 }
 
 
 void WebSocketServer::runLoop() {
 void WebSocketServer::runLoop() {
+	utils::this_thread::set_name("RTC server");
 	PLOG_INFO << "Starting WebSocketServer";
 	PLOG_INFO << "Starting WebSocketServer";
 
 
 	try {
 	try {