SpyKernelImpl.mesa
Last edited by Bruce 14-Feb-81 19:30:51
Last edited by MBrown 16-Aug-81 13:30:07
Last edited by Levin 2-Nov-81 18:16:29
Last edited by Maxwell September 16, 1983 3:44 pm
DIRECTORY
Basics USING [BITAND],
BasicTime USING [GetClockPulses, Pulses, PulsesToMicroseconds],
DebuggerSwap USING [CallDebugger],
FastBreak USING [FastBreakProc],
IntervalTimer USING [WaitForExpirationInterval],
IntervalTimerFace USING [exists],
PrincOps,
PrincOpsUtils USING [DisableInterrupts, EnableInterrupts, LongNotify, LongReEnter, LongWait, PsbIndexToHandle, PsbHandleToIndex, ReadPSB, ReadPTC],
PageMap USING [GetF, flagsVacant],
Process USING [Detach, GetCurrent, GetPriority, InitializeCondition, Priority, priorityRealTime, priorityNormal, SecondsToTicks, SetPriority],
Rope USING [ROPE],
SafeStorage USING [NWordsAllocated, NWordsReclaimed],
Loader USING [MakeProcedureResident, MakeGlobalFrameResident],
SpyClient USING [DataType],
SpyLog USING [active, Here, OpenForWrite, WriteData],
SpyOps USING [Count, DataType, Frame, SetAllocationBreak, SpyState, Stack, stackHeader, stackLength, StackType],
Terminal USING [Current, Virtual, WaitForBWVerticalRetrace],
VM USING [PageNumberForAddress],
VMStatistics USING [pageFaults];
SpyKernelImpl: MONITOR
IMPORTS
Basics, BasicTime, DebuggerSwap, Loader, PrincOpsUtils, Process, SafeStorage, SpyLog, SpyOps, Terminal, VM, VMStatistics
EXPORTS SpyClient, SpyOps =
BEGIN OPEN PrincOps, Rope;
-- SpyKernel parameters --
spyState: PUBLIC SpyOps.SpyState ← off;
watching: PUBLIC SpyOps.DataType ← CPU;
justMe: PUBLIC PrincOps.PsbIndex ← 0;
general statistics
runningTime: PUBLIC BasicTime.Pulses; -- total time running
pageFaults: PUBLIC LONG CARDINAL;
active, starts, stops: PUBLIC INTEGER ← 0;
wordsAllocated: PUBLIC LONG CARDINAL;
wordsReclaimed: PUBLIC LONG CARDINAL;
code: PUBLIC SpyOps.Count ← 0; -- page faults on code
data: PUBLIC SpyOps.Count ← 0; -- page faults on data
****************************************************************************
initializing the Spy and data
****************************************************************************
InitializeSpy: PUBLIC ENTRY PROCEDURE[
dataType: SpyClient.DataType ← CPU,
process: PrincOps.PsbIndex ← PrincOps.PsbNull,
spyOnSpyLog: BOOL ← FALSE]
RETURNS[errorMsg: ROPE] =
BEGIN
DisableSpy[];
IF dataType IN [allocations..wordsAllocated] THEN errorMsg ← SpyOps.SetAllocationBreak[];
IF errorMsg # NIL THEN RETURN[errorMsg];
InitializeTables[];
watching ← dataType;
justMe ← IF dataType = process THEN process ELSE 0;
SpyLog.OpenForWrite[spyOnSpyLog];
EnableSpy[];
END;
InitializeTables: PROCEDURE =
BEGIN
runningTime ← 0;
code ← data ← 0;
wordsAllocated ← wordsReclaimed ← 0;
END;
Initialize: PROCEDURE =
BEGIN
dolphin ← BasicTime.PulsesToMicroseconds[1]#32; -- hack
Loader.MakeProcedureResident[Spy];
Loader.MakeProcedureResident[Record];
Loader.MakeProcedureResident[UserBreak];
Loader.MakeProcedureResident[PageFaultRecorder];
Loader.MakeGlobalFrameResident[Initialize];
Process.InitializeCondition[@processDead, 10000];
SetTimeouts[];
END;
SetTimeouts: PROC =
BEGIN
ticks: PrincOps.Ticks;
cutoff: PrincOps.Ticks;
found: BOOLEAN;
frame: PrincOps.FrameHandle;
IF IntervalTimerFace.exists THEN RETURN;
cutoff ← Process.SecondsToTicks[10];
PrincOpsUtils.DisableInterrupts[]; -- stops the clock. NO PAGE FAULTS ALLOWED!
ticks ← PrincOpsUtils.ReadPTC[];
FOR psbi: PsbIndex IN [PrincOps.StartPsb..PrincOps.StartPsb+PrincOps.PDA.count) DO
IF PrincOps.PDA.block[psbi].mds = 0 THEN LOOP;
IF PrincOps.PDA.block[psbi].mds - ticks > cutoff THEN LOOP; -- not significant
IF PrincOps.PDA.block[psbi].link.vector
THEN frame ← PDA[PrincOps.PDA.block[psbi].context.state].frame
ELSE frame ← PrincOps.PDA.block[psbi].context.frame;
found ← FALSE;
FOR i: CARDINAL IN [0..index) DO
IF timeout[i] = [frame.accesslink.gfi, frame.pc] THEN {found ← TRUE; EXIT};
ENDLOOP;
IF found THEN LOOP;
timeout[index] ← [frame.accesslink.gfi, frame.pc];
index ← index + 1;
IF index = maxIndex THEN EXIT;
ENDLOOP;
PrincOpsUtils.EnableInterrupts[];
END;
ZeroLog: PUBLIC PROCEDURE = {
SpyLog.Initialize[NIL, 40, TRUE];
SpyLog.Open[TRUE]};
****************************************************************************
starting and stopping the Spy
****************************************************************************
Note: These procedures need not be resident.
processDead: CONDITION;
spyProcess: PROCESSNIL;
oldSpyState: SpyOps.SpyState ← off;
startTime: BasicTime.Pulses;
startPageFaults: LONG CARDINAL;
startWordsAllocated: LONG CARDINAL;
startWordsReclaimed: LONG CARDINAL;
StartCounting: PUBLIC ENTRY FastBreak.FastBreakProc =
BEGIN
priority: Process.Priority;
starts ← starts + 1;
IF active = 0 THEN WHILE spyProcess # NIL DO WAIT processDead; ENDLOOP;
IF (active ← active + 1) > 1 THEN RETURN[useOldBreak: FALSE]; -- a psuedo start
startPageFaults ← VMStatistics.pageFaults;
startWordsAllocated ← SafeStorage.NWordsAllocated[];
startWordsReclaimed ← SafeStorage.NWordsReclaimed[];
startTime ← BasicTime.GetClockPulses[];
IF watching = breakProcess THEN justMe ← LOOPHOLE[Process.GetCurrent[]];
IF justMe # 0 THEN justMePrincOps ← @PrincOps.PDA[PrincOpsUtils.PsbIndexToHandle[justMe]];
SetTimeouts[]; -- do it a second time in case the first time missed some
priority ← Process.GetPriority[];
Process.SetPriority[Process.priorityRealTime];
SELECT watching FROM
CPU, process, breakProcess => Process.Detach[spyProcess ← FORK Spy[]];
pagefaults => Process.Detach[spyProcess ← FORK PageFaultRecorder[]];
allocations, wordsAllocated => {
RTStorageAccounting.AllocatorCallbackProcForSpy ← SensitiveBreak;
spyState ← on};
ENDCASE => spyState ← on;
Process.SetPriority[priority];
RETURN[useOldBreak: FALSE];
END;
StopCounting: PUBLIC ENTRY FastBreak.FastBreakProc =
BEGIN
IF active = 0 THEN RETURN[useOldBreak: FALSE]; -- already stopped
stops ← stops + 1;
IF (active ← active - 1) > 0 THEN RETURN; -- a psuedo stop
spyState ← off;
runningTime ← runningTime + BasicTime.GetClockPulses[] - startTime;
pageFaults ← pageFaults + VMStatistics.pageFaults - startPageFaults;
wordsAllocated ← wordsAllocated + SafeStorage.NWordsAllocated[] - startWordsAllocated;
wordsReclaimed ← wordsReclaimed + SafeStorage.NWordsReclaimed[] - startWordsReclaimed;
RETURN[useOldBreak: FALSE];
END;
DisableSpy: PROCEDURE =
BEGIN
IF spyState = disabled THEN RETURN;
oldSpyState ← spyState;
spyState ← disabled;
END;
EnableSpy: PROCEDURE = {spyState ← oldSpyState};
*********************************************************************
watching CPU
*********************************************************************
All code invoked by this process should be resident
justMePrincOps: LONG POINTER TO ProcessStateBlock ← NIL;
wait: CARDINAL ← 0; -- used with dolphins
monitor: BOOLEANFALSE; -- measure performance of spy
dolphin: BOOLEANFALSE;
maxStackDepth: CARDINAL = 200;
timeout: ARRAY [0..maxIndex) OF RECORD[gfi: PrincOps.GFTIndex, pc: CARDINAL];
maxIndex: CARDINAL = 20;
index: CARDINAL← 0;
sampleInterval: INT ← 10*1000; -- measured in microseconds
screen: Terminal.Virtual = Terminal.Current[];
Spy: PROCEDURE =
BEGIN
top: PsbIndex;
frame: FrameHandle;
myPrincOps: PsbHandle = PrincOpsUtils.ReadPSB[];
handleMask: PsbLink = [failed: FALSE, priority: 0, next: LAST[PsbIndex], reserved: 0, vector: FALSE];
NextHandle: PROCEDURE [link: CARDINAL] RETURNS [PsbHandle] = INLINE
{RETURN[LOOPHOLE[Basics.BITAND[link, LOOPHOLE[handleMask]]]]};
SearchReadyList: PROCEDURE = INLINE
BEGIN
skip, once: BOOLEANFALSE;
headOfReadyList, current: PsbHandle;
PrincOpsUtils.DisableInterrupts[];
top ← PrincOps.PsbNull;
headOfReadyList ← NextHandle[LOOPHOLE[PDA.ready]];
headOfReadyList ← NextHandle[LOOPHOLE[PDA[headOfReadyList].link]]; -- want the SECOND psb.
FOR current ← headOfReadyList, NextHandle[LOOPHOLE[PDA[current].link]] DO
psb: LONG POINTER TO ProcessStateBlock = @PDA[current];
link: PsbLink = psb.link;
level: Process.Priority = link.priority;
IF current = headOfReadyList THEN IF once THEN EXIT ELSE once ← TRUE;
IF level = 0 THEN LOOP;
IF current = myPrincOps THEN LOOP;
IF ~link.vector AND PDA.state[level] = NullStateVectorHandle THEN LOOP; -- no SV.
frame ← IF link.vector THEN PDA[psb.context.state].frame ELSE psb.context.frame;
skip over a process that appears in the ready queue because it just timed out.
(The Spy wakes up with all of the other timeouts. Since it is the highest priority,
it will run first. All of the other timeouts will appear on the ready list. Most likely,
they will just check some condition and then go back to sleep. This will mask the
more interesting processes.)
skip ← FALSE;
FOR i: CARDINAL IN [0..maxIndex) DO
IF timeout[i] = [0, 0] THEN EXIT;
IF timeout[i] # [frame.accesslink.gfi, frame.pc] THEN LOOP;
skip ← TRUE; EXIT;
ENDLOOP;
IF skip THEN LOOP;
we have a good process!
top ← PrincOpsUtils.PsbHandleToIndex[current];
EXIT; ENDLOOP;
PrincOpsUtils.EnableInterrupts[];
END;
spyState ← on;
-- MAIN LOOP --
DO -- IF IntervalTimerFace.exists
-- THEN IntervalTimer.WaitForExpirationInterval[sampleInterval] ELSE
Terminal.WaitForBWVerticalRetrace[screen];
IF active <= 0 THEN EXIT;
IF spyState = disabled THEN LOOP;
IF dolphin AND wait > 0 THEN {wait ← wait - 1; LOOP} ELSE wait ← 8;
IF monitor THEN SpyLog.Here[];
SearchReadyList[];
SELECT TRUE FROM
justMe = 0 => Record[top, frame];
justMePrincOps.link.failed => Record[justMe, NIL, 2]; -- waiting ML
justMePrincOps.flags.waiting => Record[justMe, NIL, 3]; -- waiting CV
OnQueue[justMe, @PDA.fault[PrincOps.qPageFault].queue] =>
Record[justMe, NIL, 1]; -- waiting pagefault
OnQueue[justMe, @PDA.fault[PrincOps.qPageFault+4].queue] =>
Record[justMe, NIL, 1]; -- waiting pagefault
OnQueue[justMe, @PrincOps.PDA.ready] =>
SELECT TRUE FROM
PDA.state[justMePrincOps.link.priority] = NullStateVectorHandle AND
~justMePrincOps.link.vector => Record[justMe, NIL, 5]; -- waiting SV
justMe # top => Record[justMe, NIL, 4]; -- prempted by a higher priority process
ENDCASE => Record[justMe]; -- ready
ENDCASE => Record[justMe, NIL, 6]; -- in some unknown state
IF monitor THEN SpyLog.Here[];
ENDLOOP;
spyProcess ← NIL;
spyState ← off;
NotifyProcessDead[];
Process.SetPriority[Process.priorityNormal];
END;
OnQueue: PROC[psbi: PsbIndex, queueHandle: PrincOps.QueueHandle]
RETURNS[BOOL] = INLINE
BEGIN
tail, prev: PsbIndex;
IF queueHandle^ = PrincOps.QueueEmpty THEN RETURN[FALSE];
prev ← tail ← queueHandle.tail;
THROUGH [FIRST[PsbIndex]..LAST[PsbIndex]+1] -- garbage protection --
DO next: PsbIndex = PrincOps.PDA.block[prev].link.next;
IF next = psbi THEN RETURN[TRUE];
prev ← next;
IF prev = tail THEN RETURN[FALSE];
ENDLOOP;
RETURN[FALSE] -- actually, the queue is thoroughly mangled! --
END;
*********************************************************************
watching pagefaults
*********************************************************************
recordPageFaulted: BOOLEANFALSE;
Data: TYPE = RECORD[process: CARDINAL, page: INTEGER];
CodeBase: TYPE = LONG POINTER TO PACKED ARRAY [0..0) OF PrincOps.op;
PageFaultRecorder: PROCEDURE = -- stolen from Ben.mesa
BEGIN
fault: Data;
type: CARDINAL;
codePage: INTEGER;
handle: PrincOps.PsbHandle;
frame: PrincOps.FrameHandle;
pda: PrincOps.PDABase = PrincOps.PDA;
qPageFault: PrincOps.FaultIndex = PrincOps.qPageFault;
pPageFaultCondition: LONG POINTER TO PrincOps.Condition =
@pda.fault[qPageFault].condition;
pPageFaultCONDITION: LONG POINTER TO CONDITION =
LOOPHOLE[pPageFaultCondition];
recorderLock: MONITORLOCK;
shouldNotifyPilot: BOOLEANFALSE;
NO PAGE FAULTS ALLOWED!
spyState ← on;
DO
wait for a page fault:
PrincOpsUtils.LongWait[@recorderLock, pPageFaultCONDITION, 1];
UNTIL PrincOpsUtils.LongReEnter[@recorderLock, pPageFaultCONDITION] DO
NULL ENDLOOP;
IF pda.fault[qPageFault].queue.tail = PrincOps.PsbNull THEN { -- timed out
IF shouldNotifyPilot
AND pda.fault[qPageFault].condition.tail ~= PrincOps.PsbNull THEN {
LongNakedNotify[pPageFaultCONDITION];
shouldNotifyPilot ← FALSE};
IF active <= 0 THEN EXIT;
LOOP}; -- go back and wait again.
figure out who faulted:
fault.process ← pda.block[pda.fault[qPageFault].queue.tail].link.next; -- walk to tail, then to head.
handle ← PrincOpsUtils.PsbIndexToHandle[fault.process];
fault.page ← VM.PageNumberForAddress[pda[pda[handle].context.state].memPointer];
frame ← IF pda[handle].link.vector
THEN pda[pda[handle].context.state].frame
ELSE pda[handle].context.frame;
codePage ← LOOPHOLE[frame.accesslink.code.longbase+frame.pc/2, INT]/256;
SELECT TRUE FROM
ABS[codePage - fault.page] <= 1 => {code ← code + 1; type ← 2};
Xfer[frame.accesslink.code.longbase, frame.pc] => {code ← code + 1; type ← 3};
ENDCASE => {data ← data + 1; type ← 1};
wake up the Pilot fault handler:
PrincOpsUtils.DisableInterrupts[];
IF pPageFaultCondition^.tail = PrincOps.PsbNull
THEN shouldNotifyPilot ← TRUE -- Pilot not ready for this fault yet...
ELSE LongNakedNotify[pPageFaultCONDITION];
PrincOpsUtils.EnableInterrupts[];
log the fault:
IF active <= 0 THEN EXIT;
IF recordPageFaulted THEN SpyLog.WriteData[@fault, SIZE[Data], CODE[Data]];
Record[fault.process, NIL, type];
ENDLOOP;
spyProcess ← NIL;
spyState ← off;
NotifyProcessDead[];
Process.SetPriority[Process.priorityNormal];
END;
Xfer: PROCEDURE[base: CodeBase, pc: PrincOps.BytePC] RETURNS[BOOLEAN] =
INLINE BEGIN -- is the current pc pointing to some sort of xfer?
IF base[pc] IN [PrincOps.zEFC0..PrincOps.zKFCB] THEN RETURN[TRUE];
RETURN[FALSE];
END;
LongNakedNotify: PROCEDURE [pCondition: LONG POINTER TO CONDITION] = INLINE {
Used ONLY to notify a condition from a high priority process outside the relevant monitor.
pCond: LONG POINTER TO PrincOps.Condition = LOOPHOLE[pCondition];
PrincOpsUtils.DisableInterrupts[];
IF pCond^.tail=PrincOps.PsbNull
THEN {pCond^.wakeup ← TRUE; PrincOpsUtils.EnableInterrupts[]}
ELSE {PrincOpsUtils.EnableInterrupts[]; PrincOpsUtils.LongNotify[pCondition]}};
NotifyProcessDead: ENTRY PROCEDURE = INLINE {NOTIFY processDead};
***************************************************************************
recording the information
***************************************************************************
stack: SpyOps.Stack;
break: BOOLEANFALSE;
breakAtom: ATOMNIL;
AllocationBreak: PUBLIC FastBreak.FastBreakProc = BEGIN
data: FastBreakData, frame: PrincOps.FrameHandle, sv: PrincOps.SVPointer
words: CARDINAL;
type: SpyOps.StackType;
local: POINTER TO ARRAY [0..7] OF CARDINAL;
IF active = 0 THEN RETURN;
IF watching NOT IN [allocations..wordsAllocated] THEN RETURN;
words ← 1; type ← 0; IF watching = wordsAllocated THEN local ← @frame.local[0];
IF data = NIL AND watching = wordsAllocated THEN words ← local[0];
IF data = LOOPHOLE[$Permanent, LONG POINTER] THEN {
type ← 1; IF watching = wordsAllocated THEN words ← local[2]};
IF data = LOOPHOLE[$Unsafe, LONG POINTER] THEN {
type ← 2; IF watching = wordsAllocated THEN words ← local[4]};
IF break AND data = LOOPHOLE[breakAtom, LONG POINTER]
THEN DebuggerSwap.CallDebugger["Allocation break"];
Record[
psbi: LOOPHOLE[Process.GetCurrent[]],
type: type,
frame: frame,
count: words];
END;
UserBreak: PUBLIC FastBreak.FastBreakProc =
TRUSTED BEGIN
count: CARDINAL ← 1;
type: SpyOps.StackType ← 1;
IF spyState # on THEN RETURN[useOldBreak: FALSE];
IF watching # userDefined THEN RETURN[useOldBreak: FALSE];
IF data # NIL THEN type ← LOOPHOLE[data, LONG POINTER TO SpyOps.StackType]^;
Record[LOOPHOLE[Process.GetCurrent[]], frame, type, count];
RETURN[useOldBreak: FALSE];
END;
Record: PUBLIC ENTRY PROCEDURE[psbi: PsbIndex, frame: FrameHandle ← NIL,
type: SpyOps.StackType ← 0, count: CARDINAL ← 1] =
BEGIN
level: Process.Priority;
psb: LONG POINTER TO ProcessStateBlock;
IF spyState = disabled THEN RETURN;
IF active <= 0 THEN RETURN;
IF psbi = PrincOps.PsbNull THEN
IF PDA.fault[qPageFault].queue.tail = PrincOps.PsbNull
AND PDA.fault[qPageFault+4].queue.tail = PrincOps.PsbNull
THEN {LogStack[psbi, 0, NIL, count, 0]; RETURN}
ELSE {LogStack[psbi, 0, NIL, count, 1]; RETURN}; -- waiting for a page fault
psb ← @PDA[PrincOpsUtils.PsbIndexToHandle[psbi]];
level ← psb.link.priority;
IF frame = NIL THEN
frame ← IF psb.link.vector THEN PDA[psb.context.state].frame ELSE psb.context.frame;
LogStack[psbi, level, frame, count, type]
END;
LogStack: INTERNAL PROC [process: PrincOps.PsbIndex, level: Process.Priority, frame: FrameHandle, count, type: CARDINAL] = -- Log the stack on the Trace Log --
INLINE BEGIN
sdLength: CARDINAL = PrincOps.SD[PrincOps.sGFTLength];
gfi: GFTIndex ← 0;
gf: GlobalFrameHandle ← NIL;
length: CARDINAL ← 0;
GfiToGF: PROCEDURE [gfi: GFTIndex] RETURNS [GlobalFrameHandle] = INLINE
{RETURN[PrincOps.GFT[gfi].framePtr]};
ValidFrame: PROCEDURE [f: ControlLink] RETURNS [FrameHandle] =
BEGIN OPEN PrincOps;
Note: as a side-effect, this procedure sets 'gf' and 'gfi', which are
used by the other local procedures below and the main loop of IncrementBucket.
IF f.proc OR f.indirect OR f.frame =NIL OR (gf ← f.frame.accesslink) = NIL OR
PageMap.GetF[Basics.LongDiv[LOOPHOLE[LONG[gf]], wordsPerPage]].flags = flagsVacant OR
(gfi ← gf.gfi) > sdLength OR GfiToGF[gfi] ~= gf THEN RETURN[NIL];
RETURN[f.frame]
END;
IF ~SpyLog.active THEN RETURN;
stack.type ← type;
stack.count ← count;
stack.level ← level;
stack.process ← process;
frame ← ValidFrame[LOOPHOLE[frame]];
THROUGH [0..maxStackDepth) UNTIL frame = NIL DO
save the current frame
this also saves the last ignored frame
IF length >= SpyOps.stackLength THEN {stack.level ← 7; length ← 0; EXIT};
stack.frame[length] ← [0, gfi, frame.pc];
IF length > 0 AND stack.frame[length] = stack.frame[length-1]
THEN length ← length - 1; -- skip recursive frames
length ← length + 1;
frame ← ValidFrame[frame.returnlink];
ENDLOOP;
SpyLog.WriteData[@stack,
SpyOps.stackHeader + length*SpyOps.Frame.SIZE,
SpyOps.Stack.CODE];
END;
Initialize[];
22-Jan-82 Maxwell: Removed cross-partition code; converted to Cedar
4-Feb-82 Maxwell: Added stack active option
11-Mar-82 Maxwell: Added page fault recorder
October 14, 1982 10:07 am Maxwell: Removed Pilot Spy code
END.
since the Spy AND timeouts are synchronized to the vertical retrace,
we need to skip over processes that have just timed out. The following
[gfi, pc] pairs indicate WAITs on timeouts.
timeout[0] ← [gfi: 100B, pc: 114B];
WAIT harwareVerticalRetrace IN UserTerminalImpl.WaitForVerticalRetrace
timeout[1] ← [gfi: 100B, pc: 143B];
WAIT verticalRetrace IN UserTerminalImpl.WaitForVerticalRetrace
timeout[2] ← [gfi: 401B, pc: 2316B];
Wait[Process.MSecToTicks[5000]] IN InscriptImpl.WaitForEntry
timeout[3] ← [gfi: 401B, pc: 3307B];
WAIT pageDone IN InscriptImpl.MaintainPage
timeout[4] ← [gfi: 1075B, pc: 12265B];
WAIT iIncr IN TEditTypeScriptImpl.GetChar
timeout[5] ← timeout[6] ← timeout[7] ← timeout[8] ← timeout[9] ← [0, 0];
END;