The hdb api is a perfect candidate for spinlocks to protect the critical
sections in the database.  This patch adds them (if available on the os)
to the hdb API.  It also adds functions to declare different kinds of
hdb databases.

performance increase = 25% msgs/sec for evsbench with small message
sizes.

regards
-steve
Index: lcr/lcr_ifact.c
===================================================================
--- lcr/lcr_ifact.c	(revision 2045)
+++ lcr/lcr_ifact.c	(working copy)
@@ -61,6 +61,11 @@
 	void (*destructor) (void *context);
 };
 	
+DECLARE_HDB_DATABASE_FIRSTRUN (lcr_component_instance_database);
+
+DECLARE_HDB_DATABASE_FIRSTRUN (lcr_iface_instance_database);
+
+/*
 static struct hdb_handle_database lcr_component_instance_database = {
 	.handle_count	= 0,
 	.handles	= 0,
@@ -72,6 +77,7 @@
 	.handles	= 0,
 	.iterator	= 0
 };
+*/
 
 static hdb_handle_t g_component_handle = 0xFFFFFFFF;
 
Index: include/corosync/hdb.h
===================================================================
--- include/corosync/hdb.h	(revision 2045)
+++ include/corosync/hdb.h	(working copy)
@@ -60,14 +60,80 @@
 	unsigned int handle_count;
 	struct hdb_handle *handles;
 	unsigned int iterator;
-	pthread_mutex_t mutex;
+#if defined(HAVE_PTHREAD_SPIN_LOCK)
+	pthread_spinlock_t lock;
+#else
+	pthread_mutex_t lock;
+#endif
+	unsigned int first_run;
 };
 
+#if defined(HAVE_PTHREAD_SPIN_LOCK)
+static inline void hdb_database_lock (pthread_spinlock_t *spinlock)
+{
+	pthread_spin_lock (spinlock);
+}
+
+static inline void hdb_database_unlock (pthread_spinlock_t *spinlock)
+{
+	pthread_spin_unlock (spinlock);
+}
+static inline void hdb_database_lock_init (pthread_spinlock_t *spinlock)
+{
+	pthread_spin_init (spinlock, 0);
+}
+
+static inline void hdb_database_lock_destroy (pthread_spinlock_t *spinlock)
+{
+	pthread_spin_destroy (spinlock);
+}
+
+#else
+static inline void hdb_database_lock (pthread_mutex_t *mutex)
+{
+	pthread_mutex_lock (mutex);
+}
+
+static inline void hdb_database_unlock (pthread_mutex_t *mutex)
+{
+	pthread_mutex_unlock (mutex);
+}
+static inline void hdb_database_lock_init (pthread_mutex_t *mutex)
+{
+	pthread_mutex_init (mutex, NULL);
+}
+
+static inline void hdb_database_lock_destroy (pthread_mutex_t *mutex)
+{
+	pthread_mutex_destroy (mutex);
+}
+#endif
+
+#define DECLARE_HDB_DATABASE(database_name)				\
+static struct hdb_handle_database (database_name); 			\
+static void database_name##_init(void)__attribute__((constructor));	\
+static void database_name##_init(void)					\
+{                                                                       \
+	memset (&(database_name), 0, sizeof (struct hdb_handle_database));\
+	hdb_database_lock_init (&(database_name).lock);		\
+}
+
+#define DECLARE_HDB_DATABASE_FIRSTRUN(database_name)			\
+static struct hdb_handle_database (database_name) = {			\
+	.first_run = 1, 						\
+};									\
+static void database_name##_init(void)__attribute__((constructor));	\
+static void database_name##_init(void)					\
+{                                                                       \
+	memset (&(database_name), 0, sizeof (struct hdb_handle_database));\
+	hdb_database_lock_init (&(database_name).lock);		\
+}
+
 static inline void hdb_create (
 	struct hdb_handle_database *handle_database)
 {
 	memset (handle_database, 0, sizeof (struct hdb_handle_database));
-	pthread_mutex_init (&handle_database->mutex, NULL);
+	hdb_database_lock_init (&handle_database->lock);
 }
 
 static inline void hdb_destroy (
@@ -76,7 +142,7 @@
 	if (handle_database->handles) {
 		free (handle_database->handles);
 	}
-	pthread_mutex_destroy (&handle_database->mutex);
+	hdb_database_lock_destroy (&handle_database->lock);
 	memset (handle_database, 0, sizeof (struct hdb_handle_database));
 }
 
@@ -93,7 +159,11 @@
 	void *instance;
 	int i;
 
-	pthread_mutex_lock (&handle_database->mutex);
+	if (handle_database->first_run == 1) {
+		memset (handle_database, 0, sizeof (struct hdb_handle_database));
+		hdb_database_lock_init (&handle_database->lock);
+	}
+	hdb_database_lock (&handle_database->lock);
 
 	for (handle = 0; handle < handle_database->handle_count; handle++) {
 		if (handle_database->handles[handle].state == HDB_HANDLE_STATE_EMPTY) {
@@ -107,7 +177,7 @@
 		new_handles = (struct hdb_handle *)realloc (handle_database->handles,
 			sizeof (struct hdb_handle) * handle_database->handle_count);
 		if (new_handles == NULL) {
-			pthread_mutex_unlock (&handle_database->mutex);
+			hdb_database_unlock (&handle_database->lock);
 			return (-1);
 		}
 		handle_database->handles = new_handles;
@@ -143,7 +213,7 @@
 
 	*handle_id_out = (((unsigned long long)(check)) << 32) | handle;
 
-	pthread_mutex_unlock (&handle_database->mutex);
+	hdb_database_unlock (&handle_database->lock);
 
 	return (0);
 }
@@ -156,23 +226,23 @@
 	unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
 	unsigned int handle = handle_in & 0xffffffff;
 
-	pthread_mutex_lock (&handle_database->mutex);
+	hdb_database_lock (&handle_database->lock);
 
 	if (check != 0xffffffff &&
 		check != handle_database->handles[handle].check) {
 
-		pthread_mutex_unlock (&handle_database->mutex);
+		hdb_database_unlock (&handle_database->lock);
 		return (-1);
 	}
 
 	*instance = NULL;
 	if (handle >= handle_database->handle_count) {
-		pthread_mutex_unlock (&handle_database->mutex);
+		hdb_database_unlock (&handle_database->lock);
 		return (-1);
 	}
 
 	if (handle_database->handles[handle].state != HDB_HANDLE_STATE_ACTIVE) {
-		pthread_mutex_unlock (&handle_database->mutex);
+		hdb_database_unlock (&handle_database->lock);
 		return (-1);
 	}
 
@@ -180,7 +250,7 @@
 
 	handle_database->handles[handle].ref_count += 1;
 
-	pthread_mutex_unlock (&handle_database->mutex);
+	hdb_database_unlock (&handle_database->lock);
 	return (0);
 }
 
@@ -191,12 +261,12 @@
 	unsigned int check = ((unsigned int)(((unsigned long long)handle_in) >> 32));
 	unsigned int handle = handle_in & 0xffffffff;
 
-	pthread_mutex_lock (&handle_database->mutex);
+	hdb_database_lock (&handle_database->lock);
 
 	if (check != 0xffffffff &&
 		check != handle_database->handles[handle].check) {
 
-		pthread_mutex_unlock (&handle_database->mutex);
+		hdb_database_unlock (&handle_database->lock);
 		return (-1);
 	}
 		
@@ -207,7 +277,7 @@
 		free (handle_database->handles[handle].instance);
 		memset (&handle_database->handles[handle], 0, sizeof (struct hdb_handle));
 	}
-	pthread_mutex_unlock (&handle_database->mutex);
+	hdb_database_unlock (&handle_database->lock);
 	return (0);
 }
 
@@ -219,16 +289,16 @@
 	unsigned int handle = handle_in & 0xffffffff;
 	int res;
 
-	pthread_mutex_lock (&handle_database->mutex);
+	hdb_database_lock (&handle_database->lock);
 
 	if (check != 0xffffffff &&
 		check != handle_database->handles[handle].check) {
-		pthread_mutex_unlock (&handle_database->mutex);
+		hdb_database_unlock (&handle_database->lock);
 		return (-1);
 	}
 
 	handle_database->handles[handle].state = HDB_HANDLE_STATE_PENDINGREMOVAL;
-	pthread_mutex_unlock (&handle_database->mutex);
+	hdb_database_unlock (&handle_database->lock);
 	res = hdb_handle_put (handle_database, handle);
 	return (res);
 }
Index: exec/totemnet.c
===================================================================
--- exec/totemnet.c	(revision 2045)
+++ exec/totemnet.c	(working copy)
@@ -206,15 +206,7 @@
 
 static struct totem_ip_address localhost;
 
-/*
- * All instances in one database
- */
-static struct hdb_handle_database totemnet_instance_database = {
-	.handle_count	= 0,
-	.handles	= 0,
-	.iterator	= 0,
-	.mutex		= PTHREAD_MUTEX_INITIALIZER
-};
+DECLARE_HDB_DATABASE (totemnet_instance_database);
 
 static void totemnet_instance_initialize (struct totemnet_instance *instance)
 {
Index: exec/totempg.c
===================================================================
--- exec/totempg.c	(revision 2045)
+++ exec/totempg.c	(working copy)
@@ -227,12 +227,7 @@
 	int groups_cnt;
 };
 
-static struct hdb_handle_database totempg_groups_instance_database = {
-	.handle_count	= 0,
-	.handles	= 0,
-	.iterator	= 0,
-	.mutex		= PTHREAD_MUTEX_INITIALIZER
-};
+DECLARE_HDB_DATABASE (totempg_groups_instance_database);
 
 static unsigned char next_fragment = 1;
 
Index: exec/objdb.c
===================================================================
--- exec/objdb.c	(revision 2045)
+++ exec/objdb.c	(working copy)
@@ -98,21 +98,10 @@
 static pthread_t lock_thread;
 static pthread_mutex_t meta_lock;
 
-static struct hdb_handle_database object_instance_database = {
-	.handle_count	= 0,
-	.handles	= 0,
-	.iterator	= 0,
-	.mutex		= PTHREAD_MUTEX_INITIALIZER
-};
+DECLARE_HDB_DATABASE (object_instance_database);
 
-static struct hdb_handle_database object_find_instance_database = {
-	.handle_count	= 0,
-	.handles	= 0,
-	.iterator	= 0,
-	.mutex		= PTHREAD_MUTEX_INITIALIZER
-};
+DECLARE_HDB_DATABASE (object_find_instance_database);
 
-
 static void objdb_wrlock(void)
 {
 	pthread_mutex_lock(&meta_lock);
Index: exec/coropoll.c
===================================================================
--- exec/coropoll.c	(revision 2045)
+++ exec/coropoll.c	(working copy)
@@ -63,14 +63,7 @@
 	int stop_requested;
 };
 
-/*
- * All instances in one database
- */
-static struct hdb_handle_database poll_instance_database = {
-	.handle_count	= 0,
-	.handles	= 0,
-	.iterator	= 0
-};
+DECLARE_HDB_DATABASE (poll_instance_database);
 
 hdb_handle_t poll_create (void)
 {
Index: exec/totemrrp.c
===================================================================
--- exec/totemrrp.c	(revision 2045)
+++ exec/totemrrp.c	(working copy)
@@ -467,14 +467,8 @@
 /*
  * All instances in one database
  */
-static struct hdb_handle_database totemrrp_instance_database = {
-	.handle_count	= 0,
-	.handles	= 0,
-	.iterator	= 0,
-	.mutex		= PTHREAD_MUTEX_INITIALIZER
-};
+DECLARE_HDB_DATABASE (totemrrp_instance_database);
 
-
 #define log_printf(level, format, args...)				\
 do {									\
 	rrp_instance->totemrrp_log_printf (				\
Index: exec/totemsrp.c
===================================================================
--- exec/totemsrp.c	(revision 2045)
+++ exec/totemsrp.c	(working copy)
@@ -612,12 +612,8 @@
 /*
  * All instances in one database
  */
-static struct hdb_handle_database totemsrp_instance_database = {
-	.handle_count	= 0,
-	.handles	= 0,
-	.iterator	= 0,
-	.mutex		= PTHREAD_MUTEX_INITIALIZER
-};
+DECLARE_HDB_DATABASE (totemsrp_instance_database);
+
 struct message_handlers totemsrp_message_handlers = {
 	6,
 	{
_______________________________________________
Openais mailing list
[email protected]
https://lists.linux-foundation.org/mailman/listinfo/openais

Reply via email to