You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

svg_viewer.py 2.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. #! /usr/bin/env python3
  2. # MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  3. #
  4. # Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  5. #
  6. # Unless required by applicable law or agreed to in writing,
  7. # software distributed under the License is distributed on an
  8. # "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. import argparse
  10. import contextlib
  11. import getopt
  12. import http.server
  13. import os
  14. import runpy
  15. import sys
  16. import tempfile
  17. from megengine.logger import get_logger
  18. def main():
  19. parser = argparse.ArgumentParser(
  20. prog="megengine.tools.svg_viewer",
  21. description="View SVG Graph produced bt megengine profiler",
  22. )
  23. parser.add_argument("-p", "--port", type=int, default=8000, help="server port")
  24. parser.add_argument(
  25. "-a", "--address", type=str, default="localhost", help="server address"
  26. )
  27. args = parser.parse_args()
  28. address = args.address
  29. port = args.port
  30. src_filename = "svg_viewer.html"
  31. dst_filename = "index.html"
  32. src_path = os.path.join(os.path.dirname(__file__), src_filename)
  33. url = "http://{}:{}/{}".format("localhost", port, dst_filename)
  34. ssh_fwd_cmd = "ssh -L {}:localhost:{} <remote ip>".format(port, port)
  35. with tempfile.TemporaryDirectory() as serve_dir:
  36. dst_path = os.path.join(serve_dir, dst_filename)
  37. os.symlink(src_path, dst_path)
  38. os.chdir(serve_dir)
  39. get_logger().info("cd to serve directory: {}, starting".format(serve_dir))
  40. server = http.server.HTTPServer(
  41. (address, port), http.server.SimpleHTTPRequestHandler
  42. )
  43. get_logger().info(
  44. "server started, please visit '{}' to watch profiling result".format(url)
  45. )
  46. get_logger().info(
  47. "if you are in remote environment, use '{}' to forward port to local".format(
  48. ssh_fwd_cmd
  49. )
  50. )
  51. try:
  52. server.serve_forever()
  53. except KeyboardInterrupt:
  54. get_logger().info("server exiting")
  55. if __name__ == "__main__":
  56. main()