From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-wr1-f42.google.com (mail-wr1-f42.google.com [209.85.221.42]) by sourceware.org (Postfix) with ESMTPS id 59603384003A for ; Mon, 2 Aug 2021 16:46:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 59603384003A Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=gcc.gnu.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-wr1-f42.google.com with SMTP id c16so22193292wrp.13 for ; Mon, 02 Aug 2021 09:46:20 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:mime-version :content-transfer-encoding; bh=v9lM+XfSA2uC+b9DGkNYoaIz8d7/eQ7w426QpUXfhNo=; b=Yy/l3lkdmx7Zw0zuOowQG6hG7LQmqt4qhfVV/xBGo/XW2c6m3Tg8IX+lusFcVm7I2k ELAVXPDFvbJPmQRw5nCzYy/0uEA55vGl1gPCC08SDNdtfTTLvk5MwrDyQvtlKMD79VNA 0SB86Sr/ATXqsx2xZ4swiyJBZmyvPpG0LCt0GPQXaHhdse3/2SJB2JAByrvs1nW0hnA3 TygLaMYGCiluuMMVQXZ4xSJ9P5W4amUQXFqfTiTkYwMqe6VeNIW6g3sMPolxhu7q0aA8 oeq1kGEtFhVZF6/9y2u5dq99nF0qOuIXtKJNk6YLqH6WstVF9SIMoh2iT4WLcD+LP7Ms c0qg== X-Gm-Message-State: AOAM5309ISVRBvOGNbw+s+1G8FXf8IavofDVB8MwTEmNsE7uC9oBZPVl EBygSrssxWGv29Ubks1Ogv8U+h+PpfFOKw== X-Google-Smtp-Source: ABdhPJzpC7p+0zcLEjPsuDO6FJSdi8McsfjzNGuaz136bLAhmgoNQ1npWjDFukxPcOJAC+igZBEGOw== X-Received: by 2002:a05:6000:248:: with SMTP id m8mr19003934wrz.145.1627922779220; Mon, 02 Aug 2021 09:46:19 -0700 (PDT) Received: from beast.fritz.box (62-178-148-172.cable.dynamic.surfer.at. [62.178.148.172]) by smtp.gmail.com with ESMTPSA id r133sm11542090wma.18.2021.08.02.09.46.18 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 02 Aug 2021 09:46:18 -0700 (PDT) From: Christoph Muellner To: newlib@sourceware.org Cc: Kito Cheng , Christoph Muellner , Christoph Muellner Subject: [PATCH] RISC-V: Reliably initialize t0 in _times() Date: Mon, 2 Aug 2021 18:46:16 +0200 Message-Id: <20210802164616.4099311-1-cmuellner@gcc.gnu.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-12.6 required=5.0 tests=BAYES_00, FREEMAIL_ENVFROM_END_DIGIT, FREEMAIL_FORGED_FROMDOMAIN, FREEMAIL_FROM, GIT_PATCH_0, HEADER_FROM_DIFFERENT_DOMAINS, KAM_DMARC_STATUS, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=unavailable autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: newlib@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Newlib mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 02 Aug 2021 16:46:21 -0000 From: Christoph Muellner The current implementation does not reliably initialize t0 once. Additionally the initialization requires two calls to _gettimeofday(). Let's sacrifice a byte to keep the initialization status and reduce the maximum number of calls to _gettimeofday(). This has caused issues in an application that invokes clock(). The problematic situation is as follows: 1) The program calls clock() which calls _times(). 2) _gettimeofday(&t0, 0) puts 0 in t0.tv_usec (because less than 1 us has elapsed since the beginning of time). 3) _gettimeofday(&t, 0) puts 1 in t.tv_usec (since now more than 1 us has elapsed since the beginning of time). 4) That call to clock() returns 1 (the value from step 3 minus the value in step 2). 5) The program does a second call to clock(). 6) The code above still sees 0 in t0 so it tries to update t0 again and _gettimeofday(&t0, 0) puts 1 in t0.tv_usec. 7) The _gettimeofday(&t, 0) puts 1 in t.tv_usec (since less than 1us has elapsed since step 3). 8) clock() returns 0 (step 7 minus step 6) and indicates that time is moving backwards. Signed-off-by: Christoph Muellner --- libgloss/riscv/sys_times.c | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/libgloss/riscv/sys_times.c b/libgloss/riscv/sys_times.c index fc8133ade..a029bcfb5 100644 --- a/libgloss/riscv/sys_times.c +++ b/libgloss/riscv/sys_times.c @@ -21,14 +21,19 @@ extern int _gettimeofday(struct timeval *, void *); clock_t _times(struct tms *buf) { - // when called for the first time, initialize t0 + static char initialized; static struct timeval t0; - if (t0.tv_sec == 0 && t0.tv_usec == 0) - _gettimeofday (&t0, 0); - struct timeval t; + _gettimeofday (&t, 0); + // when called for the first time, initialize t0 + if (!initialized) { + t0.tv_sec = t.tv_sec; + t0.tv_usec = t.tv_usec; + initialized = 1; + } + long long utime = (t.tv_sec - t0.tv_sec) * 1000000 + (t.tv_usec - t0.tv_usec); buf->tms_utime = utime * CLOCKS_PER_SEC / 1000000; buf->tms_stime = buf->tms_cstime = buf->tms_cutime = 0; -- 2.31.1