[PATCH 3/3] test: slab_arena -- Verify madvise

Cyrill Gorcunov gorcunov at gmail.com
Wed May 1 18:50:06 MSK 2019


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.

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 <stdio.h>
 #include <limits.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 #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(&quota, 2000000);
+	slab_arena_create(&arena, &quota, 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, &quota, 3000000, 1, MAP_PRIVATE);
 	slab_arena_print(&arena);
 	slab_arena_destroy(&arena);
+
+	slab_test_madvise();
 }
-- 
2.20.1




More information about the Tarantool-patches mailing list