root.cc revision 7942:c122a3e1b204
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2011 Advanced Micro Devices
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met: redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer;
10 * redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution;
13 * neither the name of the copyright holders nor the names of its
14 * contributors may be used to endorse or promote products derived from
15 * this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Authors: Nathan Binkert
30 *          Steve Reinhardt
31 *          Gabe Black
32 */
33
34#include "base/misc.hh"
35#include "sim/root.hh"
36
37Root *Root::_root = NULL;
38
39/*
40 * This function is called periodically by an event in M5 and ensures that
41 * at least as much real time has passed between invocations as simulated time.
42 * If not, the function either sleeps, or if the difference is small enough
43 * spin waits.
44 */
45void
46Root::timeSync()
47{
48    Time cur_time, diff, period = timeSyncPeriod();
49
50    do {
51        cur_time.setTimer();
52        diff = cur_time - lastTime;
53        Time remainder = period - diff;
54        if (diff < period && remainder > _spinThreshold) {
55            DPRINTF(TimeSync, "Sleeping to sync with real time.\n");
56            // Sleep until the end of the period, or until a signal.
57            sleep(remainder);
58            // Refresh the current time.
59            cur_time.setTimer();
60        }
61    } while (diff < period);
62    lastTime = cur_time;
63    schedule(&syncEvent, curTick() + _periodTick);
64}
65
66void
67Root::timeSyncEnable(bool en)
68{
69    if (en == _enabled)
70        return;
71    _enabled = en;
72    if (_enabled) {
73        // Get event going.
74        Tick periods = ((curTick() + _periodTick - 1) / _periodTick);
75        Tick nextPeriod = periods * _periodTick;
76        schedule(&syncEvent, nextPeriod);
77    } else {
78        // Stop event.
79        deschedule(&syncEvent);
80    }
81}
82
83/// Configure the period for time sync events.
84void
85Root::timeSyncPeriod(Time newPeriod)
86{
87    bool en = timeSyncEnabled();
88    _period = newPeriod;
89    _periodTick = _period.getTick();
90    timeSyncEnable(en);
91}
92
93/// Set the threshold for time remaining to spin wait.
94void
95Root::timeSyncSpinThreshold(Time newThreshold)
96{
97    bool en = timeSyncEnabled();
98    _spinThreshold = newThreshold;
99    timeSyncEnable(en);
100}
101
102Root::Root(RootParams *p) : SimObject(p), _enabled(false),
103    _periodTick(p->time_sync_period), syncEvent(this)
104{
105    _period.setTick(p->time_sync_period);
106    _spinThreshold.setTick(p->time_sync_spin_threshold);
107
108    assert(_root == NULL);
109    _root = this;
110    lastTime.setTimer();
111}
112
113void
114Root::initState()
115{
116    timeSyncEnable(params()->time_sync_enable);
117}
118
119void
120Root::loadState(Checkpoint *cp)
121{
122    timeSyncEnable(params()->time_sync_enable);
123}
124
125Root *
126RootParams::create()
127{
128    static bool created = false;
129    if (created)
130        panic("only one root object allowed!");
131
132    created = true;
133
134    return new Root(this);
135}
136