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.

aicpu_data_parser.py 4.4 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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. """
  16. The parser for AI CPU preprocess data.
  17. """
  18. import os
  19. from tabulate import tabulate
  20. from mindinsight.profiler.common._utils import fwrite_format, get_file_join_name
  21. from mindinsight.profiler.common.log import logger
  22. _source_file_target = 'DATA_PREPROCESS.dev.AICPU'
  23. _dst_file_title = 'title:DATA_PREPROCESS AICPU'
  24. _dst_file_column_title = ['serial_number', 'node_name', 'total_time(us)', 'dispatch_time(us)',
  25. 'RunV2_start', 'compute_start', 'memcpy_start', 'memcpy_end', 'RunV2_end']
  26. class DataPreProcessParser:
  27. """
  28. The Parser for AI CPU preprocess data.
  29. Args:
  30. input_path(str): The profiling job path.
  31. output_filename(str): The output data path and name.
  32. """
  33. def __init__(self, input_path, output_filename):
  34. self._input_path = input_path
  35. self._output_filename = output_filename
  36. self._source_file_name = self._get_source_file()
  37. def _get_source_file(self):
  38. """Get log file name, which was created by ada service."""
  39. return get_file_join_name(self._input_path, _source_file_target)
  40. def execute(self):
  41. """Execute the parser, get result data, and write it to the output file."""
  42. if not os.path.exists(self._source_file_name):
  43. logger.info("Did not find the aicpu profiling source file")
  44. return
  45. with open(self._source_file_name, 'rb') as ai_cpu_data:
  46. ai_cpu_str = str(ai_cpu_data.read().replace(b'\n\x00', b' ___ ')
  47. .replace(b'\x00', b' ___ '))[2:-1]
  48. ai_cpu_lines = ai_cpu_str.split(" ___ ")
  49. node_list = list()
  50. ai_cpu_total_time_summary = 0
  51. # node serial number
  52. serial_number = 1
  53. for i in range(len(ai_cpu_lines)-1):
  54. node_line = ai_cpu_lines[i]
  55. thread_line = ai_cpu_lines[i+1]
  56. if "Node" in node_line and "Thread" in thread_line:
  57. # get the node data from node_line
  58. node_name = node_line.split(',')[0].split(':')[-1]
  59. run_v2_start = node_line.split(',')[1].split(':')[-1]
  60. compute_start = node_line.split(',')[2].split(':')[-1]
  61. mercy_start = node_line.split(',')[3].split(':')[-1]
  62. mercy_end = node_line.split(',')[4].split(':')[-1]
  63. run_v2_end = node_line.split(',')[5].split(':')[-1]
  64. # get total_time and dispatch_time from thread line
  65. total_time = thread_line.split(',')[-1].split('=')[-1].split()[0]
  66. dispatch_time = thread_line.split(',')[-2].split('=')[-1].split()[0]
  67. node_data = [serial_number, node_name, total_time, dispatch_time, run_v2_start, compute_start,
  68. mercy_start, mercy_end, run_v2_end]
  69. node_list.append(node_data)
  70. # calculate the total time
  71. ai_cpu_total_time_summary += int(total_time)
  72. # increase node serial number
  73. serial_number += 1
  74. elif "Node" in node_line and "Thread" not in thread_line:
  75. node_name = node_line.split(',')[0].split(':')[-1]
  76. logger.warning("The node:%s cannot find thread data", node_name)
  77. node_list.append(["AI CPU Total Time(us):", ai_cpu_total_time_summary])
  78. if node_list:
  79. fwrite_format(self._output_filename, data_source=_dst_file_title, is_print=True,
  80. is_start=True)
  81. fwrite_format(self._output_filename,
  82. data_source=tabulate(node_list, _dst_file_column_title,
  83. tablefmt='simple'),
  84. is_start=True, is_print=True)