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
Open
mutatrum
wants to merge
7
commits into
bitaxeorg:master
Choose a base branch
from
mutatrum:refactor/unified-stratum-timing
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+225
−117
Open
Changes from 3 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
999a84c
refactor(stratum): unify share latency timing tracker across protocols
mutatrum f22ee32
Merge remote-tracking branch 'upstream/master' into refactor/unified-…
mutatrum 40cabf6
Remove c++ ifdef
mutatrum 26dc081
Guard on negative message_id and decouple share submit from timing
mutatrum e246810
Merge remote-tracking branch 'upstream/master' into refactor/unified-…
mutatrum 3464140
Merge remote-tracking branch 'upstream/master' into refactor/unified-…
mutatrum cc629c0
refactor(stratum): centralize submitted and pending share accounting
mutatrum File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 */ | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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)); | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.