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