Basic sysfs support to read temp and read/write fan speed for amd-gpu-pro

pull/574/head
jsteube 8 years ago
parent 720b307ed7
commit 9eb9543cda

@ -31,6 +31,8 @@ int sort_by_stringptr (const void *p1, const void *p2);
int count_dictionaries (char **dictionary_files);
char *first_file_in_directory (const char *path);
char **scan_directory (hashcat_ctx_t *hashcat_ctx, const char *path);
int folder_config_init (hashcat_ctx_t *hashcat_ctx, MAYBE_UNUSED const char *install_folder, MAYBE_UNUSED const char *shared_folder);

@ -3,6 +3,8 @@
* License.....: MIT
*/
#include <errno.h>
#ifndef _HWMON_H
#define _HWMON_H
@ -19,6 +21,7 @@ int hm_get_throttle_with_device_id (hashcat_ctx_t *hashcat_ctx, const
int hm_set_fanspeed_with_device_id_adl (hashcat_ctx_t *hashcat_ctx, const u32 device_id, const int fanspeed, const int fanpolicy);
int hm_set_fanspeed_with_device_id_nvapi (hashcat_ctx_t *hashcat_ctx, const u32 device_id, const int fanspeed, const int fanpolicy);
int hm_set_fanspeed_with_device_id_xnvctrl (hashcat_ctx_t *hashcat_ctx, const u32 device_id, const int fanspeed);
int hm_set_fanspeed_with_device_id_sysfs (hashcat_ctx_t *hashcat_ctx, const u32 device_id, const int fanspeed);
int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx);
void hwmon_ctx_destroy (hashcat_ctx_t *hashcat_ctx);

@ -1046,6 +1046,7 @@ typedef struct opencl_ctx
bool need_nvml;
bool need_nvapi;
bool need_xnvctrl;
bool need_sysfs;
int force_jit_compilation;
@ -1055,6 +1056,7 @@ typedef struct opencl_ctx
#include "ext_nvapi.h"
#include "ext_nvml.h"
#include "ext_xnvctrl.h"
#include "ext_sysfs.h"
typedef struct hm_attrs
{
@ -1062,6 +1064,7 @@ typedef struct hm_attrs
HM_ADAPTER_NVML nvml;
HM_ADAPTER_NVAPI nvapi;
HM_ADAPTER_XNVCTRL xnvctrl;
HM_ADAPTER_SYSFS sysfs;
int od_version;
@ -1078,6 +1081,7 @@ typedef struct hwmon_ctx
void *hm_nvml;
void *hm_nvapi;
void *hm_xnvctrl;
void *hm_sysfs;
hm_attrs_t *hm_device;

@ -253,7 +253,7 @@ include $(CRT_GLOB_INCLUDE_FOLDER)/win_file_globbing.mk
## Objects
##
OBJS_ALL := affinity autotune benchmark bitmap bitops combinator common convert cpt cpu_aes cpu_crc32 cpu_des cpu_md4 cpu_md5 cpu_sha1 cpu_sha256 debugfile dictstat dispatch dynloader event ext_ADL ext_nvapi ext_nvml ext_OpenCL ext_xnvctrl filehandling folder hashcat hashes hlfmt hwmon induct interface locking logfile loopback memory monitor mpsp opencl outfile_check outfile potfile restore rp rp_cpu rp_kernel_on_cpu shared status stdout straight terminal thread timer tuningdb usage user_options weak_hash wordlist
OBJS_ALL := affinity autotune benchmark bitmap bitops combinator common convert cpt cpu_aes cpu_crc32 cpu_des cpu_md4 cpu_md5 cpu_sha1 cpu_sha256 debugfile dictstat dispatch dynloader event ext_ADL ext_nvapi ext_nvml ext_OpenCL ext_sysfs ext_xnvctrl filehandling folder hashcat hashes hlfmt hwmon induct interface locking logfile loopback memory monitor mpsp opencl outfile_check outfile potfile restore rp rp_cpu rp_kernel_on_cpu shared status stdout straight terminal thread timer tuningdb usage user_options weak_hash wordlist
NATIVE_OBJS := $(foreach OBJ,$(OBJS_ALL),obj/$(OBJ).NATIVE.o)
NATIVE_SHARED_OBJS := $(foreach OBJ,$(OBJS_ALL),obj/$(OBJ).NATIVE.SHARED.o)

@ -115,6 +115,56 @@ int count_dictionaries (char **dictionary_files)
return (cnt);
}
char *first_file_in_directory (const char *path)
{
DIR *d = NULL;
if ((d = opendir (path)) != NULL)
{
char *first_file = NULL;
#if defined (__APPLE__)
struct dirent e;
for (;;)
{
memset (&e, 0, sizeof (e));
struct dirent *de = NULL;
if (readdir_r (d, &e, &de) != 0) break;
if (de == NULL) break;
#else
struct dirent *de;
while ((de = readdir (d)) != NULL)
{
#endif
if ((strcmp (de->d_name, ".") == 0) || (strcmp (de->d_name, "..") == 0)) continue;
first_file = strdup (de->d_name);
break;
}
closedir (d);
return first_file;
}
else if (errno == ENOTDIR)
{
return NULL;
}
return NULL;
}
char **scan_directory (hashcat_ctx_t *hashcat_ctx, const char *path)
{
char *tmp_path = hcstrdup (hashcat_ctx, path);

@ -8,8 +8,241 @@
#include "memory.h"
#include "event.h"
#include "dynloader.h"
#include "shared.h"
#include "folder.h"
#include "hwmon.h"
// sysfs functions
static int sysfs_init (hashcat_ctx_t *hashcat_ctx)
{
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
SYSFS_PTR *sysfs = hwmon_ctx->hm_sysfs;
memset (sysfs, 0, sizeof (SYSFS_PTR));
char *path = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY);
snprintf (path, HCBUFSIZ_TINY - 1, "%s", SYS_BUS_PCI_DEVICES);
hc_stat_t s;
int rc = hc_stat (path, &s);
hcfree (path);
return rc;
}
static void sysfs_close (hashcat_ctx_t *hashcat_ctx)
{
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
SYSFS_PTR *sysfs = hwmon_ctx->hm_sysfs;
if (sysfs)
{
hcfree (sysfs);
}
return;
}
static char *hm_SYSFS_get_syspath (hashcat_ctx_t *hashcat_ctx, const int device_id)
{
opencl_ctx_t *opencl_ctx = hashcat_ctx->opencl_ctx;
hc_device_param_t *device_param = &opencl_ctx->devices_param[device_id];
char *syspath = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY);
snprintf (syspath, HCBUFSIZ_TINY - 1, "%s/0000:%02x:%02x.%01x/hwmon", SYS_BUS_PCI_DEVICES, device_param->pcie_bus, device_param->pcie_device, device_param->pcie_function);
char *hwmonN = first_file_in_directory (syspath);
if (hwmonN == NULL)
{
event_log_error (hashcat_ctx, "first_file_in_directory (%s) failed", syspath);
return NULL;
}
snprintf (syspath, HCBUFSIZ_TINY - 1, "%s/0000:%02x:%02x.%01x/hwmon/%s", SYS_BUS_PCI_DEVICES, device_param->pcie_bus, device_param->pcie_device, device_param->pcie_function, hwmonN);
hcfree (hwmonN);
return syspath;
}
static int hm_SYSFS_get_fan_speed_current (hashcat_ctx_t *hashcat_ctx, const int device_id, int *val)
{
char *syspath = hm_SYSFS_get_syspath (hashcat_ctx, device_id);
if (syspath == NULL) return -1;
char *path_cur = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY);
char *path_max = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY);
snprintf (path_cur, HCBUFSIZ_TINY - 1, "%s/pwm1", syspath);
snprintf (path_max, HCBUFSIZ_TINY - 1, "%s/pwm1_max", syspath);
FILE *fd_cur = fopen (path_cur, "r");
if (fd_cur == NULL)
{
event_log_error (hashcat_ctx, "%s: %s", path_cur, strerror (errno));
return -1;
}
int pwm1_cur = 0;
if (fscanf (fd_cur, "%d", &pwm1_cur) != 1)
{
event_log_error (hashcat_ctx, "%s: unexpected data", path_cur);
return -1;
}
fclose (fd_cur);
FILE *fd_max = fopen (path_max, "r");
if (fd_max == NULL)
{
event_log_error (hashcat_ctx, "%s: %s", path_max, strerror (errno));
return -1;
}
int pwm1_max = 0;
if (fscanf (fd_max, "%d", &pwm1_max) != 1)
{
event_log_error (hashcat_ctx, "%s: unexpected data", path_max);
return -1;
}
fclose (fd_max);
float pwm1_percent = ((float) pwm1_cur / (float) pwm1_max) * 100.0f;
*val = (int) pwm1_percent;
hcfree (syspath);
hcfree (path_cur);
hcfree (path_max);
return 0;
}
static int hm_SYSFS_set_fan_control (hashcat_ctx_t *hashcat_ctx, const int device_id, int val)
{
char *syspath = hm_SYSFS_get_syspath (hashcat_ctx, device_id);
if (syspath == NULL) return -1;
char *path = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY);
snprintf (path, HCBUFSIZ_TINY - 1, "%s/pwm1_enable", syspath);
FILE *fd = fopen (path, "w");
if (fd == NULL)
{
event_log_error (hashcat_ctx, "%s: %s", path, strerror (errno));
return -1;
}
fprintf (fd, "%d", val);
fclose (fd);
hcfree (syspath);
hcfree (path);
return 0;
}
static int hm_SYSFS_set_fan_speed_target (hashcat_ctx_t *hashcat_ctx, const int device_id, int val)
{
char *syspath = hm_SYSFS_get_syspath (hashcat_ctx, device_id);
if (syspath == NULL) return -1;
char *path = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY);
snprintf (path, HCBUFSIZ_TINY - 1, "%s/pwm1", syspath);
FILE *fd = fopen (path, "w");
if (fd == NULL)
{
event_log_error (hashcat_ctx, "%s: %s", path, strerror (errno));
return -1;
}
val = (float) val * 2.55f; // should be pwm1_max
fprintf (fd, "%d", val);
fclose (fd);
hcfree (syspath);
hcfree (path);
return 0;
}
static int hm_SYSFS_get_temperature_current (hashcat_ctx_t *hashcat_ctx, const int device_id, int *val)
{
char *syspath = hm_SYSFS_get_syspath (hashcat_ctx, device_id);
if (syspath == NULL) return -1;
char *path = hcmalloc (hashcat_ctx, HCBUFSIZ_TINY);
snprintf (path, HCBUFSIZ_TINY - 1, "%s/temp1_input", syspath);
FILE *fd = fopen (path, "r");
if (fd == NULL)
{
event_log_error (hashcat_ctx, "%s: %s", path, strerror (errno));
return -1;
}
int temperature = 0;
if (fscanf (fd, "%d", &temperature) != 1)
{
event_log_error (hashcat_ctx, "%s: unexpected data", path);
return -1;
}
fclose (fd);
*val = temperature / 1000;
hcfree (syspath);
hcfree (path);
return 0;
}
// nvml functions
static int nvml_init (hashcat_ctx_t *hashcat_ctx)
@ -2236,6 +2469,15 @@ int hm_get_temperature_with_device_id (hashcat_ctx_t *hashcat_ctx, const u32 dev
return Temperature / 1000;
}
}
if (hwmon_ctx->hm_sysfs)
{
int temperature = 0;
if (hm_SYSFS_get_temperature_current (hashcat_ctx, hwmon_ctx->hm_device[device_id].sysfs, &temperature) == -1) return -1;
return temperature;
}
}
if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_NV)
@ -2283,6 +2525,11 @@ int hm_get_fanpolicy_with_device_id (hashcat_ctx_t *hashcat_ctx, const u32 devic
return 1;
}
}
if (hwmon_ctx->hm_sysfs)
{
return 1;
}
}
if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_NV)
@ -2334,6 +2581,15 @@ int hm_get_fanspeed_with_device_id (hashcat_ctx_t *hashcat_ctx, const u32 device
return faninfo.iFanSpeedPercent;
}
}
if (hwmon_ctx->hm_sysfs)
{
int speed = 0;
if (hm_SYSFS_get_fan_speed_current (hashcat_ctx, hwmon_ctx->hm_device[device_id].sysfs, &speed) == -1) return -1;
return speed;
}
}
if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_NV)
@ -2676,6 +2932,25 @@ int hm_set_fanspeed_with_device_id_xnvctrl (hashcat_ctx_t *hashcat_ctx, const u3
return -1;
}
int hm_set_fanspeed_with_device_id_sysfs (hashcat_ctx_t *hashcat_ctx, const u32 device_id, const int fanspeed)
{
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
if (hwmon_ctx->enabled == false) return -1;
if (hwmon_ctx->hm_device[device_id].fan_set_supported == true)
{
if (hwmon_ctx->hm_sysfs)
{
if (hm_SYSFS_set_fan_speed_target (hashcat_ctx, hwmon_ctx->hm_device[device_id].sysfs, fanspeed) == -1) return -1;
return 0;
}
}
return -1;
}
static int hm_set_fanctrl_with_device_id_xnvctrl (hashcat_ctx_t *hashcat_ctx, const u32 device_id, const int val)
{
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
@ -2695,6 +2970,25 @@ static int hm_set_fanctrl_with_device_id_xnvctrl (hashcat_ctx_t *hashcat_ctx, co
return -1;
}
static int hm_set_fanctrl_with_device_id_sysfs (hashcat_ctx_t *hashcat_ctx, const u32 device_id, const int val)
{
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
if (hwmon_ctx->enabled == false) return -1;
if (hwmon_ctx->hm_device[device_id].fan_set_supported == true)
{
if (hwmon_ctx->hm_sysfs)
{
if (hm_SYSFS_set_fan_control (hashcat_ctx, hwmon_ctx->hm_device[device_id].sysfs, val) == -1) return -1;
return 0;
}
}
return -1;
}
int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
{
hwmon_ctx_t *hwmon_ctx = hashcat_ctx->hwmon_ctx;
@ -2722,11 +3016,13 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
NVAPI_PTR *nvapi = (NVAPI_PTR *) hcmalloc (hashcat_ctx, sizeof (NVAPI_PTR));
NVML_PTR *nvml = (NVML_PTR *) hcmalloc (hashcat_ctx, sizeof (NVML_PTR));
XNVCTRL_PTR *xnvctrl = (XNVCTRL_PTR *) hcmalloc (hashcat_ctx, sizeof (XNVCTRL_PTR));
SYSFS_PTR *sysfs = (SYSFS_PTR *) hcmalloc (hashcat_ctx, sizeof (SYSFS_PTR));
hm_attrs_t *hm_adapters_adl = (hm_attrs_t *) hccalloc (hashcat_ctx, DEVICES_MAX, sizeof (hm_attrs_t)); VERIFY_PTR (hm_adapters_adl);
hm_attrs_t *hm_adapters_nvapi = (hm_attrs_t *) hccalloc (hashcat_ctx, DEVICES_MAX, sizeof (hm_attrs_t)); VERIFY_PTR (hm_adapters_nvapi);
hm_attrs_t *hm_adapters_nvml = (hm_attrs_t *) hccalloc (hashcat_ctx, DEVICES_MAX, sizeof (hm_attrs_t)); VERIFY_PTR (hm_adapters_nvml);
hm_attrs_t *hm_adapters_xnvctrl = (hm_attrs_t *) hccalloc (hashcat_ctx, DEVICES_MAX, sizeof (hm_attrs_t)); VERIFY_PTR (hm_adapters_xnvctrl);
hm_attrs_t *hm_adapters_sysfs = (hm_attrs_t *) hccalloc (hashcat_ctx, DEVICES_MAX, sizeof (hm_attrs_t)); VERIFY_PTR (hm_adapters_sysfs);
if (opencl_ctx->need_nvml == true)
{
@ -2776,6 +3072,27 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
}
}
if (opencl_ctx->need_sysfs == true)
{
hwmon_ctx->hm_sysfs = sysfs;
if (sysfs_init (hashcat_ctx) == -1)
{
hcfree (hwmon_ctx->hm_sysfs);
hwmon_ctx->hm_sysfs = NULL;
}
// also if there's ADL, we don't need sysfs
if (hwmon_ctx->hm_adl)
{
hcfree (hwmon_ctx->hm_sysfs);
hwmon_ctx->hm_sysfs = NULL;
}
}
if (hwmon_ctx->hm_nvml)
{
if (hm_NVML_nvmlInit (hashcat_ctx) == 0)
@ -2883,7 +3200,26 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
}
}
if (hwmon_ctx->hm_adl == NULL && hwmon_ctx->hm_nvml == NULL && hwmon_ctx->hm_xnvctrl == NULL)
if (hwmon_ctx->hm_sysfs)
{
if (1)
{
for (u32 device_id = 0; device_id < opencl_ctx->devices_cnt; device_id++)
{
hc_device_param_t *device_param = &opencl_ctx->devices_param[device_id];
if ((device_param->device_type & CL_DEVICE_TYPE_GPU) == 0) continue;
hm_adapters_sysfs[device_id].sysfs = device_id;
int speed = 0;
if (hm_SYSFS_get_fan_speed_current (hashcat_ctx, device_id, &speed) == 0) hm_adapters_sysfs[device_id].fan_get_supported = true;
}
}
}
if (hwmon_ctx->hm_adl == NULL && hwmon_ctx->hm_nvml == NULL && hwmon_ctx->hm_xnvctrl == NULL && hwmon_ctx->hm_sysfs == NULL)
{
return 0;
}
@ -2921,17 +3257,20 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
if (device_param->device_vendor_id == VENDOR_ID_AMD)
{
hwmon_ctx->hm_device[device_id].adl = hm_adapters_adl[platform_devices_id].adl;
hwmon_ctx->hm_device[device_id].sysfs = hm_adapters_sysfs[platform_devices_id].sysfs;
hwmon_ctx->hm_device[device_id].nvapi = 0;
hwmon_ctx->hm_device[device_id].nvml = 0;
hwmon_ctx->hm_device[device_id].xnvctrl = 0;
hwmon_ctx->hm_device[device_id].od_version = hm_adapters_adl[platform_devices_id].od_version;
hwmon_ctx->hm_device[device_id].fan_get_supported = hm_adapters_adl[platform_devices_id].fan_get_supported;
hwmon_ctx->hm_device[device_id].fan_get_supported = hm_adapters_adl[platform_devices_id].fan_get_supported
| hm_adapters_sysfs[platform_devices_id].fan_get_supported;
hwmon_ctx->hm_device[device_id].fan_set_supported = false;
}
if (device_param->device_vendor_id == VENDOR_ID_NV)
{
hwmon_ctx->hm_device[device_id].adl = 0;
hwmon_ctx->hm_device[device_id].sysfs = 0;
hwmon_ctx->hm_device[device_id].nvapi = hm_adapters_nvapi[platform_devices_id].nvapi;
hwmon_ctx->hm_device[device_id].nvml = hm_adapters_nvml[platform_devices_id].nvml;
hwmon_ctx->hm_device[device_id].xnvctrl = hm_adapters_xnvctrl[platform_devices_id].xnvctrl;
@ -2945,6 +3284,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
hcfree (hm_adapters_nvapi);
hcfree (hm_adapters_nvml);
hcfree (hm_adapters_xnvctrl);
hcfree (hm_adapters_sysfs);
/**
* powertune on user request
@ -3160,7 +3500,15 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx)
if (device_param->device_vendor_id == VENDOR_ID_AMD)
{
rc = hm_set_fanspeed_with_device_id_adl (hashcat_ctx, device_id, fanspeed, 1);
if (hwmon_ctx->hm_adl)
{
rc = hm_set_fanspeed_with_device_id_adl (hashcat_ctx, device_id, fanspeed, 1);
}
if (hwmon_ctx->hm_sysfs)
{
rc = hm_set_fanctrl_with_device_id_sysfs (hashcat_ctx, device_id, 1);
}
}
else if (device_param->device_vendor_id == VENDOR_ID_NV)
{
@ -3221,7 +3569,15 @@ void hwmon_ctx_destroy (hashcat_ctx_t *hashcat_ctx)
if (device_param->device_vendor_id == VENDOR_ID_AMD)
{
rc = hm_set_fanspeed_with_device_id_adl (hashcat_ctx, device_id, 100, 0);
if (hwmon_ctx->hm_adl)
{
rc = hm_set_fanspeed_with_device_id_adl (hashcat_ctx, device_id, 100, 0);
}
if (hwmon_ctx->hm_sysfs)
{
rc = hm_set_fanctrl_with_device_id_sysfs (hashcat_ctx, device_id, 2);
}
}
else if (device_param->device_vendor_id == VENDOR_ID_NV)
{
@ -3339,6 +3695,12 @@ void hwmon_ctx_destroy (hashcat_ctx_t *hashcat_ctx)
adl_close (hashcat_ctx);
}
if (hwmon_ctx->hm_sysfs)
{
sysfs_close (hashcat_ctx);
}
// free memory
hcfree (hwmon_ctx->nvml_power_limit);

@ -223,7 +223,8 @@ static int monitor (hashcat_ctx_t *hashcat_ctx)
{
if (device_param->device_vendor_id == VENDOR_ID_AMD)
{
hm_set_fanspeed_with_device_id_adl (hashcat_ctx, device_id, fan_speed_new, 1);
hm_set_fanspeed_with_device_id_adl (hashcat_ctx, device_id, fan_speed_new, 1);
hm_set_fanspeed_with_device_id_sysfs (hashcat_ctx, device_id, fan_speed_new);
}
else if (device_param->device_vendor_id == VENDOR_ID_NV)
{

@ -2126,6 +2126,7 @@ int opencl_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime)
bool need_nvml = false;
bool need_nvapi = false;
bool need_xnvctrl = false;
bool need_sysfs = false;
u32 devices_cnt = 0;
@ -2590,6 +2591,10 @@ int opencl_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime)
if ((device_param->platform_vendor_id == VENDOR_ID_AMD) && (device_param->device_vendor_id == VENDOR_ID_AMD))
{
need_adl = true;
#if defined (__linux__)
need_sysfs = true;
#endif
}
if ((device_param->platform_vendor_id == VENDOR_ID_NV) && (device_param->device_vendor_id == VENDOR_ID_NV))
@ -2792,6 +2797,7 @@ int opencl_ctx_devices_init (hashcat_ctx_t *hashcat_ctx, const int comptime)
opencl_ctx->need_nvml = need_nvml;
opencl_ctx->need_nvapi = need_nvapi;
opencl_ctx->need_xnvctrl = need_xnvctrl;
opencl_ctx->need_sysfs = need_sysfs;
return 0;
}
@ -2829,6 +2835,7 @@ void opencl_ctx_devices_destroy (hashcat_ctx_t *hashcat_ctx)
opencl_ctx->need_nvml = false;
opencl_ctx->need_nvapi = false;
opencl_ctx->need_xnvctrl = false;
opencl_ctx->need_sysfs = false;
}
void opencl_ctx_devices_update_power (hashcat_ctx_t *hashcat_ctx)

Loading…
Cancel
Save