From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Date: Mon, 6 May 2019 13:45:20 +0300 From: Vladimir Davydov Subject: Re: [PATCH 3/3] test: slab_arena -- Verify madvise Message-ID: <20190506104520.it7uem5woxykg5xt@esperanza> References: <20190501155006.14546-1-gorcunov@gmail.com> <20190501155006.14546-4-gorcunov@gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190501155006.14546-4-gorcunov@gmail.com> To: Cyrill Gorcunov Cc: tml , Alexander Turenko List-ID: [Cc += Alexander re tests] On Wed, May 01, 2019 at 06:50:06PM +0300, Cyrill Gorcunov wrote: > Since madvise support depends on sys libraries > and kernel version we print error if only small > supports it and /proc/self/smaps provide more > less decent VmFlags. TBO I wouldn't bother testing this feature at all, because it's way too platform dependent. Alternatively, we could test it by crashing a process and checking the size of the generated core file, but then again it doesn't look like a portable way. Alexander, any ideas? > > Part of #3509 > --- > https://github.com/tarantool/tarantool/issues/3509 > > test/slab_arena.c | 90 +++++++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 90 insertions(+) > > diff --git a/test/slab_arena.c b/test/slab_arena.c > index d0a78d1..5c5085c 100644 > --- a/test/slab_arena.c > +++ b/test/slab_arena.c > @@ -3,6 +3,7 @@ > #include > #include > #include > +#include > #include > #include "unit.h" > > @@ -15,6 +16,93 @@ slab_arena_print(struct slab_arena *arena) > arena->used, arena->slab_size); > } > > +#define is_hex_digit(c) \ > + (((c) >= '0' && (c) <= '9') || \ > + ((c) >= 'a' && (c) <= 'f') || \ > + ((c) >= 'A' && (c) <= 'F')) > + > +static int is_vma_range_fmt(char *line, unsigned long *start, unsigned long *end) > +{ > + char *p = line; > + while (*line && is_hex_digit(*line)) > + line++; > + > + if (*line++ != '-') > + return 0; > + > + while (*line && is_hex_digit(*line)) > + line++; > + > + if (*line++ != ' ') > + return 0; > + > + sscanf(p, "%lx-%lx", start, end); > + return 1; > +} > + > +static void > +slab_test_madvise(void) > +{ > + struct slab_arena arena; > + struct quota quota; > + FILE *f = NULL; > + void *ptr; > + > + quota_init("a, 2000000); > + slab_arena_create(&arena, "a, 3000000, 1, MAP_PRIVATE); > + > + /* > + * Will fetch from preallocated area. > + */ > + ptr = slab_map(&arena); > + if (!ptr) { > + printf("can't obtain slab\n"); > + goto out; > + } > + > + /* > + * If system supports madvise call, we should try > + * fetch the precise VMA state from smaps file, > + * but if only it exists. > + */ > + if (!slab_arena_madvise_dontdump(&arena)) { > + unsigned long start = 0, end = 0; > + char buf[1024], *tok = NULL; > + bool found = false; > + > + f = fopen("/proc/self/smaps", "r"); > + if (!f) > + goto out; > + > + while (fgets(buf, sizeof(buf), f)) { > + is_vma_range_fmt(buf, &start, &end); > + if (strncmp(buf, "VmFlags: ", 9) || (void *)start != ptr) > + continue; > + tok = buf; > + break; > + } > + > + if (tok) { > + for (tok = strtok(tok, " \n"); tok; > + tok = strtok(NULL, " \n")) { > + if (strcmp(tok, "dd")) > + continue; > + found = true; > + break; > + } > + } > + > + if (!found) > + printf("ERROR: Expected dd flag on VMA address %p\n", ptr); > + > + fclose(f); > + } > + > +out: > + slab_unmap(&arena, ptr); > + slab_arena_destroy(&arena); > +} > + > int main() > { > struct quota quota; > @@ -42,4 +130,6 @@ int main() > slab_arena_create(&arena, "a, 3000000, 1, MAP_PRIVATE); > slab_arena_print(&arena); > slab_arena_destroy(&arena); > + > + slab_test_madvise(); > }