gem5 v24.0.0.0
Loading...
Searching...
No Matches
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
61namespace gem5
62{
63
64static pid_t
66{
67 return syscall(__NR_gettid);
68}
69
77static const uint64_t MIN_HOST_CYCLES = 1000;
78
79PosixKvmTimer::PosixKvmTimer(int signo, clockid_t clockID,
80 float hostFactor, Tick hostFreq)
81 : BaseKvmTimer(signo, hostFactor, hostFreq),
82 clockID(clockID)
83{
84 struct sigevent sev;
85
86 sev.sigev_notify = SIGEV_THREAD_ID;
87 sev.sigev_signo = signo;
88 sev.sigev_notify_thread_id = sysGettid();
89 sev.sigev_value.sival_ptr = NULL;
90
91 while (timer_create(clockID, &sev, &timer) == -1) {
92 if (errno != EAGAIN)
93 panic("timer_create: %i", errno);
94 }
95}
96
98{
99 timer_delete(timer);
100}
101
102void
104{
105 struct itimerspec ts;
106 memset(&ts, 0, sizeof(ts));
107
108 ts.it_interval.tv_sec = 0;
109 ts.it_interval.tv_nsec = 0;
110 ts.it_value.tv_sec = hostNs(ticks) / 1000000000ULL;
111 ts.it_value.tv_nsec = hostNs(ticks) % 1000000000ULL;
112
113 assert(ts.it_value.tv_nsec > 0 || ts.it_value.tv_sec > 0);
114
115 DPRINTF(KvmTimer, "Arming POSIX timer: %i ticks (%is%ins)\n",
116 ticks, ts.it_value.tv_sec, ts.it_value.tv_nsec);
117
118 if (timer_settime(timer, 0, &ts, NULL) == -1)
119 panic("PosixKvmTimer: Failed to arm timer\n");
120}
121
122void
124{
125 struct itimerspec ts;
126 memset(&ts, 0, sizeof(ts));
127
128 if (timer_settime(timer, 0, &ts, &prevTimerSpec) == -1)
129 panic("PosixKvmTimer: Failed to disarm timer\n");
130
131 DPRINTF(KvmTimer, "Disarmed POSIX timer: %is%ins left\n",
132 prevTimerSpec.it_value.tv_sec,
133 prevTimerSpec.it_value.tv_nsec);
134}
135
136bool
138{
139 return (prevTimerSpec.it_value.tv_nsec == 0 &&
140 prevTimerSpec.it_value.tv_sec == 0);
141}
142
143Tick
145{
146 struct timespec ts;
147
148 if (clock_getres(clockID, &ts) == -1)
149 panic("PosixKvmTimer: Failed to get timer resolution\n");
150
151 const uint64_t res_ns(ts.tv_sec * 1000000000ULL + ts.tv_nsec);
152 // We preferrably want ticksFromHostNs() to calculate the the
153 // ceiling rather than truncating the value. However, there are
154 // other cases where truncating is fine, so we just add 1 here to
155 // make sure that the actual resolution is strictly less than what
156 // we return. We could get all kinds of nasty behavior if
157 // arm(resolution) is called and the resulting time is 0 (which
158 // could happen if we truncate the results and the resolution is
159 // 1ns).
160 const Tick resolution(ticksFromHostNs(res_ns) + 1);
161 // It might not make sense to enter into KVM for less than a
162 // certain number of host cycles. In some systems (e.g., Linux)
163 // the resolution of the timer we use is 1ns (a few cycles on most
164 // CPUs), which isn't very useful.
165 const Tick min_cycles(ticksFromHostCycles(MIN_HOST_CYCLES));
166
167 return std::max(resolution, min_cycles);
168}
169
170
172 int signo, float hostFactor, Tick hostFreq)
173 : BaseKvmTimer(signo, hostFactor, hostFreq),
174 hwOverflow(ctr)
175{
177}
178
182
183void
189
190void
195
196Tick
201
202} // namespace gem5
#define DPRINTF(x,...)
Definition trace.hh:210
Timer functions to interrupt VM execution after a number of simulation ticks.
Definition timer.hh:64
Tick ticksFromHostNs(uint64_t ns)
Convert nanoseconds executed on the host into Ticks executed in the simulator.
Definition timer.hh:134
uint64_t hostCycles(Tick ticks)
Convert a time in simulator ticks to host cycles.
Definition timer.hh:162
Tick resolution()
Determine the resolution of the timer in ticks.
Definition timer.hh:110
int signo
Signal to deliver when the timer times out.
Definition timer.hh:167
uint64_t hostNs(Tick ticks)
Convert a time in simulator ticks to host nanoseconds.
Definition timer.hh:152
Tick ticksFromHostCycles(uint64_t cycles)
Convert cycles executed on the host into Ticks executed in the simulator.
Definition timer.hh:123
An instance of a performance counter.
Definition perfevent.hh:172
void period(uint64_t period)
Update the period of an overflow counter.
Definition perfevent.cc:124
void refresh(int refresh)
Enable a counter for a fixed number of events.
Definition perfevent.cc:131
void enableSignals(pid_t tid, int signal)
Enable signal delivery to a thread on counter overflow.
Definition perfevent.cc:147
void stop()
Stop counting.
Definition perfevent.cc:117
Tick calcResolution()
Calculate the timer resolution, used by resolution() which caches the result.
Definition timer.cc:197
void disarm()
Disarm the timer.
Definition timer.cc:191
PerfKvmCounter & hwOverflow
Definition timer.hh:250
PerfKvmTimer(PerfKvmCounter &ctr, int signo, float hostFactor, Tick hostFreq)
Create a timer that uses an existing hardware cycle counter.
Definition timer.cc:171
void arm(Tick ticks)
Arm the timer so that it fires after a certain number of ticks.
Definition timer.cc:184
bool expired() override
Definition timer.cc:137
clockid_t clockID
Definition timer.hh:208
void arm(Tick ticks) override
Arm the timer so that it fires after a certain number of ticks.
Definition timer.cc:103
PosixKvmTimer(int signo, clockid_t clockID, float hostFactor, Tick hostFreq)
Definition timer.cc:79
Tick calcResolution() override
Calculate the timer resolution, used by resolution() which caches the result.
Definition timer.cc:144
struct itimerspec prevTimerSpec
Definition timer.hh:210
void disarm() override
Disarm the timer.
Definition timer.cc:123
#define panic(...)
This implements a cprintf based panic() function.
Definition logging.hh:188
Bitfield< 55, 52 > ts
Copyright (c) 2024 - Pranith Kumar Copyright (c) 2020 Inria All rights reserved.
Definition binary32.hh:36
Bitfield< 14 > sev
uint64_t Tick
Tick count type.
Definition types.hh:58
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:77
static pid_t sysGettid()
Definition timer.cc:65

Generated on Tue Jun 18 2024 16:24:01 for gem5 by doxygen 1.11.0