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 5.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. self._enable_recheck = False
  30. self._cur_graph_name = ""
  31. # If recommendation_confirmed is true, it only means the user has answered yes or no to the question,
  32. # it does not necessarily mean that the user will use the recommended watch points.
  33. self._recommendation_confirmed = False
  34. @property
  35. def device_name(self):
  36. """The property of device name."""
  37. return self._device_name
  38. @property
  39. def step(self):
  40. """The property of current step."""
  41. return self._step
  42. @property
  43. def node_name(self):
  44. """The property of current node name."""
  45. return self._cur_node_name
  46. @node_name.setter
  47. def node_name(self, node_name):
  48. """The property of current node name."""
  49. self._cur_node_name = node_name
  50. @property
  51. def graph_name(self):
  52. """The property of current node name."""
  53. return self._cur_graph_name
  54. @graph_name.setter
  55. def graph_name(self, graph_name):
  56. """The property of current node name."""
  57. self._cur_graph_name = graph_name if graph_name else ''
  58. @property
  59. def full_name(self):
  60. """The property of current node name."""
  61. return self._cur_full_name
  62. @property
  63. def backend(self):
  64. """The property of current backend."""
  65. return self._backend
  66. @property
  67. def state(self):
  68. """The property of state."""
  69. return self._state.value
  70. @state.setter
  71. def state(self, value):
  72. """
  73. Set the property of state.
  74. Args:
  75. value (str): The new state.
  76. """
  77. self._state = ServerStatus(value)
  78. @property
  79. def client_ip(self):
  80. """The property of client ip."""
  81. return self._client_ip
  82. @client_ip.setter
  83. def client_ip(self, value):
  84. """
  85. Set the property of client ip.
  86. Args:
  87. value (str): The new ip.
  88. """
  89. self._client_ip = str(value)
  90. @property
  91. def enable_recheck(self):
  92. """The property of enable_recheck."""
  93. return self._enable_recheck and self._state == ServerStatus.WAITING and self._step > 0
  94. @enable_recheck.setter
  95. def enable_recheck(self, value):
  96. """
  97. Set the property of enable_recheck.
  98. Args:
  99. value (bool): The new ip.
  100. """
  101. self._enable_recheck = bool(value)
  102. @property
  103. def recommendation_confirmed(self):
  104. """The property of recommendation_confirmed."""
  105. return self._recommendation_confirmed
  106. @recommendation_confirmed.setter
  107. def recommendation_confirmed(self, value):
  108. """
  109. Set the property of recommendation_confirmed.
  110. Args:
  111. value (str): The new ip.
  112. """
  113. self._recommendation_confirmed = value
  114. def put(self, value):
  115. """
  116. Put value into metadata cache. Called by grpc server.
  117. Args:
  118. value (MetadataProto): The Metadata proto message.
  119. """
  120. self._device_name = value.device_name.split(':')[0]
  121. self._step = value.cur_step
  122. self._cur_full_name = value.cur_node
  123. self._backend = value.backend if value.backend else "Ascend"
  124. log.debug("Put metadata into cache at the %d-th step.", self._step)
  125. def get(self, filter_condition=None):
  126. """
  127. Get updated value. Called by main server.
  128. Args:
  129. filter_condition (Union[str, list[str]]): The filter property.
  130. Returns:
  131. dict, the metadata.
  132. """
  133. metadata = {}
  134. if filter_condition is None:
  135. metadata = {
  136. 'state': self.state,
  137. 'step': self.step,
  138. 'device_name': self.device_name,
  139. 'pos': '0',
  140. 'ip': self.client_ip,
  141. 'node_name': self.node_name,
  142. 'backend': self.backend,
  143. 'enable_recheck': self.enable_recheck,
  144. 'graph_name': self.graph_name,
  145. 'recommendation_confirmed': self._recommendation_confirmed
  146. }
  147. else:
  148. if not isinstance(filter_condition, list):
  149. filter_condition = [filter_condition]
  150. for field in filter_condition:
  151. metadata[field] = getattr(self, field) if \
  152. hasattr(self, field) else None
  153. return {'metadata': metadata}