stop-by-icount.py 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. # End ROI after x instructions (aggregated over all cores), with configurable x (default 1B) and optional warmup
  2. # Combine with --no-cache-warming to use fast-forward rather than cache warmup
  3. # Usage: -s stop-by-icount:30000000 # Start in detailed, and end after 30M instructions
  4. # -s stop-by-icount:30000000 --roi # Start in warmup, switch to detailed at application ROI marker, and end after 30M instructions
  5. # -s stop-by-icount:30000000:100000000 --roi-script # Start in cache-warmup, switch to detailed after 100M instructions, and run for 30M in detailed
  6. # -s stop-by-icount:30000000:roi+100000000 --roi-script # Start in cache-warmup, wait for application ROI, switch to detailed after 100M instructions, and run for 30M in detailed
  7. import sim
  8. class StopByIcount:
  9. def _min_callback(self):
  10. return min(self.ninstrs_start or (), self.ninstrs, self.min_ins_global)
  11. def setup(self, args):
  12. self.magic = sim.config.get_bool('general/magic')
  13. self.min_ins_global = long(sim.config.get('core/hook_periodic_ins/ins_global'))
  14. self.wait_for_app_roi = False
  15. self.verbose = False
  16. args = dict(enumerate((args or '').split(':')))
  17. self.ninstrs = long(args.get(0, 1e9))
  18. start = args.get(1, None)
  19. roirelstart = False
  20. # Make the start input value canonical
  21. if start == '':
  22. start = None
  23. roiscript = sim.config.get_bool('general/roi_script')
  24. if start == None and not roiscript:
  25. if self.magic:
  26. self.roi_rel = True
  27. self.ninstrs_start = 0
  28. self.inroi = False
  29. print '[STOPBYICOUNT] Waiting for application ROI'
  30. else:
  31. self.roi_rel = True
  32. self.ninstrs_start = 0
  33. self.inroi = True
  34. print '[STOPBYICOUNT] Starting in ROI (detail)'
  35. else:
  36. if self.magic:
  37. print '[STOPBYICOUNT] ERROR: Application ROIs and warmup cannot be combined when using --roi'
  38. print '[STOPBYICOUNT] Use syntax: -s stop-by-icount:NDETAIL:roi+NWARMUP --roi-script'
  39. sim.control.abort()
  40. self.done = True
  41. return
  42. if not roiscript:
  43. print '[STOPBYICOUNT] ERROR: --roi-script is not set, but is required when using a start instruction count. Aborting'
  44. sim.control.abort()
  45. self.done = True
  46. return
  47. if start == None:
  48. # If start == None, then an explicit start has not been set, but --roi-script has also been enabled.
  49. # Therefore, set to start on the next instruction callback
  50. print '[STOPBYICOUNT] WARNING: No explicit start instruction count was set, but --roi-script is set'
  51. print ' WARNING: Starting detailed simulation on the next callback, %d instructions' % self.min_ins_global
  52. print ' WARNING: To start from the beginning, do not use --roi-script with a single stop argument'
  53. start = self.min_ins_global
  54. if start.startswith('roi+'):
  55. self.ninstrs_start = long(start[4:])
  56. self.roi_rel = True
  57. self.wait_for_app_roi = True
  58. print '[STOPBYICOUNT] Starting %s instructions after ROI begin' % self.ninstrs_start
  59. else:
  60. self.ninstrs_start = long(start)
  61. self.roi_rel = False
  62. print '[STOPBYICOUNT] Starting after %s instructions' % self.ninstrs_start
  63. self.inroi = False
  64. print '[STOPBYICOUNT] Then stopping after simulating %s instructions in detail' % ((self.roi_rel and 'at least ' or '') + str(self.ninstrs))
  65. self.done = False
  66. sim.util.EveryIns(self._min_callback(), self.periodic, roi_only = (start == None))
  67. def hook_application_roi_begin(self):
  68. if self.wait_for_app_roi:
  69. print '[STOPBYICOUNT] Application at ROI begin, fast-forwarding for', self.ninstrs_start, 'more instructions'
  70. self.wait_for_app_roi = False
  71. self.ninstrs_start = sim.stats.icount() + self.ninstrs_start
  72. def hook_roi_begin(self):
  73. if self.magic:
  74. self.ninstrs_start = sim.stats.icount()
  75. self.inroi = True
  76. print '[STOPBYICOUNT] Application ROI started, now simulating', self.ninstrs, 'in detail'
  77. def periodic(self, icount, icount_delta):
  78. if not self.wait_for_app_roi and not self.done and icount < (self.ninstrs + self.ninstrs_start):
  79. sim.control.set_progress(icount / float(self.ninstrs + self.ninstrs_start + 1))
  80. if self.done:
  81. return
  82. if self.verbose:
  83. print '[STOPBYICOUNT] Periodic at', icount, ' delta =', icount_delta
  84. if self.inroi and icount > (self.ninstrs + self.ninstrs_start):
  85. print '[STOPBYICOUNT] Ending ROI after %s instructions (%s requested)' % (icount - self.ninstrs_start, self.ninstrs)
  86. sim.control.set_roi(False)
  87. self.inroi = False
  88. self.done = True
  89. sim.control.abort()
  90. elif not self.inroi and not self.wait_for_app_roi and icount > self.ninstrs_start:
  91. print '[STOPBYICOUNT] Starting ROI after %s instructions' % icount
  92. sim.control.set_roi(True)
  93. self.inroi = True
  94. sim.util.register(StopByIcount())