Skip to content

Commit

Permalink
add gpu memory info
Browse files Browse the repository at this point in the history
  • Loading branch information
a1ive committed Aug 8, 2024
1 parent a970d49 commit 19bfa0d
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 123 deletions.
3 changes: 1 addition & 2 deletions gnwinfo/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,7 @@ gnwinfo_ctx_update(WPARAM wparam)
g_ctx.cpu_freq = NWL_GetCpuFreq();
NWL_GetCpuMsr(g_ctx.cpu_count, g_ctx.cpu_info);
NWL_GetCurDisplay(g_ctx.wnd, &g_ctx.cur_display);
g_ctx.gpu_3d_usage = NWL_GetGpuUsage(L"engtype_3D");
g_ctx.gpu_count = NWL_GetGpuInfo(g_ctx.gpu_info, NWL_GPU_MAX_COUNT);
NWL_GetGpuInfo(&g_ctx.gpu_info);
if (g_ctx.audio)
{
free(g_ctx.audio);
Expand Down
18 changes: 9 additions & 9 deletions gnwinfo/display.c
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ static VOID
draw_gpu(struct nk_context* ctx)
{
int i;
for (i = 0; i < g_ctx.gpu_count; i++)
for (i = 0; i < g_ctx.gpu_info.DeviceCount; i++)
{
nk_layout_row_dynamic(ctx, 0, 1);
nk_image_label(ctx, GET_PNG(IDR_PNG_GPU), g_ctx.gpu_info[i].gpu_hwid, NK_TEXT_LEFT, g_color_text_l);
nk_image_label(ctx, GET_PNG(IDR_PNG_GPU), g_ctx.gpu_info.Device[i].gpu_hwid, NK_TEXT_LEFT, g_color_text_l);

nk_layout_row(ctx, NK_DYNAMIC, 0, 2, (float[2]) { 0.3f, 0.7f });

draw_gpu_attr(ctx, "Vendor", g_ctx.gpu_info[i].gpu_vendor);
draw_gpu_attr(ctx, "Device", g_ctx.gpu_info[i].gpu_device);
if (g_ctx.gpu_info[i].driver)
draw_gpu_attr(ctx, "Vendor", g_ctx.gpu_info.Device[i].gpu_vendor);
draw_gpu_attr(ctx, "Device", g_ctx.gpu_info.Device[i].gpu_device);
if (g_ctx.gpu_info.Device[i].driver)
{
draw_gpu_attr(ctx, "Driver Date", g_ctx.gpu_info[i].gpu_driver_date);
draw_gpu_attr(ctx, "Driver Version", g_ctx.gpu_info[i].gpu_driver_ver);
draw_gpu_attr(ctx, "Location Info", g_ctx.gpu_info[i].gpu_location);
draw_gpu_attr(ctx, "Driver Date", g_ctx.gpu_info.Device[i].gpu_driver_date);
draw_gpu_attr(ctx, "Driver Version", g_ctx.gpu_info.Device[i].gpu_driver_ver);
draw_gpu_attr(ctx, "Location Info", g_ctx.gpu_info.Device[i].gpu_location);
draw_gpu_attr(ctx, "Memory Size",
NWL_GetHumanSize(g_ctx.gpu_info[i].gpu_mem_size, NWLC->NwUnits, 1024));
NWL_GetHumanSize(g_ctx.gpu_info.Device[i].gpu_mem_size, NWLC->NwUnits, 1024));
}
}
}
Expand Down
4 changes: 1 addition & 3 deletions gnwinfo/gnwinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,7 @@ typedef struct _GNW_CONTEXT
NWLIB_MEM_INFO mem_status;

NWLIB_CUR_DISPLAY cur_display;
double gpu_3d_usage;
int gpu_count;
NWLIB_GPU_INFO gpu_info[NWL_GPU_MAX_COUNT];
NWLIB_GPU_INFO gpu_info;

UINT audio_count;
NWLIB_AUDIO_DEV* audio;
Expand Down
17 changes: 9 additions & 8 deletions gnwinfo/summary.c
Original file line number Diff line number Diff line change
Expand Up @@ -311,28 +311,29 @@ draw_display(struct nk_context* ctx)
"%ldx%ld %u DPI (%u%%)",
g_ctx.cur_display.Width, g_ctx.cur_display.Height, g_ctx.cur_display.Dpi, g_ctx.cur_display.Scale);
nk_lhcf(ctx, NK_TEXT_LEFT, g_color_text_l,
"GPU 3D %.0f%%",
g_ctx.gpu_3d_usage);
"GPU 3D %.1f%% RAM %.1f%%",
g_ctx.gpu_info.Usage3D,
g_ctx.gpu_info.UsageDedicated);
if (nk_button_image_hover(ctx, GET_PNG(IDR_PNG_MONITOR), gnwinfo_get_text(L"Display Devices")))
g_ctx.window_flag |= GUI_WINDOW_DISPLAY;

for (i = 0; i < g_ctx.gpu_count; i++)
for (i = 0; i < g_ctx.gpu_info.DeviceCount; i++)
{
if (g_ctx.gpu_info[i].driver)
if (g_ctx.gpu_info.Device[i].driver)
{
CHAR name[32];
snprintf(name, sizeof(name), "GPU%d", i);
nk_layout_row(ctx, NK_DYNAMIC, 0, 3, (float[3]) { 0.3f, 0.4f, 0.3f });
nk_lhsc(ctx, name, NK_TEXT_LEFT, g_color_text_d, nk_false, nk_true);
nk_lhc(ctx, g_ctx.gpu_info[i].gpu_device, NK_TEXT_LEFT, g_color_text_l);
nk_lhc(ctx, NWL_GetHumanSize(g_ctx.gpu_info[i].gpu_mem_size, NWLC->NwUnits, 1024),
nk_lhc(ctx, g_ctx.gpu_info.Device[i].gpu_device, NK_TEXT_LEFT, g_color_text_l);
nk_lhc(ctx, NWL_GetHumanSize(g_ctx.gpu_info.Device[i].gpu_mem_size, NWLC->NwUnits, 1024),
NK_TEXT_LEFT, g_color_text_l);
}
else
{
nk_layout_row(ctx, NK_DYNAMIC, 0, 2, (float[2]) { 0.3f, 0.7f });
nk_lhsc(ctx, g_ctx.gpu_info[i].gpu_vendor, NK_TEXT_LEFT, g_color_text_d, nk_true, nk_true);
nk_lhc(ctx, g_ctx.gpu_info[i].gpu_device, NK_TEXT_LEFT, g_color_text_l);
nk_lhsc(ctx, g_ctx.gpu_info.Device[i].gpu_vendor, NK_TEXT_LEFT, g_color_text_d, nk_true, nk_true);
nk_lhc(ctx, g_ctx.gpu_info.Device[i].gpu_device, NK_TEXT_LEFT, g_color_text_l);
}
}
for (i = 0; g_ctx.edid->Children[i].LinkedNode; i++)
Expand Down
104 changes: 38 additions & 66 deletions libnw/gpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ SetDevicePropertyString(CHAR* strBuf, size_t strSize, DEVINST devHandle, const D
}

static void
FillGpuInfo(NWLIB_GPU_INFO* info, LPCWSTR devIf)
FillGpuInfo(NWLIB_GPU_DEV* info, LPCWSTR devIf)
{
DEVPROPTYPE devicePropertyType;
DEVINST deviceInstanceHandle;
Expand Down Expand Up @@ -132,7 +132,7 @@ FillGpuInfo(NWLIB_GPU_INFO* info, LPCWSTR devIf)
}

static inline BOOL
CompareHwid(LPCSTR hwid, NWLIB_GPU_INFO* info, int count)
CompareHwid(LPCSTR hwid, NWLIB_GPU_DEV* info, int count)
{
for (int i = 0; i < count && info[i].driver; i++)
{
Expand All @@ -142,8 +142,8 @@ CompareHwid(LPCSTR hwid, NWLIB_GPU_INFO* info, int count)
return FALSE;
}

int
NWL_GetGpuInfo(NWLIB_GPU_INFO* info, int count)
static int
GetGpuDev(NWLIB_GPU_DEV* dev, int count, PUINT64 ram)
{
int ret = 0;
PWSTR deviceInterfaceList = NULL;
Expand All @@ -152,6 +152,8 @@ NWL_GetGpuInfo(NWLIB_GPU_INFO* info, int count)
GUID guidDisplayDevice = { 0x1CA05180, 0xA699, 0x450A,
{ 0x9A, 0x0C, 0xDE, 0x4F, 0xBE, 0x3D, 0xDD, 0x89 } };

*ram = 0;

if (CM_Get_Device_Interface_List_SizeW(&deviceInterfaceListLength,
&guidDisplayDevice, NULL,
CM_GET_DEVICE_INTERFACE_LIST_PRESENT) != CR_SUCCESS)
Expand All @@ -170,7 +172,8 @@ NWL_GetGpuInfo(NWLIB_GPU_INFO* info, int count)
{
if (_wcsnicmp(p, L"\\\\?\\PCI#VEN_", 12) != 0)
continue;
FillGpuInfo(&info[ret], p);
FillGpuInfo(&dev[ret], p);
*ram += dev[ret].gpu_mem_size;
ret++;
}

Expand All @@ -179,11 +182,11 @@ NWL_GetGpuInfo(NWLIB_GPU_INFO* info, int count)
{
PNODE pci = pciList->Children[i].LinkedNode;
LPCSTR hwid = NWL_NodeAttrGet(pci, "HWID");
if (CompareHwid(hwid, info, count))
if (CompareHwid(hwid, dev, count))
continue;
strcpy_s(info[ret].gpu_hwid, NWL_STR_SIZE, hwid);
strcpy_s(info[ret].gpu_device, NWL_STR_SIZE, NWL_NodeAttrGet(pci, "Device"));
strcpy_s(info[ret].gpu_vendor, NWL_STR_SIZE, NWL_NodeAttrGet(pci, "Vendor"));
strcpy_s(dev[ret].gpu_hwid, NWL_STR_SIZE, hwid);
strcpy_s(dev[ret].gpu_device, NWL_STR_SIZE, NWL_NodeAttrGet(pci, "Device"));
strcpy_s(dev[ret].gpu_vendor, NWL_STR_SIZE, NWL_NodeAttrGet(pci, "Vendor"));
ret++;
}

Expand All @@ -193,79 +196,48 @@ NWL_GetGpuInfo(NWLIB_GPU_INFO* info, int count)
return ret;
}

static inline BOOL
CompareWcsSuffix(LPCWSTR str, LPCWSTR suffix)
VOID NWL_GetGpuInfo(PNWLIB_GPU_INFO info)
{
size_t strLen = wcslen(str);
size_t suffixLen = wcslen(suffix);
if (suffixLen > strLen)
return FALSE;
return (wcscmp(&str[strLen - suffixLen], suffix) == 0);
}

static double
GetPdhLargeSum(PDH_HCOUNTER counter, LPCWSTR suffix)
{
PDH_STATUS status = ERROR_SUCCESS;
DWORD dwBufferSize = 0;
DWORD dwItemCount = 0;
PDH_FMT_COUNTERVALUE_ITEM* pItems = NULL;
double ret = 0.0f;

status = NWLC->PdhGetFormattedCounterArrayW(counter, PDH_FMT_DOUBLE, &dwBufferSize, &dwItemCount, pItems);
if (status != PDH_MORE_DATA)
return 0.0f;
pItems = malloc(dwBufferSize);
if (!pItems)
return 0.0f;
status = NWLC->PdhGetFormattedCounterArrayW(counter, PDH_FMT_DOUBLE, &dwBufferSize, &dwItemCount, pItems);
if (status != ERROR_SUCCESS)
goto out;
for (DWORD i = 0; i < dwItemCount; i++)
ZeroMemory(info, sizeof(NWLIB_GPU_INFO));
info->DeviceCount = GetGpuDev(info->Device, NWL_GPU_MAX_COUNT, &info->DedicatedTotal);
if (NWLC->PdhGpuUsage)
{
if (CompareWcsSuffix(pItems[i].szName, suffix))
ret += pItems[i].FmtValue.doubleValue;
info->Usage3D = NWL_GetPdhSum(NWLC->PdhGpuUsage, PDH_FMT_DOUBLE, L"engtype_3D").doubleValue;
info->UsageCopy = NWL_GetPdhSum(NWLC->PdhGpuUsage, PDH_FMT_DOUBLE, L"engtype_Copy").doubleValue;
info->UsageCompute0 = NWL_GetPdhSum(NWLC->PdhGpuUsage, PDH_FMT_DOUBLE, L"engtype_Compute 0").doubleValue;
info->UsageCompute1 = NWL_GetPdhSum(NWLC->PdhGpuUsage, PDH_FMT_DOUBLE, L"engtype_Compute 1").doubleValue;
}

out:
if (pItems)
free(pItems);
return ret;
}

double
NWL_GetGpuUsage(LPCWSTR suffix)
{
if (NWLC->PdhGpuUsage)
return GetPdhLargeSum(NWLC->PdhGpuUsage, suffix);
// TODO: get gpu usage
return 0.0f;
if (NWLC->PdhGpuCurMem)
info->DedicatedInUse = NWL_GetPdhSum(NWLC->PdhGpuCurMem, PDH_FMT_LARGE, NULL).largeValue;
if (info->DedicatedTotal > 0)
info->UsageDedicated = (double)info->DedicatedInUse / (double)info->DedicatedTotal * 100.0f;
if (info->UsageDedicated > 100.0f)
info->UsageDedicated = 100.0f;
}

PNODE NW_Gpu(VOID)
{
NWLIB_GPU_INFO info[NWL_GPU_MAX_COUNT] = { 0 };
int count = NWL_GPU_MAX_COUNT;
NWLIB_GPU_INFO info;

PNODE node = NWL_NodeAlloc("GPU", NFLG_TABLE);
if (NWLC->GpuInfo)
NWL_NodeAppendChild(NWLC->NwRoot, node);

count = NWL_GetGpuInfo(info, count);
for (int i = 0; i < count; i++)
NWL_GetGpuInfo(&info);
for (int i = 0; i < info.DeviceCount; i++)
{
PNODE gpu = NWL_NodeAppendNew(node, "Device", NFLG_TABLE_ROW);
NWL_NodeAttrSet(gpu, "HWID", info[i].gpu_hwid, 0);
NWL_NodeAttrSet(gpu, "Device", info[i].gpu_device, 0);
NWL_NodeAttrSet(gpu, "Vendor", info[i].gpu_vendor, 0);
if (info[i].driver)
NWL_NodeAttrSet(gpu, "HWID", info.Device[i].gpu_hwid, 0);
NWL_NodeAttrSet(gpu, "Device", info.Device[i].gpu_device, 0);
NWL_NodeAttrSet(gpu, "Vendor", info.Device[i].gpu_vendor, 0);
if (info.Device[i].driver)
{
NWL_NodeAttrSet(gpu, "Interface", info[i].gpu_if, 0);
NWL_NodeAttrSet(gpu, "Driver Date", info[i].gpu_driver_date, 0);
NWL_NodeAttrSet(gpu, "Driver Version", info[i].gpu_driver_ver, 0);
NWL_NodeAttrSet(gpu, "Location", info[i].gpu_location, 0);
NWL_NodeAttrSet(gpu, "Interface", info.Device[i].gpu_if, 0);
NWL_NodeAttrSet(gpu, "Driver Date", info.Device[i].gpu_driver_date, 0);
NWL_NodeAttrSet(gpu, "Driver Version", info.Device[i].gpu_driver_ver, 0);
NWL_NodeAttrSet(gpu, "Location", info.Device[i].gpu_location, 0);
NWL_NodeAttrSet(gpu, "Memory Size",
NWL_GetHumanSize(info[i].gpu_mem_size, NWLC->NwUnits, 1024), NAFLG_FMT_HUMAN_SIZE);
NWL_GetHumanSize(info.Device[i].gpu_mem_size, NWLC->NwUnits, 1024), NAFLG_FMT_HUMAN_SIZE);
}
}

Expand Down
33 changes: 29 additions & 4 deletions libnw/libnw.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ typedef struct _NWLIB_CONTEXT
PDH_HCOUNTER PdhNetRecv;
PDH_HCOUNTER PdhNetSend;
PDH_HCOUNTER PdhGpuUsage;
PDH_HCOUNTER PdhGpuCurMem;

struct wr0_drv_t* NwDrv;
UINT CodePage;
Expand Down Expand Up @@ -142,6 +143,7 @@ VOID NW_Fini(VOID);
VOID NWL_PdhInit(VOID);
VOID NWL_PdhUpdate(VOID);
VOID NWL_PdhFini(VOID);
PDH_FMT_COUNTERVALUE NWL_GetPdhSum(PDH_HCOUNTER counter, DWORD fmt, LPCWSTR suffix);

noreturn VOID NWL_ErrExit(INT nExitCode, LPCSTR lpszText);

Expand Down Expand Up @@ -232,7 +234,7 @@ typedef struct _NWLIB_CUR_DISPLAY
} NWLIB_CUR_DISPLAY;
VOID NWL_GetCurDisplay(HWND wnd, NWLIB_CUR_DISPLAY* info);

typedef struct _NWLIB_GPU_INFO
typedef struct _NWLIB_GPU_DEV
{
BOOL driver;
CHAR gpu_if[NWL_STR_SIZE];
Expand All @@ -243,11 +245,34 @@ typedef struct _NWLIB_GPU_INFO
CHAR gpu_driver_ver[NWL_STR_SIZE];
CHAR gpu_location[NWL_STR_SIZE];
UINT64 gpu_mem_size;
} NWLIB_GPU_INFO;
} NWLIB_GPU_DEV;

#define NWL_GPU_MAX_COUNT 8
int NWL_GetGpuInfo(NWLIB_GPU_INFO* info, int count);

double NWL_GetGpuUsage(LPCWSTR suffix);
typedef struct _NWLIB_GPU_INFO
{
int DeviceCount;
NWLIB_GPU_DEV Device[NWL_GPU_MAX_COUNT];
double Usage3D;
double UsageCopy;
double UsageCompute0;
double UsageCompute1;
#if 0
double UsageHighPriority3D;
double UsageHighPriorityCompute;
double UsageTrueAudio0;
double UsageTrueAudio1;
double UsageVideoCodec0;
double UsageVideoJPEG;
double UsageTimer0;
double UsageSecurity1;
#endif
double UsageDedicated;
UINT64 DedicatedTotal;
UINT64 DedicatedInUse;
} NWLIB_GPU_INFO, * PNWLIB_GPU_INFO;

VOID NWL_GetGpuInfo(PNWLIB_GPU_INFO info);

#define NWL_Debugf(...) \
do \
Expand Down
33 changes: 2 additions & 31 deletions libnw/network.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,35 +21,6 @@ static const char* bps_human_sizes[6] =

static UINT64 total_recv = 0, total_send = 0;

static UINT64
GetPdhLargeSum(PDH_HCOUNTER counter)
{
PDH_STATUS status = ERROR_SUCCESS;
DWORD dwBufferSize = 0;
DWORD dwItemCount = 0;
PDH_FMT_COUNTERVALUE_ITEM* pItems = NULL;
UINT64 ret = 0;

status = NWLC->PdhGetFormattedCounterArrayW(counter, PDH_FMT_LARGE, &dwBufferSize, &dwItemCount, pItems);
if (status != PDH_MORE_DATA)
return 0;
pItems = malloc(dwBufferSize);
if (!pItems)
return 0;
status = NWLC->PdhGetFormattedCounterArrayW(counter, PDH_FMT_LARGE, &dwBufferSize, &dwItemCount, pItems);
if (status != ERROR_SUCCESS)
goto out;
for (DWORD i = 0; i < dwItemCount; i++)
{
ret += pItems[i].FmtValue.largeValue;
}

out:
if (pItems)
free(pItems);
return ret;
}

VOID
NWL_GetNetTraffic(NWLIB_NET_TRAFFIC* info, BOOL bit)
{
Expand All @@ -66,9 +37,9 @@ NWL_GetNetTraffic(NWLIB_NET_TRAFFIC* info, BOOL bit)
old_send = total_send;

if (NWLC->PdhNetRecv)
diff_recv = GetPdhLargeSum(NWLC->PdhNetRecv);
diff_recv = NWL_GetPdhSum(NWLC->PdhNetRecv, PDH_FMT_LARGE, NULL).largeValue;
if (NWLC->PdhNetSend)
diff_send = GetPdhLargeSum(NWLC->PdhNetSend);
diff_send = NWL_GetPdhSum(NWLC->PdhNetSend, PDH_FMT_LARGE, NULL).largeValue;
if (bit)
{
memcpy(info->StrRecv, NWL_GetHumanSize(diff_recv * 8, bit_units, 1000), NWL_STR_SIZE);
Expand Down
Loading

1 comment on commit 19bfa0d

@a1ive
Copy link
Owner Author

@a1ive a1ive commented on 19bfa0d Aug 8, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#36

Please sign in to comment.