File: mp-test.lisp

package info (click to toggle)
cmucl 21d-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 45,328 kB
  • sloc: lisp: 378,758; ansic: 30,673; asm: 2,977; sh: 1,417; makefile: 357; csh: 31
file content (351 lines) | stat: -rw-r--r-- 11,033 bytes parent folder | download | duplicates (12)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
;;;
;;; **********************************************************************
;;; This code was written by Douglas T. Crosher and has been placed in
;;; the Public domain, and is provided 'as is'.
;;;
;;; $Id: mp-test.lisp,v 1.9 1998/01/01 06:48:45 dtc Exp $
;;;
;;; **********************************************************************
;;;
;;; Stack-group and multi-process support for CMUCL x86.
;;;
;;; Test code and examples.
;;;

(in-package "MP")

;;;; Bindings stack

;;; Show the current binding stack.
(defun show-binding-stack ()
  (let* ((binding-stack-pointer (kernel:binding-stack-pointer-sap))
	 (binding-stack 
	  (sys:int-sap (alien:extern-alien "binding_stack" alien:unsigned)))
	 (size (sys:sap- binding-stack-pointer binding-stack)))
    (declare (type (unsigned-byte 29) size))
    (do ((binding 0 (+ 8 binding)))
	((= binding size))
      (declare (type (unsigned-byte 29) binding))
      (let* ((value 
	      (kernel:make-lisp-obj
	       (sys:sap-int (sys:sap-ref-sap binding-stack binding))))
	     (symbol
	      (kernel:make-lisp-obj
	       (sys:sap-int (sys:sap-ref-sap binding-stack (+ binding 4))))))
	(format t "~s ~s~%" symbol value)))))

(defun tst-binding ()
  (show-binding-stack)
  (unbind-binding-stack)
  (multiple-value-bind (stack size)
      (save-binding-stack #())
    (restore-binding-stack stack size))
  (rebind-binding-stack)
  (show-binding-stack))

;;;; Alien stack

(defun tst-alien ()
  (alien:with-alien ((buf (array char 256)))
    (format t "~s~%" buf)
    (multiple-value-bind (save-stack size alien-stack)
	(save-alien-stack (make-array 0 :element-type '(unsigned-byte 32)))
      (restore-alien-stack save-stack size alien-stack))
    (format t "~s~%" buf)))

;;;; Control stack

(defun show-control-stack (control-stack-id)
  (declare (type lisp::index control-stack-id))
  (let ((stack (aref x86::*control-stacks* control-stack-id)))
    (declare (type (or null (simple-array (unsigned-byte 32) (*))) stack))
    (when stack
      (format t "Saved control stack ~d~%" control-stack-id)
      ;; First element has the stack-pointer.
      (let ((stack-pointer (aref stack 0))
	    (length (length stack)))
	(do ((addr (- (alien:extern-alien "control_stack_end" alien:unsigned)
		      4)
		   (- addr 4))
	     (index 1 (1+ index)))
	    ((or (< addr stack-pointer)
		 (>= index length)))
	  (declare (type (unsigned-byte 32) addr)
		   (type (unsigned-byte 29) index))
	  (format t "0x~8x : 0x~8x~%" addr (aref stack index)))
	(format t "Stack pointer: 0x~x~%" (aref stack 0))
	(format t "Return address: 0x~x~%" (aref stack (- length 2)))
	(format t "Frame pointer: 0x~x~%" (aref stack (- length 1)))))))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;; Multi-process example.

;;; All the processes are going to write to the standsard output. Use
;;; an output lock to prevent conflict; also provides a good test.
(defvar *output-lock* nil)

;;; Results stack.
(defvar *results* nil)

;;; Do some time consuming work. Occasionally write out some results,
;;; and when done place the result the the *results* stack.
(defun work (itter msg)
  (declare (fixnum itter))
  (let ((sum 1d0))
    (declare (double-float sum))
    (do ((i 0  (1+ i)))
	((> i itter))
      (declare (fixnum i))
      (dotimes (i 1000000)
	(declare (fixnum i))
	(incf sum 1d-6)
	(incf sum 1d-6))
      (with-lock-held (*output-lock* "Waiting for output lock")
	(format t "~a ~d ~s~%" msg i sum))
      ;; May want to yield occasionally if an interrupt isn't going to
      ;; force a yield.
      #+nil (process-yield))
    (push sum *results*)))


;;;;
;;; Test catch, unwind, throw.
(declaim (double-float *work2-sum*))
(defvar *work2-sum* 0d0)

(defun work2 (itter msg)
  (declare (fixnum itter))
  (let ((*work2-sum* 0d0))
    (do ((i 0  (1+ i)))
	((> i itter))
      (declare (fixnum i))
      (dotimes (i 100000)
	(declare (fixnum i))
	(incf *work2-sum* (catch 'work2-sum
			    (work2b))))
      (with-lock-held (*output-lock* "Waiting for output lock")
	(format t "~a ~d ~s~%" msg i *work2-sum*))
      ;; May want to yield occasionally if an interrupt isn't going to
      ;; force a yield.
      #+nil (process-yield))
    (push *work2-sum* *results*)))

(defun work2b ()
  (unwind-protect
       (work2c)
    (incf *work2-sum* 1d-6)))

(defun work2c ()
  (unwind-protect
       (work2d)
    (incf *work2-sum* 1d-6)))

(defun work2d ()
  (unwind-protect
       (throw 'work2-sum 1d-6)
    (incf *work2-sum* 1d-6)))
;;;;

;;; Some process will require a lock to do any work.
(defvar *work-lock* nil)


(declaim (fixnum *int-count*))
(defvar *int-count* 0)

(declaim (fixnum *count* *local-count*))
(defvar *count* 0)
(defvar *local-count* 0)

(defun tst (&optional (scale 1))
  (init-multi-processing)
  ;; Start the yield interrupt - for the brave.
  (start-sigalrm-yield 0 5000)
  ;;
  (setf *output-lock* (make-lock "Output lock"))

  ;; Process to periodically show the processes.
  (let ((show-processes-process
	 (make-process
	  #'(lambda ()
	      (unwind-protect ; Test process unwinding when destroyed.
		   (loop
		    (process-wait-with-timeout "Sleeping" 2 #'(lambda () nil))
		    (with-lock-held (*output-lock* "Waiting for output lock")
		      (format t "-=-=-=-=-=-~%")
		      (format t "All processes:~%")
		      (show-processes t)
		      (format t "-=-=-=-=-=-~%")))
		(with-lock-held (*output-lock* "Waiting for output lock")
		  (format t "Process ~s unwinding~%" *current-process*))))
	  :name "Show processes")))
    
    (setf *results* nil)
    ;; Process to check and print and results pushed onto the
    ;; *results* stack. Will timeout if there have been no result for
    ;; 20 seconds, and kill the show-processes-process.
    (make-process
     #'(lambda ()
	 (loop
	  (let ((results
		 (process-wait-with-timeout "Waiting for results" (* 20 scale)
					      #'(lambda () *results*))))
	    (when (null results)
		(with-lock-held (*output-lock* "Waiting for output lock")
		  (format t "~s Timeout~%" *current-process*))
		(destroy-process show-processes-process)	      
		(return))
	    (with-lock-held (*output-lock* "Waiting for output lock")
	      (format t "Results: ~s~%" results))
	    (setf *results* nil))))
     :name "Show results"))
  
  (progn
    ;; Two results generating processes, running in parallel.
    (make-process #'(lambda () (work (* 5 scale) "P1 working"))
		  :name "Worker 1")
    (make-process #'(lambda () (work2 (* 10 scale) "P2 working"))
		  :name "Worker 2"))
  
  (progn
    ;; Two processes competing over a lock to work.
    (setf *work-lock* (make-lock "Work lock"))
    (make-process
     #'(lambda ()
	 (dotimes (i (* 5 scale))
	   (with-lock-held (*work-lock* "Waiting for work lock")
	     (work (* 1 scale) "P4 working"))))
     :name "Worker 4")
    (make-process
     #'(lambda ()
	 (dotimes (i (* 5 scale))
	   (with-lock-held (*work-lock* "Waiting for work lock")
	     (work (* 1 scale) "P5 working"))))
     :name "Worker 5"))
  
  ;; Local special counter.
  (setq *count* 0)
  (setq *local-count* 0)
  ;; New processes do not inherit local special bindings and will thus
  ;; see the global value of *local-count* even though the parent
  ;; process makes a local binding.
  (let ((*local-count* 20))
    (make-process
     #'(lambda ()
	 (dotimes (i (* 10 scale))
	   (with-lock-held (*output-lock* "Waiting for output lock")
	     (incf *count*)
	     (incf *local-count*)
	     (format t "~s ~d ~d ~d~%"
		     *current-process* i *local-count* *count*))
	   (process-wait-with-timeout "Sleeping" 2 #'(lambda () nil))))
     :name "Counter 2"))
  ;; This process makes a local binding of *local-count*.
  (make-process
   #'(lambda ()
       (let ((*local-count* 0))
	 (dotimes (i (* 10 scale))
	   (with-lock-held (*output-lock* "Waiting for output lock")
	     (incf *count*)
	     (incf *local-count*)
	     (format t "~s ~d ~d ~d~%"
		     *current-process* i *local-count* *count*))
	   (process-wait-with-timeout "Sleeping" 2 #'(lambda () nil)))))
   :name "Counter 1")
  
  ;; Recursively interrupted processes.
  (let (;; Setup three sleepers that will be interrupted.
	(ps1 (make-process
	      #'(lambda ()
		  (process-wait-with-timeout "Sleeping" (* 20 scale)
					     #'(lambda () nil)))
	      :name "Sleeper 1"))
	(ps2 (make-process
	      #'(lambda ()
		  (process-wait-with-timeout "Sleeping" (* 20 scale)
					     #'(lambda () nil)))
	      :name "Sleeper 2"))
	(ps3 (make-process
	      #'(lambda ()
		  (process-wait-with-timeout "Sleeping" (* 20 scale)
					     #'(lambda () nil)))
	      :name "Sleeper 3"))
	interrupt)
    (setq *int-count* 0)
    (setq interrupt
	  #'(lambda ()
	      (with-lock-held (*output-lock* "Waiting for output lock")
		(format t "Process ~s interrupted ~d~%"
			*current-process* *int-count*)
		(incf *int-count*)
		(process-wait-with-timeout "Sleeping" 1 #'(lambda () nil))
		(cond ((eq *current-process* ps1)
		       (process-interrupt ps2 interrupt))
		      ((eq *current-process* ps2)
		       (process-interrupt ps3 interrupt))
		      ((eq *current-process* ps3)
		       (process-interrupt ps1 interrupt))))))
    ;; Start the ball rolling.
    (process-interrupt ps1 interrupt))
  
  ;; Have the initial process do some work also.
  (work (* 10 scale) "Init. working"))

(defun tst-comp ()
  (start-sigalrm-yield 0 50000)
  ;; Try compiling two files simultaneously.
  (make-process
   #'(lambda ()
       (compile-file "irrat" :error-output nil :byte-compile t)))
  (make-process
   #'(lambda ()
       (compile-file "numbers" :error-output nil :byte-compile t))))


(defvar *tst-lock* (make-lock "Test lock"))

(defun tst-lock ()
  (declare (optimize (speed 3) (safety 0)))
  (let ((sum 0))
    (declare (fixnum sum))
    (dotimes (i 10000000)
      (declare (fixnum i))
      (with-lock-held (*tst-lock* "Waiting for test lock")
	(incf sum)))
    sum))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;

(defun tst-alien-stack-save ()
  (alien:with-alien ((buf (array char 256)))
    (format t "~s~%" buf)
    (multiple-value-bind (save-stack size alien-stack)
	(save-alien-stack (make-array 0 :element-type '(unsigned-byte 32)))
      (restore-alien-stack save-stack size alien-stack))
    (format t "~s~%" buf)))

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Test the stack-group switching speed.

(defun tst-sg-speed ()
  (init-stack-groups)
  (let* ((num-switch 0)
	 t1 t2)
    (declare (fixnum num-switch)
	     (type (or stack-group null) t1 t2))
    (setq t1 (make-stack-group "T1"
			       #'(lambda ()
				   (dotimes (i 100000)
				     (declare (fixnum i))
				     (incf num-switch)
				     (stack-group-resume t2)))))
    (setq t2 (make-stack-group "T2"
			       #'(lambda ()
				   (dotimes (i 100000)
				     (declare (fixnum i))
				     (incf num-switch)
				     (stack-group-resume t1)))))
    (stack-group-resume t1)
    (inactivate-stack-group t1)
    (inactivate-stack-group t2)
    num-switch))