From: Bruno Haible <bruno@clisp.org>
To: Paul Eggert <eggert@cs.ucla.edu>, Po Lu <luangruo@yahoo.com>
Cc: "Robert Pluim" <rpluim@gmail.com>,
bug-gnulib@gnu.org, "Pádraig Brady" <P@draigbrady.com>,
"Sven Joachim" <svenjoac@gmx.de>,
64937@debbugs.gnu.org, "Natanael Copa" <natanael.copa@gmail.com>,
Emacs-devel@gnu.org, "Thorsten Kukuk" <kukuk@suse.com>
Subject: Re: boot time on Linux
Date: Thu, 10 Aug 2023 02:14:20 +0200 [thread overview]
Message-ID: <5962135.d8TiXCRyrx@nimes> (raw)
In-Reply-To: <s0d5y5n93j2.fsf@yahoo.com>
[-- Attachment #1: Type: text/plain, Size: 645 bytes --]
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
[-- Attachment #2: foo.c --]
[-- Type: text/x-csrc, Size: 2906 bytes --]
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <sys/sysinfo.h>
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;
}
next prev parent reply other threads:[~2023-08-10 0:14 UTC|newest]
Thread overview: 17+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <87tttmpt5h.fsf@turtle.gmx.de>
[not found] ` <20230808173430.GA27131@suse.com>
[not found] ` <26226778.6c9BZvbsD2@nimes>
[not found] ` <3732835.vtg8X0x55z@nimes>
2023-08-09 19:31 ` boot time on Linux Paul Eggert
2023-08-09 21:06 ` Bruno Haible
2023-08-09 23:53 ` Po Lu
2023-08-10 0:14 ` Bruno Haible [this message]
2023-08-10 2:14 ` Po Lu
2023-08-10 6:22 ` Paul Eggert
2023-08-10 6:58 ` Po Lu
2023-08-10 10:30 ` Bruno Haible
2023-08-10 12:23 ` bug#64937: " Po Lu via GNU coreutils Bug Reports
2023-08-10 12:25 ` Bruno Haible
2023-08-10 13:04 ` Po Lu
2023-08-10 14:12 ` Bruno Haible
2023-08-11 8:27 ` Po Lu
2023-08-10 9:30 ` Natanael Copa
2023-08-10 9:38 ` Po Lu
2023-08-10 10:05 ` Natanael Copa
2023-08-10 15:30 ` Bruno Haible
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=5962135.d8TiXCRyrx@nimes \
--to=bruno@clisp.org \
--cc=64937@debbugs.gnu.org \
--cc=Emacs-devel@gnu.org \
--cc=P@draigbrady.com \
--cc=bug-gnulib@gnu.org \
--cc=eggert@cs.ucla.edu \
--cc=kukuk@suse.com \
--cc=luangruo@yahoo.com \
--cc=natanael.copa@gmail.com \
--cc=rpluim@gmail.com \
--cc=svenjoac@gmx.de \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).