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.

stop.py 5.3 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. # Copyright 2019 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """Stop mindinsight service."""
  16. import os
  17. import sys
  18. import argparse
  19. import signal
  20. import getpass
  21. import psutil
  22. from mindinsight.conf import settings
  23. from mindinsight.utils.command import BaseCommand
  24. from mindinsight.utils.hook import HookUtils
  25. class PortAction(argparse.Action):
  26. """Port action class definition."""
  27. MIN_PORT = 1
  28. MAX_PORT = 65535
  29. def __call__(self, parser, namespace, values, option_string=None):
  30. """
  31. Inherited __call__ method from argparse.Action.
  32. Args:
  33. parser (ArgumentParser): Passed-in argument parser.
  34. namespace (Namespace): Namespace object to hold arguments.
  35. values (object): Argument values with type depending on argument definition.
  36. option_string (str): Optional string for specific argument name. Default: None.
  37. """
  38. port = values
  39. if not self.MIN_PORT <= port <= self.MAX_PORT:
  40. parser.error(f'{option_string} should be chosen from {self.MIN_PORT} to {self.MAX_PORT}')
  41. setattr(namespace, self.dest, port)
  42. class Command(BaseCommand):
  43. """Stop command."""
  44. name = 'stop'
  45. description = 'stop mindinsight service'
  46. cmd_regex = 'mindinsight.backend.application:APP'
  47. access_log_path = os.path.join('gunicorn', 'access.log')
  48. def add_arguments(self, parser):
  49. """
  50. Add arguments to parser.
  51. Args:
  52. parser (ArgumentParser): Specify parser to which arguments are added.
  53. """
  54. parser.add_argument(
  55. '--port',
  56. type=int,
  57. action=PortAction,
  58. help="""
  59. Custom port ranging from %s to %s. Default value is %s
  60. """ % (PortAction.MIN_PORT, PortAction.MAX_PORT, settings.PORT))
  61. def update_settings(self, args):
  62. """
  63. Update settings.
  64. Args:
  65. args (Namespace): parsed arguments to hold customized parameters.
  66. """
  67. if args.port is None:
  68. args.port = settings.PORT
  69. pid, workspace = self.get_process(args.port)
  70. settings.config_workspace(workspace)
  71. setattr(args, 'pid', pid)
  72. def run(self, args):
  73. """
  74. Run to stop.
  75. Args:
  76. args (Namespace): Parsed arguments to hold customized parameters.
  77. """
  78. port, pid = args.port, args.pid
  79. if not pid:
  80. msg = f'No mindinsight service found for port {port}'
  81. self.console.error(msg)
  82. sys.exit(1)
  83. self.logfile.info('Stop mindinsight with port %s and pid %s.', port, pid)
  84. process = psutil.Process(pid)
  85. child_pids = [child.pid for child in process.children()]
  86. # kill gunicorn master process
  87. try:
  88. os.kill(pid, signal.SIGKILL)
  89. except PermissionError:
  90. self.console.info('kill pid %s failed due to permission error', pid)
  91. sys.exit(1)
  92. # cleanup gunicorn worker processes
  93. for child_pid in child_pids:
  94. try:
  95. os.kill(child_pid, signal.SIGKILL)
  96. except ProcessLookupError:
  97. pass
  98. for hook in HookUtils.instance().hooks():
  99. hook.on_shutdown(self.logfile)
  100. self.console.info('Stop mindinsight service successfully')
  101. def get_process(self, port):
  102. """
  103. Get mindinsight process
  104. Args:
  105. port (int): Specified port for mindinsight process.
  106. Returns:
  107. tuple, return mindinsight process pid and workspace.
  108. """
  109. pid, workspace = 0, settings.WORKSPACE
  110. user = getpass.getuser()
  111. connections = psutil.net_connections()
  112. for connection in connections:
  113. if connection.status != 'LISTEN':
  114. continue
  115. if connection.laddr.port != port:
  116. continue
  117. try:
  118. process = psutil.Process(connection.pid)
  119. except psutil.NoSuchProcess:
  120. continue
  121. cmds = process.cmdline()
  122. if ' '.join(cmds).find(self.cmd_regex) == -1:
  123. continue
  124. if user != process.username():
  125. continue
  126. pid = process.pid if process.ppid() == 1 else process.ppid()
  127. for open_file in process.open_files():
  128. if open_file.path.endswith(self.access_log_path):
  129. log_base_dir = open_file.path[:-len(self.access_log_path)]
  130. workspace = os.path.realpath(os.path.join(log_base_dir, os.pardir))
  131. break
  132. break
  133. return pid, workspace

MindInsight为MindSpore提供了简单易用的调优调试能力。在训练过程中,可以将标量、张量、图像、计算图、模型超参、训练耗时等数据记录到文件中,通过MindInsight可视化页面进行查看及分析。