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.

timeline_analyser.py 4.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. """The Timeline Analyser."""
  16. import json
  17. import os
  18. from mindinsight.profiler.analyser.base_analyser import BaseAnalyser
  19. from mindinsight.profiler.common.exceptions.exceptions import ProfilerIOException
  20. from mindinsight.profiler.common.log import logger
  21. from mindinsight.profiler.common.validator.validate_path import validate_and_normalize_path
  22. from mindinsight.utils.exceptions import ParamValueError
  23. class TimelineAnalyser(BaseAnalyser):
  24. """
  25. Analyse timeline data from file.
  26. """
  27. _ascend_display_filename = 'ascend_timeline_display_{}.json'
  28. _gpu_display_filename = 'gpu_timeline_display_{}.json'
  29. _ascend_timeline_summary_filename = 'ascend_timeline_summary_{}.json'
  30. _gpu_timeline_summary_filename = 'gpu_timeline_summary_{}.json'
  31. def _load(self):
  32. """Load data according to the parsed profiling files."""
  33. def _filter(self, filter_condition):
  34. """
  35. Filter the profiling data according to the filter condition.
  36. Args:
  37. filter_condition (dict): The filter condition.
  38. """
  39. def get_display_timeline(self, device_type, scope_name_num):
  40. """
  41. Get timeline data for UI display.
  42. Returns:
  43. json, the content of timeline data.
  44. """
  45. if device_type == "ascend":
  46. display_filename = self._ascend_display_filename.format(self._device_id)
  47. elif device_type == "gpu":
  48. display_filename = self._gpu_display_filename.format(self._device_id)
  49. else:
  50. logger.info('device type should be ascend or gpu. Please check the device type.')
  51. raise ParamValueError("Invalid device_type.")
  52. file_path = os.path.join(self._profiling_dir, display_filename)
  53. file_path = validate_and_normalize_path(
  54. file_path, raise_key='Invalid timeline json path.'
  55. )
  56. timeline = []
  57. if os.path.exists(file_path):
  58. try:
  59. with open(file_path, 'r') as f_obj:
  60. timeline = json.load(f_obj)
  61. for idx, time_item in enumerate(timeline):
  62. if time_item["tid"] == "Name Scope" and \
  63. time_item["scope_level"] >= scope_name_num:
  64. timeline[idx] = None
  65. timeline = list(filter(lambda x: x, timeline))
  66. except (IOError, OSError, json.JSONDecodeError) as err:
  67. logger.error('Error occurred when read timeline display file: %s', err)
  68. raise ProfilerIOException()
  69. else:
  70. logger.info('No timeline file. Please check the output path.')
  71. return timeline
  72. def get_timeline_summary(self, device_type):
  73. """
  74. Get timeline summary information for UI display.
  75. Returns:
  76. json, the content of timeline summary information.
  77. """
  78. if device_type == "ascend":
  79. summary_filename = self._ascend_timeline_summary_filename.format(self._device_id)
  80. elif device_type == "gpu":
  81. summary_filename = self._gpu_timeline_summary_filename.format(self._device_id)
  82. else:
  83. logger.info('device type should be ascend or gpu. Please check the device type.')
  84. raise ParamValueError("Invalid device_type.")
  85. file_path = os.path.join(self._profiling_dir, summary_filename)
  86. file_path = validate_and_normalize_path(
  87. file_path, raise_key='Invalid timeline summary path.'
  88. )
  89. timeline_summary = {}
  90. if os.path.exists(file_path):
  91. try:
  92. with open(file_path, 'r') as f_obj:
  93. timeline_summary = json.load(f_obj)
  94. except (IOError, OSError, json.JSONDecodeError) as err:
  95. logger.error('Error occurred when read timeline summary file: %s', err)
  96. raise ProfilerIOException()
  97. else:
  98. logger.info('No timeline summary file. Please check the output path.')
  99. return timeline_summary