profile.py 1.2 KB

123456789101112131415161718192021222324252627282930313233
  1. import os, sys, json, collections, distutils.spawn
  2. HOME = os.path.abspath(os.path.dirname(__file__))
  3. sys.path.extend( [os.path.abspath(os.path.join(HOME, '..'))] )
  4. import gen_profile, sniper_lib, sniper_config, sniper_stats
  5. # From http://stackoverflow.com/questions/600268/mkdir-p-functionality-in-python
  6. def mkdir_p(path):
  7. import errno
  8. try:
  9. os.makedirs(path)
  10. except OSError, exc:
  11. if exc.errno == errno.EEXIST and os.path.isdir(path):
  12. pass
  13. else: raise
  14. def createJSONData(resultsdir, outputdir, verbose = False):
  15. profiledir = os.path.join(outputdir,'levels','profile')
  16. mkdir_p(profiledir)
  17. prof = gen_profile.Profile(resultsdir)
  18. callgrindfile = os.path.join(profiledir, 'callgrind.out.sniper')
  19. prof.writeCallgrind(file(callgrindfile, 'w'))
  20. gprof2dot_py = os.path.join(HOME, '..', 'gprof2dot.py')
  21. dotbasefile = os.path.join(profiledir, 'sim.profile')
  22. os.system('%s --format=callgrind --output=%s.dot %s' % (gprof2dot_py, dotbasefile, callgrindfile))
  23. if not distutils.spawn.find_executable('dot'):
  24. raise RuntimeError("Could not find `dot' executable, make sure graphviz is installed")
  25. os.system('dot -Tsvg %s.dot -o %s.svg' % (dotbasefile, dotbasefile))
  26. os.system('dot -Tpng %s.dot -o %s.png' % (dotbasefile, dotbasefile))