* Copyright (c) 1994, Regents of the University of California
*
* IDENTIFICATION
- * $Id: fd.c,v 1.16 1997/02/20 22:54:18 scrappy Exp $
+ * $Id: fd.c,v 1.17 1997/05/22 16:51:19 vadim Exp $
*
* NOTES:
*
static Vfd *VfdCache;
static Size SizeVfdCache = 0;
-/*
- * Minimum number of file descriptors known to be free.
- */
-static int FreeFd = 0;
-
/*
* Number of file descriptors known to be open.
*/
{
int mru = VfdCache[0].lruLessRecently;
Vfd *vfdP = &VfdCache[mru];
+ char buf[2048];
- printf("MOST %d ", mru);
+ sprintf(buf, "LRU: MOST %d ", mru);
while (mru != 0)
- {
- mru = vfdP->lruLessRecently;
- vfdP = &VfdCache[mru];
- printf("%d ", mru);
- }
- printf("LEAST\n");
+ {
+ mru = vfdP->lruLessRecently;
+ vfdP = &VfdCache[mru];
+ sprintf (buf + strlen(buf), "%d ", mru);
+ }
+ sprintf(buf + strlen(buf), "LEAST");
+ elog (DEBUG, buf);
}
#endif /* FDDEBUG */
{
Vfd *fileP;
- DO_DB(printf("DEBUG: Delete %d (%s)\n",
+ DO_DB(elog (DEBUG, "Delete %d (%s)",
file, VfdCache[file].fileName));
DO_DB(_dump_lru());
Vfd *fileP;
int returnValue;
- DO_DB(printf("DEBUG: LruDelete %d (%s)\n",
+ DO_DB(elog (DEBUG, "LruDelete %d (%s)",
file, VfdCache[file].fileName));
Assert(file != 0);
--nfile;
fileP->fd = VFD_CLOSED;
-
- /* note that there is now one more free real file descriptor */
- FreeFd++;
+
}
static void
{
Vfd *vfdP;
- DO_DB(printf("DEBUG: Insert %d (%s)\n",
+ DO_DB(elog(DEBUG, "Insert %d (%s)",
file, VfdCache[file].fileName));
DO_DB(_dump_lru());
Vfd *vfdP;
int returnValue;
- DO_DB(printf("DEBUG: LruInsert %d (%s)\n",
+ DO_DB(elog(DEBUG, "LruInsert %d (%s)",
file, VfdCache[file].fileName));
vfdP = &VfdCache[file];
if (FileIsNotOpen(file)) {
int tmpfd;
+
+ if ( nfile >= pg_nofile() )
+ AssertLruRoom();
/*
* Note, we check to see if there's a free file descriptor
tryAgain:
tmpfd = open(Nulldev, O_CREAT|O_RDWR, 0666);
if (tmpfd < 0) {
- FreeFd = 0;
errno = 0;
AssertLruRoom();
goto tryAgain;
vfdP->fd = open(vfdP->fileName,vfdP->fileFlags,vfdP->fileMode);
if (vfdP->fd < 0) {
- DO_DB(printf("RE_OPEN FAILED: %d\n",
+ DO_DB(elog(DEBUG, "RE_OPEN FAILED: %d",
errno));
return (vfdP->fd);
} else {
- DO_DB(printf("RE_OPEN SUCCESS\n"));
+ DO_DB(elog (DEBUG, "RE_OPEN SUCCESS"));
++nfile;
}
/* init state on open */
vfdP->fdstate = 0x0;
- /* note that a file descriptor has been used up */
- if (FreeFd > 0)
- FreeFd--;
}
/*
static void
AssertLruRoom()
{
- DO_DB(printf("DEBUG: AssertLruRoom (FreeFd = %d)\n",
- FreeFd));
-
- if (FreeFd <= 0 || nfile >= pg_nofile()) {
- /* We supposedly are using more vfds than we want to be. First
- assert that there is at least one used vfd in the ring.
- */
- Assert(VfdCache[0].lruMoreRecently != 0);
- LruDelete(VfdCache[0].lruMoreRecently);
- }
+ DO_DB(elog(DEBUG, "AssertLruRoom. Opened %d", nfile));
+
+ if ( nfile <= 0 )
+ elog (FATAL, "AssertLruRoom: No opened files - no one can be closed");
+ /*
+ * There are opened files and so there should be at least one used vfd
+ * in the ring.
+ */
+ Assert(VfdCache[0].lruMoreRecently != 0);
+ LruDelete(VfdCache[0].lruMoreRecently);
}
static File
Index i;
File file;
- DO_DB(printf("DEBUG: AllocateVfd\n"));
+ DO_DB(elog(DEBUG, "AllocateVfd. Size %d", SizeVfdCache));
if (SizeVfdCache == 0) {
/* initialize */
VfdCache = (Vfd *)malloc(sizeof(Vfd));
-
VfdCache->nextFree = 0;
VfdCache->lruMoreRecently = 0;
VfdCache->lruLessRecently = 0;
SizeVfdCache = 1;
}
- if (VfdCache[0].nextFree == 0) {
-
+ if (VfdCache[0].nextFree == 0)
+ {
/*
* The free list is empty so it is time to increase the
* size of the array
VfdCache =(Vfd *)realloc(VfdCache, sizeof(Vfd)*SizeVfdCache*2);
Assert(VfdCache != NULL);
-
+
/*
* Set up the free list for the new entries
*/
static void
FreeVfd(File file)
{
- DO_DB(printf("DB: FreeVfd: %d (%s)\n",
+ DO_DB(elog(DEBUG, "FreeVfd: %d (%s)",
file, VfdCache[file].fileName));
VfdCache[file].nextFree = VfdCache[0].nextFree;
{
int returnValue;
- DO_DB(printf("DB: FileAccess %d (%s)\n",
+ DO_DB(elog(DEBUG, "FileAccess %d (%s)",
file, VfdCache[file].fileName));
/*
if (FileIsNotOpen(file)) {
- AssertLruRoom();
-
returnValue = LruInsert(file);
if (returnValue != 0)
return returnValue;
int fileFlags,
int fileMode)
{
- static int osRanOut = 0;
File file;
Vfd *vfdP;
int tmpfd;
- DO_DB(printf("DEBUG: FileNameOpenFile: %s %x %o\n",
+ DO_DB(elog(DEBUG, "fileNameOpenFile: %s %x %o",
fileName, fileFlags, fileMode));
file = AllocateVfd();
vfdP = &VfdCache[file];
- if (nfile >= pg_nofile() || (FreeFd == 0 && osRanOut)) {
+ if ( nfile >= pg_nofile() )
AssertLruRoom();
- }
tryAgain:
tmpfd = open(Nulldev, O_CREAT|O_RDWR, 0666);
if (tmpfd < 0) {
- DO_DB(printf("DB: not enough descs, retry, er= %d\n",
+ DO_DB(elog(DEBUG, "fileNameOpenFile: not enough descs, retry, er= %d",
errno));
errno = 0;
- FreeFd = 0;
- osRanOut = 1;
AssertLruRoom();
goto tryAgain;
} else {
return -1;
}
++nfile;
- DO_DB(printf("DB: FNOF success %d\n",
+ DO_DB(elog(DEBUG, "fileNameOpenFile: success %d",
vfdP->fd));
- (void)LruInsert(file);
+ Insert(file);
if (fileName==NULL) {
elog(WARN, "fileNameOpenFile: NULL fname");
}
vfdP->fileName = malloc(strlen(fileName)+1);
strcpy(vfdP->fileName,fileName);
-
+
vfdP->fileFlags = fileFlags & ~(O_TRUNC|O_EXCL);
vfdP->fileMode = fileMode;
vfdP->seekPos = 0;
{
int returnValue;
- DO_DB(printf("DEBUG: FileClose: %d (%s)\n",
+ DO_DB(elog(DEBUG, "FileClose: %d (%s)",
file, VfdCache[file].fileName));
if (!FileIsNotOpen(file)) {
/* remove the file from the lru ring */
Delete(file);
- /* record the new free operating system file descriptor */
- FreeFd++;
-
/* if we did any writes, sync the file before closing */
if (VfdCache[file].fdstate & FD_DIRTY) {
returnValue = fsync(VfdCache[file].fd);
{
int returnValue;
- DO_DB(printf("DB: FileClose: %d (%s)\n",
+ DO_DB(elog(DEBUG, "FileUnlink: %d (%s)",
file, VfdCache[file].fileName));
if (!FileIsNotOpen(file)) {
/* remove the file from the lru ring */
Delete(file);
- /* record the new free operating system file descriptor */
- FreeFd++;
-
/* if we did any writes, sync the file before closing */
if (VfdCache[file].fdstate & FD_DIRTY) {
returnValue = fsync(VfdCache[file].fd);
{
int returnCode;
- DO_DB(printf("DEBUG: FileRead: %d (%s) %d 0x%x\n",
+ DO_DB(elog(DEBUG, "FileRead: %d (%s) %d %p",
file, VfdCache[file].fileName, amount, buffer));
FileAccess(file);
{
int returnCode;
- DO_DB(printf("DB: FileWrite: %d (%s) %d 0x%lx\n",
+ DO_DB(elog(DEBUG, "FileWrite: %d (%s) %d %p",
file, VfdCache[file].fileName, amount, buffer));
FileAccess(file);
{
int returnCode;
- DO_DB(printf("DEBUG: FileSeek: %d (%s) %d %d\n",
+ DO_DB(elog (DEBUG, "FileSeek: %d (%s) %ld %d",
file, VfdCache[file].fileName, offset, whence));
if (FileIsNotOpen(file)) {
long
FileTell(File file)
{
- DO_DB(printf("DEBUG: FileTell %d (%s)\n",
+ DO_DB(elog(DEBUG, "FileTell %d (%s)",
file, VfdCache[file].fileName));
return VfdCache[file].seekPos;
}
{
int returnCode;
- DO_DB(printf("DEBUG: FileTruncate %d (%s)\n",
+ DO_DB(elog(DEBUG, "FileTruncate %d (%s)",
file, VfdCache[file].fileName));
(void) FileSync(file);
{
int fd;
int fdleft;
+
+ DO_DB(elog(DEBUG, "AllocateFile: Allocated %d.", allocatedFiles));
while ((fd = open(Nulldev,O_WRONLY,0)) < 0) {
if (errno == EMFILE) {
errno = 0;
- FreeFd = 0;
AssertLruRoom();
} else {
- elog(WARN,"Open: %s in %s line %d, %s\n", Nulldev,
+ elog(WARN,"Open: %s in %s line %d, %s", Nulldev,
__FILE__, __LINE__, strerror(errno));
}
}
close(fd);
++allocatedFiles;
- fdleft = pg_nofile() - allocatedFiles;
+ fdleft = pg_nofile() - allocatedFiles - nfile;
if (fdleft < 6) {
- elog(DEBUG,"warning: few usable file descriptors left (%d)", fdleft);
+ elog(NOTICE,"warning: few usable file descriptors left (%d)", fdleft);
}
-
- DO_DB(printf("DEBUG: AllocatedFile. FreeFd = %d\n",
- FreeFd));
+
}
/*
void
FreeFile()
{
- DO_DB(printf("DEBUG: FreeFile. FreeFd now %d\n",
- FreeFd));
- FreeFd++;
- nfile++; /* dangerous */
+ DO_DB(elog(DEBUG, "FreeFile: Allocated %d.", allocatedFiles));
+
Assert(allocatedFiles > 0);
--allocatedFiles;
}
int tmpfd;
tmpfd = open(Nulldev, O_CREAT | O_RDWR, 0666);
- if (tmpfd < 0) {
- FreeFd = 0;
+ if (tmpfd < 0)
AssertLruRoom();
- FreeFd = 0;
- }
else
close(tmpfd);
}