sim_events.cc revision 5606
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"
381127SN/A#include "sim/startup.hh"
39695SN/A#include "sim/stats.hh"
402SN/A
412SN/Ausing namespace std;
422SN/A
435606Snate@binkert.orgSimLoopExitEvent::SimLoopExitEvent(const std::string &_cause, int c, Tick r)
445606Snate@binkert.org    : Event(Sim_Exit_Pri), cause(_cause), code(c), repeat(r)
455606Snate@binkert.org{
465606Snate@binkert.org    setFlags(IsExitEvent);
475606Snate@binkert.org}
485606Snate@binkert.org
495606Snate@binkert.org
502SN/A//
512SN/A// handle termination event
522SN/A//
532SN/Avoid
542667Sstever@eecs.umich.eduSimLoopExitEvent::process()
552SN/A{
562667Sstever@eecs.umich.edu    // if this got scheduled on a different queue (e.g. the committed
572667Sstever@eecs.umich.edu    // instruction queue) then make a corresponding event on the main
582667Sstever@eecs.umich.edu    // queue.
595606Snate@binkert.org    if (!getFlags(IsMainQueue)) {
602667Sstever@eecs.umich.edu        exitSimLoop(cause, code);
612SN/A        delete this;
622SN/A    }
632667Sstever@eecs.umich.edu
642667Sstever@eecs.umich.edu    // otherwise do nothing... the IsExitEvent flag takes care of
652667Sstever@eecs.umich.edu    // exiting the simulation loop and returning this object to Python
663144Shsul@eecs.umich.edu
673144Shsul@eecs.umich.edu    // but if you are doing this on intervals, don't forget to make another
683144Shsul@eecs.umich.edu    if (repeat) {
695606Snate@binkert.org        assert(getFlags(IsMainQueue));
705606Snate@binkert.org        mainEventQueue.schedule(this, curTick + repeat);
713144Shsul@eecs.umich.edu    }
722SN/A}
732SN/A
742SN/A
752SN/Aconst char *
765336Shines@cs.fsu.eduSimLoopExitEvent::description() const
772SN/A{
782667Sstever@eecs.umich.edu    return "simulation loop exit";
792667Sstever@eecs.umich.edu}
802667Sstever@eecs.umich.edu
812667Sstever@eecs.umich.eduvoid
822667Sstever@eecs.umich.eduexitSimLoop(const std::string &message, int exit_code)
832667Sstever@eecs.umich.edu{
845606Snate@binkert.org    Event *event = new SimLoopExitEvent(message, exit_code);
855606Snate@binkert.org    mainEventQueue.schedule(event, curTick);
862SN/A}
872SN/A
885606Snate@binkert.orgCountedDrainEvent::CountedDrainEvent()
895606Snate@binkert.org    : SimLoopExitEvent("Finished drain", 0), count(0)
905606Snate@binkert.org{ }
915606Snate@binkert.org
922797Sktlim@umich.eduvoid
932839Sktlim@umich.eduCountedDrainEvent::process()
942797Sktlim@umich.edu{
955606Snate@binkert.org    if (--count == 0)
965606Snate@binkert.org        exitSimLoop(cause, code);
972797Sktlim@umich.edu}
982797Sktlim@umich.edu
992SN/A//
1002SN/A// constructor: automatically schedules at specified time
1012SN/A//
1025606Snate@binkert.orgCountedExitEvent::CountedExitEvent(const std::string &_cause, int &counter)
1035606Snate@binkert.org    : Event(Sim_Exit_Pri), cause(_cause), downCounter(counter)
1042SN/A{
1052SN/A    // catch stupid mistakes
1062SN/A    assert(downCounter > 0);
1072SN/A}
1082SN/A
1092SN/A
1102SN/A//
1112SN/A// handle termination event
1122SN/A//
1132SN/Avoid
1142SN/ACountedExitEvent::process()
1152SN/A{
1162SN/A    if (--downCounter == 0) {
1172667Sstever@eecs.umich.edu        exitSimLoop(cause, 0);
1182SN/A    }
1192SN/A}
1202SN/A
1212SN/A
1222SN/Aconst char *
1235336Shines@cs.fsu.eduCountedExitEvent::description() const
1242SN/A{
1252SN/A    return "counted exit";
1262SN/A}
1272SN/A
1285606Snate@binkert.orgCheckSwapEvent::CheckSwapEvent(int ival)
1295606Snate@binkert.org    : interval(ival)
1305606Snate@binkert.org{
1315606Snate@binkert.org    mainEventQueue.schedule(this, curTick + interval);
1325606Snate@binkert.org}
1332SN/A
1342SN/Avoid
1352SN/ACheckSwapEvent::process()
1362SN/A{
1372SN/A    /*  Check the amount of free swap space  */
1382SN/A    long swap;
1392SN/A
1402SN/A    /*  returns free swap in KBytes  */
14152SN/A    swap = procInfo("/proc/meminfo", "SwapFree:");
1422SN/A
1432SN/A    if (swap < 1000)
1442SN/A        ccprintf(cerr, "\a\a\aWarning! Swap space is low (%d)\n", swap);
1452SN/A
1462SN/A    if (swap < 100) {
1472SN/A        cerr << "\a\aAborting Simulation! Inadequate swap space!\n\n";
1482667Sstever@eecs.umich.edu        exitSimLoop("Lack of swap space");
1492SN/A    }
1502SN/A
1515606Snate@binkert.org    assert(getFlags(IsMainQueue));
1525606Snate@binkert.org    mainEventQueue.schedule(this, curTick + interval);
1532SN/A}
1542SN/A
1552SN/Aconst char *
1565336Shines@cs.fsu.eduCheckSwapEvent::description() const
1572SN/A{
1582SN/A    return "check swap";
1592SN/A}
160