aboutsummaryrefslogtreecommitdiff
path: root/sys/fmio/fmfcache.x
blob: 6e1d16fec4c8092187fa7c185347df3b87e0622d (plain) (blame)
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# Copyright(c) 1986 Association of Universities for Research in Astronomy Inc.

include	<syserr.h>
include	<error.h>
include	<fset.h>
include	"fmset.h"
include	"fmio.h"

.help fcache
.nf -------------------------------------------------------------------------
FCACHE -- FMIO File (Buffer) Cache package.

    This package is used to manage a cache of lfile data buffers maintained on
a FMIO datafile.  The number and size of data buffers is user configurable:
the number of buffers by a call to FM_SETI to set FM_FCACHESIZE, and the buffer
size by a call to FSETI to set the file buffer size.

		  fm_fcinit (fm, cachesize)
                 fm_fcdebug (fm, out, what)
		  fm_fcsync (fm)
		  fm_fcfree (fm)

              fd = fm_getfd (fm, lfile, mode, type)
                   fm_retfd (fm, lfile)
                 fm_lockout (fm, lfile)
                  fm_unlock (fm, lfile)
	   bool = fm_locked (fm, lfile)

The cache is a conventional LRU cache.  FM_GETFD returns the file descriptor
of an open, cached file opened on an lfile, locking the file in the cache in
the process.  A subsequent call to FM_RETFD is required to allow the cache
slot to be reused.  FM_LOCKOUT may be used to lock a particular lfile out of
the cache; FM_UNLOCK releases the lock.
.endhelp --------------------------------------------------------------------

define	LEN_FCACHE	(3+($1)*LEN_FSLOT)	# len = LEN_FCACHE(cachesize)
define	FC_NFILES	Memi[$1]		# number of files in cache
define	FC_REFCNT	Memi[$1+1]		# cache reference count
define	FC_LFSTAT	Memi[$1+2]		# lfile statistics array
define	FC_FS		((($2)-1)*LEN_FSLOT+($1)+3)	# get slot pointer

define	LEN_FSLOT	4
define	FC_NREF		Memi[$1]		# number of refs to this slot
define	FC_LRU		Memi[$1+1]		# LRU count for slot
define	FC_LFILE	Memi[$1+2]		# lfile assigned to slot
define	FC_FD		Memi[$1+3]		# file descriptor


# FM_GETFD -- Map a FIO file descriptor onto an lfile and return the file
# descriptor.  The opened file descriptor remains in the cache after the
# access, until the file ages out of the cache.  

int procedure fm_getfd (fm, lfile, mode, type)

pointer	fm		# FMIO descriptor
int	lfile		# lfile to be opened
int	mode		# file access mode
int	type		# file type

int	acmode, lru, i
pointer	oldest, fc, fs, st

bool	fm_locked()
pointer	fm_findlf()
int	fm_fopen(), fstati()
errchk	fm_fopen, fm_fcinit, syserrs, close
define	ref_	91

begin
	fc = FM_FCACHE(fm)
	if (fc == NULL) {
	    call fm_fcinit (fm, FM_SZFCACHE(fm))
	    fc = FM_FCACHE(fm)
	}

	# Keep debug statistics on lfile accesses.
	st = FC_LFSTAT(fc)
	if (st != NULL)
	    Mems[st+lfile] = Mems[st+lfile] + 1

	fs = fm_findlf (fc, lfile)
	if (fs != NULL) {
	    # If lfile is already in the cache and the new mode is NEW_FILE,
	    # or we need write perm and do not currently have it, close and
	    # reopen the lfile with the desired mode.

	    if (mode == NEW_FILE ||
		(mode != READ_ONLY && fstati (FC_FD(fs), F_WRITE) == NO)) {
ref_
		if (FC_NREF(fs) > 1)
		    call syserrs (SYS_FMFSINUSE, FM_DFNAME(fm))
		if (FC_NREF(fs) == 1)
		    call close (FC_FD(fs))
		if (fm_locked (fm, lfile))
		    call syserrs (SYS_FMLFLOCKED, FM_DFNAME(fm))

		if (mode == WRITE_ONLY || mode == APPEND)
		    acmode = READ_WRITE
		else
		    acmode = mode

		FC_FD(fs) = fm_fopen (fm, lfile, acmode, type)
		FC_NREF(fs) = 1
		FC_LFILE(fs) = lfile
	    }

	    # Reference the cached lfile.
	    FC_REFCNT(fc) = FC_REFCNT(fc) + 1
	    FC_LRU(fs)    = FC_REFCNT(fc)
	    FC_NREF(fs)   = FC_NREF(fs) + 1
	    if (mode == APPEND)
		call seek (FC_FD(fs), EOFL)

	    return (FC_FD(fs))

	} else {
	    # Lfile is not in cache.  Find the oldest slot. 
	    oldest = NULL
	    lru = NULL
	    do i = 1, FC_NFILES(fc) {
		fs = FC_FS(fc,i)
		if (FC_NREF(fs) <= 1)
		    if (lru == NULL || FC_LRU(fs) < lru) {
			lru = FC_LRU(fs)
			oldest = fs
			if (FC_NREF(fs) <= 0)
			    break
		    }
	    }

	    # Abort if all cache slots are busy.
	    if (oldest == NULL)
		call syserrs (SYS_FMFCFULL, FM_DFNAME(fm))

	    # Replace the file in the cache, and return new descriptor.
	    fs = oldest
	    goto ref_
	}
end


# FM_RETFD -- Return a cached file, i.e., decrement the reference count for
# the file so that it may be returned.  If FM_RETFD is called when the file
# is sitting in the cache idle, the file is physically closed (i.e., a GETFD
# followed by one RETFD leaves the file cached and idle, ready for another
# GETFD without losing context, but another RETFD while the file is idle
# closes the file and removes it from the cache).

procedure fm_retfd (fm, lfile)

pointer	fm			#I FMIO descriptor
int	lfile			#I lfile to be returned

pointer	fc, fs
pointer	fm_findlf()
errchk	fm_fcinit

begin
	fc = FM_FCACHE(fm)
	if (fc == NULL) {
	    call fm_fcinit (fm, FM_SZFCACHE(fm))
	    fc = FM_FCACHE(fm)
	}

	fs = fm_findlf (fc, lfile)
	if (fs != NULL) {
	    FC_NREF(fs) = FC_NREF(fs) - 1
	    if (FC_NREF(fs) <= 0) {
		call close (FC_FD(fs))
		FC_NREF(fs) = 0
	    }
	}
end


# FM_LOCKOUT -- Lock an lfile out of the file cache.

procedure fm_lockout (fm, lfile)

pointer	fm		#I FMIO descriptor
int	lfile		#I lfile to be locked out

pointer	fc, fs, lf
pointer	fm_findlf()
errchk	fm_fcinit, close, syserrs

begin
	fc = FM_FCACHE(fm)
	if (fc == NULL) {
	    call fm_fcinit (fm, FM_SZFCACHE(fm))
	    fc = FM_FCACHE(fm)
	}

	# Close lfile if it is already in the cache, but idle.
	fs = fm_findlf (fc, lfile)
	if (fs != NULL) {
	    if (FC_NREF(fs) > 1)
		call syserrs (SYS_FMLOKACTLF, FM_DFNAME(fm))
	    FC_NREF(fs) = FC_NREF(fs) - 1
	    if (FC_NREF(fs) <= 0) {
		call close (FC_FD(fs))
		FC_NREF(fs) = 0
	    }
	}

	# Set the lockout bit in the file table.
	lf = FM_FTABLE(fm) + lfile * LEN_FTE
	LF_FLAGS(lf) = or (LF_FLAGS(lf), LFF_LOCKOUT)
end


# FM_UNLOCK -- Unlock an lfile.

procedure fm_unlock (fm, lfile)

pointer	fm		#I FMIO descriptor
int	lfile		#I lfile to be locked out

pointer	lf

begin
	# Clear the lockout bit in the file table.
	lf = FM_FTABLE(fm) + lfile * LEN_FTE
	LF_FLAGS(lf) = and (LF_FLAGS(lf), not(LFF_LOCKOUT))
end


# FM_LOCKED -- Test the lfile lockout bit.

bool procedure fm_locked (fm, lfile)

pointer	fm		#I FMIO descriptor
int	lfile		#I lfile to be locked out

pointer	lf

begin
	# Test the lockout bit in the file table.
	lf = FM_FTABLE(fm) + lfile * LEN_FTE
	return (and (LF_FLAGS(lf), LFF_LOCKOUT) != 0)
end


# FM_FINDLF -- Search the cache for the given lfile.

pointer procedure fm_findlf (fc, lfile)

pointer	fc		#I file cache descriptor
int	lfile		#I lfile to search for

int	i
pointer	fs

begin
	do i = 1, FC_NFILES(fc) {
	    fs = FC_FS(fc,i)
	    if (FC_NREF(fs) != 0 && FC_LFILE(fs) == lfile)
		return (fs)
	}

	return (NULL)
end


# FM_FCDEBUG -- Print debug info describing the contents and status of the
# file cache.

procedure fm_fcdebug (fm, out, what)

pointer	fm		#I FMIO descriptor
int	out		#I output file
int	what		#I type of debug output

int	nref, i
pointer	fc, fs, st
errchk	fm_fcinit

begin
	fc = FM_FCACHE(fm)
	if (fc == NULL) {
	    call fm_fcinit (fm, FM_SZFCACHE(fm))
	    fc = FM_FCACHE(fm)
	}

	# Print cache status.
	if (and (what, FCD_CACHE) != 0) {
	    call fprintf (out, "#  LRU NREF LFILE  FD\n")
	    do i = 1, FC_NFILES(fc) {
		fs = FC_FS(fc,i)
		call fprintf (out, "%6d %4d %5d %3d\n")
		    call pargi (FC_LRU(fs))
		    call pargi (FC_NREF(fs))
		    if (FC_NREF(fs) > 0) {
			call pargi (FC_LFILE(fs))
			call pargi (FC_FD(fs))
		    } else {
			call pargi (0)
			call pargi (0)
		    }
	    }
	}

	# Print lfile access statistics.
	if (and (what, FCD_LFSTATISTICS) != 0) {
	    st = FC_LFSTAT(fc)
	    if (st != NULL) {
		call fprintf (out, "-------- getfd's per lfile ---------\n")
		do i = 0, FM_NLFILES(fm) {
		    nref = Mems[st+i]
		    if (nref > 0) {
			call fprintf (out, "%4d %4d\n")
			    call pargi (i)
			    call pargi (nref)
		    }
		}
	    }
	}
end


# FM_FCINIT -- Init the file cache.

procedure fm_fcinit (fm, cachesize)

pointer	fm		#I FMIO descriptor
int	cachesize	#I size of cache, file slots

pointer	fc

begin
	if (FM_FCACHE(fm) != NULL)
	    call fm_fcfree (fm)

	call calloc (fc, LEN_FCACHE(cachesize), TY_STRUCT)
	call calloc (FC_LFSTAT(fc), FM_NLFILES(fm)+1, TY_SHORT)
	FC_NFILES(fc) = cachesize
	FC_REFCNT(fc) = 1

	FM_FCACHE(fm) = fc
end


# FM_FCFREE -- Shutdown the file cache and free all resources, ignoring
# all file reference counts.

procedure fm_fcfree (fm)

pointer	fm		#I FMIO descriptor

int	i
pointer	fc, fs

begin
	fc = FM_FCACHE(fm)
	if (fc == NULL)
	    return

	# Update the datafile.
	call fm_fcsync (fm)

	# Close any cached files.
	do i = 1, FC_NFILES(fc) {
	    fs = FC_FS(fc,i)
	    if (FC_NREF(fs) >= 1)
		iferr (call close (FC_FD(fs)))
		    call erract (EA_WARN)
	}

	call mfree (FC_LFSTAT(fc), TY_SHORT)
	call mfree (FM_FCACHE(fm), TY_STRUCT)
end


# FM_FCSYNC -- Sync the file cache and datafile.

procedure fm_fcsync (fm)

pointer	fm		#I FMIO descriptor

int	i
pointer	fc, fs

begin
	fc = FM_FCACHE(fm)
	if (fc != NULL) {
	    do i = 1, FC_NFILES(fc) {
		fs = FC_FS(fc,i)
		if (FC_NREF(fs) >= 1)
		    iferr (call flush (FC_FD(fs)))
			call erract (EA_WARN)
	    }
	}

	call fm_sync (fm)
end