From 9fc6c26f8ccc6251c8fb88cb82f4a505a8c1db43 Mon Sep 17 00:00:00 2001 From: Jens Steube Date: Sat, 10 Jul 2021 14:24:51 +0200 Subject: [PATCH] Added temperature watchdog and utilization for CPU on linux using sysfs and procfs --- docs/changes.txt | 2 +- include/ext_sysfs_cpu.h | 20 +++++++++++ src/ext_sysfs_cpu.c | 78 +++++++++++++++++++++++++++++++++++++++++ src/hwmon.c | 19 +++++++++- 4 files changed, 117 insertions(+), 2 deletions(-) diff --git a/docs/changes.txt b/docs/changes.txt index 51648b8ff..bb7495ce1 100644 --- a/docs/changes.txt +++ b/docs/changes.txt @@ -19,7 +19,7 @@ - Added option --markov-inverse to inverse markov statistics, with the idea of reversing the order of the password candidates - Added temperature watchdog and fanspeed readings for CPU and GPU on macOS using iokit -- Added temperature watchdog for CPU on linux using sysfs +- Added temperature watchdog and utilization for CPU on linux using sysfs and procfs ## ## Bugs diff --git a/include/ext_sysfs_cpu.h b/include/ext_sysfs_cpu.h index 06d0f744d..ca15f83f9 100644 --- a/include/ext_sysfs_cpu.h +++ b/include/ext_sysfs_cpu.h @@ -15,6 +15,8 @@ static const char SENSOR_K10TEMP[] = "k10temp"; static const char SENSOR_K8TEMP[] = "k8temp"; static const char SENSOR_ACPITZ[] = "acpitz"; +static const char PROC_STAT[] = "/proc/stat"; + typedef int HM_ADAPTER_SYSFS_CPU; typedef void *SYSFS_CPU_LIB; @@ -27,6 +29,21 @@ typedef struct hm_sysfs_cpu_lib } hm_sysfs_cpu_lib_t; +typedef struct +{ + unsigned long user; + unsigned long nice; + unsigned long system; + unsigned long idle; + unsigned long iowait; + unsigned long irq; + unsigned long softirq; + unsigned long steal; + unsigned long guest; + unsigned long guest_nice; + +} proc_stat_t; + typedef hm_sysfs_cpu_lib_t SYSFS_CPU_PTR; bool sysfs_cpu_init (void *hashcat_ctx); @@ -34,4 +51,7 @@ void sysfs_cpu_close (void *hashcat_ctx); char *hm_SYSFS_CPU_get_syspath_hwmon (); int hm_SYSFS_CPU_get_temperature_current (void *hashcat_ctx, int *val); +bool read_proc_stat (void *hashcat_ctx, proc_stat_t *proc_stat); +int hm_SYSFS_CPU_get_utilization_current (void *hashcat_ctx, int *val); + #endif // _EXT_SYSFS_CPU_H diff --git a/src/ext_sysfs_cpu.c b/src/ext_sysfs_cpu.c index c03507ca8..ddfd5d4d4 100644 --- a/src/ext_sysfs_cpu.c +++ b/src/ext_sysfs_cpu.c @@ -132,3 +132,81 @@ int hm_SYSFS_CPU_get_temperature_current (void *hashcat_ctx, int *val) return 0; } + +bool read_proc_stat (void *hashcat_ctx, proc_stat_t *proc_stat) +{ + FILE *fd = fopen (PROC_STAT, "r"); + + if (fd == NULL) return false; + + const int e = fscanf (fd, "cpu %lu %lu %lu %lu %lu %lu %lu %lu %lu %lu\n", + &proc_stat->user, + &proc_stat->nice, + &proc_stat->system, + &proc_stat->idle, + &proc_stat->iowait, + &proc_stat->irq, + &proc_stat->softirq, + &proc_stat->steal, + &proc_stat->guest, + &proc_stat->guest_nice); + + fclose (fd); + + if (e != 10) + { + event_log_error (hashcat_ctx, "%s: unexpected data.", PROC_STAT); + + return false; + } + + return true; +} + +int hm_SYSFS_CPU_get_utilization_current (void *hashcat_ctx, int *val) +{ + static proc_stat_t prev; + + proc_stat_t cur; + + if (read_proc_stat (hashcat_ctx, &cur) == false) return false; + + unsigned long prev_idle = prev.idle + + prev.iowait; + + unsigned long prev_load = prev.user + + prev.nice + + prev.system + + prev.irq + + prev.softirq; + + unsigned long prev_total = prev_idle + prev_load; + + + unsigned long cur_idle = cur.idle + + cur.iowait; + + unsigned long cur_load = cur.user + + cur.nice + + cur.system + + cur.irq + + cur.softirq; + + unsigned long cur_total = cur_idle + cur_load; + + memcpy (&prev, &cur, sizeof (prev)); + + unsigned long rem_total = cur_total - prev_total; + unsigned long rem_idle = cur_idle - prev_idle; + + if (rem_total) + { + const double cpu_percentage = ((double) (rem_total - rem_idle) / (double) rem_total) * 100; + + *val = (int) cpu_percentage; + + return true; + } + + return false; +} diff --git a/src/hwmon.c b/src/hwmon.c index 463e8c654..bc95f9515 100644 --- a/src/hwmon.c +++ b/src/hwmon.c @@ -750,6 +750,23 @@ int hm_get_utilization_with_devices_idx (hashcat_ctx_t *hashcat_ctx, const int b } } } + + if (backend_ctx->devices_param[backend_device_idx].opencl_device_type & CL_DEVICE_TYPE_CPU) + { + if (hwmon_ctx->hm_sysfs_cpu) + { + int utilization = 0; + + if (hm_SYSFS_CPU_get_utilization_current (hashcat_ctx, &utilization) == -1) + { + hwmon_ctx->hm_device[backend_device_idx].utilization_get_supported = false; + + return -1; + } + + return utilization; + } + } } hwmon_ctx->hm_device[backend_device_idx].utilization_get_supported = false; @@ -1482,7 +1499,7 @@ int hwmon_ctx_init (hashcat_ctx_t *hashcat_ctx) hm_adapters_sysfs_cpu[device_id].fanpolicy_get_supported = false; hm_adapters_sysfs_cpu[device_id].memoryspeed_get_supported = false; hm_adapters_sysfs_cpu[device_id].temperature_get_supported = true; - hm_adapters_sysfs_cpu[device_id].utilization_get_supported = false; + hm_adapters_sysfs_cpu[device_id].utilization_get_supported = true; } } }