| 1 |
;;;; -*- Mode: Lisp; Syntax: Common-Lisp; indent-tabs-mode: nil -*- |
| 2 |
;;;; vim: set filetype=lisp tabstop=8 shiftwidth=2 expandtab: |
| 3 |
|
| 4 |
;; Author: Daniel Kochmański |
| 5 |
;; Contains: Multiprocessing stress tests |
| 6 |
|
| 7 |
(in-package :cl-test) |
| 8 |
|
| 9 |
(suite 'stress) |
| 10 |
|
| 11 |
(defparameter *runs* 5) |
| 12 |
|
| 13 |
|
| 14 |
;; Semaphores |
| 15 |
|
| 16 |
;; Submitted by James M. Lawrence |
| 17 |
;; |
| 18 |
;; Notes: couldn't reproduce on 64-bit machine, but author uses 32-bit |
| 19 |
(test semaphore.wait/signal |
| 20 |
(let* ((message-count 10000) |
| 21 |
(worker-count 64) |
| 22 |
(to-workers (mp:make-semaphore)) |
| 23 |
(from-workers (mp:make-semaphore)) |
| 24 |
(threads |
| 25 |
(loop repeat worker-count |
| 26 |
collect (mp:process-run-function |
| 27 |
"test" |
| 28 |
(lambda () |
| 29 |
(loop |
| 30 |
(mp:wait-on-semaphore to-workers) |
| 31 |
(mp:signal-semaphore from-workers))))))) |
| 32 |
(dotimes (i *runs*) |
| 33 |
(loop repeat message-count |
| 34 |
do (mp:signal-semaphore to-workers)) |
| 35 |
(loop repeat message-count |
| 36 |
do (mp:wait-on-semaphore from-workers)) |
| 37 |
(is (zerop (mp:semaphore-count to-workers))) |
| 38 |
(is (zerop (mp:semaphore-count from-workers))) |
| 39 |
(finish-output)) |
| 40 |
(mapcar #'mp:process-kill threads))) |
| 41 |
|
| 42 |
|
| 43 |
;; Implementation of a semaphore using locks and condition variables |
| 44 |
|
| 45 |
;; Submitted by James M. Lawrence |
| 46 |
;; |
| 47 |
;; Notes: couldn't reproduce on 64-bit machine, but author uses 32-bit |
| 48 |
(defstruct sema |
| 49 |
(count 0) |
| 50 |
(lock (mp:make-lock :recursive nil)) |
| 51 |
(cvar (mp:make-condition-variable))) |
| 52 |
|
| 53 |
(defun inc-sema (sema) |
| 54 |
(mp:with-lock ((sema-lock sema)) |
| 55 |
(incf (sema-count sema)) |
| 56 |
(mp:condition-variable-signal (sema-cvar sema)))) |
| 57 |
|
| 58 |
(defun dec-sema (sema) |
| 59 |
(mp:with-lock ((sema-lock sema)) |
| 60 |
(loop (cond ((plusp (sema-count sema)) |
| 61 |
(decf (sema-count sema)) |
| 62 |
(return)) |
| 63 |
(t |
| 64 |
(mp:condition-variable-wait |
| 65 |
(sema-cvar sema) (sema-lock sema))))))) |
| 66 |
|
| 67 |
(test semaphore/condition-wait |
| 68 |
(let* ((message-count 10000) |
| 69 |
(worker-count 64) |
| 70 |
(to-workers (make-sema)) |
| 71 |
(from-workers (make-sema)) |
| 72 |
(threads |
| 73 |
(loop repeat worker-count |
| 74 |
collect (mp:process-run-function |
| 75 |
"test" |
| 76 |
(lambda () |
| 77 |
(loop |
| 78 |
(dec-sema to-workers) |
| 79 |
(inc-sema from-workers))))))) |
| 80 |
(dotimes (i *runs*) |
| 81 |
(loop repeat message-count |
| 82 |
do (inc-sema to-workers)) |
| 83 |
(loop repeat message-count |
| 84 |
do (dec-sema from-workers)) |
| 85 |
(is (zerop (sema-count to-workers))) |
| 86 |
(is (zerop (sema-count from-workers))) |
| 87 |
(finish-output)) |
| 88 |
(mapcar #'mp:process-kill threads))) |
| 89 |
|
| 90 |
|
| 91 |
;; Implementation of a queue using locks and condition variables |
| 92 |
|
| 93 |
;; Submitted by James M. Lawrence |
| 94 |
;; |
| 95 |
;; Notes: couldn't reproduce on 64-bit machine, but author uses 32-bit |
| 96 |
(defstruct (raw-queue (:conc-name nil)) |
| 97 |
(head nil) |
| 98 |
(tail nil)) |
| 99 |
|
| 100 |
(defun push-raw-queue (value queue) |
| 101 |
(let ((new (cons value nil))) |
| 102 |
(if (head queue) |
| 103 |
(setf (cdr (tail queue)) new) |
| 104 |
(setf (head queue) new)) |
| 105 |
(setf (tail queue) new))) |
| 106 |
|
| 107 |
(defun pop-raw-queue (queue) |
| 108 |
(let ((node (head queue))) |
| 109 |
(if node |
| 110 |
(multiple-value-prog1 (values (car node) t) |
| 111 |
(when (null (setf (head queue) (cdr node))) |
| 112 |
(setf (tail queue) nil)) |
| 113 |
(setf (car node) nil |
| 114 |
(cdr node) nil)) |
| 115 |
(values nil nil)))) |
| 116 |
|
| 117 |
(defstruct queue |
| 118 |
(impl (make-raw-queue)) |
| 119 |
(lock (mp:make-lock)) |
| 120 |
(cvar (mp:make-condition-variable))) |
| 121 |
|
| 122 |
(defun push-queue (object queue) |
| 123 |
(mp:with-lock ((queue-lock queue)) |
| 124 |
(push-raw-queue object (queue-impl queue)) |
| 125 |
(mp:condition-variable-signal (queue-cvar queue)))) |
| 126 |
|
| 127 |
(defun pop-queue (queue) |
| 128 |
(mp:with-lock ((queue-lock queue)) |
| 129 |
(loop (multiple-value-bind (value presentp) |
| 130 |
(pop-raw-queue (queue-impl queue)) |
| 131 |
(if presentp |
| 132 |
(return value) |
| 133 |
(mp:condition-variable-wait |
| 134 |
(queue-cvar queue) |
| 135 |
(queue-lock queue))))))) |
| 136 |
|
| 137 |
(defun qtest (message-count worker-count) |
| 138 |
(dotimes (i *runs*) |
| 139 |
(let ((to-workers (make-queue)) |
| 140 |
(from-workers (make-queue))) |
| 141 |
(loop repeat worker-count |
| 142 |
do (mp:process-run-function |
| 143 |
"test" |
| 144 |
(lambda () |
| 145 |
(loop |
| 146 |
(let ((message (pop-queue to-workers))) |
| 147 |
(push-queue message from-workers) |
| 148 |
(unless message (return))))))) |
| 149 |
(loop repeat message-count do (push-queue t to-workers)) |
| 150 |
(loop repeat message-count do (pop-queue from-workers)) |
| 151 |
(loop repeat worker-count do (push-queue nil to-workers)) |
| 152 |
(loop repeat worker-count do (pop-queue from-workers)) |
| 153 |
(format t ".") |
| 154 |
(finish-output)))) |
| 155 |
|
| 156 |
(test qtest.1 (finishes (let ((*runs* (* *runs* 10))) |
| 157 |
(qtest 0 64)))) ; => segfault |
| 158 |
(test qtest.2 (finishes (let ((*runs* (* *runs* 10))) |
| 159 |
(qtest 1 64)))) ; => hang |
| 160 |
(test qtest.3 (finishes (qtest 10000 64))) ; => error "Attempted to recursively lock..." |
| 161 |
|
| 162 |
|
| 163 |
;; Interrupts |
| 164 |
|
| 165 |
;;; simplified version of with-timeout from bordeaux-threads |
| 166 |
(defmacro with-timeout ((timeout) &body body) |
| 167 |
`(let (sleeper) |
| 168 |
(multiple-value-prog1 |
| 169 |
(catch 'exit |
| 170 |
(catch 'timeout |
| 171 |
(let ((caller mp:*current-process*)) |
| 172 |
(setf sleeper |
| 173 |
(mp:process-run-function |
| 174 |
"sleeper-thread" |
| 175 |
#'(lambda () |
| 176 |
(sleep ,timeout) |
| 177 |
(mp:interrupt-process caller |
| 178 |
#'(lambda () |
| 179 |
(ignore-errors |
| 180 |
(throw 'timeout nil))))))) |
| 181 |
(throw 'exit (progn ,@body)))) |
| 182 |
(error 'ext:timeout :value ,timeout)) |
| 183 |
(when (mp:process-active-p sleeper) |
| 184 |
(ignore-errors (mp:process-kill sleeper)))))) |
| 185 |
|
| 186 |
(defun log-random (min max) |
| 187 |
"Randomly distributed number on a log scale between min and max" |
| 188 |
(exp (+ (random (- (log max) (log min))) |
| 189 |
(log min)))) |
| 190 |
|
| 191 |
;; with-timeout macro from bordeaux threads, tests interrupt safety of |
| 192 |
;; various stuff (catch frames, error handling, killing threads |
| 193 |
;; which may be inactive). The code itself does nothing except sleep, |
| 194 |
;; but it shouldn't segfault. |
| 195 |
(test with-timeout |
| 196 |
(finishes (dotimes (i (* *runs* 10000)) |
| 197 |
(let ((timeout-value (log-random 1e-8 1e-2))) |
| 198 |
(handler-case |
| 199 |
(with-timeout (timeout-value) (sleep (* timeout-value 10))) |
| 200 |
(ext:timeout (c))))))) |
| 201 |
|
| 202 |
;; interrupt safety of binding special variables |
| 203 |
(defvar *test-var* 0) |
| 204 |
(test interrupt-bind-special |
| 205 |
(dotimes (i *runs*) |
| 206 |
(let* ((interrupt-count 10000) |
| 207 |
(worker-count 64) |
| 208 |
(*test-var* -1) |
| 209 |
(threads |
| 210 |
(loop for j from 1 upto worker-count collect |
| 211 |
(let ((k j)) |
| 212 |
(mp:process-run-function |
| 213 |
"test" |
| 214 |
#'(lambda () |
| 215 |
(loop (sleep (log-random 1e-8 1e-2)) |
| 216 |
(let ((*test-var* k)) |
| 217 |
(sleep (log-random 1e-8 1e-2))))))))) |
| 218 |
failure-p) |
| 219 |
(loop repeat interrupt-count do |
| 220 |
(loop for j from 1 upto worker-count |
| 221 |
for thread in threads do |
| 222 |
(let ((k j)) |
| 223 |
(mp:interrupt-process |
| 224 |
thread |
| 225 |
#'(lambda () |
| 226 |
(unless (or (eq *test-var* 0) |
| 227 |
(eq *test-var* k)) |
| 228 |
(setf failure-p t))))))) |
| 229 |
(mapcar #'mp:process-kill threads) |
| 230 |
(is (not failure-p))))) |
| 231 |
|
| 232 |
|