Repository navigation
Extract share latency timing tracker out of sv1 and sv2 #2013
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
999a84c
f22ee32
40cabf6
26dc081
e246810
3464140
cc629c0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| #ifndef STRATUM_TIMING_H | ||
| #define STRATUM_TIMING_H | ||
|
|
||
| #include <stdint.h> | ||
| #include <stdbool.h> | ||
|
|
||
| #define STRATUM_TIMING_SLOTS 64 | ||
|
|
||
| typedef struct { | ||
| uint64_t submit_time_us[STRATUM_TIMING_SLOTS]; | ||
| } stratum_timing_tracker_t; | ||
|
|
||
| /** | ||
| * @brief Record a submission timestamp for a given request / share ID. | ||
| * | ||
| * @param tracker Pointer to the timing tracker. | ||
| * @param id The request ID / share ID / sequence number. | ||
| * @param submit_time_us The submission timestamp in microseconds. | ||
| */ | ||
| void stratum_timing_record(stratum_timing_tracker_t *tracker, uint32_t id, uint64_t submit_time_us); | ||
|
|
||
| /** | ||
| * @brief Calculate round-trip response time in milliseconds for an ID, and clear the slot. | ||
| * | ||
| * @param tracker Pointer to the timing tracker. | ||
| * @param id The response ID / share ID / sequence number. | ||
| * @param now_us The current timestamp in microseconds. | ||
| * @return Latency in milliseconds, or -1.0f if the ID was not tracked or invalid. | ||
| */ | ||
| float stratum_timing_calculate_ms(stratum_timing_tracker_t *tracker, uint32_t id, uint64_t now_us); | ||
|
|
||
| /** | ||
| * @brief Reset/clear all tracking slots (e.g. on disconnect or pool switch). | ||
| * | ||
| * @param tracker Pointer to the timing tracker. | ||
| */ | ||
| void stratum_timing_reset(stratum_timing_tracker_t *tracker); | ||
|
|
||
| #endif /* STRATUM_TIMING_H */ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| #include "stratum_timing.h" | ||
| #include <string.h> | ||
|
|
||
| void stratum_timing_record(stratum_timing_tracker_t *tracker, uint32_t id, uint64_t submit_time_us) | ||
| { | ||
| if (!tracker) { | ||
| return; | ||
| } | ||
| tracker->submit_time_us[id % STRATUM_TIMING_SLOTS] = submit_time_us; | ||
| } | ||
|
|
||
| float stratum_timing_calculate_ms(stratum_timing_tracker_t *tracker, uint32_t id, uint64_t now_us) | ||
| { | ||
| if (!tracker) { | ||
| return -1.0f; | ||
| } | ||
| uint32_t slot = id % STRATUM_TIMING_SLOTS; | ||
| uint64_t submit_time_us = tracker->submit_time_us[slot]; | ||
| if (submit_time_us == 0 || now_us < submit_time_us) { | ||
| return -1.0f; | ||
| } | ||
| tracker->submit_time_us[slot] = 0; | ||
| return (float)(now_us - submit_time_us) / 1000.0f; | ||
| } | ||
|
|
||
| void stratum_timing_reset(stratum_timing_tracker_t *tracker) | ||
| { | ||
| if (!tracker) { | ||
| return; | ||
| } | ||
| memset(tracker->submit_time_us, 0, sizeof(tracker->submit_time_us)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| #include "unity.h" | ||
| #include "stratum_timing.h" | ||
| #include <string.h> | ||
|
|
||
| TEST_CASE("stratum_timing record and calculate latency", "[stratum_timing]") | ||
| { | ||
| stratum_timing_tracker_t tracker; | ||
| stratum_timing_reset(&tracker); | ||
|
|
||
| // Record request 1 at t = 1,000,000 us (1.0s) | ||
| stratum_timing_record(&tracker, 1, 1000000ULL); | ||
|
|
||
| // Calculate response at t = 1,025,000 us (25ms later) | ||
| float latency_ms = stratum_timing_calculate_ms(&tracker, 1, 1025000ULL); | ||
| TEST_ASSERT_FLOAT_WITHIN(0.01f, 25.0f, latency_ms); | ||
|
|
||
| // Second call should return -1.0f as the slot is cleared | ||
| latency_ms = stratum_timing_calculate_ms(&tracker, 1, 1030000ULL); | ||
| TEST_ASSERT_EQUAL_FLOAT(-1.0f, latency_ms); | ||
| } | ||
|
|
||
| TEST_CASE("stratum_timing wraparound and slot reuse", "[stratum_timing]") | ||
| { | ||
| stratum_timing_tracker_t tracker; | ||
| stratum_timing_reset(&tracker); | ||
|
|
||
| uint32_t id_first = 5; | ||
| uint32_t id_second = 5 + STRATUM_TIMING_SLOTS; // Wraps around to the exact same slot | ||
|
|
||
| // Record first ID | ||
| stratum_timing_record(&tracker, id_first, 2000000ULL); | ||
|
|
||
| // Second ID wraps around and overwrites the slot | ||
| stratum_timing_record(&tracker, id_second, 2100000ULL); | ||
|
|
||
| // Calculate for the second ID | ||
| float latency_ms = stratum_timing_calculate_ms(&tracker, id_second, 2115500ULL); | ||
| TEST_ASSERT_FLOAT_WITHIN(0.01f, 15.5f, latency_ms); | ||
|
|
||
| // Slot is now cleared, checking first or second ID yields -1.0f | ||
| TEST_ASSERT_EQUAL_FLOAT(-1.0f, stratum_timing_calculate_ms(&tracker, id_first, 2120000ULL)); | ||
| TEST_ASSERT_EQUAL_FLOAT(-1.0f, stratum_timing_calculate_ms(&tracker, id_second, 2120000ULL)); | ||
| } | ||
|
|
||
| TEST_CASE("stratum_timing reset", "[stratum_timing]") | ||
| { | ||
| stratum_timing_tracker_t tracker; | ||
| stratum_timing_reset(&tracker); | ||
|
|
||
| stratum_timing_record(&tracker, 10, 5000000ULL); | ||
| stratum_timing_record(&tracker, 11, 5000100ULL); | ||
| stratum_timing_record(&tracker, 12, 5000200ULL); | ||
|
|
||
| stratum_timing_reset(&tracker); | ||
|
|
||
| TEST_ASSERT_EQUAL_FLOAT(-1.0f, stratum_timing_calculate_ms(&tracker, 10, 5050000ULL)); | ||
| TEST_ASSERT_EQUAL_FLOAT(-1.0f, stratum_timing_calculate_ms(&tracker, 11, 5050000ULL)); | ||
| TEST_ASSERT_EQUAL_FLOAT(-1.0f, stratum_timing_calculate_ms(&tracker, 12, 5050000ULL)); | ||
| } | ||
|
|
||
| TEST_CASE("stratum_timing invalid timestamps and null safety", "[stratum_timing]") | ||
| { | ||
| stratum_timing_tracker_t tracker; | ||
| stratum_timing_reset(&tracker); | ||
|
|
||
| // NULL tracker safety | ||
| stratum_timing_record(NULL, 1, 1000ULL); | ||
| stratum_timing_reset(NULL); | ||
| TEST_ASSERT_EQUAL_FLOAT(-1.0f, stratum_timing_calculate_ms(NULL, 1, 2000ULL)); | ||
|
|
||
| // Backward clock jump (now_us < submit_time_us) | ||
| stratum_timing_record(&tracker, 7, 5000000ULL); | ||
| TEST_ASSERT_EQUAL_FLOAT(-1.0f, stratum_timing_calculate_ms(&tracker, 7, 4999999ULL)); | ||
|
|
||
| // Non-existent ID | ||
| TEST_ASSERT_EQUAL_FLOAT(-1.0f, stratum_timing_calculate_ms(&tracker, 999, 6000000ULL)); | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -269,7 +269,9 @@ int stratum_submit_share(GlobalState *GLOBAL_STATE, const asic_job_t *active_job | |
| ret = stratum_v1_submit_share(GLOBAL_STATE, active_job, nonce, rolled_version, sent_time_us); | ||
| } | ||
|
|
||
| if (ret < 0) { | ||
| if (ret >= 0) { | ||
| SYSTEM_notify_submitted_share(GLOBAL_STATE); | ||
|
Comment on lines
+272
to
+273
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Record successful submissions before releasing the connection lock. A disconnect can reset the session before this counter update, counting an old submission in the new session. A controlled test leaves one phantom pending share; the base leaves zero. Add a regression test for this sequence. |
||
| } else { | ||
| ESP_LOGW(TAG, "Failed to submit share to socket (ret: %d, errno %d: %s)", ret, errno, strerror(errno)); | ||
| // stratum_task recv loop will detect a broken connection on its next read and handle reconnection | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.