Skip to content

Commit

Permalink
[lit] Factor out a results consumer interface for test execution.
Browse files Browse the repository at this point in the history
 - Also, change TestProvider interface to operate on test indices.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@189552 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
ddunbar committed Aug 29, 2013
1 parent 8c59003 commit ec8e059
Showing 1 changed file with 44 additions and 20 deletions.
64 changes: 44 additions & 20 deletions utils/lit/lit/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ def handleUpdate(self, test):
class TestProvider:
def __init__(self, tests, maxTime):
self.maxTime = maxTime
self.iter = iter(tests)
self.iter = iter(range(len(tests)))
self.lock = threading.Lock()
self.startTime = time.time()
self.canceled = False
Expand Down Expand Up @@ -101,48 +101,70 @@ def get(self):
self.lock.release()
return item

class Tester(threading.Thread):
def __init__(self, run_instance, provider, display):
threading.Thread.__init__(self)
class Tester(object):
def __init__(self, run_instance, provider, consumer):
self.run_instance = run_instance
self.provider = provider
self.display = display
self.consumer = consumer

def run(self):
while 1:
item = self.provider.get()
if item is None:
break
self.runTest(item)
self.consumer.taskFinished()

def runTest(self, test):
def runTest(self, test_index):
test = self.run_instance.tests[test_index]
try:
self.run_instance.execute_test(test)
except KeyboardInterrupt:
# This is a sad hack. Unfortunately subprocess goes
# bonkers with ctrl-c and we start forking merrily.
print('\nCtrl-C detected, goodbye.')
os.kill(0,9)
self.consumer.update(test_index, test)

class ThreadResultsConsumer(object):
def __init__(self, display):
self.display = display

def update(self, test_index, test):
self.display.update(test)

def taskFinished(self):
pass

def handleResults(self):
pass

def run_one_tester(run, provider, display):
tester = Tester(run, provider, display)
tester.run()

def runTests(numThreads, run, provider, display):
# If only using one testing thread, don't use threads at all; this lets us
consumer = ThreadResultsConsumer(display)

# If only using one testing thread, don't use tasks at all; this lets us
# profile, among other things.
if numThreads == 1:
t = Tester(run, provider, display)
t.run()
run_one_tester(run, provider, consumer)
return

# Otherwise spin up the testing threads and wait for them to finish.
testers = [Tester(run, provider, display)
for i in range(numThreads)]
for t in testers:
# Start all of the tasks.
tasks = [threading.Thread(target=run_one_tester,
args=(run, provider, consumer))
for i in range(numThreads)]
for t in tasks:
t.start()
try:
for t in testers:
t.join()
except KeyboardInterrupt:
sys.exit(2)

# Allow the consumer to handle results, if necessary.
consumer.handleResults()

# Wait for all the tasks to complete.
for t in tasks:
t.join()

def main(builtinParameters = {}):
# Bump the GIL check interval, its more important to get any one thread to a
Expand Down Expand Up @@ -362,8 +384,10 @@ def console_ctrl_handler(type):
provider.cancel()
return True
win32api.SetConsoleCtrlHandler(console_ctrl_handler, True)

runTests(opts.numThreads, run, provider, display)
try:
runTests(opts.numThreads, run, provider, display)
except KeyboardInterrupt:
sys.exit(2)
display.finish()

if not opts.quiet:
Expand Down

0 comments on commit ec8e059

Please sign in to comment.