Changed thread Id handling in trace.

Review URL: http://webrtc-codereview.appspot.com/331020

git-svn-id: http://webrtc.googlecode.com/svn/trunk@1383 4adac7df-926f-26a2-2b94-8c16560cd09d
This commit is contained in:
pwestin@webrtc.org 2012-01-11 08:28:04 +00:00
parent e3eaf44ccf
commit b54d72778c
10 changed files with 75 additions and 94 deletions

View File

@ -16,6 +16,9 @@
#ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_ #ifndef WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_
#define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_ #define WEBRTC_SYSTEM_WRAPPERS_INTERFACE_THREAD_WRAPPER_H_
#include "common_types.h"
#include "typedefs.h"
namespace webrtc { namespace webrtc {
// Object that will be passed by the spawned thread when it enters the callback // Object that will be passed by the spawned thread when it enters the callback
// function. // function.
@ -51,9 +54,12 @@ public:
// threadName NULL terminated thread name, will be visable in the Windows // threadName NULL terminated thread name, will be visable in the Windows
// debugger. // debugger.
static ThreadWrapper* CreateThread(ThreadRunFunction func = 0, static ThreadWrapper* CreateThread(ThreadRunFunction func = 0,
ThreadObj obj= 0, ThreadObj obj= 0,
ThreadPriority prio = kNormalPriority, ThreadPriority prio = kNormalPriority,
const char* threadName = 0); const char* threadName = 0);
// Get the current thread's kernel thread ID.
static uint32_t GetThreadId();
// Non blocking termination of the spawned thread. Note that it is not safe // Non blocking termination of the spawned thread. Note that it is not safe
// to delete this class until the spawned thread has been reclaimed. // to delete this class until the spawned thread has been reclaimed.
@ -69,8 +75,9 @@ public:
// should be lower than (number of CPUs - 1). amountOfProcessors should be // should be lower than (number of CPUs - 1). amountOfProcessors should be
// equal to the number of processors listed in processorNumbers // equal to the number of processors listed in processorNumbers
virtual bool SetAffinity(const int* /*processorNumbers*/, virtual bool SetAffinity(const int* /*processorNumbers*/,
const unsigned int /*amountOfProcessors*/) const unsigned int /*amountOfProcessors*/) {
{return false;} return false;
}
// Stops the spawned thread and waits for it to be reclaimed with a timeout // Stops the spawned thread and waits for it to be reclaimed with a timeout
// of two seconds. Will return false if the thread was not reclaimed. // of two seconds. Will return false if the thread was not reclaimed.

View File

@ -22,6 +22,10 @@
#include <sys/prctl.h> #include <sys/prctl.h>
#endif #endif
#if defined(WEBRTC_MAC)
#include <mach/mach.h>
#endif
#include "event_wrapper.h" #include "event_wrapper.h"
#include "trace.h" #include "trace.h"
@ -35,17 +39,6 @@ extern "C"
} }
} }
#if (defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID))
static pid_t gettid()
{
#if defined(__NR_gettid)
return syscall(__NR_gettid);
#else
return -1;
#endif
}
#endif
ThreadWrapper* ThreadPosix::Create(ThreadRunFunction func, ThreadObj obj, ThreadWrapper* ThreadPosix::Create(ThreadRunFunction func, ThreadObj obj,
ThreadPriority prio, const char* threadName) ThreadPriority prio, const char* threadName)
{ {
@ -71,11 +64,9 @@ ThreadPosix::ThreadPosix(ThreadRunFunction func, ThreadObj obj,
_dead(true), _dead(true),
_prio(prio), _prio(prio),
_event(EventWrapper::Create()), _event(EventWrapper::Create()),
_setThreadName(false) _setThreadName(false),
_pid(-1)
{ {
#ifdef WEBRTC_LINUX
_linuxPid = -1;
#endif
if (threadName != NULL) if (threadName != NULL)
{ {
_setThreadName = true; _setThreadName = true;
@ -83,6 +74,16 @@ ThreadPosix::ThreadPosix(ThreadRunFunction func, ThreadObj obj,
} }
} }
uint32_t ThreadWrapper::GetThreadId() {
#if defined(WEBRTC_ANDROID) || defined(WEBRTC_LINUX)
return static_cast<uint32_t>(syscall(__NR_gettid));
#elif defined(WEBRTC_MAC)
return static_cast<uint32_t>(mach_thread_self());
#else
return reinterpret_cast<uint32_t>(pthread_self());
#endif
}
int ThreadPosix::Construct() int ThreadPosix::Construct()
{ {
int result = 0; int result = 0;
@ -104,7 +105,6 @@ int ThreadPosix::Construct()
{ {
return -1; return -1;
} }
return 0; return 0;
} }
@ -191,33 +191,38 @@ bool ThreadPosix::Start(unsigned int& /*threadID*/)
return true; return true;
} }
#if (defined(WEBRTC_LINUX) && !defined(WEBRTC_ANDROID)) #if (defined(WEBRTC_LINUX) || defined(WEBRTC_ANDROID))
bool ThreadPosix::SetAffinity(const int* processorNumbers, bool ThreadPosix::SetAffinity(const int* processorNumbers,
const unsigned int amountOfProcessors) const unsigned int amountOfProcessors) {
{ if (!processorNumbers || (amountOfProcessors == 0)) {
if (!processorNumbers || (amountOfProcessors == 0)) return false;
{ }
return false; cpu_set_t mask;
} CPU_ZERO(&mask);
cpu_set_t mask; for (unsigned int processor = 0;
CPU_ZERO(&mask); processor < amountOfProcessors;
processor++) {
for(unsigned int processor = 0; CPU_SET(processorNumbers[processor], &mask);
processor < amountOfProcessors; }
processor++) #if defined(WEBRTC_ANDROID)
{ // Android.
CPU_SET(processorNumbers[processor], &mask); const int result = syscall(__NR_sched_setaffinity,
} _pid,
const int result = sched_setaffinity(_linuxPid, (unsigned int)sizeof(mask), sizeof(mask),
&mask); &mask);
if (result != 0) #else
{ // "Normal" Linux.
return false; const int result = sched_setaffinity(_pid,
sizeof(mask),
} &mask);
return true; #endif
if (result != 0) {
return false;
}
return true;
} }
#else #else
// NOTE: On Mac OS X, use the Thread affinity API in // NOTE: On Mac OS X, use the Thread affinity API in
// /usr/include/mach/thread_policy.h: thread_policy_set and mach_thread_self() // /usr/include/mach/thread_policy.h: thread_policy_set and mach_thread_self()
@ -274,34 +279,21 @@ void ThreadPosix::Run()
{ {
_alive = true; _alive = true;
_dead = false; _dead = false;
#ifdef WEBRTC_LINUX _pid = GetThreadId();
if(_linuxPid == -1)
{
_linuxPid = gettid();
}
#endif
// The event the Start() is waiting for. // The event the Start() is waiting for.
_event->Set(); _event->Set();
if (_setThreadName) if (_setThreadName)
{ {
#ifdef WEBRTC_LINUX #ifdef WEBRTC_LINUX
WEBRTC_TRACE(kTraceStateInfo, kTraceUtility,-1,
"Thread with id:%d name:%s started ", _linuxPid, _name);
prctl(PR_SET_NAME, (unsigned long)_name, 0, 0, 0); prctl(PR_SET_NAME, (unsigned long)_name, 0, 0, 0);
#else #endif
WEBRTC_TRACE(kTraceStateInfo, kTraceUtility,-1, WEBRTC_TRACE(kTraceStateInfo, kTraceUtility,-1,
"Thread with name:%s started ", _name); "Thread with name:%s started ", _name);
#endif } else
}else
{ {
#ifdef WEBRTC_LINUX
WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
"Thread with id:%d without name started", _linuxPid);
#else
WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1, WEBRTC_TRACE(kTraceStateInfo, kTraceUtility, -1,
"Thread without name started"); "Thread without name started");
#endif
} }
do do
{ {

View File

@ -59,10 +59,7 @@ private:
// handle to thread // handle to thread
pthread_attr_t _attr; pthread_attr_t _attr;
pthread_t _thread; pthread_t _thread;
#ifdef WEBRTC_LINUX pid_t _pid;
pid_t _linuxPid;
#endif
}; };
} // namespace webrtc } // namespace webrtc

View File

@ -68,6 +68,10 @@ ThreadWindows::~ThreadWindows()
} }
} }
uint32_t ThreadWrapper::GetThreadId() {
return GetCurrentThreadId();
}
unsigned int WINAPI ThreadWindows::StartThread(LPVOID lpParameter) unsigned int WINAPI ThreadWindows::StartThread(LPVOID lpParameter)
{ {
static_cast<ThreadWindows*>(lpParameter)->Run(); static_cast<ThreadWindows*>(lpParameter)->Run();

View File

@ -147,6 +147,12 @@ TraceImpl::~TraceImpl()
} }
} }
WebRtc_Word32 TraceImpl::AddThreadId(char* traceMessage) const {
WebRtc_UWord32 threadId = ThreadWrapper::GetThreadId();
// Messages is 12 characters.
return sprintf(traceMessage, "%10u; ", threadId);
}
WebRtc_Word32 TraceImpl::AddLevel(char* szMessage, const TraceLevel level) const WebRtc_Word32 TraceImpl::AddLevel(char* szMessage, const TraceLevel level) const
{ {
switch (level) switch (level)
@ -637,7 +643,7 @@ void TraceImpl::AddImpl(const TraceLevel level, const TraceModule module,
ackLen += len; ackLen += len;
len = AddThreadId(meassagePtr); len = AddThreadId(meassagePtr);
if(len == -1) if(len < 0)
{ {
return; return;
} }

View File

@ -69,8 +69,9 @@ protected:
static TraceImpl* StaticInstance(CountOperation count_operation, static TraceImpl* StaticInstance(CountOperation count_operation,
const TraceLevel level = kTraceAll); const TraceLevel level = kTraceAll);
WebRtc_Word32 AddThreadId(char* traceMessage) const;
// OS specific implementations // OS specific implementations
virtual WebRtc_Word32 AddThreadId(char* traceMessage) const = 0;
virtual WebRtc_Word32 AddTime(char* traceMessage, virtual WebRtc_Word32 AddTime(char* traceMessage,
const TraceLevel level) const = 0; const TraceLevel level) const = 0;

View File

@ -15,9 +15,6 @@
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
#include <time.h> #include <time.h>
#ifdef __linux__
#include <sys/syscall.h>
#endif
#ifdef WEBRTC_ANDROID #ifdef WEBRTC_ANDROID
#include <pthread.h> #include <pthread.h>
#else #else
@ -50,19 +47,6 @@ TracePosix::~TracePosix()
StopThread(); StopThread();
} }
WebRtc_Word32 TracePosix::AddThreadId(char* traceMessage) const {
#ifdef __linux__
pid_t threadId = (pid_t) syscall(__NR_gettid);
sprintf(traceMessage, "%10d; ", threadId);
#else
WebRtc_UWord64 threadId = (WebRtc_UWord64)pthread_self();
sprintf(traceMessage, "%10llu; ",
static_cast<long long unsigned int>(threadId));
#endif
// 12 bytes are written.
return 12;
}
WebRtc_Word32 TracePosix::AddTime(char* traceMessage, WebRtc_Word32 TracePosix::AddTime(char* traceMessage,
const TraceLevel level) const const TraceLevel level) const
{ {

View File

@ -21,7 +21,6 @@ public:
TracePosix(); TracePosix();
virtual ~TracePosix(); virtual ~TracePosix();
virtual WebRtc_Word32 AddThreadId(char *traceMessage) const;
virtual WebRtc_Word32 AddTime(char* traceMessage, virtual WebRtc_Word32 AddTime(char* traceMessage,
const TraceLevel level) const; const TraceLevel level) const;

View File

@ -41,14 +41,6 @@ TraceWindows::~TraceWindows()
StopThread(); StopThread();
} }
WebRtc_Word32 TraceWindows::AddThreadId(char* traceMessage) const
{
WebRtc_UWord32 threadId= GetCurrentThreadId();
sprintf (traceMessage, "%10u; ", threadId);
// Messages is 12 characters.
return 12;
}
WebRtc_Word32 TraceWindows::AddTime(char* traceMessage, WebRtc_Word32 TraceWindows::AddTime(char* traceMessage,
const TraceLevel level) const const TraceLevel level) const
{ {

View File

@ -20,9 +20,8 @@ class TraceWindows : public TraceImpl
{ {
public: public:
TraceWindows(); TraceWindows();
virtual ~TraceWindows(); virtual ~TraceWindows();
virtual WebRtc_Word32 AddThreadId(char *traceMessage) const;
virtual WebRtc_Word32 AddTime(char* traceMessage, virtual WebRtc_Word32 AddTime(char* traceMessage,
const TraceLevel level) const; const TraceLevel level) const;