rainmeter-studio/Plugins/PluginPerfMon/PerfData.cpp

209 lines
4.6 KiB
C++
Raw Normal View History

2009-02-10 18:37:48 +00:00
/*
Copyright (C) 2004 Kimmo Pekkola
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
2009-02-10 18:37:48 +00:00
*/
#include <windows.h>
2012-03-17 14:20:51 +00:00
#include <vector>
#include "Titledb.h"
#include "PerfSnap.h"
#include "PerfObj.h"
#include "PerfCntr.h"
#include "ObjList.h"
#include "ObjInst.h"
#include "../API/RainmeterAPI.h"
#include "../../Library/RawString.h"
2011-02-03 18:09:24 +00:00
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
2012-03-17 14:20:51 +00:00
struct MeasureData
2009-02-10 18:37:48 +00:00
{
2012-03-17 14:20:51 +00:00
CRawString objectName;
CRawString counterName;
CRawString instanceName;
ULONGLONG oldValue;
bool difference;
bool firstTime;
MeasureData() :
oldValue(),
difference(false),
firstTime(true)
{
}
2009-02-10 18:37:48 +00:00
};
2012-03-17 14:20:51 +00:00
ULONGLONG GetPerfData(PCTSTR ObjectName, PCTSTR InstanceName, PCTSTR CounterName);
2009-02-10 18:37:48 +00:00
2012-03-17 14:20:51 +00:00
PLUGIN_EXPORT void Initialize(void** data, void* rm)
2009-02-10 18:37:48 +00:00
{
2012-03-17 14:20:51 +00:00
MeasureData* measure = new MeasureData;
*data = measure;
}
2009-02-10 18:37:48 +00:00
2012-03-17 14:20:51 +00:00
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
MeasureData* measure = (MeasureData*)data;
bool changed = false;
2009-02-10 18:37:48 +00:00
LPCWSTR value = RmReadString(rm, L"PerfMonObject", L"");
if (_wcsicmp(value, measure->objectName.c_str()) != 0)
{
measure->objectName = value;
changed = true;
}
value = RmReadString(rm, L"PerfMonCounter", L"");
if (_wcsicmp(value, measure->counterName.c_str()) != 0)
{
measure->counterName = value;
changed = true;
}
value = RmReadString(rm, L"PerfMonInstance", L"");
if (_wcsicmp(value, measure->instanceName.c_str()) != 0)
{
measure->instanceName = value;
changed = true;
}
bool diff = RmReadInt(rm, L"PerfMonDifference", 1) == 1;
if (diff != measure->difference)
{
measure->difference = diff;
changed = true;
}
if (changed)
{
measure->oldValue = 0;
measure->firstTime = true;
}
*maxValue = 0.0;
2009-02-10 18:37:48 +00:00
}
2012-03-17 14:20:51 +00:00
PLUGIN_EXPORT double Update(void* data)
2009-02-10 18:37:48 +00:00
{
2012-03-17 14:20:51 +00:00
MeasureData* measure = (MeasureData*)data;
double value = 0;
2009-02-10 18:37:48 +00:00
2012-03-17 14:20:51 +00:00
ULONGLONG longValue = GetPerfData(
measure->objectName.c_str(),
measure->instanceName.c_str(),
measure->counterName.c_str());
2009-02-10 18:37:48 +00:00
2012-03-17 14:20:51 +00:00
if (measure->difference)
{
// Compare with the old value
if (!measure->firstTime)
2009-02-10 18:37:48 +00:00
{
2012-03-17 14:20:51 +00:00
value = (double)(longValue - measure->oldValue);
2009-02-10 18:37:48 +00:00
}
2012-03-17 14:20:51 +00:00
measure->oldValue = longValue;
measure->firstTime = false;
}
else
{
value = (double)longValue;
2009-02-10 18:37:48 +00:00
}
return value;
}
2012-03-17 14:20:51 +00:00
PLUGIN_EXPORT void Finalize(void* data)
2009-02-10 18:37:48 +00:00
{
2012-03-17 14:20:51 +00:00
MeasureData* measure = (MeasureData*)data;
delete measure;
2009-02-10 18:37:48 +00:00
CPerfSnapshot::CleanUp();
}
2012-03-17 14:20:51 +00:00
ULONGLONG GetPerfData(LPCWSTR objectName, LPCWSTR instanceName, LPCWSTR counterName)
2009-02-10 18:37:48 +00:00
{
2012-03-17 14:20:51 +00:00
static CPerfTitleDatabase s_CounterTitles(PERF_TITLE_COUNTER);
2009-02-10 18:37:48 +00:00
BYTE data[256];
WCHAR name[256];
ULONGLONG value = 0;
2012-03-17 14:20:51 +00:00
CPerfSnapshot snapshot(&s_CounterTitles);
CPerfObjectList objList(&snapshot, &s_CounterTitles);
2009-02-10 18:37:48 +00:00
2012-03-17 14:20:51 +00:00
if (snapshot.TakeSnapshot(objectName))
2009-02-10 18:37:48 +00:00
{
2012-03-17 14:20:51 +00:00
CPerfObject* pPerfObj = objList.GetPerfObject(objectName);
2009-02-10 18:37:48 +00:00
2011-03-29 19:21:57 +00:00
if (pPerfObj)
2009-02-10 18:37:48 +00:00
{
2012-03-17 14:20:51 +00:00
for (CPerfObjectInstance* pObjInst = pPerfObj->GetFirstObjectInstance();
2009-02-10 18:37:48 +00:00
pObjInst != NULL;
pObjInst = pPerfObj->GetNextObjectInstance())
{
2012-03-17 14:20:51 +00:00
if (*instanceName)
2009-02-10 18:37:48 +00:00
{
2011-03-29 19:21:57 +00:00
if (pObjInst->GetObjectInstanceName(name, 256))
2009-02-10 18:37:48 +00:00
{
2012-03-17 14:20:51 +00:00
if (_wcsicmp(instanceName, name) != 0)
2009-02-10 18:37:48 +00:00
{
delete pObjInst;
continue;
}
}
else
{
delete pObjInst;
continue;
}
}
2012-03-17 14:20:51 +00:00
CPerfCounter* pPerfCntr = pObjInst->GetCounterByName(counterName);
2011-03-29 19:21:57 +00:00
if (pPerfCntr != NULL)
2009-02-10 18:37:48 +00:00
{
pPerfCntr->GetData(data, 256, NULL);
2011-03-29 19:21:57 +00:00
if (pPerfCntr->GetSize() == 1)
2009-02-10 18:37:48 +00:00
{
value = *(BYTE*)data;
2011-03-29 19:21:57 +00:00
}
else if (pPerfCntr->GetSize() == 2)
2009-02-10 18:37:48 +00:00
{
value = *(WORD*)data;
}
2011-03-29 19:21:57 +00:00
else if (pPerfCntr->GetSize() == 4)
2009-02-10 18:37:48 +00:00
{
value = *(DWORD*)data;
}
2011-03-29 19:21:57 +00:00
else if (pPerfCntr->GetSize() == 8)
2009-02-10 18:37:48 +00:00
{
value = *(ULONGLONG*)data;
}
delete pPerfCntr;
delete pObjInst;
break; // No need to continue
}
2012-03-17 14:20:51 +00:00
2009-02-10 18:37:48 +00:00
delete pObjInst;
}
2012-03-17 14:20:51 +00:00
2009-02-10 18:37:48 +00:00
delete pPerfObj;
}
}
return value;
}