2 * Copyright (c) 2002-2005 The Regents of The University of Michigan
3 * Copyright (c) 2011 Advanced Micro Devices, Inc.
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.
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.
29 * Authors: Nathan Binkert
34 #include "base/logging.hh"
35 #include "base/trace.hh"
36 #include "config/the_isa.hh"
37 #include "debug/TimeSync.hh"
38 #include "sim/eventq_impl.hh"
39 #include "sim/full_system.hh"
40 #include "sim/root.hh"
42 Root
*Root::_root
= NULL
;
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
53 Time cur_time
, diff
, period
= timeSyncPeriod();
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.
63 // Refresh the current time.
66 } while (diff
< period
);
68 schedule(&syncEvent
, curTick() + _periodTick
);
72 Root::timeSyncEnable(bool en
)
79 Tick periods
= ((curTick() + _periodTick
- 1) / _periodTick
);
80 Tick nextPeriod
= periods
* _periodTick
;
81 schedule(&syncEvent
, nextPeriod
);
84 deschedule(&syncEvent
);
88 /// Configure the period for time sync events.
90 Root::timeSyncPeriod(Time newPeriod
)
92 bool en
= timeSyncEnabled();
94 _periodTick
= _period
.getTick();
98 /// Set the threshold for time remaining to spin wait.
100 Root::timeSyncSpinThreshold(Time newThreshold
)
102 bool en
= timeSyncEnabled();
103 _spinThreshold
= newThreshold
;
107 Root::Root(RootParams
*p
)
108 : SimObject(p
), _enabled(false), _periodTick(p
->time_sync_period
),
109 syncEvent([this]{ timeSync(); }, name())
111 _period
.setTick(p
->time_sync_period
);
112 _spinThreshold
.setTick(p
->time_sync_spin_threshold
);
114 assert(_root
== NULL
);
118 simQuantum
= p
->sim_quantum
;
124 timeSyncEnable(params()->time_sync_enable
);
128 Root::serialize(CheckpointOut
&cp
) const
130 SERIALIZE_SCALAR(FullSystem
);
131 std::string isa
= THE_ISA_STR
;
132 SERIALIZE_SCALAR(isa
);
137 unsigned int FullSystemInt
;
142 static bool created
= false;
144 panic("only one root object allowed!");
148 FullSystem
= full_system
;
149 FullSystemInt
= full_system
? 1 : 0;
151 return new Root(this);