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

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

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

RevLine 
[55]1#include <nrEngine/nrEngine.h>
[1]2
[55]3#if NR_PLATFORM == NR_PLATFORM_WIN32
4    #define _PluginExport __declspec( dllexport )
5#else
6    #define _PluginExport
7#endif
[1]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;
[56]25uint32 syncInterval = 0;
26uint32 syncTime = 10;
[1]27
[56]28
[1]29//---------------------------------------------------------
30// Initialize the plugin
31//---------------------------------------------------------
[56]32extern "C" int plgInitialize(Engine* root, PropertyList* args)
[55]33{       
[1]34        mRoot = root;
[55]35    mOldTimeSource = mRoot->sClock()->getTimeSource();
36
[56]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   
[1]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) );
[56]68    mTimeSource->setSyncDuration(syncTime);
69   
[55]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);
[1]76
[55]77        // let sync the timesource every 500 ms
[56]78        mRoot->sClock()->setSyncInterval(syncInterval);
[55]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    }   
[1]87        return 0;       
88} 
89 
90
91//---------------------------------------------------------
[55]92// Get full string according to the plugin version
[1]93//---------------------------------------------------------
[55]94extern "C" _PluginExport char* plgVersionString( void )
[1]95{
[55]96    return "High Performance Time Source v0.1 (rdtsc & cpuid instruction using)";
[1]97}
98
99//---------------------------------------------------------
[55]100// Get the engines version number for which this plugin was wrote
[1]101//---------------------------------------------------------
[55]102extern "C" unsigned int plgEngineVersion( void )
[1]103{
[55]104        return createVersionInteger(0,0,8);
[1]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);
[55]128    mRoot->sClock()->setSyncInterval(0);
[1]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.