1 # Copyright (c) 2006-2008 The Regents of The University of Michigan
2 # Copyright (c) 2010 Advanced Micro Devices, Inc.
5 # Redistribution and use in source and binary forms, with or without
6 # modification, are permitted provided that the following conditions are
7 # met: redistributions of source code must retain the above copyright
8 # notice, this list of conditions and the following disclaimer;
9 # redistributions in binary form must reproduce the above copyright
10 # notice, this list of conditions and the following disclaimer in the
11 # documentation and/or other materials provided with the distribution;
12 # neither the name of the copyright holders nor the names of its
13 # contributors may be used to endorse or promote products derived from
14 # this software without specific prior written permission.
16 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
19 # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
20 # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
21 # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
22 # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
26 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 from os
.path
import join
as joinpath
35 from m5
.defines
import buildEnv
36 from m5
.objects
import *
38 from O3_ARM_v7a
import *
40 addToPath('../common')
42 def getCPUClass(cpu_type
):
43 """Returns the required cpu class and the mode of operation.
46 if cpu_type
== "timing":
47 return TimingSimpleCPU
, 'timing'
48 elif cpu_type
== "detailed":
49 return DerivO3CPU
, 'timing'
50 elif cpu_type
== "arm_detailed":
51 return O3_ARM_v7a_3
, 'timing'
52 elif cpu_type
== "inorder":
53 return InOrderCPU
, 'timing'
55 return AtomicSimpleCPU
, 'atomic'
57 def setCPUClass(options
):
58 """Returns two cpu classes and the initial mode of operation.
60 Restoring from a checkpoint or fast forwarding through a benchmark
61 can be done using one type of cpu, and then the actual
62 simulation can be carried out using another type. This function
63 returns these two types of cpus and the initial mode of operation
64 depending on the options provided.
67 if options
.cpu_type
== "detailed" or \
68 options
.cpu_type
== "arm_detailed" or \
69 options
.cpu_type
== "inorder" :
70 if not options
.caches
and not options
.ruby
:
71 fatal("O3/Inorder CPU must be used with caches")
73 TmpClass
, test_mem_mode
= getCPUClass(options
.cpu_type
)
76 if options
.checkpoint_restore
!= None:
77 if options
.restore_with_cpu
!= options
.cpu_type
:
79 TmpClass
, test_mem_mode
= getCPUClass(options
.restore_with_cpu
)
80 elif options
.fast_forward
:
82 TmpClass
= AtomicSimpleCPU
83 test_mem_mode
= 'atomic'
85 return (TmpClass
, test_mem_mode
, CPUClass
)
87 def setWorkCountOptions(system
, options
):
88 if options
.work_item_id
!= None:
89 system
.work_item_id
= options
.work_item_id
90 if options
.work_begin_cpu_id_exit
!= None:
91 system
.work_begin_cpu_id_exit
= options
.work_begin_cpu_id_exit
92 if options
.work_end_exit_count
!= None:
93 system
.work_end_exit_count
= options
.work_end_exit_count
94 if options
.work_end_checkpoint_count
!= None:
95 system
.work_end_ckpt_count
= options
.work_end_checkpoint_count
96 if options
.work_begin_exit_count
!= None:
97 system
.work_begin_exit_count
= options
.work_begin_exit_count
98 if options
.work_begin_checkpoint_count
!= None:
99 system
.work_begin_ckpt_count
= options
.work_begin_checkpoint_count
100 if options
.work_cpus_checkpoint_count
!= None:
101 system
.work_cpus_ckpt_count
= options
.work_cpus_checkpoint_count
103 def findCptDir(options
, maxtick
, cptdir
, testsys
):
104 """Figures out the directory from which the checkpointed state is read.
106 There are two different ways in which the directories holding checkpoints
108 1. cpt.<benchmark name>.<instruction count when the checkpoint was taken>
109 2. cpt.<some number, usually the tick value when the checkpoint was taken>
111 This function parses through the options to figure out which one of the
112 above should be used for selecting the checkpoint, and then figures out
113 the appropriate directory.
115 It also sets the value of the maximum tick value till which the simulation
119 from os
.path
import isdir
, exists
120 from os
import listdir
123 if not isdir(cptdir
):
124 fatal("checkpoint dir %s does not exist!", cptdir
)
126 if options
.at_instruction
or options
.simpoint
:
127 inst
= options
.checkpoint_restore
129 # assume workload 0 has the simpoint
130 if testsys
.cpu
[0].workload
[0].simpoint
== 0:
131 fatal('Unable to find simpoint')
132 inst
+= int(testsys
.cpu
[0].workload
[0].simpoint
)
134 checkpoint_dir
= joinpath(cptdir
, "cpt.%s.%s" % (options
.bench
, inst
))
135 if not exists(checkpoint_dir
):
136 fatal("Unable to find checkpoint directory %s", checkpoint_dir
)
138 dirs
= listdir(cptdir
)
139 expr
= re
.compile('cpt\.([0-9]*)')
142 match
= expr
.match(dir)
144 cpts
.append(match
.group(1))
146 cpts
.sort(lambda a
,b
: cmp(long(a
), long(b
)))
148 cpt_num
= options
.checkpoint_restore
149 if cpt_num
> len(cpts
):
150 fatal('Checkpoint %d not found', cpt_num
)
152 maxtick
= maxtick
- int(cpts
[cpt_num
- 1])
153 checkpoint_dir
= joinpath(cptdir
, "cpt.%s" % cpts
[cpt_num
- 1])
155 return maxtick
, checkpoint_dir
157 def scriptCheckpoints(options
, maxtick
, cptdir
):
158 if options
.at_instruction
or options
.simpoint
:
159 checkpoint_inst
= int(options
.take_checkpoints
)
161 # maintain correct offset if we restored from some instruction
162 if options
.checkpoint_restore
!= None:
163 checkpoint_inst
+= options
.checkpoint_restore
165 print "Creating checkpoint at inst:%d" % (checkpoint_inst
)
166 exit_event
= m5
.simulate()
167 exit_cause
= exit_event
.getCause()
168 print "exit cause = %s" % exit_cause
170 # skip checkpoint instructions should they exist
171 while exit_cause
== "checkpoint":
172 exit_event
= m5
.simulate()
173 exit_cause
= exit_event
.getCause()
175 if exit_cause
== "a thread reached the max instruction count":
176 m5
.checkpoint(joinpath(cptdir
, "cpt.%s.%d" % \
177 (options
.bench
, checkpoint_inst
)))
178 print "Checkpoint written."
181 when
, period
= options
.take_checkpoints
.split(",", 1)
186 exit_event
= m5
.simulate(when
)
187 exit_cause
= exit_event
.getCause()
188 while exit_cause
== "checkpoint":
189 exit_event
= m5
.simulate(when
- m5
.curTick())
190 exit_cause
= exit_event
.getCause()
192 if exit_cause
== "simulate() limit reached":
193 m5
.checkpoint(joinpath(cptdir
, "cpt.%d"))
197 max_checkpoints
= options
.max_checkpoints
199 while num_checkpoints
< max_checkpoints
and \
200 exit_cause
== "simulate() limit reached":
201 if (sim_ticks
+ period
) > maxtick
:
202 exit_event
= m5
.simulate(maxtick
- sim_ticks
)
203 exit_cause
= exit_event
.getCause()
206 exit_event
= m5
.simulate(period
)
207 exit_cause
= exit_event
.getCause()
209 while exit_event
.getCause() == "checkpoint":
210 exit_event
= m5
.simulate(sim_ticks
- m5
.curTick())
211 if exit_event
.getCause() == "simulate() limit reached":
212 m5
.checkpoint(joinpath(cptdir
, "cpt.%d"))
217 def benchCheckpoints(options
, maxtick
, cptdir
):
218 exit_event
= m5
.simulate(maxtick
)
219 exit_cause
= exit_event
.getCause()
222 max_checkpoints
= options
.max_checkpoints
224 while exit_cause
== "checkpoint":
225 m5
.checkpoint(joinpath(cptdir
, "cpt.%d"))
227 if num_checkpoints
== max_checkpoints
:
228 exit_cause
= "maximum %d checkpoints dropped" % max_checkpoints
231 exit_event
= m5
.simulate(maxtick
- m5
.curTick())
232 exit_cause
= exit_event
.getCause()
236 def repeatSwitch(testsys
, repeat_switch_cpu_list
, maxtick
, switch_freq
):
237 print "starting switch loop"
239 exit_event
= m5
.simulate(switch_freq
)
240 exit_cause
= exit_event
.getCause()
242 if exit_cause
!= "simulate() limit reached":
245 print "draining the system"
247 m5
.switchCpus(repeat_switch_cpu_list
)
251 for old_cpu
, new_cpu
in repeat_switch_cpu_list
:
252 tmp_cpu_list
.append((new_cpu
, old_cpu
))
253 repeat_switch_cpu_list
= tmp_cpu_list
255 if (maxtick
- m5
.curTick()) <= switch_freq
:
256 exit_event
= m5
.simulate(maxtick
- m5
.curTick())
257 return exit_event
.getCause()
259 def run(options
, root
, testsys
, cpu_class
):
261 maxtick
= options
.maxtick
262 elif options
.maxtime
:
263 simtime
= m5
.ticks
.seconds(simtime
)
264 print "simulating for: ", simtime
269 if options
.checkpoint_dir
:
270 cptdir
= options
.checkpoint_dir
271 elif m5
.options
.outdir
:
272 cptdir
= m5
.options
.outdir
276 if options
.fast_forward
and options
.checkpoint_restore
!= None:
277 fatal("Can't specify both --fast-forward and --checkpoint-restore")
279 if options
.standard_switch
and not options
.caches
:
280 fatal("Must specify --caches when using --standard-switch")
282 if options
.standard_switch
and options
.repeat_switch
:
283 fatal("Can't specify both --standard-switch and --repeat-switch")
285 if options
.repeat_switch
and options
.take_checkpoints
:
286 fatal("Can't specify both --repeat-switch and --take-checkpoints")
288 np
= options
.num_cpus
291 if options
.prog_interval
:
293 testsys
.cpu
[i
].progress_interval
= options
.prog_interval
297 testsys
.cpu
[i
].max_insts_any_thread
= options
.maxinsts
300 switch_cpus
= [cpu_class(switched_out
=True, cpu_id
=(i
))
304 if options
.fast_forward
:
305 testsys
.cpu
[i
].max_insts_any_thread
= int(options
.fast_forward
)
306 switch_cpus
[i
].system
= testsys
307 switch_cpus
[i
].workload
= testsys
.cpu
[i
].workload
308 switch_cpus
[i
].clock
= testsys
.cpu
[i
].clock
311 switch_cpus
[i
].max_insts_any_thread
= options
.maxinsts
312 # Add checker cpu if selected
314 switch_cpus
[i
].addCheckerCpu()
316 testsys
.switch_cpus
= switch_cpus
317 switch_cpu_list
= [(testsys
.cpu
[i
], switch_cpus
[i
]) for i
in xrange(np
)]
319 if options
.repeat_switch
:
320 if options
.cpu_type
== "arm_detailed":
321 if not options
.caches
:
322 print "O3 CPU must be used with caches"
325 repeat_switch_cpus
= [O3_ARM_v7a_3(switched_out
=True, \
326 cpu_id
=(i
)) for i
in xrange(np
)]
327 elif options
.cpu_type
== "detailed":
328 if not options
.caches
:
329 print "O3 CPU must be used with caches"
332 repeat_switch_cpus
= [DerivO3CPU(switched_out
=True, \
333 cpu_id
=(i
)) for i
in xrange(np
)]
334 elif options
.cpu_type
== "inorder":
335 print "inorder CPU switching not supported"
337 elif options
.cpu_type
== "timing":
338 repeat_switch_cpus
= [TimingSimpleCPU(switched_out
=True, \
339 cpu_id
=(i
)) for i
in xrange(np
)]
341 repeat_switch_cpus
= [AtomicSimpleCPU(switched_out
=True, \
342 cpu_id
=(i
)) for i
in xrange(np
)]
345 repeat_switch_cpus
[i
].system
= testsys
346 repeat_switch_cpus
[i
].workload
= testsys
.cpu
[i
].workload
347 repeat_switch_cpus
[i
].clock
= testsys
.cpu
[i
].clock
350 repeat_switch_cpus
[i
].max_insts_any_thread
= options
.maxinsts
353 repeat_switch_cpus
[i
].addCheckerCpu()
355 testsys
.repeat_switch_cpus
= repeat_switch_cpus
358 repeat_switch_cpu_list
= [(switch_cpus
[i
], repeat_switch_cpus
[i
])
361 repeat_switch_cpu_list
= [(testsys
.cpu
[i
], repeat_switch_cpus
[i
])
364 if options
.standard_switch
:
365 switch_cpus
= [TimingSimpleCPU(switched_out
=True, cpu_id
=(i
))
367 switch_cpus_1
= [DerivO3CPU(switched_out
=True, cpu_id
=(i
))
371 switch_cpus
[i
].system
= testsys
372 switch_cpus_1
[i
].system
= testsys
373 switch_cpus
[i
].workload
= testsys
.cpu
[i
].workload
374 switch_cpus_1
[i
].workload
= testsys
.cpu
[i
].workload
375 switch_cpus
[i
].clock
= testsys
.cpu
[i
].clock
376 switch_cpus_1
[i
].clock
= testsys
.cpu
[i
].clock
378 # if restoring, make atomic cpu simulate only a few instructions
379 if options
.checkpoint_restore
!= None:
380 testsys
.cpu
[i
].max_insts_any_thread
= 1
381 # Fast forward to specified location if we are not restoring
382 elif options
.fast_forward
:
383 testsys
.cpu
[i
].max_insts_any_thread
= int(options
.fast_forward
)
384 # Fast forward to a simpoint (warning: time consuming)
385 elif options
.simpoint
:
386 if testsys
.cpu
[i
].workload
[0].simpoint
== 0:
387 fatal('simpoint not found')
388 testsys
.cpu
[i
].max_insts_any_thread
= \
389 testsys
.cpu
[i
].workload
[0].simpoint
390 # No distance specified, just switch
392 testsys
.cpu
[i
].max_insts_any_thread
= 1
395 if options
.warmup_insts
:
396 switch_cpus
[i
].max_insts_any_thread
= options
.warmup_insts
400 switch_cpus_1
[i
].max_insts_any_thread
= options
.maxinsts
402 # attach the checker cpu if selected
404 switch_cpus
[i
].addCheckerCpu()
405 switch_cpus_1
[i
].addCheckerCpu()
407 testsys
.switch_cpus
= switch_cpus
408 testsys
.switch_cpus_1
= switch_cpus_1
409 switch_cpu_list
= [(testsys
.cpu
[i
], switch_cpus
[i
]) for i
in xrange(np
)]
410 switch_cpu_list1
= [(switch_cpus
[i
], switch_cpus_1
[i
]) for i
in xrange(np
)]
412 # set the checkpoint in the cpu before m5.instantiate is called
413 if options
.take_checkpoints
!= None and \
414 (options
.simpoint
or options
.at_instruction
):
415 offset
= int(options
.take_checkpoints
)
416 # Set an instruction break point
419 if testsys
.cpu
[i
].workload
[0].simpoint
== 0:
420 fatal('no simpoint for testsys.cpu[%d].workload[0]', i
)
421 checkpoint_inst
= int(testsys
.cpu
[i
].workload
[0].simpoint
) + offset
422 testsys
.cpu
[i
].max_insts_any_thread
= checkpoint_inst
423 # used for output below
424 options
.take_checkpoints
= checkpoint_inst
426 options
.take_checkpoints
= offset
427 # Set all test cpus with the right number of instructions
428 # for the upcoming simulation
430 testsys
.cpu
[i
].max_insts_any_thread
= offset
432 checkpoint_dir
= None
433 if options
.checkpoint_restore
!= None:
434 maxtick
, checkpoint_dir
= findCptDir(options
, maxtick
, cptdir
, testsys
)
435 m5
.instantiate(checkpoint_dir
)
437 if options
.standard_switch
or cpu_class
:
438 if options
.standard_switch
:
439 print "Switch at instruction count:%s" % \
440 str(testsys
.cpu
[0].max_insts_any_thread
)
441 exit_event
= m5
.simulate()
442 elif cpu_class
and options
.fast_forward
:
443 print "Switch at instruction count:%s" % \
444 str(testsys
.cpu
[0].max_insts_any_thread
)
445 exit_event
= m5
.simulate()
447 print "Switch at curTick count:%s" % str(10000)
448 exit_event
= m5
.simulate(10000)
449 print "Switched CPUS @ tick %s" % (m5
.curTick())
451 # when you change to Timing (or Atomic), you halt the system
452 # given as argument. When you are finished with the system
453 # changes (including switchCpus), you must resume the system
454 # manually. You DON'T need to resume after just switching
455 # CPUs if you haven't changed anything on the system level.
457 m5
.changeToTiming(testsys
)
458 m5
.switchCpus(switch_cpu_list
)
461 if options
.standard_switch
:
462 print "Switch at instruction count:%d" % \
463 (testsys
.switch_cpus
[0].max_insts_any_thread
)
465 #warmup instruction count may have already been set
466 if options
.warmup_insts
:
467 exit_event
= m5
.simulate()
469 exit_event
= m5
.simulate(options
.standard_switch
)
470 print "Switching CPUS @ tick %s" % (m5
.curTick())
471 print "Simulation ends instruction count:%d" % \
472 (testsys
.switch_cpus_1
[0].max_insts_any_thread
)
474 m5
.switchCpus(switch_cpu_list1
)
477 # If we're taking and restoring checkpoints, use checkpoint_dir
478 # option only for finding the checkpoints to restore from. This
479 # lets us test checkpointing by restoring from one set of
480 # checkpoints, generating a second set, and then comparing them.
481 if options
.take_checkpoints
and options
.checkpoint_restore
:
482 if m5
.options
.outdir
:
483 cptdir
= m5
.options
.outdir
487 if options
.take_checkpoints
!= None :
488 # Checkpoints being taken via the command line at <when> and at
489 # subsequent periods of <period>. Checkpoint instructions
490 # received from the benchmark running are ignored and skipped in
491 # favor of command line checkpoint instructions.
492 exit_cause
= scriptCheckpoints(options
, maxtick
, cptdir
)
494 if options
.fast_forward
:
496 print "**** REAL SIMULATION ****"
498 # If checkpoints are being taken, then the checkpoint instruction
499 # will occur in the benchmark code it self.
500 if options
.repeat_switch
and maxtick
> options
.repeat_switch
:
501 exit_cause
= repeatSwitch(testsys
, repeat_switch_cpu_list
,
502 maxtick
, options
.repeat_switch
)
504 exit_cause
= benchCheckpoints(options
, maxtick
, cptdir
)
506 print 'Exiting @ tick %i because %s' % (m5
.curTick(), exit_cause
)
507 if options
.checkpoint_at_end
:
508 m5
.checkpoint(joinpath(cptdir
, "cpt.%d"))
510 sys
.exit(exit_event
.getCode())