From f114fec545ba2189f3b2a0797b2ede8921a0f5f7 Mon Sep 17 00:00:00 2001 From: Howard Chu Date: Thu, 16 Aug 2012 10:25:40 -0700 Subject: [PATCH 1/2] Loop thru all DBs and show their stats This is quick and dirty, should be integrated into mdb_stat. --- libraries/libmdb/mdb_stata.c | 82 ++++++++++++++++++++++++++++++++++++ 1 file changed, 82 insertions(+) create mode 100644 libraries/libmdb/mdb_stata.c diff --git a/libraries/libmdb/mdb_stata.c b/libraries/libmdb/mdb_stata.c new file mode 100644 index 0000000000..106cb8eb1f --- /dev/null +++ b/libraries/libmdb/mdb_stata.c @@ -0,0 +1,82 @@ +/* mdb_stat.c - memory-mapped database status tool */ +/* + * Copyright 2011 Howard Chu, Symas Corp. + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted only as authorized by the OpenLDAP + * Public License. + * + * A copy of this license is available in the file LICENSE in the + * top-level directory of the distribution or, alternatively, at + * . + */ +#include +#include +#include +#include "mdb.h" + +int main(int argc,char * argv[]) +{ + int rc; + MDB_env *env; + MDB_txn *txn; + MDB_dbi dbi; + MDB_stat mst; + MDB_cursor *cursor; + MDB_val key; + char *envname = argv[1]; + + rc = mdb_env_create(&env); + + mdb_env_set_maxdbs(env, 4); + + rc = mdb_env_open(env, envname, MDB_RDONLY, 0); + if (rc) { + printf("mdb_env_open failed, error %d\n", rc); + exit(1); + } + rc = mdb_txn_begin(env, NULL, 1, &txn); + if (rc) { + printf("mdb_txn_begin failed, error %d\n", rc); + exit(1); + } + rc = mdb_open(txn, NULL, 0, &dbi); + if (rc) { + printf("mdb_open failed, error %d\n", rc); + exit(1); + } + + rc = mdb_stat(txn, dbi, &mst); + printf("Page size: %u\n", mst.ms_psize); + printf("Tree depth: %u\n", mst.ms_depth); + printf("Branch pages: %zu\n", mst.ms_branch_pages); + printf("Leaf pages: %zu\n", mst.ms_leaf_pages); + printf("Overflow pages: %zu\n", mst.ms_overflow_pages); + printf("Entries: %zu\n", mst.ms_entries); + + rc = mdb_cursor_open(txn, dbi, &cursor); + while ((rc = mdb_cursor_get(cursor, &key, NULL, MDB_NEXT)) == 0) { + char *str = malloc(key.mv_size+1); + MDB_dbi db2; + memcpy(str, key.mv_data, key.mv_size); + str[key.mv_size] = '\0'; + printf("\n%s\n", str); + rc = mdb_open(txn, str, 0, &db2); + if (rc) break; + free(str); + rc = mdb_stat(txn, db2, &mst); + printf("Tree depth: %u\n", mst.ms_depth); + printf("Branch pages: %zu\n", mst.ms_branch_pages); + printf("Leaf pages: %zu\n", mst.ms_leaf_pages); + printf("Overflow pages: %zu\n", mst.ms_overflow_pages); + printf("Entries: %zu\n", mst.ms_entries); + mdb_close(env, db2); + } + mdb_cursor_close(cursor); + mdb_close(env, dbi); + mdb_txn_abort(txn); + mdb_env_close(env); + + return 0; +} From 9f983b7999e380300ba83156f21b15d33fe2e680 Mon Sep 17 00:00:00 2001 From: Chris Mikkelson Date: Wed, 22 Aug 2012 13:22:19 -0500 Subject: [PATCH 2/2] ITS#7363 Use posix semaphores on apple and bsd systems. --- libraries/libmdb/mdb.c | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/libraries/libmdb/mdb.c b/libraries/libmdb/mdb.c index d1184d11a1..0e02685468 100644 --- a/libraries/libmdb/mdb.c +++ b/libraries/libmdb/mdb.c @@ -61,9 +61,13 @@ #include /* defines BYTE_ORDER on HPUX and Solaris */ #endif +#if defined(__APPLE__) || defined (BSD) +#define USE_POSIX_SEM +#endif + #ifndef _WIN32 #include -#ifdef __APPLE__ +#ifdef USE_POSIX_SEM #include #endif #endif @@ -150,7 +154,7 @@ #define close(fd) CloseHandle(fd) #define munmap(ptr,len) UnmapViewOfFile(ptr) #else -#ifdef __APPLE__ +#ifdef USE_POSIX_SEM #define LOCK_MUTEX_R(env) sem_wait((env)->me_rmutex) #define UNLOCK_MUTEX_R(env) sem_post((env)->me_rmutex) #define LOCK_MUTEX_W(env) sem_wait((env)->me_wmutex) @@ -175,7 +179,7 @@ /** Unlock the writer mutex. */ #define UNLOCK_MUTEX_W(env) pthread_mutex_unlock(&(env)->me_txns->mti_wmutex) -#endif /* __APPLE__ */ +#endif /* USE_POSIX_SEM */ /** Get the error code for the last failed system function. */ @@ -200,7 +204,7 @@ #define GET_PAGESIZE(x) ((x) = sysconf(_SC_PAGE_SIZE)) #endif -#if defined(_WIN32) || defined(__APPLE__) +#if defined(_WIN32) || defined(USE_POSIX_SEM) #define MNAME_LEN 32 #else #define MNAME_LEN (sizeof(pthread_mutex_t)) @@ -464,7 +468,7 @@ typedef struct MDB_txbody { uint32_t mtb_magic; /** Version number of this lock file. Must be set to #MDB_VERSION. */ uint32_t mtb_version; -#if defined(_WIN32) || defined(__APPLE__) +#if defined(_WIN32) || defined(USE_POSIX_SEM) char mtb_rmname[MNAME_LEN]; #else /** Mutex protecting access to this table. @@ -497,7 +501,7 @@ typedef struct MDB_txninfo { char pad[(sizeof(MDB_txbody)+CACHELINE-1) & ~(CACHELINE-1)]; } mt1; union { -#if defined(_WIN32) || defined(__APPLE__) +#if defined(_WIN32) || defined(USE_POSIX_SEM) char mt2_wmname[MNAME_LEN]; #define mti_wmname mt2.mt2_wmname #else @@ -914,7 +918,7 @@ struct MDB_env { HANDLE me_rmutex; /* Windows mutexes don't reside in shared mem */ HANDLE me_wmutex; #endif -#ifdef __APPLE__ +#ifdef USE_POSIX_SEM sem_t *me_rmutex; /* Apple doesn't support shared mutexes */ sem_t *me_wmutex; #endif @@ -2644,7 +2648,7 @@ mdb_env_share_locks(MDB_env *env) } #endif } -#if defined(_WIN32) || defined(__APPLE__) +#if defined(_WIN32) || defined(USE_POSIX_SEM) /* * hash_64 - 64 bit Fowler/Noll/Vo-0 FNV-1a hash code * @@ -2883,7 +2887,7 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl) goto fail; } #else /* _WIN32 */ -#ifdef __APPLE__ +#ifdef USE_POSIX_SEM struct stat stbuf; struct { dev_t dev; @@ -2920,7 +2924,7 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl) rc = ErrCode(); goto fail; } -#else /* __APPLE__ */ +#else /* USE_POSIX_SEM */ pthread_mutexattr_t mattr; pthread_mutexattr_init(&mattr); @@ -2930,7 +2934,7 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl) } pthread_mutex_init(&env->me_txns->mti_mutex, &mattr); pthread_mutex_init(&env->me_txns->mti_wmutex, &mattr); -#endif /* __APPLE__ */ +#endif /* USE_POSIX_SEM */ #endif /* _WIN32 */ env->me_txns->mti_version = MDB_VERSION; env->me_txns->mti_magic = MDB_MAGIC; @@ -2965,7 +2969,7 @@ mdb_env_setup_locks(MDB_env *env, char *lpath, int mode, int *excl) goto fail; } #endif -#ifdef __APPLE__ +#ifdef USE_POSIX_SEM env->me_rmutex = sem_open(env->me_txns->mti_rmname, 0); if (!env->me_rmutex) { rc = ErrCode();