From patchwork Fri Jun 5 02:46:36 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Wen-Chi Yang X-Patchwork-Id: 5151 Return-Path: X-Original-To: patchwork@dpdk.org Delivered-To: patchwork@dpdk.org Received: from [92.243.14.124] (localhost [IPv6:::1]) by dpdk.org (Postfix) with ESMTP id 1BD645A06; Fri, 5 Jun 2015 04:46:44 +0200 (CEST) Received: from mail-pa0-f41.google.com (mail-pa0-f41.google.com [209.85.220.41]) by dpdk.org (Postfix) with ESMTP id 9146158DD for ; Fri, 5 Jun 2015 04:46:42 +0200 (CEST) Received: by padj3 with SMTP id j3so41235668pad.0 for ; Thu, 04 Jun 2015 19:46:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=from:to:cc:subject:date:message-id; bh=WTJGCUH3xYQgjp5G7ldE3+6HKQQ2wmfV+T5fVQHGlDQ=; b=I8d91BFpGL4Iu7+Qchj10831yDW1pMhSaY45gTiL5rKCkCnsMPM1cCiuPToxmWMo0y VRk6yIU+eVNm8XA6tQb8wVH8lyhRLoU9FaafeDo9A+JFUiWoHlJpvP4kKcyegBCkI+6+ q63QeZ1w1DlgpFx9KBU1imDdnszTjYBM1/2EeH5T3Hscp94NYXW0UGpon+QDflDIxA6M u0NidWMmALS6nCXzWiyUUizVWW7PW+B/G187i96xS3IMAXThb3rDVeHzceGjYveP8mif IV1OkLsIfdTgEqnsOTku5aLMiJzLVXxl+JPyGU6mMDPBeX87p9O3LblxUjpoIaRv81Vo 3u1Q== X-Received: by 10.66.154.111 with SMTP id vn15mr1981146pab.108.1433472401131; Thu, 04 Jun 2015 19:46:41 -0700 (PDT) Received: from selmon-VirtualBox64.client.tw.trendnet.org (61-222-220-134.HINET-IP.hinet.net. [61.222.220.134]) by mx.google.com with ESMTPSA id cu8sm5134711pad.1.2015.06.04.19.46.39 (version=TLSv1.2 cipher=ECDHE-RSA-AES128-SHA bits=128/128); Thu, 04 Jun 2015 19:46:40 -0700 (PDT) From: Wen-Chi Yang To: dev@dpdk.org Date: Fri, 5 Jun 2015 10:46:36 +0800 Message-Id: <1433472396-18852-1-git-send-email-wolkayang@gmail.com> X-Mailer: git-send-email 1.9.1 Subject: [dpdk-dev] [PATCH] rte_alarm: modify it to make it not to be affected by discontinuous jumps in the system time X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.15 Precedence: list List-Id: patches and discussions about DPDK List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Due to eal_alarm_callback() and rte_eal_alarm_set() use gettimeofday() to get the current time, and gettimeofday() is affected by jumps. For example, set up a rte_alarm which will be triggerd next second ( current time + 1 second) by rte_eal_alarm_set(). And the callback function of this rte_alarm sets up another rte_alarm which will be triggered next second (current time + 2 second). Once we change the system time when the callback function is triggered, it is possiblb that rte alarm functionalities work out of expectation. Replace gettimeofday() with clock_gettime(CLOCK_MONOTONIC_RAW, &now) could avoid this phenomenon. Signed-off-by: Wen-Chi Yang Acked-by: Stephen Hemminger --- lib/librte_eal/linuxapp/eal/eal_alarm.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/lib/librte_eal/linuxapp/eal/eal_alarm.c b/lib/librte_eal/linuxapp/eal/eal_alarm.c index a0eae1e..ff57323 100644 --- a/lib/librte_eal/linuxapp/eal/eal_alarm.c +++ b/lib/librte_eal/linuxapp/eal/eal_alarm.c @@ -99,14 +99,14 @@ static void eal_alarm_callback(struct rte_intr_handle *hdl __rte_unused, void *arg __rte_unused) { - struct timeval now; + struct timespec now; struct alarm_entry *ap; rte_spinlock_lock(&alarm_list_lk); while ((ap = LIST_FIRST(&alarm_list)) !=NULL && - gettimeofday(&now, NULL) == 0 && + clock_gettime(CLOCK_MONOTONIC_RAW, &now) == 0 && (ap->time.tv_sec < now.tv_sec || (ap->time.tv_sec == now.tv_sec && - ap->time.tv_usec <= now.tv_usec))){ + (ap->time.tv_usec * NS_PER_US) <= now.tv_nsec))) { ap->executing = 1; ap->executing_id = pthread_self(); rte_spinlock_unlock(&alarm_list_lk); @@ -126,11 +126,11 @@ eal_alarm_callback(struct rte_intr_handle *hdl __rte_unused, atime.it_value.tv_sec = ap->time.tv_sec; atime.it_value.tv_nsec = ap->time.tv_usec * NS_PER_US; /* perform borrow for subtraction if necessary */ - if (now.tv_usec > ap->time.tv_usec) + if (now.tv_nsec > (ap->time.tv_usec * NS_PER_US)) atime.it_value.tv_sec--, atime.it_value.tv_nsec += US_PER_S * NS_PER_US; atime.it_value.tv_sec -= now.tv_sec; - atime.it_value.tv_nsec -= now.tv_usec * NS_PER_US; + atime.it_value.tv_nsec -= now.tv_nsec; timerfd_settime(intr_handle.fd, 0, &atime, NULL); } rte_spinlock_unlock(&alarm_list_lk); @@ -139,7 +139,7 @@ eal_alarm_callback(struct rte_intr_handle *hdl __rte_unused, int rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) { - struct timeval now; + struct timespec now; int ret = 0; struct alarm_entry *ap, *new_alarm; @@ -152,12 +152,12 @@ rte_eal_alarm_set(uint64_t us, rte_eal_alarm_callback cb_fn, void *cb_arg) return -ENOMEM; /* use current time to calculate absolute time of alarm */ - gettimeofday(&now, NULL); + clock_gettime(CLOCK_MONOTONIC_RAW, &now); new_alarm->cb_fn = cb_fn; new_alarm->cb_arg = cb_arg; - new_alarm->time.tv_usec = (now.tv_usec + us) % US_PER_S; - new_alarm->time.tv_sec = now.tv_sec + ((now.tv_usec + us) / US_PER_S); + new_alarm->time.tv_usec = ((now.tv_nsec / NS_PER_US) + us) % US_PER_S; + new_alarm->time.tv_sec = now.tv_sec + (((now.tv_nsec / NS_PER_US) + us) / US_PER_S); rte_spinlock_lock(&alarm_list_lk); if (!handler_registered) {