Merge branch 'bugfix/adjtime_ut_v3.3' into 'release/v3.3'

newlib: Fix adjtime (v3.3)

See merge request idf/esp-idf!4870
This commit is contained in:
Angus Gratton 2019-05-15 15:44:33 +08:00
commit e209307297
2 changed files with 88 additions and 62 deletions

View File

@ -205,97 +205,123 @@ TEST_CASE("test for no interlocking adjtime, gettimeofday and settimeofday funct
}
#ifndef CONFIG_FREERTOS_UNICORE
static xSemaphoreHandle gettime_start_sema;
static xSemaphoreHandle adjtime_continue_sema;
static void adjtimeTask(void *pvParameters)
{
const uint64_t adjtime_us = 2000000000; // 2000 sec
struct timeval delta = {.tv_sec = adjtime_us / 1000000, .tv_usec = 900000};
struct timeval outdelta;
struct timeval tv_time;
int shift = 1;
int cycle = 0;
#define ADJTIME_CORRECTION_FACTOR 6
static int64_t result_adjtime_correction_us[2];
static void get_time_task(void *pvParameters)
{
xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
struct timeval tv_time;
// although exit flag is set in another task, checking (exit_flag == false) is safe
while (exit_flag == false) {
gettimeofday(&tv_time, NULL);
uint64_t start_time_us = (uint64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
++cycle;
if (shift > 3000) {
shift = 1;
}
int i_shift = shift++;
xSemaphoreGive(gettime_start_sema);
while (--i_shift) {
__asm__ __volatile__ ("nop": : : "memory");
};
adjtime(&delta, &outdelta);
xSemaphoreTake(adjtime_continue_sema, portMAX_DELAY);
gettimeofday(&tv_time, NULL);
uint64_t end_time_us = (uint64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
if (!(start_time_us < end_time_us) || (end_time_us - start_time_us > adjtime_us)
|| (end_time_us - start_time_us > 300) || (end_time_us - start_time_us < 30)) {
printf("ERROR: start_time_us %lld usec.\n", start_time_us);
printf("ERROR: end_time_us %lld usec.\n", end_time_us);
printf("ERROR: dt %lld usec.\n", end_time_us - start_time_us);
printf("ERROR: cycle %d\n", cycle);
printf("ERROR: shift %d\n", shift);
TEST_FAIL_MESSAGE("The time is corrupted due to the lack of locks for the adjtime function.");
}
}
xSemaphoreGive(adjtime_continue_sema);
xSemaphoreGive(gettime_start_sema);
xSemaphoreGive(*sema);
vTaskDelete(NULL);
}
static void gettimeofdayTask(void *pvParameters)
static void start_measure(int64_t* sys_time, int64_t* real_time)
{
struct timeval tv_time;
*real_time = esp_timer_get_time();
gettimeofday(&tv_time, NULL);
*sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
}
static void end_measure(int64_t* sys_time, int64_t* real_time)
{
struct timeval tv_time;
gettimeofday(&tv_time, NULL);
*real_time = esp_timer_get_time();
*sys_time = (int64_t)tv_time.tv_sec * 1000000L + tv_time.tv_usec;
}
static int64_t calc_correction(const char* tag, int64_t* sys_time, int64_t* real_time)
{
int64_t dt_real_time_us = real_time[1] - real_time[0];
int64_t dt_sys_time_us = sys_time[1] - sys_time[0];
int64_t calc_correction_us = dt_real_time_us >> ADJTIME_CORRECTION_FACTOR;
int64_t real_correction_us = dt_sys_time_us - dt_real_time_us;
int64_t error_us = calc_correction_us - real_correction_us;
printf("%s: dt_real_time = %lli us, dt_sys_time = %lli us, calc_correction = %lli us, error = %lli us\n",
tag, dt_real_time_us, dt_sys_time_us, calc_correction_us, error_us);
TEST_ASSERT_TRUE(dt_sys_time_us > 0 && dt_real_time_us > 0);
TEST_ASSERT_INT_WITHIN(100, 0, error_us);
return real_correction_us;
}
static void measure_time_task(void *pvParameters)
{
struct timeval tv_time;
int64_t real_time_us[2];
int64_t sys_time_us[2];
int64_t delay_us = 2 * 1000000; // 2 sec
xSemaphoreHandle *sema = (xSemaphoreHandle *) pvParameters;
gettimeofday(&tv_time, NULL);
start_measure(&sys_time_us[0], &real_time_us[0]);
// although exit flag is set in another task, checking (exit_flag == false) is safe
while (exit_flag == false) {
xSemaphoreTake(gettime_start_sema, portMAX_DELAY);
gettimeofday(&tv_time, NULL);
xSemaphoreGive(adjtime_continue_sema);
ets_delay_us(delay_us);
end_measure(&sys_time_us[1], &real_time_us[1]);
result_adjtime_correction_us[1] += calc_correction("measure", sys_time_us, real_time_us);
sys_time_us[0] = sys_time_us[1];
real_time_us[0] = real_time_us[1];
}
xSemaphoreGive(adjtime_continue_sema);
xSemaphoreGive(gettime_start_sema);
xSemaphoreGive(*sema);
vTaskDelete(NULL);
}
TEST_CASE("test for thread safety adjtime and gettimeofday functions", "[newlib]")
TEST_CASE("test time adjustment happens linearly", "[newlib][timeout=35]")
{
int64_t real_time_us[2];
int64_t sys_time_us[2];
exit_flag = false;
struct timeval tv_time = { .tv_sec = 1520000000, .tv_usec = 0 };
struct timeval tv_time = {.tv_sec = 1550000000, .tv_usec = 0};
TEST_ASSERT_EQUAL(0, settimeofday(&tv_time, NULL));
gettime_start_sema = xSemaphoreCreateBinary();
adjtime_continue_sema = xSemaphoreCreateBinary();
struct timeval delta = {.tv_sec = 2000, .tv_usec = 900000};
adjtime(&delta, NULL);
gettimeofday(&tv_time, NULL);
printf("CPU0 and CPU1. Tasks run: 1 - adjtimeTask, 2 - gettimeofdayTask\n");
xTaskCreatePinnedToCore(adjtimeTask, "adjtimeTask", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
xTaskCreatePinnedToCore(gettimeofdayTask, "gettimeofdayTask", 2048, NULL, UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
xSemaphoreHandle exit_sema[2];
for (int i = 0; i < 2; ++i) {
exit_sema[i] = xSemaphoreCreateBinary();
result_adjtime_correction_us[i] = 0;
}
printf("start wait for 10 seconds\n");
vTaskDelay(10000 / portTICK_PERIOD_MS);
start_measure(&sys_time_us[0], &real_time_us[0]);
xTaskCreatePinnedToCore(get_time_task, "get_time_task", 2048, &exit_sema[0], UNITY_FREERTOS_PRIORITY - 1, NULL, 0);
xTaskCreatePinnedToCore(measure_time_task, "measure_time_task", 2048, &exit_sema[1], UNITY_FREERTOS_PRIORITY - 1, NULL, 1);
printf("start waiting for 30 seconds\n");
vTaskDelay(30000 / portTICK_PERIOD_MS);
// set exit flag to let thread exit
exit_flag = true;
vTaskDelay(2000 / portTICK_PERIOD_MS);
if (!xSemaphoreTake(gettime_start_sema, 1000 / portTICK_PERIOD_MS)) {
TEST_FAIL_MESSAGE("gettime_start_semaphore not released by test task");
for (int i = 0; i < 2; ++i) {
if (!xSemaphoreTake(exit_sema[i], 2100/portTICK_PERIOD_MS)) {
TEST_FAIL_MESSAGE("exit_sema not released by test task");
}
}
if (!xSemaphoreTake(adjtime_continue_sema, 1000 / portTICK_PERIOD_MS)) {
TEST_FAIL_MESSAGE("adjtime_continue_semaphore not released by test task");
end_measure(&sys_time_us[1], &real_time_us[1]);
result_adjtime_correction_us[0] = calc_correction("main", sys_time_us, real_time_us);
int64_t delta_us = result_adjtime_correction_us[0] - result_adjtime_correction_us[1];
printf("\nresult of adjtime correction: %lli us, %lli us. delta = %lli us\n", result_adjtime_correction_us[0], result_adjtime_correction_us[1], delta_us);
TEST_ASSERT_INT_WITHIN(100, 0, delta_us);
for (int i = 0; i < 2; ++i) {
vSemaphoreDelete(exit_sema[i]);
}
vSemaphoreDelete(gettime_start_sema);
vSemaphoreDelete(adjtime_continue_sema);
}
#endif

View File

@ -132,7 +132,7 @@ static uint64_t adjust_boot_time()
// and the correction will be equal to (1_000_000us >> 6) = 15_625 us.
// The minimum possible correction step can be (64us >> 6) = 1us.
// Example: if the time error is 1 second, then it will be compensate for 1 sec / 0,015625 = 64 seconds.
int64_t correction = (since_boot - adjtime_start) >> ADJTIME_CORRECTION_FACTOR;
int64_t correction = (since_boot >> ADJTIME_CORRECTION_FACTOR) - (adjtime_start >> ADJTIME_CORRECTION_FACTOR);
if (correction > 0) {
adjtime_start = since_boot;
if (adjtime_total_correction < 0) {