views:

249

answers:

2

I am working on a parallel app (C, pthread). I traced the system calls because at some point I have bad parallel performances. My traces shown that my program calls mprotect() many many times ... enough to significantly slow down my program.

I do allocate a lot of memory (with malloc()) but there is only a reasonable number of calls to brk() to increase the heap size. So why so many calls to mprotect() ?!

+2  A: 

If you can, run your program under a debug libc and break on mprotect(). Look at the call stack, see what your code is doing that's leading to the mprotect() calls.

Steve McKay
+2  A: 

Are you creating and destroying lots of threads?

Most pthread implementations will add a "guard page" when allocating a threads stack. It's an access protected memory page used to detect stack overflows. I'd expect at least one call to mprotect each time a thread is created or terminated to (un)protect the guard page. If this is the case, there are several obvious strategies:

  1. Set the guard page size to zero using pthread_attr_setguardsize() before creating threads.
  2. Use a thread-pool (of as many threads as processors say). Once a thread is done with a task, return it to the pool to get a new task rather than terminate and create a new thread.

Another explanation might be that you're on a platform where a thread's stack will be grown if overflow is detected. I don't think this is implemented on Linux with GCC/Glibc as yet, but there have been some proposals along these lines recently. If you use a lot of stack space whilst processing, you might explicitely increase the initial/minimum stack size using pthread_attr_setstacksize.

Or it might be something else entirely!

Wuggy
No I only create a finite number of threads at the beginning, thanks however.
Ben
Hmm... are you using mmap()? I vaguely recall an issue a colleague of mine had where mprotect() was being called many times - once per page - of an mmapped region. I don't know whether we ever got to the bottom of that one.Might it be possible to post the strace output?
Wuggy