Commit 74e86ded authored by petrcermak's avatar petrcermak Committed by Commit bot

Replace multiprocessing with threading in memory_inspector

This patch is part of an ongoing process to unbundle memory_inspector
and make it a chrome app. It replaces multiprocessing, which is
currently not supported by naclports/python, with threading.

BUG=448399

Review URL: https://codereview.chromium.org/836933004

Cr-Commit-Position: refs/heads/master@{#311515}
parent 1f9befb6
...@@ -11,14 +11,14 @@ operations, an instance of |BackgroundTask| is created here and the server ...@@ -11,14 +11,14 @@ operations, an instance of |BackgroundTask| is created here and the server
returns just its id. The client can later poll the status of the asynchronous returns just its id. The client can later poll the status of the asynchronous
task to check for its progress. task to check for its progress.
From a technical viewpoint, each background task is just a python subprocess From a technical viewpoint, each background task is just a python thread
which communicates its progress updates through a Queue. The messages enqueued which communicates its progress updates through a Queue. The messages enqueued
are tuples with the following format: (completion_ratio%, 'message string'). are tuples with the following format: (completion_ratio%, 'message string').
""" """
import datetime import datetime
import multiprocessing
import Queue import Queue
import threading
import time import time
from memory_inspector.core import backends from memory_inspector.core import backends
...@@ -40,8 +40,8 @@ def StartTracer(process, storage_path, interval, count, trace_native_heap): ...@@ -40,8 +40,8 @@ def StartTracer(process, storage_path, interval, count, trace_native_heap):
count=count, count=count,
trace_native_heap=trace_native_heap) trace_native_heap=trace_native_heap)
task.start() task.start()
_tasks[task.pid] = task _tasks[task.ident] = task
return task.pid return task.ident
def Get(task_id): def Get(task_id):
...@@ -49,9 +49,9 @@ def Get(task_id): ...@@ -49,9 +49,9 @@ def Get(task_id):
def TerminateAll(): def TerminateAll():
for proc in _tasks.itervalues(): for task in _tasks.itervalues():
if proc.is_alive(): if task.is_alive():
proc.terminate() task.terminate()
_tasks.clear() _tasks.clear()
...@@ -129,15 +129,23 @@ def TracerMain_(log, storage_path, backend_name, device_id, pid, interval, ...@@ -129,15 +129,23 @@ def TracerMain_(log, storage_path, backend_name, device_id, pid, interval,
return 0 return 0
class BackgroundTask(multiprocessing.Process): class BackgroundTask(threading.Thread):
def __init__(self, entry_point, *args, **kwargs): def __init__(self, entry_point, *args, **kwargs):
self._log_queue = multiprocessing.Queue() self._log_queue = Queue.Queue()
self._progress_log = [] # A list of tuples [(50%, 'msg1'), (100%, 'msg2')]. self._progress_log = [] # A list of tuples [(50%, 'msg1'), (100%, 'msg2')].
super(BackgroundTask, self).__init__( super(BackgroundTask, self).__init__(
target=entry_point, target=entry_point,
args=((self._log_queue,) + args), # Just propagate all args. args=((self._log_queue,) + args), # Just propagate all args.
kwargs=kwargs) kwargs=kwargs)
def run(self):
try:
super(BackgroundTask, self).run()
except Exception, e:
self._log_queue.put((
100, 'An error occurred (%s). See the log for more details.' % e))
raise
def GetProgress(self): def GetProgress(self):
""" Returns a tuple (completion_rate, message). """ """ Returns a tuple (completion_rate, message). """
while True: while True:
...@@ -145,6 +153,4 @@ class BackgroundTask(multiprocessing.Process): ...@@ -145,6 +153,4 @@ class BackgroundTask(multiprocessing.Process):
self._progress_log += [self._log_queue.get(block=False)] self._progress_log += [self._log_queue.get(block=False)]
except Queue.Empty: except Queue.Empty:
break break
if not self.is_alive() and self.exitcode != 0:
return self._progress_log + [(100, 'Failed with code %d' % self.exitcode)]
return self._progress_log return self._progress_log
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment