Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5337ad4

Browse files
committed
Fix count_usable_fds() to stop trying to open files once it reaches
max_files_per_process. Going further than that is just a waste of cycles, and it seems that current Cygwin does not cope gracefully with deliberately running the system out of FDs. Per Andrew Dunstan.
1 parent 9b29f9f commit 5337ad4

File tree

1 file changed

+21
-10
lines changed
  • src/backend/storage/file

1 file changed

+21
-10
lines changed

src/backend/storage/file/fd.c

+21-10
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1994, Regents of the University of California
88
*
99
* IDENTIFICATION
10-
* $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.118 2005/07/04 04:51:48 tgl Exp $
10+
* $PostgreSQL: pgsql/src/backend/storage/file/fd.c,v 1.119 2005/08/07 18:47:19 tgl Exp $
1111
*
1212
* NOTES:
1313
*
@@ -300,10 +300,16 @@ pg_fdatasync(int fd)
300300
* count_usable_fds --- count how many FDs the system will let us open,
301301
* and estimate how many are already open.
302302
*
303+
* We stop counting if usable_fds reaches max_to_probe. Note: a small
304+
* value of max_to_probe might result in an underestimate of already_open;
305+
* we must fill in any "gaps" in the set of used FDs before the calculation
306+
* of already_open will give the right answer. In practice, max_to_probe
307+
* of a couple of dozen should be enough to ensure good results.
308+
*
303309
* We assume stdin (FD 0) is available for dup'ing
304310
*/
305311
static void
306-
count_usable_fds(int *usable_fds, int *already_open)
312+
count_usable_fds(int max_to_probe, int *usable_fds, int *already_open)
307313
{
308314
int *fd;
309315
int size;
@@ -314,7 +320,7 @@ count_usable_fds(int *usable_fds, int *already_open)
314320
size = 1024;
315321
fd = (int *) palloc(size * sizeof(int));
316322

317-
/* dup until failure ... */
323+
/* dup until failure or probe limit reached */
318324
for (;;)
319325
{
320326
int thisfd;
@@ -337,6 +343,9 @@ count_usable_fds(int *usable_fds, int *already_open)
337343

338344
if (highestfd < thisfd)
339345
highestfd = thisfd;
346+
347+
if (used >= max_to_probe)
348+
break;
340349
}
341350

342351
/* release the files we opened */
@@ -364,14 +373,16 @@ set_max_safe_fds(void)
364373
int usable_fds;
365374
int already_open;
366375

367-
/*
368-
* We want to set max_safe_fds to MIN(usable_fds,
369-
* max_files_per_process - already_open) less the slop factor for
370-
* files that are opened without consulting fd.c. This ensures that
371-
* we won't exceed either max_files_per_process or the
372-
* experimentally-determined EMFILE limit.
376+
/*----------
377+
* We want to set max_safe_fds to
378+
* MIN(usable_fds, max_files_per_process - already_open)
379+
* less the slop factor for files that are opened without consulting
380+
* fd.c. This ensures that we won't exceed either max_files_per_process
381+
* or the experimentally-determined EMFILE limit.
382+
*----------
373383
*/
374-
count_usable_fds(&usable_fds, &already_open);
384+
count_usable_fds(max_files_per_process,
385+
&usable_fds, &already_open);
375386

376387
max_safe_fds = Min(usable_fds, max_files_per_process - already_open);
377388

0 commit comments

Comments
 (0)