gem5  v20.1.0.0
timer.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012 ARM Limited
3  * All rights reserved
4  *
5  * The license below extends only to copyright in the software and shall
6  * not be construed as granting a license to any other intellectual
7  * property including but not limited to intellectual property relating
8  * to a hardware implementation of the functionality of the software
9  * licensed hereunder. You may use the software subject to the license
10  * terms below provided that you ensure that this notice is replicated
11  * unmodified and in its entirety in all distributions of the software,
12  * modified or unmodified, in source code or in binary form.
13  *
14  * Redistribution and use in source and binary forms, with or without
15  * modification, are permitted provided that the following conditions are
16  * met: redistributions of source code must retain the above copyright
17  * notice, this list of conditions and the following disclaimer;
18  * redistributions in binary form must reproduce the above copyright
19  * notice, this list of conditions and the following disclaimer in the
20  * documentation and/or other materials provided with the distribution;
21  * neither the name of the copyright holders nor the names of its
22  * contributors may be used to endorse or promote products derived from
23  * this software without specific prior written permission.
24  *
25  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
26  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
27  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
28  * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
29  * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
30  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
31  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
32  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
33  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
34  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
35  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36  */
37 
38 #include "cpu/kvm/timer.hh"
39 
40 #include <sys/syscall.h>
41 #include <unistd.h>
42 
43 #include <algorithm>
44 #include <csignal>
45 #include <ctime>
46 
47 #include "base/logging.hh"
48 #include "base/trace.hh"
49 #include "debug/KvmTimer.hh"
50 
51 /* According to timer_create(2), the value SIGEV_THREAD_ID can be used
52  * to specify which thread a timer signal gets delivered to. According
53  * to the man page, the member sigev_notify_thread is used to specify
54  * the TID. This member is currently not defined by default in
55  * siginfo.h on x86, so we define it here as a workaround.
56  */
57 #ifndef sigev_notify_thread_id
58 #define sigev_notify_thread_id _sigev_un._tid
59 #endif
60 
61 static pid_t
63 {
64  return syscall(__NR_gettid);
65 }
66 
74 static const uint64_t MIN_HOST_CYCLES = 1000;
75 
76 PosixKvmTimer::PosixKvmTimer(int signo, clockid_t clockID,
77  float hostFactor, Tick hostFreq)
78  : BaseKvmTimer(signo, hostFactor, hostFreq),
79  clockID(clockID)
80 {
81  struct sigevent sev;
82 
83  sev.sigev_notify = SIGEV_THREAD_ID;
84  sev.sigev_signo = signo;
85  sev.sigev_notify_thread_id = sysGettid();
86  sev.sigev_value.sival_ptr = NULL;
87 
88  while (timer_create(clockID, &sev, &timer) == -1) {
89  if (errno != EAGAIN)
90  panic("timer_create: %i", errno);
91  }
92 }
93 
95 {
96  timer_delete(timer);
97 }
98 
99 void
101 {
102  struct itimerspec ts;
103  memset(&ts, 0, sizeof(ts));
104 
105  ts.it_interval.tv_sec = 0;
106  ts.it_interval.tv_nsec = 0;
107  ts.it_value.tv_sec = hostNs(ticks) / 1000000000ULL;
108  ts.it_value.tv_nsec = hostNs(ticks) % 1000000000ULL;
109 
110  assert(ts.it_value.tv_nsec > 0 || ts.it_value.tv_sec > 0);
111 
112  DPRINTF(KvmTimer, "Arming POSIX timer: %i ticks (%is%ins)\n",
113  ticks, ts.it_value.tv_sec, ts.it_value.tv_nsec);
114 
115  if (timer_settime(timer, 0, &ts, NULL) == -1)
116  panic("PosixKvmTimer: Failed to arm timer\n");
117 }
118 
119 void
121 {
122  struct itimerspec ts;
123  memset(&ts, 0, sizeof(ts));
124 
125  if (timer_settime(timer, 0, &ts, &prevTimerSpec) == -1)
126  panic("PosixKvmTimer: Failed to disarm timer\n");
127 
128  DPRINTF(KvmTimer, "Disarmed POSIX timer: %is%ins left\n",
129  prevTimerSpec.it_value.tv_sec,
130  prevTimerSpec.it_value.tv_nsec);
131 }
132 
133 bool
135 {
136  return (prevTimerSpec.it_value.tv_nsec == 0 &&
137  prevTimerSpec.it_value.tv_sec == 0);
138 }
139 
140 Tick
142 {
143  struct timespec ts;
144 
145  if (clock_getres(clockID, &ts) == -1)
146  panic("PosixKvmTimer: Failed to get timer resolution\n");
147 
148  const uint64_t res_ns(ts.tv_sec * 1000000000ULL + ts.tv_nsec);
149  // We preferrably want ticksFromHostNs() to calculate the the
150  // ceiling rather than truncating the value. However, there are
151  // other cases where truncating is fine, so we just add 1 here to
152  // make sure that the actual resolution is strictly less than what
153  // we return. We could get all kinds of nasty behavior if
154  // arm(resolution) is called and the resulting time is 0 (which
155  // could happen if we truncate the results and the resolution is
156  // 1ns).
157  const Tick resolution(ticksFromHostNs(res_ns) + 1);
158  // It might not make sense to enter into KVM for less than a
159  // certain number of host cycles. In some systems (e.g., Linux)
160  // the resolution of the timer we use is 1ns (a few cycles on most
161  // CPUs), which isn't very useful.
162  const Tick min_cycles(ticksFromHostCycles(MIN_HOST_CYCLES));
163 
164  return std::max(resolution, min_cycles);
165 }
166 
167 
169  int signo, float hostFactor, Tick hostFreq)
170  : BaseKvmTimer(signo, hostFactor, hostFreq),
171  hwOverflow(ctr)
172 {
174 }
175 
177 {
178 }
179 
180 void
182 {
183  hwOverflow.period(hostCycles(ticks));
184  hwOverflow.refresh(1);
185 }
186 
187 void
189 {
190  hwOverflow.stop();
191 }
192 
193 Tick
195 {
197 }
PerfKvmTimer::arm
void arm(Tick ticks)
Arm the timer so that it fires after a certain number of ticks.
Definition: timer.cc:181
PerfKvmCounter
An instance of a performance counter.
Definition: perfevent.hh:168
PosixKvmTimer::timer
timer_t timer
Definition: timer.hh:206
PerfKvmTimer::disarm
void disarm()
Disarm the timer.
Definition: timer.cc:188
PosixKvmTimer::~PosixKvmTimer
~PosixKvmTimer()
Definition: timer.cc:94
BaseKvmTimer::ticksFromHostNs
Tick ticksFromHostNs(uint64_t ns)
Convert nanoseconds executed on the host into Ticks executed in the simulator.
Definition: timer.hh:131
BaseKvmTimer::ticksFromHostCycles
Tick ticksFromHostCycles(uint64_t cycles)
Convert cycles executed on the host into Ticks executed in the simulator.
Definition: timer.hh:120
PerfKvmTimer::~PerfKvmTimer
~PerfKvmTimer()
Definition: timer.cc:176
PosixKvmTimer::arm
void arm(Tick ticks) override
Arm the timer so that it fires after a certain number of ticks.
Definition: timer.cc:100
BaseKvmTimer::hostNs
uint64_t hostNs(Tick ticks)
Convert a time in simulator ticks to host nanoseconds.
Definition: timer.hh:149
PerfKvmTimer::PerfKvmTimer
PerfKvmTimer(PerfKvmCounter &ctr, int signo, float hostFactor, Tick hostFreq)
Create a timer that uses an existing hardware cycle counter.
Definition: timer.cc:168
Tick
uint64_t Tick
Tick count type.
Definition: types.hh:63
PosixKvmTimer::prevTimerSpec
struct itimerspec prevTimerSpec
Definition: timer.hh:207
PosixKvmTimer::disarm
void disarm() override
Disarm the timer.
Definition: timer.cc:120
PerfKvmTimer::calcResolution
Tick calcResolution()
Calculate the timer resolution, used by resolution() which caches the result.
Definition: timer.cc:194
sysGettid
static pid_t sysGettid()
Definition: timer.cc:62
PerfKvmCounter::period
void period(uint64_t period)
Update the period of an overflow counter.
Definition: perfevent.cc:121
PerfKvmCounter::enableSignals
void enableSignals(pid_t tid, int signal)
Enable signal delivery to a thread on counter overflow.
Definition: perfevent.cc:144
ArmISA::ts
Bitfield< 55, 52 > ts
Definition: miscregs_types.hh:89
BaseKvmTimer::signo
int signo
Signal to deliver when the timer times out.
Definition: timer.hh:164
DPRINTF
#define DPRINTF(x,...)
Definition: trace.hh:234
PosixKvmTimer::PosixKvmTimer
PosixKvmTimer(int signo, clockid_t clockID, float hostFactor, Tick hostFreq)
Definition: timer.cc:76
MIN_HOST_CYCLES
static const uint64_t MIN_HOST_CYCLES
Minimum number of cycles that a host can spend in a KVM call (used to calculate the resolution of som...
Definition: timer.cc:74
PosixKvmTimer::calcResolution
Tick calcResolution() override
Calculate the timer resolution, used by resolution() which caches the result.
Definition: timer.cc:141
BaseKvmTimer
Timer functions to interrupt VM execution after a number of simulation ticks.
Definition: timer.hh:60
timer.hh
PosixKvmTimer::clockID
clockid_t clockID
Definition: timer.hh:205
PerfKvmCounter::stop
void stop()
Stop counting.
Definition: perfevent.cc:114
BaseKvmTimer::resolution
Tick resolution()
Determine the resolution of the timer in ticks.
Definition: timer.hh:107
PosixKvmTimer::expired
bool expired() override
Definition: timer.cc:134
PerfKvmTimer::hwOverflow
PerfKvmCounter & hwOverflow
Definition: timer.hh:247
logging.hh
trace.hh
BaseKvmTimer::hostCycles
uint64_t hostCycles(Tick ticks)
Convert a time in simulator ticks to host cycles.
Definition: timer.hh:159
ULL
#define ULL(N)
uint64_t constant
Definition: types.hh:50
panic
#define panic(...)
This implements a cprintf based panic() function.
Definition: logging.hh:171
PerfKvmCounter::refresh
void refresh(int refresh)
Enable a counter for a fixed number of events.
Definition: perfevent.cc:128

Generated on Wed Sep 30 2020 14:02:08 for gem5 by doxygen 1.8.17