diff options
author | Mohit Agrawal <moagrawa@redhat.com> | 2017-10-20 12:39:29 +0530 |
---|---|---|
committer | Amar Tumballi <amarts@redhat.com> | 2017-12-12 09:05:56 +0000 |
commit | 430484c92ab5a6234958d1143e0bb14aeb0cd1c0 (patch) | |
tree | 0fb39e2a1c9be94396736d76b9aaababf232821b /xlators/protocol/client | |
parent | 8483ed87165c1695b513e223549d33d2d63891d9 (diff) |
glusterfs: Use gcc builtin ATOMIC operator to increase/decreate refcount.
Problem: In glusterfs code base we call mutex_lock/unlock to take
reference/dereference for a object.Sometime it could be
reason for lock contention also.
Solution: There is no need to use mutex to increase/decrease ref
counter, instead of using mutex use gcc builtin ATOMIC
operation.
Test: I have not observed yet how much performance gain after apply
this patch specific to glusterfs but i have tested same
with below small program(mutex and atomic both) and
get good difference.
static int numOuterLoops;
static void *
threadFunc(void *arg)
{
int j;
for (j = 0; j < numOuterLoops; j++) {
__atomic_add_fetch (&glob, 1,__ATOMIC_ACQ_REL);
}
return NULL;
}
int
main(int argc, char *argv[])
{
int opt, s, j;
int numThreads;
pthread_t *thread;
int verbose;
int64_t n = 0;
if (argc < 2 ) {
printf(" Please provide 2 args Num of threads && Outer Loop\n");
exit (-1);
}
numThreads = atoi(argv[1]);
numOuterLoops = atoi (argv[2]);
if (1) {
printf("\tthreads: %d; outer loops: %d;\n",
numThreads, numOuterLoops);
}
thread = calloc(numThreads, sizeof(pthread_t));
if (thread == NULL) {
printf ("calloc error so exit\n");
exit (-1);
}
__atomic_store (&glob, &n, __ATOMIC_RELEASE);
for (j = 0; j < numThreads; j++) {
s = pthread_create(&thread[j], NULL, threadFunc, NULL);
if (s != 0) {
printf ("pthread_create failed so exit\n");
exit (-1);
}
}
for (j = 0; j < numThreads; j++) {
s = pthread_join(thread[j], NULL);
if (s != 0) {
printf ("pthread_join failed so exit\n");
exit (-1);
}
}
printf("glob value is %ld\n",__atomic_load_n (&glob,__ATOMIC_RELAXED));
exit(0);
}
time ./thr_count 800 800000
threads: 800; outer loops: 800000;
glob value is 640000000
real 1m10.288s
user 0m57.269s
sys 3m31.565s
time ./thr_count_atomic 800 800000
threads: 800; outer loops: 800000;
glob value is 640000000
real 0m20.313s
user 1m20.558s
sys 0m0.028
Change-Id: Ie5030a52ea264875e002e108dd4b207b15ab7cc7
Signed-off-by: Mohit Agrawal <moagrawa@redhat.com>
Diffstat (limited to 'xlators/protocol/client')
-rw-r--r-- | xlators/protocol/client/src/client-handshake.c | 16 | ||||
-rw-r--r-- | xlators/protocol/client/src/client.c | 2 |
2 files changed, 5 insertions, 13 deletions
diff --git a/xlators/protocol/client/src/client-handshake.c b/xlators/protocol/client/src/client-handshake.c index ca3d140d08e..6429dc2e8ae 100644 --- a/xlators/protocol/client/src/client-handshake.c +++ b/xlators/protocol/client/src/client-handshake.c @@ -32,7 +32,7 @@ int client_set_lk_version_cbk (struct rpc_req *req, struct iovec *iov, int client_set_lk_version (xlator_t *this); typedef struct client_fd_lk_local { - int ref; + gf_atomic_t ref; gf_boolean_t error; gf_lock_t lock; clnt_fd_ctx_t *fdctx; @@ -285,11 +285,7 @@ clnt_fd_lk_local_ref (xlator_t *this, clnt_fd_lk_local_t *local) { GF_VALIDATE_OR_GOTO (this->name, local, out); - LOCK (&local->lock); - { - local->ref++; - } - UNLOCK (&local->lock); + GF_ATOMIC_INC (local->ref); out: return local; } @@ -301,11 +297,7 @@ clnt_fd_lk_local_unref (xlator_t *this, clnt_fd_lk_local_t *local) GF_VALIDATE_OR_GOTO (this->name, local, out); - LOCK (&local->lock); - { - ref = --local->ref; - } - UNLOCK (&local->lock); + ref = GF_ATOMIC_DEC (local->ref); if (ref == 0) { LOCK_DESTROY (&local->lock); @@ -325,7 +317,7 @@ clnt_fd_lk_local_create (clnt_fd_ctx_t *fdctx) if (!local) goto out; - local->ref = 1; + GF_ATOMIC_INIT (local->ref, 1); local->error = _gf_false; local->fdctx = fdctx; diff --git a/xlators/protocol/client/src/client.c b/xlators/protocol/client/src/client.c index 8df697ed883..8ec9eaeb8a9 100644 --- a/xlators/protocol/client/src/client.c +++ b/xlators/protocol/client/src/client.c @@ -2844,7 +2844,7 @@ client_fd_lk_ctx_dump (xlator_t *this, fd_lk_ctx_t *lk_ctx, int nth_fd) fd_lk_ctx_node_t *plock = NULL; char key[GF_DUMP_MAX_BUF_LEN] = {0,}; - lk_ctx_ref = fd_lk_ctx_try_ref (lk_ctx); + lk_ctx_ref = fd_lk_ctx_ref (lk_ctx); if (!lk_ctx_ref) return; |