unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Helmut Eller <eller.helmut@gmail.com>
To: Eli Zaretskii <eliz@gnu.org>
Cc: Pip Cet <pipcet@protonmail.com>,
	 gerd.moellmann@gmail.com, spd@toadstyle.org,
	 emacs-devel@gnu.org
Subject: Re: igc, macOS avoiding signals
Date: Fri, 03 Jan 2025 19:37:59 +0100	[thread overview]
Message-ID: <87pll3ivzs.fsf@gmail.com> (raw)
In-Reply-To: <867c7fncom.fsf@gnu.org> (Eli Zaretskii's message of "Tue, 31 Dec 2024 16:34:49 +0200")

[-- Attachment #1: Type: text/plain, Size: 3490 bytes --]

On Tue, Dec 31 2024, Eli Zaretskii wrote:
> We'd need to add a new function to process_pending_signals, which
> would process SIGPROF and maybe also SIGALRM.  The signal handlers for
> those would then only set a flag (not pending_signals, some other
> flag).

I implemented this with the two attached patches.  The trouble is that,
the recorded backtraces are not same.  This can be seen by looking at
the call tree produced by profiler.el and the attached profiler-test.el.
When add_sample is called in the signal handler, then the call tree for
the foo example looks so:

    ...
    1986 100%         main
    1986 100%           record-samples
    1986 100%             foo
    1074  54%               float-time
       0   0%   ...

When add_sample is called from process_pending_signals, it looks like
this:

    ...
    1986 100%         main
    1986 100%           record-samples
    1986 100%             foo
       0   0%   ...

Not the absence of float-time.  The reason for this is, that in
bytecode.c, maybe_quit is called before the function is pushed to the
backtrace with record_in_backtrace.  In the second patch, I moved this
call forward to before the function is popped with lisp_eval_depth--.
With this patch, the call tree includes float-time again:

    ...
    1989 100%         main
    1989 100%           record-samples
    1989 100%             foo
    1981  99%               float-time
       0   0%   ...

However, float-time has now 99% as opposed to 54% in the first call
tree.

A more complex pair of call trees is attached in the files
bar-0.report and bar-2.report.  A significant difference there is
in this section:

     ...
     781  73%                     animate-place-char
      19   1%                       delete-char
      16   1%                       floor
       4   0%                       undo-auto--undoable-change
       4   0%                         undo-auto--boundary-ensure-timer
      96   9%                       insert-char
      14   1%                         undo-auto--undoable-change
       6   0%                           undo-auto--boundary-ensure-timer
       5   0%                       beginning-of-line
     232  21%                       move-to-column
     ...

compared to the version with both patches applied:

     ...
     693  72%                     animate-place-char
      32   3%                       delete-char
      29   3%                       window-start
      43   4%                       insert-char
     309  32%                       move-to-column
     222  23%                       beginning-of-line
       8   0%                       undo-auto--undoable-change
       8   0%                         undo-auto--boundary-ensure-timer
       8   0%                           run-at-time
       8   0%                             timer-set-function
       8   0%                               timerp
       8   0%                                 vectorp
     ...
     
E.g. the percentage attributed to beginning-of-line is quite different
in those two versions (23% and 0%).   

I'm not sure if those differences are acceptable.  I also have no good
idea how to reduce it, except inserting more calls to maybe_quit.

(In eval_sub and Ffuncall, it would also help the profiler to move the
maybe_quit call forward before lisp_eval_depth--. This would only matter
for interpreted functions, not in byte compiled code.  Curiously,
apply_lambda doesn't call maybe_quit at all.)

Helmut



[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-Delay-processing-of-SIGPROF-to-the-next-safepoint.patch --]
[-- Type: text/x-diff, Size: 2359 bytes --]

From ed26a2e8fc92a321f0afeb38c1f88db46ec957a6 Mon Sep 17 00:00:00 2001
From: Helmut Eller <eller.helmut@gmail.com>
Date: Fri, 3 Jan 2025 17:27:10 +0100
Subject: [PATCH 1/2] Delay processing of SIGPROF to the next safepoint

* src/lisp.h (process_pending_profiler_signals): New function.
* src/profiler.c (pending_profiler_signals): New variable.
(handle_profiler_signal): Instead of calling add_sample,
set pending_signals and increment pending_profiler_signals.
(process_pending_profiler_signals): New function.
* src/keyboard.c (process_pending_signals): Call
process_pending_profiler_signals.
---
 src/keyboard.c |  1 +
 src/lisp.h     |  1 +
 src/profiler.c | 17 ++++++++++++++++-
 3 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/src/keyboard.c b/src/keyboard.c
index e875e98fde6..5d6cebdc990 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -8191,6 +8191,7 @@ process_pending_signals (void)
   handle_async_input ();
   do_pending_atimers ();
   do_async_work ();
+  process_pending_profiler_signals ();
 }
 
 /* Undo any number of BLOCK_INPUT calls down to level LEVEL,
diff --git a/src/lisp.h b/src/lisp.h
index 48585c2d8a1..774667a4f9c 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -5938,6 +5938,7 @@ maybe_disable_address_randomization (int argc, char **argv)
 extern void malloc_probe (size_t);
 extern void syms_of_profiler (void);
 extern void mark_profiler (void);
+extern void process_pending_profiler_signals (void);
 
 
 #ifdef DOS_NT
diff --git a/src/profiler.c b/src/profiler.c
index 3db7fe0eb3e..47367982cab 100644
--- a/src/profiler.c
+++ b/src/profiler.c
@@ -387,6 +387,9 @@ add_sample (struct profiler_log *plog, EMACS_INT count)
 /* Hash-table log of CPU profiler.  */
 static struct profiler_log cpu;
 
+/* Number of unprocessed profiler signals. */
+static uintptr_t pending_profiler_signals;
+
 /* The current sampling interval in nanoseconds.  */
 static EMACS_INT current_sampling_interval;
 
@@ -402,7 +405,19 @@ handle_profiler_signal (int signal)
       count += overruns;
     }
 #endif
-  add_sample (&cpu, count);
+  pending_signals = true;
+  pending_profiler_signals += count;
+}
+
+void
+process_pending_profiler_signals (void)
+{
+  uintptr_t count = pending_profiler_signals;
+  if (count)
+    {
+      pending_profiler_signals = 0;
+      add_sample (&cpu, count);
+    }
 }
 
 static void
-- 
2.39.5


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #3: 0002-Call-maybe_quit-at-a-different-point-to-the-help-the.patch --]
[-- Type: text/x-diff, Size: 1154 bytes --]

From ec3227c060f12ca137b5a5bd1e607b922a6dafec Mon Sep 17 00:00:00 2001
From: Helmut Eller <eller.helmut@gmail.com>
Date: Fri, 3 Jan 2025 18:12:41 +0100
Subject: [PATCH 2/2] Call maybe_quit at a different point to the help the
 profiler.

* src/bytecode.c (exec_byte_code): In the docall sequence, move
the to maybe_quit forward immediately before lisp_eval_depth--.
This helps the profiler to see the function that was
interrupted by the SIGPROF signal.
---
 src/bytecode.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/src/bytecode.c b/src/bytecode.c
index 31f7404cbd1..53c200e2c18 100644
--- a/src/bytecode.c
+++ b/src/bytecode.c
@@ -784,7 +784,6 @@ #define DEFINE(name, value) [name] = &&insn_ ## name,
 		  }
 	      }
 #endif
-	    maybe_quit ();
 
 	    if (++lisp_eval_depth > max_lisp_eval_depth)
 	      {
@@ -829,6 +828,7 @@ #define DEFINE(name, value) [name] = &&insn_ ## name,
 	    else
 	      val = funcall_general (original_fun, call_nargs, call_args);
 
+	    maybe_quit ();
 	    lisp_eval_depth--;
 	    if (backtrace_debug_on_exit (specpdl_ptr - 1))
 	      val = call_debugger (list2 (Qexit, val));
-- 
2.39.5


[-- Attachment #4: profiler-test.el --]
[-- Type: application/emacs-lisp, Size: 1730 bytes --]

[-- Attachment #5: bar-0.report --]
[-- Type: text/plain, Size: 1532 bytes --]

       0   0% nil
    1056 100%   normal-top-level
    1056 100%     command-line
    1056 100%       command-line-1
    1056 100%         main
    1056 100%           record-samples
    1056 100%             bar
    1056 100%               animate-birthday-present
    1046  99%                 animate-string
      61   5%                   sit-for
      61   5%                     sleep-for
     805  76%                   animate-step
     781  73%                     animate-place-char
      19   1%                       delete-char
      16   1%                       floor
       4   0%                       undo-auto--undoable-change
       4   0%                         undo-auto--boundary-ensure-timer
      96   9%                       insert-char
      14   1%                         undo-auto--undoable-change
       6   0%                           undo-auto--boundary-ensure-timer
       5   0%                       beginning-of-line
     232  21%                       move-to-column
     132  12%                   primitive-undo
       6   0%                     undo-auto--undoable-change
      10   0%                 capitalize
      10   0%                   load-with-code-conversion
       8   0%                     hack-read-symbol-shorthands
       8   0%                       hack-local-variables--find-variables
       8   0%                         search-backward
       2   0%                     generate-new-buffer
       2   0%                       get-buffer-create
       0   0%   ...

[-- Attachment #6: bar-2.report --]
[-- Type: text/plain, Size: 1526 bytes --]

       0   0% nil
     959 100%   normal-top-level
     959 100%     command-line
     959 100%       command-line-1
     959 100%         main
     959 100%           record-samples
     959 100%             bar
     959 100%               animate-birthday-present
     953  99%                 animate-string
      12   1%                   animate-initialize
      12   1%                     window-width
     139  14%                   sit-for
     139  14%                     sleep-for
     109  11%                   primitive-undo
      15   1%                     markerp
      32   3%                     abs
     693  72%                   animate-step
     693  72%                     animate-place-char
      32   3%                       delete-char
      29   3%                       window-start
      43   4%                       insert-char
     309  32%                       move-to-column
     222  23%                       beginning-of-line
       8   0%                       undo-auto--undoable-change
       8   0%                         undo-auto--boundary-ensure-timer
       8   0%                           run-at-time
       8   0%                             timer-set-function
       8   0%                               timerp
       8   0%                                 vectorp
       6   0%                 capitalize
       6   0%                   load-with-code-conversion
       6   0%                     eval-buffer
       6   0%                       read
       0   0%   ...

  parent reply	other threads:[~2025-01-03 18:37 UTC|newest]

Thread overview: 133+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-12-28 13:24 igc, macOS avoiding signals Sean Devlin
2024-12-28 13:28 ` Gerd Möllmann
2024-12-28 14:31   ` Eli Zaretskii
2024-12-28 14:45     ` Gerd Möllmann
2024-12-30  7:13       ` Gerd Möllmann
2024-12-30  7:23         ` Gerd Möllmann
2024-12-30  7:39         ` Helmut Eller
2024-12-30  7:51           ` Gerd Möllmann
2024-12-30  8:02             ` Helmut Eller
2024-12-30  8:47               ` Gerd Möllmann
2024-12-30  9:29                 ` Helmut Eller
2024-12-30  9:47                   ` Helmut Eller
2024-12-30 11:54                     ` Gerd Möllmann
2024-12-30 10:05                   ` Gerd Möllmann
2024-12-30 10:27                     ` Helmut Eller
2024-12-30 11:53                       ` Gerd Möllmann
2024-12-30 14:54                         ` Eli Zaretskii
2024-12-30 15:05                           ` Gerd Möllmann
2024-12-30 15:05                           ` Pip Cet via Emacs development discussions.
2024-12-30 12:32                       ` Pip Cet via Emacs development discussions.
2024-12-30 14:24                         ` Eli Zaretskii
2024-12-30 14:59                         ` Helmut Eller
2024-12-30 15:15                           ` Eli Zaretskii
2024-12-30 15:24                             ` Helmut Eller
2024-12-30 15:25                           ` Pip Cet via Emacs development discussions.
2024-12-30 15:34                             ` Gerd Möllmann
2024-12-30 19:02                             ` Helmut Eller
2024-12-30 20:03                               ` Pip Cet via Emacs development discussions.
2024-12-30 15:30                           ` Gerd Möllmann
2024-12-30 16:57                             ` Helmut Eller
2024-12-30 17:41                               ` Gerd Möllmann
2024-12-30 17:49                               ` Pip Cet via Emacs development discussions.
2024-12-30 18:33                                 ` Helmut Eller
2024-12-30 17:49                               ` Eli Zaretskii
2024-12-30 18:37                                 ` Gerd Möllmann
2024-12-30 19:15                                   ` Eli Zaretskii
2024-12-30 19:55                                     ` Gerd Möllmann
2024-12-31  7:34                                       ` Helmut Eller
2024-12-31  9:19                                         ` Gerd Möllmann
2024-12-31  9:51                                           ` Helmut Eller
2024-12-31 10:00                                             ` Gerd Möllmann
2024-12-31 13:49                                             ` Pip Cet via Emacs development discussions.
2024-12-31 14:13                                               ` Eli Zaretskii
2024-12-31  9:51                                           ` Gerd Möllmann
2024-12-31 13:18                                           ` Eli Zaretskii
2024-12-31 14:15                                             ` Gerd Möllmann
2024-12-31 14:27                                               ` Eli Zaretskii
2024-12-31 15:05                                                 ` Gerd Möllmann
2024-12-31 15:14                                                   ` Eli Zaretskii
2024-12-31 15:20                                                     ` Gerd Möllmann
2024-12-31 15:12                                               ` Helmut Eller
2024-12-31 15:31                                                 ` Gerd Möllmann
2024-12-31 15:37                                                   ` Helmut Eller
2024-12-31 15:39                                                     ` Gerd Möllmann
2024-12-31 10:09                                         ` Pip Cet via Emacs development discussions.
2024-12-31 13:27                                           ` Eli Zaretskii
2024-12-31 14:29                                             ` Pip Cet via Emacs development discussions.
2024-12-31 14:34                                               ` Eli Zaretskii
2024-12-31 15:08                                                 ` Gerd Möllmann
2025-01-03 18:37                                                 ` Helmut Eller [this message]
2025-01-03 19:55                                                   ` Eli Zaretskii
2025-01-03 20:28                                                   ` Pip Cet via Emacs development discussions.
2025-01-04  7:01                                                   ` Gerd Möllmann
2025-01-04  8:02                                                     ` Gerd Möllmann
2025-01-04  8:34                                                       ` Eli Zaretskii
2025-01-04  9:02                                                         ` Gerd Möllmann
2025-01-04  9:59                                                           ` Eli Zaretskii
2025-01-04 10:22                                                             ` Gerd Möllmann
2025-01-04 13:59                                                   ` Helmut Eller
2025-01-04 14:15                                                     ` Gerd Möllmann
2025-01-04 14:17                                                     ` Eli Zaretskii
2025-01-04 14:55                                                       ` Helmut Eller
2025-01-04 15:06                                                         ` Eli Zaretskii
2025-01-04 16:02                                                         ` Pip Cet via Emacs development discussions.
2025-01-04 17:13                                                           ` Helmut Eller
2024-12-31 15:07                                               ` Gerd Möllmann
2024-12-31 13:14                                         ` Eli Zaretskii
2024-12-31 14:19                                           ` Pip Cet via Emacs development discussions.
2024-12-31 14:31                                             ` Eli Zaretskii
2024-12-31 14:40                                           ` Helmut Eller
2024-12-31 14:55                                             ` Gerd Möllmann
2024-12-31 15:07                                               ` Eli Zaretskii
2024-12-31 15:13                                                 ` Gerd Möllmann
2024-12-31 15:16                                                   ` Helmut Eller
2025-01-02  8:37                                                   ` Stefan Kangas
2025-01-02  9:05                                                     ` Eli Zaretskii
2025-01-02 10:00                                                       ` Helmut Eller
2025-01-02 12:34                                                       ` Pip Cet via Emacs development discussions.
2025-01-02 13:08                                                         ` Gerd Möllmann
2025-01-02 15:42                                                         ` Eli Zaretskii
2025-01-02 17:56                                                           ` Pip Cet via Emacs development discussions.
2024-12-30 12:42                       ` Pip Cet via Emacs development discussions.
2024-12-30 13:40                         ` Gerd Möllmann
2024-12-30 13:53                           ` Pip Cet via Emacs development discussions.
2024-12-30 14:02                             ` Gerd Möllmann
2024-12-30 14:32                               ` Pip Cet via Emacs development discussions.
2024-12-30 14:52                                 ` Gerd Möllmann
2024-12-30 11:18                 ` Pip Cet via Emacs development discussions.
2024-12-30 12:23                   ` Gerd Möllmann
2024-12-30 11:11             ` Pip Cet via Emacs development discussions.
2024-12-30 12:13               ` Gerd Möllmann
2024-12-30 10:53           ` Pip Cet via Emacs development discussions.
2024-12-30 10:46         ` Pip Cet via Emacs development discussions.
2024-12-30 12:00           ` Gerd Möllmann
2024-12-30 12:07           ` Gerd Möllmann
2024-12-28 15:12 ` Pip Cet via Emacs development discussions.
2024-12-28 17:30   ` Eli Zaretskii
2024-12-28 18:40     ` Pip Cet via Emacs development discussions.
2024-12-28 18:50       ` Eli Zaretskii
2024-12-28 19:07         ` Eli Zaretskii
2024-12-28 19:20           ` Pip Cet via Emacs development discussions.
2024-12-28 19:36             ` Eli Zaretskii
2024-12-28 20:54               ` Pip Cet via Emacs development discussions.
2024-12-29  5:51                 ` Eli Zaretskii
2024-12-28 19:15         ` Pip Cet via Emacs development discussions.
2024-12-28 19:30           ` Eli Zaretskii
2024-12-28 16:29 ` Pip Cet via Emacs development discussions.
2024-12-29  2:21   ` Sean Devlin
2024-12-29 12:22     ` Pip Cet via Emacs development discussions.
2024-12-29 15:01       ` Gerd Möllmann
2024-12-29 19:44         ` Pip Cet via Emacs development discussions.
2024-12-30  6:16           ` Gerd Möllmann
2024-12-30 12:51             ` Gerd Möllmann
2024-12-30 13:09               ` Pip Cet via Emacs development discussions.
2024-12-30 13:28                 ` Gerd Möllmann
2024-12-30  5:24         ` Sean Devlin
2024-12-30  6:17           ` Gerd Möllmann
2024-12-30  5:23       ` Sean Devlin
  -- strict thread matches above, loose matches on Subject: below --
2024-12-28  6:40 Gerd Möllmann
2024-12-28 12:49 ` Pip Cet via Emacs development discussions.
2024-12-28 12:55   ` Gerd Möllmann
2024-12-28 13:50     ` Óscar Fuentes
2024-12-29  8:02       ` Helmut Eller

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=87pll3ivzs.fsf@gmail.com \
    --to=eller.helmut@gmail.com \
    --cc=eliz@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=gerd.moellmann@gmail.com \
    --cc=pipcet@protonmail.com \
    --cc=spd@toadstyle.org \
    /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).