[M3devel] Thread testing program
Mika Nystrom
mika at async.caltech.edu
Tue Jan 11 11:29:23 CET 2011
Hello everyone,
After studying failure modes and implementation of the current
threading implementation in CM3, I've developed a little stress-testing
program, attached. Currently it crashes in various ways.
It takes one command-line argument, the approximate number of threads
to create (e.g., 100). It creates threads of three types:
reader (reads a file), forker (forks a process), allocator (allocates
memory that constantly goes out of scope).
Files attached below. Happy 1/11/11.
Mika
My m3makefile:
import ("libm3")
implementation ("Main")
program ("threadtest")
My m3overrides (optional):
if defined("CM3_VERSION")
/* Critical Mass Modula-3 */
% see http://www.elegosoft.com/cm3/doc/help/cm3/cm3.html
% not sure about this:
build_standalone()
m3_optimize("")
end
My Main.m3:
(* $Id: Main.m3,v 1.1 2011/01/11 10:06:21 mika Exp $ *)
MODULE Main;
(* threading stress-test *)
IMPORT Params, Scan, Thread, Rd, FileRd, Wr, FileWr, Process;
VAR n := Scan.Int(Params.Get(1));
PROCEDURE MakeReaderThread() =
BEGIN
EVAL Thread.Fork(NEW(Thread.Closure, apply := RApply))
END MakeReaderThread;
PROCEDURE MakeForkerThread() =
BEGIN
EVAL Thread.Fork(NEW(Thread.Closure, apply := FApply))
END MakeForkerThread;
PROCEDURE MakeAllocatorThread() =
BEGIN
EVAL Thread.Fork(NEW(Thread.Closure, apply := AApply))
END MakeAllocatorThread;
(**********************************************************************)
PROCEDURE RApply(cl : Thread.Closure) : REFANY =
BEGIN
LOOP
WITH rd = FileRd.Open(Filename) DO
TRY
LOOP
VAR c := Rd.GetChar(rd); BEGIN END
END
EXCEPT
Rd.EndOfFile => Rd.Close(rd)
END
END
END
END RApply;
PROCEDURE FApply(cl : Thread.Closure) : REFANY =
BEGIN
LOOP
WITH proc = Process.Create("sleep",
ARRAY OF TEXT { "1" }) DO
EVAL Process.Wait(proc)
END
END
END FApply;
PROCEDURE AApply(cl : Thread.Closure) : REFANY =
BEGIN
LOOP
VAR
arr := NEW(REF ARRAY OF INTEGER, 1025);
BEGIN
FOR i := FIRST(arr^)+1 TO LAST(arr^) DO
arr[i] := arr[i] - arr[i-1]
END
END
END
END AApply;
CONST Filename = "hohum";
PROCEDURE WriteAFile() =
BEGIN
WITH wr = FileWr.Open(Filename) DO
FOR i := 1 TO 256 DO
FOR j := 1 TO i DO
Wr.PutChar(wr, VAL(ORD('A') + i MOD 25, CHAR))
END;
Wr.PutChar(wr, '\n')
END
END
END WriteAFile;
BEGIN
WriteAFile();
FOR i := 1 TO n DIV 3 DO
MakeReaderThread()
END;
FOR i := 1 TO n DIV 3 DO
MakeForkerThread()
END;
FOR i := 1 TO n DIV 3 DO
MakeAllocatorThread()
END;
LOOP
Thread.Pause(1.0d0)
END
END Main.
More information about the M3devel
mailing list