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

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