starpu_trace_state_stats.py 13 KB

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