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

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