Skip to content

Commit 674ef90

Browse files
committed
Merge branch 'sp/maint-fd-limit'
* sp/maint-fd-limit: sha1_file.c: Don't retain open fds on small packs mingw: add minimum getrlimit() compatibility stub Limit file descriptors used by packs
2 parents ccf6d62 + d131b7a commit 674ef90

File tree

4 files changed

+85
-19
lines changed

4 files changed

+85
-19
lines changed

cache.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -913,7 +913,8 @@ extern struct packed_git {
913913
time_t mtime;
914914
int pack_fd;
915915
unsigned pack_local:1,
916-
pack_keep:1;
916+
pack_keep:1,
917+
do_not_close:1;
917918
unsigned char sha1[20];
918919
/* something like ".git/objects/pack/xxxxx.pack" */
919920
char pack_name[FLEX_ARRAY]; /* more */

compat/mingw.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,22 @@ int mingw_getpagesize(void);
231231
#define getpagesize mingw_getpagesize
232232
#endif
233233

234+
struct rlimit {
235+
unsigned int rlim_cur;
236+
};
237+
#define RLIMIT_NOFILE 0
238+
239+
static inline int getrlimit(int resource, struct rlimit *rlp)
240+
{
241+
if (resource != RLIMIT_NOFILE) {
242+
errno = EINVAL;
243+
return -1;
244+
}
245+
246+
rlp->rlim_cur = 2048;
247+
return 0;
248+
}
249+
234250
/* Use mingw_lstat() instead of lstat()/stat() and
235251
* mingw_fstat() instead of fstat() on Windows.
236252
*/

fast-import.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -877,6 +877,7 @@ static void start_packfile(void)
877877
p = xcalloc(1, sizeof(*p) + strlen(tmpfile) + 2);
878878
strcpy(p->pack_name, tmpfile);
879879
p->pack_fd = pack_fd;
880+
p->do_not_close = 1;
880881
pack_file = sha1fd(pack_fd, p->pack_name);
881882

882883
hdr.hdr_signature = htonl(PACK_SIGNATURE);

sha1_file.c

Lines changed: 66 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,8 @@ static unsigned int pack_used_ctr;
418418
static unsigned int pack_mmap_calls;
419419
static unsigned int peak_pack_open_windows;
420420
static unsigned int pack_open_windows;
421+
static unsigned int pack_open_fds;
422+
static unsigned int pack_max_fds;
421423
static size_t peak_pack_mapped;
422424
static size_t pack_mapped;
423425
struct packed_git *packed_git;
@@ -595,8 +597,10 @@ static int unuse_one_window(struct packed_git *current, int keep_fd)
595597
lru_l->next = lru_w->next;
596598
else {
597599
lru_p->windows = lru_w->next;
598-
if (!lru_p->windows && lru_p->pack_fd != keep_fd) {
600+
if (!lru_p->windows && lru_p->pack_fd != -1
601+
&& lru_p->pack_fd != keep_fd) {
599602
close(lru_p->pack_fd);
603+
pack_open_fds--;
600604
lru_p->pack_fd = -1;
601605
}
602606
}
@@ -681,8 +685,10 @@ void free_pack_by_name(const char *pack_name)
681685
if (strcmp(pack_name, p->pack_name) == 0) {
682686
clear_delta_base_cache();
683687
close_pack_windows(p);
684-
if (p->pack_fd != -1)
688+
if (p->pack_fd != -1) {
685689
close(p->pack_fd);
690+
pack_open_fds--;
691+
}
686692
close_pack_index(p);
687693
free(p->bad_object_sha1);
688694
*pp = p->next;
@@ -708,9 +714,29 @@ static int open_packed_git_1(struct packed_git *p)
708714
if (!p->index_data && open_pack_index(p))
709715
return error("packfile %s index unavailable", p->pack_name);
710716

717+
if (!pack_max_fds) {
718+
struct rlimit lim;
719+
unsigned int max_fds;
720+
721+
if (getrlimit(RLIMIT_NOFILE, &lim))
722+
die_errno("cannot get RLIMIT_NOFILE");
723+
724+
max_fds = lim.rlim_cur;
725+
726+
/* Save 3 for stdin/stdout/stderr, 22 for work */
727+
if (25 < max_fds)
728+
pack_max_fds = max_fds - 25;
729+
else
730+
pack_max_fds = 1;
731+
}
732+
733+
while (pack_max_fds <= pack_open_fds && unuse_one_window(NULL, -1))
734+
; /* nothing */
735+
711736
p->pack_fd = git_open_noatime(p->pack_name, p);
712737
if (p->pack_fd < 0 || fstat(p->pack_fd, &st))
713738
return -1;
739+
pack_open_fds++;
714740

715741
/* If we created the struct before we had the pack we lack size. */
716742
if (!p->pack_size) {
@@ -762,6 +788,7 @@ static int open_packed_git(struct packed_git *p)
762788
return 0;
763789
if (p->pack_fd != -1) {
764790
close(p->pack_fd);
791+
pack_open_fds--;
765792
p->pack_fd = -1;
766793
}
767794
return -1;
@@ -787,14 +814,13 @@ unsigned char *use_pack(struct packed_git *p,
787814
{
788815
struct pack_window *win = *w_cursor;
789816

790-
if (p->pack_fd == -1 && open_packed_git(p))
791-
die("packfile %s cannot be accessed", p->pack_name);
792-
793817
/* Since packfiles end in a hash of their content and it's
794818
* pointless to ask for an offset into the middle of that
795819
* hash, and the in_window function above wouldn't match
796820
* don't allow an offset too close to the end of the file.
797821
*/
822+
if (!p->pack_size && p->pack_fd == -1 && open_packed_git(p))
823+
die("packfile %s cannot be accessed", p->pack_name);
798824
if (offset > (p->pack_size - 20))
799825
die("offset beyond end of packfile (truncated pack?)");
800826

@@ -808,6 +834,10 @@ unsigned char *use_pack(struct packed_git *p,
808834
if (!win) {
809835
size_t window_align = packed_git_window_size / 2;
810836
off_t len;
837+
838+
if (p->pack_fd == -1 && open_packed_git(p))
839+
die("packfile %s cannot be accessed", p->pack_name);
840+
811841
win = xcalloc(1, sizeof(*win));
812842
win->offset = (offset / window_align) * window_align;
813843
len = p->pack_size - win->offset;
@@ -825,6 +855,12 @@ unsigned char *use_pack(struct packed_git *p,
825855
die("packfile %s cannot be mapped: %s",
826856
p->pack_name,
827857
strerror(errno));
858+
if (!win->offset && win->len == p->pack_size
859+
&& !p->do_not_close) {
860+
close(p->pack_fd);
861+
pack_open_fds--;
862+
p->pack_fd = -1;
863+
}
828864
pack_mmap_calls++;
829865
pack_open_windows++;
830866
if (pack_mapped > peak_pack_mapped)
@@ -919,6 +955,9 @@ struct packed_git *parse_pack_index(unsigned char *sha1, const char *idx_path)
919955

920956
void install_packed_git(struct packed_git *pack)
921957
{
958+
if (pack->pack_fd != -1)
959+
pack_open_fds++;
960+
922961
pack->next = packed_git;
923962
packed_git = pack;
924963
}
@@ -936,8 +975,6 @@ static void prepare_packed_git_one(char *objdir, int local)
936975
sprintf(path, "%s/pack", objdir);
937976
len = strlen(path);
938977
dir = opendir(path);
939-
while (!dir && errno == EMFILE && unuse_one_window(NULL, -1))
940-
dir = opendir(path);
941978
if (!dir) {
942979
if (errno != ENOENT)
943980
error("unable to open object pack directory: %s: %s",
@@ -1093,14 +1130,6 @@ static int git_open_noatime(const char *name, struct packed_git *p)
10931130
if (fd >= 0)
10941131
return fd;
10951132

1096-
/* Might the failure be insufficient file descriptors? */
1097-
if (errno == EMFILE) {
1098-
if (unuse_one_window(p, -1))
1099-
continue;
1100-
else
1101-
return -1;
1102-
}
1103-
11041133
/* Might the failure be due to O_NOATIME? */
11051134
if (errno != ENOENT && sha1_file_open_flag) {
11061135
sha1_file_open_flag = 0;
@@ -1932,6 +1961,27 @@ off_t find_pack_entry_one(const unsigned char *sha1,
19321961
return 0;
19331962
}
19341963

1964+
static int is_pack_valid(struct packed_git *p)
1965+
{
1966+
/* An already open pack is known to be valid. */
1967+
if (p->pack_fd != -1)
1968+
return 1;
1969+
1970+
/* If the pack has one window completely covering the
1971+
* file size, the pack is known to be valid even if
1972+
* the descriptor is not currently open.
1973+
*/
1974+
if (p->windows) {
1975+
struct pack_window *w = p->windows;
1976+
1977+
if (!w->offset && w->len == p->pack_size)
1978+
return 1;
1979+
}
1980+
1981+
/* Force the pack to open to prove its valid. */
1982+
return !open_packed_git(p);
1983+
}
1984+
19351985
static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
19361986
{
19371987
static struct packed_git *last_found = (void *)1;
@@ -1961,7 +2011,7 @@ static int find_pack_entry(const unsigned char *sha1, struct pack_entry *e)
19612011
* it may have been deleted since the index
19622012
* was loaded!
19632013
*/
1964-
if (p->pack_fd == -1 && open_packed_git(p)) {
2014+
if (!is_pack_valid(p)) {
19652015
error("packfile %s cannot be accessed", p->pack_name);
19662016
goto next;
19672017
}
@@ -2360,8 +2410,6 @@ static int write_loose_object(const unsigned char *sha1, char *hdr, int hdrlen,
23602410

23612411
filename = sha1_file_name(sha1);
23622412
fd = create_tmpfile(tmpfile, sizeof(tmpfile), filename);
2363-
while (fd < 0 && errno == EMFILE && unuse_one_window(NULL, -1))
2364-
fd = create_tmpfile(tmpfile, sizeof(tmpfile), filename);
23652413
if (fd < 0) {
23662414
if (errno == EACCES)
23672415
return error("insufficient permission for adding an object to repository database %s\n", get_object_directory());

0 commit comments

Comments
 (0)