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

Revision 57, 4.1 KB checked in by art, 12 years ago (diff)
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;
25uint32 syncInterval = 500;
26uint32 syncTime = 10;
27
28
29//---------------------------------------------------------
30// Initialize the plugin
31//---------------------------------------------------------
32extern "C" int plgInitialize(Engine* root, PropertyList* args)
33{       
34        mRoot = root;
35    mOldTimeSource = mRoot->sClock()->getTimeSource();
36
37    if (args){
38        if (args->exists("sync_interval"))
39        {
40            Property& prop = (*args)["luastate"];
41            try{
42                syncInterval = prop.get<uint32>();
43                NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: sync_interval=%d", syncInterval);
44            }catch(boost::bad_any_cast& err){
45                NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: sync_interval is given, but has a bad type");
46            }
47        }
48        if (args->exists("sync_duration"))
49        {
50            Property& prop = (*args)["luastate"];
51            try{
52                syncTime = prop.get<uint32>();
53                NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: sync_duration=%d", syncTime);
54            }catch(boost::bad_any_cast& err){
55                NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: sync_duration is given, but has a bad type");
56            }
57        }
58    }
59   
60        // create cpu instance
61        mCpu.reset(new Cpu());
62        NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: Retrieve CPU Information");
63        mCpu->detect();
64        NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: %s", mCpu->getCPUDescription().c_str());
65       
66        // create Time Source
67        mTimeSource.reset( new rdtscTimeSource(mCpu) );
68    mTimeSource->setSyncDuration(syncTime);
69   
70    // check whenever CPU does support the thing
71    if (mTimeSource->isSupported())
72    {               
73            // now set new time source to the clock
74            NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: Set the high performance time source for the clock");
75            mRoot->sClock()->setTimeSource(mTimeSource);
76
77        // let sync the timesource every 500 ms
78        mRoot->sClock()->setSyncInterval(syncInterval);
79       
80            // declare new function to the scripting engine
81            //std::vector<ScriptParam> param;
82            //param.push_back(mCpu.get());
83            mRoot->sScriptEngine()->add("rdtscPlugin_getCpuString", Cpu::getCpuString, VarArg(mCpu.get()));
84        }else{
85        NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: Your CPU seems not to support the rdtsc instruction");
86    }   
87        return 0;       
88} 
89 
90
91//---------------------------------------------------------
92// Get full string according to the plugin version
93//---------------------------------------------------------
94extern "C" _PluginExport char* plgVersionString( void )
95{
96    return "High Performance Time Source v0.1 (rdtsc & cpuid instruction using)";
97}
98
99//---------------------------------------------------------
100// Get the engines version number for which this plugin was wrote
101//---------------------------------------------------------
102extern "C" unsigned int plgEngineVersion( void )
103{
104        return createVersionInteger(0,0,8);
105}
106
107
108//---------------------------------------------------------
109// Return a string according to the given error code
110//---------------------------------------------------------
111extern "C" char* plgError(int err)
112{
113        return "Unknown";
114}
115
116
117//---------------------------------------------------------
118// Release the plugin
119//---------------------------------------------------------
120extern "C" void plgRelease( void )
121{
122        // free pointers
123        mCpu.reset();
124        mTimeSource.reset();
125
126        // set the old time source to the clock back
127        mRoot->sClock()->setTimeSource(mOldTimeSource);
128    mRoot->sClock()->setSyncInterval(0);
129               
130        // some log
131        NR_Log(Log::LOG_PLUGIN, "rdtscTimeSource: Set old time source back and good bye!");
132}
133
Note: See TracBrowser for help on using the repository browser.