diff --git a/mdb.c b/mdb.c index a07306c8..0c7397c2 100644 --- a/mdb.c +++ b/mdb.c @@ -4184,8 +4184,8 @@ mdb_env_map(MDB_env *env, void *addr) if (ftruncate(env->me_fd, env->me_mapsize) < 0) return errno; } - env->me_map = mmap(addr, env->me_mapsize, prot, MAP_SHARED, - env->me_fd, 0); + + env->me_map = mmap(addr, env->me_mapsize, prot, MAP_SHARED, env->me_fd, 0); if (env->me_map == MAP_FAILED) { env->me_map = NULL; return errno; @@ -4195,11 +4195,9 @@ mdb_env_map(MDB_env *env, void *addr) /* Turn off readahead. It's harmful when the DB is larger than RAM. */ #ifdef MADV_RANDOM madvise(env->me_map, env->me_mapsize, MADV_RANDOM); -#else -#ifdef POSIX_MADV_RANDOM +#elif defined(POSIX_MADV_RANDOM) posix_madvise(env->me_map, env->me_mapsize, POSIX_MADV_RANDOM); -#endif /* POSIX_MADV_RANDOM */ -#endif /* MADV_RANDOM */ +#endif /* MADV_RANDOM & POSIX_MADV_RANDOM */ } /* Can happen because the address argument to mmap() is just a @@ -4207,8 +4205,10 @@ mdb_env_map(MDB_env *env, void *addr) * The MAP_FIXED flag would prevent that, but then mmap could * instead unmap existing pages to make room for the new map. */ - if (addr && env->me_map != addr) + if (addr && env->me_map != addr) { + errno = 0; /* LY: clean errno as a hit for this case */ return EBUSY; /* TODO: Make a new MDB_* error code? */ + } /* Lock meta pages to avoid unexpected write, * before the data pages would be synchronized. */ diff --git a/mtest0.c b/mtest0.c index 4fe9a15a..0fdd0898 100644 --- a/mtest0.c +++ b/mtest0.c @@ -14,6 +14,9 @@ #include #include #include +#include +#include +#include #include "lmdb.h" #define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) @@ -21,6 +24,10 @@ #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) +#ifndef DBPATH +# define DBPATH "./testdb" +#endif + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -34,6 +41,8 @@ int main(int argc,char * argv[]) int count; int *values; char sval[32] = ""; + int env_oflags; + struct stat db_stat, exe_stat; srand(time(NULL)); @@ -47,7 +56,19 @@ int main(int argc,char * argv[]) E(mdb_env_create(&env)); E(mdb_env_set_maxreaders(env, 1)); E(mdb_env_set_mapsize(env, 10485760)); - E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP /*|MDB_NOSYNC*/, 0664)); + + E(stat("/proc/self/exe", &exe_stat)?errno:0); + E(stat(DBPATH "/.", &db_stat)?errno:0); + env_oflags = MDB_FIXEDMAP | MDB_NOSYNC; + if (major(db_stat.st_dev) != major(exe_stat.st_dev)) { + /* LY: Assume running inside a CI-environment: + * 1) don't use FIXEDMAP to avoid EBUSY in case collision, + * which could be inspired by address space randomisation feature. + * 2) drop MDB_NOSYNC expecting that DBPATH is at a tmpfs or some dedicated storage. + */ + env_oflags = 0; + } + E(mdb_env_open(env, DBPATH, env_oflags, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); E(mdb_dbi_open(txn, NULL, 0, &dbi)); diff --git a/mtest1.c b/mtest1.c index eb26ffd2..e531cacb 100644 --- a/mtest1.c +++ b/mtest1.c @@ -23,6 +23,9 @@ #include #include #include +#include +#include +#include #include "lmdb.h" #define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) @@ -30,6 +33,10 @@ #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) +#ifndef DBPATH +# define DBPATH "./testdb" +#endif + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -42,6 +49,8 @@ int main(int argc,char * argv[]) int count; int *values; char sval[32] = ""; + int env_oflags; + struct stat db_stat, exe_stat; srand(time(NULL)); @@ -53,11 +62,25 @@ int main(int argc,char * argv[]) } E(mdb_env_create(&env)); - E(mdb_env_set_maxreaders(env, 1)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_set_maxdbs(env, 4)); - E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + + E(stat("/proc/self/exe", &exe_stat)?errno:0); + E(stat(DBPATH "/.", &db_stat)?errno:0); + env_oflags = MDB_FIXEDMAP | MDB_NOSYNC; + if (major(db_stat.st_dev) != major(exe_stat.st_dev)) { + /* LY: Assume running inside a CI-environment: + * 1) don't use FIXEDMAP to avoid EBUSY in case collision, + * which could be inspired by address space randomisation feature. + * 2) drop MDB_NOSYNC expecting that DBPATH is at a tmpfs or some dedicated storage. + */ + env_oflags = 0; + } + /* LY: especially here we always needs MDB_NOSYNC + * for testing mdb_env_close_ex() and "redo-to-steady" on open. */ + env_oflags |= MDB_NOSYNC; + E(mdb_env_open(env, DBPATH, env_oflags, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); if (mdb_dbi_open(txn, "id1", MDB_CREATE, &dbi) == MDB_SUCCESS) @@ -144,7 +167,7 @@ int main(int argc,char * argv[]) mdb_env_close_ex(env, 1); E(mdb_env_create(&env)); E(mdb_env_set_maxdbs(env, 4)); - E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + E(mdb_env_open(env, DBPATH, env_oflags, 0664)); printf("check-preset-c.cursor-next\n"); E(mdb_env_stat(env, &mst)); diff --git a/mtest2.c b/mtest2.c index e1ab1ca8..5650af3f 100644 --- a/mtest2.c +++ b/mtest2.c @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include #include "lmdb.h" #define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) @@ -24,6 +27,10 @@ #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) +#ifndef DBPATH +# define DBPATH "./testdb" +#endif + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -36,6 +43,8 @@ int main(int argc,char * argv[]) int count; int *values; char sval[32] = ""; + int env_oflags; + struct stat db_stat, exe_stat; srand(time(NULL)); @@ -50,7 +59,19 @@ int main(int argc,char * argv[]) E(mdb_env_set_maxreaders(env, 1)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_set_maxdbs(env, 4)); - E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + + E(stat("/proc/self/exe", &exe_stat)?errno:0); + E(stat(DBPATH "/.", &db_stat)?errno:0); + env_oflags = MDB_FIXEDMAP | MDB_NOSYNC; + if (major(db_stat.st_dev) != major(exe_stat.st_dev)) { + /* LY: Assume running inside a CI-environment: + * 1) don't use FIXEDMAP to avoid EBUSY in case collision, + * which could be inspired by address space randomisation feature. + * 2) drop MDB_NOSYNC expecting that DBPATH is at a tmpfs or some dedicated storage. + */ + env_oflags = 0; + } + E(mdb_env_open(env, DBPATH, env_oflags, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); if (mdb_dbi_open(txn, "id2", MDB_CREATE, &dbi) == MDB_SUCCESS) diff --git a/mtest3.c b/mtest3.c index 6e3b9e02..bbb0906e 100644 --- a/mtest3.c +++ b/mtest3.c @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include #include "lmdb.h" #define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) @@ -24,6 +27,10 @@ #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) +#ifndef DBPATH +# define DBPATH "./testdb" +#endif + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -37,6 +44,8 @@ int main(int argc,char * argv[]) int *values; char sval[32]; char kval[sizeof(int)]; + int env_oflags; + struct stat db_stat, exe_stat; srand(time(NULL)); @@ -52,7 +61,19 @@ int main(int argc,char * argv[]) E(mdb_env_create(&env)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_set_maxdbs(env, 4)); - E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + + E(stat("/proc/self/exe", &exe_stat)?errno:0); + E(stat(DBPATH "/.", &db_stat)?errno:0); + env_oflags = MDB_FIXEDMAP | MDB_NOSYNC; + if (major(db_stat.st_dev) != major(exe_stat.st_dev)) { + /* LY: Assume running inside a CI-environment: + * 1) don't use FIXEDMAP to avoid EBUSY in case collision, + * which could be inspired by address space randomisation feature. + * 2) drop MDB_NOSYNC expecting that DBPATH is at a tmpfs or some dedicated storage. + */ + env_oflags = 0; + } + E(mdb_env_open(env, DBPATH, env_oflags, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); if (mdb_dbi_open(txn, "id3", MDB_CREATE, &dbi) == MDB_SUCCESS) diff --git a/mtest4.c b/mtest4.c index 22f4af85..c24a7034 100644 --- a/mtest4.c +++ b/mtest4.c @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include #include "lmdb.h" #define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) @@ -24,6 +27,10 @@ #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) +#ifndef DBPATH +# define DBPATH "./testdb" +#endif + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -37,6 +44,8 @@ int main(int argc,char * argv[]) int *values; char sval[8]; char kval[sizeof(int)]; + int env_oflags; + struct stat db_stat, exe_stat; memset(sval, 0, sizeof(sval)); @@ -50,7 +59,19 @@ int main(int argc,char * argv[]) E(mdb_env_create(&env)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_set_maxdbs(env, 4)); - E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + + E(stat("/proc/self/exe", &exe_stat)?errno:0); + E(stat(DBPATH "/.", &db_stat)?errno:0); + env_oflags = MDB_FIXEDMAP | MDB_NOSYNC; + if (major(db_stat.st_dev) != major(exe_stat.st_dev)) { + /* LY: Assume running inside a CI-environment: + * 1) don't use FIXEDMAP to avoid EBUSY in case collision, + * which could be inspired by address space randomisation feature. + * 2) drop MDB_NOSYNC expecting that DBPATH is at a tmpfs or some dedicated storage. + */ + env_oflags = 0; + } + E(mdb_env_open(env, DBPATH, env_oflags, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); if (mdb_dbi_open(txn, "id4", MDB_CREATE, &dbi) == MDB_SUCCESS) diff --git a/mtest5.c b/mtest5.c index 11913da8..877911ed 100644 --- a/mtest5.c +++ b/mtest5.c @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include #include "lmdb.h" #define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) @@ -24,6 +27,10 @@ #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) +#ifndef DBPATH +# define DBPATH "./testdb" +#endif + int main(int argc,char * argv[]) { int i = 0, j = 0, rc; @@ -37,6 +44,8 @@ int main(int argc,char * argv[]) int *values; char sval[32]; char kval[sizeof(int)]; + int env_oflags; + struct stat db_stat, exe_stat; srand(time(NULL)); @@ -52,7 +61,19 @@ int main(int argc,char * argv[]) E(mdb_env_create(&env)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_set_maxdbs(env, 4)); - E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + + E(stat("/proc/self/exe", &exe_stat)?errno:0); + E(stat(DBPATH "/.", &db_stat)?errno:0); + env_oflags = MDB_FIXEDMAP | MDB_NOSYNC; + if (major(db_stat.st_dev) != major(exe_stat.st_dev)) { + /* LY: Assume running inside a CI-environment: + * 1) don't use FIXEDMAP to avoid EBUSY in case collision, + * which could be inspired by address space randomisation feature. + * 2) drop MDB_NOSYNC expecting that DBPATH is at a tmpfs or some dedicated storage. + */ + env_oflags = 0; + } + E(mdb_env_open(env, DBPATH, env_oflags, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); if (mdb_dbi_open(txn, "id5", MDB_CREATE, &dbi) == MDB_SUCCESS) diff --git a/mtest6.c b/mtest6.c index a1255bca..51ac39e3 100644 --- a/mtest6.c +++ b/mtest6.c @@ -17,6 +17,9 @@ #include #include #include +#include +#include +#include #include "lmdb.h" #define E(expr) CHECK((rc = (expr)) == MDB_SUCCESS, #expr) @@ -24,6 +27,10 @@ #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) +#ifndef DBPATH +# define DBPATH "./testdb" +#endif + char dkbuf[1024]; int main(int argc,char * argv[]) @@ -37,13 +44,27 @@ int main(int argc,char * argv[]) MDB_cursor *cursor; long kval; char *sval; + int env_oflags; + struct stat db_stat, exe_stat; srand(time(NULL)); E(mdb_env_create(&env)); E(mdb_env_set_mapsize(env, 10485760)); E(mdb_env_set_maxdbs(env, 4)); - E(mdb_env_open(env, "./testdb", MDB_FIXEDMAP|MDB_NOSYNC, 0664)); + + E(stat("/proc/self/exe", &exe_stat)?errno:0); + E(stat(DBPATH "/.", &db_stat)?errno:0); + env_oflags = MDB_FIXEDMAP | MDB_NOSYNC; + if (major(db_stat.st_dev) != major(exe_stat.st_dev)) { + /* LY: Assume running inside a CI-environment: + * 1) don't use FIXEDMAP to avoid EBUSY in case collision, + * which could be inspired by address space randomisation feature. + * 2) drop MDB_NOSYNC expecting that DBPATH is at a tmpfs or some dedicated storage. + */ + env_oflags = 0; + } + E(mdb_env_open(env, DBPATH, env_oflags, 0664)); E(mdb_txn_begin(env, NULL, 0, &txn)); if (mdb_dbi_open(txn, "id6", MDB_CREATE, &dbi) == MDB_SUCCESS) diff --git a/wbench.c b/wbench.c index 50a7aca3..1b5f3794 100644 --- a/wbench.c +++ b/wbench.c @@ -25,6 +25,7 @@ #include #include #include +#include #include "lmdb.h" @@ -33,7 +34,9 @@ #define CHECK(test, msg) ((test) ? (void)0 : ((void)fprintf(stderr, \ "%s:%d: %s: %s\n", __FILE__, __LINE__, msg, mdb_strerror(rc)), abort())) -#define DBPATH "./testdb" +#ifndef DBPATH +# define DBPATH "./testdb" +#endif struct t0 { struct rusage ru; @@ -124,7 +127,7 @@ static void wbench(int flags, int mb, int count, int salt) E(mdb_env_create(&env)); E(mdb_env_set_mapsize(env, (1ull << 20) * mb)); - E(mdb_env_open(env, "./testdb", flags, 0664)); + E(mdb_env_open(env, DBPATH, flags, 0664)); key.mv_size = sizeof(key_value); key.mv_data = &key_value;