AdvancedCPU.dll: Updated to new API.

This commit is contained in:
spx 2012-04-18 23:40:27 -07:00
parent 7415ed0889
commit a96ebff1ee
3 changed files with 137 additions and 241 deletions

View File

@ -17,43 +17,50 @@
*/ */
#include <windows.h> #include <windows.h>
#include "AdvancedCPU.h"
#include <string>
#include <vector> #include <vector>
#include <map> #include "../PluginPerfMon/Titledb.h"
#include "../../Library/Export.h" // Rainmeter's exported functions #include "../PluginPerfMon/PerfSnap.h"
#include "../PluginPerfMon/PerfObj.h"
#include "../PluginPerfMon/PerfCntr.h"
#include "../PluginPerfMon/ObjList.h"
#include "../PluginPerfMon/ObjInst.h"
#include "../API/RainmeterAPI.h"
#include "../../Library/RawString.h"
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point #include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
//ULONGLONG GetPerfData(PCTSTR ObjectName, PCTSTR InstanceName, PCTSTR CounterName); struct MeasureData
void UpdateProcesses();
struct CPUMeasure
{ {
std::vector< std::wstring > includes; std::vector<CRawString> includes;
std::vector< std::wstring > excludes; std::vector<CRawString> excludes;
CRawString includesCache;
CRawString excludesCache;
int topProcess; int topProcess;
std::wstring topProcessName; CRawString topProcessName;
LONGLONG topProcessValue; LONGLONG topProcessValue;
MeasureData() :
topProcess(-1),
topProcessValue()
{
}
}; };
struct ProcessValues struct ProcessValues
{ {
std::wstring name; CRawString name;
LONGLONG oldValue; LONGLONG oldValue;
LONGLONG newValue; LONGLONG newValue;
bool found; bool found;
}; };
static CPerfTitleDatabase g_CounterTitles( PERF_TITLE_COUNTER ); static CPerfTitleDatabase g_CounterTitles(PERF_TITLE_COUNTER);
std::vector< ProcessValues > g_Processes; std::vector<ProcessValues> g_Processes;
static std::map<UINT, CPUMeasure*> g_CPUMeasures;
void SplitName(WCHAR* names, std::vector< std::wstring >& splittedNames) void UpdateProcesses();
void SplitName(WCHAR* names, std::vector<CRawString>& splittedNames)
{ {
WCHAR* token; WCHAR* token = wcstok(names, L";");
token = wcstok(names, L";");
while (token != NULL) while (token != NULL)
{ {
splittedNames.push_back(token); splittedNames.push_back(token);
@ -61,60 +68,61 @@ void SplitName(WCHAR* names, std::vector< std::wstring >& splittedNames)
} }
} }
/* PLUGIN_EXPORT void Initialize(void** data, void* rm)
This function is called when the measure is initialized.
The function must return the maximum value that can be measured.
The return value can also be 0, which means that Rainmeter will
track the maximum value automatically. The parameters for this
function are:
instance The instance of this DLL
iniFile The name of the ini-file (usually Rainmeter.ini)
section The name of the section in the ini-file for this measure
id The identifier for the measure. This is used to identify the measures that use the same plugin.
*/
UINT Initialize(HMODULE instance, LPCTSTR iniFile, LPCTSTR section, UINT id)
{ {
WCHAR buffer[4096]; MeasureData* measure = new MeasureData;
CPUMeasure* measure = new CPUMeasure; *data = measure;
measure->topProcess = 0;
measure->topProcessValue = 0;
LPCTSTR data = ReadConfigString(section, L"CPUInclude", L"");
if (data)
{
wcsncpy(buffer, data, 4096);
buffer[4095] = 0;
SplitName(buffer, measure->includes);
}
data = ReadConfigString(section, L"CPUExclude", L"");
if (data)
{
wcsncpy(buffer, data, 4096);
buffer[4095] = 0;
SplitName(buffer, measure->excludes);
}
measure->topProcess = 0;
data = ReadConfigString(section, L"TopProcess", L"0");
if (data)
{
measure->topProcess = _wtoi(data);
}
g_CPUMeasures[id] = measure;
return 10000000; // The values are 100 * 100000
} }
bool CheckProcess(CPUMeasure* measure, const std::wstring& name) PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
{
MeasureData* measure = (MeasureData*)data;
bool changed = false;
LPCWSTR value = RmReadString(rm, L"CPUInclude", L"");
if (_wcsicmp(value, measure->includesCache.c_str()) != 0)
{
measure->includesCache = value;
measure->includes.clear();
WCHAR* buffer = _wcsdup(value);
SplitName(buffer, measure->includes);
delete buffer;
changed = true;
}
value = RmReadString(rm, L"CPUExclude", L"");
if (_wcsicmp(value, measure->excludesCache.c_str()) != 0)
{
measure->excludesCache = value;
measure->excludes.clear();
WCHAR* buffer = _wcsdup(value);
SplitName(buffer, measure->excludes);
delete buffer;
changed = true;
}
int topProcess = RmReadInt(rm, L"TopProcess", 0);
if (topProcess != measure->topProcess)
{
measure->topProcess = topProcess;
changed = true;
}
if (changed)
{
*maxValue = 10000000; // The values are 100 * 100000
}
}
bool CheckProcess(MeasureData* measure, const WCHAR* name)
{ {
if (measure->includes.empty()) if (measure->includes.empty())
{ {
for (size_t i = 0; i < measure->excludes.size(); i++) for (size_t i = 0; i < measure->excludes.size(); ++i)
{ {
if (_wcsicmp(measure->excludes[i].c_str(), name.c_str()) == 0) if (_wcsicmp(measure->excludes[i].c_str(), name) == 0)
{ {
return false; // Exclude return false; // Exclude
} }
@ -123,9 +131,9 @@ bool CheckProcess(CPUMeasure* measure, const std::wstring& name)
} }
else else
{ {
for (size_t i = 0; i < measure->includes.size(); i++) for (size_t i = 0; i < measure->includes.size(); ++i)
{ {
if (_wcsicmp(measure->includes[i].c_str(), name.c_str()) == 0) if (_wcsicmp(measure->includes[i].c_str(), name) == 0)
{ {
return true; // Include return true; // Include
} }
@ -134,16 +142,32 @@ bool CheckProcess(CPUMeasure* measure, const std::wstring& name)
return false; return false;
} }
/* ULONGLONG _GetTickCount64()
This function is called when new value should be measured.
The function returns the new value.
*/
double Update2(UINT id)
{ {
static DWORD oldTime = 0; typedef ULONGLONG (WINAPI * FPGETTICKCOUNT64)();
static FPGETTICKCOUNT64 c_GetTickCount64 = (FPGETTICKCOUNT64)GetProcAddress(GetModuleHandle(L"kernel32"), "GetTickCount64");
if (c_GetTickCount64)
{
return c_GetTickCount64();
}
else
{
static ULONGLONG lastTicks = 0;
ULONGLONG ticks = GetTickCount();
while (ticks < lastTicks) ticks += 0x100000000;
lastTicks = ticks;
return ticks;
}
}
PLUGIN_EXPORT double Update(void* data)
{
MeasureData* measure = (MeasureData*)data;
static ULONGLONG oldTime = 0;
// Only update twice per second // Only update twice per second
DWORD time = GetTickCount(); ULONGLONG time = _GetTickCount64();
if (oldTime == 0 || time - oldTime > 500) if (oldTime == 0 || time - oldTime > 500)
{ {
UpdateProcesses(); UpdateProcesses();
@ -152,31 +176,26 @@ double Update2(UINT id)
LONGLONG newValue = 0; LONGLONG newValue = 0;
std::map<UINT, CPUMeasure*>::iterator i = g_CPUMeasures.find(id); for (size_t i = 0; i < g_Processes.size(); ++i)
if (i != g_CPUMeasures.end())
{
CPUMeasure* measure = (*i).second;
if (measure)
{
for (size_t i = 0; i < g_Processes.size(); i++)
{ {
// Check process include/exclude // Check process include/exclude
if (CheckProcess(measure, g_Processes[i].name)) if (CheckProcess(measure, g_Processes[i].name.c_str()))
{ {
if (g_Processes[i].oldValue != 0) if (g_Processes[i].oldValue != 0)
{ {
LONGLONG value = g_Processes[i].newValue - g_Processes[i].oldValue;
if (measure->topProcess == 0) if (measure->topProcess == 0)
{ {
// Add all values together // Add all values together
newValue += g_Processes[i].newValue - g_Processes[i].oldValue; newValue += value;
} }
else else
{ {
// Find the top process // Find the top process
if (newValue < g_Processes[i].newValue - g_Processes[i].oldValue) if (newValue < value)
{ {
newValue = g_Processes[i].newValue - g_Processes[i].oldValue; newValue = value;
measure->topProcessName = g_Processes[i].name; measure->topProcessName = g_Processes[i].name;
measure->topProcessValue = newValue; measure->topProcessValue = newValue;
} }
@ -184,93 +203,26 @@ double Update2(UINT id)
} }
} }
} }
}
// LONGLONG newValue = 0;
// ULONGLONG longvalue = 0;
//
// if (measure->includes.empty())
// {
// // First get the total CPU value
// longvalue = GetPerfData(L"Processor", L"_Total", L"% Processor Time");
// newValue = longvalue;
//
// // Then substract the excluded processes
// std::vector< std::wstring >::iterator j = measure->excludes.begin();
// for ( ; j != measure->excludes.end(); j++)
// {
// longvalue = GetPerfData(L"Process", (*j).c_str(), L"% Processor Time");
// newValue += longvalue; // Adding means actually substraction
// }
//
// // Compare with the old value
// if (measure->oldValue != 0)
// {
// int val = 10000000 - (UINT)(newValue - measure->oldValue);
// if (val < 0) val = 0;
// value = val;
// }
// measure->oldValue = newValue;
// }
// else
// {
// // Add the included processes
// std::vector< std::wstring >::iterator j = measure->includes.begin();
// for ( ; j != measure->includes.end(); j++)
// {
// longvalue = GetPerfData(L"Process", (*j).c_str(), L"% Processor Time");
// newValue += longvalue;
// }
//
// // Compare with the old value
// if (measure->oldValue != 0)
// {
// value = (UINT)(newValue - measure->oldValue);
// }
// measure->oldValue = newValue;
//
// }
//
// }
}
return (double)newValue; return (double)newValue;
} }
/* PLUGIN_EXPORT LPCWSTR GetString(void* data)
This function is called when the value should be
returned as a string.
*/
LPCTSTR GetString(UINT id, UINT flags)
{ {
std::map<UINT, CPUMeasure*>::iterator i = g_CPUMeasures.find(id); MeasureData* measure = (MeasureData*)data;
if (i != g_CPUMeasures.end())
{
CPUMeasure* measure = (*i).second;
if (measure->topProcess == 2) if (measure->topProcess == 2)
{ {
return measure->topProcessName.c_str(); return measure->topProcessName.c_str();
} }
}
return NULL; return NULL;
} }
/* PLUGIN_EXPORT void Finalize(void* data)
If the measure needs to free resources before quitting.
The plugin can export Finalize function, which is called
when Rainmeter quits (or refreshes).
*/
void Finalize(HMODULE instance, UINT id)
{ {
// delete the measure MeasureData* measure = (MeasureData*)data;
std::map<UINT, CPUMeasure*>::iterator i = g_CPUMeasures.find(id); delete measure;
if (i != g_CPUMeasures.end())
{
delete (*i).second;
g_CPUMeasures.erase(i);
}
CPerfSnapshot::CleanUp(); CPerfSnapshot::CleanUp();
} }
@ -280,24 +232,22 @@ void Finalize(HMODULE instance, UINT id)
*/ */
void UpdateProcesses() void UpdateProcesses()
{ {
CPerfObject* pPerfObj;
CPerfObjectInstance* pObjInst;
CPerfCounter* pPerfCntr;
BYTE data[256]; BYTE data[256];
WCHAR name[256]; WCHAR name[256];
std::vector< ProcessValues > newProcesses; std::vector<ProcessValues> newProcesses;
newProcesses.reserve(g_Processes.size());
CPerfSnapshot snapshot(&g_CounterTitles); CPerfSnapshot snapshot(&g_CounterTitles);
CPerfObjectList objList(&snapshot, &g_CounterTitles); CPerfObjectList objList(&snapshot, &g_CounterTitles);
if (snapshot.TakeSnapshot(L"Process")) if (snapshot.TakeSnapshot(L"Process"))
{ {
pPerfObj = objList.GetPerfObject(L"Process"); CPerfObject* pPerfObj = objList.GetPerfObject(L"Process");
if (pPerfObj) if (pPerfObj)
{ {
for (pObjInst = pPerfObj->GetFirstObjectInstance(); for (CPerfObjectInstance* pObjInst = pPerfObj->GetFirstObjectInstance();
pObjInst != NULL; pObjInst != NULL;
pObjInst = pPerfObj->GetNextObjectInstance()) pObjInst = pPerfObj->GetNextObjectInstance())
{ {
@ -309,7 +259,7 @@ void UpdateProcesses()
continue; continue;
} }
pPerfCntr = pObjInst->GetCounterByName(L"% Processor Time"); CPerfCounter* pPerfCntr = pObjInst->GetCounterByName(L"% Processor Time");
if (pPerfCntr != NULL) if (pPerfCntr != NULL)
{ {
pPerfCntr->GetData(data, 256, NULL); pPerfCntr->GetData(data, 256, NULL);
@ -319,11 +269,13 @@ void UpdateProcesses()
ProcessValues values; ProcessValues values;
values.name = name; values.name = name;
values.oldValue = 0; values.oldValue = 0;
values.newValue = *(ULONGLONG*)data;
values.found = false;
// Check if we can find the old value // Check if we can find the old value
for (size_t i = 0; i < g_Processes.size(); i++) for (size_t i = 0; i < g_Processes.size(); ++i)
{ {
if (!g_Processes[i].found && g_Processes[i].name == name) if (!g_Processes[i].found && _wcsicmp(g_Processes[i].name.c_str(), name) == 0)
{ {
values.oldValue = g_Processes[i].newValue; values.oldValue = g_Processes[i].newValue;
g_Processes[i].found = true; g_Processes[i].found = true;
@ -331,11 +283,7 @@ void UpdateProcesses()
} }
} }
values.newValue = *(ULONGLONG*)data; newProcesses.push_back(std::move(values));
values.found = false;
newProcesses.push_back(values);
// LSLog(LOG_DEBUG, NULL, name); // DEBUG
} }
delete pPerfCntr; delete pPerfCntr;
@ -344,21 +292,10 @@ void UpdateProcesses()
delete pObjInst; delete pObjInst;
} }
delete pPerfObj; delete pPerfObj;
} }
} }
g_Processes = newProcesses; g_Processes = std::move(newProcesses);
} }
UINT GetPluginVersion()
{
return 1005;
}
LPCTSTR GetPluginAuthor()
{
return L"Rainy (rainy@iki.fi)";
}

View File

@ -1,40 +0,0 @@
/*
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.
*/
#ifndef __ADVANCEDCPU_H__
#define __ADVANCEDCPU_H__
#include "../PluginPerfMon/titledb.h"
#include "../PluginPerfMon/perfsnap.h"
#include "../PluginPerfMon/objlist.h"
#include "../PluginPerfMon/perfobj.h"
#include "../PluginPerfMon/objinst.h"
#include "../PluginPerfMon/perfcntr.h"
/* The exported functions */
extern "C"
{
__declspec( dllexport ) UINT Initialize(HMODULE instance, LPCTSTR iniFile, LPCTSTR section, UINT id);
__declspec( dllexport ) void Finalize(HMODULE instance, UINT id);
__declspec( dllexport ) double Update2(UINT id);
__declspec( dllexport ) UINT GetPluginVersion();
__declspec( dllexport ) LPCTSTR GetString(UINT id, UINT flags);
__declspec( dllexport ) LPCTSTR GetPluginAuthor();
}
#endif

View File

@ -267,7 +267,6 @@
</ClCompile> </ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="AdvancedCPU.h" />
<ClInclude Include="..\PluginPerfMon\MakePtr.h" /> <ClInclude Include="..\PluginPerfMon\MakePtr.h" />
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>