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.

metadata_handler.py 3.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. # Copyright 2020 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. """Define the metadata stream handler."""
  16. from mindinsight.debugger.common.log import logger as log
  17. from mindinsight.debugger.common.utils import ServerStatus
  18. from mindinsight.debugger.stream_handler.base_handler import StreamHandlerBase
  19. class MetadataHandler(StreamHandlerBase):
  20. """Metadata Handler."""
  21. def __init__(self):
  22. self._state = ServerStatus.PENDING
  23. self._device_name = ""
  24. self._step = 0
  25. self._client_ip = ""
  26. self._cur_node_name = ""
  27. self._cur_full_name = ""
  28. self._backend = ""
  29. @property
  30. def device_name(self):
  31. """The property of device name."""
  32. return self._device_name
  33. @property
  34. def step(self):
  35. """The property of current step."""
  36. return self._step
  37. @property
  38. def node_name(self):
  39. """The property of current node name."""
  40. return self._cur_node_name
  41. @node_name.setter
  42. def node_name(self, node_name):
  43. """The property of current node name."""
  44. self._cur_node_name = node_name
  45. @property
  46. def full_name(self):
  47. """The property of current node name."""
  48. return self._cur_full_name
  49. @property
  50. def backend(self):
  51. """The property of current backend."""
  52. return self._backend
  53. @property
  54. def state(self):
  55. """The property of state."""
  56. return self._state.value
  57. @state.setter
  58. def state(self, value):
  59. """
  60. Set the property of state.
  61. Args:
  62. value (str): The new state.
  63. """
  64. self._state = ServerStatus(value)
  65. @property
  66. def client_ip(self):
  67. """The property of client ip."""
  68. return self._client_ip
  69. @client_ip.setter
  70. def client_ip(self, value):
  71. """
  72. Set the property of client ip.
  73. Args:
  74. value (str): The new ip.
  75. """
  76. self._client_ip = str(value)
  77. def put(self, value):
  78. """
  79. Put value into metadata cache. Called by grpc server.
  80. Args:
  81. value (MetadataProto): The Metadata proto message.
  82. """
  83. self._device_name = value.device_name.split(':')[0]
  84. self._step = value.cur_step
  85. self._cur_full_name = value.cur_node
  86. self._backend = value.backend if value.backend else "Ascend"
  87. log.debug("Put metadata into cache at the %d-th step.", self._step)
  88. def get(self, filter_condition=None):
  89. """
  90. Get updated value. Called by main server.
  91. Args:
  92. filter_condition (str): The filter property.
  93. Returns:
  94. dict, the metadata.
  95. """
  96. metadata = {}
  97. if filter_condition is None:
  98. metadata = {
  99. 'state': self.state,
  100. 'step': self.step,
  101. 'device_name': self.device_name,
  102. 'pos': '0',
  103. 'ip': self.client_ip,
  104. 'node_name': self.node_name,
  105. 'backend': self.backend
  106. }
  107. else:
  108. metadata[filter_condition] = getattr(self, filter_condition) if \
  109. hasattr(self, filter_condition) else ''
  110. return {'metadata': metadata}