rainmeter-studio/Plugins/PluginAdvancedCPU/AdvancedCPU.cpp

302 lines
6.9 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>
#include <vector>
2012-04-19 06:40:27 +00:00
#include "../PluginPerfMon/Titledb.h"
#include "../PluginPerfMon/PerfSnap.h"
#include "../PluginPerfMon/PerfObj.h"
#include "../PluginPerfMon/PerfCntr.h"
#include "../PluginPerfMon/ObjList.h"
#include "../PluginPerfMon/ObjInst.h"
#include "../API/RainmeterAPI.h"
2013-05-31 14:34:36 +00:00
#include "../../Common/RawString.h"
2011-02-03 18:09:24 +00:00
#include "../../Library/DisableThreadLibraryCalls.h" // contains DllMain entry point
2012-04-19 06:40:27 +00:00
struct MeasureData
2009-02-10 18:37:48 +00:00
{
2013-05-31 14:18:52 +00:00
std::vector<RawString> includes;
std::vector<RawString> excludes;
RawString includesCache;
RawString excludesCache;
2009-02-10 18:37:48 +00:00
int topProcess;
2013-05-31 14:18:52 +00:00
RawString topProcessName;
2009-02-10 18:37:48 +00:00
LONGLONG topProcessValue;
2012-04-19 06:40:27 +00:00
MeasureData() :
topProcess(-1),
topProcessValue()
{
}
2009-02-10 18:37:48 +00:00
};
struct ProcessValues
{
2013-05-31 14:18:52 +00:00
RawString name;
2009-02-10 18:37:48 +00:00
LONGLONG oldValue;
LONGLONG newValue;
bool found;
};
2012-04-19 06:40:27 +00:00
static CPerfTitleDatabase g_CounterTitles(PERF_TITLE_COUNTER);
std::vector<ProcessValues> g_Processes;
2009-02-10 18:37:48 +00:00
2012-04-19 06:40:27 +00:00
void UpdateProcesses();
2011-03-29 19:21:57 +00:00
2013-05-31 14:18:52 +00:00
void SplitName(WCHAR* names, std::vector<RawString>& splittedNames)
2012-04-19 06:40:27 +00:00
{
WCHAR* token = wcstok(names, L";");
2013-05-31 14:28:39 +00:00
while (token != nullptr)
2009-02-10 18:37:48 +00:00
{
splittedNames.push_back(token);
2013-05-31 14:28:39 +00:00
token = wcstok(nullptr, L";");
2009-02-10 18:37:48 +00:00
}
}
2012-04-19 06:40:27 +00:00
PLUGIN_EXPORT void Initialize(void** data, void* rm)
{
MeasureData* measure = new MeasureData;
*data = measure;
}
PLUGIN_EXPORT void Reload(void* data, void* rm, double* maxValue)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
MeasureData* measure = (MeasureData*)data;
bool changed = false;
2009-02-10 18:37:48 +00:00
2012-04-19 06:40:27 +00:00
LPCWSTR value = RmReadString(rm, L"CPUInclude", L"");
if (_wcsicmp(value, measure->includesCache.c_str()) != 0)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
measure->includesCache = value;
measure->includes.clear();
WCHAR* buffer = _wcsdup(value);
2009-02-10 18:37:48 +00:00
SplitName(buffer, measure->includes);
2012-04-19 06:40:27 +00:00
delete buffer;
changed = true;
2009-02-10 18:37:48 +00:00
}
2012-04-19 06:40:27 +00:00
value = RmReadString(rm, L"CPUExclude", L"");
if (_wcsicmp(value, measure->excludesCache.c_str()) != 0)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
measure->excludesCache = value;
measure->excludes.clear();
WCHAR* buffer = _wcsdup(value);
2009-02-10 18:37:48 +00:00
SplitName(buffer, measure->excludes);
2012-04-19 06:40:27 +00:00
delete buffer;
changed = true;
2009-02-10 18:37:48 +00:00
}
2012-04-19 06:40:27 +00:00
int topProcess = RmReadInt(rm, L"TopProcess", 0);
if (topProcess != measure->topProcess)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
measure->topProcess = topProcess;
changed = true;
2009-02-10 18:37:48 +00:00
}
2012-04-19 06:40:27 +00:00
if (changed)
{
*maxValue = 10000000; // The values are 100 * 100000
}
2009-02-10 18:37:48 +00:00
}
2012-04-19 06:40:27 +00:00
bool CheckProcess(MeasureData* measure, const WCHAR* name)
2009-02-10 18:37:48 +00:00
{
2011-03-29 19:21:57 +00:00
if (measure->includes.empty())
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
for (size_t i = 0; i < measure->excludes.size(); ++i)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
if (_wcsicmp(measure->excludes[i].c_str(), name) == 0)
2009-02-10 18:37:48 +00:00
{
return false; // Exclude
}
}
return true; // Include
}
else
{
2012-04-19 06:40:27 +00:00
for (size_t i = 0; i < measure->includes.size(); ++i)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
if (_wcsicmp(measure->includes[i].c_str(), name) == 0)
2009-02-10 18:37:48 +00:00
{
return true; // Include
}
}
}
return false;
}
2012-04-19 06:40:27 +00:00
ULONGLONG _GetTickCount64()
{
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)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
MeasureData* measure = (MeasureData*)data;
static ULONGLONG oldTime = 0;
2011-03-29 19:21:57 +00:00
2009-02-10 18:37:48 +00:00
// Only update twice per second
2012-04-19 06:40:27 +00:00
ULONGLONG time = _GetTickCount64();
2009-02-10 18:37:48 +00:00
if (oldTime == 0 || time - oldTime > 500)
{
UpdateProcesses();
oldTime = time;
}
LONGLONG newValue = 0;
2012-04-19 06:40:27 +00:00
for (size_t i = 0; i < g_Processes.size(); ++i)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
// Check process include/exclude
if (CheckProcess(measure, g_Processes[i].name.c_str()))
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
if (g_Processes[i].oldValue != 0)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
LONGLONG value = g_Processes[i].newValue - g_Processes[i].oldValue;
if (measure->topProcess == 0)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
// Add all values together
newValue += value;
}
else
{
// Find the top process
if (newValue < value)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
newValue = value;
measure->topProcessName = g_Processes[i].name;
measure->topProcessValue = newValue;
2009-02-10 18:37:48 +00:00
}
}
}
}
}
return (double)newValue;
}
2012-04-19 06:40:27 +00:00
PLUGIN_EXPORT LPCWSTR GetString(void* data)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
MeasureData* measure = (MeasureData*)data;
2009-02-10 18:37:48 +00:00
2012-04-19 06:40:27 +00:00
if (measure->topProcess == 2)
{
return measure->topProcessName.c_str();
2009-02-10 18:37:48 +00:00
}
2013-05-31 14:28:39 +00:00
return nullptr;
2009-02-10 18:37:48 +00:00
}
2012-04-19 06:40:27 +00:00
PLUGIN_EXPORT void Finalize(void* data)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
MeasureData* measure = (MeasureData*)data;
delete measure;
2009-02-10 18:37:48 +00:00
CPerfSnapshot::CleanUp();
}
/*
This updates the process status
*/
void UpdateProcesses()
{
BYTE data[256];
WCHAR name[256];
2012-04-19 06:40:27 +00:00
std::vector<ProcessValues> newProcesses;
newProcesses.reserve(g_Processes.size());
2009-02-10 18:37:48 +00:00
CPerfSnapshot snapshot(&g_CounterTitles);
CPerfObjectList objList(&snapshot, &g_CounterTitles);
2011-03-29 19:21:57 +00:00
if (snapshot.TakeSnapshot(L"Process"))
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
CPerfObject* pPerfObj = objList.GetPerfObject(L"Process");
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-04-19 06:40:27 +00:00
for (CPerfObjectInstance* pObjInst = pPerfObj->GetFirstObjectInstance();
2013-05-31 14:28:39 +00:00
pObjInst != nullptr;
2009-02-10 18:37:48 +00:00
pObjInst = pPerfObj->GetNextObjectInstance())
{
2011-03-29 19:21:57 +00:00
if (pObjInst->GetObjectInstanceName(name, 256))
2009-02-10 18:37:48 +00:00
{
2010-09-17 08:47:22 +00:00
if (_wcsicmp(name, L"_Total") == 0)
2009-02-10 18:37:48 +00:00
{
delete pObjInst;
2009-02-10 18:37:48 +00:00
continue;
}
2012-04-19 06:40:27 +00:00
CPerfCounter* pPerfCntr = pObjInst->GetCounterByName(L"% Processor Time");
2013-05-31 14:28:39 +00:00
if (pPerfCntr != nullptr)
2009-02-10 18:37:48 +00:00
{
2013-05-31 14:28:39 +00:00
pPerfCntr->GetData(data, 256, nullptr);
2011-03-29 19:21:57 +00:00
if (pPerfCntr->GetSize() == 8)
2009-02-10 18:37:48 +00:00
{
ProcessValues values;
values.name = name;
values.oldValue = 0;
2012-04-19 06:40:27 +00:00
values.newValue = *(ULONGLONG*)data;
values.found = false;
2009-02-10 18:37:48 +00:00
// Check if we can find the old value
2012-04-19 06:40:27 +00:00
for (size_t i = 0; i < g_Processes.size(); ++i)
2009-02-10 18:37:48 +00:00
{
2012-04-19 06:40:27 +00:00
if (!g_Processes[i].found && _wcsicmp(g_Processes[i].name.c_str(), name) == 0)
2009-02-10 18:37:48 +00:00
{
values.oldValue = g_Processes[i].newValue;
g_Processes[i].found = true;
break;
}
}
2012-04-19 06:40:27 +00:00
newProcesses.push_back(std::move(values));
2009-02-10 18:37:48 +00:00
}
delete pPerfCntr;
}
}
2009-02-10 18:37:48 +00:00
delete pObjInst;
}
2012-04-19 06:40:27 +00:00
2009-02-10 18:37:48 +00:00
delete pPerfObj;
}
}
2012-04-19 06:40:27 +00:00
g_Processes = std::move(newProcesses);
2009-02-10 18:37:48 +00:00
}