Skip to content

Commit

Permalink
refresh pl
Browse files Browse the repository at this point in the history
  • Loading branch information
a1ive committed Jan 15, 2024
1 parent cc8b14c commit 82c7f84
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 105 deletions.
7 changes: 7 additions & 0 deletions gnwinfo/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,13 @@ get_cpu_info(int index)
value = cpu_msrinfo(g_ctx.lib.NwDrv, INFO_BUS_CLOCK);
if (value != CPU_INVALID_VALUE && value > 0)
g_ctx.cpu_info[index].cpu_msr_bus = value / 100.0;
value = cpu_msrinfo(g_ctx.lib.NwDrv, INFO_PKG_PL1);
if (value != CPU_INVALID_VALUE && value > 0)
g_ctx.cpu_info[index].cpu_msr_pl1 = value / 100.0;
value = cpu_msrinfo(g_ctx.lib.NwDrv, INFO_PKG_PL2);
if (value != CPU_INVALID_VALUE && value > 0)
g_ctx.cpu_info[index].cpu_msr_pl2 = value / 100.0;

break;
}
}
Expand Down
18 changes: 4 additions & 14 deletions gnwinfo/cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,6 @@ draw_msr(struct nk_context* ctx, int index, PNODE cpu, LPCSTR brand)
{
if (nk_group_begin(ctx, gnwinfo_get_text(L"MSR"), NK_WINDOW_BORDER | NK_WINDOW_TITLE))
{
const char* pl;
const float ratio[] = { 0.5f, 0.5f };
nk_layout_row(ctx, NK_DYNAMIC, 0, 2, ratio);

Expand All @@ -85,19 +84,10 @@ draw_msr(struct nk_context* ctx, int index, PNODE cpu, LPCSTR brand)
nk_lhcf(ctx, NK_TEXT_LEFT, g_color_text_l, "%.2f V", g_ctx.cpu_info[index].cpu_msr_volt);
nk_l(ctx, gnwinfo_get_text(L"Power"), NK_TEXT_LEFT);
nk_lhcf(ctx, NK_TEXT_LEFT, g_color_text_l, "%.2f W", g_ctx.cpu_info[index].cpu_msr_power);

pl = gnwinfo_get_node_attr(cpu, "PL1 (W)");
if (pl[0] != '-')
{
nk_l(ctx, gnwinfo_get_text(L"PL1"), NK_TEXT_LEFT);
nk_lhcf(ctx, NK_TEXT_LEFT, g_color_text_l, "%s W", pl);
}
pl = gnwinfo_get_node_attr(cpu, "PL2 (W)");
if (pl[0] != '-')
{
nk_l(ctx, gnwinfo_get_text(L"PL2"), NK_TEXT_LEFT);
nk_lhcf(ctx, NK_TEXT_LEFT, g_color_text_l, "%s W", pl);
}
nk_l(ctx, gnwinfo_get_text(L"PL1"), NK_TEXT_LEFT);
nk_lhcf(ctx, NK_TEXT_LEFT, g_color_text_l, "%.2f W", g_ctx.cpu_info[index].cpu_msr_pl1);
nk_l(ctx, gnwinfo_get_text(L"PL2"), NK_TEXT_LEFT);
nk_lhcf(ctx, NK_TEXT_LEFT, g_color_text_l, "%.2f W", g_ctx.cpu_info[index].cpu_msr_pl2);

nk_group_end(ctx);
}
Expand Down
2 changes: 2 additions & 0 deletions gnwinfo/gnwinfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ typedef struct _GNW_CPU_INFO
double cpu_msr_bus;
int cpu_energy;
double cpu_msr_power;
double cpu_msr_pl1;
double cpu_msr_pl2;
}GNW_CPU_INFO;

#define GUI_WINDOW_CPUID (1U << 0)
Expand Down
100 changes: 100 additions & 0 deletions libcpuid/rdmsr.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@
#include <windows.h>
#include <winioctl.h>
#include <winerror.h>
#include <pathcch.h>

#include "../ryzenadj/ryzenadj.h"

#ifdef _WIN64
#define RY_DLL L"ryzenadjx64.dll"
#else
#define RY_DLL L"ryzenadj.dll"
#endif

#ifndef RDMSR_UNSUPPORTED_OS

Expand Down Expand Up @@ -149,6 +158,7 @@ static const uint32_t intel_msr[] = {

struct msr_info_t {
int cpu_clock;
bool ryzenadj;
struct wr0_drv_t *handle;
struct cpu_id_t *id;
struct internal_id_info_t *internal;
Expand Down Expand Up @@ -407,6 +417,76 @@ static double get_info_max_multiplier(struct msr_info_t *info)
return (double) CPU_INVALID_VALUE / 100;
}

static struct
{
HMODULE dll;
ryzen_access ry;
ryzen_access(CALL* init_ryzenadj)(VOID);
void (CALL* cleanup_ryzenadj)(ryzen_access ry);
int (CALL* init_table)(ryzen_access ry);
uint32_t(CALL* get_table_ver)(ryzen_access ry);
size_t(CALL* get_table_size)(ryzen_access ry);
float* (CALL* get_table_values)(ryzen_access ry);
int (CALL* refresh_table)(ryzen_access ry);
float (CALL* get_stapm_limit)(ryzen_access ry);
float (CALL* get_fast_limit)(ryzen_access ry);
float (CALL* get_slow_limit)(ryzen_access ry);
} m_ry;

static void
ry_fini(void)
{
if (m_ry.ry)
m_ry.cleanup_ryzenadj(m_ry.ry);
if (m_ry.dll)
FreeLibrary(m_ry.dll);
ZeroMemory(&m_ry, sizeof(m_ry));
}

static bool
ry_init(void)
{
WCHAR dll_path[MAX_PATH];
GetModuleFileNameW(NULL, dll_path, MAX_PATH);
PathCchRemoveFileSpec(dll_path, MAX_PATH);
PathCchAppend(dll_path, MAX_PATH, RY_DLL);
m_ry.dll = LoadLibraryW(dll_path);
if (!m_ry.dll)
goto fail;
*(FARPROC*)&m_ry.init_ryzenadj = GetProcAddress(m_ry.dll, "init_ryzenadj");
*(FARPROC*)&m_ry.cleanup_ryzenadj = GetProcAddress(m_ry.dll, "cleanup_ryzenadj");
*(FARPROC*)&m_ry.init_table = GetProcAddress(m_ry.dll, "init_table");
*(FARPROC*)&m_ry.get_table_ver = GetProcAddress(m_ry.dll, "get_table_ver");
*(FARPROC*)&m_ry.get_table_size = GetProcAddress(m_ry.dll, "get_table_size");
*(FARPROC*)&m_ry.get_table_values = GetProcAddress(m_ry.dll, "get_table_values");
*(FARPROC*)&m_ry.refresh_table = GetProcAddress(m_ry.dll, "refresh_table");
*(FARPROC*)&m_ry.get_stapm_limit = GetProcAddress(m_ry.dll, "get_stapm_limit");
*(FARPROC*)&m_ry.get_fast_limit = GetProcAddress(m_ry.dll, "get_fast_limit");
*(FARPROC*)&m_ry.get_slow_limit = GetProcAddress(m_ry.dll, "get_slow_limit");
if (m_ry.init_ryzenadj == NULL ||
m_ry.cleanup_ryzenadj == NULL ||
m_ry.init_table == NULL ||
m_ry.get_table_ver == NULL ||
m_ry.get_table_size == NULL ||
m_ry.get_table_values == NULL ||
m_ry.refresh_table == NULL ||
m_ry.get_stapm_limit == NULL ||
m_ry.get_fast_limit == NULL ||
m_ry.get_slow_limit == NULL)
{
goto fail;
}
m_ry.ry = m_ry.init_ryzenadj();
if (m_ry.ry == NULL)
goto fail;
if (m_ry.init_table(m_ry.ry))
goto fail;
return TRUE;
fail:
ry_fini();
return FALSE;
}

static int get_info_temperature(struct msr_info_t *info)
{
int err;
Expand Down Expand Up @@ -646,6 +726,16 @@ static double get_info_pkg_pl1(struct msr_info_t* info)
err += cpu_rdmsr_range(info->handle, MSR_RAPL_POWER_UNIT, 3, 0, &PowerUnits);
if (!err) return (double)PowerLimit1 / (1ULL << PowerUnits);
}
else if (info->id->vendor == VENDOR_AMD)
{
if (!info->ryzenadj)
info->ryzenadj = ry_init();
if (info->ryzenadj)
{
m_ry.refresh_table(m_ry.ry);
return m_ry.get_slow_limit(m_ry.ry);
}
}
return (double)CPU_INVALID_VALUE / 100;
}

Expand All @@ -658,6 +748,16 @@ static double get_info_pkg_pl2(struct msr_info_t* info)
err += cpu_rdmsr_range(info->handle, MSR_RAPL_POWER_UNIT, 3, 0, &PowerUnits);
if (!err) return (double)PowerLimit2 / (1ULL << PowerUnits);
}
else if (info->id->vendor == VENDOR_AMD)
{
if (!info->ryzenadj)
info->ryzenadj = ry_init();
if (info->ryzenadj)
{
m_ry.refresh_table(m_ry.ry);
return m_ry.get_slow_limit(m_ry.ry);
}
}
return (double)CPU_INVALID_VALUE / 100;
}

Expand Down
91 changes: 0 additions & 91 deletions libnw/cpuid.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,88 +7,9 @@
#include "libnw.h"
#include <libcpuid.h>
#include <libcpuid_util.h>
#include <pathcch.h>

#include "utils.h"

#include "../ryzenadj/ryzenadj.h"

#ifdef _WIN64
#define RY_DLL L"ryzenadjx64.dll"
#else
#define RY_DLL L"ryzenadj.dll"
#endif

static struct
{
HMODULE dll;
ryzen_access ry;
ryzen_access (CALL *init_ryzenadj)(VOID);
void (CALL* cleanup_ryzenadj)(ryzen_access ry);
int (CALL* init_table)(ryzen_access ry);
uint32_t (CALL *get_table_ver)(ryzen_access ry);
size_t (CALL *get_table_size)(ryzen_access ry);
float* (CALL *get_table_values)(ryzen_access ry);
int (CALL *refresh_table)(ryzen_access ry);
float (CALL *get_stapm_limit)(ryzen_access ry);
float (CALL *get_fast_limit)(ryzen_access ry);
float (CALL *get_slow_limit)(ryzen_access ry);
} m_ry;

static void
RyzenAdjCleanup(void)
{
if (m_ry.ry)
m_ry.cleanup_ryzenadj(m_ry.ry);
if (m_ry.dll)
FreeLibrary(m_ry.dll);
ZeroMemory(&m_ry, sizeof(m_ry));
}

static bool
RyzenAdjInit(void)
{
WCHAR dll_path[MAX_PATH];
GetModuleFileNameW(NULL, dll_path, MAX_PATH);
PathCchRemoveFileSpec(dll_path, MAX_PATH);
PathCchAppend(dll_path, MAX_PATH, RY_DLL);
m_ry.dll = LoadLibraryW(dll_path);
if (!m_ry.dll)
goto fail;
*(FARPROC*)&m_ry.init_ryzenadj = GetProcAddress(m_ry.dll, "init_ryzenadj");
*(FARPROC*)&m_ry.cleanup_ryzenadj = GetProcAddress(m_ry.dll, "cleanup_ryzenadj");
*(FARPROC*)&m_ry.init_table = GetProcAddress(m_ry.dll, "init_table");
*(FARPROC*)&m_ry.get_table_ver = GetProcAddress(m_ry.dll, "get_table_ver");
*(FARPROC*)&m_ry.get_table_size = GetProcAddress(m_ry.dll, "get_table_size");
*(FARPROC*)&m_ry.get_table_values = GetProcAddress(m_ry.dll, "get_table_values");
*(FARPROC*)&m_ry.refresh_table = GetProcAddress(m_ry.dll, "refresh_table");
*(FARPROC*)&m_ry.get_stapm_limit = GetProcAddress(m_ry.dll, "get_stapm_limit");
*(FARPROC*)&m_ry.get_fast_limit = GetProcAddress(m_ry.dll, "get_fast_limit");
*(FARPROC*)&m_ry.get_slow_limit = GetProcAddress(m_ry.dll, "get_slow_limit");
if (m_ry.init_ryzenadj == NULL ||
m_ry.cleanup_ryzenadj == NULL ||
m_ry.init_table == NULL ||
m_ry.get_table_ver == NULL ||
m_ry.get_table_size == NULL ||
m_ry.get_table_values == NULL ||
m_ry.refresh_table == NULL ||
m_ry.get_stapm_limit == NULL ||
m_ry.get_fast_limit == NULL ||
m_ry.get_slow_limit == NULL)
{
goto fail;
}
m_ry.ry = m_ry.init_ryzenadj();
if (m_ry.ry == NULL)
goto fail;
if (m_ry.init_table(m_ry.ry))
goto fail;
return TRUE;
fail:
RyzenAdjCleanup();
return FALSE;
}

static LPCSTR
CpuVendorToStr(cpu_vendor_t vendor)
{
Expand Down Expand Up @@ -190,13 +111,10 @@ PrintCpuMsr(PNODE node, struct cpu_id_t* data)
{
logical_cpu_t i;
bool affinity_saved = FALSE;
bool use_ryzenadj = FALSE;
int value = CPU_INVALID_VALUE;
if (!data->flags[CPU_FEATURE_MSR] || NWLC->NwDrv == NULL)
return;
affinity_saved = save_cpu_affinity();
if (data->vendor == VENDOR_AMD)
use_ryzenadj = RyzenAdjInit();
for (i = 0; i < data->num_logical_cpus; i++)
{
if (!get_affinity_mask_bit(i, &data->affinity_mask))
Expand Down Expand Up @@ -232,17 +150,8 @@ PrintCpuMsr(PNODE node, struct cpu_id_t* data)
value = cpu_msrinfo(NWLC->NwDrv, INFO_PKG_PL2);
if (value != CPU_INVALID_VALUE && value > 0)
NWL_NodeAttrSetf(node, "PL2 (W)", NAFLG_FMT_NUMERIC, "%.2lf", value / 100.0);
if (use_ryzenadj)
{
m_ry.refresh_table(m_ry.ry);
NWL_NodeAttrSetf(node, "STAPM Limit (W)", NAFLG_FMT_NUMERIC, "%.2lf", m_ry.get_stapm_limit(m_ry.ry));
NWL_NodeAttrSetf(node, "PL1 (W)", NAFLG_FMT_NUMERIC, "%.2lf", m_ry.get_slow_limit(m_ry.ry));
NWL_NodeAttrSetf(node, "PL2 (W)", NAFLG_FMT_NUMERIC, "%.2lf", m_ry.get_fast_limit(m_ry.ry));
}
break;
}
if (use_ryzenadj)
RyzenAdjCleanup();
if (affinity_saved)
restore_cpu_affinity();
}
Expand Down

0 comments on commit 82c7f84

Please sign in to comment.