7
7
* Portions Copyright (c) 1994, Regents of the University of California
8
8
*
9
9
* 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 $
11
11
*
12
12
* NOTES:
13
13
*
@@ -300,10 +300,16 @@ pg_fdatasync(int fd)
300
300
* count_usable_fds --- count how many FDs the system will let us open,
301
301
* and estimate how many are already open.
302
302
*
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
+ *
303
309
* We assume stdin (FD 0) is available for dup'ing
304
310
*/
305
311
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 )
307
313
{
308
314
int * fd ;
309
315
int size ;
@@ -314,7 +320,7 @@ count_usable_fds(int *usable_fds, int *already_open)
314
320
size = 1024 ;
315
321
fd = (int * ) palloc (size * sizeof (int ));
316
322
317
- /* dup until failure ... */
323
+ /* dup until failure or probe limit reached */
318
324
for (;;)
319
325
{
320
326
int thisfd ;
@@ -337,6 +343,9 @@ count_usable_fds(int *usable_fds, int *already_open)
337
343
338
344
if (highestfd < thisfd )
339
345
highestfd = thisfd ;
346
+
347
+ if (used >= max_to_probe )
348
+ break ;
340
349
}
341
350
342
351
/* release the files we opened */
@@ -364,14 +373,16 @@ set_max_safe_fds(void)
364
373
int usable_fds ;
365
374
int already_open ;
366
375
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
+ *----------
373
383
*/
374
- count_usable_fds (& usable_fds , & already_open );
384
+ count_usable_fds (max_files_per_process ,
385
+ & usable_fds , & already_open );
375
386
376
387
max_safe_fds = Min (usable_fds , max_files_per_process - already_open );
377
388
0 commit comments