Esempio n. 1
0
    def test_collector(self):
        start = time.time()

        def a(end):
            while time.time() < end:
                pass
            c(time.time() + 0.1)

        def b(end):
            while time.time() < end:
                pass
            c(time.time() + 0.1)

        def c(end):
            while time.time() < end:
                pass

        profiler = Profiler("/tmp")
        profiler.start(interval=0.01)
        a(time.time() + 0.1)
        b(time.time() + 0.2)
        c(time.time() + 0.3)
        end = time.time()
        profiler.stop("profiler_test.plop")
        elapsed = end - start
        self.assertTrue(0.8 < elapsed < 0.9, elapsed)

        with open("/tmp/profiler_test.plop") as f:
            results = f.read()
        counts = self.filter_stacks(results)

        expected = {
            ("a", "test_collector"): 10,
            ("c", "a", "test_collector"): 10,
            ("b", "test_collector"): 20,
            ("c", "b", "test_collector"): 10,
            ("c", "test_collector"): 30,
        }
        self.check_counts(counts, expected)
Esempio n. 2
0
def main():
    print("Loading malcolm...")
    from malcolm.profiler import Profiler

    args = parse_args()

    # Make some log config using command line args or defaults
    log_config = make_logging_config(args)

    # Start it off, and tell it to stop when we quit
    listener = make_async_logging(log_config)
    listener.start()
    atexit.register(listener.stop)

    # Setup profiler dir
    profiler = Profiler()
    # profiler.start()

    # If using p4p then set cothread to use the right ca libs before it is
    try:
        import epicscorelibs.path.cothread  # noqa
    except ImportError:
        pass

    # Import the Malcolm process
    q = queue.Queue()
    t = threading.Thread(target=try_prepare_locals, args=(q, args))
    t.start()
    process = q.get(timeout=65)
    if isinstance(process, Exception):
        # Startup failed, exit now
        sys.exit(1)

    # Now its safe to import Malcolm and cothread
    import cothread

    from malcolm.core import Context
    from malcolm.modules.builtin.blocks import proxy_block

    # Make a user context
    class UserContext(Context):
        def post(self, path, params=None, timeout=None, event_timeout=None):
            try:
                return super().post(path, params, timeout, event_timeout)
            except KeyboardInterrupt:
                self.post([path[0], "abort"])

        def _make_proxy(self, comms, mri):
            self._process.add_controller(proxy_block(comms=comms, mri=mri)[-1])

        def make_proxy(self, comms, mri):
            # Need to do this in cothread's thread
            cothread.CallbackResult(self._make_proxy, comms, mri)

        def block_view(self, mri):
            return cothread.CallbackResult(super().block_view, mri)

        def make_view(self, controller, data, child_name):
            return cothread.CallbackResult(super().make_view, controller, data,
                                           child_name)

        def handle_request(self, controller, request):
            cothread.CallbackResult(super().handle_request, controller,
                                    request)

    self = UserContext(process)

    header = f"""Welcome to iMalcolm.

self.mri_list:
    {self.mri_list}

# To create a view of an existing Block
block = self.block_view("<mri>")

# To create a proxy of a Block in another Malcolm
self.make_proxy("<client_comms_mri>", "<mri>")
block = self.block_view("<mri>")

# To view state of Blocks in a GUI
!firefox localhost:8008"""

    try:
        import IPython
    except ImportError:
        import code

        code.interact(header, local=locals())
    else:
        IPython.embed(header=header)
    if profiler.running and not profiler.stopping:
        profiler.stop()

    cothread.CallbackResult(process.stop, timeout=0.1)
    cothread.CallbackResult(cothread.Quit)
Esempio n. 3
0
def main():
    from malcolm.compat import queue
    from malcolm.profiler import Profiler

    args = parse_args()

    # Make some log config, either fron command line or
    log_config = make_logging_config(args)

    # Start it off, and tell it to stop when we quit
    listener = make_async_logging(log_config)
    listener.start()
    atexit.register(listener.stop)

    # Setup profiler dir
    profiler = Profiler()
    # profiler.start()

    # If using p4p then set cothread to use the right ca libs before it is
    try:
        import epicscorelibs.path.cothread
    except ImportError:
        pass

    # Import the Malcolm process
    q = queue.Queue()
    t = threading.Thread(target=try_prepare_locals, args=(q, args))
    t.start()
    process = q.get(timeout=65)
    if isinstance(process, Exception):
        # Startup failed, exit now
        sys.exit(1)

    # Now its safe to import Malcolm and cothread
    import cothread

    from malcolm.core import Context
    from malcolm.modules.builtin.blocks import proxy_block

    # Make a user context
    class UserContext(Context):
        def post(self, path, params=None, timeout=None, event_timeout=None):
            try:
                return super(UserContext, self).post(path, params, timeout,
                                                     event_timeout)
            except KeyboardInterrupt:
                self.post([path[0], "abort"])

        def _make_proxy(self, comms, mri):
            self._process.add_controller(proxy_block(comms=comms, mri=mri)[-1])

        def make_proxy(self, comms, mri):
            # Need to do this in cothread's thread
            cothread.CallbackResult(self._make_proxy, comms, mri)

        def block_view(self, mri):
            return cothread.CallbackResult(
                super(UserContext, self).block_view, mri)

        def make_view(self, controller, data, child_name):
            return cothread.CallbackResult(
                super(UserContext, self).make_view, controller, data,
                child_name)

        def handle_request(self, controller, request):
            cothread.CallbackResult(
                super(UserContext, self).handle_request, controller, request)

    self = UserContext(process)

    header = """Welcome to iMalcolm.

self.mri_list:
    %s

Try:
hello = self.block_view("HELLO")
hello.greet("me")

or

gui(self.block_view("COUNTER"))

or

self.make_proxy("localhost:8008", "HELLO")
self.block_view("HELLO").greet("me")
""" % (self.mri_list, )

    try:
        import IPython
    except ImportError:
        import code
        code.interact(header, local=locals())
    else:
        IPython.embed(header=header)
    if profiler.running and not profiler.stopping:
        profiler.stop()

    cothread.CallbackResult(process.stop)
    cothread.CallbackResult(cothread.Quit)

class TemplatedGuiPart(web.parts.GuiServerPart):
    # Override the things that returns the GUI html to use
    # a templated version
    GuiHandler = TemplateHandler


# Check the options
if os.path.exists(args.optionsdir):
    options = sorted(os.listdir(args.optionsdir))
else:
    options = []

# Make a profiler
profiler = Profiler()

# Make the top level process
process = Process("Process")

# Add the websocket server
controller = web.controllers.HTTPServerComms(port=args.wsport, mri="WS")
controller.add_part(web.parts.WebsocketServerPart(
    subnet_validation="no-subnet-validation" not in options
))
controller.add_part(TemplatedGuiPart())
process.add_controller(controller)

# Add the PandABox
controller = pandablocks.controllers.PandAManagerController(
    config_dir=args.configdir, hostname=args.hostname,