timer.cc (10073:2360411a16be) timer.cc (11793:ef606668d247)
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 * Authors: Andreas Sandberg
38 */
39
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 * Authors: Andreas Sandberg
38 */
39
40#include "cpu/kvm/timer.hh"
41
42#include <sys/syscall.h>
43#include <unistd.h>
44
40#include <algorithm>
41#include <csignal>
42#include <ctime>
45#include <algorithm>
46#include <csignal>
47#include <ctime>
43#include <unistd.h>
44#include <sys/syscall.h>
45
46#include "base/misc.hh"
47#include "base/trace.hh"
48
49#include "base/misc.hh"
50#include "base/trace.hh"
48#include "cpu/kvm/timer.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
61static pid_t
62gettid()
63{
64 return syscall(__NR_gettid);
65}
66
67/**
68 * Minimum number of cycles that a host can spend in a KVM call (used
69 * to calculate the resolution of some timers).
70 *
71 * The value of this constant is a bit arbitrary, but in practice, we
72 * can't really do anything useful in less than ~1000 cycles.
73 */
74static const uint64_t MIN_HOST_CYCLES = 1000;
75
76PosixKvmTimer::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 = gettid();
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
94PosixKvmTimer::~PosixKvmTimer()
95{
96 timer_delete(timer);
97}
98
99void
100PosixKvmTimer::arm(Tick ticks)
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
119void
120PosixKvmTimer::disarm()
121{
122 struct itimerspec ts;
123 memset(&ts, 0, sizeof(ts));
124
125 DPRINTF(KvmTimer, "Disarming POSIX timer\n");
126
127 if (timer_settime(timer, 0, &ts, NULL) == -1)
128 panic("PosixKvmTimer: Failed to disarm timer\n");
129}
130
131Tick
132PosixKvmTimer::calcResolution()
133{
134 struct timespec ts;
135
136 if (clock_getres(clockID, &ts) == -1)
137 panic("PosixKvmTimer: Failed to get timer resolution\n");
138
139 const uint64_t res_ns(ts.tv_sec * 1000000000ULL + ts.tv_nsec);
140 // We preferrably want ticksFromHostNs() to calculate the the
141 // ceiling rather than truncating the value. However, there are
142 // other cases where truncating is fine, so we just add 1 here to
143 // make sure that the actual resolution is strictly less than what
144 // we return. We could get all kinds of nasty behavior if
145 // arm(resolution) is called and the resulting time is 0 (which
146 // could happen if we truncate the results and the resolution is
147 // 1ns).
148 const Tick resolution(ticksFromHostNs(res_ns) + 1);
149 // It might not make sense to enter into KVM for less than a
150 // certain number of host cycles. In some systems (e.g., Linux)
151 // the resolution of the timer we use is 1ns (a few cycles on most
152 // CPUs), which isn't very useful.
153 const Tick min_cycles(ticksFromHostCycles(MIN_HOST_CYCLES));
154
155 return std::max(resolution, min_cycles);
156}
157
158
159PerfKvmTimer::PerfKvmTimer(PerfKvmCounter &ctr,
160 int signo, float hostFactor, Tick hostFreq)
161 : BaseKvmTimer(signo, hostFactor, hostFreq),
162 hwOverflow(ctr)
163{
164 hwOverflow.enableSignals(signo);
165}
166
167PerfKvmTimer::~PerfKvmTimer()
168{
169}
170
171void
172PerfKvmTimer::arm(Tick ticks)
173{
174 hwOverflow.period(hostCycles(ticks));
175 hwOverflow.refresh(1);
176}
177
178void
179PerfKvmTimer::disarm()
180{
181 hwOverflow.stop();
182}
183
184Tick
185PerfKvmTimer::calcResolution()
186{
187 return ticksFromHostCycles(MIN_HOST_CYCLES);
188}
51#include "debug/KvmTimer.hh"
52
53/* According to timer_create(2), the value SIGEV_THREAD_ID can be used
54 * to specify which thread a timer signal gets delivered to. According
55 * to the man page, the member sigev_notify_thread is used to specify
56 * the TID. This member is currently not defined by default in
57 * siginfo.h on x86, so we define it here as a workaround.
58 */
59#ifndef sigev_notify_thread_id
60#define sigev_notify_thread_id _sigev_un._tid
61#endif
62
63static pid_t
64gettid()
65{
66 return syscall(__NR_gettid);
67}
68
69/**
70 * Minimum number of cycles that a host can spend in a KVM call (used
71 * to calculate the resolution of some timers).
72 *
73 * The value of this constant is a bit arbitrary, but in practice, we
74 * can't really do anything useful in less than ~1000 cycles.
75 */
76static const uint64_t MIN_HOST_CYCLES = 1000;
77
78PosixKvmTimer::PosixKvmTimer(int signo, clockid_t clockID,
79 float hostFactor, Tick hostFreq)
80 : BaseKvmTimer(signo, hostFactor, hostFreq),
81 clockID(clockID)
82{
83 struct sigevent sev;
84
85 sev.sigev_notify = SIGEV_THREAD_ID;
86 sev.sigev_signo = signo;
87 sev.sigev_notify_thread_id = gettid();
88 sev.sigev_value.sival_ptr = NULL;
89
90 while (timer_create(clockID, &sev, &timer) == -1) {
91 if (errno != EAGAIN)
92 panic("timer_create: %i", errno);
93 }
94}
95
96PosixKvmTimer::~PosixKvmTimer()
97{
98 timer_delete(timer);
99}
100
101void
102PosixKvmTimer::arm(Tick ticks)
103{
104 struct itimerspec ts;
105 memset(&ts, 0, sizeof(ts));
106
107 ts.it_interval.tv_sec = 0;
108 ts.it_interval.tv_nsec = 0;
109 ts.it_value.tv_sec = hostNs(ticks) / 1000000000ULL;
110 ts.it_value.tv_nsec = hostNs(ticks) % 1000000000ULL;
111
112 assert(ts.it_value.tv_nsec > 0 || ts.it_value.tv_sec > 0);
113
114 DPRINTF(KvmTimer, "Arming POSIX timer: %i ticks (%is%ins)\n",
115 ticks, ts.it_value.tv_sec, ts.it_value.tv_nsec);
116
117 if (timer_settime(timer, 0, &ts, NULL) == -1)
118 panic("PosixKvmTimer: Failed to arm timer\n");
119}
120
121void
122PosixKvmTimer::disarm()
123{
124 struct itimerspec ts;
125 memset(&ts, 0, sizeof(ts));
126
127 DPRINTF(KvmTimer, "Disarming POSIX timer\n");
128
129 if (timer_settime(timer, 0, &ts, NULL) == -1)
130 panic("PosixKvmTimer: Failed to disarm timer\n");
131}
132
133Tick
134PosixKvmTimer::calcResolution()
135{
136 struct timespec ts;
137
138 if (clock_getres(clockID, &ts) == -1)
139 panic("PosixKvmTimer: Failed to get timer resolution\n");
140
141 const uint64_t res_ns(ts.tv_sec * 1000000000ULL + ts.tv_nsec);
142 // We preferrably want ticksFromHostNs() to calculate the the
143 // ceiling rather than truncating the value. However, there are
144 // other cases where truncating is fine, so we just add 1 here to
145 // make sure that the actual resolution is strictly less than what
146 // we return. We could get all kinds of nasty behavior if
147 // arm(resolution) is called and the resulting time is 0 (which
148 // could happen if we truncate the results and the resolution is
149 // 1ns).
150 const Tick resolution(ticksFromHostNs(res_ns) + 1);
151 // It might not make sense to enter into KVM for less than a
152 // certain number of host cycles. In some systems (e.g., Linux)
153 // the resolution of the timer we use is 1ns (a few cycles on most
154 // CPUs), which isn't very useful.
155 const Tick min_cycles(ticksFromHostCycles(MIN_HOST_CYCLES));
156
157 return std::max(resolution, min_cycles);
158}
159
160
161PerfKvmTimer::PerfKvmTimer(PerfKvmCounter &ctr,
162 int signo, float hostFactor, Tick hostFreq)
163 : BaseKvmTimer(signo, hostFactor, hostFreq),
164 hwOverflow(ctr)
165{
166 hwOverflow.enableSignals(signo);
167}
168
169PerfKvmTimer::~PerfKvmTimer()
170{
171}
172
173void
174PerfKvmTimer::arm(Tick ticks)
175{
176 hwOverflow.period(hostCycles(ticks));
177 hwOverflow.refresh(1);
178}
179
180void
181PerfKvmTimer::disarm()
182{
183 hwOverflow.stop();
184}
185
186Tick
187PerfKvmTimer::calcResolution()
188{
189 return ticksFromHostCycles(MIN_HOST_CYCLES);
190}