From 477112ebc3497e9529cc06b24134d85d17c642ee Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Sun, 15 Jun 2025 15:02:07 -0700 Subject: [PATCH] process-attributes now uses get_boot_time MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit I noticed this issue while looking into Bug#63496. GNU Emacs does not infer the boot time consistently on GNU/Linux and similar platforms: android_notifications_notify_1 and get_boot_sec use Gnulib’s boot-time module, but process-attributes has idiosyncratic code that evidently predates that module. The idiosyncratic code returns an unstable etime, which is a minor bug. Simplify process-attributes by just using Gnulib. This returns a stable etime. It may also fix Bug#63496; if not, we can fix Gnulib later. * src/sysdep.c: Include . (get_up_time) [GNU_LINUX || CYGWIN || __ANDROID__]: Remove. (system_process_attributes) [GNU_LINUX || CYGWIN || __ANDROID__]: Use get_boot_time instead of get_up_time. --- src/sysdep.c | 41 +++++------------------------------------ 1 file changed, 5 insertions(+), 36 deletions(-) diff --git a/src/sysdep.c b/src/sysdep.c index 042de2acf80..71108f50db1 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -30,6 +30,7 @@ along with GNU Emacs. If not, see . */ #include #include +#include #include #include #include @@ -3453,39 +3454,6 @@ put_jiffies (Lisp_Object attrs, Lisp_Object propname, return Fcons (Fcons (propname, time_from_jiffies (ticks, hz, Qnil)), attrs); } -static Lisp_Object -get_up_time (void) -{ - FILE *fup; - Lisp_Object up = Qnil; - - block_input (); - fup = emacs_fopen ("/proc/uptime", "r"); - - if (fup) - { - unsigned long long upsec; - EMACS_UINT upfrac; - int upfrac_start, upfrac_end; - - if (fscanf (fup, "%llu.%n%"pI"u%n", - &upsec, &upfrac_start, &upfrac, &upfrac_end) - == 2) - { - EMACS_INT hz = 1; - for (int i = upfrac_start; i < upfrac_end; i++) - hz *= 10; - Lisp_Object sec = make_uint (upsec); - Lisp_Object subsec = Fcons (make_fixnum (upfrac), make_fixnum (hz)); - up = Ftime_add (sec, subsec); - } - emacs_fclose (fup); - } - unblock_input (); - - return up; -} - # if defined GNU_LINUX || defined __ANDROID__ #define MAJOR(d) (((unsigned)(d) >> 8) & 0xfff) #define MINOR(d) (((unsigned)(d) & 0xff) | (((unsigned)(d) & 0xfff00000) >> 12)) @@ -3702,11 +3670,12 @@ system_process_attributes (Lisp_Object pid) attrs = put_jiffies (attrs, Qcstime, cstime, hz); attrs = put_jiffies (attrs, Qctime, cstime + cutime, hz); - Lisp_Object uptime = get_up_time (); - if (!NILP (uptime)) + struct timespec bt; + if (get_boot_time (&bt) == 0) { + Lisp_Object boot = Ftime_convert (timespec_to_lisp (bt), hz); Lisp_Object now = Ftime_convert (Qnil, hz); - Lisp_Object boot = Ftime_subtract (now, uptime); + Lisp_Object uptime = Ftime_subtract (now, boot); Lisp_Object tstart = time_from_jiffies (start, hz, hz); Lisp_Object lstart = Ftime_convert (Ftime_add (boot, tstart), Qnil);