From 841de74dbf38c245f3e7a73b3a89150e1b522aa4 Mon Sep 17 00:00:00 2001 From: WantClue Date: Mon, 19 Aug 2024 15:36:50 +0200 Subject: [PATCH] Revert "Merge pull request #293 from shufps/hashrate" This reverts commit 80e72cc87443b22ba5920f775a97da69621b5975, reversing changes made to 064b89207fe8fa90058609e8f497ce5f1526884e. --- main/global_state.h | 7 ++-- main/system.c | 79 ++++++++++++++--------------------- main/tasks/asic_result_task.c | 11 +++-- 3 files changed, 41 insertions(+), 56 deletions(-) diff --git a/main/global_state.h b/main/global_state.h index e551eedff..479455378 100644 --- a/main/global_state.h +++ b/main/global_state.h @@ -15,8 +15,7 @@ #define STRATUM_USER CONFIG_STRATUM_USER -// best is power of 2 for ring buffers -#define HISTORY_LENGTH 512 +#define HISTORY_LENGTH 100 #define DIFF_STRING_SIZE 10 typedef enum @@ -46,9 +45,11 @@ typedef struct typedef struct { + double duration_start; int historical_hashrate_rolling_index; - uint64_t historical_hashrate_time_stamps[HISTORY_LENGTH]; + double historical_hashrate_time_stamps[HISTORY_LENGTH]; double historical_hashrate[HISTORY_LENGTH]; + int historical_hashrate_init; double current_hashrate; int64_t start_time; uint64_t shares_accepted; diff --git a/main/system.c b/main/system.c index c49c5bf93..d46a54a52 100644 --- a/main/system.c +++ b/main/system.c @@ -60,10 +60,9 @@ static void _init_system(GlobalState * GLOBAL_STATE) { SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; + module->duration_start = 0; module->historical_hashrate_rolling_index = 0; - memset(module->historical_hashrate, 0, sizeof(module->historical_hashrate)); - memset(module->historical_hashrate_time_stamps, 0, sizeof(module->historical_hashrate_time_stamps)); - + module->historical_hashrate_init = 0; module->current_hashrate = 0; module->screen_page = 0; module->shares_accepted = 0; @@ -74,7 +73,7 @@ static void _init_system(GlobalState * GLOBAL_STATE) module->lastClockSync = 0; module->FOUND_BLOCK = false; module->startup_done = false; - + // set the pool url module->pool_url = nvs_config_get_string(NVS_CONFIG_STRATUM_URL, CONFIG_STRATUM_URL); @@ -148,7 +147,7 @@ void SYSTEM_update_overheat_mode(GlobalState * GLOBAL_STATE) { SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; uint16_t new_overheat_mode = nvs_config_get_u16(NVS_CONFIG_OVERHEAT_MODE, 0); - + if (new_overheat_mode != module->overheat_mode) { module->overheat_mode = new_overheat_mode; ESP_LOGI(TAG, "Overheat mode updated to: %d", module->overheat_mode); @@ -192,7 +191,7 @@ static void _update_hashrate(GlobalState * GLOBAL_STATE) float efficiency = GLOBAL_STATE->POWER_MANAGEMENT_MODULE.power / (module->current_hashrate / 1000.0); OLED_clearLine(0); memset(module->oled_buf, 0, 20); - snprintf(module->oled_buf, 20, "Gh: %.1f J/Th: %.1f", + snprintf(module->oled_buf, 20, "Gh%s: %.1f J/Th: %.1f", module->historical_hashrate_init < HISTORY_LENGTH ? "*" : "", module->current_hashrate, efficiency); OLED_writeString(0, 0, module->oled_buf); break; @@ -355,7 +354,7 @@ static void _update_connection(GlobalState * GLOBAL_STATE) strncpy(module->oled_buf, module->ssid, sizeof(module->oled_buf)); module->oled_buf[sizeof(module->oled_buf) - 1] = 0; OLED_writeString(0, 1, module->oled_buf); - + memset(module->oled_buf, 0, 20); snprintf(module->oled_buf, 20, "Configuration SSID:"); OLED_writeString(0, 2, module->oled_buf); @@ -578,7 +577,7 @@ void SYSTEM_task(void * pvParameters) break; } else if (strcmp(input_event, "LONG") == 0) { ESP_LOGI(TAG, "Long button press detected, toggling WiFi SoftAP"); - toggle_wifi_softap(); // Toggle AP + toggle_wifi_softap(); // Toggle AP } } } @@ -603,6 +602,9 @@ void SYSTEM_notify_rejected_share(GlobalState * GLOBAL_STATE) void SYSTEM_notify_mining_started(GlobalState * GLOBAL_STATE) { + SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; + + module->duration_start = esp_timer_get_time(); } void SYSTEM_notify_new_ntime(GlobalState * GLOBAL_STATE, uint32_t ntime) @@ -625,54 +627,37 @@ void SYSTEM_notify_found_nonce(GlobalState * GLOBAL_STATE, double found_diff, ui { SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE; + // Calculate the time difference in seconds with sub-second precision // hashrate = (nonce_difficulty * 2^32) / time_to_find - // let's calculate the 10min average hashrate - uint64_t time_period = 600 * 1e6; - uint64_t current_time = esp_timer_get_time(); + module->historical_hashrate[module->historical_hashrate_rolling_index] = GLOBAL_STATE->initial_ASIC_difficulty; + module->historical_hashrate_time_stamps[module->historical_hashrate_rolling_index] = esp_timer_get_time(); - int index = module->historical_hashrate_rolling_index; + module->historical_hashrate_rolling_index = (module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH; - module->historical_hashrate[index] = found_diff; - module->historical_hashrate_time_stamps[index] = current_time; + // ESP_LOGI(TAG, "nonce_diff %.1f, ttf %.1f, res %.1f", nonce_diff, duration, + // historical_hashrate[historical_hashrate_rolling_index]); + if (module->historical_hashrate_init < HISTORY_LENGTH) { + module->historical_hashrate_init++; + } else { + module->duration_start = + module->historical_hashrate_time_stamps[(module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH]; + } double sum = 0; - uint64_t oldest_time = 0; - int valid_shares = 0; - for (int i = 0; i < HISTORY_LENGTH; i++) { - // sum backwards - // avoid modulo of a negative number - int rindex = (index - i + HISTORY_LENGTH) % HISTORY_LENGTH; - - uint64_t timestamp = module->historical_hashrate_time_stamps[rindex]; - - // zero timestamps indicate that the "slot" is not used - if (timestamp == 0) { - break; - } - - // out of scope? break - if (current_time - timestamp > time_period) { - break; - } - - sum += module->historical_hashrate[rindex]; - oldest_time = timestamp; - valid_shares++; + for (int i = 0; i < module->historical_hashrate_init; i++) { + sum += module->historical_hashrate[i]; } - // increment rolling index - // can't be done before summation - index = (index + 1) % HISTORY_LENGTH; - module->historical_hashrate_rolling_index = index; - - double rolling_rate = (sum * 4.294967296e9) / (double) (time_period / 1e6); - double rolling_rate_gh = rolling_rate / 1.0e9; - - ESP_LOGI(TAG, "hashrate: %.3fGH%s shares: %d (historical buffer spans %ds)", rolling_rate_gh, - (current_time - oldest_time >= time_period) ? "" : "*", valid_shares, (int) ((current_time - oldest_time) / 1e6)); + double duration = (double) (esp_timer_get_time() - module->duration_start) / 1000000; - module->current_hashrate = rolling_rate_gh; + double rolling_rate = (sum * 4294967296) / (duration * 1000000000); + if (module->historical_hashrate_init < HISTORY_LENGTH) { + module->current_hashrate = rolling_rate; + } else { + // More smoothing + module->current_hashrate = ((module->current_hashrate * 9) + rolling_rate) / 10; + } _update_hashrate(GLOBAL_STATE); diff --git a/main/tasks/asic_result_task.c b/main/tasks/asic_result_task.c index 492da88d5..6b9313ae2 100644 --- a/main/tasks/asic_result_task.c +++ b/main/tasks/asic_result_task.c @@ -39,12 +39,10 @@ void ASIC_result_task(void *pvParameters) asic_result->nonce, asic_result->rolled_version); - uint32_t pool_difficulty = GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff; - //log the ASIC response - ESP_LOGI(TAG, "Ver: %08" PRIX32 " Nonce %08" PRIX32 " diff %.1f of %ld.", asic_result->rolled_version, asic_result->nonce, nonce_diff, pool_difficulty); + ESP_LOGI(TAG, "Ver: %08" PRIX32 " Nonce %08" PRIX32 " diff %.1f of %ld.", asic_result->rolled_version, asic_result->nonce, nonce_diff, GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff); - if (nonce_diff > pool_difficulty) + if (nonce_diff > GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff) { STRATUM_V1_submit_share( @@ -56,7 +54,8 @@ void ASIC_result_task(void *pvParameters) asic_result->nonce, asic_result->rolled_version ^ GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->version); - SYSTEM_notify_found_nonce(GLOBAL_STATE, (double) pool_difficulty, job_id); } + + SYSTEM_notify_found_nonce(GLOBAL_STATE, nonce_diff, job_id); } -} +} \ No newline at end of file