source: trunk/Plugins/rdtscTimeSource/TimeSource.cpp @ 55

Revision 55, 2.8 KB checked in by art, 12 years ago (diff)
  • rdtscTimerSource plugin added
  • NOTE: Be carefull on systems with dynamicly changeable frequencies, since the rdtsc instruction may produce wrong results
Line 
1/***************************************************************************
2 *                                                                         *
3 *   (c) Art Tevs, MPI Informatik Saarbruecken                             *
4 *       mailto: <tevs@mpi-sb.mpg.de>                                      *
5 *                                                                         *
6 *   This program is free software; you can redistribute it and/or modify  *
7 *   it under the terms of the GNU General Public License as published by  *
8 *   the Free Software Foundation; either version 2 of the License, or     *
9 *   (at your option) any later version.                                   *
10 *                                                                         *
11 ***************************************************************************/
12
13//----------------------------------------------------------------------------------
14// Includes
15//----------------------------------------------------------------------------------
16#include "TimeSource.h"
17#include "Cpu.h"
18#include <time.h>
19
20
21#if NR_PLATFORM != NR_PLATFORM_WIN32
22#    include <sys/time.h>
23#endif
24
25               
26using namespace nrEngine;
27
28//------------------------------------------------------------------------
29rdtscTimeSource::rdtscTimeSource(boost::shared_ptr<Cpu> cpu) : TimeSource()
30{
31        mCpu = cpu;
32    _isSupported = false;
33       
34        // check whenever we have got the high performance counter support
35        if (mCpu->isRDTSC())
36        {
37                NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: It seems your CPU supports the \"rdtsc\" instruction");
38                NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: High perfromance time counter will be used!");
39        _isSupported = true;     
40        }
41}
42
43//------------------------------------------------------------------------
44rdtscTimeSource::~rdtscTimeSource()
45{
46
47}
48       
49
50//------------------------------------------------------------------------
51float64 rdtscTimeSource::getTime()
52{
53        // get current time
54        if (mCpu->isRDTSC())
55    {
56                // retrive current ticks number
57                uint64 now = _startTimeRT;
58                mCpu->rdtsc(now);
59
60                // some precalculation
61                //float64 a =  1.0 / static_cast<float64>(mCpu->getSpeed());
62                //float64 b = static_cast<float64>(now - _startTimeRT);
63               
64                uint64 b = now - _startTimeRT;
65                               
66                // calculate current time in seconds
67                _currentTime = (0.001 * float64(b)) / float64(mCpu->getSpeed()) + _resetTime + _syncTime;
68        }
69       
70        // return it back
71        return _currentTime;
72}
73
74//------------------------------------------------------------------------
75void rdtscTimeSource::reset(float64 startValue)
76{
77        // check whenever we have got the RDTSC support
78        if (mCpu->isRDTSC()){
79                mCpu->rdtsc(_startTimeRT);
80        }
81        _resetTime = startValue;
82}
83
84//------------------------------------------------------------------------
85void rdtscTimeSource::sync()
86{
87        _syncTime = _currentTime;
88        reset();
89        mCpu->calculateSpeed(10);
90}
91
Note: See TracBrowser for help on using the repository browser.