;****************************************************************
;*	Program:	pfmfilwrt
;*	Author:		Bruce Ellis
;*	Synopsis:	Picks up page fault monitor buffers
;*			and dumps them out to a data file
;*	Notes:		data file:	pfdata.dat
;*			to initiate the user must call $setpfm
;*			this program must be located in sys$system:
;****************************************************************
	.link	/sys$system:sys.stb/
	.macro	check	?l
	blbs	r0,l
	$exit_s	r0
l:	
	.endm	check
outfab:	$fab	fnm=<pfdata.dat>,alq=4096,fac=PUT,fop=<UFO,CBT,CIF>,-
		mrs=512,org=SEQ,rfm=FIX
block:	.blkb	512
ochan:	.blkl	1
exit_sts:
	.blkl	1
exit_block:
	.blkl	1
	.address	exit_h
	.long	1
	.address	exit_sts
flag:	.blkl	1
;****************************************************************
;	Code to setup for and handle $wakes from pfm$mon
;****************************************************************
	.entry	pfmfilwrt,^m<r10>
	clrl	flag				;Note not in exit handler
	$dclexh_s	desblk=exit_block	;set up exit handler
	check
	$create	fab=outfab			;open/create the output file
	check
	movw	fab$l_stv+outfab,ochan		;save off channel to file
	movl	#1,r10				;set up virtual block counter
sleep:	blbs	flag,exit_h_ret			;If here because exit scram
	$hiber_s			;z-out till we are busy
	check
get_next:
;	pushr	#^m<r0,r1,r2,r3,r4,r5>
;	movc5	#0,.,#0,#512,block
;	popr	#^m<r0,r1,r2,r3,r4,r5>
	$cmkrnl_s	routin=grab_pf	;Go grab the buffer
	cmpl	#ss$_nodata,r0		;continue to grab buffers till
	beql	sleep			;	empty
;	check
	jsb	flush			;else flush current buffer
	brb	get_next		;and grab next buffer

	.entry	exit_h,^m<>
	movl	#1,flag		;note in exit handler
	brw	get_next	;grab last buffers
exit_h_ret:
	ret			;leave exit handler

flush:					;Routine to flush buffers
;(((((( For now we dump 1 block at a time better to dump more))))))
	$qiow_s	chan=ochan,func=#io$_writevblk,p1=block,p2=#512,p3=r10
	check
	incl	r10			;update vbn in file
	rsb				;scram
;****************************************************************
;*	Routine to grab the buffer
;****************************************************************
	.entry	grab_pf,^m<r2,r3,r4,r5>
	moval	exc,(fp)		;Do not want to crash system
	moval	block,r1		;setup buffer for spilling pf data
	movl	#512,r2			;set up max buffer size
	jsb	g^pfm$getbuf		;grab the buffer
	ret				;scram
	.entry	exc,^m<>	;exit in kernel so no crashes
	$exit_s
	.end	pfmfilwrt
