Skip to content

Commit ba4f716

Browse files
committed
dll: Implemented per-CPU monitoring feature
from permon.c in tclock-101021-analog
1 parent 49b85f6 commit ba4f716

File tree

3 files changed

+159
-7
lines changed

3 files changed

+159
-7
lines changed

format-kt.txt

+2-1
Original file line numberDiff line numberDiff line change
@@ -217,5 +217,6 @@ $ Ss =
217217

218218
�� CPU�g�p�� ��
219219
�� TC_ENABLE_CPU �� 1 �̂Ƃ��Ɏg�p�”\
220-
* CU = CPU�g�p��(%)
220+
* CU = �S�̂� CPU �g�p�� ( % )
221+
* CU[0-7] = �R�A [ 0 - 7 ] �� CPU �g�p�� ( % )
221222

source/dll/cpu.c

+140-1
Original file line numberDiff line numberDiff line change
@@ -2,23 +2,162 @@
22
cpu.c
33
get percentage of CPU usage
44
Kazubon 2001
5+
tapetums 2016
56
---------------------------------------------------------------------------*/
67

78
#include "tcdll.h"
89

910
#if TC_ENABLE_CPU
1011

12+
#include <pdh.h>
13+
14+
#define MAX_PROCESSOR 8
15+
16+
// cpu usage
17+
int CPUUsage[MAX_PROCESSOR] = { 0 };
18+
19+
HMODULE hmodPDH = NULL;
20+
PDH_HQUERY hQuery = NULL;
21+
22+
typedef PDH_STATUS (WINAPI* pfnPdhOpenQueryW) (LPCWSTR, DWORD_PTR, PDH_HQUERY*);
23+
typedef PDH_STATUS (WINAPI* pfnPdhAddCounterW) (PDH_HQUERY, LPCWSTR, DWORD_PTR, PDH_HCOUNTER*);
24+
typedef PDH_STATUS (WINAPI* pfnPdhCollectQueryData) (PDH_HQUERY);
25+
typedef PDH_STATUS (WINAPI* pfnPdhGetFormattedCounterValue)(PDH_HCOUNTER, DWORD, LPDWORD, PPDH_FMT_COUNTERVALUE);
26+
typedef PDH_STATUS (WINAPI* pfnPdhCloseQuery) (PDH_HQUERY);
27+
typedef PDH_STATUS (WINAPI* pfnPdhRemoveCounter) (PDH_HCOUNTER);
28+
29+
pfnPdhOpenQueryW pPdhOpenQueryW = NULL;
30+
pfnPdhAddCounterW pPdhAddCounterW = NULL;
31+
pfnPdhCollectQueryData pPdhCollectQueryData = NULL;
32+
pfnPdhGetFormattedCounterValue pPdhGetFormattedCounterValue = NULL;
33+
pfnPdhCloseQuery pPdhCloseQuery = NULL;
34+
pfnPdhRemoveCounter pPdhRemoveCounter = NULL;
35+
36+
PDH_HCOUNTER hTotalCPUCounter = NULL;
37+
PDH_HCOUNTER hCPUCounter[MAX_PROCESSOR] = { NULL };
38+
1139
void CpuMoni_start(void)
1240
{
41+
if ( hmodPDH )
42+
{
43+
CpuMoni_end();
44+
}
45+
46+
hmodPDH = LoadLibraryExW(L"pdh.dll", NULL, LOAD_WITH_ALTERED_SEARCH_PATH);
47+
if ( hmodPDH == NULL )
48+
{
49+
return;
50+
}
51+
52+
pPdhOpenQueryW = (pfnPdhOpenQueryW) GetProcAddress(hmodPDH, "PdhOpenQueryW");
53+
pPdhAddCounterW = (pfnPdhAddCounterW) GetProcAddress(hmodPDH, "PdhAddCounterW");
54+
pPdhRemoveCounter = (pfnPdhRemoveCounter) GetProcAddress(hmodPDH, "PdhRemoveCounter");
55+
pPdhCollectQueryData = (pfnPdhCollectQueryData) GetProcAddress(hmodPDH, "PdhCollectQueryData");
56+
pPdhGetFormattedCounterValue = (pfnPdhGetFormattedCounterValue)GetProcAddress(hmodPDH, "PdhGetFormattedCounterValue");
57+
pPdhCloseQuery = (pfnPdhCloseQuery) GetProcAddress(hmodPDH, "PdhCloseQuery");
58+
59+
if ( pPdhOpenQueryW == NULL ||
60+
pPdhAddCounterW == NULL ||
61+
pPdhCollectQueryData == NULL ||
62+
pPdhRemoveCounter == NULL ||
63+
pPdhGetFormattedCounterValue == NULL ||
64+
pPdhCloseQuery == NULL )
65+
{
66+
goto FAILURE_PDH_COUNTER_INITIALIZATION;
67+
}
68+
69+
PDH_STATUS status;
70+
71+
// initialize
72+
status = pPdhOpenQueryW(NULL, 0, &hQuery);
73+
if ( status != ERROR_SUCCESS )
74+
{
75+
goto FAILURE_PDH_COUNTER_INITIALIZATION;
76+
}
77+
78+
// create cpu counter
79+
wchar_t counterName[64];
80+
for ( int i = 0; i < MAX_PROCESSOR; ++i )
81+
{
82+
wsprintfW(counterName, L"\\Processor(%d)\\%% Processor Time", i);
83+
84+
status = pPdhAddCounterW(hQuery, counterName, 0, &hCPUCounter[i]);
85+
if ( status != ERROR_SUCCESS )
86+
{
87+
goto FAILURE_PDH_COUNTER_INITIALIZATION;
88+
}
89+
}
90+
91+
// create total cpu usage counter
92+
status = pPdhAddCounterW(hQuery, L"\\Processor(_Total)\\% Processor Time", 0, &hTotalCPUCounter);
93+
if ( status != ERROR_SUCCESS )
94+
{
95+
goto FAILURE_PDH_COUNTER_INITIALIZATION;
96+
}
97+
98+
return; /* SUCCESS */
99+
100+
FAILURE_PDH_COUNTER_INITIALIZATION:
101+
hQuery = NULL;
102+
103+
FreeLibrary(hmodPDH);
104+
hmodPDH = NULL;
105+
106+
return; /* FAILURE */
13107
}
14108

15109
int CpuMoni_get(void)
16110
{
17-
return -1;
111+
if ( hQuery == NULL )
112+
{
113+
return 0;
114+
}
115+
116+
PDH_STATUS status;
117+
PDH_FMT_COUNTERVALUE FmtValue;
118+
119+
// get current data
120+
status = pPdhCollectQueryData(hQuery);
121+
if ( status != ERROR_SUCCESS )
122+
{
123+
return 0;
124+
}
125+
126+
// get cpu counter
127+
for ( int i = 0; i < MAX_PROCESSOR; ++i )
128+
{
129+
status = pPdhGetFormattedCounterValue(hCPUCounter[i], PDH_FMT_DOUBLE, NULL, &FmtValue);
130+
if ( status != ERROR_SUCCESS )
131+
{
132+
CPUUsage[i] = 0;
133+
}
134+
else
135+
{
136+
CPUUsage[i] = (int)(FmtValue.doubleValue + 0.5);
137+
}
138+
}
139+
140+
// get total cpu usage
141+
status = pPdhGetFormattedCounterValue(hTotalCPUCounter, PDH_FMT_DOUBLE, NULL, &FmtValue);
142+
if ( status != ERROR_SUCCESS )
143+
{
144+
return 0;
145+
}
146+
147+
return (int)(FmtValue.doubleValue + 0.5);
18148
}
19149

20150
void CpuMoni_end(void)
21151
{
152+
// finalize
153+
if ( pPdhCloseQuery )
154+
{
155+
pPdhCloseQuery(hQuery);
156+
FreeLibrary(hmodPDH);
157+
}
158+
159+
hQuery = NULL;
160+
hmodPDH = NULL;
22161
}
23162

24163
#endif /* TC_ENABLE_CPU */

source/dll/sysinfo.c

+17-5
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ static BOOL bMuteFlg;
3434
#endif
3535

3636
#if TC_ENABLE_CPU
37+
extern int CPUUsage[];
3738
static BOOL m_bCPU;
38-
static int iCPUUsage;
39+
static int totalCPUUsage;
3940
#endif
4041

4142
#if TC_ENABLE_NETWORK
@@ -132,7 +133,7 @@ void OnTimerSysInfo(void)
132133
#if TC_ENABLE_CPU
133134
if(m_bCPU)
134135
{
135-
iCPUUsage = CpuMoni_get(); // cpu.c
136+
totalCPUUsage = CpuMoni_get(); // cpu.c
136137
}
137138
#endif
138139
#if TC_ENABLE_BATTERY
@@ -454,12 +455,23 @@ void CPUHandler(FORMATHANDLERSTRUCT* pstruc)
454455
{
455456
m_bCPU = TRUE;
456457
CpuMoni_start(); // cpu.c
457-
iCPUUsage = CpuMoni_get(); // cpu.c
458+
totalCPUUsage = CpuMoni_get(); // cpu.c
458459
g_bDispSecond = TRUE;
459460
}
460461

461-
pstruc->sp += 2;
462-
FormatNum(&pstruc->sp, &pstruc->dp, iCPUUsage, pstruc->bZeroPad);
462+
if('0' <= pstruc->sp[2] && pstruc->sp[2] <='7') // to be fixed for more than 8 CPU cores
463+
{
464+
// CPU usage per core
465+
const int processorNum = pstruc->sp[2] - '0';
466+
pstruc->sp += 3;
467+
FormatNum(&pstruc->sp, &pstruc->dp, CPUUsage[processorNum], pstruc->bZeroPad);
468+
}
469+
else
470+
{
471+
// total CPU usage
472+
pstruc->sp += 2;
473+
FormatNum(&pstruc->sp, &pstruc->dp, totalCPUUsage, pstruc->bZeroPad);
474+
}
463475
}
464476
#endif
465477

0 commit comments

Comments
 (0)