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.

hwts_log_parser.py 5.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  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 parser for hwts log file."""
  16. import os
  17. import struct
  18. from mindspore.profiler.common.util import fwrite_format, get_file_join_name
  19. from mindspore import log as logger
  20. from mindspore.profiler.common.validator.validate_path import \
  21. validate_and_normalize_path
  22. class HWTSLogParser:
  23. """
  24. The Parser for hwts log files.
  25. Args:
  26. input_path (str): The profiling job path. Such as: '/var/log/npu/profiling/JOBAIFGJEJFEDCBAEADIFJAAAAAAAAAA".
  27. output_filename (str): The output data path and name. Such as: './output_format_data_hwts_0.txt'.
  28. """
  29. _source_file_target_old = 'hwts.log.data.45.dev.profiler_default_tag'
  30. _source_file_target = 'hwts.data'
  31. _dst_file_title = 'title:45 HWTS data'
  32. _dst_file_column_title = 'Type cnt Core_ID Block_ID Task_ID Cycle_counter Stream_ID'
  33. def __init__(self, input_path, output_filename):
  34. self._input_path = input_path
  35. self._output_filename = output_filename
  36. self._source_flie_name = self._get_source_file()
  37. def _get_source_file(self):
  38. """Get hwts log file name, which was created by ada service."""
  39. file_name = get_file_join_name(self._input_path, self._source_file_target)
  40. if not file_name:
  41. file_name = get_file_join_name(self._input_path, self._source_file_target_old)
  42. if not file_name:
  43. data_path = os.path.join(self._input_path, "data")
  44. file_name = get_file_join_name(data_path, self._source_file_target)
  45. if not file_name:
  46. file_name = get_file_join_name(data_path, self._source_file_target_old)
  47. if not file_name:
  48. msg = "Fail to find hwts log file, under profiling directory"
  49. raise RuntimeError(msg)
  50. return file_name
  51. def execute(self):
  52. """
  53. Execute the parser, get result data, and write it to the output file.
  54. Returns:
  55. bool, whether succeed to analyse hwts log.
  56. """
  57. content_format = ['QIIIIIIIIIIII', 'QIIQIIIIIIII', 'IIIIQIIIIIIII']
  58. log_type = ['Start of task', 'End of task', 'Start of block', 'End of block', 'Block PMU']
  59. result_data = ""
  60. self._source_flie_name = validate_and_normalize_path(self._source_flie_name)
  61. with open(self._source_flie_name, 'rb') as hwts_data:
  62. while True:
  63. # read 64 bit data
  64. line = hwts_data.read(64)
  65. if line:
  66. if not line.strip():
  67. continue
  68. else:
  69. break
  70. byte_first_four = struct.unpack('BBHHH', line[0:8])
  71. # byte_first[0:4] refers to count. byte_first[4] refers to is_warn_res0_0v.
  72. # byte_first[5:8] refers to the type of ms.
  73. byte_first = bin(byte_first_four[0]).replace('0b', '').zfill(8)
  74. ms_type = byte_first[-3:]
  75. is_warn_res0_ov = byte_first[4]
  76. cnt = int(byte_first[0:4], 2)
  77. core_id = byte_first_four[1]
  78. blk_id, task_id = byte_first_four[3], byte_first_four[4]
  79. if ms_type in ['000', '001', '010']: # log type 0,1,2
  80. result = struct.unpack(content_format[0], line[8:])
  81. syscnt = result[0]
  82. stream_id = result[1]
  83. elif ms_type == '011': # log type 3
  84. result = struct.unpack(content_format[1], line[8:])
  85. syscnt = result[0]
  86. stream_id = result[1]
  87. elif ms_type == '100': # log type 4
  88. result = struct.unpack(content_format[2], line[8:])
  89. stream_id = result[2]
  90. if is_warn_res0_ov == '0':
  91. syscnt = result[4]
  92. else:
  93. syscnt = None
  94. else:
  95. logger.info("Profiling: invalid hwts log record type %s", ms_type)
  96. continue
  97. if int(task_id) < 25000:
  98. task_id = str(stream_id) + "_" + str(task_id)
  99. result_data += ("%-14s %-4s %-8s %-9s %-8s %-15s %s\n" %(log_type[int(ms_type, 2)], cnt, core_id,
  100. blk_id, task_id, syscnt, stream_id))
  101. fwrite_format(self._output_filename, data_source=self._dst_file_title, is_start=True)
  102. fwrite_format(self._output_filename, data_source=self._dst_file_column_title)
  103. fwrite_format(self._output_filename, data_source=result_data)
  104. return True