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

Revision 56, 2.8 KB checked in by art, 12 years ago (diff)

User can specify the timings needed for synchronisation of the rdtscTimeSource Plugin

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    _syncDuration = 10;
34       
35        // check whenever we have got the high performance counter support
36        if (mCpu->isRDTSC())
37        {
38                NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: It seems your CPU supports the \"rdtsc\" instruction");
39                NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: High perfromance time counter will be used!");
40        _isSupported = true;     
41        }
42}
43
44//------------------------------------------------------------------------
45rdtscTimeSource::~rdtscTimeSource()
46{
47
48}
49       
50
51//------------------------------------------------------------------------
52float64 rdtscTimeSource::getTime()
53{
54        // get current time
55        if (mCpu->isRDTSC())
56    {
57                // retrive current ticks number
58                uint64 now = _startTimeRT;
59                mCpu->rdtsc(now);
60
61                // some precalculation
62                //float64 a =  1.0 / static_cast<float64>(mCpu->getSpeed());
63                //float64 b = static_cast<float64>(now - _startTimeRT);
64               
65                uint64 b = now - _startTimeRT;
66                               
67                // calculate current time in seconds
68                _currentTime = (0.001 * float64(b)) / float64(mCpu->getSpeed()) + _resetTime + _syncTime;
69        }
70       
71        // return it back
72        return _currentTime;
73}
74
75//------------------------------------------------------------------------
76void rdtscTimeSource::reset(float64 startValue)
77{
78        // check whenever we have got the RDTSC support
79        if (mCpu->isRDTSC()){
80                mCpu->rdtsc(_startTimeRT);
81        }
82        _resetTime = startValue;
83}
84
85//------------------------------------------------------------------------
86void rdtscTimeSource::sync()
87{
88        _syncTime = _currentTime;
89        reset();
90        mCpu->calculateSpeed(_syncDuration);
91}
92
Note: See TracBrowser for help on using the repository browser.