root.cc (8332:23711432221f) root.cc (8801:1a84c6a81299)
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 "base/misc.hh"
35#include "debug/TimeSync.hh"
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 "base/misc.hh"
35#include "debug/TimeSync.hh"
36#include "sim/full_system.hh"
36#include "sim/root.hh"
37
38Root *Root::_root = NULL;
39
40/*
41 * This function is called periodically by an event in M5 and ensures that
42 * at least as much real time has passed between invocations as simulated time.
43 * If not, the function either sleeps, or if the difference is small enough
44 * spin waits.
45 */
46void
47Root::timeSync()
48{
49 Time cur_time, diff, period = timeSyncPeriod();
50
51 do {
52 cur_time.setTimer();
53 diff = cur_time - lastTime;
54 Time remainder = period - diff;
55 if (diff < period && remainder > _spinThreshold) {
56 DPRINTF(TimeSync, "Sleeping to sync with real time.\n");
57 // Sleep until the end of the period, or until a signal.
58 sleep(remainder);
59 // Refresh the current time.
60 cur_time.setTimer();
61 }
62 } while (diff < period);
63 lastTime = cur_time;
64 schedule(&syncEvent, curTick() + _periodTick);
65}
66
67void
68Root::timeSyncEnable(bool en)
69{
70 if (en == _enabled)
71 return;
72 _enabled = en;
73 if (_enabled) {
74 // Get event going.
75 Tick periods = ((curTick() + _periodTick - 1) / _periodTick);
76 Tick nextPeriod = periods * _periodTick;
77 schedule(&syncEvent, nextPeriod);
78 } else {
79 // Stop event.
80 deschedule(&syncEvent);
81 }
82}
83
84/// Configure the period for time sync events.
85void
86Root::timeSyncPeriod(Time newPeriod)
87{
88 bool en = timeSyncEnabled();
89 _period = newPeriod;
90 _periodTick = _period.getTick();
91 timeSyncEnable(en);
92}
93
94/// Set the threshold for time remaining to spin wait.
95void
96Root::timeSyncSpinThreshold(Time newThreshold)
97{
98 bool en = timeSyncEnabled();
99 _spinThreshold = newThreshold;
100 timeSyncEnable(en);
101}
102
103Root::Root(RootParams *p) : SimObject(p), _enabled(false),
104 _periodTick(p->time_sync_period), syncEvent(this)
105{
106 _period.setTick(p->time_sync_period);
107 _spinThreshold.setTick(p->time_sync_spin_threshold);
108
109 assert(_root == NULL);
110 _root = this;
111 lastTime.setTimer();
112}
113
114void
115Root::initState()
116{
117 timeSyncEnable(params()->time_sync_enable);
118}
119
120void
121Root::loadState(Checkpoint *cp)
122{
123 timeSyncEnable(params()->time_sync_enable);
124}
125
37#include "sim/root.hh"
38
39Root *Root::_root = NULL;
40
41/*
42 * This function is called periodically by an event in M5 and ensures that
43 * at least as much real time has passed between invocations as simulated time.
44 * If not, the function either sleeps, or if the difference is small enough
45 * spin waits.
46 */
47void
48Root::timeSync()
49{
50 Time cur_time, diff, period = timeSyncPeriod();
51
52 do {
53 cur_time.setTimer();
54 diff = cur_time - lastTime;
55 Time remainder = period - diff;
56 if (diff < period && remainder > _spinThreshold) {
57 DPRINTF(TimeSync, "Sleeping to sync with real time.\n");
58 // Sleep until the end of the period, or until a signal.
59 sleep(remainder);
60 // Refresh the current time.
61 cur_time.setTimer();
62 }
63 } while (diff < period);
64 lastTime = cur_time;
65 schedule(&syncEvent, curTick() + _periodTick);
66}
67
68void
69Root::timeSyncEnable(bool en)
70{
71 if (en == _enabled)
72 return;
73 _enabled = en;
74 if (_enabled) {
75 // Get event going.
76 Tick periods = ((curTick() + _periodTick - 1) / _periodTick);
77 Tick nextPeriod = periods * _periodTick;
78 schedule(&syncEvent, nextPeriod);
79 } else {
80 // Stop event.
81 deschedule(&syncEvent);
82 }
83}
84
85/// Configure the period for time sync events.
86void
87Root::timeSyncPeriod(Time newPeriod)
88{
89 bool en = timeSyncEnabled();
90 _period = newPeriod;
91 _periodTick = _period.getTick();
92 timeSyncEnable(en);
93}
94
95/// Set the threshold for time remaining to spin wait.
96void
97Root::timeSyncSpinThreshold(Time newThreshold)
98{
99 bool en = timeSyncEnabled();
100 _spinThreshold = newThreshold;
101 timeSyncEnable(en);
102}
103
104Root::Root(RootParams *p) : SimObject(p), _enabled(false),
105 _periodTick(p->time_sync_period), syncEvent(this)
106{
107 _period.setTick(p->time_sync_period);
108 _spinThreshold.setTick(p->time_sync_spin_threshold);
109
110 assert(_root == NULL);
111 _root = this;
112 lastTime.setTimer();
113}
114
115void
116Root::initState()
117{
118 timeSyncEnable(params()->time_sync_enable);
119}
120
121void
122Root::loadState(Checkpoint *cp)
123{
124 timeSyncEnable(params()->time_sync_enable);
125}
126
127bool FullSystem;
128
126Root *
127RootParams::create()
128{
129 static bool created = false;
130 if (created)
131 panic("only one root object allowed!");
132
133 created = true;
134
129Root *
130RootParams::create()
131{
132 static bool created = false;
133 if (created)
134 panic("only one root object allowed!");
135
136 created = true;
137
138 FullSystem = full_system;
139
135 return new Root(this);
136}
140 return new Root(this);
141}