Commit ff8ce523 authored by Vadim B. Mikheev's avatar Vadim B. Mikheev

1. Cleanup (no more FreeFd - unuseful; others).

2. FreeFile() has to do nothing with nfile (# of files opened
   by VFD manager).
parent 12d9a12e
...@@ -6,7 +6,7 @@ ...@@ -6,7 +6,7 @@
* Copyright (c) 1994, Regents of the University of California * Copyright (c) 1994, Regents of the University of California
* *
* IDENTIFICATION * 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: * NOTES:
* *
...@@ -119,11 +119,6 @@ typedef struct vfd { ...@@ -119,11 +119,6 @@ typedef struct vfd {
static Vfd *VfdCache; static Vfd *VfdCache;
static Size SizeVfdCache = 0; 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. * Number of file descriptors known to be open.
*/ */
...@@ -215,15 +210,17 @@ _dump_lru() ...@@ -215,15 +210,17 @@ _dump_lru()
{ {
int mru = VfdCache[0].lruLessRecently; int mru = VfdCache[0].lruLessRecently;
Vfd *vfdP = &VfdCache[mru]; Vfd *vfdP = &VfdCache[mru];
char buf[2048];
printf("MOST %d ", mru); sprintf(buf, "LRU: MOST %d ", mru);
while (mru != 0) while (mru != 0)
{ {
mru = vfdP->lruLessRecently; mru = vfdP->lruLessRecently;
vfdP = &VfdCache[mru]; vfdP = &VfdCache[mru];
printf("%d ", mru); sprintf (buf + strlen(buf), "%d ", mru);
} }
printf("LEAST\n"); sprintf(buf + strlen(buf), "LEAST");
elog (DEBUG, buf);
} }
#endif /* FDDEBUG */ #endif /* FDDEBUG */
...@@ -232,7 +229,7 @@ Delete(File file) ...@@ -232,7 +229,7 @@ Delete(File file)
{ {
Vfd *fileP; Vfd *fileP;
DO_DB(printf("DEBUG: Delete %d (%s)\n", DO_DB(elog (DEBUG, "Delete %d (%s)",
file, VfdCache[file].fileName)); file, VfdCache[file].fileName));
DO_DB(_dump_lru()); DO_DB(_dump_lru());
...@@ -254,7 +251,7 @@ LruDelete(File file) ...@@ -254,7 +251,7 @@ LruDelete(File file)
Vfd *fileP; Vfd *fileP;
int returnValue; int returnValue;
DO_DB(printf("DEBUG: LruDelete %d (%s)\n", DO_DB(elog (DEBUG, "LruDelete %d (%s)",
file, VfdCache[file].fileName)); file, VfdCache[file].fileName));
Assert(file != 0); Assert(file != 0);
...@@ -281,9 +278,7 @@ LruDelete(File file) ...@@ -281,9 +278,7 @@ LruDelete(File file)
--nfile; --nfile;
fileP->fd = VFD_CLOSED; fileP->fd = VFD_CLOSED;
/* note that there is now one more free real file descriptor */
FreeFd++;
} }
static void static void
...@@ -291,7 +286,7 @@ Insert(File file) ...@@ -291,7 +286,7 @@ Insert(File file)
{ {
Vfd *vfdP; Vfd *vfdP;
DO_DB(printf("DEBUG: Insert %d (%s)\n", DO_DB(elog(DEBUG, "Insert %d (%s)",
file, VfdCache[file].fileName)); file, VfdCache[file].fileName));
DO_DB(_dump_lru()); DO_DB(_dump_lru());
...@@ -311,13 +306,16 @@ LruInsert (File file) ...@@ -311,13 +306,16 @@ LruInsert (File file)
Vfd *vfdP; Vfd *vfdP;
int returnValue; int returnValue;
DO_DB(printf("DEBUG: LruInsert %d (%s)\n", DO_DB(elog(DEBUG, "LruInsert %d (%s)",
file, VfdCache[file].fileName)); file, VfdCache[file].fileName));
vfdP = &VfdCache[file]; vfdP = &VfdCache[file];
if (FileIsNotOpen(file)) { if (FileIsNotOpen(file)) {
int tmpfd; int tmpfd;
if ( nfile >= pg_nofile() )
AssertLruRoom();
/* /*
* Note, we check to see if there's a free file descriptor * Note, we check to see if there's a free file descriptor
...@@ -329,7 +327,6 @@ LruInsert (File file) ...@@ -329,7 +327,6 @@ LruInsert (File file)
tryAgain: tryAgain:
tmpfd = open(Nulldev, O_CREAT|O_RDWR, 0666); tmpfd = open(Nulldev, O_CREAT|O_RDWR, 0666);
if (tmpfd < 0) { if (tmpfd < 0) {
FreeFd = 0;
errno = 0; errno = 0;
AssertLruRoom(); AssertLruRoom();
goto tryAgain; goto tryAgain;
...@@ -339,11 +336,11 @@ LruInsert (File file) ...@@ -339,11 +336,11 @@ LruInsert (File file)
vfdP->fd = open(vfdP->fileName,vfdP->fileFlags,vfdP->fileMode); vfdP->fd = open(vfdP->fileName,vfdP->fileFlags,vfdP->fileMode);
if (vfdP->fd < 0) { if (vfdP->fd < 0) {
DO_DB(printf("RE_OPEN FAILED: %d\n", DO_DB(elog(DEBUG, "RE_OPEN FAILED: %d",
errno)); errno));
return (vfdP->fd); return (vfdP->fd);
} else { } else {
DO_DB(printf("RE_OPEN SUCCESS\n")); DO_DB(elog (DEBUG, "RE_OPEN SUCCESS"));
++nfile; ++nfile;
} }
...@@ -357,9 +354,6 @@ LruInsert (File file) ...@@ -357,9 +354,6 @@ LruInsert (File file)
/* init state on open */ /* init state on open */
vfdP->fdstate = 0x0; vfdP->fdstate = 0x0;
/* note that a file descriptor has been used up */
if (FreeFd > 0)
FreeFd--;
} }
/* /*
...@@ -374,16 +368,16 @@ LruInsert (File file) ...@@ -374,16 +368,16 @@ LruInsert (File file)
static void static void
AssertLruRoom() AssertLruRoom()
{ {
DO_DB(printf("DEBUG: AssertLruRoom (FreeFd = %d)\n", DO_DB(elog(DEBUG, "AssertLruRoom. Opened %d", nfile));
FreeFd));
if ( nfile <= 0 )
if (FreeFd <= 0 || nfile >= pg_nofile()) { elog (FATAL, "AssertLruRoom: No opened files - no one can be closed");
/* We supposedly are using more vfds than we want to be. First /*
assert that there is at least one used vfd in the ring. * 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); Assert(VfdCache[0].lruMoreRecently != 0);
} LruDelete(VfdCache[0].lruMoreRecently);
} }
static File static File
...@@ -392,13 +386,12 @@ AllocateVfd() ...@@ -392,13 +386,12 @@ AllocateVfd()
Index i; Index i;
File file; File file;
DO_DB(printf("DEBUG: AllocateVfd\n")); DO_DB(elog(DEBUG, "AllocateVfd. Size %d", SizeVfdCache));
if (SizeVfdCache == 0) { if (SizeVfdCache == 0) {
/* initialize */ /* initialize */
VfdCache = (Vfd *)malloc(sizeof(Vfd)); VfdCache = (Vfd *)malloc(sizeof(Vfd));
VfdCache->nextFree = 0; VfdCache->nextFree = 0;
VfdCache->lruMoreRecently = 0; VfdCache->lruMoreRecently = 0;
VfdCache->lruLessRecently = 0; VfdCache->lruLessRecently = 0;
...@@ -408,8 +401,8 @@ AllocateVfd() ...@@ -408,8 +401,8 @@ AllocateVfd()
SizeVfdCache = 1; SizeVfdCache = 1;
} }
if (VfdCache[0].nextFree == 0) { if (VfdCache[0].nextFree == 0)
{
/* /*
* The free list is empty so it is time to increase the * The free list is empty so it is time to increase the
* size of the array * size of the array
...@@ -417,7 +410,7 @@ AllocateVfd() ...@@ -417,7 +410,7 @@ AllocateVfd()
VfdCache =(Vfd *)realloc(VfdCache, sizeof(Vfd)*SizeVfdCache*2); VfdCache =(Vfd *)realloc(VfdCache, sizeof(Vfd)*SizeVfdCache*2);
Assert(VfdCache != NULL); Assert(VfdCache != NULL);
/* /*
* Set up the free list for the new entries * Set up the free list for the new entries
*/ */
...@@ -452,7 +445,7 @@ AllocateVfd() ...@@ -452,7 +445,7 @@ AllocateVfd()
static void static void
FreeVfd(File file) FreeVfd(File file)
{ {
DO_DB(printf("DB: FreeVfd: %d (%s)\n", DO_DB(elog(DEBUG, "FreeVfd: %d (%s)",
file, VfdCache[file].fileName)); file, VfdCache[file].fileName));
VfdCache[file].nextFree = VfdCache[0].nextFree; VfdCache[file].nextFree = VfdCache[0].nextFree;
...@@ -488,7 +481,7 @@ FileAccess(File file) ...@@ -488,7 +481,7 @@ FileAccess(File file)
{ {
int returnValue; int returnValue;
DO_DB(printf("DB: FileAccess %d (%s)\n", DO_DB(elog(DEBUG, "FileAccess %d (%s)",
file, VfdCache[file].fileName)); file, VfdCache[file].fileName));
/* /*
...@@ -498,8 +491,6 @@ FileAccess(File file) ...@@ -498,8 +491,6 @@ FileAccess(File file)
if (FileIsNotOpen(file)) { if (FileIsNotOpen(file)) {
AssertLruRoom();
returnValue = LruInsert(file); returnValue = LruInsert(file);
if (returnValue != 0) if (returnValue != 0)
return returnValue; return returnValue;
...@@ -537,29 +528,25 @@ fileNameOpenFile(FileName fileName, ...@@ -537,29 +528,25 @@ fileNameOpenFile(FileName fileName,
int fileFlags, int fileFlags,
int fileMode) int fileMode)
{ {
static int osRanOut = 0;
File file; File file;
Vfd *vfdP; Vfd *vfdP;
int tmpfd; int tmpfd;
DO_DB(printf("DEBUG: FileNameOpenFile: %s %x %o\n", DO_DB(elog(DEBUG, "fileNameOpenFile: %s %x %o",
fileName, fileFlags, fileMode)); fileName, fileFlags, fileMode));
file = AllocateVfd(); file = AllocateVfd();
vfdP = &VfdCache[file]; vfdP = &VfdCache[file];
if (nfile >= pg_nofile() || (FreeFd == 0 && osRanOut)) { if ( nfile >= pg_nofile() )
AssertLruRoom(); AssertLruRoom();
}
tryAgain: tryAgain:
tmpfd = open(Nulldev, O_CREAT|O_RDWR, 0666); tmpfd = open(Nulldev, O_CREAT|O_RDWR, 0666);
if (tmpfd < 0) { 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));
errno = 0; errno = 0;
FreeFd = 0;
osRanOut = 1;
AssertLruRoom(); AssertLruRoom();
goto tryAgain; goto tryAgain;
} else { } else {
...@@ -574,17 +561,17 @@ fileNameOpenFile(FileName fileName, ...@@ -574,17 +561,17 @@ fileNameOpenFile(FileName fileName,
return -1; return -1;
} }
++nfile; ++nfile;
DO_DB(printf("DB: FNOF success %d\n", DO_DB(elog(DEBUG, "fileNameOpenFile: success %d",
vfdP->fd)); vfdP->fd));
(void)LruInsert(file); Insert(file);
if (fileName==NULL) { if (fileName==NULL) {
elog(WARN, "fileNameOpenFile: NULL fname"); elog(WARN, "fileNameOpenFile: NULL fname");
} }
vfdP->fileName = malloc(strlen(fileName)+1); vfdP->fileName = malloc(strlen(fileName)+1);
strcpy(vfdP->fileName,fileName); strcpy(vfdP->fileName,fileName);
vfdP->fileFlags = fileFlags & ~(O_TRUNC|O_EXCL); vfdP->fileFlags = fileFlags & ~(O_TRUNC|O_EXCL);
vfdP->fileMode = fileMode; vfdP->fileMode = fileMode;
vfdP->seekPos = 0; vfdP->seekPos = 0;
...@@ -621,7 +608,7 @@ FileClose(File file) ...@@ -621,7 +608,7 @@ FileClose(File file)
{ {
int returnValue; int returnValue;
DO_DB(printf("DEBUG: FileClose: %d (%s)\n", DO_DB(elog(DEBUG, "FileClose: %d (%s)",
file, VfdCache[file].fileName)); file, VfdCache[file].fileName));
if (!FileIsNotOpen(file)) { if (!FileIsNotOpen(file)) {
...@@ -629,9 +616,6 @@ FileClose(File file) ...@@ -629,9 +616,6 @@ FileClose(File file)
/* remove the file from the lru ring */ /* remove the file from the lru ring */
Delete(file); Delete(file);
/* record the new free operating system file descriptor */
FreeFd++;
/* if we did any writes, sync the file before closing */ /* if we did any writes, sync the file before closing */
if (VfdCache[file].fdstate & FD_DIRTY) { if (VfdCache[file].fdstate & FD_DIRTY) {
returnValue = fsync(VfdCache[file].fd); returnValue = fsync(VfdCache[file].fd);
...@@ -661,7 +645,7 @@ FileUnlink(File file) ...@@ -661,7 +645,7 @@ FileUnlink(File file)
{ {
int returnValue; int returnValue;
DO_DB(printf("DB: FileClose: %d (%s)\n", DO_DB(elog(DEBUG, "FileUnlink: %d (%s)",
file, VfdCache[file].fileName)); file, VfdCache[file].fileName));
if (!FileIsNotOpen(file)) { if (!FileIsNotOpen(file)) {
...@@ -669,9 +653,6 @@ FileUnlink(File file) ...@@ -669,9 +653,6 @@ FileUnlink(File file)
/* remove the file from the lru ring */ /* remove the file from the lru ring */
Delete(file); Delete(file);
/* record the new free operating system file descriptor */
FreeFd++;
/* if we did any writes, sync the file before closing */ /* if we did any writes, sync the file before closing */
if (VfdCache[file].fdstate & FD_DIRTY) { if (VfdCache[file].fdstate & FD_DIRTY) {
returnValue = fsync(VfdCache[file].fd); returnValue = fsync(VfdCache[file].fd);
...@@ -699,7 +680,7 @@ FileRead(File file, char *buffer, int amount) ...@@ -699,7 +680,7 @@ FileRead(File file, char *buffer, int amount)
{ {
int returnCode; 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)); file, VfdCache[file].fileName, amount, buffer));
FileAccess(file); FileAccess(file);
...@@ -716,7 +697,7 @@ FileWrite(File file, char *buffer, int amount) ...@@ -716,7 +697,7 @@ FileWrite(File file, char *buffer, int amount)
{ {
int returnCode; 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)); file, VfdCache[file].fileName, amount, buffer));
FileAccess(file); FileAccess(file);
...@@ -736,7 +717,7 @@ FileSeek(File file, long offset, int whence) ...@@ -736,7 +717,7 @@ FileSeek(File file, long offset, int whence)
{ {
int returnCode; 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)); file, VfdCache[file].fileName, offset, whence));
if (FileIsNotOpen(file)) { if (FileIsNotOpen(file)) {
...@@ -771,7 +752,7 @@ FileSeek(File file, long offset, int whence) ...@@ -771,7 +752,7 @@ FileSeek(File file, long offset, int whence)
long long
FileTell(File file) FileTell(File file)
{ {
DO_DB(printf("DEBUG: FileTell %d (%s)\n", DO_DB(elog(DEBUG, "FileTell %d (%s)",
file, VfdCache[file].fileName)); file, VfdCache[file].fileName));
return VfdCache[file].seekPos; return VfdCache[file].seekPos;
} }
...@@ -781,7 +762,7 @@ FileTruncate(File file, int offset) ...@@ -781,7 +762,7 @@ FileTruncate(File file, int offset)
{ {
int returnCode; int returnCode;
DO_DB(printf("DEBUG: FileTruncate %d (%s)\n", DO_DB(elog(DEBUG, "FileTruncate %d (%s)",
file, VfdCache[file].fileName)); file, VfdCache[file].fileName));
(void) FileSync(file); (void) FileSync(file);
...@@ -840,26 +821,25 @@ AllocateFile() ...@@ -840,26 +821,25 @@ AllocateFile()
{ {
int fd; int fd;
int fdleft; int fdleft;
DO_DB(elog(DEBUG, "AllocateFile: Allocated %d.", allocatedFiles));
while ((fd = open(Nulldev,O_WRONLY,0)) < 0) { while ((fd = open(Nulldev,O_WRONLY,0)) < 0) {
if (errno == EMFILE) { if (errno == EMFILE) {
errno = 0; errno = 0;
FreeFd = 0;
AssertLruRoom(); AssertLruRoom();
} else { } 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)); __FILE__, __LINE__, strerror(errno));
} }
} }
close(fd); close(fd);
++allocatedFiles; ++allocatedFiles;
fdleft = pg_nofile() - allocatedFiles; fdleft = pg_nofile() - allocatedFiles - nfile;
if (fdleft < 6) { 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));
} }
/* /*
...@@ -869,10 +849,8 @@ AllocateFile() ...@@ -869,10 +849,8 @@ AllocateFile()
void void
FreeFile() FreeFile()
{ {
DO_DB(printf("DEBUG: FreeFile. FreeFd now %d\n", DO_DB(elog(DEBUG, "FreeFile: Allocated %d.", allocatedFiles));
FreeFd));
FreeFd++;
nfile++; /* dangerous */
Assert(allocatedFiles > 0); Assert(allocatedFiles > 0);
--allocatedFiles; --allocatedFiles;
} }
...@@ -894,11 +872,8 @@ closeOneVfd() ...@@ -894,11 +872,8 @@ closeOneVfd()
int tmpfd; int tmpfd;
tmpfd = open(Nulldev, O_CREAT | O_RDWR, 0666); tmpfd = open(Nulldev, O_CREAT | O_RDWR, 0666);
if (tmpfd < 0) { if (tmpfd < 0)
FreeFd = 0;
AssertLruRoom(); AssertLruRoom();
FreeFd = 0;
}
else else
close(tmpfd); close(tmpfd);
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment