antirez/redis — Issue #357: Remove the pthread mutex for memory usage counting
Author: antirez
Created: 2012-02-25 11:34:53+00:00
State: open
Labels: feature request

Redis maxmemory directive and INFO memory reporting is implemented using a malloc wrapper in zmalloc.c that is able to keep track of allocations and deallocations. The global variable (but static to zmalloc.c) is modified on malloc/free/realloc calls to reflect the amount of memory allocated by Redis, and since allocations may happen in different threads (now that we have bio.c for background syscalls, and before because of the Redis VM system, now deprecated) we currently protect the increment and decrement of this global counter using a pthread_mutex.

Profiling Redis shows that, at least on osx (proof needed for Linux), even without contention the mutex is burning a large amount of CPU, so it is probably a good idea to replace the mutex with an alternative system.
Among alternatives we could use:
* Atomic operations where supported (enabling it only if GCC is used is already enough probably).
* Pthread spin locks instead of mutex.
* Per-thread local counter, at least now that only the main thread is the one allocating memory for the data set.
The most promising short term solution appears to be to use GCC-specific atomic primitives if GCC is used, otherwise switching to the current mutex based implementation.