starpu_trace_state_stats.py 12 KB

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