From 719178029648a55a598cfe09e0e048eefa1c03e0 Mon Sep 17 00:00:00 2001 From: jsteube Date: Wed, 28 Sep 2016 22:28:44 +0200 Subject: [PATCH] Make use of hwmon_ctx_t --- include/hwmon.h | 50 +-- include/status.h | 4 +- include/types.h | 19 +- src/hashcat.c | 677 ++---------------------------------- src/hwmon.c | 851 +++++++++++++++++++++++++++++++++++---------- src/monitor.c | 21 +- src/opencl.c | 19 +- src/status.c | 22 +- src/terminal.c | 3 +- src/user_options.c | 6 + 10 files changed, 755 insertions(+), 917 deletions(-) diff --git a/include/hwmon.h b/include/hwmon.h index dca6644fd..27c698cbc 100644 --- a/include/hwmon.h +++ b/include/hwmon.h @@ -6,41 +6,21 @@ #ifndef _HWMON_H #define _HWMON_H -int get_adapters_num_adl (void *adl, int *iNumberAdapters); +int hm_get_threshold_slowdown_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id); +int hm_get_threshold_shutdown_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id); +int hm_get_temperature_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id); +int hm_get_fanpolicy_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id); +int hm_get_fanspeed_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id); +int hm_get_buslanes_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id); +int hm_get_utilization_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id); +int hm_get_memoryspeed_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id); +int hm_get_corespeed_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id); +int hm_get_throttle_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id); +int hm_set_fanspeed_with_device_id_adl (const hwmon_ctx_t *hwmon_ctx, const uint device_id, const int fanspeed, const int fanpolicy); +int hm_set_fanspeed_with_device_id_nvapi (const hwmon_ctx_t *hwmon_ctx, const uint device_id, const int fanspeed, const int fanpolicy); +int hm_set_fanspeed_with_device_id_xnvctrl (const hwmon_ctx_t *hwmon_ctx, const uint device_id, const int fanspeed); -int hm_get_adapter_index_adl (hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo); - -int hm_get_adapter_index_nvapi (HM_ADAPTER_NVAPI nvapiGPUHandle[DEVICES_MAX]); - -int hm_get_adapter_index_nvml (HM_ADAPTER_NVML nvmlGPUHandle[DEVICES_MAX]); - -LPAdapterInfo hm_get_adapter_info_adl (void *adl, int iNumberAdapters); - -u32 *hm_get_list_valid_adl_adapters (int iNumberAdapters, int *num_adl_adapters, LPAdapterInfo lpAdapterInfo); - -int hm_get_overdrive_version (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo); -int hm_check_fanspeed_control (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo); - -// int hm_get_device_num (void *adl, HM_ADAPTER_ADL hm_adapter_index, int *hm_device_num); -// void hm_get_opencl_busid_devid (hm_attrs_t *hm_device, uint opencl_num_devices, cl_device_id *devices); - -int hm_get_threshold_slowdown_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id); -int hm_get_threshold_shutdown_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id); -int hm_get_temperature_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id); -int hm_get_fanspeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id); -int hm_get_fanpolicy_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id); -int hm_get_buslanes_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id); -int hm_get_utilization_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id); -int hm_get_memoryspeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id); -int hm_get_corespeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id); -int hm_get_throttle_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id); -int hm_set_fanspeed_with_device_id_adl (const uint device_id, const int fanspeed, const int fanpolicy); -int hm_set_fanspeed_with_device_id_nvapi (const uint device_id, const int fanspeed, const int fanpolicy); -int hm_set_fanspeed_with_device_id_xnvctrl (const uint device_id, const int fanspeed); - -void hm_device_val_to_str (char *target_buf, int max_buf_size, char *suffix, int value); - -int hwmon_ctx_init (hwmon_ctx_t *hwmon_ctx, const user_options_t *user_options); -void hwmon_ctx_destroy (hwmon_ctx_t *hwmon_ctx); +int hwmon_ctx_init (hwmon_ctx_t *hwmon_ctx, const user_options_t *user_options, const opencl_ctx_t *opencl_ctx); +void hwmon_ctx_destroy (hwmon_ctx_t *hwmon_ctx, const user_options_t *user_options, const opencl_ctx_t *opencl_ctx); #endif // _HWMON_H diff --git a/include/status.h b/include/status.h index 18556911f..ccc3fc888 100644 --- a/include/status.h +++ b/include/status.h @@ -11,8 +11,8 @@ double get_avg_exec_time (hc_device_param_t *device_param, const int last_num_entries); -void status_display_machine_readable (opencl_ctx_t *opencl_ctx, const hashes_t *hashes, const restore_ctx_t *restore_ctx, const user_options_t *user_options, const user_options_extra_t *user_options_extra, const straight_ctx_t *straight_ctx, const combinator_ctx_t *combinator_ctx, const mask_ctx_t *mask_ctx); -void status_display (opencl_ctx_t *opencl_ctx, const hashconfig_t *hashconfig, const hashes_t *hashes, const restore_ctx_t *restore_ctx, const user_options_t *user_options, const user_options_extra_t *user_options_extra, const straight_ctx_t *straight_ctx, const combinator_ctx_t *combinator_ctx, const mask_ctx_t *mask_ctx); +void status_display_machine_readable (opencl_ctx_t *opencl_ctx, const hwmon_ctx_t *hwmon_ctx, const hashes_t *hashes, const restore_ctx_t *restore_ctx, const user_options_t *user_options, const user_options_extra_t *user_options_extra, const straight_ctx_t *straight_ctx, const combinator_ctx_t *combinator_ctx, const mask_ctx_t *mask_ctx); +void status_display (opencl_ctx_t *opencl_ctx, const hwmon_ctx_t *hwmon_ctx, const hashconfig_t *hashconfig, const hashes_t *hashes, const restore_ctx_t *restore_ctx, const user_options_t *user_options, const user_options_extra_t *user_options_extra, const straight_ctx_t *straight_ctx, const combinator_ctx_t *combinator_ctx, const mask_ctx_t *mask_ctx); void status_benchmark_automate (opencl_ctx_t *opencl_ctx, const hashconfig_t *hashconfig); void status_benchmark (opencl_ctx_t *opencl_ctx, const hashconfig_t *hashconfig, const user_options_t *user_options); diff --git a/include/types.h b/include/types.h index 268358d72..636fb75e7 100644 --- a/include/types.h +++ b/include/types.h @@ -809,8 +809,8 @@ typedef struct int od_version; - int fan_get_supported; - int fan_set_supported; + bool fan_get_supported; + bool fan_set_supported; } hm_attrs_t; @@ -1210,6 +1210,11 @@ typedef struct hm_attrs_t *hm_device; + ADLOD6MemClockState *od_clock_mem_status; + int *od_power_control_status; + unsigned int *nvml_power_limit; + + } hwmon_ctx_t; typedef struct @@ -1245,16 +1250,6 @@ typedef struct u32 shutdown_inner; u32 shutdown_outer; - /** - * hardware watchdog - */ - - void *hm_adl; - void *hm_nvml; - void *hm_nvapi; - void *hm_xnvctrl; - hm_attrs_t hm_device[DEVICES_MAX]; - /** * crack-per-time */ diff --git a/src/hashcat.c b/src/hashcat.c index 06c2fd876..0b3b0f3c7 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -224,7 +224,7 @@ static void goodbye_screen (const user_options_t *user_options, const time_t *pr log_info_nn ("Stopped: %s", ctime (proc_stop)); } -static int inner1_loop (user_options_t *user_options, user_options_extra_t *user_options_extra, restore_ctx_t *restore_ctx, logfile_ctx_t *logfile_ctx, induct_ctx_t *induct_ctx, dictstat_ctx_t *dictstat_ctx, loopback_ctx_t *loopback_ctx, opencl_ctx_t *opencl_ctx, hashconfig_t *hashconfig, hashes_t *hashes, wl_data_t *wl_data, straight_ctx_t *straight_ctx, combinator_ctx_t *combinator_ctx, mask_ctx_t *mask_ctx) +static int inner1_loop (user_options_t *user_options, user_options_extra_t *user_options_extra, restore_ctx_t *restore_ctx, logfile_ctx_t *logfile_ctx, induct_ctx_t *induct_ctx, dictstat_ctx_t *dictstat_ctx, loopback_ctx_t *loopback_ctx, opencl_ctx_t *opencl_ctx, hwmon_ctx_t *hwmon_ctx, hashconfig_t *hashconfig, hashes_t *hashes, wl_data_t *wl_data, straight_ctx_t *straight_ctx, combinator_ctx_t *combinator_ctx, mask_ctx_t *mask_ctx) { //opencl_ctx->run_main_level1 = true; //opencl_ctx->run_main_level2 = true; @@ -1365,7 +1365,7 @@ static int inner1_loop (user_options_t *user_options, user_options_extra_t *user if (hashes->digests_saved != hashes->digests_done) log_info (""); - status_display (opencl_ctx, hashconfig, hashes, restore_ctx, user_options, user_options_extra, straight_ctx, combinator_ctx, mask_ctx); + status_display (opencl_ctx, hwmon_ctx, hashconfig, hashes, restore_ctx, user_options, user_options_extra, straight_ctx, combinator_ctx, mask_ctx); log_info (""); } @@ -1373,7 +1373,7 @@ static int inner1_loop (user_options_t *user_options, user_options_extra_t *user { if (user_options->status == true) { - status_display (opencl_ctx, hashconfig, hashes, restore_ctx, user_options, user_options_extra, straight_ctx, combinator_ctx, mask_ctx); + status_display (opencl_ctx, hwmon_ctx, hashconfig, hashes, restore_ctx, user_options, user_options_extra, straight_ctx, combinator_ctx, mask_ctx); log_info (""); } @@ -1420,7 +1420,7 @@ static int inner1_loop (user_options_t *user_options, user_options_extra_t *user return 0; } -static int outer_loop (user_options_t *user_options, user_options_extra_t *user_options_extra, restore_ctx_t *restore_ctx, folder_config_t *folder_config, logfile_ctx_t *logfile_ctx, tuning_db_t *tuning_db, induct_ctx_t *induct_ctx, outcheck_ctx_t *outcheck_ctx, outfile_ctx_t *outfile_ctx, potfile_ctx_t *potfile_ctx, dictstat_ctx_t *dictstat_ctx, loopback_ctx_t *loopback_ctx, opencl_ctx_t *opencl_ctx) +static int outer_loop (user_options_t *user_options, user_options_extra_t *user_options_extra, restore_ctx_t *restore_ctx, folder_config_t *folder_config, logfile_ctx_t *logfile_ctx, tuning_db_t *tuning_db, induct_ctx_t *induct_ctx, outcheck_ctx_t *outcheck_ctx, outfile_ctx_t *outfile_ctx, potfile_ctx_t *potfile_ctx, dictstat_ctx_t *dictstat_ctx, loopback_ctx_t *loopback_ctx, opencl_ctx_t *opencl_ctx, hwmon_ctx_t *hwmon_ctx) { opencl_ctx->devices_status = STATUS_INIT; @@ -1657,27 +1657,27 @@ static int outer_loop (user_options_t *user_options, user_options_extra_t *user_ * Watchdog and Temperature balance */ - if (user_options->gpu_temp_disable == false && data.hm_adl == NULL && data.hm_nvml == NULL && data.hm_xnvctrl == NULL) + if (hwmon_ctx->enabled == false && user_options->gpu_temp_disable == false) { log_info ("Watchdog: Hardware Monitoring Interface not found on your system"); } - if (user_options->gpu_temp_abort == 0) - { - log_info ("Watchdog: Temperature abort trigger disabled"); - } - else + if (hwmon_ctx->enabled == true && user_options->gpu_temp_abort > 0) { log_info ("Watchdog: Temperature abort trigger set to %uc", user_options->gpu_temp_abort); } - - if (user_options->gpu_temp_retain == 0) + else { - log_info ("Watchdog: Temperature retain trigger disabled"); + log_info ("Watchdog: Temperature abort trigger disabled"); + } + + if (hwmon_ctx->enabled == true && user_options->gpu_temp_retain > 0) + { + log_info ("Watchdog: Temperature retain trigger set to %uc", user_options->gpu_temp_retain); } else { - log_info ("Watchdog: Temperature retain trigger set to %uc", user_options->gpu_temp_retain); + log_info ("Watchdog: Temperature retain trigger disabled"); } if (user_options->quiet == false) log_info (""); @@ -1822,7 +1822,7 @@ static int outer_loop (user_options_t *user_options, user_options_extra_t *user_ mask_ctx->masks_pos = masks_pos; - const int rc_inner1_loop = inner1_loop (user_options, user_options_extra, restore_ctx, logfile_ctx, induct_ctx, dictstat_ctx, loopback_ctx, opencl_ctx, hashconfig, hashes, wl_data, straight_ctx, combinator_ctx, mask_ctx); + const int rc_inner1_loop = inner1_loop (user_options, user_options_extra, restore_ctx, logfile_ctx, induct_ctx, dictstat_ctx, loopback_ctx, opencl_ctx, hwmon_ctx, hashconfig, hashes, wl_data, straight_ctx, combinator_ctx, mask_ctx); if (rc_inner1_loop == -1) return -1; @@ -1831,7 +1831,7 @@ static int outer_loop (user_options_t *user_options, user_options_extra_t *user_ } else { - const int rc_inner1_loop = inner1_loop (user_options, user_options_extra, restore_ctx, logfile_ctx, induct_ctx, dictstat_ctx, loopback_ctx, opencl_ctx, hashconfig, hashes, wl_data, straight_ctx, combinator_ctx, mask_ctx); + const int rc_inner1_loop = inner1_loop (user_options, user_options_extra, restore_ctx, logfile_ctx, induct_ctx, dictstat_ctx, loopback_ctx, opencl_ctx, hwmon_ctx, hashconfig, hashes, wl_data, straight_ctx, combinator_ctx, mask_ctx); if (rc_inner1_loop == -1) return -1; } @@ -2223,7 +2223,7 @@ int main (int argc, char **argv) data.hwmon_ctx = hwmon_ctx; - const int rc_hwmon_init = hwmon_ctx_init (hwmon_ctx, user_options); + const int rc_hwmon_init = hwmon_ctx_init (hwmon_ctx, user_options, opencl_ctx); if (rc_hwmon_init == -1) { @@ -2232,480 +2232,6 @@ int main (int argc, char **argv) return -1; } - hm_attrs_t hm_adapters_adl[DEVICES_MAX]; - hm_attrs_t hm_adapters_nvapi[DEVICES_MAX]; - hm_attrs_t hm_adapters_nvml[DEVICES_MAX]; - hm_attrs_t hm_adapters_xnvctrl[DEVICES_MAX]; - - memset (hm_adapters_adl, 0, sizeof (hm_adapters_adl)); - memset (hm_adapters_nvapi, 0, sizeof (hm_adapters_nvapi)); - memset (hm_adapters_nvml, 0, sizeof (hm_adapters_nvml)); - memset (hm_adapters_xnvctrl, 0, sizeof (hm_adapters_xnvctrl)); - - if (user_options->gpu_temp_disable == false) - { - ADL_PTR *adl = (ADL_PTR *) mymalloc (sizeof (ADL_PTR)); - NVAPI_PTR *nvapi = (NVAPI_PTR *) mymalloc (sizeof (NVAPI_PTR)); - NVML_PTR *nvml = (NVML_PTR *) mymalloc (sizeof (NVML_PTR)); - XNVCTRL_PTR *xnvctrl = (XNVCTRL_PTR *) mymalloc (sizeof (XNVCTRL_PTR)); - - data.hm_adl = NULL; - data.hm_nvapi = NULL; - data.hm_nvml = NULL; - data.hm_xnvctrl = NULL; - - if ((opencl_ctx->need_nvml == true) && (nvml_init (nvml) == 0)) - { - data.hm_nvml = nvml; - } - - if (data.hm_nvml) - { - if (hm_NVML_nvmlInit (data.hm_nvml) == NVML_SUCCESS) - { - HM_ADAPTER_NVML nvmlGPUHandle[DEVICES_MAX] = { 0 }; - - int tmp_in = hm_get_adapter_index_nvml (nvmlGPUHandle); - - int tmp_out = 0; - - for (int i = 0; i < tmp_in; i++) - { - hm_adapters_nvml[tmp_out++].nvml = nvmlGPUHandle[i]; - } - - for (int i = 0; i < tmp_out; i++) - { - unsigned int speed; - - if (hm_NVML_nvmlDeviceGetFanSpeed (data.hm_nvml, 0, hm_adapters_nvml[i].nvml, &speed) == NVML_SUCCESS) hm_adapters_nvml[i].fan_get_supported = 1; - - // doesn't seem to create any advantages - //hm_NVML_nvmlDeviceSetComputeMode (data.hm_nvml, 1, hm_adapters_nvml[i].nvml, NVML_COMPUTEMODE_EXCLUSIVE_PROCESS); - //hm_NVML_nvmlDeviceSetGpuOperationMode (data.hm_nvml, 1, hm_adapters_nvml[i].nvml, NVML_GOM_ALL_ON); - } - } - } - - if ((opencl_ctx->need_nvapi == true) && (nvapi_init (nvapi) == 0)) - { - data.hm_nvapi = nvapi; - } - - if (data.hm_nvapi) - { - if (hm_NvAPI_Initialize (data.hm_nvapi) == NVAPI_OK) - { - HM_ADAPTER_NVAPI nvGPUHandle[DEVICES_MAX] = { 0 }; - - int tmp_in = hm_get_adapter_index_nvapi (nvGPUHandle); - - int tmp_out = 0; - - for (int i = 0; i < tmp_in; i++) - { - hm_adapters_nvapi[tmp_out++].nvapi = nvGPUHandle[i]; - } - } - } - - if ((opencl_ctx->need_xnvctrl == true) && (xnvctrl_init (xnvctrl) == 0)) - { - data.hm_xnvctrl = xnvctrl; - } - - if (data.hm_xnvctrl) - { - if (hm_XNVCTRL_XOpenDisplay (data.hm_xnvctrl) == 0) - { - for (uint 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_xnvctrl[device_id].xnvctrl = device_id; - - int speed = 0; - - if (get_fan_speed_current (data.hm_xnvctrl, device_id, &speed) == 0) hm_adapters_xnvctrl[device_id].fan_get_supported = 1; - } - } - } - - if ((opencl_ctx->need_adl == true) && (adl_init (adl) == 0)) - { - data.hm_adl = adl; - } - - if (data.hm_adl) - { - if (hm_ADL_Main_Control_Create (data.hm_adl, ADL_Main_Memory_Alloc, 0) == ADL_OK) - { - // total number of adapters - - int hm_adapters_num; - - if (get_adapters_num_adl (data.hm_adl, &hm_adapters_num) != 0) return -1; - - // adapter info - - LPAdapterInfo lpAdapterInfo = hm_get_adapter_info_adl (data.hm_adl, hm_adapters_num); - - if (lpAdapterInfo == NULL) return -1; - - // get a list (of ids of) valid/usable adapters - - int num_adl_adapters = 0; - - u32 *valid_adl_device_list = hm_get_list_valid_adl_adapters (hm_adapters_num, &num_adl_adapters, lpAdapterInfo); - - if (num_adl_adapters > 0) - { - hc_thread_mutex_lock (mux_hwmon); - - // hm_get_opencl_busid_devid (hm_adapters_adl, devices_all_cnt, devices_all); - - hm_get_adapter_index_adl (hm_adapters_adl, valid_adl_device_list, num_adl_adapters, lpAdapterInfo); - - hm_get_overdrive_version (data.hm_adl, hm_adapters_adl, valid_adl_device_list, num_adl_adapters, lpAdapterInfo); - hm_check_fanspeed_control (data.hm_adl, hm_adapters_adl, valid_adl_device_list, num_adl_adapters, lpAdapterInfo); - - hc_thread_mutex_unlock (mux_hwmon); - } - - myfree (valid_adl_device_list); - myfree (lpAdapterInfo); - } - } - - if (data.hm_adl == NULL && data.hm_nvml == NULL && data.hm_xnvctrl == NULL) - { - user_options->gpu_temp_disable = true; - } - } - - /** - * User-defined GPU temp handling - */ - - if (user_options->gpu_temp_disable == true) - { - user_options->gpu_temp_abort = 0; - user_options->gpu_temp_retain = 0; - } - - /** - * OpenCL devices: allocate buffer for device specific information - */ - - ADLOD6MemClockState *od_clock_mem_status = (ADLOD6MemClockState *) mycalloc (opencl_ctx->devices_cnt, sizeof (ADLOD6MemClockState)); - - int *od_power_control_status = (int *) mycalloc (opencl_ctx->devices_cnt, sizeof (int)); - - unsigned int *nvml_power_limit = (unsigned int *) mycalloc (opencl_ctx->devices_cnt, sizeof (unsigned int)); - - - /** - * HM devices: copy - */ - - if (user_options->gpu_temp_disable == false) - { - for (uint 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; - - if (device_param->skipped) continue; - - const uint platform_devices_id = device_param->platform_devices_id; - - if (device_param->device_vendor_id == VENDOR_ID_AMD) - { - data.hm_device[device_id].adl = hm_adapters_adl[platform_devices_id].adl; - data.hm_device[device_id].nvapi = 0; - data.hm_device[device_id].nvml = 0; - data.hm_device[device_id].xnvctrl = 0; - data.hm_device[device_id].od_version = hm_adapters_adl[platform_devices_id].od_version; - data.hm_device[device_id].fan_get_supported = hm_adapters_adl[platform_devices_id].fan_get_supported; - data.hm_device[device_id].fan_set_supported = 0; - } - - if (device_param->device_vendor_id == VENDOR_ID_NV) - { - data.hm_device[device_id].adl = 0; - data.hm_device[device_id].nvapi = hm_adapters_nvapi[platform_devices_id].nvapi; - data.hm_device[device_id].nvml = hm_adapters_nvml[platform_devices_id].nvml; - data.hm_device[device_id].xnvctrl = hm_adapters_xnvctrl[platform_devices_id].xnvctrl; - data.hm_device[device_id].od_version = 0; - data.hm_device[device_id].fan_get_supported = hm_adapters_nvml[platform_devices_id].fan_get_supported; - data.hm_device[device_id].fan_set_supported = 0; - } - } - } - - /** - * powertune on user request - */ - - if (user_options->powertune_enable == true) - { - hc_thread_mutex_lock (mux_hwmon); - - for (uint 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->skipped) continue; - - if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) - { - /** - * Temporary fix: - * with AMD r9 295x cards it seems that we need to set the powertune value just AFTER the ocl init stuff - * otherwise after hc_clCreateContext () etc, powertune value was set back to "normal" and cards unfortunately - * were not working @ full speed (setting hm_ADL_Overdrive_PowerControl_Set () here seems to fix the problem) - * Driver / ADL bug? - */ - - if (data.hm_device[device_id].od_version == 6) - { - int ADL_rc; - - // check powertune capabilities first, if not available then skip device - - int powertune_supported = 0; - - if ((ADL_rc = hm_ADL_Overdrive6_PowerControl_Caps (data.hm_adl, data.hm_device[device_id].adl, &powertune_supported)) != ADL_OK) - { - log_error ("ERROR: Failed to get ADL PowerControl Capabilities"); - - return -1; - } - - // first backup current value, we will restore it later - - if (powertune_supported != 0) - { - // powercontrol settings - - ADLOD6PowerControlInfo powertune = {0, 0, 0, 0, 0}; - - if ((ADL_rc = hm_ADL_Overdrive_PowerControlInfo_Get (data.hm_adl, data.hm_device[device_id].adl, &powertune)) == ADL_OK) - { - ADL_rc = hm_ADL_Overdrive_PowerControl_Get (data.hm_adl, data.hm_device[device_id].adl, &od_power_control_status[device_id]); - } - - if (ADL_rc != ADL_OK) - { - log_error ("ERROR: Failed to get current ADL PowerControl settings"); - - return -1; - } - - if ((ADL_rc = hm_ADL_Overdrive_PowerControl_Set (data.hm_adl, data.hm_device[device_id].adl, powertune.iMaxValue)) != ADL_OK) - { - log_error ("ERROR: Failed to set new ADL PowerControl values"); - - return -1; - } - - // clocks - - memset (&od_clock_mem_status[device_id], 0, sizeof (ADLOD6MemClockState)); - - od_clock_mem_status[device_id].state.iNumberOfPerformanceLevels = 2; - - if ((ADL_rc = hm_ADL_Overdrive_StateInfo_Get (data.hm_adl, data.hm_device[device_id].adl, ADL_OD6_GETSTATEINFO_CUSTOM_PERFORMANCE, &od_clock_mem_status[device_id])) != ADL_OK) - { - log_error ("ERROR: Failed to get ADL memory and engine clock frequency"); - - return -1; - } - - // Query capabilities only to see if profiles were not "damaged", if so output a warning but do accept the users profile settings - - ADLOD6Capabilities caps = {0, 0, 0, {0, 0, 0}, {0, 0, 0}, 0, 0}; - - if ((ADL_rc = hm_ADL_Overdrive_Capabilities_Get (data.hm_adl, data.hm_device[device_id].adl, &caps)) != ADL_OK) - { - log_error ("ERROR: Failed to get ADL device capabilities"); - - return -1; - } - - int engine_clock_max = (int) (0.6666 * caps.sEngineClockRange.iMax); - int memory_clock_max = (int) (0.6250 * caps.sMemoryClockRange.iMax); - - int warning_trigger_engine = (int) (0.25 * engine_clock_max); - int warning_trigger_memory = (int) (0.25 * memory_clock_max); - - int engine_clock_profile_max = od_clock_mem_status[device_id].state.aLevels[1].iEngineClock; - int memory_clock_profile_max = od_clock_mem_status[device_id].state.aLevels[1].iMemoryClock; - - // warning if profile has too low max values - - if ((engine_clock_max - engine_clock_profile_max) > warning_trigger_engine) - { - log_info ("WARN: The custom profile seems to have too low maximum engine clock values. You therefore may not reach full performance"); - } - - if ((memory_clock_max - memory_clock_profile_max) > warning_trigger_memory) - { - log_info ("WARN: The custom profile seems to have too low maximum memory clock values. You therefore may not reach full performance"); - } - - ADLOD6StateInfo *performance_state = (ADLOD6StateInfo*) mycalloc (1, sizeof (ADLOD6StateInfo) + sizeof (ADLOD6PerformanceLevel)); - - performance_state->iNumberOfPerformanceLevels = 2; - - performance_state->aLevels[0].iEngineClock = engine_clock_profile_max; - performance_state->aLevels[1].iEngineClock = engine_clock_profile_max; - performance_state->aLevels[0].iMemoryClock = memory_clock_profile_max; - performance_state->aLevels[1].iMemoryClock = memory_clock_profile_max; - - if ((ADL_rc = hm_ADL_Overdrive_State_Set (data.hm_adl, data.hm_device[device_id].adl, ADL_OD6_SETSTATE_PERFORMANCE, performance_state)) != ADL_OK) - { - log_info ("ERROR: Failed to set ADL performance state"); - - return -1; - } - - myfree (performance_state); - } - - // set powertune value only - - if (powertune_supported != 0) - { - // powertune set - ADLOD6PowerControlInfo powertune = {0, 0, 0, 0, 0}; - - if ((ADL_rc = hm_ADL_Overdrive_PowerControlInfo_Get (data.hm_adl, data.hm_device[device_id].adl, &powertune)) != ADL_OK) - { - log_error ("ERROR: Failed to get current ADL PowerControl settings"); - - return -1; - } - - if ((ADL_rc = hm_ADL_Overdrive_PowerControl_Set (data.hm_adl, data.hm_device[device_id].adl, powertune.iMaxValue)) != ADL_OK) - { - log_error ("ERROR: Failed to set new ADL PowerControl values"); - - return -1; - } - } - } - } - - if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_NV) - { - // first backup current value, we will restore it later - - unsigned int limit; - - bool powertune_supported = false; - - if (hm_NVML_nvmlDeviceGetPowerManagementLimit (data.hm_nvml, 0, data.hm_device[device_id].nvml, &limit) == NVML_SUCCESS) - { - powertune_supported = true; - } - - // if backup worked, activate the maximum allowed - - if (powertune_supported == true) - { - unsigned int minLimit; - unsigned int maxLimit; - - if (hm_NVML_nvmlDeviceGetPowerManagementLimitConstraints (data.hm_nvml, 0, data.hm_device[device_id].nvml, &minLimit, &maxLimit) == NVML_SUCCESS) - { - if (maxLimit > 0) - { - if (hm_NVML_nvmlDeviceSetPowerManagementLimit (data.hm_nvml, 0, data.hm_device[device_id].nvml, maxLimit) == NVML_SUCCESS) - { - // now we can be sure we need to reset later - - nvml_power_limit[device_id] = limit; - } - } - } - } - } - } - - hc_thread_mutex_unlock (mux_hwmon); - } - - /** - * Store initial fanspeed if gpu_temp_retain is enabled - */ - - if (user_options->gpu_temp_disable == false) - { - if (user_options->gpu_temp_retain) - { - for (uint 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->skipped) continue; - - hc_thread_mutex_lock (mux_hwmon); - - if (data.hm_device[device_id].fan_get_supported == true) - { - const int fanspeed = hm_get_fanspeed_with_device_id (opencl_ctx, device_id); - const int fanpolicy = hm_get_fanpolicy_with_device_id (opencl_ctx, device_id); - - // we also set it to tell the OS we take control over the fan and it's automatic controller - // if it was set to automatic. we do not control user-defined fanspeeds. - - if (fanpolicy == 1) - { - data.hm_device[device_id].fan_set_supported = true; - - int rc = -1; - - if (device_param->device_vendor_id == VENDOR_ID_AMD) - { - rc = hm_set_fanspeed_with_device_id_adl (device_id, fanspeed, 1); - } - else if (device_param->device_vendor_id == VENDOR_ID_NV) - { - #if defined (__linux__) - rc = set_fan_control (data.hm_xnvctrl, data.hm_device[device_id].xnvctrl, NV_CTRL_GPU_COOLER_MANUAL_CONTROL_TRUE); - #endif - - #if defined (_WIN) - rc = hm_set_fanspeed_with_device_id_nvapi (device_id, fanspeed, 1); - #endif - } - - if (rc == 0) - { - data.hm_device[device_id].fan_set_supported = 1; - } - else - { - log_info ("WARNING: Failed to set initial fan speed for device #%u", device_id + 1); - - data.hm_device[device_id].fan_set_supported = 0; - } - } - else - { - data.hm_device[device_id].fan_set_supported = 0; - } - } - - hc_thread_mutex_unlock (mux_hwmon); - } - } - } - /** * keypress thread */ @@ -2736,7 +2262,7 @@ int main (int argc, char **argv) if (user_options->hash_mode_chgd == true) { - const int rc = outer_loop (user_options, user_options_extra, restore_ctx, folder_config, logfile_ctx, tuning_db, induct_ctx, outcheck_ctx, outfile_ctx, potfile_ctx, dictstat_ctx, loopback_ctx, opencl_ctx); + const int rc = outer_loop (user_options, user_options_extra, restore_ctx, folder_config, logfile_ctx, tuning_db, induct_ctx, outcheck_ctx, outfile_ctx, potfile_ctx, dictstat_ctx, loopback_ctx, opencl_ctx, hwmon_ctx); if (rc == -1) return -1; } @@ -2746,7 +2272,7 @@ int main (int argc, char **argv) { user_options->hash_mode = DEFAULT_BENCHMARK_ALGORITHMS_BUF[algorithm_pos]; - const int rc = outer_loop (user_options, user_options_extra, restore_ctx, folder_config, logfile_ctx, tuning_db, induct_ctx, outcheck_ctx, outfile_ctx, potfile_ctx, dictstat_ctx, loopback_ctx, opencl_ctx); + const int rc = outer_loop (user_options, user_options_extra, restore_ctx, folder_config, logfile_ctx, tuning_db, induct_ctx, outcheck_ctx, outfile_ctx, potfile_ctx, dictstat_ctx, loopback_ctx, opencl_ctx, hwmon_ctx); if (rc == -1) return -1; @@ -2756,7 +2282,7 @@ int main (int argc, char **argv) } else { - const int rc = outer_loop (user_options, user_options_extra, restore_ctx, folder_config, logfile_ctx, tuning_db, induct_ctx, outcheck_ctx, outfile_ctx, potfile_ctx, dictstat_ctx, loopback_ctx, opencl_ctx); + const int rc = outer_loop (user_options, user_options_extra, restore_ctx, folder_config, logfile_ctx, tuning_db, induct_ctx, outcheck_ctx, outfile_ctx, potfile_ctx, dictstat_ctx, loopback_ctx, opencl_ctx, hwmon_ctx); if (rc == -1) return -1; } @@ -2777,161 +2303,6 @@ int main (int argc, char **argv) user_options->quiet = false; } - // reset default fan speed - - if (user_options->gpu_temp_disable == false) - { - if (user_options->gpu_temp_retain) - { - hc_thread_mutex_lock (mux_hwmon); - - for (uint 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->skipped) continue; - - if (data.hm_device[device_id].fan_set_supported == true) - { - int rc = -1; - - if (device_param->device_vendor_id == VENDOR_ID_AMD) - { - rc = hm_set_fanspeed_with_device_id_adl (device_id, 100, 0); - } - else if (device_param->device_vendor_id == VENDOR_ID_NV) - { - #if defined (__linux__) - rc = set_fan_control (data.hm_xnvctrl, data.hm_device[device_id].xnvctrl, NV_CTRL_GPU_COOLER_MANUAL_CONTROL_FALSE); - #endif - - #if defined (_WIN) - rc = hm_set_fanspeed_with_device_id_nvapi (device_id, 100, 0); - #endif - } - - if (rc == -1) log_info ("WARNING: Failed to restore default fan speed and policy for device #%", device_id + 1); - } - } - - hc_thread_mutex_unlock (mux_hwmon); - } - } - - // reset power tuning - - if (user_options->powertune_enable == true) - { - hc_thread_mutex_lock (mux_hwmon); - - for (uint 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->skipped) continue; - - if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) - { - if (data.hm_device[device_id].od_version == 6) - { - // check powertune capabilities first, if not available then skip device - - int powertune_supported = 0; - - if ((hm_ADL_Overdrive6_PowerControl_Caps (data.hm_adl, data.hm_device[device_id].adl, &powertune_supported)) != ADL_OK) - { - log_error ("ERROR: Failed to get ADL PowerControl Capabilities"); - - return -1; - } - - if (powertune_supported != 0) - { - // powercontrol settings - - if ((hm_ADL_Overdrive_PowerControl_Set (data.hm_adl, data.hm_device[device_id].adl, od_power_control_status[device_id])) != ADL_OK) - { - log_info ("ERROR: Failed to restore the ADL PowerControl values"); - - return -1; - } - - // clocks - - ADLOD6StateInfo *performance_state = (ADLOD6StateInfo*) mycalloc (1, sizeof (ADLOD6StateInfo) + sizeof (ADLOD6PerformanceLevel)); - - performance_state->iNumberOfPerformanceLevels = 2; - - performance_state->aLevels[0].iEngineClock = od_clock_mem_status[device_id].state.aLevels[0].iEngineClock; - performance_state->aLevels[1].iEngineClock = od_clock_mem_status[device_id].state.aLevels[1].iEngineClock; - performance_state->aLevels[0].iMemoryClock = od_clock_mem_status[device_id].state.aLevels[0].iMemoryClock; - performance_state->aLevels[1].iMemoryClock = od_clock_mem_status[device_id].state.aLevels[1].iMemoryClock; - - if ((hm_ADL_Overdrive_State_Set (data.hm_adl, data.hm_device[device_id].adl, ADL_OD6_SETSTATE_PERFORMANCE, performance_state)) != ADL_OK) - { - log_info ("ERROR: Failed to restore ADL performance state"); - - return -1; - } - - myfree (performance_state); - } - } - } - - if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_NV) - { - unsigned int power_limit = nvml_power_limit[device_id]; - - if (power_limit > 0) - { - hm_NVML_nvmlDeviceSetPowerManagementLimit (data.hm_nvml, 0, data.hm_device[device_id].nvml, power_limit); - } - } - } - - hc_thread_mutex_unlock (mux_hwmon); - } - - if (user_options->gpu_temp_disable == false) - { - if (data.hm_nvml) - { - hm_NVML_nvmlShutdown (data.hm_nvml); - - nvml_close (data.hm_nvml); - - data.hm_nvml = NULL; - } - - if (data.hm_nvapi) - { - hm_NvAPI_Unload (data.hm_nvapi); - - nvapi_close (data.hm_nvapi); - - data.hm_nvapi = NULL; - } - - if (data.hm_xnvctrl) - { - hm_XNVCTRL_XCloseDisplay (data.hm_xnvctrl); - - xnvctrl_close (data.hm_xnvctrl); - - data.hm_xnvctrl = NULL; - } - - if (data.hm_adl) - { - hm_ADL_Main_Control_Destroy (data.hm_adl); - - adl_close (data.hm_adl); - - data.hm_adl = NULL; - } - } - // destroy others mutex hc_thread_mutex_delete (mux_display); @@ -2939,10 +2310,6 @@ int main (int argc, char **argv) // free memory - myfree (od_clock_mem_status); - myfree (od_power_control_status); - myfree (nvml_power_limit); - debugfile_destroy (debugfile_ctx); tuning_db_destroy (tuning_db); @@ -2963,9 +2330,9 @@ int main (int argc, char **argv) user_options_extra_destroy (user_options_extra); - opencl_ctx_devices_destroy (opencl_ctx); + hwmon_ctx_destroy (hwmon_ctx, user_options, opencl_ctx); - hwmon_ctx_destroy (hwmon_ctx); + opencl_ctx_devices_destroy (opencl_ctx); restore_ctx_destroy (restore_ctx); diff --git a/src/hwmon.c b/src/hwmon.c index 62fe23746..98a2b4793 100644 --- a/src/hwmon.c +++ b/src/hwmon.c @@ -5,34 +5,13 @@ #include "common.h" #include "types.h" -#include "interface.h" -#include "timer.h" #include "memory.h" #include "logging.h" -#include "ext_OpenCL.h" -#include "ext_ADL.h" -#include "ext_nvapi.h" -#include "ext_nvml.h" -#include "ext_xnvctrl.h" -#include "mpsp.h" -#include "rp_cpu.h" -#include "tuningdb.h" -#include "thread.h" -#include "opencl.h" -#include "hwmon.h" -#include "restore.h" -#include "hash_management.h" -#include "outfile.h" -#include "potfile.h" -#include "debugfile.h" -#include "loopback.h" #include "data.h" hc_thread_mutex_t mux_hwmon; -extern hc_global_data_t data; - -int get_adapters_num_adl (void *adl, int *iNumberAdapters) +static int get_adapters_num_adl (void *adl, int *iNumberAdapters) { if (hm_ADL_Adapter_NumberOfAdapters_Get ((ADL_PTR *) adl, iNumberAdapters) != ADL_OK) return -1; @@ -46,43 +25,7 @@ int get_adapters_num_adl (void *adl, int *iNumberAdapters) return 0; } -/* -int hm_show_performance_level (HM_LIB hm_dll, int iAdapterIndex) -{ - ADLODPerformanceLevels *lpOdPerformanceLevels = NULL; - ADLODParameters lpOdParameters; - - lpOdParameters.iSize = sizeof (ADLODParameters); - size_t plevels_size = 0; - - if (hm_ADL_Overdrive_ODParameters_Get (hm_dll, iAdapterIndex, &lpOdParameters) != ADL_OK) return -1; - - log_info ("[DEBUG] %s, adapter %d performance level (%d) : %s %s", - __func__, iAdapterIndex, - lpOdParameters.iNumberOfPerformanceLevels, - (lpOdParameters.iActivityReportingSupported) ? "activity reporting" : "", - (lpOdParameters.iDiscretePerformanceLevels) ? "discrete performance levels" : "performance ranges"); - - plevels_size = sizeof (ADLODPerformanceLevels) + sizeof (ADLODPerformanceLevel) * (lpOdParameters.iNumberOfPerformanceLevels - 1); - - lpOdPerformanceLevels = (ADLODPerformanceLevels *) mymalloc (plevels_size); - - lpOdPerformanceLevels->iSize = sizeof (ADLODPerformanceLevels) + sizeof (ADLODPerformanceLevel) * (lpOdParameters.iNumberOfPerformanceLevels - 1); - - if (hm_ADL_Overdrive_ODPerformanceLevels_Get (hm_dll, iAdapterIndex, 0, lpOdPerformanceLevels) != ADL_OK) return -1; - - for (int j = 0; j < lpOdParameters.iNumberOfPerformanceLevels; j++) - log_info ("[DEBUG] %s, adapter %d, level %d : engine %d, memory %d, voltage: %d", - __func__, iAdapterIndex, j, - lpOdPerformanceLevels->aLevels[j].iEngineClock / 100, lpOdPerformanceLevels->aLevels[j].iMemoryClock / 100, lpOdPerformanceLevels->aLevels[j].iVddc); - - myfree (lpOdPerformanceLevels); - - return 0; -} -*/ - -LPAdapterInfo hm_get_adapter_info_adl (void *adl, int iNumberAdapters) +static LPAdapterInfo hm_get_adapter_info_adl (void *adl, int iNumberAdapters) { size_t AdapterInfoSize = iNumberAdapters * sizeof (AdapterInfo); @@ -93,11 +36,11 @@ LPAdapterInfo hm_get_adapter_info_adl (void *adl, int iNumberAdapters) return lpAdapterInfo; } -int hm_get_adapter_index_nvapi (HM_ADAPTER_NVAPI nvapiGPUHandle[DEVICES_MAX]) +static int hm_get_adapter_index_nvapi (const hwmon_ctx_t *hwmon_ctx, HM_ADAPTER_NVAPI *nvapiGPUHandle) { NvU32 pGpuCount; - if (hm_NvAPI_EnumPhysicalGPUs (data.hm_nvapi, nvapiGPUHandle, &pGpuCount) != NVAPI_OK) return 0; + if (hm_NvAPI_EnumPhysicalGPUs (hwmon_ctx->hm_nvapi, nvapiGPUHandle, &pGpuCount) != NVAPI_OK) return 0; if (pGpuCount == 0) { @@ -109,17 +52,17 @@ int hm_get_adapter_index_nvapi (HM_ADAPTER_NVAPI nvapiGPUHandle[DEVICES_MAX]) return (pGpuCount); } -int hm_get_adapter_index_nvml (HM_ADAPTER_NVML nvmlGPUHandle[DEVICES_MAX]) +static int hm_get_adapter_index_nvml (const hwmon_ctx_t *hwmon_ctx, HM_ADAPTER_NVML *nvmlGPUHandle) { int pGpuCount = 0; for (uint i = 0; i < DEVICES_MAX; i++) { - if (hm_NVML_nvmlDeviceGetHandleByIndex (data.hm_nvml, 1, i, &nvmlGPUHandle[i]) != NVML_SUCCESS) break; + if (hm_NVML_nvmlDeviceGetHandleByIndex (hwmon_ctx->hm_nvml, 1, i, &nvmlGPUHandle[i]) != NVML_SUCCESS) break; // can be used to determine if the device by index matches the cuda device by index // char name[100]; memset (name, 0, sizeof (name)); - // hm_NVML_nvmlDeviceGetName (data.hm_nvml, nvGPUHandle[i], name, sizeof (name) - 1); + // hm_NVML_nvmlDeviceGetName (hwmon_ctx->hm_nvml, nvGPUHandle[i], name, sizeof (name) - 1); pGpuCount++; } @@ -134,47 +77,6 @@ int hm_get_adapter_index_nvml (HM_ADAPTER_NVML nvmlGPUHandle[DEVICES_MAX]) return (pGpuCount); } -/* -// -// does not help at all, since ADL does not assign different bus id, device id when we have multi GPU setups -// - -int hm_get_opencl_device_index (hm_attrs_t *hm_device, uint num_adl_adapters, int bus_num, int dev_num) -{ - u32 idx = -1; - - for (uint i = 0; i < num_adl_adapters; i++) - { - int opencl_bus_num = hm_device[i].busid; - int opencl_dev_num = hm_device[i].devid; - - if ((opencl_bus_num == bus_num) && (opencl_dev_num == dev_num)) - { - idx = i; - - break; - } - } - - if (idx >= DEVICES_MAX) return -1; - - return idx; -} - -void hm_get_opencl_busid_devid (hm_attrs_t *hm_device, uint opencl_num_devices, cl_device_id *devices) -{ - for (uint i = 0; i < opencl_num_devices; i++) - { - cl_device_topology_amd device_topology; - - hc_clGetDeviceInfo (devices[i], CL_DEVICE_TOPOLOGY_AMD, sizeof (device_topology), &device_topology, NULL); - - hm_device[i].busid = device_topology.pcie.bus; - hm_device[i].devid = device_topology.pcie.device; - } -} -*/ - static void hm_sort_adl_adapters_by_busid_devid (u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo) { // basically bubble sort @@ -224,7 +126,7 @@ static void hm_sort_adl_adapters_by_busid_devid (u32 *valid_adl_device_list, int } } -u32 *hm_get_list_valid_adl_adapters (int iNumberAdapters, int *num_adl_adapters, LPAdapterInfo lpAdapterInfo) +static u32 *hm_get_list_valid_adl_adapters (int iNumberAdapters, int *num_adl_adapters, LPAdapterInfo lpAdapterInfo) { *num_adl_adapters = 0; @@ -288,7 +190,7 @@ u32 *hm_get_list_valid_adl_adapters (int iNumberAdapters, int *num_adl_adapters, return adl_adapters; } -int hm_check_fanspeed_control (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo) +static int hm_check_fanspeed_control (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo) { // loop through all valid devices @@ -325,11 +227,11 @@ int hm_check_fanspeed_control (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_ if ((FanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_READ) && (FanSpeedInfo.iFlags & ADL_DL_FANCTRL_SUPPORTS_PERCENT_WRITE)) { - hm_device[opencl_device_index].fan_get_supported = 1; + hm_device[opencl_device_index].fan_get_supported = true; } else { - hm_device[opencl_device_index].fan_get_supported = 0; + hm_device[opencl_device_index].fan_get_supported = false; } } else // od_version == 6 @@ -344,11 +246,11 @@ int hm_check_fanspeed_control (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_ if (faninfo.iSpeedType & ADL_OD6_FANSPEED_TYPE_PERCENT) { - hm_device[opencl_device_index].fan_get_supported = 1; + hm_device[opencl_device_index].fan_get_supported = true; } else { - hm_device[opencl_device_index].fan_get_supported = 0; + hm_device[opencl_device_index].fan_get_supported = false; } } } @@ -356,7 +258,7 @@ int hm_check_fanspeed_control (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_ return 0; } -int hm_get_overdrive_version (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo) +static int hm_get_overdrive_version (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo) { for (int i = 0; i < num_adl_adapters; i++) { @@ -388,7 +290,7 @@ int hm_get_overdrive_version (void *adl, hm_attrs_t *hm_device, u32 *valid_adl_d return 0; } -int hm_get_adapter_index_adl (hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo) +static int hm_get_adapter_index_adl (hm_attrs_t *hm_device, u32 *valid_adl_device_list, int num_adl_adapters, LPAdapterInfo lpAdapterInfo) { for (int i = 0; i < num_adl_adapters; i++) { @@ -412,24 +314,24 @@ int hm_get_adapter_index_adl (hm_attrs_t *hm_device, u32 *valid_adl_device_list, return num_adl_adapters; } -int hm_get_threshold_slowdown_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id) +int hm_get_threshold_slowdown_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id) { if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) { - if (data.hm_adl) + if (hwmon_ctx->hm_adl) { - if (data.hm_device[device_id].od_version == 5) + if (hwmon_ctx->hm_device[device_id].od_version == 5) { } - else if (data.hm_device[device_id].od_version == 6) + else if (hwmon_ctx->hm_device[device_id].od_version == 6) { int CurrentValue = 0; int DefaultValue = 0; - if (hm_ADL_Overdrive6_TargetTemperatureData_Get (data.hm_adl, data.hm_device[device_id].adl, &CurrentValue, &DefaultValue) != ADL_OK) return -1; + if (hm_ADL_Overdrive6_TargetTemperatureData_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &CurrentValue, &DefaultValue) != ADL_OK) return -1; // the return value has never been tested since hm_ADL_Overdrive6_TargetTemperatureData_Get() never worked on any system. expect problems. @@ -442,7 +344,7 @@ int hm_get_threshold_slowdown_with_device_id (opencl_ctx_t *opencl_ctx, const ui { int target = 0; - if (hm_NVML_nvmlDeviceGetTemperatureThreshold (data.hm_nvml, 1, data.hm_device[device_id].nvml, NVML_TEMPERATURE_THRESHOLD_SLOWDOWN, (unsigned int *) &target) != NVML_SUCCESS) return -1; + if (hm_NVML_nvmlDeviceGetTemperatureThreshold (hwmon_ctx->hm_nvml, 1, hwmon_ctx->hm_device[device_id].nvml, NVML_TEMPERATURE_THRESHOLD_SLOWDOWN, (unsigned int *) &target) != NVML_SUCCESS) return -1; return target; } @@ -450,19 +352,19 @@ int hm_get_threshold_slowdown_with_device_id (opencl_ctx_t *opencl_ctx, const ui return -1; } -int hm_get_threshold_shutdown_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id) +int hm_get_threshold_shutdown_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id) { if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) { - if (data.hm_adl) + if (hwmon_ctx->hm_adl) { - if (data.hm_device[device_id].od_version == 5) + if (hwmon_ctx->hm_device[device_id].od_version == 5) { } - else if (data.hm_device[device_id].od_version == 6) + else if (hwmon_ctx->hm_device[device_id].od_version == 6) { } @@ -473,7 +375,7 @@ int hm_get_threshold_shutdown_with_device_id (opencl_ctx_t *opencl_ctx, const ui { int target = 0; - if (hm_NVML_nvmlDeviceGetTemperatureThreshold (data.hm_nvml, 1, data.hm_device[device_id].nvml, NVML_TEMPERATURE_THRESHOLD_SHUTDOWN, (unsigned int *) &target) != NVML_SUCCESS) return -1; + if (hm_NVML_nvmlDeviceGetTemperatureThreshold (hwmon_ctx->hm_nvml, 1, hwmon_ctx->hm_device[device_id].nvml, NVML_TEMPERATURE_THRESHOLD_SHUTDOWN, (unsigned int *) &target) != NVML_SUCCESS) return -1; return target; } @@ -481,29 +383,29 @@ int hm_get_threshold_shutdown_with_device_id (opencl_ctx_t *opencl_ctx, const ui return -1; } -int hm_get_temperature_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id) +int hm_get_temperature_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id) { if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) { - if (data.hm_adl) + if (hwmon_ctx->hm_adl) { - if (data.hm_device[device_id].od_version == 5) + if (hwmon_ctx->hm_device[device_id].od_version == 5) { ADLTemperature Temperature; Temperature.iSize = sizeof (ADLTemperature); - if (hm_ADL_Overdrive5_Temperature_Get (data.hm_adl, data.hm_device[device_id].adl, 0, &Temperature) != ADL_OK) return -1; + if (hm_ADL_Overdrive5_Temperature_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, 0, &Temperature) != ADL_OK) return -1; return Temperature.iTemperature / 1000; } - else if (data.hm_device[device_id].od_version == 6) + else if (hwmon_ctx->hm_device[device_id].od_version == 6) { int Temperature = 0; - if (hm_ADL_Overdrive6_Temperature_Get (data.hm_adl, data.hm_device[device_id].adl, &Temperature) != ADL_OK) return -1; + if (hm_ADL_Overdrive6_Temperature_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &Temperature) != ADL_OK) return -1; return Temperature / 1000; } @@ -514,7 +416,7 @@ int hm_get_temperature_with_device_id (opencl_ctx_t *opencl_ctx, const uint devi { int temperature = 0; - if (hm_NVML_nvmlDeviceGetTemperature (data.hm_nvml, 1, data.hm_device[device_id].nvml, NVML_TEMPERATURE_GPU, (uint *) &temperature) != NVML_SUCCESS) return -1; + if (hm_NVML_nvmlDeviceGetTemperature (hwmon_ctx->hm_nvml, 1, hwmon_ctx->hm_device[device_id].nvml, NVML_TEMPERATURE_GPU, (uint *) &temperature) != NVML_SUCCESS) return -1; return temperature; } @@ -522,17 +424,17 @@ int hm_get_temperature_with_device_id (opencl_ctx_t *opencl_ctx, const uint devi return -1; } -int hm_get_fanpolicy_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id) +int hm_get_fanpolicy_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id) { if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; - if (data.hm_device[device_id].fan_get_supported == 1) + if (hwmon_ctx->hm_device[device_id].fan_get_supported == true) { if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) { - if (data.hm_adl) + if (hwmon_ctx->hm_adl) { - if (data.hm_device[device_id].od_version == 5) + if (hwmon_ctx->hm_device[device_id].od_version == 5) { ADLFanSpeedValue lpFanSpeedValue; @@ -541,7 +443,7 @@ int hm_get_fanpolicy_with_device_id (opencl_ctx_t *opencl_ctx, const uint device lpFanSpeedValue.iSize = sizeof (lpFanSpeedValue); lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT; - if (hm_ADL_Overdrive5_FanSpeed_Get (data.hm_adl, data.hm_device[device_id].adl, 0, &lpFanSpeedValue) != ADL_OK) return -1; + if (hm_ADL_Overdrive5_FanSpeed_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, 0, &lpFanSpeedValue) != ADL_OK) return -1; return (lpFanSpeedValue.iFanSpeed & ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED) ? 0 : 1; } @@ -561,17 +463,17 @@ int hm_get_fanpolicy_with_device_id (opencl_ctx_t *opencl_ctx, const uint device return -1; } -int hm_get_fanspeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id) +int hm_get_fanspeed_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id) { if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; - if (data.hm_device[device_id].fan_get_supported == 1) + if (hwmon_ctx->hm_device[device_id].fan_get_supported == true) { if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) { - if (data.hm_adl) + if (hwmon_ctx->hm_adl) { - if (data.hm_device[device_id].od_version == 5) + if (hwmon_ctx->hm_device[device_id].od_version == 5) { ADLFanSpeedValue lpFanSpeedValue; @@ -581,7 +483,7 @@ int hm_get_fanspeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_ lpFanSpeedValue.iSpeedType = ADL_DL_FANCTRL_SPEED_TYPE_PERCENT; lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED; - if (hm_ADL_Overdrive5_FanSpeed_Get (data.hm_adl, data.hm_device[device_id].adl, 0, &lpFanSpeedValue) != ADL_OK) return -1; + if (hm_ADL_Overdrive5_FanSpeed_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, 0, &lpFanSpeedValue) != ADL_OK) return -1; return lpFanSpeedValue.iFanSpeed; } @@ -591,7 +493,7 @@ int hm_get_fanspeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_ memset (&faninfo, 0, sizeof (faninfo)); - if (hm_ADL_Overdrive6_FanSpeed_Get (data.hm_adl, data.hm_device[device_id].adl, &faninfo) != ADL_OK) return -1; + if (hm_ADL_Overdrive6_FanSpeed_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &faninfo) != ADL_OK) return -1; return faninfo.iFanSpeedPercent; } @@ -602,7 +504,7 @@ int hm_get_fanspeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_ { int speed = 0; - if (hm_NVML_nvmlDeviceGetFanSpeed (data.hm_nvml, 0, data.hm_device[device_id].nvml, (uint *) &speed) != NVML_SUCCESS) return -1; + if (hm_NVML_nvmlDeviceGetFanSpeed (hwmon_ctx->hm_nvml, 0, hwmon_ctx->hm_device[device_id].nvml, (uint *) &speed) != NVML_SUCCESS) return -1; return speed; } @@ -611,19 +513,19 @@ int hm_get_fanspeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_ return -1; } -int hm_get_buslanes_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id) +int hm_get_buslanes_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id) { if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) { - if (data.hm_adl) + if (hwmon_ctx->hm_adl) { ADLPMActivity PMActivity; PMActivity.iSize = sizeof (ADLPMActivity); - if (hm_ADL_Overdrive_CurrentActivity_Get (data.hm_adl, data.hm_device[device_id].adl, &PMActivity) != ADL_OK) return -1; + if (hm_ADL_Overdrive_CurrentActivity_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &PMActivity) != ADL_OK) return -1; return PMActivity.iCurrentBusLanes; } @@ -633,7 +535,7 @@ int hm_get_buslanes_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_ { unsigned int currLinkWidth; - if (hm_NVML_nvmlDeviceGetCurrPcieLinkWidth (data.hm_nvml, 1, data.hm_device[device_id].nvml, &currLinkWidth) != NVML_SUCCESS) return -1; + if (hm_NVML_nvmlDeviceGetCurrPcieLinkWidth (hwmon_ctx->hm_nvml, 1, hwmon_ctx->hm_device[device_id].nvml, &currLinkWidth) != NVML_SUCCESS) return -1; return currLinkWidth; } @@ -641,19 +543,19 @@ int hm_get_buslanes_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_ return -1; } -int hm_get_utilization_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id) +int hm_get_utilization_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id) { if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) { - if (data.hm_adl) + if (hwmon_ctx->hm_adl) { ADLPMActivity PMActivity; PMActivity.iSize = sizeof (ADLPMActivity); - if (hm_ADL_Overdrive_CurrentActivity_Get (data.hm_adl, data.hm_device[device_id].adl, &PMActivity) != ADL_OK) return -1; + if (hm_ADL_Overdrive_CurrentActivity_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &PMActivity) != ADL_OK) return -1; return PMActivity.iActivityPercent; } @@ -663,7 +565,7 @@ int hm_get_utilization_with_device_id (opencl_ctx_t *opencl_ctx, const uint devi { nvmlUtilization_t utilization; - if (hm_NVML_nvmlDeviceGetUtilizationRates (data.hm_nvml, 1, data.hm_device[device_id].nvml, &utilization) != NVML_SUCCESS) return -1; + if (hm_NVML_nvmlDeviceGetUtilizationRates (hwmon_ctx->hm_nvml, 1, hwmon_ctx->hm_device[device_id].nvml, &utilization) != NVML_SUCCESS) return -1; return utilization.gpu; } @@ -671,19 +573,19 @@ int hm_get_utilization_with_device_id (opencl_ctx_t *opencl_ctx, const uint devi return -1; } -int hm_get_memoryspeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id) +int hm_get_memoryspeed_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id) { if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) { - if (data.hm_adl) + if (hwmon_ctx->hm_adl) { ADLPMActivity PMActivity; PMActivity.iSize = sizeof (ADLPMActivity); - if (hm_ADL_Overdrive_CurrentActivity_Get (data.hm_adl, data.hm_device[device_id].adl, &PMActivity) != ADL_OK) return -1; + if (hm_ADL_Overdrive_CurrentActivity_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &PMActivity) != ADL_OK) return -1; return PMActivity.iMemoryClock / 100; } @@ -693,7 +595,7 @@ int hm_get_memoryspeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint devi { unsigned int clock; - if (hm_NVML_nvmlDeviceGetClockInfo (data.hm_nvml, 1, data.hm_device[device_id].nvml, NVML_CLOCK_MEM, &clock) != NVML_SUCCESS) return -1; + if (hm_NVML_nvmlDeviceGetClockInfo (hwmon_ctx->hm_nvml, 1, hwmon_ctx->hm_device[device_id].nvml, NVML_CLOCK_MEM, &clock) != NVML_SUCCESS) return -1; return clock; } @@ -701,19 +603,19 @@ int hm_get_memoryspeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint devi return -1; } -int hm_get_corespeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id) +int hm_get_corespeed_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id) { if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) { - if (data.hm_adl) + if (hwmon_ctx->hm_adl) { ADLPMActivity PMActivity; PMActivity.iSize = sizeof (ADLPMActivity); - if (hm_ADL_Overdrive_CurrentActivity_Get (data.hm_adl, data.hm_device[device_id].adl, &PMActivity) != ADL_OK) return -1; + if (hm_ADL_Overdrive_CurrentActivity_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &PMActivity) != ADL_OK) return -1; return PMActivity.iEngineClock / 100; } @@ -723,7 +625,7 @@ int hm_get_corespeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device { unsigned int clock; - if (hm_NVML_nvmlDeviceGetClockInfo (data.hm_nvml, 1, data.hm_device[device_id].nvml, NVML_CLOCK_SM, &clock) != NVML_SUCCESS) return -1; + if (hm_NVML_nvmlDeviceGetClockInfo (hwmon_ctx->hm_nvml, 1, hwmon_ctx->hm_device[device_id].nvml, NVML_CLOCK_SM, &clock) != NVML_SUCCESS) return -1; return clock; } @@ -731,7 +633,7 @@ int hm_get_corespeed_with_device_id (opencl_ctx_t *opencl_ctx, const uint device return -1; } -int hm_get_throttle_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_id) +int hm_get_throttle_with_device_id (const hwmon_ctx_t *hwmon_ctx, const opencl_ctx_t *opencl_ctx, const uint device_id) { if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) return -1; @@ -745,8 +647,8 @@ int hm_get_throttle_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_ unsigned long long clocksThrottleReasons = 0; unsigned long long supportedThrottleReasons = 0; - if (hm_NVML_nvmlDeviceGetCurrentClocksThrottleReasons (data.hm_nvml, 1, data.hm_device[device_id].nvml, &clocksThrottleReasons) != NVML_SUCCESS) return -1; - if (hm_NVML_nvmlDeviceGetSupportedClocksThrottleReasons (data.hm_nvml, 1, data.hm_device[device_id].nvml, &supportedThrottleReasons) != NVML_SUCCESS) return -1; + if (hm_NVML_nvmlDeviceGetCurrentClocksThrottleReasons (hwmon_ctx->hm_nvml, 1, hwmon_ctx->hm_device[device_id].nvml, &clocksThrottleReasons) != NVML_SUCCESS) return -1; + if (hm_NVML_nvmlDeviceGetSupportedClocksThrottleReasons (hwmon_ctx->hm_nvml, 1, hwmon_ctx->hm_device[device_id].nvml, &supportedThrottleReasons) != NVML_SUCCESS) return -1; clocksThrottleReasons &= supportedThrottleReasons; clocksThrottleReasons &= ~nvmlClocksThrottleReasonGpuIdle; @@ -764,15 +666,15 @@ int hm_get_throttle_with_device_id (opencl_ctx_t *opencl_ctx, const uint device_ return -1; } -int hm_set_fanspeed_with_device_id_adl (const uint device_id, const int fanspeed, const int fanpolicy) +int hm_set_fanspeed_with_device_id_adl (hwmon_ctx_t *hwmon_ctx, const uint device_id, const int fanspeed, const int fanpolicy) { - if (data.hm_device[device_id].fan_set_supported == 1) + if (hwmon_ctx->hm_device[device_id].fan_set_supported == true) { - if (data.hm_adl) + if (hwmon_ctx->hm_adl) { if (fanpolicy == 1) { - if (data.hm_device[device_id].od_version == 5) + if (hwmon_ctx->hm_device[device_id].od_version == 5) { ADLFanSpeedValue lpFanSpeedValue; @@ -783,7 +685,7 @@ int hm_set_fanspeed_with_device_id_adl (const uint device_id, const int fanspeed lpFanSpeedValue.iFlags = ADL_DL_FANCTRL_FLAG_USER_DEFINED_SPEED; lpFanSpeedValue.iFanSpeed = fanspeed; - if (hm_ADL_Overdrive5_FanSpeed_Set (data.hm_adl, data.hm_device[device_id].adl, 0, &lpFanSpeedValue) != ADL_OK) return -1; + if (hm_ADL_Overdrive5_FanSpeed_Set (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, 0, &lpFanSpeedValue) != ADL_OK) return -1; return 0; } @@ -796,22 +698,22 @@ int hm_set_fanspeed_with_device_id_adl (const uint device_id, const int fanspeed fan_speed_value.iSpeedType = ADL_OD6_FANSPEED_TYPE_PERCENT; fan_speed_value.iFanSpeed = fanspeed; - if (hm_ADL_Overdrive6_FanSpeed_Set (data.hm_adl, data.hm_device[device_id].adl, &fan_speed_value) != ADL_OK) return -1; + if (hm_ADL_Overdrive6_FanSpeed_Set (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &fan_speed_value) != ADL_OK) return -1; return 0; } } else { - if (data.hm_device[device_id].od_version == 5) + if (hwmon_ctx->hm_device[device_id].od_version == 5) { - if (hm_ADL_Overdrive5_FanSpeedToDefault_Set (data.hm_adl, data.hm_device[device_id].adl, 0) != ADL_OK) return -1; + if (hm_ADL_Overdrive5_FanSpeedToDefault_Set (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, 0) != ADL_OK) return -1; return 0; } else // od_version == 6 { - if (hm_ADL_Overdrive6_FanSpeed_Reset (data.hm_adl, data.hm_device[device_id].adl) != ADL_OK) return -1; + if (hm_ADL_Overdrive6_FanSpeed_Reset (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl) != ADL_OK) return -1; return 0; } @@ -822,11 +724,11 @@ int hm_set_fanspeed_with_device_id_adl (const uint device_id, const int fanspeed return -1; } -int hm_set_fanspeed_with_device_id_nvapi (const uint device_id, const int fanspeed, const int fanpolicy) +int hm_set_fanspeed_with_device_id_nvapi (hwmon_ctx_t *hwmon_ctx, const uint device_id, const int fanspeed, const int fanpolicy) { - if (data.hm_device[device_id].fan_set_supported == 1) + if (hwmon_ctx->hm_device[device_id].fan_set_supported == true) { - if (data.hm_nvapi) + if (hwmon_ctx->hm_nvapi) { if (fanpolicy == 1) { @@ -839,13 +741,13 @@ int hm_set_fanspeed_with_device_id_nvapi (const uint device_id, const int fanspe CoolerLevels.Levels[0].Level = fanspeed; CoolerLevels.Levels[0].Policy = 1; - if (hm_NvAPI_GPU_SetCoolerLevels (data.hm_nvapi, data.hm_device[device_id].nvapi, 0, &CoolerLevels) != NVAPI_OK) return -1; + if (hm_NvAPI_GPU_SetCoolerLevels (hwmon_ctx->hm_nvapi, hwmon_ctx->hm_device[device_id].nvapi, 0, &CoolerLevels) != NVAPI_OK) return -1; return 0; } else { - if (hm_NvAPI_GPU_RestoreCoolerSettings (data.hm_nvapi, data.hm_device[device_id].nvapi, 0) != NVAPI_OK) return -1; + if (hm_NvAPI_GPU_RestoreCoolerSettings (hwmon_ctx->hm_nvapi, hwmon_ctx->hm_device[device_id].nvapi, 0) != NVAPI_OK) return -1; return 0; } @@ -855,13 +757,13 @@ int hm_set_fanspeed_with_device_id_nvapi (const uint device_id, const int fanspe return -1; } -int hm_set_fanspeed_with_device_id_xnvctrl (const uint device_id, const int fanspeed) +int hm_set_fanspeed_with_device_id_xnvctrl (hwmon_ctx_t *hwmon_ctx, const uint device_id, const int fanspeed) { - if (data.hm_device[device_id].fan_set_supported == 1) + if (hwmon_ctx->hm_device[device_id].fan_set_supported == true) { - if (data.hm_xnvctrl) + if (hwmon_ctx->hm_xnvctrl) { - if (set_fan_speed_target (data.hm_xnvctrl, data.hm_device[device_id].xnvctrl, fanspeed) != 0) return -1; + if (set_fan_speed_target (hwmon_ctx->hm_xnvctrl, hwmon_ctx->hm_device[device_id].xnvctrl, fanspeed) != 0) return -1; return 0; } @@ -870,7 +772,7 @@ int hm_set_fanspeed_with_device_id_xnvctrl (const uint device_id, const int fans return -1; } -int hwmon_ctx_init (hwmon_ctx_t *hwmon_ctx, const user_options_t *user_options) +int hwmon_ctx_init (hwmon_ctx_t *hwmon_ctx, const user_options_t *user_options, const opencl_ctx_t *opencl_ctx) { hwmon_ctx->enabled = false; @@ -883,15 +785,596 @@ int hwmon_ctx_init (hwmon_ctx_t *hwmon_ctx, const user_options_t *user_options) hwmon_ctx->hm_device = (hm_attrs_t *) mycalloc (DEVICES_MAX, sizeof (hm_attrs_t)); + /** + * Initialize shared libraries + */ + + ADL_PTR *adl = (ADL_PTR *) mymalloc (sizeof (ADL_PTR)); + NVAPI_PTR *nvapi = (NVAPI_PTR *) mymalloc (sizeof (NVAPI_PTR)); + NVML_PTR *nvml = (NVML_PTR *) mymalloc (sizeof (NVML_PTR)); + XNVCTRL_PTR *xnvctrl = (XNVCTRL_PTR *) mymalloc (sizeof (XNVCTRL_PTR)); + + hwmon_ctx->hm_adl = NULL; + hwmon_ctx->hm_nvapi = NULL; + hwmon_ctx->hm_nvml = NULL; + hwmon_ctx->hm_xnvctrl = NULL; + + if ((opencl_ctx->need_nvml == true) && (nvml_init (nvml) == 0)) + { + hwmon_ctx->hm_nvml = nvml; + } + + if (hwmon_ctx->hm_nvml) + { + if (hm_NVML_nvmlInit (hwmon_ctx->hm_nvml) == NVML_SUCCESS) + { + HM_ADAPTER_NVML *nvmlGPUHandle = (HM_ADAPTER_NVML *) mycalloc (DEVICES_MAX, sizeof (HM_ADAPTER_NVML)); + + int tmp_in = hm_get_adapter_index_nvml (hwmon_ctx, nvmlGPUHandle); + + int tmp_out = 0; + + for (int i = 0; i < tmp_in; i++) + { + hwmon_ctx->hm_adapters_nvml[tmp_out++].nvml = nvmlGPUHandle[i]; + } + + for (int i = 0; i < tmp_out; i++) + { + unsigned int speed; + + if (hm_NVML_nvmlDeviceGetFanSpeed (hwmon_ctx->hm_nvml, 0, hwmon_ctx->hm_adapters_nvml[i].nvml, &speed) == NVML_SUCCESS) hwmon_ctx->hm_adapters_nvml[i].fan_get_supported = true; + + // doesn't seem to create any advantages + //hm_NVML_nvmlDeviceSetComputeMode (hwmon_ctx->hm_nvml, 1, hm_adapters_nvml[i].nvml, NVML_COMPUTEMODE_EXCLUSIVE_PROCESS); + //hm_NVML_nvmlDeviceSetGpuOperationMode (hwmon_ctx->hm_nvml, 1, hm_adapters_nvml[i].nvml, NVML_GOM_ALL_ON); + } + + myfree (nvmlGPUHandle); + } + } + + if ((opencl_ctx->need_nvapi == true) && (nvapi_init (nvapi) == 0)) + { + hwmon_ctx->hm_nvapi = nvapi; + } + + if (hwmon_ctx->hm_nvapi) + { + if (hm_NvAPI_Initialize (hwmon_ctx->hm_nvapi) == NVAPI_OK) + { + HM_ADAPTER_NVAPI *nvGPUHandle = (HM_ADAPTER_NVAPI *) mycalloc (DEVICES_MAX, sizeof (HM_ADAPTER_NVAPI)); + + int tmp_in = hm_get_adapter_index_nvapi (hwmon_ctx, nvGPUHandle); + + int tmp_out = 0; + + for (int i = 0; i < tmp_in; i++) + { + hwmon_ctx->hm_adapters_nvapi[tmp_out++].nvapi = nvGPUHandle[i]; + } + + myfree (nvGPUHandle); + } + } + + if ((opencl_ctx->need_xnvctrl == true) && (xnvctrl_init (xnvctrl) == 0)) + { + hwmon_ctx->hm_xnvctrl = xnvctrl; + } + + if (hwmon_ctx->hm_xnvctrl) + { + if (hm_XNVCTRL_XOpenDisplay (hwmon_ctx->hm_xnvctrl) == 0) + { + for (uint 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; + + hwmon_ctx->hm_adapters_xnvctrl[device_id].xnvctrl = device_id; + + int speed = 0; + + if (get_fan_speed_current (hwmon_ctx->hm_xnvctrl, device_id, &speed) == 0) hwmon_ctx->hm_adapters_xnvctrl[device_id].fan_get_supported = true; + } + } + } + + if ((opencl_ctx->need_adl == true) && (adl_init (adl) == 0)) + { + hwmon_ctx->hm_adl = adl; + } + + if (hwmon_ctx->hm_adl) + { + if (hm_ADL_Main_Control_Create (hwmon_ctx->hm_adl, ADL_Main_Memory_Alloc, 0) == ADL_OK) + { + // total number of adapters + + int hm_adapters_num; + + if (get_adapters_num_adl (hwmon_ctx->hm_adl, &hm_adapters_num) != 0) return -1; + + // adapter info + + LPAdapterInfo lpAdapterInfo = hm_get_adapter_info_adl (hwmon_ctx->hm_adl, hm_adapters_num); + + if (lpAdapterInfo == NULL) return -1; + + // get a list (of ids of) valid/usable adapters + + int num_adl_adapters = 0; + + u32 *valid_adl_device_list = hm_get_list_valid_adl_adapters (hm_adapters_num, &num_adl_adapters, lpAdapterInfo); + + if (num_adl_adapters > 0) + { + // hm_get_opencl_busid_devid (hm_adapters_adl, devices_all_cnt, devices_all); + + hm_get_adapter_index_adl (hwmon_ctx->hm_adapters_adl, valid_adl_device_list, num_adl_adapters, lpAdapterInfo); + + hm_get_overdrive_version (hwmon_ctx->hm_adl, hwmon_ctx->hm_adapters_adl, valid_adl_device_list, num_adl_adapters, lpAdapterInfo); + + hm_check_fanspeed_control (hwmon_ctx->hm_adl, hwmon_ctx->hm_adapters_adl, valid_adl_device_list, num_adl_adapters, lpAdapterInfo); + } + + myfree (valid_adl_device_list); + + myfree (lpAdapterInfo); + } + } + + if (hwmon_ctx->hm_adl == NULL && hwmon_ctx->hm_nvml == NULL && hwmon_ctx->hm_xnvctrl == NULL) + { + return 0; + } + + /** + * looks like we have some manageable device + */ + hwmon_ctx->enabled = true; + /** + * save buffer required for later restores + */ + + hwmon_ctx->od_clock_mem_status = (ADLOD6MemClockState *) mycalloc (opencl_ctx->devices_cnt, sizeof (ADLOD6MemClockState)); + + hwmon_ctx->od_power_control_status = (int *) mycalloc (opencl_ctx->devices_cnt, sizeof (int)); + + hwmon_ctx->nvml_power_limit = (unsigned int *) mycalloc (opencl_ctx->devices_cnt, sizeof (unsigned int)); + + /** + * HM devices: copy + */ + + for (uint 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->skipped) continue; + + if ((device_param->device_type & CL_DEVICE_TYPE_GPU) == 0) continue; + + const uint platform_devices_id = device_param->platform_devices_id; + + if (device_param->device_vendor_id == VENDOR_ID_AMD) + { + hwmon_ctx->hm_device[device_id].adl = hwmon_ctx->hm_adapters_adl[platform_devices_id].adl; + 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 = hwmon_ctx->hm_adapters_adl[platform_devices_id].od_version; + hwmon_ctx->hm_device[device_id].fan_get_supported = hwmon_ctx->hm_adapters_adl[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].nvapi = hwmon_ctx->hm_adapters_nvapi[platform_devices_id].nvapi; + hwmon_ctx->hm_device[device_id].nvml = hwmon_ctx->hm_adapters_nvml[platform_devices_id].nvml; + hwmon_ctx->hm_device[device_id].xnvctrl = hwmon_ctx->hm_adapters_xnvctrl[platform_devices_id].xnvctrl; + hwmon_ctx->hm_device[device_id].od_version = 0; + hwmon_ctx->hm_device[device_id].fan_get_supported = hwmon_ctx->hm_adapters_nvml[platform_devices_id].fan_get_supported; + hwmon_ctx->hm_device[device_id].fan_set_supported = false; + } + } + + /** + * powertune on user request + */ + + for (uint 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->skipped) continue; + + if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) + { + /** + * Temporary fix: + * with AMD r9 295x cards it seems that we need to set the powertune value just AFTER the ocl init stuff + * otherwise after hc_clCreateContext () etc, powertune value was set back to "normal" and cards unfortunately + * were not working @ full speed (setting hm_ADL_Overdrive_PowerControl_Set () here seems to fix the problem) + * Driver / ADL bug? + */ + + if (hwmon_ctx->hm_device[device_id].od_version == 6) + { + int ADL_rc; + + // check powertune capabilities first, if not available then skip device + + int powertune_supported = 0; + + if ((ADL_rc = hm_ADL_Overdrive6_PowerControl_Caps (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &powertune_supported)) != ADL_OK) + { + log_error ("ERROR: Failed to get ADL PowerControl Capabilities"); + + return -1; + } + + // first backup current value, we will restore it later + + if (powertune_supported != 0) + { + // powercontrol settings + + ADLOD6PowerControlInfo powertune = {0, 0, 0, 0, 0}; + + if ((ADL_rc = hm_ADL_Overdrive_PowerControlInfo_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &powertune)) == ADL_OK) + { + ADL_rc = hm_ADL_Overdrive_PowerControl_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &hwmon_ctx->od_power_control_status[device_id]); + } + + if (ADL_rc != ADL_OK) + { + log_error ("ERROR: Failed to get current ADL PowerControl settings"); + + return -1; + } + + if ((ADL_rc = hm_ADL_Overdrive_PowerControl_Set (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, powertune.iMaxValue)) != ADL_OK) + { + log_error ("ERROR: Failed to set new ADL PowerControl values"); + + return -1; + } + + // clocks + + memset (&hwmon_ctx->od_clock_mem_status[device_id], 0, sizeof (ADLOD6MemClockState)); + + hwmon_ctx->od_clock_mem_status[device_id].state.iNumberOfPerformanceLevels = 2; + + if ((ADL_rc = hm_ADL_Overdrive_StateInfo_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, ADL_OD6_GETSTATEINFO_CUSTOM_PERFORMANCE, &hwmon_ctx->od_clock_mem_status[device_id])) != ADL_OK) + { + log_error ("ERROR: Failed to get ADL memory and engine clock frequency"); + + return -1; + } + + // Query capabilities only to see if profiles were not "damaged", if so output a warning but do accept the users profile settings + + ADLOD6Capabilities caps = {0, 0, 0, {0, 0, 0}, {0, 0, 0}, 0, 0}; + + if ((ADL_rc = hm_ADL_Overdrive_Capabilities_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &caps)) != ADL_OK) + { + log_error ("ERROR: Failed to get ADL device capabilities"); + + return -1; + } + + int engine_clock_max = (int) (0.6666 * caps.sEngineClockRange.iMax); + int memory_clock_max = (int) (0.6250 * caps.sMemoryClockRange.iMax); + + int warning_trigger_engine = (int) (0.25 * engine_clock_max); + int warning_trigger_memory = (int) (0.25 * memory_clock_max); + + int engine_clock_profile_max = hwmon_ctx->od_clock_mem_status[device_id].state.aLevels[1].iEngineClock; + int memory_clock_profile_max = hwmon_ctx->od_clock_mem_status[device_id].state.aLevels[1].iMemoryClock; + + // warning if profile has too low max values + + if ((engine_clock_max - engine_clock_profile_max) > warning_trigger_engine) + { + log_info ("WARN: The custom profile seems to have too low maximum engine clock values. You therefore may not reach full performance"); + } + + if ((memory_clock_max - memory_clock_profile_max) > warning_trigger_memory) + { + log_info ("WARN: The custom profile seems to have too low maximum memory clock values. You therefore may not reach full performance"); + } + + ADLOD6StateInfo *performance_state = (ADLOD6StateInfo*) mycalloc (1, sizeof (ADLOD6StateInfo) + sizeof (ADLOD6PerformanceLevel)); + + performance_state->iNumberOfPerformanceLevels = 2; + + performance_state->aLevels[0].iEngineClock = engine_clock_profile_max; + performance_state->aLevels[1].iEngineClock = engine_clock_profile_max; + performance_state->aLevels[0].iMemoryClock = memory_clock_profile_max; + performance_state->aLevels[1].iMemoryClock = memory_clock_profile_max; + + if ((ADL_rc = hm_ADL_Overdrive_State_Set (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, ADL_OD6_SETSTATE_PERFORMANCE, performance_state)) != ADL_OK) + { + log_info ("ERROR: Failed to set ADL performance state"); + + return -1; + } + + myfree (performance_state); + } + + // set powertune value only + + if (powertune_supported != 0) + { + // powertune set + ADLOD6PowerControlInfo powertune = {0, 0, 0, 0, 0}; + + if ((ADL_rc = hm_ADL_Overdrive_PowerControlInfo_Get (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &powertune)) != ADL_OK) + { + log_error ("ERROR: Failed to get current ADL PowerControl settings"); + + return -1; + } + + if ((ADL_rc = hm_ADL_Overdrive_PowerControl_Set (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, powertune.iMaxValue)) != ADL_OK) + { + log_error ("ERROR: Failed to set new ADL PowerControl values"); + + return -1; + } + } + } + } + + if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_NV) + { + // first backup current value, we will restore it later + + unsigned int limit; + + bool powertune_supported = false; + + if (hm_NVML_nvmlDeviceGetPowerManagementLimit (hwmon_ctx->hm_nvml, 0, hwmon_ctx->hm_device[device_id].nvml, &limit) == NVML_SUCCESS) + { + powertune_supported = true; + } + + // if backup worked, activate the maximum allowed + + if (powertune_supported == true) + { + unsigned int minLimit; + unsigned int maxLimit; + + if (hm_NVML_nvmlDeviceGetPowerManagementLimitConstraints (hwmon_ctx->hm_nvml, 0, hwmon_ctx->hm_device[device_id].nvml, &minLimit, &maxLimit) == NVML_SUCCESS) + { + if (maxLimit > 0) + { + if (hm_NVML_nvmlDeviceSetPowerManagementLimit (hwmon_ctx->hm_nvml, 0, hwmon_ctx->hm_device[device_id].nvml, maxLimit) == NVML_SUCCESS) + { + // now we can be sure we need to reset later + + hwmon_ctx->nvml_power_limit[device_id] = limit; + } + } + } + } + } + } + + /** + * Store initial fanspeed if gpu_temp_retain is enabled + */ + + if (user_options->gpu_temp_retain) + { + for (uint 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->skipped) continue; + + if (hwmon_ctx->hm_device[device_id].fan_get_supported == true) + { + const int fanspeed = hm_get_fanspeed_with_device_id (hwmon_ctx, opencl_ctx, device_id); + const int fanpolicy = hm_get_fanpolicy_with_device_id (hwmon_ctx, opencl_ctx, device_id); + + // we also set it to tell the OS we take control over the fan and it's automatic controller + // if it was set to automatic. we do not control user-defined fanspeeds. + + if (fanpolicy == 1) + { + hwmon_ctx->hm_device[device_id].fan_set_supported = true; + + int rc = -1; + + if (device_param->device_vendor_id == VENDOR_ID_AMD) + { + rc = hm_set_fanspeed_with_device_id_adl (hwmon_ctx, device_id, fanspeed, 1); + } + else if (device_param->device_vendor_id == VENDOR_ID_NV) + { + #if defined (__linux__) + rc = set_fan_control (hwmon_ctx->hm_xnvctrl, hwmon_ctx->hm_device[device_id].xnvctrl, NV_CTRL_GPU_COOLER_MANUAL_CONTROL_TRUE); + #endif + + #if defined (_WIN) + rc = hm_set_fanspeed_with_device_id_nvapi (hwmon_ctx, device_id, fanspeed, 1); + #endif + } + + if (rc == 0) + { + hwmon_ctx->hm_device[device_id].fan_set_supported = true; + } + else + { + log_info ("WARNING: Failed to set initial fan speed for device #%u", device_id + 1); + + hwmon_ctx->hm_device[device_id].fan_set_supported = false; + } + } + else + { + hwmon_ctx->hm_device[device_id].fan_set_supported = false; + } + } + } + } + return 0; } -void hwmon_ctx_destroy (hwmon_ctx_t *hwmon_ctx) +void hwmon_ctx_destroy (hwmon_ctx_t *hwmon_ctx, const user_options_t *user_options, const opencl_ctx_t *opencl_ctx) { if (hwmon_ctx->enabled == false) return; + // reset default fan speed + + if (user_options->gpu_temp_retain) + { + for (uint 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->skipped) continue; + + if (hwmon_ctx->hm_device[device_id].fan_set_supported == true) + { + int rc = -1; + + if (device_param->device_vendor_id == VENDOR_ID_AMD) + { + rc = hm_set_fanspeed_with_device_id_adl (hwmon_ctx, device_id, 100, 0); + } + else if (device_param->device_vendor_id == VENDOR_ID_NV) + { + #if defined (__linux__) + rc = set_fan_control (hwmon_ctx->hm_xnvctrl, hwmon_ctx->hm_device[device_id].xnvctrl, NV_CTRL_GPU_COOLER_MANUAL_CONTROL_FALSE); + #endif + + #if defined (_WIN) + rc = hm_set_fanspeed_with_device_id_nvapi (hwmon_ctx, device_id, 100, 0); + #endif + } + + if (rc == -1) log_info ("WARNING: Failed to restore default fan speed and policy for device #%", device_id + 1); + } + } + } + + // reset power tuning + + for (uint 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->skipped) continue; + + if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_AMD) + { + if (hwmon_ctx->hm_device[device_id].od_version == 6) + { + // check powertune capabilities first, if not available then skip device + + int powertune_supported = 0; + + if ((hm_ADL_Overdrive6_PowerControl_Caps (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, &powertune_supported)) != ADL_OK) + { + log_error ("ERROR: Failed to get ADL PowerControl Capabilities"); + + continue; + } + + if (powertune_supported != 0) + { + // powercontrol settings + + if ((hm_ADL_Overdrive_PowerControl_Set (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, hwmon_ctx->od_power_control_status[device_id])) != ADL_OK) + { + log_info ("ERROR: Failed to restore the ADL PowerControl values"); + + continue; + } + + // clocks + + ADLOD6StateInfo *performance_state = (ADLOD6StateInfo*) mycalloc (1, sizeof (ADLOD6StateInfo) + sizeof (ADLOD6PerformanceLevel)); + + performance_state->iNumberOfPerformanceLevels = 2; + + performance_state->aLevels[0].iEngineClock = hwmon_ctx->od_clock_mem_status[device_id].state.aLevels[0].iEngineClock; + performance_state->aLevels[1].iEngineClock = hwmon_ctx->od_clock_mem_status[device_id].state.aLevels[1].iEngineClock; + performance_state->aLevels[0].iMemoryClock = hwmon_ctx->od_clock_mem_status[device_id].state.aLevels[0].iMemoryClock; + performance_state->aLevels[1].iMemoryClock = hwmon_ctx->od_clock_mem_status[device_id].state.aLevels[1].iMemoryClock; + + if ((hm_ADL_Overdrive_State_Set (hwmon_ctx->hm_adl, hwmon_ctx->hm_device[device_id].adl, ADL_OD6_SETSTATE_PERFORMANCE, performance_state)) != ADL_OK) + { + log_info ("ERROR: Failed to restore ADL performance state"); + + continue; + } + + myfree (performance_state); + } + } + } + + if (opencl_ctx->devices_param[device_id].device_vendor_id == VENDOR_ID_NV) + { + unsigned int power_limit = hwmon_ctx->nvml_power_limit[device_id]; + + if (power_limit > 0) + { + hm_NVML_nvmlDeviceSetPowerManagementLimit (hwmon_ctx->hm_nvml, 0, hwmon_ctx->hm_device[device_id].nvml, power_limit); + } + } + } + + // unload shared libraries + + if (hwmon_ctx->hm_nvml) + { + hm_NVML_nvmlShutdown (hwmon_ctx->hm_nvml); + + nvml_close (hwmon_ctx->hm_nvml); + } + + if (hwmon_ctx->hm_nvapi) + { + hm_NvAPI_Unload (hwmon_ctx->hm_nvapi); + + nvapi_close (hwmon_ctx->hm_nvapi); + } + + if (hwmon_ctx->hm_xnvctrl) + { + hm_XNVCTRL_XCloseDisplay (hwmon_ctx->hm_xnvctrl); + + xnvctrl_close (hwmon_ctx->hm_xnvctrl); + } + + if (hwmon_ctx->hm_adl) + { + hm_ADL_Main_Control_Destroy (hwmon_ctx->hm_adl); + + adl_close (hwmon_ctx->hm_adl); + } + + // free memory + + myfree (hwmon_ctx->nvml_power_limit); + myfree (hwmon_ctx->od_power_control_status); + myfree (hwmon_ctx->od_clock_mem_status); + myfree (hwmon_ctx->hm_device); myfree (hwmon_ctx->hm_adapters_adl); @@ -899,6 +1382,10 @@ void hwmon_ctx_destroy (hwmon_ctx_t *hwmon_ctx) myfree (hwmon_ctx->hm_adapters_nvml); myfree (hwmon_ctx->hm_adapters_xnvctrl); + hwmon_ctx->nvml_power_limit = NULL; + hwmon_ctx->od_power_control_status = NULL; + hwmon_ctx->od_clock_mem_status = NULL; + hwmon_ctx->hm_device = NULL; hwmon_ctx->hm_adapters_adl = NULL; diff --git a/src/monitor.c b/src/monitor.c index 1da0e0d72..ecc24a335 100644 --- a/src/monitor.c +++ b/src/monitor.c @@ -48,6 +48,7 @@ void *thread_monitor (void *p) combinator_ctx_t *combinator_ctx = data.combinator_ctx; mask_ctx_t *mask_ctx = data.mask_ctx; opencl_ctx_t *opencl_ctx = data.opencl_ctx; + hwmon_ctx_t *hwmon_ctx = data.hwmon_ctx; bool runtime_check = false; bool remove_check = false; @@ -127,7 +128,7 @@ void *thread_monitor (void *p) if (device_param->device_vendor_id == VENDOR_ID_NV) { - if (data.hm_nvapi) + if (hwmon_ctx->hm_nvapi) { NV_GPU_PERF_POLICIES_INFO_PARAMS_V1 perfPolicies_info; NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 perfPolicies_status; @@ -138,11 +139,11 @@ void *thread_monitor (void *p) perfPolicies_info.version = MAKE_NVAPI_VERSION (NV_GPU_PERF_POLICIES_INFO_PARAMS_V1, 1); perfPolicies_status.version = MAKE_NVAPI_VERSION (NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1, 1); - hm_NvAPI_GPU_GetPerfPoliciesInfo (data.hm_nvapi, data.hm_device[device_id].nvapi, &perfPolicies_info); + hm_NvAPI_GPU_GetPerfPoliciesInfo (hwmon_ctx->hm_nvapi, hwmon_ctx->hm_device[device_id].nvapi, &perfPolicies_info); perfPolicies_status.info_value = perfPolicies_info.info_value; - hm_NvAPI_GPU_GetPerfPoliciesStatus (data.hm_nvapi, data.hm_device[device_id].nvapi, &perfPolicies_status); + hm_NvAPI_GPU_GetPerfPoliciesStatus (hwmon_ctx->hm_nvapi, hwmon_ctx->hm_device[device_id].nvapi, &perfPolicies_status); if (perfPolicies_status.throttle & 2) { @@ -193,7 +194,7 @@ void *thread_monitor (void *p) if ((opencl_ctx->devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) continue; - const int temperature = hm_get_temperature_with_device_id (opencl_ctx, device_id); + const int temperature = hm_get_temperature_with_device_id (hwmon_ctx, opencl_ctx, device_id); if (temperature > (int) user_options->gpu_temp_abort) { @@ -208,7 +209,7 @@ void *thread_monitor (void *p) if (gpu_temp_retain) { - if (data.hm_device[device_id].fan_set_supported == 1) + if (hwmon_ctx->hm_device[device_id].fan_set_supported == 1) { int temp_cur = temperature; @@ -230,7 +231,7 @@ void *thread_monitor (void *p) if (abs (fan_diff_required) >= temp_threshold) { - const int fan_speed_cur = hm_get_fanspeed_with_device_id (opencl_ctx, device_id); + const int fan_speed_cur = hm_get_fanspeed_with_device_id (hwmon_ctx, opencl_ctx, device_id); int fan_speed_level = fan_speed_cur; @@ -250,16 +251,16 @@ void *thread_monitor (void *p) { if (device_param->device_vendor_id == VENDOR_ID_AMD) { - hm_set_fanspeed_with_device_id_adl (device_id, fan_speed_new, 1); + hm_set_fanspeed_with_device_id_adl (hwmon_ctx, device_id, fan_speed_new, 1); } else if (device_param->device_vendor_id == VENDOR_ID_NV) { #if defined (_WIN) - hm_set_fanspeed_with_device_id_nvapi (device_id, fan_speed_new, 1); + hm_set_fanspeed_with_device_id_nvapi (hwmon_ctx, device_id, fan_speed_new, 1); #endif #if defined (__linux__) - hm_set_fanspeed_with_device_id_xnvctrl (device_id, fan_speed_new); + hm_set_fanspeed_with_device_id_xnvctrl (hwmon_ctx, device_id, fan_speed_new); #endif } @@ -347,7 +348,7 @@ void *thread_monitor (void *p) if (user_options->quiet == false) log_info (""); - status_display (opencl_ctx, hashconfig, hashes, restore_ctx, user_options, user_options_extra, straight_ctx, combinator_ctx, mask_ctx); + status_display (opencl_ctx, hwmon_ctx, hashconfig, hashes, restore_ctx, user_options, user_options_extra, straight_ctx, combinator_ctx, mask_ctx); if (user_options->quiet == false) log_info (""); diff --git a/src/opencl.c b/src/opencl.c index c9a07235f..bf1c2a7e3 100644 --- a/src/opencl.c +++ b/src/opencl.c @@ -40,8 +40,6 @@ extern hc_global_data_t data; -extern hc_thread_mutex_t mux_hwmon; - extern const int comptime; static const u32 full01 = 0x01010101; @@ -2262,13 +2260,16 @@ int opencl_ctx_devices_init (opencl_ctx_t *opencl_ctx, const user_options_t *use { char *format = " Device ID #%u\n Type : %s\n Vendor ID : %u\n Vendor : %s\n Name : %s\n Processor(s) : %u\n Clock : %u\n Memory : %lu/%lu MB allocatable\n OpenCL Version : %s\n\n"; - fprintf(stdout, format, device_id + 1, - ((device_type & CL_DEVICE_TYPE_CPU) ? "CPU" : ((device_type & CL_DEVICE_TYPE_GPU) ? "GPU" : "Accelerator")), - device_vendor_id, device_vendor, - device_name, device_processors, - device_maxclock_frequency, - device_maxmem_alloc/1024/1024, device_global_mem/1024/1024, - device_opencl_version); + fprintf (stdout, format, device_id + 1, + ((device_type & CL_DEVICE_TYPE_CPU) ? "CPU" : ((device_type & CL_DEVICE_TYPE_GPU) ? "GPU" : "Accelerator")), + device_vendor_id, + device_vendor, + device_name, + device_processors, + device_maxclock_frequency, + device_maxmem_alloc / 1024 / 1024, + device_global_mem / 1024 / 1024, + device_opencl_version); } myfree (device_opencl_version); diff --git a/src/status.c b/src/status.c index 24889c79a..0eef2faf9 100644 --- a/src/status.c +++ b/src/status.c @@ -164,7 +164,7 @@ double get_avg_exec_time (hc_device_param_t *device_param, const int last_num_en return exec_ms_sum / exec_ms_cnt; } -void status_display_machine_readable (opencl_ctx_t *opencl_ctx, const hashes_t *hashes, const restore_ctx_t *restore_ctx, const user_options_t *user_options, const user_options_extra_t *user_options_extra, const straight_ctx_t *straight_ctx, const combinator_ctx_t *combinator_ctx, const mask_ctx_t *mask_ctx) +void status_display_machine_readable (opencl_ctx_t *opencl_ctx, const hwmon_ctx_t *hwmon_ctx, const hashes_t *hashes, const restore_ctx_t *restore_ctx, const user_options_t *user_options, const user_options_extra_t *user_options_extra, const straight_ctx_t *straight_ctx, const combinator_ctx_t *combinator_ctx, const mask_ctx_t *mask_ctx) { if (opencl_ctx->devices_status == STATUS_INIT) { @@ -297,7 +297,7 @@ void status_display_machine_readable (opencl_ctx_t *opencl_ctx, const hashes_t * if (device_param->skipped) continue; - int temp = hm_get_temperature_with_device_id (opencl_ctx, device_id); + int temp = hm_get_temperature_with_device_id (hwmon_ctx, opencl_ctx, device_id); fprintf (out, "%d\t", temp); } @@ -313,7 +313,7 @@ void status_display_machine_readable (opencl_ctx_t *opencl_ctx, const hashes_t * fflush (out); } -void status_display (opencl_ctx_t *opencl_ctx, const hashconfig_t *hashconfig, const hashes_t *hashes, const restore_ctx_t *restore_ctx, const user_options_t *user_options, const user_options_extra_t *user_options_extra, const straight_ctx_t *straight_ctx, const combinator_ctx_t *combinator_ctx, const mask_ctx_t *mask_ctx) +void status_display (opencl_ctx_t *opencl_ctx, const hwmon_ctx_t *hwmon_ctx, const hashconfig_t *hashconfig, const hashes_t *hashes, const restore_ctx_t *restore_ctx, const user_options_t *user_options, const user_options_extra_t *user_options_extra, const straight_ctx_t *straight_ctx, const combinator_ctx_t *combinator_ctx, const mask_ctx_t *mask_ctx) { if (opencl_ctx->devices_status == STATUS_INIT) { @@ -327,7 +327,7 @@ void status_display (opencl_ctx_t *opencl_ctx, const hashconfig_t *hashconfig, c if (user_options->machine_readable == true) { - status_display_machine_readable (opencl_ctx, hashes, restore_ctx, user_options, user_options_extra, straight_ctx, combinator_ctx, mask_ctx); + status_display_machine_readable (opencl_ctx, hwmon_ctx, hashes, restore_ctx, user_options, user_options_extra, straight_ctx, combinator_ctx, mask_ctx); return; } @@ -1026,13 +1026,13 @@ void status_display (opencl_ctx_t *opencl_ctx, const hashconfig_t *hashconfig, c if (device_param->skipped) continue; - const int num_temperature = hm_get_temperature_with_device_id (opencl_ctx, device_id); - const int num_fanspeed = hm_get_fanspeed_with_device_id (opencl_ctx, device_id); - const int num_utilization = hm_get_utilization_with_device_id (opencl_ctx, device_id); - const int num_corespeed = hm_get_corespeed_with_device_id (opencl_ctx, device_id); - const int num_memoryspeed = hm_get_memoryspeed_with_device_id (opencl_ctx, device_id); - const int num_buslanes = hm_get_buslanes_with_device_id (opencl_ctx, device_id); - const int num_throttle = hm_get_throttle_with_device_id (opencl_ctx, device_id); + const int num_temperature = hm_get_temperature_with_device_id (hwmon_ctx, opencl_ctx, device_id); + const int num_fanspeed = hm_get_fanspeed_with_device_id (hwmon_ctx, opencl_ctx, device_id); + const int num_utilization = hm_get_utilization_with_device_id (hwmon_ctx, opencl_ctx, device_id); + const int num_corespeed = hm_get_corespeed_with_device_id (hwmon_ctx, opencl_ctx, device_id); + const int num_memoryspeed = hm_get_memoryspeed_with_device_id (hwmon_ctx, opencl_ctx, device_id); + const int num_buslanes = hm_get_buslanes_with_device_id (hwmon_ctx, opencl_ctx, device_id); + const int num_throttle = hm_get_throttle_with_device_id (hwmon_ctx, opencl_ctx, device_id); char output_buf[256] = { 0 }; diff --git a/src/terminal.c b/src/terminal.c index 6241e814d..a920820b8 100644 --- a/src/terminal.c +++ b/src/terminal.c @@ -71,6 +71,7 @@ void *thread_keypress (void *p) straight_ctx_t *straight_ctx = data.straight_ctx; combinator_ctx_t *combinator_ctx = data.combinator_ctx; mask_ctx_t *mask_ctx = data.mask_ctx; + hwmon_ctx_t *hwmon_ctx = data.hwmon_ctx; const bool quiet = user_options->quiet; @@ -101,7 +102,7 @@ void *thread_keypress (void *p) log_info (""); - status_display (opencl_ctx, hashconfig, hashes, restore_ctx, user_options, user_options_extra, straight_ctx, combinator_ctx, mask_ctx); + status_display (opencl_ctx, hwmon_ctx, hashconfig, hashes, restore_ctx, user_options, user_options_extra, straight_ctx, combinator_ctx, mask_ctx); log_info (""); diff --git a/src/user_options.c b/src/user_options.c index c12eee580..30846c284 100644 --- a/src/user_options.c +++ b/src/user_options.c @@ -720,6 +720,12 @@ int user_options_sanity (user_options_t *user_options, restore_ctx_t *restore_ct return -1; } + if (user_options->gpu_temp_disable == true) + { + user_options->gpu_temp_abort = 0; + user_options->gpu_temp_retain = 0; + } + if ((user_options->gpu_temp_abort != 0) && (user_options->gpu_temp_retain != 0)) { if (user_options->gpu_temp_abort < user_options->gpu_temp_retain)