timer.cc revision 9655:78c9adc85718
14486Sbinkertn@umich.edu/*
27897Shestness@cs.utexas.edu * Copyright (c) 2012 ARM Limited
34486Sbinkertn@umich.edu * All rights reserved
44486Sbinkertn@umich.edu *
54486Sbinkertn@umich.edu * The license below extends only to copyright in the software and shall
64486Sbinkertn@umich.edu * not be construed as granting a license to any other intellectual
74486Sbinkertn@umich.edu * property including but not limited to intellectual property relating
84486Sbinkertn@umich.edu * to a hardware implementation of the functionality of the software
94486Sbinkertn@umich.edu * licensed hereunder.  You may use the software subject to the license
104486Sbinkertn@umich.edu * terms below provided that you ensure that this notice is replicated
114486Sbinkertn@umich.edu * unmodified and in its entirety in all distributions of the software,
124486Sbinkertn@umich.edu * modified or unmodified, in source code or in binary form.
134486Sbinkertn@umich.edu *
144486Sbinkertn@umich.edu * Redistribution and use in source and binary forms, with or without
154486Sbinkertn@umich.edu * modification, are permitted provided that the following conditions are
164486Sbinkertn@umich.edu * met: redistributions of source code must retain the above copyright
174486Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer;
184486Sbinkertn@umich.edu * redistributions in binary form must reproduce the above copyright
194486Sbinkertn@umich.edu * notice, this list of conditions and the following disclaimer in the
204486Sbinkertn@umich.edu * documentation and/or other materials provided with the distribution;
214486Sbinkertn@umich.edu * neither the name of the copyright holders nor the names of its
224486Sbinkertn@umich.edu * contributors may be used to endorse or promote products derived from
234486Sbinkertn@umich.edu * this software without specific prior written permission.
244486Sbinkertn@umich.edu *
254486Sbinkertn@umich.edu * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
264486Sbinkertn@umich.edu * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
274486Sbinkertn@umich.edu * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
284486Sbinkertn@umich.edu * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
297897Shestness@cs.utexas.edu * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
304486Sbinkertn@umich.edu * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
313102SN/A * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
326654Snate@binkert.org * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
333102SN/A * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
343102SN/A * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
356654Snate@binkert.org * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
368931Sandreas.hansson@arm.com *
372212SN/A * Authors: Andreas Sandberg
389524SAndreas.Sandberg@ARM.com */
399524SAndreas.Sandberg@ARM.com
402902SN/A#include <csignal>
418703Sandreas.hansson@arm.com#include <ctime>
421783SN/A
439338SAndreas.Sandberg@arm.com#include "base/misc.hh"
448839Sandreas.hansson@arm.com#include "base/trace.hh"
457673Snate@binkert.org#include "cpu/kvm/timer.hh"
469281Sandreas.hansson@arm.com#include "debug/KvmTimer.hh"
479313Sandreas.hansson@arm.com
489313Sandreas.hansson@arm.com
499313Sandreas.hansson@arm.comPosixKvmTimer::PosixKvmTimer(int signo, clockid_t clockID,
509313Sandreas.hansson@arm.com                             float hostFactor, Tick hostFreq)
519281Sandreas.hansson@arm.com    : BaseKvmTimer(signo, hostFactor, hostFreq),
527673Snate@binkert.org      clockID(clockID)
538597Ssteve.reinhardt@amd.com{
548597Ssteve.reinhardt@amd.com    struct sigevent sev;
558597Ssteve.reinhardt@amd.com
568597Ssteve.reinhardt@amd.com    // TODO: We should request signal delivery to thread instead of
578597Ssteve.reinhardt@amd.com    // the process here. Unfortunately this seems to be broken, or at
588597Ssteve.reinhardt@amd.com    // least not work as specified in the man page.
599524SAndreas.Sandberg@ARM.com    sev.sigev_notify = SIGEV_SIGNAL;
608597Ssteve.reinhardt@amd.com    sev.sigev_signo = signo;
618597Ssteve.reinhardt@amd.com    sev.sigev_value.sival_ptr = NULL;
624859Snate@binkert.org    if (timer_create(clockID, &sev, &timer) == -1)
638931Sandreas.hansson@arm.com        panic("timer_create");
648931Sandreas.hansson@arm.com}
652902SN/A
669408Sandreas.hansson@arm.comPosixKvmTimer::~PosixKvmTimer()
679408Sandreas.hansson@arm.com{
689408Sandreas.hansson@arm.com    timer_delete(timer);
699408Sandreas.hansson@arm.com}
709408Sandreas.hansson@arm.com
719408Sandreas.hansson@arm.comvoid
727914SBrad.Beckmann@amd.comPosixKvmTimer::arm(Tick ticks)
738666SPrakash.Ramrakhyani@arm.com{
747914SBrad.Beckmann@amd.com    struct itimerspec ts;
757914SBrad.Beckmann@amd.com    memset(&ts, 0, sizeof(ts));
767914SBrad.Beckmann@amd.com
777914SBrad.Beckmann@amd.com    ts.it_interval.tv_sec = 0;
787914SBrad.Beckmann@amd.com    ts.it_interval.tv_nsec = 0;
797914SBrad.Beckmann@amd.com    ts.it_value.tv_sec = hostNs(ticks) / 1000000000ULL;
807914SBrad.Beckmann@amd.com    ts.it_value.tv_nsec = hostNs(ticks) % 1000000000ULL;
817914SBrad.Beckmann@amd.com
827914SBrad.Beckmann@amd.com    DPRINTF(KvmTimer, "Arming POSIX timer: %i ticks (%is%ins)\n",
837914SBrad.Beckmann@amd.com            ticks, ts.it_value.tv_sec, ts.it_value.tv_nsec);
847914SBrad.Beckmann@amd.com
857914SBrad.Beckmann@amd.com    if (timer_settime(timer, 0, &ts, NULL) == -1)
867914SBrad.Beckmann@amd.com        panic("PosixKvmTimer: Failed to arm timer\n");
878769Sgblack@eecs.umich.edu}
888769Sgblack@eecs.umich.edu
898769Sgblack@eecs.umich.eduvoid
908769Sgblack@eecs.umich.eduPosixKvmTimer::disarm()
918769Sgblack@eecs.umich.edu{
928769Sgblack@eecs.umich.edu    struct itimerspec ts;
938769Sgblack@eecs.umich.edu    memset(&ts, 0, sizeof(ts));
94
95    DPRINTF(KvmTimer, "Disarming POSIX timer\n");
96
97    if (timer_settime(timer, 0, &ts, NULL) == -1)
98        panic("PosixKvmTimer: Failed to disarm timer\n");
99}
100
101Tick
102PosixKvmTimer::calcResolution()
103{
104    struct timespec ts;
105
106    if (clock_getres(clockID, &ts) == -1)
107        panic("PosixKvmTimer: Failed to get timer resolution\n");
108
109    Tick resolution(ticksFromHostNs(ts.tv_sec * 1000000000ULL + ts.tv_nsec));
110
111    return resolution;
112}
113
114
115PerfKvmTimer::PerfKvmTimer(PerfKvmCounter &ctr,
116                           int signo, float hostFactor, Tick hostFreq)
117    : BaseKvmTimer(signo, hostFactor, hostFreq),
118      hwOverflow(ctr)
119{
120    hwOverflow.enableSignals(signo);
121}
122
123PerfKvmTimer::~PerfKvmTimer()
124{
125}
126
127void
128PerfKvmTimer::arm(Tick ticks)
129{
130    hwOverflow.period(hostCycles(ticks));
131    hwOverflow.refresh(1);
132}
133
134void
135PerfKvmTimer::disarm()
136{
137    hwOverflow.stop();
138}
139
140Tick
141PerfKvmTimer::calcResolution()
142{
143    // This is a bit arbitrary, but in practice, we can't really do
144    // anything useful in less than ~1000 anyway.
145    return ticksFromHostCycles(1000);
146}
147