sim_events.cc revision 7823
12SN/A/*
21762SN/A * Copyright (c) 2002-2005 The Regents of The University of Michigan
32SN/A * All rights reserved.
42SN/A *
52SN/A * Redistribution and use in source and binary forms, with or without
62SN/A * modification, are permitted provided that the following conditions are
72SN/A * met: redistributions of source code must retain the above copyright
82SN/A * notice, this list of conditions and the following disclaimer;
92SN/A * redistributions in binary form must reproduce the above copyright
102SN/A * notice, this list of conditions and the following disclaimer in the
112SN/A * documentation and/or other materials provided with the distribution;
122SN/A * neither the name of the copyright holders nor the names of its
132SN/A * contributors may be used to endorse or promote products derived from
142SN/A * this software without specific prior written permission.
152SN/A *
162SN/A * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
172SN/A * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
182SN/A * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
192SN/A * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
202SN/A * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
212SN/A * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
222SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
232SN/A * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
242SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
252SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
262SN/A * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
272665Ssaidi@eecs.umich.edu *
282665Ssaidi@eecs.umich.edu * Authors: Nathan Binkert
292SN/A */
302SN/A
312SN/A#include <string>
322SN/A
33601SN/A#include "base/callback.hh"
34601SN/A#include "base/hostinfo.hh"
35601SN/A#include "sim/eventq.hh"
3656SN/A#include "sim/sim_events.hh"
3756SN/A#include "sim/sim_exit.hh"
38695SN/A#include "sim/stats.hh"
392SN/A
402SN/Ausing namespace std;
412SN/A
425606Snate@binkert.orgSimLoopExitEvent::SimLoopExitEvent(const std::string &_cause, int c, Tick r)
435606Snate@binkert.org    : Event(Sim_Exit_Pri), cause(_cause), code(c), repeat(r)
445606Snate@binkert.org{
455606Snate@binkert.org    setFlags(IsExitEvent);
465606Snate@binkert.org}
475606Snate@binkert.org
485606Snate@binkert.org
492SN/A//
502SN/A// handle termination event
512SN/A//
522SN/Avoid
532667Sstever@eecs.umich.eduSimLoopExitEvent::process()
542SN/A{
552667Sstever@eecs.umich.edu    // if this got scheduled on a different queue (e.g. the committed
562667Sstever@eecs.umich.edu    // instruction queue) then make a corresponding event on the main
572667Sstever@eecs.umich.edu    // queue.
585606Snate@binkert.org    if (!getFlags(IsMainQueue)) {
592667Sstever@eecs.umich.edu        exitSimLoop(cause, code);
602SN/A        delete this;
612SN/A    }
622667Sstever@eecs.umich.edu
632667Sstever@eecs.umich.edu    // otherwise do nothing... the IsExitEvent flag takes care of
642667Sstever@eecs.umich.edu    // exiting the simulation loop and returning this object to Python
653144Shsul@eecs.umich.edu
663144Shsul@eecs.umich.edu    // but if you are doing this on intervals, don't forget to make another
673144Shsul@eecs.umich.edu    if (repeat) {
685606Snate@binkert.org        assert(getFlags(IsMainQueue));
697823Ssteve.reinhardt@amd.com        mainEventQueue.schedule(this, curTick() + repeat);
703144Shsul@eecs.umich.edu    }
712SN/A}
722SN/A
732SN/A
742SN/Aconst char *
755336Shines@cs.fsu.eduSimLoopExitEvent::description() const
762SN/A{
772667Sstever@eecs.umich.edu    return "simulation loop exit";
782667Sstever@eecs.umich.edu}
792667Sstever@eecs.umich.edu
802667Sstever@eecs.umich.eduvoid
817819Ssteve.reinhardt@amd.comexitSimLoop(const std::string &message, int exit_code, Tick when, Tick repeat)
822667Sstever@eecs.umich.edu{
837819Ssteve.reinhardt@amd.com    Event *event = new SimLoopExitEvent(message, exit_code, repeat);
847819Ssteve.reinhardt@amd.com    mainEventQueue.schedule(event, when);
852SN/A}
862SN/A
875606Snate@binkert.orgCountedDrainEvent::CountedDrainEvent()
887821Ssteve.reinhardt@amd.com    : count(0)
895606Snate@binkert.org{ }
905606Snate@binkert.org
912797Sktlim@umich.eduvoid
922839Sktlim@umich.eduCountedDrainEvent::process()
932797Sktlim@umich.edu{
945606Snate@binkert.org    if (--count == 0)
957821Ssteve.reinhardt@amd.com        exitSimLoop("Finished drain", 0);
962797Sktlim@umich.edu}
972797Sktlim@umich.edu
982SN/A//
992SN/A// constructor: automatically schedules at specified time
1002SN/A//
1015606Snate@binkert.orgCountedExitEvent::CountedExitEvent(const std::string &_cause, int &counter)
1025606Snate@binkert.org    : Event(Sim_Exit_Pri), cause(_cause), downCounter(counter)
1032SN/A{
1042SN/A    // catch stupid mistakes
1052SN/A    assert(downCounter > 0);
1062SN/A}
1072SN/A
1082SN/A
1092SN/A//
1102SN/A// handle termination event
1112SN/A//
1122SN/Avoid
1132SN/ACountedExitEvent::process()
1142SN/A{
1152SN/A    if (--downCounter == 0) {
1162667Sstever@eecs.umich.edu        exitSimLoop(cause, 0);
1172SN/A    }
1182SN/A}
1192SN/A
1202SN/A
1212SN/Aconst char *
1225336Shines@cs.fsu.eduCountedExitEvent::description() const
1232SN/A{
1242SN/A    return "counted exit";
1252SN/A}
126