starpu_trace_state_stats.py 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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. curr_event = self._stack.pop()
  81. # Compute duration with the next event.
  82. a = curr_event._start_time
  83. b = next_event._start_time
  84. found = False
  85. for j in xrange(len(self._stats)):
  86. if self._stats[j]._name == curr_event._name:
  87. self._stats[j].aggregate(b - a)
  88. found = True
  89. break
  90. if not found == True:
  91. self._stats.append(EventStats(curr_event._name, b - a, curr_event._category))
  92. def read_blocks(input_file):
  93. empty_lines = 0
  94. first_line = 1
  95. blocks = []
  96. for line in open(input_file):
  97. if first_line:
  98. blocks.append([])
  99. blocks[-1].append(line)
  100. first_line = 0
  101. # Check for empty lines
  102. if not line or line[0] == '\n':
  103. # If 1st one: new block
  104. if empty_lines == 0:
  105. blocks.append([])
  106. empty_lines += 1
  107. else:
  108. # Non empty line: add line in current(last) block
  109. empty_lines = 0
  110. blocks[-1].append(line)
  111. return blocks
  112. def read_field(field, index):
  113. return field[index+1:-1]
  114. def insert_worker_event(workers, prog_events, block):
  115. worker_id = -1
  116. name = None
  117. start_time = 0.0
  118. category = None
  119. for line in block:
  120. if line[:2] == "E:": # EventType
  121. event_type = read_field(line, 2)
  122. elif line[:2] == "C:": # Category
  123. category = read_field(line, 2)
  124. elif line[:2] == "W:": # WorkerId
  125. worker_id = int(read_field(line, 2))
  126. elif line[:2] == "N:": # Name
  127. name = read_field(line, 2)
  128. elif line[:2] == "S:": # StartTime
  129. start_time = float(read_field(line, 2))
  130. # Program events don't belong to workers, they are globals.
  131. if category == "Program":
  132. prog_events.append(Event(event_type, name, category, start_time))
  133. return
  134. for worker in workers:
  135. if worker._id == worker_id:
  136. worker.add_event(event_type, name, category, start_time)
  137. return
  138. worker = Worker(worker_id)
  139. worker.add_event(event_type, name, category, start_time)
  140. workers.append(worker)
  141. def calc_times(stats):
  142. tr = 0.0 # Runtime
  143. tt = 0.0 # Task
  144. ti = 0.0 # Idle
  145. ts = 0.0 # Scheduling
  146. for stat in stats:
  147. if stat._category == None:
  148. continue
  149. if stat._category == "Runtime":
  150. if stat._name == "Scheduling":
  151. # Scheduling time is part of runtime but we want to have
  152. # it separately.
  153. ts += stat._duration_time
  154. else:
  155. tr += stat._duration_time
  156. elif stat._category == "Task":
  157. tt += stat._duration_time
  158. elif stat._category == "Other":
  159. ti += stat._duration_time
  160. else:
  161. sys.exit("Unknown category '" + stat._category + "'!")
  162. return (ti, tr, tt, ts)
  163. def save_times(ti, tr, tt, ts):
  164. f = open("times.csv", "w+")
  165. f.write("\"Time\",\"Duration\"\n")
  166. f.write("\"Runtime\"," + str(tr) + "\n")
  167. f.write("\"Task\"," + str(tt) + "\n")
  168. f.write("\"Idle\"," + str(ti) + "\n")
  169. f.write("\"Scheduling\"," + str(ts) + "\n")
  170. f.close()
  171. def calc_et(tt_1, tt_p):
  172. """ Compute the task efficiency (et). This measures the exploitation of
  173. data locality. """
  174. return tt_1 / tt_p
  175. def calc_es(tt_p, ts_p):
  176. """ Compute the scheduling efficiency (es). This measures time spent in
  177. the runtime scheduler. """
  178. return tt_p / (tt_p + ts_p)
  179. def calc_er(tt_p, tr_p, ts_p):
  180. """ Compute the runtime efficiency (er). This measures how the runtime
  181. overhead affects performance."""
  182. return (tt_p + ts_p) / (tt_p + tr_p + ts_p)
  183. def calc_ep(tt_p, tr_p, ti_p, ts_p):
  184. """ Compute the pipeline efficiency (et). This measures how much
  185. concurrency is available and how well it's exploited. """
  186. return (tt_p + tr_p + ts_p) / (tt_p + tr_p + ti_p + ts_p)
  187. def calc_e(et, er, ep, es):
  188. """ Compute the parallel efficiency. """
  189. return et * er * ep * es
  190. def save_efficiencies(e, ep, er, et, es):
  191. f = open("efficiencies.csv", "w+")
  192. f.write("\"Efficiency\",\"Value\"\n")
  193. f.write("\"Parallel\"," + str(e) + "\n")
  194. f.write("\"Task\"," + str(et) + "\n")
  195. f.write("\"Runtime\"," + str(er) + "\n")
  196. f.write("\"Scheduling\"," + str(es) + "\n")
  197. f.write("\"Pipeline\"," + str(ep) + "\n")
  198. f.close()
  199. def usage():
  200. print "USAGE:"
  201. print "starpu_trace_state_stats.py [ -te -s=<time> ] <trace.rec>"
  202. print
  203. print "OPTIONS:"
  204. print " -t or --time Compute and dump times to times.csv"
  205. print
  206. print " -e or --efficiency Compute and dump efficiencies to efficiencies.csv"
  207. print
  208. print " -s or --seq_task_time Used to compute task efficiency between sequential and parallel times"
  209. print " (if not set, task efficiency will be 1.0)"
  210. print
  211. print "EXAMPLES:"
  212. print "# Compute event statistics and report them to stdout:"
  213. print "python starpu_trace_state_stats.py trace.rec"
  214. print
  215. print "# Compute event stats, times and efficiencies:"
  216. print "python starpu_trace_state_stats.py -te trace.rec"
  217. print
  218. print "# Compute correct task efficiency with the sequential task time:"
  219. print "python starpu_trace_state_stats.py -s=60093.950614 trace.rec"
  220. def main():
  221. try:
  222. opts, args = getopt.getopt(sys.argv[1:], "hets:",
  223. ["help", "time", "efficiency", "seq_task_time="])
  224. except getopt.GetoptError as err:
  225. usage()
  226. sys.exit(1)
  227. dump_time = False
  228. dump_efficiency = False
  229. tt_1 = 0.0
  230. for o, a in opts:
  231. if o in ("-h", "--help"):
  232. usage()
  233. sys.exit()
  234. elif o in ("-t", "--time"):
  235. dump_time = True
  236. elif o in ("-e", "--efficiency"):
  237. dump_efficiency = True
  238. elif o in ("-s", "--seq_task_time"):
  239. tt_1 = float(a)
  240. if len(args) < 1:
  241. usage()
  242. sys.exit()
  243. recfile = args[0]
  244. if not os.path.isfile(recfile):
  245. sys.exit("File does not exist!")
  246. # Declare a list for all workers.
  247. workers = []
  248. # Declare a list for program events
  249. prog_events = []
  250. # Read the recutils file format per blocks.
  251. blocks = read_blocks(recfile)
  252. for block in blocks:
  253. if not len(block) == 0:
  254. first_line = block[0]
  255. if first_line[:2] == "E:":
  256. insert_worker_event(workers, prog_events, block)
  257. # Find allowed range times between start/stop profiling events.
  258. start_profiling_times = []
  259. stop_profiling_times = []
  260. for prog_event in prog_events:
  261. if prog_event._name == "start_profiling":
  262. start_profiling_times.append(prog_event._start_time)
  263. if prog_event._name == "stop_profiling":
  264. stop_profiling_times.append(prog_event._start_time)
  265. if len(start_profiling_times) != len(stop_profiling_times):
  266. sys.exit("Mismatch number of start/stop profiling events!")
  267. # Compute worker statistics.
  268. stats = []
  269. for worker in workers:
  270. worker.calc_stats(start_profiling_times, stop_profiling_times)
  271. for stat in worker._stats:
  272. found = False
  273. for s in stats:
  274. if stat._name == s._name:
  275. found = True
  276. break
  277. if not found == True:
  278. stats.append(EventStats(stat._name, 0.0, stat._category, 0))
  279. # Compute global statistics for all workers.
  280. for i in xrange(0, len(workers)):
  281. for stat in stats:
  282. s = workers[i].get_event_stats(stat._name)
  283. if not s == None:
  284. # A task might not be executed on all workers.
  285. stat._duration_time += s._duration_time
  286. stat._count += s._count
  287. # Output statistics.
  288. print "\"Name\",\"Count\",\"Type\",\"Duration\""
  289. for stat in stats:
  290. stat.show()
  291. # Compute runtime, task, idle, scheduling times and dump them to times.csv
  292. ti_p = tr_p = tt_p = ts_p = 0.0
  293. if dump_time == True:
  294. ti_p, tr_p, tt_p, ts_p = calc_times(stats)
  295. save_times(ti_p, tr_p, tt_p, ts_p)
  296. # Compute runtime, task, idle efficiencies and dump them to
  297. # efficiencies.csv.
  298. if dump_efficiency == True or not tt_1 == 0.0:
  299. if dump_time == False:
  300. ti_p, tr_p, tt_p = ts_p = calc_times(stats)
  301. if tt_1 == 0.0:
  302. sys.stderr.write("WARNING: Task efficiency will be 1.0 because -s is not set!\n")
  303. tt_1 = tt_p
  304. # Compute efficiencies.
  305. et = round(calc_et(tt_1, tt_p), 6)
  306. es = round(calc_es(tt_p, ts_p), 6)
  307. er = round(calc_er(tt_p, tr_p, ts_p), 6)
  308. ep = round(calc_ep(tt_p, tr_p, ti_p, ts_p), 6)
  309. e = round(calc_e(et, er, ep, es), 6)
  310. save_efficiencies(e, ep, er, et, es)
  311. if __name__ == "__main__":
  312. main()