From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Bruno Haible Newsgroups: gmane.comp.lib.gnulib.bugs,gmane.emacs.devel Subject: Re: boot time on Linux Date: Thu, 10 Aug 2023 02:14:20 +0200 Message-ID: <5962135.d8TiXCRyrx@nimes> References: <87tttmpt5h.fsf@turtle.gmx.de> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart3550542.lMWV1o5SAq" Content-Transfer-Encoding: 7Bit Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="12942"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Robert Pluim , bug-gnulib@gnu.org, =?ISO-8859-1?Q?P=E1draig?= Brady , Sven Joachim , 64937@debbugs.gnu.org, Natanael Copa , Emacs-devel@gnu.org, Thorsten Kukuk To: Paul Eggert , Po Lu Original-X-From: bug-gnulib-bounces+gnu-bug-gnulib=m.gmane-mx.org@gnu.org Thu Aug 10 02:14:46 2023 Return-path: Envelope-to: gnu-bug-gnulib@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1qTtK9-00032h-2h for gnu-bug-gnulib@m.gmane-mx.org; Thu, 10 Aug 2023 02:14:45 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qTtJy-0005aE-7h; Wed, 09 Aug 2023 20:14:34 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qTtJw-0005Zm-UO; Wed, 09 Aug 2023 20:14:32 -0400 Original-Received: from mo4-p01-ob.smtp.rzone.de ([85.215.255.52]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qTtJt-0001Il-Gl; Wed, 09 Aug 2023 20:14:32 -0400 ARC-Seal: i=1; a=rsa-sha256; t=1691626460; cv=none; d=strato.com; s=strato-dkim-0002; b=FD17Lx76vLPfA6F/EvDTRClHLfYWrIh++nVMAR+t6swNqYUu4xcxIwNzj/y9gZzCjd xk0+agC4x4y4HAxBtNhKkecq1Gt+cv63YKxs4UTcezCIu/yVv/MAnHFivq761YikKwwb 2O5wiwCW18r1jamYH8pRBhf3N5k7TAmfuM+MQC9VC6ZmUz/83quf6DT/Wrabp0FmKutM /I5p8VliSStxQ8HSx/gu+UVfJZyeZF7/3XcDoYO1mPLpNH1yBTVPivUY1mQ9mF4Z0rsy qIQegF5kHH4lYgntaxeXmqSXYNjZfg7wxrOHEGghgK5yT/g6UWC7/RYKBCRei84nNQ0M nd4g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; t=1691626460; s=strato-dkim-0002; d=strato.com; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=IZ6zbs9PzkrZ59SIF4yZkD0H3+yqEmYwkZK0e+Qfvwc=; b=JcBVS7HpeAyhA9o8F9sjGq+Xjq6u5FJiiBU3U+O0RKHRUywtGs6Gw3KcyDdt25vyR+ UELxyeiO3kwhoItvFW4LYFCyUjy0O4VKm8sjlrXyc/7UZOjwJdZ+DqaHlqNP6gAXzA6t a+aBPRtz0cND+t5q2py/u/GVmLcnKTU7hBLfW3QLr0Pjoc8V5K9LnxHkpsCXlT6uE3PJ hm3aDaX3i5sMMrPvnb+6SBfIz6BbA93zezXbX9yEz18MM+fHh8SlqhVdjsRmaa40LOMe gWsQtTghGAqWyOZD1N2EWHK/oUEgAxAdngKhRXTjL6Z8YS3buVV4ghDnb5sAmisgsls1 7Kmw== ARC-Authentication-Results: i=1; strato.com; arc=none; dkim=none X-RZG-CLASS-ID: mo01 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; t=1691626460; s=strato-dkim-0002; d=clisp.org; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=IZ6zbs9PzkrZ59SIF4yZkD0H3+yqEmYwkZK0e+Qfvwc=; b=Qm2wym0ihlJ70hYGGb+0MTeELNtUlsQzU9JuqX3t+3oxBRwQquYg05r4RrX01dgEEy kampnJoyfqDDarzcJN0TyMaaUxaK7GTbpm422XdRW6FFA8/7vGopSxn0aPs8QaX3wzxi kbQWvG+95GgMqgmLserqHr0zHCsA6AWugyeSTI+FCbjCWljDFFylegbCr/a7B3aA3Rm7 bXrmLs4xnT9i0naJwItssmX7M8lytvlY+q6gD1m3eqwrSEsTPy55Z9QUgWJkr8GX6R9r mxp8tXshj6XowmSXpCsPaWJnziKrt5qHKdvBAfxrJcDvmoSYXzb0zYrgRFVcrNHWGZgz 3qbw== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; t=1691626460; s=strato-dkim-0003; d=clisp.org; h=References:In-Reply-To:Message-ID:Date:Subject:Cc:To:From:Cc:Date: From:Subject:Sender; bh=IZ6zbs9PzkrZ59SIF4yZkD0H3+yqEmYwkZK0e+Qfvwc=; b=FZQ0SjaAxokAs0MY7P7e7tRf4FPfg11hWHCYUTq93HIDSfBlfw0d9CiEMSNDdLp2FC 5xzPq+qavDPDXkV4jkBg== X-RZG-AUTH: ":Ln4Re0+Ic/6oZXR1YgKryK8brlshOcZlIWs+iCP5vnk6shH0WWb0LN8XZoH94zq68+3cfpOV1PzvI5g5gRFjtxIUOVst8mjP7Q==" Original-Received: from nimes.localnet by smtp.strato.de (RZmta 49.6.6 AUTH) with ESMTPSA id x129eaz7A0EKsNt (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256 bits)) (Client did not present a certificate); Thu, 10 Aug 2023 02:14:20 +0200 (CEST) In-Reply-To: Received-SPF: none client-ip=85.215.255.52; envelope-from=bruno@clisp.org; helo=mo4-p01-ob.smtp.rzone.de X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H5=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: bug-gnulib@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gnulib discussion list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnulib-bounces+gnu-bug-gnulib=m.gmane-mx.org@gnu.org Original-Sender: bug-gnulib-bounces+gnu-bug-gnulib=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.comp.lib.gnulib.bugs:48134 gmane.emacs.devel:308495 Archived-At: This is a multi-part message in MIME format. --nextPart3550542.lMWV1o5SAq Content-Transfer-Encoding: 7Bit Content-Type: text/plain; charset="us-ascii" Po Lu wrote: > > Also, I don't know how Android records boot time so I'll cc this to Po > > Lu, the main developer for Emacs on Android. > > The boot time is off limits to user programs on Android, for security > reasons. No, it isn't. The attached file, when compiled and run under Termux (which doesn't have particular permissions), prints e.g.: from clock : 1691616762.476870660 = 2023-08-09 21:32:42.476870660 from sysinfo: 1691616762.329261637 = 2023-08-09 21:32:42.329261637 Note that this uses the kernel's uptime counter, so it will not work well when the user changes the current time manually. But this is rare on Android. Bruno --nextPart3550542.lMWV1o5SAq Content-Disposition: attachment; filename="foo.c" Content-Transfer-Encoding: 7Bit Content-Type: text/x-csrc; charset="UTF-8"; name="foo.c" #include #include #include #include #include static const char * as_time_string (time_t tim) { struct tm *gmt = gmtime (&tim); static char timbuf[100]; if (gmt == NULL || strftime (timbuf, sizeof (timbuf), "%Y-%m-%d %H:%M:%S", gmt) == 0) strcpy (timbuf, "---"); return timbuf; } int main () { /* clock() returns the uptime with a resolution of ca. 1 usec. */ struct timespec ts_now; struct timespec up; if (clock_gettime (CLOCK_REALTIME, &ts_now) == 0 && clock_gettime (CLOCK_BOOTTIME, &up) == 0) { struct timespec result = ts_now; if (result.tv_nsec < up.tv_nsec) { result.tv_nsec += 1000000000; result.tv_sec -= 1; } result.tv_sec -= up.tv_sec; result.tv_nsec -= up.tv_nsec; printf ("from clock : %d.%09d = %s.%09d\n", (int) result.tv_sec, (int) result.tv_nsec, as_time_string (result.tv_sec), (int) result.tv_nsec); } /* /proc/uptime contains the uptime with a resolution of 0.01 sec. */ FILE *fp = fopen ("/proc/uptime", "re"); if (fp != NULL) { char buf[32 + 1]; size_t n = fread (buf, 1, sizeof (buf) - 1, fp); fclose (fp); if (n > 0) { buf[n] = '\0'; /* buf now contains two values: the uptime and the idle time. */ char *endptr; double uptime = strtod (buf, &endptr); if (endptr > buf) { struct timespec result; if (clock_gettime (CLOCK_REALTIME, &result) == 0) { time_t uptime_sec = uptime; struct timespec up = { .tv_sec = uptime_sec, .tv_nsec = (uptime - uptime_sec) * 1e9 + 0.5 }; if (result.tv_nsec < up.tv_nsec) { result.tv_nsec += 1000000000; result.tv_sec -= 1; } result.tv_sec -= up.tv_sec; result.tv_nsec -= up.tv_nsec; printf ("from /proc : %d.%09d = %s.%09d\n", (int) result.tv_sec, (int) result.tv_nsec, as_time_string (result.tv_sec), (int) result.tv_nsec); } } } } /* The sysinfo call returns the uptime with a resolution of 1 sec only. */ struct sysinfo info; if (sysinfo (&info) >= 0) { struct timespec result; if (clock_gettime (CLOCK_REALTIME, &result) == 0) { result.tv_sec -= info.uptime; printf ("from sysinfo: %d.%09d = %s.%09d\n", (int) result.tv_sec, (int) result.tv_nsec, as_time_string (result.tv_sec), (int) result.tv_nsec); } } return 0; } --nextPart3550542.lMWV1o5SAq--