[Zodb-checkins] SVN: ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/ First implementation using nanosleep.

Jürgen Kartnaller juergen at kartnaller.at
Tue Mar 27 02:34:32 EDT 2007


Log message for revision 73659:
  First implementation using nanosleep.
  
  This version is only tested on MACi OS-X!
  
  Speed could be increased by factor 6.3 on MAC OS-X :)
  The minimum sleep time on MAC OS-X is 10ms.
  

Changed:
  A   ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/condition.py
  U   ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/connection.py
  A   ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/nanosleep.py

-=-
Added: ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/condition.py
===================================================================
--- ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/condition.py	2007-03-27 06:24:50 UTC (rev 73658)
+++ ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/condition.py	2007-03-27 06:34:31 UTC (rev 73659)
@@ -0,0 +1,71 @@
+##############################################################################
+#
+# Copyright (c) 2006 Lovely Systems and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+This is a patch to thread.Condition which uses nanosleep.
+
+$Id$
+"""
+__docformat__ = 'restructuredtext'
+
+import threading
+
+from nanosleep import nanosleep
+
+
+def Condition(*args, **kwargs):
+    return _Condition(*args, **kwargs)
+
+
+class _Condition(threading._Condition):
+
+    def wait(self, timeout=None):
+        assert self._is_owned(), "wait() of un-acquire()d lock"
+        waiter = threading._allocate_lock()
+        waiter.acquire()
+        self.__waiters.append(waiter)
+        saved_state = self._release_save()
+        try:    # restore state no matter what (e.g., KeyboardInterrupt)
+            if timeout is None:
+                waiter.acquire()
+                if __debug__:
+                    self._note("%s.wait(): got it", self)
+            else:
+                # Balancing act:  We can't afford a pure busy loop, so we
+                # have to sleep; but if we sleep the whole timeout time,
+                # we'll be unresponsive.  The scheme here sleeps very
+                # little at first, longer as time goes on, but never longer
+                # than 20 times per second (or the timeout time remaining).
+                endtime = threading._time() + timeout
+                delay = 100000 # 100 microsec
+                while True:
+                    gotit = waiter.acquire(0)
+                    if gotit:
+                        break
+                    remaining = endtime - threading._time()
+                    if remaining <= 0:
+                        break
+                    nanosleep(0, delay)
+                if not gotit:
+                    if __debug__:
+                        self._note("%s.wait(%s): timed out", self, timeout)
+                    try:
+                        self.__waiters.remove(waiter)
+                    except ValueError:
+                        pass
+                else:
+                    if __debug__:
+                        self._note("%s.wait(%s): got it", self, timeout)
+        finally:
+            self._acquire_restore(saved_state)
+


Property changes on: ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/condition.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native

Modified: ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/connection.py
===================================================================
--- ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/connection.py	2007-03-27 06:24:50 UTC (rev 73658)
+++ ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/connection.py	2007-03-27 06:34:31 UTC (rev 73659)
@@ -28,6 +28,7 @@
 from ZEO.zrpc.marshal import Marshaller
 from ZEO.zrpc.trigger import trigger
 from ZEO.zrpc.log import short_repr, log
+from ZEO.zrpc import condition
 from ZODB.loglevels import BLATHER, TRACE
 
 REPLY = ".reply" # message name used for replies
@@ -386,7 +387,7 @@
 
         # replies_cond is used to block when a synchronous call is
         # waiting for a response
-        self.replies_cond = threading.Condition()
+        self.replies_cond = condition.Condition()
         self.replies = {}
 
         # waiting_for_reply is used internally to indicate whether

Added: ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/nanosleep.py
===================================================================
--- ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/nanosleep.py	2007-03-27 06:24:50 UTC (rev 73658)
+++ ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/nanosleep.py	2007-03-27 06:34:31 UTC (rev 73659)
@@ -0,0 +1,69 @@
+##############################################################################
+#
+# Copyright (c) 2006 Lovely Systems and Contributors.
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+"""
+Because time.sleep() has a system dependend minimum sleep time the ZEO
+connection can't use the full speed of the network because sleep is used to
+wait for packages from ZEO.
+
+We use nanosleep which allowes the use of a system independent sleep time.
+
+To be able to use nanosleep we use ctypes from python 2.5. Because we can not
+use python 2.5 with zope the ctypes packe needs to be installed separately.
+
+$Id$
+"""
+__docformat__ = 'restructuredtext'
+
+import logging
+
+logger = logging.getLogger('ZEO.ClientStorage')
+
+try:
+    import ctypes
+    try:
+        libc = ctypes.CDLL("libc.so")
+    except OSError:
+        libc = None
+    if libc is None:
+        # MAC OS-X
+        try:
+            libc = ctypes.CDLL("libc.dylib", ctypes.RTLD_GLOBAL)
+        except OSError:
+            raise ImportError
+
+    class timespec(ctypes.Structure):
+        _fields_ = [('secs', ctypes.c_long),
+                    ('nsecs', ctypes.c_long),
+                   ]
+
+    libc.nanosleep.argtypes = \
+            [ctypes.POINTER(timespec), ctypes.POINTER(timespec)]
+
+    logger.info('Connection using nanosleep!')
+    def nanosleep(sec, nsec):
+        sleeptime = timespec()
+        sleeptime.secs = sec
+        sleeptime.nsecs = nsec
+        remaining = timespec()
+        libc.nanosleep(sleeptime, remaining)
+        return (remaining.secs, remaining.nsecs)
+
+except ImportError:
+    # if ctypes is not available or no reasonable library is found we provide
+    # a dummy which uses time.sleep
+    logger.info('Connection using time.sleep!')
+    import time
+    def nanosleep(sec, nsec):
+        time.sleep(sec + (nsec * 0.000000001))
+


Property changes on: ZODB/branches/jukart_nanosleep/src/ZEO/zrpc/nanosleep.py
___________________________________________________________________
Name: svn:keywords
   + Id
Name: svn:eol-style
   + native



More information about the Zodb-checkins mailing list