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

Revision 55, 3.4 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#ifndef _PLUGIN_RDTSC_TIMESOURCE_H_
15#define _PLUGIN_RDTSC_TIMESOURCE_H_
16
17//----------------------------------------------------------------------------------
18// Includes
19//----------------------------------------------------------------------------------
20#include <nrEngine/nrEngine.h>
21#include "Cpu.h"
22
23
24using namespace nrEngine;
25
26
27/**
28* This is a derived class of time source which provide the functionality
29* of rdtsc and cpuid instructions to get the time.
30* Use this as Plugin for the engine.
31* \par
32* Because we are using high definition timers in our engine, so each time
33* step depends on the current cpu frequency. If you retrive a wrong speed
34* by initialization of the clock, you will get wrong time calculations
35* along whole time calculation process. So sometimes we need to recalculate
36* the speed of used cpu (i.e. CPU/FSB speed stepping)<br>
37* To do this our clock has a function named sync(). This will snyc the current
38* clock with a system time and will force the CPU class to recalculate it's speed.
39* Syncing is done automatically every user defined time step. Sou you can decide
40* how important the high performance time for your application is.
41**/
42class rdtscTimeSource : public TimeSource {
43public:
44       
45        /**
46        * Create an instance of the time source object.
47        **/
48        rdtscTimeSource(boost::shared_ptr<Cpu> cpu);
49
50        /**
51        * Release used memory
52        **/
53        ~rdtscTimeSource();
54       
55        /**
56        * Get the current time in seconds. Returned time is not the same as system time.
57        * @return current time in seconds
58        **/
59        float64 getTime();
60
61       
62        /**
63        * Reset the time source, so it can start reading the time from the beginning.
64        * You do not have to call this method, clock will do it automaticaly, if
65        * it is needed.
66        **/
67        void reset(float64 startValue = 0.0);
68
69        /**
70        * Syncing the time source means we get the current time store it as sync time point
71        * caluclate the new start point do all calculations again.
72        * This syncing can help us to sync clock for example if the cpu speed changes.
73        * If no new calculation about the cpu speed is done and we use rdtsc to compute
74        * the current time, so we will get wrong time steps. Syncing should prevent this.<br>
75        * Clock-Engine will decide by itself when to sync, so time source shouldn't do it.
76        **/
77        void sync();
78
79    /**
80     * Check if the rdtsc instruction is supported
81     **/
82    bool isSupported() { return _isSupported; }
83       
84private:
85
86    //! Is supported
87    bool _isSupported;
88   
89        //! Store the start time as 64Bit integer, for rtdsc counter
90        uint64  _startTimeRT;
91
92        //! Cpu to get info
93        boost::shared_ptr<Cpu>  mCpu;
94               
95};
96
97#endif
Note: See TracBrowser for help on using the repository browser.