From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on dcvr.yhbt.net X-Spam-Level: X-Spam-ASN: AS31976 209.132.180.0/23 X-Spam-Status: No, score=-4.0 required=3.0 tests=AWL,BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_EF,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RCVD_IN_DNSWL_MED,SPF_HELO_PASS,SPF_PASS shortcircuit=no autolearn=ham autolearn_force=no version=3.4.2 Received: from sourceware.org (server1.sourceware.org [209.132.180.131]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dcvr.yhbt.net (Postfix) with ESMTPS id CF61D1F461 for ; Wed, 28 Aug 2019 21:12:17 +0000 (UTC) DomainKey-Signature: a=rsa-sha1; c=nofws; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:subject:to:cc:references:from:message-id:date :mime-version:in-reply-to:content-type; q=dns; s=default; b=MhRa L3YMzt1P/UD4VOQ1oOXxwUU9DjHDBbuqhH/gkRqPASJ0Hm8hAAyGVK2F5Bm2TjlQ 0L+UcMHXjlaQDH0YHz5ad1nm9k9+wkfKEqmX7vz9+obaW4Cv5rKQ4M0lcVjgyxQK R7cS1Ggb4fEKKBtCwrBxlD2gKRlLBPM50GTa6HE= DKIM-Signature: v=1; a=rsa-sha1; c=relaxed; d=sourceware.org; h=list-id :list-unsubscribe:list-subscribe:list-archive:list-post :list-help:sender:subject:to:cc:references:from:message-id:date :mime-version:in-reply-to:content-type; s=default; bh=d5k/629gL5 am/IxV7gaOa32B8sg=; b=l04pa0sCXaDIhEdQnXjkGbCTsX/qjgUJvRM5jrF9kz bKYvPJBAmPP5463yG61S+Sh00bRwAf0i91ivGNDINWXJ1HVULIAri0wn7LXN28Nv xy+gF11O3/u8yM2lx4mSTs5jU6q1L22lsdVviqqfR3YZLFO++MiAW5wqhDa1YA/0 M= Received: (qmail 45198 invoked by alias); 28 Aug 2019 21:12:14 -0000 Mailing-List: contact libc-alpha-help@sourceware.org; run by ezmlm Precedence: bulk List-Id: List-Unsubscribe: List-Subscribe: List-Archive: List-Post: List-Help: , Sender: libc-alpha-owner@sourceware.org Received: (qmail 45102 invoked by uid 89); 28 Aug 2019 21:12:14 -0000 Authentication-Results: sourceware.org; auth=none X-HELO: zimbra.cs.ucla.edu Subject: Re: [PATCH v2 05/10] Use clock_gettime to implement time. To: Florian Weimer Cc: Zack Weinberg , libc-alpha@sourceware.org, Joseph Myers , Lukasz Majewski , Alistair Francis , Stepan Golosunov , Arnd Bergmann , Adhemerval Zanella , Samuel Thibault References: <20190828153236.18229-1-zackw@panix.com> <20190828153236.18229-6-zackw@panix.com> <87muftb1fk.fsf@oldenburg2.str.redhat.com> <87sgpl9h2o.fsf@oldenburg2.str.redhat.com> From: Paul Eggert Message-ID: <321d1bd0-a9f6-310b-5412-a023d813e90f@cs.ucla.edu> Date: Wed, 28 Aug 2019 14:12:10 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <87sgpl9h2o.fsf@oldenburg2.str.redhat.com> Content-Type: multipart/mixed; boundary="------------9A0227A702FD84A44DED69B0" This is a multi-part message in MIME format. --------------9A0227A702FD84A44DED69B0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Florian Weimer wrote: > time can definitely return the > value of a variable that is incremented periodically from the timer > interrupt. Is that variable the one that CLOCK_REALTIME_COARSE uses? If so, and if we're going to replace calls to 'time' with calls to 'clock_realtime', we can do either of the following: * Use CLOCK_REALTIME_COARSE. This takes less CPU time and its behavior better matches what the current glibc does. * Use CLOCK_REALTIME. This will lessen bugs due to naive code (quite possibly some code within glibc!) which assumes that 'time (0)' and clock_gettime (CLOCK_REALTIME, ...)' use the same clock. It sounds you're leaning towards (1) and I'm inclined to agree. However, shouldn't the manual say that 'time' does not necessarily agree with CLOCK_REALTIME? The current behavior is a trap for the unwary. To illustrate the problems with naive code, see the attached program. On my Fedora 30 x86-64 desktop, './a.out' outputs this: clock_gettime (CLOCK_REALTIME, ...) yielded a seconds count of 1567026298; then time (0) yielded a seconds count of 1567026297, which was 1 s earlier. Time warp! In contrast, './a.out 1' (which uses CLOCK_REALTIME_COARSE) finds no discrepancies in 2**32 attempts. --------------9A0227A702FD84A44DED69B0 Content-Type: text/x-csrc; name="time.c" Content-Transfer-Encoding: 7bit Content-Disposition: attachment; filename="time.c" #include #include int main (int argc, char **argv) { int clock = argc == 1 ? CLOCK_REALTIME : CLOCK_REALTIME_COARSE; for (unsigned int i = 1; i; i++) { struct timespec ts; if (clock_gettime (clock, &ts)) { perror ("clock_gettime"); return 1; } time_t x = time (0); if (x < ts.tv_sec) { printf ("clock_gettime (%s, ...)" " yielded a seconds count of %ld;\n" "then time (0) yielded a seconds count of %ld," " which was %ld s earlier.\n" "Time warp!\n", (clock == CLOCK_REALTIME ? "CLOCK_REALTIME" : "CLOCK_REALTIME_COARSE"), (long) ts.tv_sec, (long) x, (long) (ts.tv_sec - x)); return 1; } } return 0; } --------------9A0227A702FD84A44DED69B0--