Simplify logic to calculate the BT/system clock latency
PATCH 1/2 Simplify logic to calculate the BT/system clock latency
This is only a cosmetic change. Using a while loop we avoid direct manipulation of counter loop variable when error happens reading the BT clock.
--- health/mcap_sync.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
health/mcap_sync.c +4 -3
@@ -384,12 +384,12 @@ static gboolean initialize_caps(struct mcap_mcl *mcl)/* Do clock read a number of times and measure latency */ avg =0 ; + i =0 ; retries =10 ; -for (i =0 ; i <20 && retries >0 ; ++i) { +while ((i <20 ) && (retries >0 )) {clock_gettime (CLK , &t1);if (!read_btclock (mcl, &btclock, &btaccuracy)) { - --i; - --retries; + retries--;continue ; }clock_gettime (CLK , &t2);
@@ -397,6 +397,7 @@ static gboolean initialize_caps(struct mcap_mcl *mcl) latency =time_us (&t2) -time_us (&t1); latencies[i] = latency; avg += latency; + i++; }if (retries <=0 )
Signature · 1 line
1.7.0.4
PATCH 2/2 Remove magic numbers in MCAP CSP
--- health/mcap_sync.c | 18 ++++++++++-------- 1 files changed, 10 insertions(+), 8 deletions(-)
health/mcap_sync.c +10 -8
@@ -48,6 +48,8 @@#define CLK CLOCK_MONOTONIC#define MCAP_CSP_ERROR g_quark_from_static_string("mcap-csp-error-quark") +#define MAX_RETRIES 10 +#define SAMPLE_COUNT 20struct mcap_csp {uint64_t base_tmstamp;/* CSP base timestamp */
@@ -363,7 +365,7 @@ uint32_t mcap_get_btclock(struct mcap_mcl *mcl)static gboolean initialize_caps (struct mcap_mcl *mcl) {struct timespec t1, t2; -int latencies[20 ]; +int latencies[SAMPLE_COUNT ];int latency, avg, dev;uint32_t btclock;uint16_t btaccuracy;
@@ -385,8 +387,8 @@ static gboolean initialize_caps(struct mcap_mcl *mcl)/* Do clock read a number of times and measure latency */ avg =0 ; i =0 ; - retries =10 ; -while ((i <20 ) && (retries >0 )) { + retries =MAX_RETRIES ; +while ((i <SAMPLE_COUNT ) && (retries >0 )) {clock_gettime (CLK , &t1);if (!read_btclock (mcl, &btclock, &btaccuracy)) { retries--;
@@ -404,21 +406,21 @@ static gboolean initialize_caps(struct mcap_mcl *mcl)return FALSE;/* Calculate average and deviation */ - avg /=20 ; + avg /=SAMPLE_COUNT ; dev =0 ; -for (i =0 ; i <20 ; ++i) +for (i =0 ; i <SAMPLE_COUNT ; ++i) dev +=abs (latencies[i] - avg); - dev /=20 ; + dev /=SAMPLE_COUNT ;/* Calculate corrected average, without 'freak' latencies */ latency =0 ; -for (i =0 ; i <20 ; ++i) { +for (i =0 ; i <SAMPLE_COUNT ; ++i) {if (latencies[i] > (avg + dev *6 )) latency += avg;else latency += latencies[i]; } - latency /=20 ; + latency /=SAMPLE_COUNT ; _caps.latency = latency; _caps.preempt_thresh = latency *4 ;
Signature · 1 line
1.7.0.4
Re: [PATCH 2/2] Remove magic numbers in MCAP CSP
Hi,
Quoted 3 lines · Santiago Carot-Nemesio
--- health/mcap_sync.c | 18 ++++++++++-------- 1 files changed, 10 insertions(+), 8 deletions(-)
Thanks. This patch is now also in the upstream tree.
Johan
Hi,
Quoted 6 lines · Santiago Carot-Nemesio
This is only a cosmetic change. Using a while loop we avoid direct manipulation of counter loop variable when error happens reading the BT clock.
--- health/mcap_sync.c | 7 ++++--- 1 files changed, 4 insertions(+), 3 deletions(-)
Thanks. The patch has been pushed upstream.
health/mcap_sync.c static gboolean initialize_caps(struct mcap_mcl *mcl) · line 389
Quoted 1 line
health/mcap_sync.c +1
-0+while ((i <20 ) && (retries >0 )) {
The extra parentheses around the comparisons are unnecessary. I fixed that in a subsequent cleanup commit. Usually we only add these extra parentheses for binary bitwise operations such as & and | for clarity.
Johan