Skip to content

Commit

Permalink
feat: tc_port add getCpuLoad
Browse files Browse the repository at this point in the history
  • Loading branch information
ruanshudong committed Feb 1, 2024
1 parent 6d28e76 commit 0980935
Show file tree
Hide file tree
Showing 4 changed files with 92 additions and 10 deletions.
15 changes: 5 additions & 10 deletions unit-test/hello_test.h
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,11 @@ class HelloTest : public testing::Test
return prx;
}

int GetUsedFileDescriptorCount()
{
int getFdCounts()
{
#if TARGET_PLATFORM_WINDOWS
return 0;
#else
// 使用 shell 命令 "lsof" 获取已使用的文件句柄数量
FILE* file = popen("lsof -p $$ | wc -l", "r");
if (!file) {
Expand All @@ -287,14 +290,6 @@ class HelloTest : public testing::Test
// 解析结果并提取文件句柄数量
int usedFileDescriptors = std::stoi(result);
return usedFileDescriptors;
}

int getFdCounts()
{
#if TARGET_PLATFORM_WINDOWS
return 0;
#else
return GetUsedFileDescriptorCount();
#endif
}

Expand Down
20 changes: 20 additions & 0 deletions unit-test/util/test_tc_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,25 @@ TEST_F(UtilPortTest, testGetDiskInfo)
ASSERT_TRUE(availableSize > 0);
}

TEST_F(UtilPortTest, testGetCpuLoad)
{
cout << "cpu load:" << TC_Port::getCpuLoad(500) << endl;
}

#if TARGET_PLATFORM_WINDOWS
#include <windows.h>

TEST_F(UtilPortTest, testGetDisk)
{
DWORD aDrive = GetLogicalDrives();

for (int i = 0; i < 26; i++) {
if (aDrive & (1 << i)) {
char drive[4];
sprintf(drive, "%c:\\", 'A' + i);
std::cout << drive << std::endl;
}
}
}

#endif
8 changes: 8 additions & 0 deletions util/include/util/tc_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -177,6 +177,14 @@ class TC_Port
*/
static void unregisterTerm(size_t id);

/**
* 获取cpu负载
* @param queryTime, windows下有效, 会阻塞的时间, 毫秒
*
* @return, 如果失败, 返回-1, 否在返回cpu负载的值
*/
static double getCpuLoad(uint32_t queryTime = 500);

/**
* 获取指定进程占用物理内存大小, 返回内存大小(K, M, G)
* @param pid: 目标进程id
Expand Down
59 changes: 59 additions & 0 deletions util/src/tc_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,15 @@
#else

#pragma comment(lib, "ws2_32.lib")
#pragma comment(lib, "pdh.lib")

#include <windows.h>
#include <time.h>
#include <sys/timeb.h>
#include <psapi.h>
#include <tlhelp32.h>
#include <pdh.h>
#include <pdhmsg.h>
#include "util/tc_strptime.h"
#endif

Expand Down Expand Up @@ -663,6 +667,61 @@ static int64_t reSize(int64_t i, const char unit)
}
#endif

double TC_Port::getCpuLoad(uint32_t queryTime)
{
#if TARGET_PLATFORM_LINUX || TARGET_PLATFORM_IOS
double loadAvg[3];
if ( getloadavg( loadAvg, 3 ) != -1 )
{
return static_cast<float>( loadAvg[0] );
}
return -1;
#else
PDH_HQUERY cpuQuery;
PDH_HCOUNTER cpuTotal;

// Initialize the PDH query
if (PdhOpenQuery(NULL, NULL, &cpuQuery) != ERROR_SUCCESS) {
return -1;
}

// Add the CPU Time counter to the query
if (PdhAddCounter(cpuQuery, "\\Processor(_Total)\\% Processor Time", NULL, &cpuTotal) != ERROR_SUCCESS) {
return -1;
}

// Collect the query data
if (PdhCollectQueryData(cpuQuery) != ERROR_SUCCESS) {
return -1;
}

// Wait for some time
TC_Common::msleep(queryTime);

// Collect the updated query data
if (PdhCollectQueryData(cpuQuery) != ERROR_SUCCESS) {
return -1;
}

// Get the collected data
PDH_FMT_COUNTERVALUE counterVal;

if (PdhGetFormattedCounterValue(cpuTotal, PDH_FMT_DOUBLE, NULL, &counterVal) != ERROR_SUCCESS) {
return -1;
}

// Print the CPU usage
// std::cout << "CPU Usage: " << counterVal.doubleValue << " %\n";

// Close the query
if (PdhCloseQuery(cpuQuery) != ERROR_SUCCESS) {
return -1;
}

return counterVal.doubleValue;
#endif
}

// 获取指定进程占用物理内存大小
int64_t TC_Port::getPidMemUsed(int64_t pid, const char unit)
{
Expand Down

0 comments on commit 0980935

Please sign in to comment.