-
Notifications
You must be signed in to change notification settings - Fork 32
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support cancellation of timer operations #110
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
;; Fibers: cooperative, event-driven user-space threads. | ||
|
||
;;;; Copyright (C) 2024 Ludovic Courtès <[email protected]> | ||
;;;; | ||
;;;; This library is free software; you can redistribute it and/or | ||
;;;; modify it under the terms of the GNU Lesser General Public | ||
;;;; License as published by the Free Software Foundation; either | ||
;;;; version 3 of the License, or (at your option) any later version. | ||
;;;; | ||
;;;; This library is distributed in the hope that it will be useful, | ||
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
;;;; Lesser General Public License for more details. | ||
;;;; | ||
;;;; You should have received a copy of the GNU Lesser General Public License | ||
;;;; along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
;;;; | ||
|
||
(define-module (tests cancel-timer) | ||
#:use-module (fibers) | ||
#:use-module (fibers channels) | ||
#:use-module (fibers operations) | ||
#:use-module (fibers timers) | ||
#:use-module (ice-9 format)) | ||
|
||
(define (heap-size) | ||
(assoc-ref (gc-stats) 'heap-size)) | ||
|
||
(define iterations 200000) | ||
|
||
;;; Check the heap growth caused by repeated choice operations where one of | ||
;;; the base operations is a timer that always "loses" the choice. | ||
;;; | ||
;;; This situation used to cause timer continuations to accumulate, thereby | ||
;;; leading to unbounded heap growth. The cancel function of | ||
;;; 'timer-operation' fixes that by immediately canceling timers that lost in | ||
;;; a choice operation. See <https://github.com/wingo/fibers/issues/109>. | ||
|
||
(run-fibers | ||
(lambda () | ||
(define channel | ||
(make-channel)) | ||
|
||
(spawn-fiber | ||
(lambda () | ||
(let loop ((i 0)) | ||
(when (< i iterations) | ||
(put-message channel 'hello) | ||
(loop (+ i 1)))))) | ||
|
||
(let ((initial-heap-size (heap-size))) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you're checking heap sizes, best do a gc beforehand, otherwise some heavy activity could lead to false negatives (as in 'no bug detected even though it exists'). Would it be possible (and sufficiently informative & meaningful) to instead check the length of the timer wheel? Seems less finicky to me (e.g. what if in the future tests are run in parallel in a single process). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also, if something finicky like heap sizes is avoided, I imagine the number of iterations could be reduced a lot (good for test performance). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I found that when #109 is present, the heap would grow way beyond the 2x limit that's tested here; it would not go unnoticed. (Maybe we could make the test faster but it was already reasonably fast in my experience.) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. How fast is 'reasonably fast'? |
||
(let loop ((i 0)) | ||
(when (< i iterations) | ||
(perform-operation | ||
(choice-operation (sleep-operation 500) | ||
(get-operation channel))) | ||
(loop (+ 1 i)))) | ||
|
||
(let ((final-heap-size (heap-size)) | ||
(MiB (lambda (size) | ||
(/ size (expt 2 20.))))) | ||
(if (<= final-heap-size (* 2 initial-heap-size)) | ||
(format #t "final heap size: ~,2f MiB; initial heap size: ~,2f MiB~%" | ||
(MiB final-heap-size) (MiB initial-heap-size)) | ||
(begin | ||
(format #t "heap grew too much: ~,2f MiB vs. ~,2f MiB~%" | ||
(MiB final-heap-size) (MiB initial-heap-size)) | ||
(primitive-exit 1))))))) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For ABI reasons, there needs to be a way to tell to Guile 'don't inline this' (not blocking this PR).