Skip to content

Commit

Permalink
Merge pull request #293 from shufps/hashrate
Browse files Browse the repository at this point in the history
improved hashrate, calculation is now more stable and hashrate will be calculated over an average of 10minutes
  • Loading branch information
WantClue authored Aug 16, 2024
2 parents 064b892 + c25806b commit 80e72cc
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 41 deletions.
7 changes: 3 additions & 4 deletions main/global_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

#define STRATUM_USER CONFIG_STRATUM_USER

#define HISTORY_LENGTH 100
// best is power of 2 for ring buffers
#define HISTORY_LENGTH 512
#define DIFF_STRING_SIZE 10

typedef enum
Expand Down Expand Up @@ -45,11 +46,9 @@ typedef struct

typedef struct
{
double duration_start;
int historical_hashrate_rolling_index;
double historical_hashrate_time_stamps[HISTORY_LENGTH];
uint64_t 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;
Expand Down
79 changes: 47 additions & 32 deletions main/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -60,9 +60,10 @@ static void _init_system(GlobalState * GLOBAL_STATE)
{
SystemModule * module = &GLOBAL_STATE->SYSTEM_MODULE;

module->duration_start = 0;
module->historical_hashrate_rolling_index = 0;
module->historical_hashrate_init = 0;
memset(module->historical_hashrate, 0, sizeof(module->historical_hashrate));
memset(module->historical_hashrate_time_stamps, 0, sizeof(module->historical_hashrate_time_stamps));

module->current_hashrate = 0;
module->screen_page = 0;
module->shares_accepted = 0;
Expand All @@ -73,7 +74,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);

Expand Down Expand Up @@ -147,7 +148,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);
Expand Down Expand Up @@ -191,7 +192,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%s: %.1f J/Th: %.1f", module->historical_hashrate_init < HISTORY_LENGTH ? "*" : "",
snprintf(module->oled_buf, 20, "Gh: %.1f J/Th: %.1f",
module->current_hashrate, efficiency);
OLED_writeString(0, 0, module->oled_buf);
break;
Expand Down Expand Up @@ -354,7 +355,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);
Expand Down Expand Up @@ -577,7 +578,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
}
}
}
Expand All @@ -602,9 +603,6 @@ 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)
Expand All @@ -627,38 +625,55 @@ 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

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();
// let's calculate the 10min average hashrate
uint64_t time_period = 600 * 1e6;
uint64_t current_time = esp_timer_get_time();

module->historical_hashrate_rolling_index = (module->historical_hashrate_rolling_index + 1) % HISTORY_LENGTH;
int index = module->historical_hashrate_rolling_index;

// ESP_LOGI(TAG, "nonce_diff %.1f, ttf %.1f, res %.1f", nonce_diff, duration,
// historical_hashrate[historical_hashrate_rolling_index]);
module->historical_hashrate[index] = found_diff;
module->historical_hashrate_time_stamps[index] = current_time;

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;
for (int i = 0; i < module->historical_hashrate_init; i++) {
sum += module->historical_hashrate[i];
}
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;

double duration = (double) (esp_timer_get_time() - module->duration_start) / 1000000;
uint64_t timestamp = module->historical_hashrate_time_stamps[rindex];

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;
// 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++;
}

// 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));

module->current_hashrate = rolling_rate_gh;

_update_hashrate(GLOBAL_STATE);

// logArrayContents(historical_hashrate, HISTORY_LENGTH);
Expand Down
11 changes: 6 additions & 5 deletions main/tasks/asic_result_task.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,12 @@ 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, GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff);
ESP_LOGI(TAG, "Ver: %08" PRIX32 " Nonce %08" PRIX32 " diff %.1f of %ld.", asic_result->rolled_version, asic_result->nonce, nonce_diff, pool_difficulty);

if (nonce_diff > GLOBAL_STATE->ASIC_TASK_MODULE.active_jobs[job_id]->pool_diff)
if (nonce_diff > pool_difficulty)
{

STRATUM_V1_submit_share(
Expand All @@ -54,8 +56,7 @@ 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);
}
}
}

0 comments on commit 80e72cc

Please sign in to comment.