unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#35418: [PATCH] Don't poll auto-revert files that use notification
@ 2019-04-24 18:14 Mattias Engdegård
  2019-04-24 18:58 ` Eli Zaretskii
                   ` (2 more replies)
  0 siblings, 3 replies; 101+ messages in thread
From: Mattias Engdegård @ 2019-04-24 18:14 UTC (permalink / raw)
  To: 35418; +Cc: Michael Albinus

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

It is a waste of power, on battery-powered devices in particular, to poll files in auto-revert mode periodically when change notification is used. The change is straightforward (attached patch); the main concern is whether the notification system is reliable enough.

In general, it probably is. There is a comment in w32notify.c about SMB-mounted file systems from Samba servers; while Samba does support notification nowadays, there are probably older systems still be deficient in that regard. However, isn't this what `auto-revert-notify-exclude-dir-regexp' is for? I'm not familiar with the way Emacs is used on Windows, but would adding something like

 (rx bos
     (or "\\\\" "//")
     (one-or-more (not (any "/:\\")))
     (any "/\\"))

to `auto-revert-notify-exclude-dir-regexp' be a good start?

Another note about what this patch does not do: global-auto-revert-mode will still use polling. This could be added later on, if there is a good place to hook into for buffer creation.


[-- Attachment #2: 0001-Don-t-poll-auto-revert-files-that-use-notification.patch --]
[-- Type: application/octet-stream, Size: 9323 bytes --]

From 5074c65347736a716f335842206a1e2a2ad36a87 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Wed, 24 Apr 2019 18:39:05 +0200
Subject: [PATCH] Don't poll auto-revert files that use notification

It is a waste to periodically poll files that use change notification
in auto-revert mode; stop doing that.  If no files need polling,
turn off the periodic execution entirely to further avoid wasting power.
Use a timer to inhibit immediate reversion for some time after a
notification.

This change does not apply to files in global-auto-revert-mode, where
polling is still necessary.

* lisp/autorevert.el (auto-revert--polled-buffers): New.
(auto-revert-remove-current-buffer, auto-revert-mode)
(global-auto-revert-mode, auto-revert-set-timer)
(auto-revert-notify-add-watch, auto-revert-buffers):
Maintain and use auto-revert--polled-buffers.
(auto-revert-buffers-counter): Remove.
(auto-revert-buffers-counter-lockedout): Remove.
(auto-revert--lockout-interval): New.
(auto-revert--lockout-timer): New.
(auto-revert-notify-handler): Maintain and use auto-revert--polled-buffers.
Honour new lockout timer.  Start lockout timer if necessary.
(auto-revert--end-lockout): New.
---
 lisp/autorevert.el | 84 +++++++++++++++++++++++++++++-----------------
 1 file changed, 53 insertions(+), 31 deletions(-)

diff --git a/lisp/autorevert.el b/lisp/autorevert.el
index 1d20896c83..f7b33360ef 100644
--- a/lisp/autorevert.el
+++ b/lisp/autorevert.el
@@ -319,6 +319,11 @@ the list of old buffers.")
 (defvar auto-revert-tail-pos 0
   "Position of last known end of file.")
 
+(defvar auto-revert--polled-buffers ()
+  "List of buffers in Auto-Revert Mode that must be polled.
+It contains the buffers in `auto-revert-buffer-list' whose
+`auto-revert-notify-watch-descriptor' is nil.")
+
 (defun auto-revert-find-file-function ()
   (setq-local auto-revert-tail-pos
               (file-attribute-size (file-attributes buffer-file-name))))
@@ -346,8 +351,12 @@ This has been reported by a file notification event.")
 (defun auto-revert-remove-current-buffer (&optional buffer)
   "Remove BUFFER from `auto-revert-buffer-list'.
 BUFFER defaults to `current-buffer'."
+  (unless buffer
+    (setq buffer (current-buffer)))
   (setq auto-revert-buffer-list
-        (delq (or buffer (current-buffer)) auto-revert-buffer-list)))
+        (delq buffer auto-revert-buffer-list))
+  (setq auto-revert--polled-buffers
+        (delq buffer auto-revert--polled-buffers)))
 
 ;;;###autoload
 (define-minor-mode auto-revert-mode
@@ -367,6 +376,7 @@ without being changed in the part that is already in the buffer."
   (if auto-revert-mode
       (when (not (memq (current-buffer) auto-revert-buffer-list))
         (push (current-buffer) auto-revert-buffer-list)
+        (push (current-buffer) auto-revert--polled-buffers)
         (add-hook
          'kill-buffer-hook
          #'auto-revert-remove-current-buffer
@@ -479,7 +489,8 @@ specifies in the mode line."
       (auto-revert-buffers)
     (dolist (buf (buffer-list))
       (with-current-buffer buf
-	(when auto-revert-notify-watch-descriptor
+        (when (and auto-revert-notify-watch-descriptor
+                   (not (memq buf auto-revert-buffer-list)))
 	  (auto-revert-notify-rm-watch))))))
 
 (defun auto-revert-set-timer ()
@@ -492,7 +503,7 @@ will use an up-to-date value of `auto-revert-interval'"
   (if (timerp auto-revert-timer)
       (cancel-timer auto-revert-timer))
   (setq auto-revert-timer
-	(if (or global-auto-revert-mode auto-revert-buffer-list)
+	(if (or global-auto-revert-mode auto-revert--polled-buffers)
 	    (run-with-timer auto-revert-interval
 			    auto-revert-interval
 			    'auto-revert-buffers))))
@@ -551,6 +562,8 @@ will use an up-to-date value of `auto-revert-interval'"
 	       (gethash auto-revert-notify-watch-descriptor
 		        auto-revert--buffers-by-watch-descriptor))
          auto-revert--buffers-by-watch-descriptor)
+        (setq auto-revert--polled-buffers
+              (delq (current-buffer) auto-revert--polled-buffers))
         (add-hook 'kill-buffer-hook #'auto-revert-notify-rm-watch nil t)))))
 
 ;; If we have file notifications, we want to update the auto-revert buffers
@@ -558,24 +571,20 @@ will use an up-to-date value of `auto-revert-interval'"
 ;; often, we want to skip some revert operations so that we don't spend all our
 ;; time reverting the buffer.
 ;;
-;; We do this by reverting immediately in response to the first in a flurry of
-;; notifications. We suppress subsequent notifications until the next time
-;; `auto-revert-buffers' is called (this happens on a timer with a period set by
-;; `auto-revert-interval').
-(defvar auto-revert-buffers-counter 1
-  "Incremented each time `auto-revert-buffers' is called")
-(defvar-local auto-revert-buffers-counter-lockedout 0
-  "Buffer-local value to indicate whether we should immediately
-update the buffer on a notification event or not. If
-
-  (= auto-revert-buffers-counter-lockedout
-     auto-revert-buffers-counter)
-
-then the updates are locked out, and we wait until the next call
-of `auto-revert-buffers' to revert the buffer. If no lockout is
-present, then we revert immediately and set the lockout, so that
-no more reverts are possible until the next call of
-`auto-revert-buffers'")
+;; We do this by reverting immediately in response to the first in a
+;; flurry of notifications. Any notifications during the following
+;; `auto-revert-lockout-interval' seconds are noted but not acted upon
+;; until the end of that interval.
+
+(defconst auto-revert--lockout-interval 2.5
+  "Duration, in seconds, of the Auto-Revert Mode notification lockout.
+This is the quiescence after each notification of a file being
+changed during which no automatic reverting takes place, to
+prevent many updates in rapid succession from overwhelming the
+system.")
+
+(defvar-local auto-revert--lockout-timer nil
+  "Timer awaiting the end of the notification lockout interval, or nil.")
 
 (defun auto-revert-notify-handler (event)
   "Handle an EVENT returned from file notification."
@@ -604,7 +613,13 @@ no more reverts are possible until the next call of
                            (file-name-nondirectory buffer-file-name)))
                      ;; A buffer w/o a file, like dired.
                      (null buffer-file-name))
-                (auto-revert-notify-rm-watch))))
+                (auto-revert-notify-rm-watch)
+                (when (memq buffer auto-revert-buffer-list)
+                  (unless (memq buffer auto-revert--polled-buffers)
+                    (push buffer auto-revert--polled-buffers))
+                  ;; Restart the timer if it wasn't running.
+                  (unless auto-revert-timer
+                    (auto-revert-set-timer))))))
 
         ;; Loop over all buffers, in order to find the intended one.
         (cl-dolist (buffer buffers)
@@ -630,11 +645,21 @@ no more reverts are possible until the next call of
                 (setq auto-revert-notify-modified-p t)
 
                 ;; Revert the buffer now if we're not locked out.
-                (when (/= auto-revert-buffers-counter-lockedout
-                          auto-revert-buffers-counter)
+                (unless auto-revert--lockout-timer
                   (auto-revert-handler)
-                  (setq auto-revert-buffers-counter-lockedout
-                        auto-revert-buffers-counter))))))))))
+                  (setq auto-revert--lockout-timer
+                        (run-with-timer
+                         auto-revert--lockout-interval nil
+                         #'auto-revert--end-lockout buffer)))))))))))
+
+(defun auto-revert--end-lockout (buffer)
+  "End the lockout period after a notification.
+If the buffer needs to be reverted, do it now."
+  (when (buffer-live-p buffer)
+    (with-current-buffer buffer
+      (setq auto-revert--lockout-timer nil)
+      (when auto-revert-notify-modified-p
+        (auto-revert-handler)))))
 
 (defun auto-revert-active-p ()
   "Check if auto-revert is active (in current buffer or globally)."
@@ -755,13 +780,10 @@ This function is also responsible for removing buffers no longer in
 Auto-Revert Mode from `auto-revert-buffer-list', and for canceling
 the timer when no buffers need to be checked."
 
-  (setq auto-revert-buffers-counter
-        (1+ auto-revert-buffers-counter))
-
   (save-match-data
     (let ((bufs (if global-auto-revert-mode
 		    (buffer-list)
-		  auto-revert-buffer-list))
+		  auto-revert--polled-buffers))
 	  remaining new)
       ;; Buffers with remote contents shall be reverted only if the
       ;; connection is established already.
@@ -811,7 +833,7 @@ the timer when no buffers need to be checked."
       (setq auto-revert-remaining-buffers bufs)
       ;; Check if we should cancel the timer.
       (when (and (not global-auto-revert-mode)
-		 (null auto-revert-buffer-list))
+		 (null auto-revert--polled-buffers))
         (if (timerp auto-revert-timer)
             (cancel-timer auto-revert-timer))
 	(setq auto-revert-timer nil)))))
-- 
2.20.1 (Apple Git-117)


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

end of thread, other threads:[~2019-05-20 19:19 UTC | newest]

Thread overview: 101+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-04-24 18:14 bug#35418: [PATCH] Don't poll auto-revert files that use notification Mattias Engdegård
2019-04-24 18:58 ` Eli Zaretskii
2019-04-24 19:36   ` Michael Albinus
2019-04-26 20:46     ` Mattias Engdegård
2019-04-27  9:40       ` Michael Albinus
2019-04-27 16:28         ` Mattias Engdegård
2019-04-25  9:56   ` Mattias Engdegård
2019-04-25 10:04     ` Eli Zaretskii
2019-04-25 18:07       ` Mattias Engdegård
2019-04-27  9:27       ` Michael Albinus
2019-04-27  9:54         ` Eli Zaretskii
2019-04-27 10:23           ` Michael Albinus
2019-04-27 16:19         ` Mattias Engdegård
2019-04-27 16:52           ` Eli Zaretskii
2019-04-28 10:21             ` Mattias Engdegård
2019-04-29  7:53               ` Michael Albinus
2019-04-29 11:06                 ` Mattias Engdegård
2019-04-29 12:18                   ` Michael Albinus
2019-04-29 16:24                     ` Eli Zaretskii
2019-04-29 18:29                     ` Mattias Engdegård
2019-04-29 20:17                       ` Michael Albinus
2019-04-30  3:57                         ` Eli Zaretskii
2019-04-30 11:41                           ` Mattias Engdegård
2019-04-30 12:59                             ` Michael Albinus
2019-04-30 13:56                               ` Mattias Engdegård
2019-04-30 14:19                                 ` Michael Albinus
2019-04-29 16:23                   ` Eli Zaretskii
2019-04-29 19:21                     ` Mattias Engdegård
2019-04-29 19:56                       ` Michael Albinus
2019-04-30 21:09                     ` Mattias Engdegård
2019-05-01 17:45                       ` Eli Zaretskii
2019-05-01 19:41                         ` Mattias Engdegård
2019-05-02 12:18                           ` Michael Albinus
2019-05-02 12:53                             ` Mattias Engdegård
2019-05-02 13:02                               ` Michael Albinus
2019-05-03 12:00                                 ` Mattias Engdegård
2019-05-03 13:44                               ` Eli Zaretskii
2019-05-03 14:47                                 ` Mattias Engdegård
2019-05-04  9:04                                   ` Eli Zaretskii
2019-05-04 11:21                                     ` Mattias Engdegård
2019-05-04 13:41                                       ` Eli Zaretskii
2019-05-04 16:53                                       ` Michael Albinus
2019-05-04 17:08                                         ` Eli Zaretskii
2019-05-04 18:50                                         ` Mattias Engdegård
2019-05-04 19:43                                           ` Michael Albinus
2019-05-04 20:31                                             ` Michael Albinus
2019-05-04 20:46                                               ` Mattias Engdegård
2019-05-05  8:22                                                 ` Michael Albinus
2019-05-05  9:58                                                   ` Mattias Engdegård
2019-05-08  8:34                                                     ` Mattias Engdegård
2019-05-08  8:47                                                       ` Eli Zaretskii
2019-05-08 10:18                                                         ` Mattias Engdegård
2019-05-08 10:58                                                           ` Eli Zaretskii
2019-05-08 11:48                                                             ` Mattias Engdegård
2019-05-08 12:35                                                               ` Eli Zaretskii
2019-05-08 12:58                                                                 ` Mattias Engdegård
2019-05-08 13:09                                                                   ` Michael Albinus
2019-05-08 13:28                                                                   ` Eli Zaretskii
2019-05-08 14:13                                                                     ` Mattias Engdegård
2019-05-08 17:24                                                                       ` Eli Zaretskii
2019-05-08 18:17                                                                         ` Michael Albinus
2019-05-09 11:50                                                               ` Michael Albinus
2019-05-10 15:22                                                                 ` Mattias Engdegård
2019-05-12  8:48                                                                   ` Michael Albinus
2019-05-12 19:49                                                                     ` Mattias Engdegård
2019-05-13 13:35                                                                       ` Michael Albinus
2019-05-14 12:41                                                                         ` Mattias Engdegård
2019-05-14 14:52                                                                           ` Michael Albinus
2019-05-08 10:23                                                       ` Mattias Engdegård
2019-05-09 10:00                                                     ` Mattias Engdegård
2019-05-09 10:48                                                       ` Eli Zaretskii
2019-05-09 11:15                                                         ` Mattias Engdegård
2019-05-10  9:49                                                       ` Michael Albinus
2019-05-10 12:27                                                         ` Mattias Engdegård
2019-05-10 12:43                                                           ` Michael Albinus
2019-05-13 11:34                                                             ` Mattias Engdegård
2019-05-13 15:08                                                               ` Michael Albinus
2019-05-18 17:39                                                                 ` Mattias Engdegård
2019-05-19  9:12                                                                   ` Michael Albinus
2019-05-19 20:25                                                                     ` Mattias Engdegård
2019-05-20  7:30                                                                       ` Michael Albinus
2019-05-20 19:19                                                                         ` Mattias Engdegård
2019-04-29  7:19           ` Michael Albinus
2019-04-29 11:54             ` Mattias Engdegård
2019-04-29 12:26               ` Michael Albinus
2019-04-29 18:58                 ` Mattias Engdegård
2019-04-29 20:04                   ` Michael Albinus
2019-04-30 15:14                   ` Eli Zaretskii
2019-04-24 19:59 ` Michael Albinus
2019-04-25  9:58   ` Mattias Engdegård
2019-04-25 11:04     ` Michael Albinus
2019-04-25 15:22       ` Mattias Engdegård
2019-04-30  1:03 ` Zhang Haijun
2019-04-30  7:06   ` Michael Albinus
2019-05-01  2:17     ` Zhang Haijun
2019-05-01  2:59       ` Zhang Haijun
2019-05-01  3:10         ` Zhang Haijun
2019-05-02 12:30           ` Michael Albinus
2019-05-02 13:24             ` Zhang Haijun
2019-05-02 12:28         ` Michael Albinus
2019-05-02 12:24       ` Michael Albinus

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).