a3470f
From 72dc3a3eff84c5e17bbc3bfddec9daf50338464f Mon Sep 17 00:00:00 2001
a3470f
From: Milind Changire <mchangir@redhat.com>
a3470f
Date: Tue, 6 Mar 2018 23:08:08 +0530
a3470f
Subject: [PATCH 175/180] rpc: make actor search parallel
a3470f
a3470f
Problem:
a3470f
On a service request, the actor is searched using an exclusive mutex
a3470f
lock which is not really necessary since most of the time the actor
a3470f
list is going to be searched and not modified.
a3470f
a3470f
Solution:
a3470f
Use a read-write lock instead of a mutex lock. Only modify operations
a3470f
on a service need to be done under a write-lock which grants exclusive
a3470f
access to the code.
a3470f
a3470f
NOTE:
a3470f
This patch has been specifically been taken in to use the pthread
a3470f
rwlock primitives that rpcsvc_ownthread_reconf() is using in commit
a3470f
8503ed9b94777d47352f19ebfa844e151352b87f
a3470f
(rpcsvc: scale rpcsvc_request_handler threads)
a3470f
a3470f
mainline:
a3470f
> BUG: 1509644
a3470f
> Reviewed-on: https://review.gluster.org/18543
a3470f
> Reviewed-by: Jeff Darcy <jeff@pl.atyp.us>
a3470f
> Signed-off-by: Milind Changire <mchangir@redhat.com>
a3470f
a3470f
Change-Id: I318026c13bb6e0385dc24018a976229182fc7f79
a3470f
BUG: 1549497
a3470f
Signed-off-by: Milind Changire <mchangir@redhat.com>
a3470f
Reviewed-on: https://code.engineering.redhat.com/gerrit/131909
a3470f
Tested-by: RHGS Build Bot <nigelb@redhat.com>
a3470f
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
a3470f
---
a3470f
 rpc/rpc-lib/src/rpcsvc-common.h |  2 +-
a3470f
 rpc/rpc-lib/src/rpcsvc.c        | 54 ++++++++++++++++++++---------------------
a3470f
 2 files changed, 28 insertions(+), 28 deletions(-)
a3470f
a3470f
diff --git a/rpc/rpc-lib/src/rpcsvc-common.h b/rpc/rpc-lib/src/rpcsvc-common.h
a3470f
index dd95803..ab715d3 100644
a3470f
--- a/rpc/rpc-lib/src/rpcsvc-common.h
a3470f
+++ b/rpc/rpc-lib/src/rpcsvc-common.h
a3470f
@@ -42,7 +42,7 @@ typedef struct rpcsvc_state {
a3470f
          * other options.
a3470f
          */
a3470f
 
a3470f
-        pthread_mutex_t         rpclock;
a3470f
+        pthread_rwlock_t        rpclock;
a3470f
 
a3470f
         unsigned int            memfactor;
a3470f
 
a3470f
diff --git a/rpc/rpc-lib/src/rpcsvc.c b/rpc/rpc-lib/src/rpcsvc.c
a3470f
index 31b5eb5..fbd1071 100644
a3470f
--- a/rpc/rpc-lib/src/rpcsvc.c
a3470f
+++ b/rpc/rpc-lib/src/rpcsvc.c
a3470f
@@ -88,11 +88,11 @@ rpcsvc_listener_destroy (rpcsvc_listener_t *listener)
a3470f
                 goto listener_free;
a3470f
         }
a3470f
 
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_wrlock (&svc->rpclock);
a3470f
         {
a3470f
                 list_del_init (&listener->list);
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
 listener_free:
a3470f
         GF_FREE (listener);
a3470f
@@ -110,7 +110,7 @@ rpcsvc_get_program_vector_sizer (rpcsvc_t *svc, uint32_t prognum,
a3470f
         if (!svc)
a3470f
                 return NULL;
a3470f
 
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_rdlock (&svc->rpclock);
a3470f
         {
a3470f
                 /* Find the matching RPC program from registered list */
a3470f
                 list_for_each_entry (program, &svc->programs, program) {
a3470f
@@ -121,7 +121,7 @@ rpcsvc_get_program_vector_sizer (rpcsvc_t *svc, uint32_t prognum,
a3470f
                         }
a3470f
                 }
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
         if (found) {
a3470f
                 /* Make sure the requested procnum is supported by RPC prog */
a3470f
@@ -237,7 +237,7 @@ rpcsvc_program_actor (rpcsvc_request_t *req)
a3470f
 
a3470f
         svc = req->svc;
a3470f
         peername = req->trans->peerinfo.identifier;
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_rdlock (&svc->rpclock);
a3470f
         {
a3470f
                 list_for_each_entry (program, &svc->programs, program) {
a3470f
                         if (program->prognum == req->prognum) {
a3470f
@@ -251,7 +251,7 @@ rpcsvc_program_actor (rpcsvc_request_t *req)
a3470f
                         }
a3470f
                 }
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
         if (!found) {
a3470f
                 if (err != PROG_MISMATCH) {
a3470f
@@ -735,7 +735,7 @@ rpcsvc_handle_disconnect (rpcsvc_t *svc, rpc_transport_t *trans)
a3470f
         event = (trans->listener == NULL) ? RPCSVC_EVENT_LISTENER_DEAD
a3470f
                 : RPCSVC_EVENT_DISCONNECT;
a3470f
 
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_rdlock (&svc->rpclock);
a3470f
         {
a3470f
                 if (!svc->notify_count)
a3470f
                         goto unlock;
a3470f
@@ -755,7 +755,7 @@ rpcsvc_handle_disconnect (rpcsvc_t *svc, rpc_transport_t *trans)
a3470f
                 wrapper_count = i;
a3470f
         }
a3470f
 unlock:
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
         if (wrappers) {
a3470f
                 for (i = 0; i < wrapper_count; i++) {
a3470f
@@ -1495,7 +1495,7 @@ rpcsvc_get_listener (rpcsvc_t *svc, uint16_t port, rpc_transport_t *trans)
a3470f
                 goto out;
a3470f
         }
a3470f
 
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_rdlock (&svc->rpclock);
a3470f
         {
a3470f
                 list_for_each_entry (listener, &svc->listeners, list) {
a3470f
                         if (trans != NULL) {
a3470f
@@ -1521,7 +1521,7 @@ rpcsvc_get_listener (rpcsvc_t *svc, uint16_t port, rpc_transport_t *trans)
a3470f
                         }
a3470f
                 }
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
         if (!found) {
a3470f
                 listener = NULL;
a3470f
@@ -1566,7 +1566,7 @@ rpcsvc_program_unregister (rpcsvc_t *svc, rpcsvc_program_t *program)
a3470f
                         " program failed");
a3470f
                 goto out;
a3470f
         }
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_rdlock (&svc->rpclock);
a3470f
         {
a3470f
                 list_for_each_entry (prog, &svc->programs, program) {
a3470f
                         if ((prog->prognum == program->prognum)
a3470f
@@ -1575,7 +1575,7 @@ rpcsvc_program_unregister (rpcsvc_t *svc, rpcsvc_program_t *program)
a3470f
                         }
a3470f
                 }
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
         if (prog == NULL) {
a3470f
                 ret = -1;
a3470f
@@ -1592,11 +1592,11 @@ rpcsvc_program_unregister (rpcsvc_t *svc, rpcsvc_program_t *program)
a3470f
                 goto out;
a3470f
         }
a3470f
 
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_wrlock (&svc->rpclock);
a3470f
         {
a3470f
                 list_del_init (&prog->program);
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
         ret = 0;
a3470f
 out:
a3470f
@@ -1655,11 +1655,11 @@ rpcsvc_listener_alloc (rpcsvc_t *svc, rpc_transport_t *trans)
a3470f
 
a3470f
         INIT_LIST_HEAD (&listener->list);
a3470f
 
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_wrlock (&svc->rpclock);
a3470f
         {
a3470f
                 list_add_tail (&listener->list, &svc->listeners);
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 out:
a3470f
         return listener;
a3470f
 }
a3470f
@@ -1813,7 +1813,7 @@ rpcsvc_unregister_notify (rpcsvc_t *svc, rpcsvc_notify_t notify, void *mydata)
a3470f
                 goto out;
a3470f
         }
a3470f
 
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_wrlock (&svc->rpclock);
a3470f
         {
a3470f
                 list_for_each_entry_safe (wrapper, tmp, &svc->notify, list) {
a3470f
                         if ((wrapper->notify == notify)
a3470f
@@ -1824,7 +1824,7 @@ rpcsvc_unregister_notify (rpcsvc_t *svc, rpcsvc_notify_t notify, void *mydata)
a3470f
                         }
a3470f
                 }
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
 out:
a3470f
         return ret;
a3470f
@@ -1844,12 +1844,12 @@ rpcsvc_register_notify (rpcsvc_t *svc, rpcsvc_notify_t notify, void *mydata)
a3470f
         wrapper->data = mydata;
a3470f
         wrapper->notify = notify;
a3470f
 
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_wrlock (&svc->rpclock);
a3470f
         {
a3470f
                 list_add_tail (&wrapper->list, &svc->notify);
a3470f
                 svc->notify_count++;
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
         ret = 0;
a3470f
 out:
a3470f
@@ -1987,7 +1987,7 @@ rpcsvc_program_register (rpcsvc_t *svc, rpcsvc_program_t *program)
a3470f
                 goto out;
a3470f
         }
a3470f
 
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_rdlock (&svc->rpclock);
a3470f
         {
a3470f
                 list_for_each_entry (newprog, &svc->programs, program) {
a3470f
                         if ((newprog->prognum == program->prognum)
a3470f
@@ -1997,7 +1997,7 @@ rpcsvc_program_register (rpcsvc_t *svc, rpcsvc_program_t *program)
a3470f
                         }
a3470f
                 }
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
         if (already_registered) {
a3470f
                 ret = 0;
a3470f
@@ -2031,11 +2031,11 @@ rpcsvc_program_register (rpcsvc_t *svc, rpcsvc_program_t *program)
a3470f
                 }
a3470f
         }
a3470f
 
a3470f
-        pthread_mutex_lock (&svc->rpclock);
a3470f
+        pthread_rwlock_wrlock (&svc->rpclock);
a3470f
         {
a3470f
                 list_add_tail (&newprog->program, &svc->programs);
a3470f
         }
a3470f
-        pthread_mutex_unlock (&svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&svc->rpclock);
a3470f
 
a3470f
         ret = 0;
a3470f
         gf_log (GF_RPCSVC, GF_LOG_DEBUG, "New program registered: %s, Num: %d,"
a3470f
@@ -2077,7 +2077,7 @@ build_prog_details (rpcsvc_request_t *req, gf_dump_rsp *rsp)
a3470f
         if (!req || !req->trans || !req->svc)
a3470f
                 goto out;
a3470f
 
a3470f
-        pthread_mutex_lock (&req->svc->rpclock);
a3470f
+        pthread_rwlock_rdlock (&req->svc->rpclock);
a3470f
         {
a3470f
                 list_for_each_entry (program, &req->svc->programs, program) {
a3470f
                         prog = GF_CALLOC (1, sizeof (*prog), 0);
a3470f
@@ -2098,7 +2098,7 @@ build_prog_details (rpcsvc_request_t *req, gf_dump_rsp *rsp)
a3470f
                         ret = 0;
a3470f
         }
a3470f
 unlock:
a3470f
-        pthread_mutex_unlock (&req->svc->rpclock);
a3470f
+        pthread_rwlock_unlock (&req->svc->rpclock);
a3470f
 out:
a3470f
         return ret;
a3470f
 }
a3470f
@@ -2456,7 +2456,7 @@ rpcsvc_init (xlator_t *xl, glusterfs_ctx_t *ctx, dict_t *options,
a3470f
         if (!svc)
a3470f
                 return NULL;
a3470f
 
a3470f
-        pthread_mutex_init (&svc->rpclock, NULL);
a3470f
+        pthread_rwlock_init (&svc->rpclock, NULL);
a3470f
         INIT_LIST_HEAD (&svc->authschemes);
a3470f
         INIT_LIST_HEAD (&svc->notify);
a3470f
         INIT_LIST_HEAD (&svc->listeners);
a3470f
-- 
a3470f
1.8.3.1
a3470f