0

I my application I have a log wrapper class and macros like LOG_DEBUG(message), LOG_ERROR(message) and so on. When I use for example LOG_DEBUG(message) it prints the time, the message and the letter D which stands for debug. What I would like to do is to add to the macro the functionality to print the thread id which executed the LOG command. At first I added syscall(SYS_gettid) to the log wrapper class, this works alright, my question is, can I do it without the system call?

I though about using thread local storage to store in user space the thread id in a map where its key is given by pthread_key_create, but I wanted to know if there is some other way which I didn't think of.

While trying to figure it out I noticed by using strace that in order to get the time I'm actually calling a system call and thus for each log I get a context switch. I can't think of of a way to circumvent this but I wanted to know if getting the thread id by system call take roughly less, more or about the same time as getting the system time.

Kris Vandermotten
  • 10,111
  • 38
  • 49
e271p314
  • 3,841
  • 7
  • 36
  • 61
  • See http://ericlippert.com/2012/12/17/performance-rant/ – Kris Vandermotten Mar 15 '14 at 18:02
  • Why would you want to optimize debug tracing? If it's too slow, take it out. I assume you will take it out in an optimized build anyway, right? So, who cares? – Kris Vandermotten Mar 15 '14 at 18:09
  • Yes, LOG_DEBUG is optimized out but what about LOG_ERROR? The application has to work super fast so I want to avoid context switches as much as possible – e271p314 Mar 15 '14 at 19:07
  • If you're really concerned about it, call `gettid()` when the thread starts and store this value somewhere. – augurar Mar 15 '14 at 20:35
  • This is too abstract. Ofcourse I can store the tid of each thread but how do I know which thread executes the LOG command? pthread_key_create can help so I will have a map (in user space) between thread key and its tid but are there other alternatives? – e271p314 Mar 15 '14 at 20:56

1 Answers1

-1

Eventually, I used gcc implementation of thread local storage see this for more details.

I added a tid variable of pid_t type in the global scope but with __thread keyword in front. Inside the function that the thread executes, I assigned to tid its value by calling syscall(SYS_gettid). From that moment tid had the right value and from each thread you can access it directly as a global variable but it has its own copy for each thread. The last thing I had to do is to add tid to the LOG macros. This solves the issue completely and no complicated API of posix threads is required.

e271p314
  • 3,841
  • 7
  • 36
  • 61
  • this would be a much better answer if you simply included the macro – Ben H Sep 25 '14 at 18:06
  • The macro is the last thing that will help you since there is nothing in common in all possible mocros, what you should pay attention to is to the fact that local thread storage is written like any other global variable but behaves like it is allocated on stack – e271p314 Sep 26 '14 at 01:26