From a2f6fa0d513db9b8b5f916ff6459831628ba49d5 Mon Sep 17 00:00:00 2001 From: Ben Date: Mon, 5 Jun 2023 21:26:51 -0400 Subject: [PATCH] Fix memory leak; improved display and hashrate calculation --- main/miner.c | 24 ++++++++++++++++-------- main/system.c | 35 ++++++++++++++++++++++------------- main/system.h | 2 +- 3 files changed, 39 insertions(+), 22 deletions(-) diff --git a/main/miner.c b/main/miner.c index 05e2b607..e3742e72 100755 --- a/main/miner.c +++ b/main/miner.c @@ -92,6 +92,8 @@ static void ASIC_task(void * pvParameters) set_max_baud(); + notify_system_mining_started(); + ESP_LOGI(TAG, "Mining!"); while (1) { bm_job * next_bm_job = (bm_job *) queue_dequeue(&ASIC_jobs_queue); struct job_packet job; @@ -104,14 +106,15 @@ static void ASIC_task(void * pvParameters) memcpy(&job.ntime, &next_bm_job->ntime, 4); memcpy(&job.merkle4, next_bm_job->merkle_root + 28, 4); memcpy(job.midstate, next_bm_job->midstate, 32); - if (active_jobs[id] != NULL) { - free(active_jobs[id]->jobid); - free(active_jobs[id]->extranonce2); - free(active_jobs[id]); + if (active_jobs[job.job_id] != NULL) { + free(active_jobs[job.job_id]->jobid); + free(active_jobs[job.job_id]->extranonce2); + free(active_jobs[job.job_id]); } - active_jobs[id] = next_bm_job; + active_jobs[job.job_id] = next_bm_job; + pthread_mutex_lock(&valid_jobs_lock); - valid_jobs[id] = 1; + valid_jobs[job.job_id] = 1; pthread_mutex_unlock(&valid_jobs_lock); clear_serial_buffer(); @@ -172,6 +175,7 @@ static void ASIC_task(void * pvParameters) active_jobs[nonce.job_id]->extranonce2, nonce.nonce); } + } } @@ -332,6 +336,7 @@ static void stratum_task(void * pvParameters) ESP_LOGI(TAG, "Set stratum difficulty: %d", stratum_difficulty); set_job_difficulty_mask(stratum_difficulty); } + free(line); } else if (method == MINING_SET_VERSION_MASK) { version_mask = parse_mining_set_version_mask_message(line); @@ -347,10 +352,13 @@ static void stratum_task(void * pvParameters) } else { ESP_LOGI(TAG, "message id %d result rejected", parsed_id); notify_system_rejected_share(); - } - } else { + } + free(line); + } else { free(line); } + + } if (sock != -1) diff --git a/main/system.c b/main/system.c index 35ef4bd9..2c579e21 100644 --- a/main/system.c +++ b/main/system.c @@ -54,9 +54,11 @@ void update_hashrate(void){ return; } + float power = INA260_read_power() / 1000; + OLED_clearLine(0); memset(oled_buf, 0, 20); - snprintf(oled_buf, 20, "GH/s%s: %.1f", historical_hashrate_init < HISTORY_LENGTH ? "*": "", current_hashrate); + snprintf(oled_buf, 20, "Gh%s: %.1f Gh/W: %.1f", historical_hashrate_init < HISTORY_LENGTH ? "*": "", current_hashrate, current_hashrate/power); OLED_writeString(0, 0, oled_buf); } @@ -80,7 +82,9 @@ void notify_system_rejected_share(void){ } - +void notify_system_mining_started(void){ + duration_start = esp_timer_get_time(); +} void notify_system_found_nonce(double nonce_diff){ @@ -112,7 +116,13 @@ void notify_system_found_nonce(double nonce_diff){ double duration = (double)(esp_timer_get_time() - duration_start) / 1000000; - current_hashrate = (sum * 4294967296) / (duration * 1000000000); + double rolling_rate = (sum * 4294967296) / (duration * 1000000000); + if(historical_hashrate_init < HISTORY_LENGTH){ + current_hashrate = rolling_rate; + }else{ + // More smoothing + current_hashrate = ((current_hashrate * 9) + rolling_rate)/10; + } update_hashrate(); @@ -158,7 +168,7 @@ void init_system(void) { OLED_fill(0); } - duration_start = esp_timer_get_time(); + } @@ -175,14 +185,11 @@ void update_system_info(void) { uint16_t fan_speed = EMC2101_get_fan_speed(); float chip_temp = EMC2101_get_chip_temp(); - //float current = INA260_read_current(); float voltage = INA260_read_voltage(); float power = INA260_read_power() / 1000; float current = INA260_read_current(); - //uint16_t vcore = ADC_get_vcore(); if (OLED_status()) { - clear_display(); memset(oled_buf, 0, 20); snprintf(oled_buf, 20, " Fan: %d RPM", fan_speed); @@ -208,16 +215,17 @@ void update_esp32_info(void) { uint32_t free_heap_size = esp_get_free_heap_size(); + uint16_t vcore = ADC_get_vcore(); + if (OLED_status()) { - clear_display(); memset(oled_buf, 0, 20); snprintf(oled_buf, 20, "FH: %u bytes", free_heap_size); OLED_writeString(0, 0, oled_buf); - // memset(oled_buf, 0, 20); - // snprintf(oled_buf, 20, "Temp: %.2f C", chip_temp); - // OLED_writeString(0, 1, oled_buf); + memset(oled_buf, 0, 20); + snprintf(oled_buf, 20, "vCore: %u Mw", vcore); + OLED_writeString(0, 1, oled_buf); // memset(oled_buf, 0, 20); // snprintf(oled_buf, 20, "Pwr: %.2f W", power); @@ -238,8 +246,6 @@ void update_system_performance(){ if (OLED_status()) { - - clear_display(); update_hashrate(); update_shares(); @@ -257,14 +263,17 @@ void system_task(void *arg) { init_system(); while(1){ + clear_display(); screen_page = 0; update_system_performance(); vTaskDelay(40000 / portTICK_RATE_MS); + clear_display(); screen_page = 1; update_system_info(); vTaskDelay(10000 / portTICK_RATE_MS); + clear_display(); screen_page = 2; update_esp32_info(); vTaskDelay(10000 / portTICK_RATE_MS); diff --git a/main/system.h b/main/system.h index 2045a378..5d442613 100644 --- a/main/system.h +++ b/main/system.h @@ -7,6 +7,6 @@ void get_stats(void); void notify_system_accepted_share(void); void notify_system_rejected_share(void); void notify_system_found_nonce(double nonce_diff); - +void notify_system_mining_started(void); #endif /* SYSTEM_H_ */ \ No newline at end of file