starpu_trace_state_stats.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362
  1. #!/usr/bin/python2.7
  2. ##
  3. # StarPU --- Runtime system for heterogeneous multicore architectures.
  4. #
  5. # Copyright (C) 2016 INRIA
  6. #
  7. # StarPU is free software; you can redistribute it and/or modify
  8. # it under the terms of the GNU Lesser General Public License as published by
  9. # the Free Software Foundation; either version 2.1 of the License, or (at
  10. # your option) any later version.
  11. #
  12. # StarPU is distributed in the hope that it will be useful, but
  13. # WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  15. #
  16. # See the GNU Lesser General Public License in COPYING.LGPL for more details.
  17. ##
  18. ##
  19. # This script parses the generated trace.rec file and reports statistics about
  20. # the number of different events/tasks and their durations. The report is
  21. # similar to the starpu_paje_state_stats.in script, except that this one
  22. # doesn't need R and pj_dump (from the pajeng repository), and it is also much
  23. # faster.
  24. ##
  25. import getopt
  26. import os
  27. import sys
  28. class Event():
  29. def __init__(self, type, name, category, start_time):
  30. self._type = type
  31. self._name = name
  32. self._category = category
  33. self._start_time = start_time
  34. class EventStats():
  35. def __init__(self, name, duration_time, category, count = 1):
  36. self._name = name
  37. self._duration_time = duration_time
  38. self._category = category
  39. self._count = count
  40. def aggregate(self, duration_time):
  41. self._duration_time += duration_time
  42. self._count += 1
  43. def show(self):
  44. if not self._name == None and not self._category == None:
  45. print "\"" + self._name + "\"," + str(self._count) + ",\"" + self._category + "\"," + str(round(self._duration_time, 6))
  46. class Worker():
  47. def __init__(self, id):
  48. self._id = id
  49. self._events = []
  50. self._stats = []
  51. self._stack = []
  52. def get_event_stats(self, name):
  53. for stat in self._stats:
  54. if stat._name == name:
  55. return stat
  56. return None
  57. def add_event(self, type, name, category, start_time):
  58. self._events.append(Event(type, name, category, start_time))
  59. def calc_stats(self, start_profiling_times, stop_profiling_times):
  60. num_events = len(self._events) - 1
  61. for i in xrange(0, num_events):
  62. curr_event = self._events[i]
  63. next_event = self._events[i+1]
  64. is_allowed = not len(start_profiling_times)
  65. # Check if the event is inbetween start/stop profiling events
  66. for t in range(len(start_profiling_times)):
  67. if (curr_event._start_time > start_profiling_times[t] and
  68. curr_event._start_time < stop_profiling_times[t]):
  69. is_allowed = True
  70. break
  71. if not is_allowed:
  72. continue
  73. if next_event._type == "PushState":
  74. self._stack.append(next_event)
  75. for j in xrange(i+1, num_events):
  76. next_event = self._events[j]
  77. if next_event._type == "SetState":
  78. break
  79. elif next_event._type == "PopState":
  80. if not len(self._stack) == 0:
  81. curr_event = self._stack.pop()
  82. # Compute duration with the next event.
  83. a = curr_event._start_time
  84. b = next_event._start_time
  85. found = False
  86. for j in xrange(len(self._stats)):
  87. if self._stats[j]._name == curr_event._name:
  88. self._stats[j].aggregate(b - a)
  89. found = True
  90. break
  91. if not found == True:
  92. self._stats.append(EventStats(curr_event._name, b - a, curr_event._category))
  93. def read_blocks(input_file):
  94. empty_lines = 0
  95. first_line = 1
  96. blocks = []
  97. for line in open(input_file):
  98. if first_line:
  99. blocks.append([])
  100. blocks[-1].append(line)
  101. first_line = 0
  102. # Check for empty lines
  103. if not line or line[0] == '\n':
  104. # If 1st one: new block
  105. if empty_lines == 0:
  106. blocks.append([])
  107. empty_lines += 1
  108. else:
  109. # Non empty line: add line in current(last) block
  110. empty_lines = 0
  111. blocks[-1].append(line)
  112. return blocks
  113. def read_field(field, index):
  114. return field[index+1:-1]
  115. def insert_worker_event(workers, prog_events, block):
  116. worker_id = -1
  117. name = None
  118. start_time = 0.0
  119. category = None
  120. for line in block:
  121. if line[:2] == "E:": # EventType
  122. event_type = read_field(line, 2)
  123. elif line[:2] == "C:": # Category
  124. category = read_field(line, 2)
  125. elif line[:2] == "W:": # WorkerId
  126. worker_id = int(read_field(line, 2))
  127. elif line[:2] == "N:": # Name
  128. name = read_field(line, 2)
  129. elif line[:2] == "S:": # StartTime
  130. start_time = float(read_field(line, 2))
  131. # Program events don't belong to workers, they are globals.
  132. if category == "Program":
  133. prog_events.append(Event(event_type, name, category, start_time))
  134. return
  135. for worker in workers:
  136. if worker._id == worker_id:
  137. worker.add_event(event_type, name, category, start_time)
  138. return
  139. worker = Worker(worker_id)
  140. worker.add_event(event_type, name, category, start_time)
  141. workers.append(worker)
  142. def calc_times(stats):
  143. tr = 0.0 # Runtime
  144. tt = 0.0 # Task
  145. ti = 0.0 # Idle
  146. ts = 0.0 # Scheduling
  147. for stat in stats:
  148. if stat._category == None:
  149. continue
  150. if stat._category == "Runtime":
  151. if stat._name == "Scheduling":
  152. # Scheduling time is part of runtime but we want to have
  153. # it separately.
  154. ts += stat._duration_time
  155. else:
  156. tr += stat._duration_time
  157. elif stat._category == "Task":
  158. tt += stat._duration_time
  159. elif stat._category == "Other":
  160. ti += stat._duration_time
  161. else:
  162. sys.exit("Unknown category '" + stat._category + "'!")
  163. return (ti, tr, tt, ts)
  164. def save_times(ti, tr, tt, ts):
  165. f = open("times.csv", "w+")
  166. f.write("\"Time\",\"Duration\"\n")
  167. f.write("\"Runtime\"," + str(tr) + "\n")
  168. f.write("\"Task\"," + str(tt) + "\n")
  169. f.write("\"Idle\"," + str(ti) + "\n")
  170. f.write("\"Scheduling\"," + str(ts) + "\n")
  171. f.close()
  172. def calc_et(tt_1, tt_p):
  173. """ Compute the task efficiency (et). This measures the exploitation of
  174. data locality. """
  175. return tt_1 / tt_p
  176. def calc_es(tt_p, ts_p):
  177. """ Compute the scheduling efficiency (es). This measures time spent in
  178. the runtime scheduler. """
  179. return tt_p / (tt_p + ts_p)
  180. def calc_er(tt_p, tr_p, ts_p):
  181. """ Compute the runtime efficiency (er). This measures how the runtime
  182. overhead affects performance."""
  183. return (tt_p + ts_p) / (tt_p + tr_p + ts_p)
  184. def calc_ep(tt_p, tr_p, ti_p, ts_p):
  185. """ Compute the pipeline efficiency (et). This measures how much
  186. concurrency is available and how well it's exploited. """
  187. return (tt_p + tr_p + ts_p) / (tt_p + tr_p + ti_p + ts_p)
  188. def calc_e(et, er, ep, es):
  189. """ Compute the parallel efficiency. """
  190. return et * er * ep * es
  191. def save_efficiencies(e, ep, er, et, es):
  192. f = open("efficiencies.csv", "w+")
  193. f.write("\"Efficiency\",\"Value\"\n")
  194. f.write("\"Parallel\"," + str(e) + "\n")
  195. f.write("\"Task\"," + str(et) + "\n")
  196. f.write("\"Runtime\"," + str(er) + "\n")
  197. f.write("\"Scheduling\"," + str(es) + "\n")
  198. f.write("\"Pipeline\"," + str(ep) + "\n")
  199. f.close()
  200. def usage():
  201. print "USAGE:"
  202. print "starpu_trace_state_stats.py [ -te -s=<time> ] <trace.rec>"
  203. print
  204. print "OPTIONS:"
  205. print " -t or --time Compute and dump times to times.csv"
  206. print
  207. print " -e or --efficiency Compute and dump efficiencies to efficiencies.csv"
  208. print
  209. print " -s or --seq_task_time Used to compute task efficiency between sequential and parallel times"
  210. print " (if not set, task efficiency will be 1.0)"
  211. print
  212. print "EXAMPLES:"
  213. print "# Compute event statistics and report them to stdout:"
  214. print "python starpu_trace_state_stats.py trace.rec"
  215. print
  216. print "# Compute event stats, times and efficiencies:"
  217. print "python starpu_trace_state_stats.py -te trace.rec"
  218. print
  219. print "# Compute correct task efficiency with the sequential task time:"
  220. print "python starpu_trace_state_stats.py -s=60093.950614 trace.rec"
  221. def main():
  222. try:
  223. opts, args = getopt.getopt(sys.argv[1:], "hets:",
  224. ["help", "time", "efficiency", "seq_task_time="])
  225. except getopt.GetoptError as err:
  226. usage()
  227. sys.exit(1)
  228. dump_time = False
  229. dump_efficiency = False
  230. tt_1 = 0.0
  231. for o, a in opts:
  232. if o in ("-h", "--help"):
  233. usage()
  234. sys.exit()
  235. elif o in ("-t", "--time"):
  236. dump_time = True
  237. elif o in ("-e", "--efficiency"):
  238. dump_efficiency = True
  239. elif o in ("-s", "--seq_task_time"):
  240. tt_1 = float(a)
  241. if len(args) < 1:
  242. usage()
  243. sys.exit()
  244. recfile = args[0]
  245. if not os.path.isfile(recfile):
  246. sys.exit("File does not exist!")
  247. # Declare a list for all workers.
  248. workers = []
  249. # Declare a list for program events
  250. prog_events = []
  251. # Read the recutils file format per blocks.
  252. blocks = read_blocks(recfile)
  253. for block in blocks:
  254. if not len(block) == 0:
  255. first_line = block[0]
  256. if first_line[:2] == "E:":
  257. insert_worker_event(workers, prog_events, block)
  258. # Find allowed range times between start/stop profiling events.
  259. start_profiling_times = []
  260. stop_profiling_times = []
  261. for prog_event in prog_events:
  262. if prog_event._name == "start_profiling":
  263. start_profiling_times.append(prog_event._start_time)
  264. if prog_event._name == "stop_profiling":
  265. stop_profiling_times.append(prog_event._start_time)
  266. if len(start_profiling_times) != len(stop_profiling_times):
  267. sys.exit("Mismatch number of start/stop profiling events!")
  268. # Compute worker statistics.
  269. stats = []
  270. for worker in workers:
  271. worker.calc_stats(start_profiling_times, stop_profiling_times)
  272. for stat in worker._stats:
  273. found = False
  274. for s in stats:
  275. if stat._name == s._name:
  276. found = True
  277. break
  278. if not found == True:
  279. stats.append(EventStats(stat._name, 0.0, stat._category, 0))
  280. # Compute global statistics for all workers.
  281. for i in xrange(0, len(workers)):
  282. for stat in stats:
  283. s = workers[i].get_event_stats(stat._name)
  284. if not s == None:
  285. # A task might not be executed on all workers.
  286. stat._duration_time += s._duration_time
  287. stat._count += s._count
  288. # Output statistics.
  289. print "\"Name\",\"Count\",\"Type\",\"Duration\""
  290. for stat in stats:
  291. stat.show()
  292. # Compute runtime, task, idle, scheduling times and dump them to times.csv
  293. ti_p = tr_p = tt_p = ts_p = 0.0
  294. if dump_time == True:
  295. ti_p, tr_p, tt_p, ts_p = calc_times(stats)
  296. save_times(ti_p, tr_p, tt_p, ts_p)
  297. # Compute runtime, task, idle efficiencies and dump them to
  298. # efficiencies.csv.
  299. if dump_efficiency == True or not tt_1 == 0.0:
  300. if dump_time == False:
  301. ti_p, tr_p, tt_p = ts_p = calc_times(stats)
  302. if tt_1 == 0.0:
  303. sys.stderr.write("WARNING: Task efficiency will be 1.0 because -s is not set!\n")
  304. tt_1 = tt_p
  305. # Compute efficiencies.
  306. et = round(calc_et(tt_1, tt_p), 6)
  307. es = round(calc_es(tt_p, ts_p), 6)
  308. er = round(calc_er(tt_p, tr_p, ts_p), 6)
  309. ep = round(calc_ep(tt_p, tr_p, ti_p, ts_p), 6)
  310. e = round(calc_e(et, er, ep, es), 6)
  311. save_efficiencies(e, ep, er, et, es)
  312. if __name__ == "__main__":
  313. main()