You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@qpid.apache.org by tr...@apache.org on 2016/09/15 21:05:21 UTC
[1/2] qpid-dispatch git commit: DISPATCH-511 - Cache-line align
mutexes and conditions. Remove extra assertions in mutexes.
Repository: qpid-dispatch
Updated Branches:
refs/heads/master 87bc8fce5 -> 375aec4a5
DISPATCH-511 - Cache-line align mutexes and conditions. Remove extra assertions in mutexes.
Project: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/commit/566a1a19
Tree: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/tree/566a1a19
Diff: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/diff/566a1a19
Branch: refs/heads/master
Commit: 566a1a19e2c662b40aa4477421bddb51e4e7310c
Parents: 87bc8fc
Author: Ted Ross <tr...@redhat.com>
Authored: Thu Sep 15 16:49:45 2016 -0400
Committer: Ted Ross <tr...@redhat.com>
Committed: Thu Sep 15 16:49:45 2016 -0400
----------------------------------------------------------------------
include/qpid/dispatch/ctools.h | 6 ++++++
src/posix/threading.c | 27 +++++----------------------
2 files changed, 11 insertions(+), 22 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/566a1a19/include/qpid/dispatch/ctools.h
----------------------------------------------------------------------
diff --git a/include/qpid/dispatch/ctools.h b/include/qpid/dispatch/ctools.h
index fc159a6..fa6ac6a 100644
--- a/include/qpid/dispatch/ctools.h
+++ b/include/qpid/dispatch/ctools.h
@@ -32,6 +32,12 @@
#define NEW_ARRAY(t,n) (t*) malloc(sizeof(t)*(n))
#define NEW_PTR_ARRAY(t,n) (t**) malloc(sizeof(t*)*(n))
+//
+// If available, use aligned_alloc for cache-line-aligned allocations. Otherwise
+// fall back to plain malloc.
+//
+#define NEW_CACHE_ALIGNED(t,p) posix_memalign((void*) &p, 64, (sizeof(t) + (sizeof(t) % 64 ? 64 - (sizeof(t) % 64) : 0)))
+
#define ZERO(p) memset(p, 0, sizeof(*p))
#define DEQ_DECLARE(i,d) typedef struct { \
http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/566a1a19/src/posix/threading.c
----------------------------------------------------------------------
diff --git a/src/posix/threading.c b/src/posix/threading.c
index d641510..85916ca 100644
--- a/src/posix/threading.c
+++ b/src/posix/threading.c
@@ -31,35 +31,21 @@
struct sys_mutex_t {
pthread_mutex_t mutex;
-#ifndef NDEBUG
- // In a debug build, used to assert correct use of mutex.
- int acquired;
-#endif
};
-// NOTE: normally it is incorrect for an assert expression to have side effects,
-// since it could change the behavior between a debug and a release build. In
-// this case however the mutex->acquired field only exists in a debug build, so
-// we want operations on mutex->acquired to be compiled out of a release build.
-#define ACQUIRE(mutex) assert(!mutex->acquired++)
-#define RELEASE(mutex) assert(!--mutex->acquired)
-
-
sys_mutex_t *sys_mutex(void)
{
- sys_mutex_t *mutex = NEW(sys_mutex_t);
+ sys_mutex_t *mutex = 0;
+ NEW_CACHE_ALIGNED(sys_mutex_t, mutex);
+ assert(mutex != 0);
pthread_mutex_init(&(mutex->mutex), 0);
-#ifndef NDEBUG
- mutex->acquired = 0;
-#endif
return mutex;
}
void sys_mutex_free(sys_mutex_t *mutex)
{
- assert(!mutex->acquired);
pthread_mutex_destroy(&(mutex->mutex));
free(mutex);
}
@@ -69,13 +55,11 @@ void sys_mutex_lock(sys_mutex_t *mutex)
{
int result = pthread_mutex_lock(&(mutex->mutex));
assert(result == 0);
- ACQUIRE(mutex);
}
void sys_mutex_unlock(sys_mutex_t *mutex)
{
- RELEASE(mutex);
int result = pthread_mutex_unlock(&(mutex->mutex));
assert(result == 0);
}
@@ -88,7 +72,8 @@ struct sys_cond_t {
sys_cond_t *sys_cond(void)
{
- sys_cond_t *cond = NEW(sys_cond_t);
+ sys_cond_t *cond = 0;
+ NEW_CACHE_ALIGNED(sys_cond_t, cond);
pthread_cond_init(&(cond->cond), 0);
return cond;
}
@@ -103,10 +88,8 @@ void sys_cond_free(sys_cond_t *cond)
void sys_cond_wait(sys_cond_t *cond, sys_mutex_t *held_mutex)
{
- RELEASE(held_mutex);
int result = pthread_cond_wait(&(cond->cond), &(held_mutex->mutex));
assert(result == 0);
- ACQUIRE(held_mutex);
}
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org
[2/2] qpid-dispatch git commit: DISPATCH-511 - Make memory-pool
statistics enableable by configuration (defaults to ON).
Posted by tr...@apache.org.
DISPATCH-511 - Make memory-pool statistics enableable by configuration (defaults to ON).
Project: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/repo
Commit: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/commit/375aec4a
Tree: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/tree/375aec4a
Diff: http://git-wip-us.apache.org/repos/asf/qpid-dispatch/diff/375aec4a
Branch: refs/heads/master
Commit: 375aec4a52de846716f202b2a066e76d8d86eb7a
Parents: 566a1a1
Author: Ted Ross <tr...@redhat.com>
Authored: Thu Sep 15 17:03:52 2016 -0400
Committer: Ted Ross <tr...@redhat.com>
Committed: Thu Sep 15 17:03:52 2016 -0400
----------------------------------------------------------------------
CMakeLists.txt | 1 +
include/qpid/dispatch/ctools.h | 5 +++--
src/alloc_pool.c | 39 +++++++++++++++++++++++++++++--------
src/alloc_pool.h | 4 ++--
src/config.h.in | 1 +
5 files changed, 38 insertions(+), 12 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/375aec4a/CMakeLists.txt
----------------------------------------------------------------------
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 989687a..5b2069d 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -28,6 +28,7 @@ project(qpid-dispatch C)
# Build time switch to turn off memory pooling.
option(USE_MEMORY_POOL "Use per-thread memory pools" ON)
+option(QD_MEMORY_STATS "Track memory pool usage statistics" ON)
file(STRINGS "${CMAKE_SOURCE_DIR}/VERSION.txt" QPID_DISPATCH_VERSION)
http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/375aec4a/include/qpid/dispatch/ctools.h
----------------------------------------------------------------------
diff --git a/include/qpid/dispatch/ctools.h b/include/qpid/dispatch/ctools.h
index fa6ac6a..6c4afec 100644
--- a/include/qpid/dispatch/ctools.h
+++ b/include/qpid/dispatch/ctools.h
@@ -36,9 +36,10 @@
// If available, use aligned_alloc for cache-line-aligned allocations. Otherwise
// fall back to plain malloc.
//
-#define NEW_CACHE_ALIGNED(t,p) posix_memalign((void*) &p, 64, (sizeof(t) + (sizeof(t) % 64 ? 64 - (sizeof(t) % 64) : 0)))
+#define NEW_CACHE_ALIGNED(t,p) posix_memalign((void*) &(p), 64, (sizeof(t) + (sizeof(t) % 64 ? 64 - (sizeof(t) % 64) : 0)))
+#define ALLOC_CACHE_ALIGNED(s,p) posix_memalign((void*) &(p), 64, (s + (s % 64 ? 64 - (s % 64) : 0)))
-#define ZERO(p) memset(p, 0, sizeof(*p))
+#define ZERO(p) memset(p, 0, sizeof(*p))
#define DEQ_DECLARE(i,d) typedef struct { \
i *head; \
http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/375aec4a/src/alloc_pool.c
----------------------------------------------------------------------
diff --git a/src/alloc_pool.c b/src/alloc_pool.c
index 69ca903..ce6fcb2 100644
--- a/src/alloc_pool.c
+++ b/src/alloc_pool.c
@@ -26,6 +26,7 @@
#include <stdio.h>
#include "entity.h"
#include "entity_cache.h"
+#include "config.h"
#if !defined(NDEBUG)
#define QD_MEMORY_DEBUG 1
@@ -89,8 +90,10 @@ static void qd_alloc_init(qd_alloc_type_desc_t *desc)
DEQ_INIT(desc->global_pool->free_list);
desc->lock = sys_mutex();
DEQ_INIT(desc->tpool_list);
+#if QD_MEMORY_STATS
desc->stats = NEW(qd_alloc_stats_t);
memset(desc->stats, 0, sizeof(qd_alloc_stats_t));
+#endif
qd_alloc_type_t *type_item = NEW(qd_alloc_type_t);
DEQ_ITEM_INIT(type_item);
@@ -114,7 +117,7 @@ void *qd_alloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool)
//
// If the descriptor is not initialized, set it up now.
//
- if (desc->trailer != PATTERN_BACK)
+ if (desc->header != PATTERN_FRONT)
qd_alloc_init(desc);
//
@@ -122,7 +125,7 @@ void *qd_alloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool)
// thread-local pool for this type.
//
if (*tpool == 0) {
- *tpool = NEW(qd_alloc_pool_t);
+ NEW_CACHE_ALIGNED(qd_alloc_pool_t, *tpool);
DEQ_ITEM_INIT(*tpool);
DEQ_INIT((*tpool)->free_list);
sys_mutex_lock(desc->lock);
@@ -157,8 +160,10 @@ void *qd_alloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool)
//
// Rebalance a full batch from the global free list to the thread list.
//
+#if QD_MEMORY_STATS
desc->stats->batches_rebalanced_to_threads++;
desc->stats->held_by_threads += desc->config->transfer_batch_size;
+#endif
for (idx = 0; idx < desc->config->transfer_batch_size; idx++) {
item = DEQ_HEAD(desc->global_pool->free_list);
DEQ_REMOVE_HEAD(desc->global_pool->free_list);
@@ -169,17 +174,20 @@ void *qd_alloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool)
// Allocate a full batch from the heap and put it on the thread list.
//
for (idx = 0; idx < desc->config->transfer_batch_size; idx++) {
- item = (qd_alloc_item_t*) malloc(sizeof(qd_alloc_item_t) + desc->total_size
+ size_t size = sizeof(qd_alloc_item_t) + desc->total_size
#ifdef QD_MEMORY_DEBUG
- + sizeof(uint32_t)
+ + sizeof(uint32_t)
#endif
- );
+ ;
+ ALLOC_CACHE_ALIGNED(size, item);
if (item == 0)
break;
DEQ_ITEM_INIT(item);
DEQ_INSERT_TAIL(pool->free_list, item);
+#if QD_MEMORY_STATS
desc->stats->held_by_threads++;
desc->stats->total_alloc_from_heap++;
+#endif
}
}
sys_mutex_unlock(desc->lock);
@@ -240,8 +248,10 @@ void qd_dealloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool, void *p)
// rebalanced back to the global list.
//
sys_mutex_lock(desc->lock);
+#if QD_MEMORY_STATS
desc->stats->batches_rebalanced_to_global++;
desc->stats->held_by_threads -= desc->config->transfer_batch_size;
+#endif
for (idx = 0; idx < desc->config->transfer_batch_size; idx++) {
item = DEQ_HEAD(pool->free_list);
DEQ_REMOVE_HEAD(pool->free_list);
@@ -257,7 +267,9 @@ void qd_dealloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool, void *p)
item = DEQ_HEAD(desc->global_pool->free_list);
DEQ_REMOVE_HEAD(desc->global_pool->free_list);
free(item);
+#if QD_MEMORY_STATS
desc->stats->total_free_to_heap++;
+#endif
}
}
@@ -308,7 +320,9 @@ void qd_alloc_finalize(void)
while (item) {
DEQ_REMOVE_HEAD(desc->global_pool->free_list);
free(item);
+#if QD_MEMORY_STATS
desc->stats->total_free_to_heap++;
+#endif
item = DEQ_HEAD(desc->global_pool->free_list);
}
free(desc->global_pool);
@@ -323,7 +337,9 @@ void qd_alloc_finalize(void)
while (item) {
DEQ_REMOVE_HEAD(tpool->free_list);
free(item);
+#if QD_MEMORY_STATS
desc->stats->total_free_to_heap++;
+#endif
item = DEQ_HEAD(tpool->free_list);
}
@@ -335,16 +351,20 @@ void qd_alloc_finalize(void)
//
// Check the stats for lost items
//
+#if QD_MEMORY_STATS
if (dump_file && desc->stats->total_free_to_heap < desc->stats->total_alloc_from_heap)
fprintf(dump_file,
"alloc.c: Items of type '%s' remain allocated at shutdown: %"PRId64"\n",
desc->type_name,
desc->stats->total_alloc_from_heap - desc->stats->total_free_to_heap);
+#endif
//
// Reclaim the descriptor components
//
+#if QD_MEMORY_STATS
free(desc->stats);
+#endif
sys_mutex_free(desc->lock);
desc->lock = 0;
desc->trailer = 0;
@@ -365,12 +385,15 @@ qd_error_t qd_entity_refresh_allocator(qd_entity_t* entity, void *impl) {
qd_entity_set_long(entity, "typeSize", alloc_type->desc->total_size) == 0 &&
qd_entity_set_long(entity, "transferBatchSize", alloc_type->desc->config->transfer_batch_size) == 0 &&
qd_entity_set_long(entity, "localFreeListMax", alloc_type->desc->config->local_free_list_max) == 0 &&
- qd_entity_set_long(entity, "globalFreeListMax", alloc_type->desc->config->global_free_list_max) == 0 &&
- qd_entity_set_long(entity, "totalAllocFromHeap", alloc_type->desc->stats->total_alloc_from_heap) == 0 &&
+ qd_entity_set_long(entity, "globalFreeListMax", alloc_type->desc->config->global_free_list_max) == 0
+#if QD_MEMORY_STATS
+ && qd_entity_set_long(entity, "totalAllocFromHeap", alloc_type->desc->stats->total_alloc_from_heap) == 0 &&
qd_entity_set_long(entity, "totalFreeToHeap", alloc_type->desc->stats->total_free_to_heap) == 0 &&
qd_entity_set_long(entity, "heldByThreads", alloc_type->desc->stats->held_by_threads) == 0 &&
qd_entity_set_long(entity, "batchesRebalancedToThreads", alloc_type->desc->stats->batches_rebalanced_to_threads) == 0 &&
- qd_entity_set_long(entity, "batchesRebalancedToGlobal", alloc_type->desc->stats->batches_rebalanced_to_global) == 0)
+ qd_entity_set_long(entity, "batchesRebalancedToGlobal", alloc_type->desc->stats->batches_rebalanced_to_global) == 0
+#endif
+ )
return QD_ERROR_NONE;
return qd_error_code();
}
http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/375aec4a/src/alloc_pool.h
----------------------------------------------------------------------
diff --git a/src/alloc_pool.h b/src/alloc_pool.h
index a4a6ac9..fcddede 100644
--- a/src/alloc_pool.h
+++ b/src/alloc_pool.h
@@ -62,7 +62,7 @@ typedef struct {
size_t *additional_size;
size_t total_size;
qd_alloc_config_t *config;
- qd_alloc_stats_t *stats;
+ qd_alloc_stats_t *stats __attribute__((aligned(64)));
qd_alloc_pool_t *global_pool;
sys_mutex_t *lock;
qd_alloc_pool_list_t tpool_list;
@@ -87,7 +87,7 @@ void qd_dealloc(qd_alloc_type_desc_t *desc, qd_alloc_pool_t **tpool, void *p);
*@internal
*/
#define ALLOC_DEFINE_CONFIG(T,S,A,C) \
- qd_alloc_type_desc_t __desc_##T = {0, #T, S, A, 0, C, 0, 0, 0, {0,0}, 0}; \
+ qd_alloc_type_desc_t __desc_##T __attribute__((aligned(64))) = {0, #T, S, A, 0, C, 0, 0, 0, {0,0}, 0}; \
__thread qd_alloc_pool_t *__local_pool_##T = 0; \
T *new_##T(void) { return (T*) qd_alloc(&__desc_##T, &__local_pool_##T); } \
void free_##T(T *p) { qd_dealloc(&__desc_##T, &__local_pool_##T, (void*) p); } \
http://git-wip-us.apache.org/repos/asf/qpid-dispatch/blob/375aec4a/src/config.h.in
----------------------------------------------------------------------
diff --git a/src/config.h.in b/src/config.h.in
index a72820b..0e0c23c 100644
--- a/src/config.h.in
+++ b/src/config.h.in
@@ -20,3 +20,4 @@
#define QPID_DISPATCH_VERSION "${QPID_DISPATCH_VERSION}"
#define QPID_DISPATCH_LIB "${QPID_DISPATCH_LIB}"
#cmakedefine01 USE_MEMORY_POOL
+#cmakedefine01 QD_MEMORY_STATS
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@qpid.apache.org
For additional commands, e-mail: commits-help@qpid.apache.org