Skip to content

Commit

Permalink
Loop option to track per thread cpu clock
Browse files Browse the repository at this point in the history
  • Loading branch information
erin committed Feb 8, 2013
1 parent 7ea4c7a commit 1270397
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 5 deletions.
2 changes: 1 addition & 1 deletion diesel/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from logmod import log, levels as loglevels, set_log_level
import events
from core import sleep, Loop, wait, fire, thread, until, Connection, UDPSocket, ConnectionClosed, ClientConnectionClosed
from core import until_eol, send, receive, call, first, fork, fork_child, label, fork_from_thread
from core import until_eol, send, receive, call, first, fork, fork_child, label, fork_from_thread, clock
from core import ParentDiedException, ClientConnectionError, TerminateLoop, datagram
from app import Application, Service, UDPService, quickstart, quickstop, Thunk
from client import Client, UDPClient
Expand Down
37 changes: 34 additions & 3 deletions diesel/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
'''Core implementation/handling of coroutines, protocol primitives,
scheduling primitives, green-thread procedures.
'''
import os
import socket
import traceback
import errno
Expand Down Expand Up @@ -115,6 +116,9 @@ def fork_from_thread(f, *args, **kw):
l = Loop(f, *args, **kw)
runtime.current_app.hub.schedule_loop_from_other_thread(l, ContinueNothing)

def clock():
return current_loop.clocktime()

class call(object):
def __init__(self, f, inst=None):
self.f = f
Expand Down Expand Up @@ -149,7 +153,7 @@ def identity(cb): return cb
ids = itertools.count(1)

class Loop(object):
def __init__(self, loop_callable, *args, **kw):
def __init__(self, loop_callable, track=False, *args, **kw):
self.loop_callable = loop_callable
self.loop_label = str(self.loop_callable)
self.args = args
Expand All @@ -162,6 +166,10 @@ def __init__(self, loop_callable, *args, **kw):
self.parent = None
self.deaths = 0
self.reset()
self._clock = 0.0
self.clock = 0.0
self.tracked = track
self.dispatch = self._dispatch_track if self.tracked else self._dispatch

def reset(self):
self.running = False
Expand Down Expand Up @@ -237,10 +245,11 @@ def thread(self, f, *args, **kw):
def fork(self, make_child, f, *args, **kw):
def wrap():
return f(*args, **kw)
l = Loop(wrap)
l = Loop(wrap, track=self.tracked)
if make_child:
self.children.add(l)
l.parent = self
l.loop_label = str(f)
self.app.add_loop(l)
return l

Expand Down Expand Up @@ -428,7 +437,26 @@ def call_in():
def fire(self, event, value=None):
self.app.waits.fire(event, value)

def dispatch(self):
def start_clock(self):
usage = os.times()
self._clock = usage[0] + usage[1]

def update_clock(self):
usage = os.times()
now = usage[0] + usage[1]
self.clock += (now - self._clock)
self._clock = now

def clocktime(self):
self.update_clock()
return self.clock

def _dispatch(self):
r = self.app.runhub.switch()
return r

def _dispatch_track(self):
self.update_clock()
r = self.app.runhub.switch()
return r

Expand All @@ -441,6 +469,9 @@ def wake(self, value=ContinueNothing):
'''Wake up this loop. Called by the main hub to resume a loop
when it is rescheduled.
'''
if self.tracked:
self.start_clock()

global current_loop

# if we have a fire pending,
Expand Down
28 changes: 28 additions & 0 deletions examples/clocker.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
import os
from diesel import Loop, fork, Application, clock, sleep

def usage():
u = os.times()
return u[0] + u[1]

def not_always_busy_worker(clocker):
start = clocker()

sleeping = 6
while sleeping:
use = usage()
for i in xrange(10000000): # do some work to forward cpu seconds
pass
sleep(0.1) # give up control
sleeping -= 0.5

end = clocker()
print "start ", start, " end ", end, " diff ", end - start

def spawn_busy_workers():
for _ in xrange(0,3):
fork(not_always_busy_worker, clock)

a = Application()
a.add_loop(Loop(spawn_busy_workers, track=True))
a.run()
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

additional_requires = []

VERSION = "3.0.11"
VERSION = "3.0.12"

setup(name="diesel",
version=VERSION,
Expand Down

0 comments on commit 1270397

Please sign in to comment.