classification
Title: python locks: blocking acquire calls useless gettimeofday
Type: performance Stage: resolved
Components: Versions: Python 3.3
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: Nosy List: neologix, pitrou
Priority: normal Keywords: patch

Created on 2011-03-05 14:09 by neologix, last changed 2011-03-06 07:41 by pitrou. This issue is now closed.

Files
File name Uploaded Description Edit
lock_gettimeofday_py3k.diff neologix, 2011-03-05 14:09
Messages (2)
msg130121 - (view) Author: Charles-Fran├žois Natali (neologix) * (Python committer) Date: 2011-03-05 14:09
While tracing a program using multiprocessing queues, I noticed that there were many calls to gettimeofday.
It turns out that acquire_timed, used by lock_PyThread_acquire_lock and rlock_acquire, always call gettimeofday, even if no timeout argument is given.
Here's an example of the performance impact (I know it's a contrived example :-):

$ cat /tmp/test_lock.py 
import threading

lock = threading.Lock()

i = 0

def do_loop():
    global i
    for j in range(500000):
        lock.acquire()
        i += 1
        lock.release()


t1 = threading.Thread(target=do_loop)
t2 = threading.Thread(target=do_loop)
t1.start()
t2.start()
t1.join()
t2.join()

With current code:
$ time ./python /tmp/test_lock.py 

real    0m5.200s
user    0m3.288s
sys     0m1.896s

Without useless calls to gettimeofday:
$ time ./python /tmp/test_lock.py 

real    0m3.091s
user    0m3.056s
sys     0m0.020s

Note that the actual gain depends on the kernel, hardware and clocksource in use (the above measurements are on a Linux 2.6.32 kernel, using acpi_pm as clocksource).

Attached is a patch removing useless calls to gettimeofday.
Note that I also removed the check for expired timeout following trylock in case of PY_LOCK_INTR, since according to http://pubs.opengroup.org/onlinepubs/009695399/functions/sem_wait.html,  it seems that only sem_wait is interruptible, not sem_trywait (e.g. on Linux, sem_trywait is implemented using futex which handle non-contended case in user-space). Windows locking primitives can't return PY_LOCK_INTR. Anyway, even if it happend once in a blue moon, we would just retry a trylock, which kind of makes sense.
msg130148 - (view) Author: Antoine Pitrou (pitrou) * (Python committer) Date: 2011-03-06 07:41
Pushed in 6ba9ba58499e, thank you.
History
Date User Action Args
2011-03-06 07:41:10pitrousetstatus: open -> closed
versions: + Python 3.3
nosy: pitrou, neologix
messages: + msg130148

resolution: fixed
stage: resolved
2011-03-05 14:09:07neologixcreate