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.

util.py 9.3 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  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. Profiler util.
  17. This module provides the utils.
  18. """
  19. import os
  20. # one sys count takes 10 ns, 1 ms has 100000 system count
  21. import re
  22. PER_MS_SYSCNT = 100000
  23. def to_int(param, param_name):
  24. """
  25. Transfer param to int type.
  26. Args:
  27. param (Any): A param transformed.
  28. param_name (str): Param name.
  29. Returns:
  30. int, value after transformed.
  31. """
  32. try:
  33. param = int(param)
  34. except ValueError:
  35. raise TypeError('Must be Integer: ' + param_name)
  36. return param
  37. def fwrite_format(output_data_path, data_source=None, is_print=False, is_start=False):
  38. """
  39. Write data to the output file.
  40. Args:
  41. output_data_path (str): The output file path of the data.
  42. data_source (str, list, tuple): The data to write.
  43. is_print (bool): whether to print the data to stdout.
  44. is_start (bool): Whether is the first line of the output file, will remove the old file if True."
  45. """
  46. if is_start is True and os.path.exists(output_data_path):
  47. os.remove(output_data_path)
  48. if isinstance(data_source, str) and data_source.startswith("title:"):
  49. title_label = '=' * 20
  50. data_source = title_label + data_source[6:] + title_label
  51. with open(output_data_path, 'a+') as f:
  52. if isinstance(data_source, (list, tuple)):
  53. for raw_data in data_source:
  54. if isinstance(raw_data, (list, tuple)):
  55. raw_data = map(str, raw_data)
  56. raw_data = " ".join(raw_data)
  57. f.write(raw_data)
  58. f.write("\n")
  59. else:
  60. f.write(data_source)
  61. f.write("\n")
  62. if is_print:
  63. if isinstance(data_source, (list, tuple)):
  64. for raw_data in data_source:
  65. if isinstance(raw_data, (list, tuple)):
  66. raw_data = map(str, raw_data)
  67. raw_data = " ".join(raw_data)
  68. print(raw_data)
  69. else:
  70. print(data_source)
  71. def get_log_slice_id(file_name):
  72. pattern = re.compile(r'(?<=slice_)\d+')
  73. slice_list = pattern.findall(file_name)
  74. index = re.findall(r'\d+', slice_list[0])
  75. return int(index[0])
  76. def get_file_join_name(input_path, file_name):
  77. """
  78. Search files under the special path, and will join all the files to one file.
  79. Args:
  80. input_path (str): The source path, will search files under it.
  81. file_name (str): The target of the filename, such as 'hwts.log.data.45.dev'.
  82. Returns:
  83. str, the join file name.
  84. """
  85. name_list = []
  86. file_join_name = ''
  87. input_path = os.path.realpath(input_path)
  88. if os.path.exists(input_path):
  89. files = os.listdir(input_path)
  90. for f in files:
  91. if file_name in f and not f.endswith('.done') and not f.endswith('.join') \
  92. and not f.endswith('.zip'):
  93. name_list.append(f)
  94. # resort name_list
  95. name_list.sort(key=get_log_slice_id)
  96. if len(name_list) == 1:
  97. file_join_name = os.path.join(input_path, name_list[0])
  98. elif len(name_list) > 1:
  99. file_join_name = os.path.join(input_path, '%s.join' % file_name)
  100. if os.path.exists(file_join_name):
  101. os.remove(file_join_name)
  102. with open(file_join_name, 'ab') as bin_data:
  103. for i in name_list:
  104. file = input_path + os.sep + i
  105. with open(file, 'rb') as txt:
  106. bin_data.write(txt.read())
  107. return file_join_name
  108. def get_file_names(input_path, file_name):
  109. """
  110. Search files under the special path.
  111. Args:
  112. input_path (str): The source path, will search files under it.
  113. file_name (str): The target of the filename, such as 'host_start_log'.
  114. Returns:
  115. list, file name list.
  116. """
  117. input_path = os.path.realpath(input_path)
  118. name_list = []
  119. if os.path.exists(input_path):
  120. files = os.listdir(input_path)
  121. for f in files:
  122. if file_name in f and not f.endswith('.done') \
  123. and not f.endswith('.zip'):
  124. name_list.append(f)
  125. break
  126. return name_list
  127. def analyse_device_list_from_profiler_dir(profiler_dir):
  128. """
  129. Analyse device list from profiler dir.
  130. Args:
  131. profiler_dir (str): The profiler data dir.
  132. Returns:
  133. list, the device_id list.
  134. """
  135. profiler_file_prefix = ["timeline_display", "output_op_compute_time"]
  136. device_id_list = set()
  137. for _, _, filenames in os.walk(profiler_dir):
  138. for filename in filenames:
  139. if filename.startswith("step_trace_raw"):
  140. items = filename.split("_")
  141. device_num = ""
  142. if len(items) > 3:
  143. device_num = items[3]
  144. else:
  145. items = filename.split("_")
  146. device_num = items[-1].split(".")[0] if items[-1].split(".") else ""
  147. if device_num.isdigit() and '_'.join(items[:-1]) in profiler_file_prefix:
  148. device_id_list.add(device_num)
  149. return sorted(list(device_id_list))
  150. def query_latest_trace_time_file(profiler_dir, device_id=0):
  151. """
  152. Query the latest trace time file.
  153. Args:
  154. profiler_dir (str): The profiler directory.
  155. device_id (int): The id of device.
  156. Returns:
  157. str, the latest trace time file path.
  158. """
  159. files = os.listdir(profiler_dir)
  160. target_file = f'step_trace_raw_{device_id}_detail_time.csv'
  161. try:
  162. latest_file = max(
  163. filter(
  164. lambda file: file == target_file,
  165. files
  166. ),
  167. key=lambda file: os.stat(os.path.join(profiler_dir, file)).st_mtime
  168. )
  169. except ValueError:
  170. return None
  171. return os.path.join(profiler_dir, latest_file)
  172. def query_step_trace_file(profiler_dir):
  173. """
  174. Query for all step trace file.
  175. Args:
  176. profiler_dir (str): The directory that contains all step trace files.
  177. Returns:
  178. str, the file path of step trace time.
  179. """
  180. files = os.listdir(profiler_dir)
  181. training_trace_file = list(
  182. filter(
  183. lambda file: file.startswith('training_trace') and not file.endswith('.done'),
  184. files
  185. )
  186. )
  187. if training_trace_file:
  188. return os.path.join(profiler_dir, training_trace_file[0])
  189. return None
  190. def get_summary_for_step_trace(average_info, header):
  191. """The property of summary info."""
  192. if not average_info or not header:
  193. return {}
  194. total_time = get_field_value(average_info, 'total', header)
  195. iteration_interval = get_field_value(average_info, 'iteration_interval',
  196. header)
  197. fp_and_bp = get_field_value(average_info, 'fp_and_bp', header)
  198. tail = get_field_value(average_info, 'tail', header)
  199. summary = {
  200. 'total_time': total_time,
  201. 'iteration_interval': iteration_interval,
  202. 'iteration_interval_percent': calculate_percent(iteration_interval, total_time),
  203. 'fp_and_bp': fp_and_bp,
  204. 'fp_and_bp_percent': calculate_percent(fp_and_bp, total_time),
  205. 'tail': tail,
  206. 'tail_percent': calculate_percent(tail, total_time)
  207. }
  208. return summary
  209. def calculate_percent(partial, total):
  210. """Calculate percent value."""
  211. if total:
  212. percent = round(partial / total * 100, 2)
  213. else:
  214. percent = 0
  215. return f'{percent}%'
  216. def to_millisecond(sys_count, limit=4):
  217. """Translate system count to millisecond."""
  218. return round(sys_count / PER_MS_SYSCNT, limit)
  219. def get_field_value(row_info, field_name, header, time_type='realtime'):
  220. """
  221. Extract basic info through row_info.
  222. Args:
  223. row_info (list): The list of data info in one row.
  224. field_name (str): The name in header.
  225. header (list[str]): The list of field names.
  226. time_type (str): The type of value, `realtime` or `systime`. Default: `realtime`.
  227. Returns:
  228. dict, step trace info in dict format.
  229. """
  230. field_index = header.index(field_name)
  231. value = row_info[field_index]
  232. value = to_int(value, field_name)
  233. if time_type == 'realtime':
  234. value = to_millisecond(value)
  235. return value
  236. def get_options(options):
  237. if options is None:
  238. options = {}
  239. return options