Updating thread naming.

Also includes experimental code to adapt the thread name as shown by
top, but it's untested.
This commit is contained in:
Robin Sommer 2012-02-03 04:01:24 -08:00
parent cf6a346b86
commit 70fe7876a1
12 changed files with 81 additions and 14 deletions

View file

@ -9,7 +9,7 @@ using namespace threading;
uint64_t BasicThread::thread_counter = 0;
BasicThread::BasicThread(const string& arg_name)
BasicThread::BasicThread()
{
started = false;
terminating = false;
@ -18,7 +18,7 @@ BasicThread::BasicThread(const string& arg_name)
buf = 0;
buf_len = 1024;
name = Fmt("%s@%d", arg_name.c_str(), ++thread_counter);
name = Fmt("thread-%d", ++thread_counter);
thread_mgr->AddThread(this);
}
@ -27,6 +27,27 @@ BasicThread::~BasicThread()
{
}
void BasicThread::SetName(const string& arg_name)
{
// Slight race condition here with reader threads, but shouldn't matter.
name = arg_name;
}
void BasicThread::SetOSName(const string& name)
{
#ifdef LINUX
pthread_setname_np(pthread_self(), name.c_str());
#endif
#ifdef __APPLE__
pthread_setname_np(name.c_str());
#endif
#ifdef FREEBSD
pthread_set_name_np(pthread_self(), name, name.c_str());
#endif
}
const char* BasicThread::Fmt(const char* format, ...)
{
if ( ! buf )

View file

@ -34,16 +34,31 @@ public:
* @param name A descriptive name for thread the thread. This may
* show up in messages to the user.
*/
BasicThread(const string& name); // Managed by manager, must not delete otherwise.
BasicThread();
/**
* Returns a descriptive name for the thread. This is the name passed
* into the constructor.
* Returns a descriptive name for the thread. If not set via
* SetName(). If not set, a default name is choosen automatically.
*
* This method is safe to call from any thread.
*/
const string& Name() const { return name; }
/**
* Sets a descriptive name for the thread. This should be a string
* that's useful in output presented to the user and uniquely
* identifies the thread.
*
* This method must be called only from the thread itself.
*/
void SetName(const string& name);
/**
* Set the name shown by the OS as the thread's description. Not
* supported on all OSs.
*/
void SetOSName(const string& name);
/**
* Starts the thread. Calling this methods will spawn a new OS thread
* executing Run(). Note that one can't restart a thread after a

View file

@ -127,7 +127,7 @@ bool ReporterMessage::Process()
return true;
}
MsgThread::MsgThread(const string& name) : BasicThread(name)
MsgThread::MsgThread() : BasicThread()
{
cnt_sent_in = cnt_sent_out = 0;
thread_mgr->AddMsgThread(this);
@ -142,6 +142,12 @@ void MsgThread::OnStop()
void MsgThread::Heartbeat()
{
SendIn(new HeartbeatMessage(this, network_time, current_time()));
string name = Fmt("%s (%d/%d)", name.c_str(),
cnt_sent_in - queue_in.Size(),
cnt_sent_out - queue_out.Size());
SetOSName(name.c_str());
}
void MsgThread::Info(const char* msg)

View file

@ -34,10 +34,8 @@ public:
* threading::Manager.
*
* Only Bro's main thread may instantiate a new thread.
*
* @param name A descriptive name. This is passed on to BasicThread().
*/
MsgThread(const string& name);
MsgThread();
/**
* Sends a message to the child thread. The message will be proceesed