1
0
mirror of https://review.coreboot.org/flashrom.git synced 2025-04-27 07:02:34 +02:00
flashrom/tests/udelay.c
Peter Marheine 183208b5cb udelay: only use OS time for delays, except on DOS
As proposed on the mailing list ("RFC: remove the calibrated delay
loop" [1]), this removes the calibrated delay loop and uses OS-based
timing functions for all delays because the calibrated delay loop can
delay for shorter times than intended.

When sleeping this now uses nanosleep() unconditionally, since usleep
was only used on DOS (where DJGPP lacks nanosleep).  When busy-looping,
it uses clock_gettime() with CLOCK_MONOTONIC or CLOCK_REALTIME depending
on availability, and gettimeofday() otherwise.

The calibrated delay loop is retained for DOS only, because timer
resolution on DJGPP is only about 50 milliseconds. Since typical delays
in flashrom are around 10 microseconds, using OS timing there would
regress performance by around 500x. The old implementation is reused
with some branches removed based on the knowledge that timer resolution
will not be better than about 50 milliseconds.

Tested by reading and writing flash on several Intel and AMD systems:

 * Lenovo P920 (Intel C620, read/verify only)
 * "nissa" chromebook (Intel Alder Lake-N)
 * "zork" chromebook (AMD Zen+)

[1]: https://mail.coreboot.org/hyperkitty/list/flashrom@flashrom.org/thread/HFH6UHPAKA4JDL4YKPSQPO72KXSSRGME/

Signed-off-by: Peter Marheine <pmarheine@chromium.org>
Change-Id: I7ac5450d194a475143698d65d64d8bcd2fd25e3f
Reviewed-on: https://review.coreboot.org/c/flashrom/+/81545
Reviewed-by: Anastasia Klimchuk <aklm@chromium.org>
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Brian Norris <briannorris@chromium.org>
2024-04-25 23:23:01 +00:00

52 lines
1.4 KiB
C

/*
* This file is part of the flashrom project.
*
* Copyright 2024 Google LLC
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include <include/test.h>
#include <stdint.h>
#include <sys/time.h>
#include <time.h>
#include "programmer.h"
#include "tests.h"
static uint64_t now_us(void) {
#if HAVE_CLOCK_GETTIME == 1
struct timespec ts;
clock_gettime(CLOCK_MONOTONIC, &ts);
return (ts.tv_nsec / 1000) + (ts.tv_sec * 1000000);
#else
struct timeval tv;
gettimeofday(&tv, NULL);
return tv.tv_usec + (tv.tv_sec * 1000000);
#endif
}
/*
* A short delay should delay for at least as long as requested,
* and more than 10x as long would be worrisome.
*
* This test could fail spuriously on a heavily-loaded system, or if we need
* to use gettimeofday() and a time change (such as DST) occurs during the
* test.
*/
void udelay_test_short(void **state) {
uint64_t start = now_us();
default_delay(100);
uint64_t elapsed = now_us() - start;
assert_in_range(elapsed, 100, 1000);
}