This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Author neologix
Recipients neologix, pitrou
Date 2011-03-05.14:09:06
SpamBayes Score 1.3785113e-09
Marked as misclassified No
Message-id <1299334147.69.0.216828112107.issue11408@psf.upfronthosting.co.za>
In-reply-to
Content
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.
History
Date User Action Args
2011-03-05 14:09:08neologixsetrecipients: + neologix, pitrou
2011-03-05 14:09:07neologixsetmessageid: <1299334147.69.0.216828112107.issue11408@psf.upfronthosting.co.za>
2011-03-05 14:09:07neologixlinkissue11408 messages
2011-03-05 14:09:06neologixcreate