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

Revision 55, 3.1 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#include <nrEngine/nrEngine.h>
2
3#if NR_PLATFORM == NR_PLATFORM_WIN32
4    #define _PluginExport __declspec( dllexport )
5#else
6    #define _PluginExport
7#endif
8
9#include "Cpu.h"
10#include "TimeSource.h"
11
12
13using namespace nrEngine;
14
15//---------------------------------------------------------
16// Some globals
17//---------------------------------------------------------
18Engine*                                                         mRoot = NULL;
19
20boost::shared_ptr<Cpu>                          mCpu;
21boost::shared_ptr<rdtscTimeSource>      mTimeSource;
22boost::shared_ptr<TimeSource>           mOldTimeSource;
23
24//std::vector<Plugin::PlgMethod>                mMethods;
25
26//---------------------------------------------------------
27// Initialize the plugin
28//---------------------------------------------------------
29extern "C" int plgInitialize(Engine* root)
30{       
31        mRoot = root;
32    mOldTimeSource = mRoot->sClock()->getTimeSource();
33
34       
35        // create cpu instance
36        mCpu.reset(new Cpu());
37        NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: Retrieve CPU Information");
38        mCpu->detect();
39        NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: %s", mCpu->getCPUDescription().c_str());
40       
41        // create Time Source
42        mTimeSource.reset( new rdtscTimeSource(mCpu) );
43
44    // check whenever CPU does support the thing
45    if (mTimeSource->isSupported())
46    {               
47            // now set new time source to the clock
48            NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: Set the high performance time source for the clock");
49            mRoot->sClock()->setTimeSource(mTimeSource);
50
51        // let sync the timesource every 500 ms
52        mRoot->sClock()->setSyncInterval(500);
53       
54            // declare new function to the scripting engine
55            //std::vector<ScriptParam> param;
56            //param.push_back(mCpu.get());
57            mRoot->sScriptEngine()->add("rdtscPlugin_getCpuString", Cpu::getCpuString, VarArg(mCpu.get()));
58        }else{
59        NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: Your CPU seems not to support the rdtsc instruction");
60    }   
61        return 0;       
62} 
63 
64
65//---------------------------------------------------------
66// Get full string according to the plugin version
67//---------------------------------------------------------
68extern "C" _PluginExport char* plgVersionString( void )
69{
70    return "High Performance Time Source v0.1 (rdtsc & cpuid instruction using)";
71}
72
73//---------------------------------------------------------
74// Get the engines version number for which this plugin was wrote
75//---------------------------------------------------------
76extern "C" unsigned int plgEngineVersion( void )
77{
78        return createVersionInteger(0,0,8);
79}
80
81
82//---------------------------------------------------------
83// Return a string according to the given error code
84//---------------------------------------------------------
85extern "C" char* plgError(int err)
86{
87        return "Unknown";
88}
89
90
91//---------------------------------------------------------
92// Release the plugin
93//---------------------------------------------------------
94extern "C" void plgRelease( void )
95{
96        // free pointers
97        mCpu.reset();
98        mTimeSource.reset();
99
100        // set the old time source to the clock back
101        mRoot->sClock()->setTimeSource(mOldTimeSource);
102    mRoot->sClock()->setSyncInterval(0);
103               
104        // some log
105        NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: Set old time source back and good bye!");
106}
107
Note: See TracBrowser for help on using the repository browser.