all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* bug#73855: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time
@ 2024-10-17 23:24 Lin Sun
  2024-10-18  7:50 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 1 reply; 2+ messages in thread
From: Lin Sun @ 2024-10-17 23:24 UTC (permalink / raw)
  To: 73855

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

Hi,

High CPU consumption after enabling the "auto-revert" for a buffer.
Here is a way to reproduce the issue:
1. on one terminal run command "strace -f -o /tmp/a.log vi -nw"
2. on second terminal, start another emacs and open the file
/tmp/a.log, enable the "auto-revert" mode on the /tmp/a.log buffer.

Typing on 1st terminal(vi), the strace will continually write the
/tmp/a.log, and the emacs try to revert the /tmp/a.log buffer again
and again, then CPU loading turns high.

The function `auto-revert-handler` may be called twice for 2.5 seconds
intervals on a rapidly changed buffer/file.

The root cause is `auto-revert--end-lockout` will call
`auto-revert-handler` in which the `auto-revert--lockout-timer` was
cleared, then the next call `auto-revert-notify-handler` will revert
the buffer immediately regardless the buffer actually was revert just
before.

This patch will record when the buffer was reverted and avoid
reverting it again in the same second.

[-- Attachment #2: 0001-lisp-autorevert.el-Avoid-reverting-buffer-in-short-t.patch --]
[-- Type: text/x-patch, Size: 2225 bytes --]

From b1dc630718c39b2fb630d349f186ac060290c515 Mon Sep 17 00:00:00 2001
From: Lin Sun <sunlin7@hotmail.com>
Date: Thu, 17 Oct 2024 06:50:31 +0000
Subject: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time

---
 lisp/autorevert.el | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index 0fdab6ffc9f..ef758584c0d 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -370,6 +370,9 @@ auto-revert-notify-modified-p
   "Non-nil when file has been modified on the file system.
 This has been reported by a file notification event.")
 
+(defvar-local auto-revert--last-time nil
+  "The last time of buffer was reverted.")
+
 (defvar auto-revert-debug nil
   "Use for debug messages.")
 
@@ -749,13 +752,17 @@ auto-revert-notify-handler
               ;; Mark buffer modified.
               (setq auto-revert-notify-modified-p t)
 
-              ;; Revert the buffer now if we're not locked out.
+              ;; Lock out the buffer
               (unless auto-revert--lockout-timer
-                (auto-revert-handler)
                 (setq auto-revert--lockout-timer
                       (run-with-timer
                        auto-revert--lockout-interval nil
-                       #'auto-revert--end-lockout buffer))))))))))
+                       #'auto-revert--end-lockout buffer))
+                ;; Revert it when first entry or it was reverted intervals ago
+                (when (or (null auto-revert--last-time)
+                          (> (float-time (time-since auto-revert--last-time))
+                             auto-revert--lockout-interval))
+                  (auto-revert-handler))))))))))
 
 (defun auto-revert--end-lockout (buffer)
   "End the lockout period after a notification.
@@ -801,7 +808,8 @@ auto-revert-handler
                               #'buffer-stale--default-function)
                           t))))
          eob eoblist)
-    (setq auto-revert-notify-modified-p nil)
+    (setq auto-revert-notify-modified-p nil
+          auto-revert--last-time (current-time))
     (when revert
       (when (and auto-revert-verbose
                  (not (eq revert 'fast)))
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* bug#73855: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time
  2024-10-17 23:24 bug#73855: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time Lin Sun
@ 2024-10-18  7:50 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors
  0 siblings, 0 replies; 2+ messages in thread
From: Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors @ 2024-10-18  7:50 UTC (permalink / raw)
  To: Lin Sun; +Cc: 73855

Lin Sun <sunlin7.mail@gmail.com> writes:

> Hi,

Hi,

> High CPU consumption after enabling the "auto-revert" for a buffer.
> Here is a way to reproduce the issue:
> 1. on one terminal run command "strace -f -o /tmp/a.log vi -nw"
> 2. on second terminal, start another emacs and open the file
> /tmp/a.log, enable the "auto-revert" mode on the /tmp/a.log buffer.
>
> Typing on 1st terminal(vi), the strace will continually write the
> /tmp/a.log, and the emacs try to revert the /tmp/a.log buffer again
> and again, then CPU loading turns high.
>
> The function `auto-revert-handler` may be called twice for 2.5 seconds
> intervals on a rapidly changed buffer/file.
>
> The root cause is `auto-revert--end-lockout` will call
> `auto-revert-handler` in which the `auto-revert--lockout-timer` was
> cleared, then the next call `auto-revert-notify-handler` will revert
> the buffer immediately regardless the buffer actually was revert just
> before.
>
> This patch will record when the buffer was reverted and avoid
> reverting it again in the same second.

There is no bug. If auto-revert-use-notify is non-nil, file
notifications are in game, which are *supposed* to revert
immediately. No delay is wanted.

In your case, where mass write happens to a file, I recommend to set
auto-revert-use-notify to nil. Polling is used then instead, with
auto-revert-interval interval between reverts. The default value is 5
(seconds), you might change it to 1.

Furthermore, your use case doesn't look ideal for enabling
auto-revert-mode. Checking fast changing log files is better done with
auto-revert-tail-mode.

Best regards, Michael.





^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-10-18  7:50 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-17 23:24 bug#73855: [PATCH] * lisp/autorevert.el: Avoid reverting buffer in short time Lin Sun
2024-10-18  7:50 ` Michael Albinus via Bug reports for GNU Emacs, the Swiss army knife of text editors

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.