root.cc (11077:fae097742b7e) root.cc (11793:ef606668d247)
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2011 Advanced Micro Devices, Inc.
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
1/*
2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2011 Advanced Micro Devices, Inc.
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 "sim/root.hh"
35
34#include "base/misc.hh"
35#include "base/trace.hh"
36#include "config/the_isa.hh"
37#include "debug/TimeSync.hh"
38#include "sim/full_system.hh"
36#include "base/misc.hh"
37#include "base/trace.hh"
38#include "config/the_isa.hh"
39#include "debug/TimeSync.hh"
40#include "sim/full_system.hh"
39#include "sim/root.hh"
40
41Root *Root::_root = NULL;
42
43/*
44 * This function is called periodically by an event in M5 and ensures that
45 * at least as much real time has passed between invocations as simulated time.
46 * If not, the function either sleeps, or if the difference is small enough
47 * spin waits.
48 */
49void
50Root::timeSync()
51{
52 Time cur_time, diff, period = timeSyncPeriod();
53
54 do {
55 cur_time.setTimer();
56 diff = cur_time - lastTime;
57 Time remainder = period - diff;
58 if (diff < period && remainder > _spinThreshold) {
59 DPRINTF(TimeSync, "Sleeping to sync with real time.\n");
60 // Sleep until the end of the period, or until a signal.
61 sleep(remainder);
62 // Refresh the current time.
63 cur_time.setTimer();
64 }
65 } while (diff < period);
66 lastTime = cur_time;
67 schedule(&syncEvent, curTick() + _periodTick);
68}
69
70void
71Root::timeSyncEnable(bool en)
72{
73 if (en == _enabled)
74 return;
75 _enabled = en;
76 if (_enabled) {
77 // Get event going.
78 Tick periods = ((curTick() + _periodTick - 1) / _periodTick);
79 Tick nextPeriod = periods * _periodTick;
80 schedule(&syncEvent, nextPeriod);
81 } else {
82 // Stop event.
83 deschedule(&syncEvent);
84 }
85}
86
87/// Configure the period for time sync events.
88void
89Root::timeSyncPeriod(Time newPeriod)
90{
91 bool en = timeSyncEnabled();
92 _period = newPeriod;
93 _periodTick = _period.getTick();
94 timeSyncEnable(en);
95}
96
97/// Set the threshold for time remaining to spin wait.
98void
99Root::timeSyncSpinThreshold(Time newThreshold)
100{
101 bool en = timeSyncEnabled();
102 _spinThreshold = newThreshold;
103 timeSyncEnable(en);
104}
105
106Root::Root(RootParams *p) : SimObject(p), _enabled(false),
107 _periodTick(p->time_sync_period), syncEvent(this)
108{
109 _period.setTick(p->time_sync_period);
110 _spinThreshold.setTick(p->time_sync_spin_threshold);
111
112 assert(_root == NULL);
113 _root = this;
114 lastTime.setTimer();
115
116 simQuantum = p->sim_quantum;
117}
118
119void
120Root::initState()
121{
122 timeSyncEnable(params()->time_sync_enable);
123}
124
125void
126Root::loadState(CheckpointIn &cp)
127{
128 SimObject::loadState(cp);
129 timeSyncEnable(params()->time_sync_enable);
130}
131
132void
133Root::serialize(CheckpointOut &cp) const
134{
135 SERIALIZE_SCALAR(FullSystem);
136 std::string isa = THE_ISA_STR;
137 SERIALIZE_SCALAR(isa);
138}
139
140void
141Root::unserialize(CheckpointIn &cp)
142{}
143
144
145bool FullSystem;
146unsigned int FullSystemInt;
147
148Root *
149RootParams::create()
150{
151 static bool created = false;
152 if (created)
153 panic("only one root object allowed!");
154
155 created = true;
156
157 FullSystem = full_system;
158 FullSystemInt = full_system ? 1 : 0;
159
160 return new Root(this);
161}
41
42Root *Root::_root = NULL;
43
44/*
45 * This function is called periodically by an event in M5 and ensures that
46 * at least as much real time has passed between invocations as simulated time.
47 * If not, the function either sleeps, or if the difference is small enough
48 * spin waits.
49 */
50void
51Root::timeSync()
52{
53 Time cur_time, diff, period = timeSyncPeriod();
54
55 do {
56 cur_time.setTimer();
57 diff = cur_time - lastTime;
58 Time remainder = period - diff;
59 if (diff < period && remainder > _spinThreshold) {
60 DPRINTF(TimeSync, "Sleeping to sync with real time.\n");
61 // Sleep until the end of the period, or until a signal.
62 sleep(remainder);
63 // Refresh the current time.
64 cur_time.setTimer();
65 }
66 } while (diff < period);
67 lastTime = cur_time;
68 schedule(&syncEvent, curTick() + _periodTick);
69}
70
71void
72Root::timeSyncEnable(bool en)
73{
74 if (en == _enabled)
75 return;
76 _enabled = en;
77 if (_enabled) {
78 // Get event going.
79 Tick periods = ((curTick() + _periodTick - 1) / _periodTick);
80 Tick nextPeriod = periods * _periodTick;
81 schedule(&syncEvent, nextPeriod);
82 } else {
83 // Stop event.
84 deschedule(&syncEvent);
85 }
86}
87
88/// Configure the period for time sync events.
89void
90Root::timeSyncPeriod(Time newPeriod)
91{
92 bool en = timeSyncEnabled();
93 _period = newPeriod;
94 _periodTick = _period.getTick();
95 timeSyncEnable(en);
96}
97
98/// Set the threshold for time remaining to spin wait.
99void
100Root::timeSyncSpinThreshold(Time newThreshold)
101{
102 bool en = timeSyncEnabled();
103 _spinThreshold = newThreshold;
104 timeSyncEnable(en);
105}
106
107Root::Root(RootParams *p) : SimObject(p), _enabled(false),
108 _periodTick(p->time_sync_period), syncEvent(this)
109{
110 _period.setTick(p->time_sync_period);
111 _spinThreshold.setTick(p->time_sync_spin_threshold);
112
113 assert(_root == NULL);
114 _root = this;
115 lastTime.setTimer();
116
117 simQuantum = p->sim_quantum;
118}
119
120void
121Root::initState()
122{
123 timeSyncEnable(params()->time_sync_enable);
124}
125
126void
127Root::loadState(CheckpointIn &cp)
128{
129 SimObject::loadState(cp);
130 timeSyncEnable(params()->time_sync_enable);
131}
132
133void
134Root::serialize(CheckpointOut &cp) const
135{
136 SERIALIZE_SCALAR(FullSystem);
137 std::string isa = THE_ISA_STR;
138 SERIALIZE_SCALAR(isa);
139}
140
141void
142Root::unserialize(CheckpointIn &cp)
143{}
144
145
146bool FullSystem;
147unsigned int FullSystemInt;
148
149Root *
150RootParams::create()
151{
152 static bool created = false;
153 if (created)
154 panic("only one root object allowed!");
155
156 created = true;
157
158 FullSystem = full_system;
159 FullSystemInt = full_system ? 1 : 0;
160
161 return new Root(this);
162}