2007-03-03 07:24:00 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2006 The Regents of The University of Michigan
|
2013-11-25 18:21:00 +01:00
|
|
|
* Copyright (c) 2013 Advanced Micro Devices, Inc.
|
|
|
|
* Copyright (c) 2013 Mark D. Hill and David A. Wood
|
2007-03-03 07:24:00 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions are
|
|
|
|
* met: redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer;
|
|
|
|
* redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution;
|
|
|
|
* neither the name of the copyright holders nor the names of its
|
|
|
|
* contributors may be used to endorse or promote products derived from
|
|
|
|
* this software without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
|
|
|
|
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* Authors: Nathan Binkert
|
|
|
|
* Steve Reinhardt
|
|
|
|
*/
|
|
|
|
|
2013-11-25 18:21:00 +01:00
|
|
|
#include <mutex>
|
|
|
|
#include <thread>
|
|
|
|
|
2007-03-03 07:24:00 +01:00
|
|
|
#include "base/misc.hh"
|
|
|
|
#include "base/pollevent.hh"
|
2009-05-17 23:34:52 +02:00
|
|
|
#include "base/types.hh"
|
2007-03-03 07:24:00 +01:00
|
|
|
#include "sim/async.hh"
|
2012-11-16 17:27:47 +01:00
|
|
|
#include "sim/eventq_impl.hh"
|
2007-03-03 07:24:00 +01:00
|
|
|
#include "sim/sim_events.hh"
|
|
|
|
#include "sim/sim_exit.hh"
|
|
|
|
#include "sim/simulate.hh"
|
2009-05-17 23:34:52 +02:00
|
|
|
#include "sim/stat_control.hh"
|
2007-03-03 07:24:00 +01:00
|
|
|
|
2013-11-25 18:21:00 +01:00
|
|
|
//! Mutex for handling async events.
|
|
|
|
std::mutex asyncEventMutex;
|
|
|
|
|
|
|
|
//! Global barrier for synchronizing threads entering/exiting the
|
|
|
|
//! simulation loop.
|
|
|
|
Barrier *threadBarrier;
|
|
|
|
|
|
|
|
//! forward declaration
|
|
|
|
Event *doSimLoop(EventQueue *);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The main function for all subordinate threads (i.e., all threads
|
|
|
|
* other than the main thread). These threads start by waiting on
|
|
|
|
* threadBarrier. Once all threads have arrived at threadBarrier,
|
|
|
|
* they enter the simulation loop concurrently. When they exit the
|
|
|
|
* loop, they return to waiting on threadBarrier. This process is
|
|
|
|
* repeated until the simulation terminates.
|
|
|
|
*/
|
|
|
|
static void
|
|
|
|
thread_loop(EventQueue *queue)
|
|
|
|
{
|
|
|
|
while (true) {
|
|
|
|
threadBarrier->wait();
|
|
|
|
doSimLoop(queue);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-23 11:57:36 +01:00
|
|
|
GlobalEvent*
|
|
|
|
getLimitEvent(void) {
|
|
|
|
static GlobalSimLoopExitEvent
|
|
|
|
simulate_limit_event(mainEventQueue[0]->getCurTick(),
|
|
|
|
"simulate() limit reached", 0);
|
|
|
|
return &simulate_limit_event;
|
|
|
|
}
|
|
|
|
|
2007-03-03 07:24:00 +01:00
|
|
|
/** Simulate for num_cycles additional cycles. If num_cycles is -1
|
|
|
|
* (the default), do not limit simulation; some other event must
|
|
|
|
* terminate the loop. Exported to Python via SWIG.
|
|
|
|
* @return The SimLoopExitEvent that caused the loop to exit.
|
|
|
|
*/
|
2013-11-25 18:21:00 +01:00
|
|
|
GlobalSimLoopExitEvent *
|
2007-03-03 07:24:00 +01:00
|
|
|
simulate(Tick num_cycles)
|
|
|
|
{
|
2013-11-25 18:21:00 +01:00
|
|
|
// The first time simulate() is called from the Python code, we need to
|
|
|
|
// create a thread for each of event queues referenced by the
|
|
|
|
// instantiated sim objects.
|
|
|
|
static bool threads_initialized = false;
|
|
|
|
static std::vector<std::thread *> threads;
|
|
|
|
|
|
|
|
if (!threads_initialized) {
|
|
|
|
threadBarrier = new Barrier(numMainEventQueues);
|
|
|
|
|
|
|
|
// the main thread (the one we're currently running on)
|
|
|
|
// handles queue 0, so we only need to allocate new threads
|
|
|
|
// for queues 1..N-1. We'll call these the "subordinate" threads.
|
|
|
|
for (uint32_t i = 1; i < numMainEventQueues; i++) {
|
|
|
|
threads.push_back(new std::thread(thread_loop, mainEventQueue[i]));
|
|
|
|
}
|
|
|
|
|
|
|
|
threads_initialized = true;
|
|
|
|
}
|
|
|
|
|
2011-01-08 06:50:29 +01:00
|
|
|
inform("Entering event queue @ %d. Starting simulation...\n", curTick());
|
2007-03-03 07:24:00 +01:00
|
|
|
|
2012-08-28 02:53:20 +02:00
|
|
|
if (num_cycles < MaxTick - curTick())
|
2011-01-08 06:50:29 +01:00
|
|
|
num_cycles = curTick() + num_cycles;
|
2012-08-28 02:53:20 +02:00
|
|
|
else // counter would roll over or be set to MaxTick anyhow
|
|
|
|
num_cycles = MaxTick;
|
2007-03-03 07:24:00 +01:00
|
|
|
|
2015-03-23 11:57:36 +01:00
|
|
|
getLimitEvent()->reschedule(num_cycles);
|
2013-11-25 18:21:00 +01:00
|
|
|
|
|
|
|
GlobalSyncEvent *quantum_event = NULL;
|
|
|
|
if (numMainEventQueues > 1) {
|
|
|
|
if (simQuantum == 0) {
|
|
|
|
fatal("Quantum for multi-eventq simulation not specified");
|
|
|
|
}
|
|
|
|
|
2014-03-06 15:59:53 +01:00
|
|
|
quantum_event = new GlobalSyncEvent(curTick() + simQuantum, simQuantum,
|
2013-11-25 18:21:00 +01:00
|
|
|
EventBase::Progress_Event_Pri, 0);
|
|
|
|
|
|
|
|
inParallelMode = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// all subordinate (created) threads should be waiting on the
|
|
|
|
// barrier; the arrival of the main thread here will satisfy the
|
|
|
|
// barrier, and all threads will enter doSimLoop in parallel
|
|
|
|
threadBarrier->wait();
|
|
|
|
Event *local_event = doSimLoop(mainEventQueue[0]);
|
|
|
|
assert(local_event != NULL);
|
|
|
|
|
|
|
|
inParallelMode = false;
|
|
|
|
|
|
|
|
// locate the global exit event and return it to Python
|
|
|
|
BaseGlobalEvent *global_event = local_event->globalEvent();
|
|
|
|
assert(global_event != NULL);
|
|
|
|
|
|
|
|
GlobalSimLoopExitEvent *global_exit_event =
|
|
|
|
dynamic_cast<GlobalSimLoopExitEvent *>(global_event);
|
|
|
|
assert(global_exit_event != NULL);
|
|
|
|
|
|
|
|
//! Delete the simulation quantum event.
|
|
|
|
if (quantum_event != NULL) {
|
|
|
|
quantum_event->deschedule();
|
|
|
|
delete quantum_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
return global_exit_event;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test and clear the global async_event flag, such that each time the
|
|
|
|
* flag is cleared, only one thread returns true (and thus is assigned
|
|
|
|
* to handle the corresponding async event(s)).
|
|
|
|
*/
|
|
|
|
static bool
|
|
|
|
testAndClearAsyncEvent()
|
|
|
|
{
|
|
|
|
bool was_set = false;
|
|
|
|
asyncEventMutex.lock();
|
|
|
|
|
|
|
|
if (async_event) {
|
|
|
|
was_set = true;
|
|
|
|
async_event = false;
|
|
|
|
}
|
|
|
|
|
|
|
|
asyncEventMutex.unlock();
|
|
|
|
return was_set;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The main per-thread simulation loop. This loop is executed by all
|
|
|
|
* simulation threads (the main thread and the subordinate threads) in
|
|
|
|
* parallel.
|
|
|
|
*/
|
|
|
|
Event *
|
|
|
|
doSimLoop(EventQueue *eventq)
|
|
|
|
{
|
|
|
|
// set the per thread current eventq pointer
|
|
|
|
curEventQueue(eventq);
|
|
|
|
eventq->handleAsyncInsertions();
|
2007-03-03 07:24:00 +01:00
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// there should always be at least one event (the SimLoopExitEvent
|
|
|
|
// we just scheduled) in the queue
|
2013-11-25 18:21:00 +01:00
|
|
|
assert(!eventq->empty());
|
|
|
|
assert(curTick() <= eventq->nextTick() &&
|
2007-03-03 07:24:00 +01:00
|
|
|
"event scheduled in the past");
|
|
|
|
|
2013-11-25 18:21:00 +01:00
|
|
|
if (async_event && testAndClearAsyncEvent()) {
|
sim: Add the ability to lock and migrate between event queues
We need the ability to lock event queues to enable device accesses
across threads. The serviceOne() method now takes a service lock prior
to handling a new event. By locking an event queue, a different
thread/eq can effectively execute in the context of the locked event
queue. To simplify temporary event queue migrations, this changeset
introduces the EventQueue::ScopedMigration class that unlocks the
current event queue, locks a new event queue, and updates the current
event queue variable.
In order to prevent deadlocks, event queues need to be released when
waiting on barriers. This is implemented using the
EventQueue::ScopedRelease class. An instance of this class is, for
example, used in the BaseGlobalEvent class to release the event queue
when waiting on the synchronization barrier.
The intended use for this functionality is when devices need to be
accessed across thread boundaries. For example, when fast-forwarding,
it might be useful to run devices and CPUs in separate threads. In
such a case, the CPU locks the device queue whenever it needs to
perform IO. This functionality is primarily intended for KVM.
Note: Migrating between event queues can lead to non-deterministic
timing. Use with extreme care!
--HG--
extra : rebase_source : 23e3a741a1fd73861d1339782dbbe1bc76285315
2014-04-03 11:22:49 +02:00
|
|
|
// Take the event queue lock in case any of the service
|
|
|
|
// routines want to schedule new events.
|
|
|
|
std::lock_guard<EventQueue> lock(*eventq);
|
2007-03-03 07:24:00 +01:00
|
|
|
if (async_statdump || async_statreset) {
|
2011-01-08 06:50:29 +01:00
|
|
|
Stats::schedStatEvent(async_statdump, async_statreset);
|
2007-03-03 07:24:00 +01:00
|
|
|
async_statdump = false;
|
|
|
|
async_statreset = false;
|
|
|
|
}
|
|
|
|
|
2013-11-29 14:36:10 +01:00
|
|
|
if (async_io) {
|
2007-03-03 07:24:00 +01:00
|
|
|
async_io = false;
|
|
|
|
pollQueue.service();
|
|
|
|
}
|
|
|
|
|
2014-12-19 22:32:34 +01:00
|
|
|
if (async_exit) {
|
|
|
|
async_exit = false;
|
|
|
|
exitSimLoop("user interrupt received");
|
|
|
|
}
|
|
|
|
|
2007-03-03 07:24:00 +01:00
|
|
|
if (async_exception) {
|
|
|
|
async_exception = false;
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2014-12-19 22:32:34 +01:00
|
|
|
|
|
|
|
Event *exit_event = eventq->serviceOne();
|
|
|
|
if (exit_event != NULL) {
|
|
|
|
return exit_event;
|
|
|
|
}
|
2007-03-03 07:24:00 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// not reached... only exit is return on SimLoopExitEvent
|
|
|
|
}
|