From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from smtp51.i.mail.ru (smtp51.i.mail.ru [94.100.177.111]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dev.tarantool.org (Postfix) with ESMTPS id 14D8D4696C2 for ; Fri, 5 Jun 2020 02:43:24 +0300 (MSK) From: Vladislav Shpilevoy Date: Fri, 5 Jun 2020 01:43:10 +0200 Message-Id: <0403d06f3846cd3072f14e51c64d7d8f93bc2d57.1591313754.git.v.shpilevoy@tarantool.org> In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Subject: [Tarantool-patches] [PATCH 03/11] test: avoid usleep() usage for error injections List-Id: Tarantool development patches List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , To: tarantool-patches@dev.tarantool.org, tsafin@tarantool.org, alyapunov@tarantool.org Some error injections use usleep() to put the current thread in sleep. For example, to simulate, that WAL thread is slow. A few of them allowed to pass custom number of microseconds to usleep, in form: usleep(injection->dvalue * 1000000); Assuming, that dvalue is a number of seconds. But usleep argument is uint32_t, at least on Mac (it is useconds_t, whose size is 4). It means, that too big dvalue easily overflows it. The patch makes it use nanosleep(), in a new wrapper: thread_sleep(). It takes a double value, and does not truncate it to 4 bytes. The overflow was the case for ERRINJ_VY_READ_PAGE_TIMEOUT = 9000 in test/vinyl/errinj_vylog.test.lua. And ERRINJ_VY_RUN_WRITE_STMT_TIMEOUT = 9000 in test/vinyl/errinj.test.lua. Part of #4609 --- src/box/vy_run.c | 2 +- src/box/vy_scheduler.c | 2 +- src/lib/core/util.c | 17 +++++++++++++++++ src/trivia/util.h | 7 +++++++ 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/box/vy_run.c b/src/box/vy_run.c index f8b096f6b..54cf028d0 100644 --- a/src/box/vy_run.c +++ b/src/box/vy_run.c @@ -914,7 +914,7 @@ vy_page_read(struct vy_page *page, const struct vy_page_info *page_info, struct errinj *inj = errinj(ERRINJ_VY_READ_PAGE_TIMEOUT, ERRINJ_DOUBLE); if (inj != NULL && inj->dparam > 0) - usleep(inj->dparam * 1000000); + thread_sleep(inj->dparam); ERROR_INJECT_SLEEP(ERRINJ_VY_READ_PAGE_DELAY); diff --git a/src/box/vy_scheduler.c b/src/box/vy_scheduler.c index cf58d5f60..a0b7ad006 100644 --- a/src/box/vy_scheduler.c +++ b/src/box/vy_scheduler.c @@ -1079,7 +1079,7 @@ vy_task_write_run(struct vy_task *task, bool no_compression) struct errinj *inj = errinj(ERRINJ_VY_RUN_WRITE_STMT_TIMEOUT, ERRINJ_DOUBLE); if (inj != NULL && inj->dparam > 0) - usleep(inj->dparam * 1000000); + thread_sleep(inj->dparam); rc = vy_run_writer_append_stmt(&writer, entry); if (rc != 0) diff --git a/src/lib/core/util.c b/src/lib/core/util.c index a65bc496c..d7f2344ed 100644 --- a/src/lib/core/util.c +++ b/src/lib/core/util.c @@ -407,3 +407,20 @@ double_compare_nint64(double lhs, int64_t rhs, int k) } return -k; } + +void +thread_sleep(double sec) +{ + uint64_t ns = (uint64_t)(sec * 1000000000); + assert(ns > 0); + struct timespec req; + struct timespec next; + req.tv_sec = ns / 1000000000; + req.tv_nsec = ns % 1000000000; + assert(req.tv_nsec < 1000000000); + int rc; + while ((rc = nanosleep(&req, &next)) == -1 && errno == EINTR) + req = next; + assert(rc == 0); + (void)rc; +} diff --git a/src/trivia/util.h b/src/trivia/util.h index 973c9df33..29c7f0194 100644 --- a/src/trivia/util.h +++ b/src/trivia/util.h @@ -534,6 +534,13 @@ double_compare_int64(double lhs, int64_t rhs, int k) return double_compare_nint64(lhs, rhs, k); } +/** + * Put the current thread in sleep for the given number of + * seconds. + */ +void +thread_sleep(double sec); + #if !defined(__cplusplus) && !defined(static_assert) # define static_assert _Static_assert #endif -- 2.21.1 (Apple Git-122.3)