From dcdd0d89cd9734033a994c5b26cd62a4b0de2334 Mon Sep 17 00:00:00 2001 From: jsteube Date: Thu, 15 Sep 2016 04:29:22 +0200 Subject: [PATCH] Move thread_monitor() to monitor.c --- include/dispatch.h | 12 + include/monitor.h | 11 + src/Makefile | 2 +- src/dispatch.c | 536 +++++++++++++++++++++++++++++++++++++++++++++ src/hashcat.c | 357 +----------------------------- src/monitor.c | 374 +++++++++++++++++++++++++++++++ 6 files changed, 935 insertions(+), 357 deletions(-) create mode 100644 include/dispatch.h create mode 100644 include/monitor.h create mode 100644 src/dispatch.c create mode 100644 src/monitor.c diff --git a/include/dispatch.h b/include/dispatch.h new file mode 100644 index 000000000..05ef1efbc --- /dev/null +++ b/include/dispatch.h @@ -0,0 +1,12 @@ +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#ifndef _DISPATCH_H +#define _DISPATCH_H + +void *thread_calc_stdin (void *p); +void *thread_calc (void *p); + +#endif // _DISPATCH_H diff --git a/include/monitor.h b/include/monitor.h new file mode 100644 index 000000000..ac0d67acb --- /dev/null +++ b/include/monitor.h @@ -0,0 +1,11 @@ +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#ifndef _MONITOR_H +#define _MONITOR_H + +void *thread_monitor (void *p); + +#endif // _MONITOR_H diff --git a/src/Makefile b/src/Makefile index 04b3e4717..25131a4d3 100644 --- a/src/Makefile +++ b/src/Makefile @@ -163,7 +163,7 @@ LFLAGS_CROSS_WIN := -lpsapi ## Objects ## -OBJS_ALL := affinity attack_mode autotune benchmark bitmap bitops common convert cpu_aes cpu_crc32 cpu_des cpu_md5 cpu_sha1 cpu_sha256 data debugfile dictstat dispatch dynloader ext_ADL ext_nvapi ext_nvml ext_OpenCL ext_xnvctrl filehandling filenames folder hash_management hlfmt hwmon induct interface locking logfile logging loopback memory mpsp opencl outfile_check outfile potfile powertune remove restore rp_cpu rp_kernel_on_cpu runtime shared status stdout terminal thread timer tuningdb usage version weak_hash wordlist +OBJS_ALL := affinity attack_mode autotune benchmark bitmap bitops common convert cpu_aes cpu_crc32 cpu_des cpu_md5 cpu_sha1 cpu_sha256 data debugfile dictstat dispatch dynloader ext_ADL ext_nvapi ext_nvml ext_OpenCL ext_xnvctrl filehandling filenames folder hash_management hlfmt hwmon induct interface locking logfile logging loopback memory monitor mpsp opencl outfile_check outfile potfile powertune remove restore rp_cpu rp_kernel_on_cpu runtime shared status stdout terminal thread timer tuningdb usage version weak_hash wordlist NATIVE_OBJS := $(foreach OBJ,$(OBJS_ALL),obj/$(OBJ).NATIVE.o) diff --git a/src/dispatch.c b/src/dispatch.c new file mode 100644 index 000000000..0426a05ac --- /dev/null +++ b/src/dispatch.c @@ -0,0 +1,536 @@ +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#include "common.h" +#include "types_int.h" +#include "types.h" +#include "memory.h" +#include "filehandling.h" +#include "interface.h" +#include "timer.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 "opencl.h" +#include "thread.h" +#include "rp_cpu.h" +#include "terminal.h" +#include "hwmon.h" +#include "mpsp.h" +#include "restore.h" +#include "outfile.h" +#include "potfile.h" +#include "debugfile.h" +#include "loopback.h" +#include "status.h" +#include "dictstat.h" +#include "wordlist.h" +#include "data.h" +#include "status.h" +#include "shared.h" +#include "dispatch.h" + +extern hc_global_data_t data; + +extern hc_thread_mutex_t mux_counter; + +hc_thread_mutex_t mux_dispatcher; + +static void set_kernel_power_final (const u64 kernel_power_final) +{ + if (data.quiet == 0) + { + clear_prompt (); + + //log_info (""); + + log_info ("INFO: approaching final keyspace, workload adjusted"); + log_info (""); + + send_prompt (); + } + + data.kernel_power_final = kernel_power_final; +} + +static u32 get_power (hc_device_param_t *device_param) +{ + const u64 kernel_power_final = data.kernel_power_final; + + if (kernel_power_final) + { + const double device_factor = (double) device_param->hardware_power / data.hardware_power_all; + + const u64 words_left_device = (u64) CEIL (kernel_power_final * device_factor); + + // work should be at least the hardware power available without any accelerator + + const u64 work = MAX (words_left_device, device_param->hardware_power); + + return work; + } + + return device_param->kernel_power; +} + +static uint get_work (hc_device_param_t *device_param, const u64 max) +{ + hc_thread_mutex_lock (mux_dispatcher); + + const u64 words_cur = data.words_cur; + const u64 words_base = (data.limit == 0) ? data.words_base : MIN (data.limit, data.words_base); + + device_param->words_off = words_cur; + + const u64 kernel_power_all = data.kernel_power_all; + + const u64 words_left = words_base - words_cur; + + if (words_left < kernel_power_all) + { + if (data.kernel_power_final == 0) + { + set_kernel_power_final (words_left); + } + } + + const u32 kernel_power = get_power (device_param); + + uint work = MIN (words_left, kernel_power); + + work = MIN (work, max); + + data.words_cur += work; + + hc_thread_mutex_unlock (mux_dispatcher); + + return work; +} + +void *thread_calc_stdin (void *p) +{ + hc_device_param_t *device_param = (hc_device_param_t *) p; + + if (device_param->skipped) return NULL; + + hashconfig_t *hashconfig = data.hashconfig; + + char *buf = (char *) mymalloc (HCBUFSIZ_LARGE); + + const uint attack_kern = data.attack_kern; + + while ((data.devices_status != STATUS_CRACKED) && (data.devices_status != STATUS_ABORTED) && (data.devices_status != STATUS_QUIT)) + { + hc_thread_mutex_lock (mux_dispatcher); + + if (feof (stdin) != 0) + { + hc_thread_mutex_unlock (mux_dispatcher); + + break; + } + + uint words_cur = 0; + + while (words_cur < device_param->kernel_power) + { + char *line_buf = fgets (buf, HCBUFSIZ_LARGE - 1, stdin); + + if (line_buf == NULL) break; + + uint line_len = in_superchop (line_buf); + + line_len = convert_from_hex (line_buf, line_len); + + // post-process rule engine + + if (run_rule_engine (data.rule_len_l, data.rule_buf_l)) + { + char rule_buf_out[BLOCK_SIZE] = { 0 }; + + int rule_len_out = -1; + + if (line_len < BLOCK_SIZE) + { + rule_len_out = _old_apply_rule (data.rule_buf_l, data.rule_len_l, line_buf, line_len, rule_buf_out); + } + + if (rule_len_out < 0) continue; + + line_buf = rule_buf_out; + line_len = rule_len_out; + } + + if (line_len > PW_MAX) + { + continue; + } + + // hmm that's always the case, or? + + if (attack_kern == ATTACK_KERN_STRAIGHT) + { + if ((line_len < data.pw_min) || (line_len > data.pw_max)) + { + hc_thread_mutex_lock (mux_counter); + + for (uint salt_pos = 0; salt_pos < data.salts_cnt; salt_pos++) + { + data.words_progress_rejected[salt_pos] += data.kernel_rules_cnt; + } + + hc_thread_mutex_unlock (mux_counter); + + continue; + } + } + + pw_add (device_param, (u8 *) line_buf, line_len); + + words_cur++; + + if (data.devices_status == STATUS_CRACKED) break; + if (data.devices_status == STATUS_ABORTED) break; + if (data.devices_status == STATUS_QUIT) break; + if (data.devices_status == STATUS_BYPASS) break; + } + + hc_thread_mutex_unlock (mux_dispatcher); + + if (data.devices_status == STATUS_CRACKED) break; + if (data.devices_status == STATUS_ABORTED) break; + if (data.devices_status == STATUS_QUIT) break; + if (data.devices_status == STATUS_BYPASS) break; + + // flush + + const uint pws_cnt = device_param->pws_cnt; + + if (pws_cnt) + { + run_copy (device_param, hashconfig, pws_cnt); + + run_cracker (device_param, hashconfig, pws_cnt); + + device_param->pws_cnt = 0; + + /* + still required? + if (attack_kern == ATTACK_KERN_STRAIGHT) + { + run_kernel_bzero (device_param, device_param->d_rules_c, device_param->size_rules_c); + } + else if (attack_kern == ATTACK_KERN_COMBI) + { + run_kernel_bzero (device_param, device_param->d_combs_c, device_param->size_combs); + } + */ + } + } + + device_param->kernel_accel = 0; + device_param->kernel_loops = 0; + + myfree (buf); + + return NULL; +} + +void *thread_calc (void *p) +{ + hc_device_param_t *device_param = (hc_device_param_t *) p; + + if (device_param->skipped) return NULL; + + hashconfig_t *hashconfig = data.hashconfig; + + const uint attack_mode = data.attack_mode; + const uint attack_kern = data.attack_kern; + + if (attack_mode == ATTACK_MODE_BF) + { + while ((data.devices_status != STATUS_CRACKED) && (data.devices_status != STATUS_ABORTED) && (data.devices_status != STATUS_QUIT)) + { + const uint work = get_work (device_param, -1u); + + if (work == 0) break; + + const u64 words_off = device_param->words_off; + const u64 words_fin = words_off + work; + + const uint pws_cnt = work; + + device_param->pws_cnt = pws_cnt; + + if (pws_cnt) + { + run_copy (device_param, hashconfig, pws_cnt); + + run_cracker (device_param, hashconfig, pws_cnt); + + device_param->pws_cnt = 0; + + /* + still required? + run_kernel_bzero (device_param, device_param->d_bfs_c, device_param->size_bfs); + */ + } + + if (data.devices_status == STATUS_STOP_AT_CHECKPOINT) check_checkpoint (); + + if (data.devices_status == STATUS_CRACKED) break; + if (data.devices_status == STATUS_ABORTED) break; + if (data.devices_status == STATUS_QUIT) break; + if (data.devices_status == STATUS_BYPASS) break; + + if (data.benchmark == 1) break; + + device_param->words_done = words_fin; + } + } + else + { + const uint segment_size = data.segment_size; + + char *dictfile = data.dictfile; + + if (attack_mode == ATTACK_MODE_COMBI) + { + if (data.combs_mode == COMBINATOR_MODE_BASE_RIGHT) + { + dictfile = data.dictfile2; + } + } + + FILE *fd = fopen (dictfile, "rb"); + + if (fd == NULL) + { + log_error ("ERROR: %s: %s", dictfile, strerror (errno)); + + return NULL; + } + + if (attack_mode == ATTACK_MODE_COMBI) + { + const uint combs_mode = data.combs_mode; + + if (combs_mode == COMBINATOR_MODE_BASE_LEFT) + { + const char *dictfilec = data.dictfile2; + + FILE *combs_fp = fopen (dictfilec, "rb"); + + if (combs_fp == NULL) + { + log_error ("ERROR: %s: %s", dictfilec, strerror (errno)); + + fclose (fd); + + return NULL; + } + + device_param->combs_fp = combs_fp; + } + else if (combs_mode == COMBINATOR_MODE_BASE_RIGHT) + { + const char *dictfilec = data.dictfile; + + FILE *combs_fp = fopen (dictfilec, "rb"); + + if (combs_fp == NULL) + { + log_error ("ERROR: %s: %s", dictfilec, strerror (errno)); + + fclose (fd); + + return NULL; + } + + device_param->combs_fp = combs_fp; + } + } + + wl_data_t *wl_data = (wl_data_t *) mymalloc (sizeof (wl_data_t)); + + wl_data->buf = (char *) mymalloc (segment_size); + wl_data->avail = segment_size; + wl_data->incr = segment_size; + wl_data->cnt = 0; + wl_data->pos = 0; + + u64 words_cur = 0; + + while ((data.devices_status != STATUS_CRACKED) && (data.devices_status != STATUS_ABORTED) && (data.devices_status != STATUS_QUIT)) + { + u64 words_off = 0; + u64 words_fin = 0; + + u64 max = -1llu; + + while (max) + { + const uint work = get_work (device_param, max); + + if (work == 0) break; + + max = 0; + + words_off = device_param->words_off; + words_fin = words_off + work; + + char *line_buf; + uint line_len; + + for ( ; words_cur < words_off; words_cur++) get_next_word (wl_data, fd, &line_buf, &line_len); + + for ( ; words_cur < words_fin; words_cur++) + { + get_next_word (wl_data, fd, &line_buf, &line_len); + + line_len = convert_from_hex (line_buf, line_len); + + // post-process rule engine + + if (run_rule_engine (data.rule_len_l, data.rule_buf_l)) + { + char rule_buf_out[BLOCK_SIZE] = { 0 }; + + int rule_len_out = -1; + + if (line_len < BLOCK_SIZE) + { + rule_len_out = _old_apply_rule (data.rule_buf_l, data.rule_len_l, line_buf, line_len, rule_buf_out); + } + + if (rule_len_out < 0) continue; + + line_buf = rule_buf_out; + line_len = rule_len_out; + } + + if (attack_kern == ATTACK_KERN_STRAIGHT) + { + if ((line_len < data.pw_min) || (line_len > data.pw_max)) + { + max++; + + hc_thread_mutex_lock (mux_counter); + + for (uint salt_pos = 0; salt_pos < data.salts_cnt; salt_pos++) + { + data.words_progress_rejected[salt_pos] += data.kernel_rules_cnt; + } + + hc_thread_mutex_unlock (mux_counter); + + continue; + } + } + else if (attack_kern == ATTACK_KERN_COMBI) + { + // do not check if minimum restriction is satisfied (line_len >= data.pw_min) here + // since we still need to combine the plains + + if (line_len > data.pw_max) + { + max++; + + hc_thread_mutex_lock (mux_counter); + + for (uint salt_pos = 0; salt_pos < data.salts_cnt; salt_pos++) + { + data.words_progress_rejected[salt_pos] += data.combs_cnt; + } + + hc_thread_mutex_unlock (mux_counter); + + continue; + } + } + + pw_add (device_param, (u8 *) line_buf, line_len); + + if (data.devices_status == STATUS_STOP_AT_CHECKPOINT) check_checkpoint (); + + if (data.devices_status == STATUS_CRACKED) break; + if (data.devices_status == STATUS_ABORTED) break; + if (data.devices_status == STATUS_QUIT) break; + if (data.devices_status == STATUS_BYPASS) break; + } + + if (data.devices_status == STATUS_STOP_AT_CHECKPOINT) check_checkpoint (); + + if (data.devices_status == STATUS_CRACKED) break; + if (data.devices_status == STATUS_ABORTED) break; + if (data.devices_status == STATUS_QUIT) break; + if (data.devices_status == STATUS_BYPASS) break; + } + + if (data.devices_status == STATUS_STOP_AT_CHECKPOINT) check_checkpoint (); + + if (data.devices_status == STATUS_CRACKED) break; + if (data.devices_status == STATUS_ABORTED) break; + if (data.devices_status == STATUS_QUIT) break; + if (data.devices_status == STATUS_BYPASS) break; + + // + // flush + // + + const uint pws_cnt = device_param->pws_cnt; + + if (pws_cnt) + { + run_copy (device_param, hashconfig, pws_cnt); + + run_cracker (device_param, hashconfig, pws_cnt); + + device_param->pws_cnt = 0; + + /* + still required? + if (attack_kern == ATTACK_KERN_STRAIGHT) + { + run_kernel_bzero (device_param, device_param->d_rules_c, device_param->size_rules_c); + } + else if (attack_kern == ATTACK_KERN_COMBI) + { + run_kernel_bzero (device_param, device_param->d_combs_c, device_param->size_combs); + } + */ + } + + if (data.devices_status == STATUS_STOP_AT_CHECKPOINT) check_checkpoint (); + + if (data.devices_status == STATUS_CRACKED) break; + if (data.devices_status == STATUS_ABORTED) break; + if (data.devices_status == STATUS_QUIT) break; + if (data.devices_status == STATUS_BYPASS) break; + + if (words_fin == 0) break; + + device_param->words_done = words_fin; + } + + if (attack_mode == ATTACK_MODE_COMBI) + { + fclose (device_param->combs_fp); + } + + free (wl_data->buf); + free (wl_data); + + fclose (fd); + } + + device_param->kernel_accel = 0; + device_param->kernel_loops = 0; + + return NULL; +} diff --git a/src/hashcat.c b/src/hashcat.c index c54bf5c24..bc4067c0e 100644 --- a/src/hashcat.c +++ b/src/hashcat.c @@ -84,6 +84,7 @@ #include "autotune.h" #include "induct.h" #include "dispatch.h" +#include "monitor.h" extern hc_global_data_t data; @@ -108,362 +109,6 @@ const int comptime = COMPTIME; #define FORCE 0 - - - - - - - - -static void *thread_monitor (void *p) -{ - uint runtime_check = 0; - uint remove_check = 0; - uint status_check = 0; - uint restore_check = 0; - - uint restore_left = data.restore_timer; - uint remove_left = data.remove_timer; - uint status_left = data.status_timer; - - #if defined (HAVE_HWMON) - uint hwmon_check = 0; - - int slowdown_warnings = 0; - - // these variables are mainly used for fan control - - int *fan_speed_chgd = (int *) mycalloc (data.devices_cnt, sizeof (int)); - - // temperature controller "loopback" values - - int *temp_diff_old = (int *) mycalloc (data.devices_cnt, sizeof (int)); - int *temp_diff_sum = (int *) mycalloc (data.devices_cnt, sizeof (int)); - - int temp_threshold = 1; // degrees celcius - - int fan_speed_min = 15; // in percentage - int fan_speed_max = 100; - - time_t last_temp_check_time; - #endif // HAVE_HWMON - - uint sleep_time = 1; - - if (data.runtime) - { - runtime_check = 1; - } - - if (data.restore_timer) - { - restore_check = 1; - } - - if ((data.remove == 1) && (data.hashlist_mode == HL_MODE_FILE)) - { - remove_check = 1; - } - - if (data.status == 1) - { - status_check = 1; - } - - #if defined (HAVE_HWMON) - if (data.gpu_temp_disable == 0) - { - time (&last_temp_check_time); - - hwmon_check = 1; - } - #endif - - if ((runtime_check == 0) && (remove_check == 0) && (status_check == 0) && (restore_check == 0)) - { - #if defined (HAVE_HWMON) - if (hwmon_check == 0) - #endif - return (p); - } - - while (data.shutdown_inner == 0) - { - hc_sleep (sleep_time); - - if (data.devices_status != STATUS_RUNNING) continue; - - #if defined (HAVE_HWMON) - - if (hwmon_check == 1) - { - hc_thread_mutex_lock (mux_hwmon); - - for (uint device_id = 0; device_id < data.devices_cnt; device_id++) - { - hc_device_param_t *device_param = &data.devices_param[device_id]; - - if (device_param->skipped) continue; - - if (device_param->device_vendor_id == VENDOR_ID_NV) - { - if (data.hm_nvapi) - { - NV_GPU_PERF_POLICIES_INFO_PARAMS_V1 perfPolicies_info; - NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 perfPolicies_status; - - memset (&perfPolicies_info, 0, sizeof (NV_GPU_PERF_POLICIES_INFO_PARAMS_V1)); - memset (&perfPolicies_status, 0, sizeof (NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1)); - - 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); - - perfPolicies_status.info_value = perfPolicies_info.info_value; - - hm_NvAPI_GPU_GetPerfPoliciesStatus (data.hm_nvapi, data.hm_device[device_id].nvapi, &perfPolicies_status); - - if (perfPolicies_status.throttle & 2) - { - if (slowdown_warnings < 3) - { - if (data.quiet == 0) clear_prompt (); - - log_info ("WARNING: Drivers temperature threshold hit on GPU #%d, expect performance to drop...", device_id + 1); - - if (slowdown_warnings == 2) - { - log_info (""); - } - - if (data.quiet == 0) send_prompt (); - - slowdown_warnings++; - } - } - else - { - slowdown_warnings = 0; - } - } - } - } - - hc_thread_mutex_unlock (mux_hwmon); - } - - if (hwmon_check == 1) - { - hc_thread_mutex_lock (mux_hwmon); - - time_t temp_check_time; - - time (&temp_check_time); - - uint Ta = temp_check_time - last_temp_check_time; // set Ta = sleep_time; is not good enough (see --remove etc) - - if (Ta == 0) Ta = 1; - - for (uint device_id = 0; device_id < data.devices_cnt; device_id++) - { - hc_device_param_t *device_param = &data.devices_param[device_id]; - - if (device_param->skipped) continue; - - if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) continue; - - const int temperature = hm_get_temperature_with_device_id (device_id); - - if (temperature > (int) data.gpu_temp_abort) - { - log_error ("ERROR: Temperature limit on GPU %d reached, aborting...", device_id + 1); - - if (data.devices_status != STATUS_QUIT) myabort (); - - break; - } - - const int gpu_temp_retain = data.gpu_temp_retain; - - if (gpu_temp_retain) - { - if (data.hm_device[device_id].fan_set_supported == 1) - { - int temp_cur = temperature; - - int temp_diff_new = gpu_temp_retain - temp_cur; - - temp_diff_sum[device_id] = temp_diff_sum[device_id] + temp_diff_new; - - // calculate Ta value (time difference in seconds between the last check and this check) - - last_temp_check_time = temp_check_time; - - float Kp = 1.8f; - float Ki = 0.005f; - float Kd = 6; - - // PID controller (3-term controller: proportional - Kp, integral - Ki, derivative - Kd) - - int fan_diff_required = (int) (Kp * (float)temp_diff_new + Ki * Ta * (float)temp_diff_sum[device_id] + Kd * ((float)(temp_diff_new - temp_diff_old[device_id])) / Ta); - - if (abs (fan_diff_required) >= temp_threshold) - { - const int fan_speed_cur = hm_get_fanspeed_with_device_id (device_id); - - int fan_speed_level = fan_speed_cur; - - if (fan_speed_chgd[device_id] == 0) fan_speed_level = temp_cur; - - int fan_speed_new = fan_speed_level - fan_diff_required; - - if (fan_speed_new > fan_speed_max) fan_speed_new = fan_speed_max; - if (fan_speed_new < fan_speed_min) fan_speed_new = fan_speed_min; - - if (fan_speed_new != fan_speed_cur) - { - int freely_change_fan_speed = (fan_speed_chgd[device_id] == 1); - int fan_speed_must_change = (fan_speed_new > fan_speed_cur); - - if ((freely_change_fan_speed == 1) || (fan_speed_must_change == 1)) - { - if (device_param->device_vendor_id == VENDOR_ID_AMD) - { - hm_set_fanspeed_with_device_id_adl (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); - #endif - - #if defined (__linux__) - hm_set_fanspeed_with_device_id_xnvctrl (device_id, fan_speed_new); - #endif - } - - fan_speed_chgd[device_id] = 1; - } - - temp_diff_old[device_id] = temp_diff_new; - } - } - } - } - } - - hc_thread_mutex_unlock (mux_hwmon); - } - #endif // HAVE_HWMON - - if (restore_check == 1) - { - restore_left--; - - if (restore_left == 0) - { - if (data.restore_disable == 0) cycle_restore (); - - restore_left = data.restore_timer; - } - } - - if ((runtime_check == 1) && (data.runtime_start > 0)) - { - double ms_paused = data.ms_paused; - - if (data.devices_status == STATUS_PAUSED) - { - double ms_paused_tmp = 0; - - hc_timer_get (data.timer_paused, ms_paused_tmp); - - ms_paused += ms_paused_tmp; - } - - time_t runtime_cur; - - time (&runtime_cur); - - int runtime_left = data.proc_start + data.runtime + data.prepare_time + (ms_paused / 1000) - runtime_cur; - - if (runtime_left <= 0) - { - if (data.benchmark == 0) - { - if (data.quiet == 0) log_info ("\nNOTE: Runtime limit reached, aborting...\n"); - } - - if (data.devices_status != STATUS_QUIT) myabort (); - } - } - - if (remove_check == 1) - { - remove_left--; - - if (remove_left == 0) - { - if (data.digests_saved != data.digests_done) - { - data.digests_saved = data.digests_done; - - save_hash (); - } - - remove_left = data.remove_timer; - } - } - - if (status_check == 1) - { - status_left--; - - if (status_left == 0) - { - hc_thread_mutex_lock (mux_display); - - if (data.quiet == 0) clear_prompt (); - - if (data.quiet == 0) log_info (""); - - status_display (); - - if (data.quiet == 0) log_info (""); - - hc_thread_mutex_unlock (mux_display); - - status_left = data.status_timer; - } - } - } - - #if defined (HAVE_HWMON) - myfree (fan_speed_chgd); - - myfree (temp_diff_old); - myfree (temp_diff_sum); - #endif - - p = NULL; - - return (p); -} - - - - - - - - - - - - - int main (int argc, char **argv) { #if defined (_WIN) diff --git a/src/monitor.c b/src/monitor.c new file mode 100644 index 000000000..93668d049 --- /dev/null +++ b/src/monitor.c @@ -0,0 +1,374 @@ +/** + * Author......: See docs/credits.txt + * License.....: MIT + */ + +#include "common.h" +#include "types_int.h" +#include "types.h" +#include "logging.h" +#include "memory.h" +#include "interface.h" +#include "timer.h" +#include "ext_OpenCL.h" +#include "ext_ADL.h" +#include "ext_nvapi.h" +#include "ext_nvml.h" +#include "ext_xnvctrl.h" +#include "hwmon.h" +#include "mpsp.h" +#include "rp_cpu.h" +#include "restore.h" +#include "opencl.h" +#include "outfile.h" +#include "potfile.h" +#include "debugfile.h" +#include "loopback.h" +#include "data.h" +#include "status.h" +#include "shared.h" +#include "terminal.h" +#include "hwmon.h" +#include "thread.h" +#include "monitor.h" + +extern hc_global_data_t data; + +extern hc_thread_mutex_t mux_display; +extern hc_thread_mutex_t mux_hwmon; + +void *thread_monitor (void *p) +{ + uint runtime_check = 0; + uint remove_check = 0; + uint status_check = 0; + uint restore_check = 0; + + uint restore_left = data.restore_timer; + uint remove_left = data.remove_timer; + uint status_left = data.status_timer; + + #if defined (HAVE_HWMON) + uint hwmon_check = 0; + + int slowdown_warnings = 0; + + // these variables are mainly used for fan control + + int *fan_speed_chgd = (int *) mycalloc (data.devices_cnt, sizeof (int)); + + // temperature controller "loopback" values + + int *temp_diff_old = (int *) mycalloc (data.devices_cnt, sizeof (int)); + int *temp_diff_sum = (int *) mycalloc (data.devices_cnt, sizeof (int)); + + int temp_threshold = 1; // degrees celcius + + int fan_speed_min = 15; // in percentage + int fan_speed_max = 100; + + time_t last_temp_check_time; + #endif // HAVE_HWMON + + uint sleep_time = 1; + + if (data.runtime) + { + runtime_check = 1; + } + + if (data.restore_timer) + { + restore_check = 1; + } + + if ((data.remove == 1) && (data.hashlist_mode == HL_MODE_FILE)) + { + remove_check = 1; + } + + if (data.status == 1) + { + status_check = 1; + } + + #if defined (HAVE_HWMON) + if (data.gpu_temp_disable == 0) + { + time (&last_temp_check_time); + + hwmon_check = 1; + } + #endif + + if ((runtime_check == 0) && (remove_check == 0) && (status_check == 0) && (restore_check == 0)) + { + #if defined (HAVE_HWMON) + if (hwmon_check == 0) + #endif + return (p); + } + + while (data.shutdown_inner == 0) + { + hc_sleep (sleep_time); + + if (data.devices_status != STATUS_RUNNING) continue; + + #if defined (HAVE_HWMON) + + if (hwmon_check == 1) + { + hc_thread_mutex_lock (mux_hwmon); + + for (uint device_id = 0; device_id < data.devices_cnt; device_id++) + { + hc_device_param_t *device_param = &data.devices_param[device_id]; + + if (device_param->skipped) continue; + + if (device_param->device_vendor_id == VENDOR_ID_NV) + { + if (data.hm_nvapi) + { + NV_GPU_PERF_POLICIES_INFO_PARAMS_V1 perfPolicies_info; + NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1 perfPolicies_status; + + memset (&perfPolicies_info, 0, sizeof (NV_GPU_PERF_POLICIES_INFO_PARAMS_V1)); + memset (&perfPolicies_status, 0, sizeof (NV_GPU_PERF_POLICIES_STATUS_PARAMS_V1)); + + 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); + + perfPolicies_status.info_value = perfPolicies_info.info_value; + + hm_NvAPI_GPU_GetPerfPoliciesStatus (data.hm_nvapi, data.hm_device[device_id].nvapi, &perfPolicies_status); + + if (perfPolicies_status.throttle & 2) + { + if (slowdown_warnings < 3) + { + if (data.quiet == 0) clear_prompt (); + + log_info ("WARNING: Drivers temperature threshold hit on GPU #%d, expect performance to drop...", device_id + 1); + + if (slowdown_warnings == 2) + { + log_info (""); + } + + if (data.quiet == 0) send_prompt (); + + slowdown_warnings++; + } + } + else + { + slowdown_warnings = 0; + } + } + } + } + + hc_thread_mutex_unlock (mux_hwmon); + } + + if (hwmon_check == 1) + { + hc_thread_mutex_lock (mux_hwmon); + + time_t temp_check_time; + + time (&temp_check_time); + + uint Ta = temp_check_time - last_temp_check_time; // set Ta = sleep_time; is not good enough (see --remove etc) + + if (Ta == 0) Ta = 1; + + for (uint device_id = 0; device_id < data.devices_cnt; device_id++) + { + hc_device_param_t *device_param = &data.devices_param[device_id]; + + if (device_param->skipped) continue; + + if ((data.devices_param[device_id].device_type & CL_DEVICE_TYPE_GPU) == 0) continue; + + const int temperature = hm_get_temperature_with_device_id (device_id); + + if (temperature > (int) data.gpu_temp_abort) + { + log_error ("ERROR: Temperature limit on GPU %d reached, aborting...", device_id + 1); + + if (data.devices_status != STATUS_QUIT) myabort (); + + break; + } + + const int gpu_temp_retain = data.gpu_temp_retain; + + if (gpu_temp_retain) + { + if (data.hm_device[device_id].fan_set_supported == 1) + { + int temp_cur = temperature; + + int temp_diff_new = gpu_temp_retain - temp_cur; + + temp_diff_sum[device_id] = temp_diff_sum[device_id] + temp_diff_new; + + // calculate Ta value (time difference in seconds between the last check and this check) + + last_temp_check_time = temp_check_time; + + float Kp = 1.8f; + float Ki = 0.005f; + float Kd = 6; + + // PID controller (3-term controller: proportional - Kp, integral - Ki, derivative - Kd) + + int fan_diff_required = (int) (Kp * (float)temp_diff_new + Ki * Ta * (float)temp_diff_sum[device_id] + Kd * ((float)(temp_diff_new - temp_diff_old[device_id])) / Ta); + + if (abs (fan_diff_required) >= temp_threshold) + { + const int fan_speed_cur = hm_get_fanspeed_with_device_id (device_id); + + int fan_speed_level = fan_speed_cur; + + if (fan_speed_chgd[device_id] == 0) fan_speed_level = temp_cur; + + int fan_speed_new = fan_speed_level - fan_diff_required; + + if (fan_speed_new > fan_speed_max) fan_speed_new = fan_speed_max; + if (fan_speed_new < fan_speed_min) fan_speed_new = fan_speed_min; + + if (fan_speed_new != fan_speed_cur) + { + int freely_change_fan_speed = (fan_speed_chgd[device_id] == 1); + int fan_speed_must_change = (fan_speed_new > fan_speed_cur); + + if ((freely_change_fan_speed == 1) || (fan_speed_must_change == 1)) + { + if (device_param->device_vendor_id == VENDOR_ID_AMD) + { + hm_set_fanspeed_with_device_id_adl (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); + #endif + + #if defined (__linux__) + hm_set_fanspeed_with_device_id_xnvctrl (device_id, fan_speed_new); + #endif + } + + fan_speed_chgd[device_id] = 1; + } + + temp_diff_old[device_id] = temp_diff_new; + } + } + } + } + } + + hc_thread_mutex_unlock (mux_hwmon); + } + #endif // HAVE_HWMON + + if (restore_check == 1) + { + restore_left--; + + if (restore_left == 0) + { + if (data.restore_disable == 0) cycle_restore (); + + restore_left = data.restore_timer; + } + } + + if ((runtime_check == 1) && (data.runtime_start > 0)) + { + double ms_paused = data.ms_paused; + + if (data.devices_status == STATUS_PAUSED) + { + double ms_paused_tmp = 0; + + hc_timer_get (data.timer_paused, ms_paused_tmp); + + ms_paused += ms_paused_tmp; + } + + time_t runtime_cur; + + time (&runtime_cur); + + int runtime_left = data.proc_start + data.runtime + data.prepare_time + (ms_paused / 1000) - runtime_cur; + + if (runtime_left <= 0) + { + if (data.benchmark == 0) + { + if (data.quiet == 0) log_info ("\nNOTE: Runtime limit reached, aborting...\n"); + } + + if (data.devices_status != STATUS_QUIT) myabort (); + } + } + + if (remove_check == 1) + { + remove_left--; + + if (remove_left == 0) + { + if (data.digests_saved != data.digests_done) + { + data.digests_saved = data.digests_done; + + save_hash (); + } + + remove_left = data.remove_timer; + } + } + + if (status_check == 1) + { + status_left--; + + if (status_left == 0) + { + hc_thread_mutex_lock (mux_display); + + if (data.quiet == 0) clear_prompt (); + + if (data.quiet == 0) log_info (""); + + status_display (); + + if (data.quiet == 0) log_info (""); + + hc_thread_mutex_unlock (mux_display); + + status_left = data.status_timer; + } + } + } + + #if defined (HAVE_HWMON) + myfree (fan_speed_chgd); + + myfree (temp_diff_old); + myfree (temp_diff_sum); + #endif + + p = NULL; + + return (p); +}