Skip to content

Commit 7f4784f

Browse files
gwendalcrEnric Balletbo i Serra
authored andcommitted
platform/chrome: cros_ec_sensorhub: Simplify legacy timestamp spreading
On some machines (nami), interrupt latency cause samples to appear to be from the future and are pegged to the current time. We would see samples with this pattern: [t, t + ~5ms, t + ~10ms, t + ~10ms + 100us, t + ~10ms + 200us], (current now) (current now) (t is the last timestamp time) Last 2 samples would be barely spread, causing applications to complain. We now spread the entire sequence. This is not great: in the example the sensor was supposed to send samples every 5ms, it now appears to send one every 2.5ms, but it is slightly closer to reality: sampling time in the example above At sensor level 1 2 3 4 5 +-----5ms-----+-----5ms-----+-----5ms-----+----5ms-----+---> t Before, at host level 1 2 3 4 5 --interrupt delay------+-----5ms-----+-----5ms-----+-+-+---> t Afer, at host level 1 2 3 4 5 --interrupt delay------+-2.5ms-+-2.5ms-+-2.5ms-+-2.5ms-+---> t Signed-off-by: Gwendal Grignou <[email protected]> Signed-off-by: Enric Balletbo i Serra <[email protected]>
1 parent c1e18d4 commit 7f4784f

File tree

1 file changed

+33
-61
lines changed

1 file changed

+33
-61
lines changed

drivers/platform/chrome/cros_ec_sensorhub_ring.c

Lines changed: 33 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -673,29 +673,22 @@ cros_ec_sensor_ring_spread_add(struct cros_ec_sensorhub *sensorhub,
673673
* cros_ec_sensor_ring_spread_add_legacy: Calculate proper timestamps then
674674
* add to ringbuffer (legacy).
675675
*
676-
* Note: This assumes we're running old firmware, where every sample's timestamp
677-
* is after the sample. Run if tight_timestamps == false.
678-
*
679-
* If there is a sample with a proper timestamp
676+
* Note: This assumes we're running old firmware, where timestamp
677+
* is inserted after its sample(s)e. There can be several samples between
678+
* timestamps, so several samples can have the same timestamp.
680679
*
681680
* timestamp | count
682681
* -----------------
683-
* older_unprocess_out --> TS1 | 1
684-
* TS1 | 2
685-
* out --> TS1 | 3
686-
* next_out --> TS2 |
687-
*
688-
* We spread time for the samples [older_unprocess_out .. out]
689-
* between TS1 and TS2: [TS1+1/4, TS1+2/4, TS1+3/4, TS2].
682+
* 1st sample --> TS1 | 1
683+
* TS2 | 2
684+
* TS2 | 3
685+
* TS3 | 4
686+
* last_out -->
690687
*
691-
* If we reach the end of the samples, we compare with the
692-
* current timestamp:
693688
*
694-
* older_unprocess_out --> TS1 | 1
695-
* TS1 | 2
696-
* out --> TS1 | 3
689+
* We spread time for the samples using perod p = (current - TS1)/4.
690+
* between TS1 and TS2: [TS1+p/4, TS1+2p/4, TS1+3p/4, current_timestamp].
697691
*
698-
* We know have [TS1+1/3, TS1+2/3, current timestamp]
699692
*/
700693
static void
701694
cros_ec_sensor_ring_spread_add_legacy(struct cros_ec_sensorhub *sensorhub,
@@ -708,58 +701,37 @@ cros_ec_sensor_ring_spread_add_legacy(struct cros_ec_sensorhub *sensorhub,
708701
int i;
709702

710703
for_each_set_bit(i, &sensor_mask, sensorhub->sensor_num) {
711-
s64 older_timestamp;
712704
s64 timestamp;
713-
struct cros_ec_sensors_ring_sample *older_unprocess_out =
714-
sensorhub->ring;
715-
struct cros_ec_sensors_ring_sample *next_out;
716-
int count = 1;
717-
718-
for (out = sensorhub->ring; out < last_out; out = next_out) {
719-
s64 time_period;
705+
int count = 0;
706+
s64 time_period;
720707

721-
next_out = out + 1;
708+
for (out = sensorhub->ring; out < last_out; out++) {
722709
if (out->sensor_id != i)
723710
continue;
724711

725712
/* Timestamp to start with */
726-
older_timestamp = out->timestamp;
727-
728-
/* Find next sample. */
729-
while (next_out < last_out && next_out->sensor_id != i)
730-
next_out++;
713+
timestamp = out->timestamp;
714+
out++;
715+
count = 1;
716+
break;
717+
}
718+
for (; out < last_out; out++) {
719+
/* Find last sample. */
720+
if (out->sensor_id != i)
721+
continue;
722+
count++;
723+
}
724+
if (count == 0)
725+
continue;
731726

732-
if (next_out >= last_out) {
733-
timestamp = current_timestamp;
734-
} else {
735-
timestamp = next_out->timestamp;
736-
if (timestamp == older_timestamp) {
737-
count++;
738-
continue;
739-
}
740-
}
727+
/* Spread uniformly between the first and last samples. */
728+
time_period = div_s64(current_timestamp - timestamp, count);
741729

742-
/*
743-
* The next sample has a new timestamp, spread the
744-
* unprocessed samples.
745-
*/
746-
if (next_out < last_out)
747-
count++;
748-
time_period = div_s64(timestamp - older_timestamp,
749-
count);
750-
751-
for (; older_unprocess_out <= out;
752-
older_unprocess_out++) {
753-
if (older_unprocess_out->sensor_id != i)
754-
continue;
755-
older_timestamp += time_period;
756-
older_unprocess_out->timestamp =
757-
older_timestamp;
758-
}
759-
count = 1;
760-
/* The next_out sample has a valid timestamp, skip. */
761-
next_out++;
762-
older_unprocess_out = next_out;
730+
for (out = sensorhub->ring; out < last_out; out++) {
731+
if (out->sensor_id != i)
732+
continue;
733+
timestamp += time_period;
734+
out->timestamp = timestamp;
763735
}
764736
}
765737

0 commit comments

Comments
 (0)