sim_events.cc revision 7819
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));
695606Snate@binkert.org        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()
885606Snate@binkert.org    : SimLoopExitEvent("Finished drain", 0), count(0)
895606Snate@binkert.org{ }
905606Snate@binkert.org
912797Sktlim@umich.eduvoid
922839Sktlim@umich.eduCountedDrainEvent::process()
932797Sktlim@umich.edu{
945606Snate@binkert.org    if (--count == 0)
955606Snate@binkert.org        exitSimLoop(cause, code);
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}
1262SN/A
1275606Snate@binkert.orgCheckSwapEvent::CheckSwapEvent(int ival)
1285606Snate@binkert.org    : interval(ival)
1295606Snate@binkert.org{
1305606Snate@binkert.org    mainEventQueue.schedule(this, curTick + interval);
1315606Snate@binkert.org}
1322SN/A
1332SN/Avoid
1342SN/ACheckSwapEvent::process()
1352SN/A{
1362SN/A    /*  Check the amount of free swap space  */
1372SN/A    long swap;
1382SN/A
1392SN/A    /*  returns free swap in KBytes  */
14052SN/A    swap = procInfo("/proc/meminfo", "SwapFree:");
1412SN/A
1422SN/A    if (swap < 1000)
1432SN/A        ccprintf(cerr, "\a\a\aWarning! Swap space is low (%d)\n", swap);
1442SN/A
1452SN/A    if (swap < 100) {
1462SN/A        cerr << "\a\aAborting Simulation! Inadequate swap space!\n\n";
1472667Sstever@eecs.umich.edu        exitSimLoop("Lack of swap space");
1482SN/A    }
1492SN/A
1505606Snate@binkert.org    assert(getFlags(IsMainQueue));
1515606Snate@binkert.org    mainEventQueue.schedule(this, curTick + interval);
1522SN/A}
1532SN/A
1542SN/Aconst char *
1555336Shines@cs.fsu.eduCheckSwapEvent::description() const
1562SN/A{
1572SN/A    return "check swap";
1582SN/A}
159