Skip to content

libgit2 does not respect mmap window settings #1272

@scottjg

Description

@scottjg

i compiled with the following patch and observed that even though we asked libgit2 limit mmap to 64MB total, it mmaped()'ed over 200mb:

$ sudo -u git ./rdag-query . /tmp/dag.cache > log.txt
$ grep 222 log.txt |head -n1
mmaped (222510768)
diff --git a/src/mwindow.c b/src/mwindow.c
index 21f3c66..d1e6781 100644
--- a/src/mwindow.c
+++ b/src/mwindow.c
@@ -14,11 +14,12 @@

 #define DEFAULT_WINDOW_SIZE \
        (sizeof(void*) >= 8 \
-               ? 1 * 1024 * 1024 * 1024 \
+               ? 32 * 1024 * 1024 \
                : 32 * 1024 * 1024)

 #define DEFAULT_MAPPED_LIMIT \
-       ((1024 * 1024) * (sizeof(void*) >= 8 ? 8192ULL : 256UL))
+  ((1024 * 1024) * 64)
+//(sizeof(void*) >= 8 ? 8192ULL : 256UL))

 /*
  * These are the global options for mmmap limits.
diff --git a/src/unix/map.c b/src/unix/map.c
index ee7888c..a385dbb 100644
--- a/src/unix/map.c
+++ b/src/unix/map.c
@@ -12,6 +12,8 @@
 #include <sys/mman.h>
 #include <errno.h>

+static size_t mapped_bytes;
+
 int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offset)
 {
        int mprot = 0;
@@ -35,6 +37,8 @@ int p_mmap(git_map *out, size_t len, int prot, int flags, int fd, git_off_t offs
                mflag = MAP_SHARED;

        out->data = mmap(NULL, len, mprot, mflag, fd, offset);
+       mapped_bytes += len;
+       printf("mmaped (%zd)\n", mapped_bytes);

        if (!out->data || out->data == MAP_FAILED) {
                giterr_set(GITERR_OS, "Failed to mmap. Could not write data");
@@ -50,6 +54,8 @@ int p_munmap(git_map *map)
 {
        assert(map != NULL);
        munmap(map->data, map->len);
+       mapped_bytes -= map->len;
+       printf("unmapped (%zd)\n", mapped_bytes);

        return 0;
 }

//cc github/gitrpc#73

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions