21ab4e
From fe5617f5422ea052b17a4ab282f80ac6b61238df Mon Sep 17 00:00:00 2001
21ab4e
From: Niels de Vos <ndevos@redhat.com>
21ab4e
Date: Mon, 17 Apr 2017 15:50:07 +0530
21ab4e
Subject: [PATCH 434/473] core: make the per glusterfs_ctx_t timer-wheel
21ab4e
 refcounted
21ab4e
21ab4e
xlators can use a 'global' timer-wheel for scheduling events. This
21ab4e
timer-wheel is managed per glusterfs_ctx_t, but does not need to be
21ab4e
allocated for every graph. When an xlator wants to use the timer-wheel,
21ab4e
it will be instanciated on demand, and provided to xlators that request
21ab4e
it later on.
21ab4e
21ab4e
By adding a reference counter to the glusterfs_ctx_t for the
21ab4e
timer-wheel, the threads and structures can be cleaned up when the last
21ab4e
xlator does not have a need for it anymore. In general, the xlators
21ab4e
request the timer-wheel in init(), and they should return it in fini().
21ab4e
21ab4e
Because the timer-wheel is managed per glusterfs_ctx_t, the functions
21ab4e
can be added to ctx.c and do not need to live in their very minimal
21ab4e
tw.[ch] files.
21ab4e
21ab4e
>Reported-by: Poornima G <pgurusid@redhat.com>
21ab4e
>Signed-off-by: Niels de Vos <ndevos@redhat.com>
21ab4e
>Reviewed-on: https://review.gluster.org/17068
21ab4e
>NetBSD-regression: NetBSD Build System <jenkins@build.gluster.org>
21ab4e
>CentOS-regression: Gluster Build System <jenkins@build.gluster.org>
21ab4e
>Smoke: Gluster Build System <jenkins@build.gluster.org>
21ab4e
>Reviewed-by: Amar Tumballi <amarts@redhat.com>
21ab4e
>Reviewed-by: Zhou Zhengping <johnzzpcrystal@gmail.com>
21ab4e
>Reviewed-by: Kaleb KEITHLEY <kkeithle@redhat.com>
21ab4e
>(cherry picked from commit 73fcf3a874b2049da31d01b8363d1ac85c9488c2)
21ab4e
21ab4e
>Conflicts:
21ab4e
>        libglusterfs/src/Makefile.am
21ab4e
21ab4e
Change-Id: I19d225b39aaa272d9005ba7adc3104c3764f1572
21ab4e
BUG: 1450336
21ab4e
Signed-off-by: Poornima G <pgurusid@redhat.com>
21ab4e
Reviewed-on: https://code.engineering.redhat.com/gerrit/106133
21ab4e
Reviewed-by: Atin Mukherjee <amukherj@redhat.com>
21ab4e
---
21ab4e
 glusterfsd/src/glusterfsd.c                 |  6 ++---
21ab4e
 libglusterfs/src/Makefile.am                |  6 ++---
21ab4e
 libglusterfs/src/ctx.c                      | 39 ++++++++++++++++++++++++++++-
21ab4e
 libglusterfs/src/globals.h                  |  3 +++
21ab4e
 libglusterfs/src/glusterfs.h                | 18 ++++++++-----
21ab4e
 libglusterfs/src/mem-types.h                |  1 +
21ab4e
 libglusterfs/src/tw.c                       | 25 ------------------
21ab4e
 libglusterfs/src/tw.h                       | 23 -----------------
21ab4e
 xlators/features/bit-rot/src/bitd/bit-rot.c |  6 ++---
21ab4e
 xlators/features/leases/src/leases.c        | 18 +++++--------
21ab4e
 xlators/features/leases/src/leases.h        |  1 -
21ab4e
 xlators/performance/nl-cache/src/nl-cache.c | 18 ++++++-------
21ab4e
 12 files changed, 76 insertions(+), 88 deletions(-)
21ab4e
 delete mode 100644 libglusterfs/src/tw.c
21ab4e
 delete mode 100644 libglusterfs/src/tw.h
21ab4e
21ab4e
diff --git a/glusterfsd/src/glusterfsd.c b/glusterfsd/src/glusterfsd.c
21ab4e
index 2724ed7..110cbb3 100644
21ab4e
--- a/glusterfsd/src/glusterfsd.c
21ab4e
+++ b/glusterfsd/src/glusterfsd.c
21ab4e
@@ -75,7 +75,6 @@
21ab4e
 #include "exports.h"
21ab4e
 
21ab4e
 #include "daemon.h"
21ab4e
-#include "tw.h"
21ab4e
 
21ab4e
 
21ab4e
 /* using argp for command line parsing */
21ab4e
@@ -2467,9 +2466,10 @@ main (int argc, char *argv[])
21ab4e
 
21ab4e
         /* do this _after_ daemonize() */
21ab4e
         if (cmd->global_timer_wheel) {
21ab4e
-                ret = glusterfs_global_timer_wheel_init (ctx);
21ab4e
-                if (ret)
21ab4e
+                if (!glusterfs_ctx_tw_get (ctx)) {
21ab4e
+                        ret = -1;
21ab4e
                         goto out;
21ab4e
+                }
21ab4e
         }
21ab4e
 
21ab4e
         ret = glusterfs_volumes_init (ctx);
21ab4e
diff --git a/libglusterfs/src/Makefile.am b/libglusterfs/src/Makefile.am
21ab4e
index 01cb314..6abced9 100644
21ab4e
--- a/libglusterfs/src/Makefile.am
21ab4e
+++ b/libglusterfs/src/Makefile.am
21ab4e
@@ -32,8 +32,8 @@ libglusterfs_la_SOURCES = dict.c xlator.c logging.c \
21ab4e
 	strfd.c parse-utils.c $(CONTRIBDIR)/mount/mntent.c \
21ab4e
 	$(CONTRIBDIR)/libexecinfo/execinfo.c quota-common-utils.c rot-buffs.c \
21ab4e
 	$(CONTRIBDIR)/timer-wheel/timer-wheel.c \
21ab4e
-	$(CONTRIBDIR)/timer-wheel/find_last_bit.c tw.c default-args.c locking.c \
21ab4e
-	compound-fop-utils.c
21ab4e
+	$(CONTRIBDIR)/timer-wheel/find_last_bit.c default-args.c locking.c \
21ab4e
+	compound-fop-utils.c throttle-tbf.c
21ab4e
 
21ab4e
 nodist_libglusterfs_la_SOURCES = y.tab.c graph.lex.c defaults.c
21ab4e
 nodist_libglusterfs_la_HEADERS = y.tab.h glusterfs-fops.h cli1-xdr.h
21ab4e
@@ -50,7 +50,7 @@ libglusterfs_la_HEADERS = common-utils.h defaults.h default-args.h \
21ab4e
 	refcount.h run.h options.h lkowner.h fd-lk.h circ-buff.h \
21ab4e
 	event-history.h gidcache.h client_t.h glusterfs-acl.h \
21ab4e
 	glfs-message-id.h template-component-messages.h strfd.h \
21ab4e
-	syncop-utils.h parse-utils.h libglusterfs-messages.h tw.h \
21ab4e
+	syncop-utils.h parse-utils.h libglusterfs-messages.h \
21ab4e
 	lvm-defaults.h quota-common-utils.h rot-buffs.h \
21ab4e
 	compat-uuid.h upcall-utils.h events.h compound-fop-utils.h \
21ab4e
         atomic.h
21ab4e
diff --git a/libglusterfs/src/ctx.c b/libglusterfs/src/ctx.c
21ab4e
index 2aa1465..1f2f1a1 100644
21ab4e
--- a/libglusterfs/src/ctx.c
21ab4e
+++ b/libglusterfs/src/ctx.c
21ab4e
@@ -9,9 +9,10 @@
21ab4e
 */
21ab4e
 
21ab4e
 #include <pthread.h>
21ab4e
-#include "globals.h"
21ab4e
 
21ab4e
+#include "globals.h"
21ab4e
 #include "glusterfs.h"
21ab4e
+#include "timer-wheel.h"
21ab4e
 
21ab4e
 glusterfs_ctx_t *
21ab4e
 glusterfs_ctx_new ()
21ab4e
@@ -46,3 +47,39 @@ out:
21ab4e
 	return ctx;
21ab4e
 }
21ab4e
 
21ab4e
+static void
21ab4e
+glusterfs_ctx_tw_destroy (struct gf_ctx_tw *ctx_tw)
21ab4e
+{
21ab4e
+        if (ctx_tw->timer_wheel)
21ab4e
+                gf_tw_cleanup_timers (ctx_tw->timer_wheel);
21ab4e
+
21ab4e
+        GF_FREE (ctx_tw);
21ab4e
+}
21ab4e
+
21ab4e
+struct tvec_base*
21ab4e
+glusterfs_ctx_tw_get (glusterfs_ctx_t *ctx)
21ab4e
+{
21ab4e
+        struct gf_ctx_tw *ctx_tw = NULL;
21ab4e
+
21ab4e
+        LOCK (&ctx->lock);
21ab4e
+        {
21ab4e
+                if (ctx->tw) {
21ab4e
+                        ctx_tw = GF_REF_GET (ctx->tw);
21ab4e
+                } else {
21ab4e
+                        ctx_tw = GF_CALLOC (1, sizeof (struct gf_ctx_tw),
21ab4e
+                                            gf_common_mt_tw_ctx);
21ab4e
+                        ctx_tw->timer_wheel = gf_tw_init_timers();
21ab4e
+                        GF_REF_INIT (ctx_tw, glusterfs_ctx_tw_destroy);
21ab4e
+                        ctx->tw = ctx_tw;
21ab4e
+                }
21ab4e
+        }
21ab4e
+        UNLOCK (&ctx->lock);
21ab4e
+
21ab4e
+        return ctx_tw->timer_wheel;
21ab4e
+}
21ab4e
+
21ab4e
+void
21ab4e
+glusterfs_ctx_tw_put (glusterfs_ctx_t *ctx)
21ab4e
+{
21ab4e
+        GF_REF_PUT (ctx->tw);
21ab4e
+}
21ab4e
diff --git a/libglusterfs/src/globals.h b/libglusterfs/src/globals.h
21ab4e
index 69c4c6d..08031a7 100644
21ab4e
--- a/libglusterfs/src/globals.h
21ab4e
+++ b/libglusterfs/src/globals.h
21ab4e
@@ -119,6 +119,9 @@ char *glusterfs_leaseid_buf_get ();
21ab4e
 /* init */
21ab4e
 int glusterfs_globals_init (glusterfs_ctx_t *ctx);
21ab4e
 
21ab4e
+struct tvec_base* glusterfs_ctx_tw_get (glusterfs_ctx_t *ctx);
21ab4e
+void glusterfs_ctx_tw_put (glusterfs_ctx_t *ctx);
21ab4e
+
21ab4e
 extern const char *gf_fop_list[];
21ab4e
 extern const char *gf_upcall_list[];
21ab4e
 
21ab4e
diff --git a/libglusterfs/src/glusterfs.h b/libglusterfs/src/glusterfs.h
21ab4e
index f77f5f8..4e686c3 100644
21ab4e
--- a/libglusterfs/src/glusterfs.h
21ab4e
+++ b/libglusterfs/src/glusterfs.h
21ab4e
@@ -36,6 +36,7 @@
21ab4e
 #include "logging.h"
21ab4e
 #include "lkowner.h"
21ab4e
 #include "compat-uuid.h"
21ab4e
+#include "refcount.h"
21ab4e
 
21ab4e
 #define GF_YES 1
21ab4e
 #define GF_NO  0
21ab4e
@@ -427,6 +428,12 @@ typedef enum {
21ab4e
 
21ab4e
 struct tvec_base;
21ab4e
 
21ab4e
+/* reference counting for the global (per ctx) timer-wheel */
21ab4e
+struct gf_ctx_tw {
21ab4e
+        GF_REF_DECL;
21ab4e
+        struct tvec_base *timer_wheel; /* global timer-wheel instance */
21ab4e
+};
21ab4e
+
21ab4e
 struct _glusterfs_ctx {
21ab4e
         cmd_args_t          cmd_args;
21ab4e
         char               *process_uuid;
21ab4e
@@ -495,14 +502,13 @@ struct _glusterfs_ctx {
21ab4e
          */
21ab4e
         mgmt_ssl_t          secure_srvr;
21ab4e
         /* Buffer to 'save' backtrace even under OOM-kill like situations*/
21ab4e
-        char btbuf[GF_BACKTRACE_LEN];
21ab4e
+        char                btbuf[GF_BACKTRACE_LEN];
21ab4e
 
21ab4e
-        pthread_mutex_t notify_lock;
21ab4e
-        pthread_cond_t notify_cond;
21ab4e
-        int notifying;
21ab4e
-
21ab4e
-        struct tvec_base *timer_wheel; /* global timer-wheel instance */
21ab4e
+        pthread_mutex_t     notify_lock;
21ab4e
+        pthread_cond_t      notify_cond;
21ab4e
+        int                 notifying;
21ab4e
 
21ab4e
+        struct gf_ctx_tw   *tw; /* refcounted timer_wheel */
21ab4e
 };
21ab4e
 typedef struct _glusterfs_ctx glusterfs_ctx_t;
21ab4e
 
21ab4e
diff --git a/libglusterfs/src/mem-types.h b/libglusterfs/src/mem-types.h
21ab4e
index afa52d8..ef7dcd0 100644
21ab4e
--- a/libglusterfs/src/mem-types.h
21ab4e
+++ b/libglusterfs/src/mem-types.h
21ab4e
@@ -164,6 +164,7 @@ enum gf_common_mem_types_ {
21ab4e
         /*used for compound fops*/
21ab4e
         gf_mt_compound_req_t,
21ab4e
         gf_mt_compound_rsp_t,
21ab4e
+        gf_common_mt_tw_ctx,
21ab4e
         gf_common_mt_tw_timer_list,
21ab4e
         /*lock migration*/
21ab4e
         gf_common_mt_lock_mig,
21ab4e
diff --git a/libglusterfs/src/tw.c b/libglusterfs/src/tw.c
21ab4e
deleted file mode 100644
21ab4e
index fa11998..0000000
21ab4e
--- a/libglusterfs/src/tw.c
21ab4e
+++ /dev/null
21ab4e
@@ -1,25 +0,0 @@
21ab4e
-/*
21ab4e
-  Copyright (c) 2008-2015 Red Hat, Inc. <http://www.redhat.com>
21ab4e
-  This file is part of GlusterFS.
21ab4e
-
21ab4e
-  This file is licensed to you under your choice of the GNU Lesser
21ab4e
-  General Public License, version 3 or any later version (LGPLv3 or
21ab4e
-  later), or the GNU General Public License, version 2 (GPLv2), in all
21ab4e
-  cases as published by the Free Software Foundation.
21ab4e
-*/
21ab4e
-
21ab4e
-#include "tw.h"
21ab4e
-#include "timer-wheel.h"
21ab4e
-
21ab4e
-int
21ab4e
-glusterfs_global_timer_wheel_init (glusterfs_ctx_t *ctx)
21ab4e
-{
21ab4e
-        ctx->timer_wheel = gf_tw_init_timers();
21ab4e
-        return ctx->timer_wheel ? 0 : -1;
21ab4e
-}
21ab4e
-
21ab4e
-struct tvec_base *
21ab4e
-glusterfs_global_timer_wheel (xlator_t *this)
21ab4e
-{
21ab4e
-        return this->ctx->timer_wheel;
21ab4e
-}
21ab4e
diff --git a/libglusterfs/src/tw.h b/libglusterfs/src/tw.h
21ab4e
deleted file mode 100644
21ab4e
index e635cd2..0000000
21ab4e
--- a/libglusterfs/src/tw.h
21ab4e
+++ /dev/null
21ab4e
@@ -1,23 +0,0 @@
21ab4e
-/*
21ab4e
-  Copyright (c) 2008-2015 Red Hat, Inc. <http://www.redhat.com>
21ab4e
-  This file is part of GlusterFS.
21ab4e
-
21ab4e
-  This file is licensed to you under your choice of the GNU Lesser
21ab4e
-  General Public License, version 3 or any later version (LGPLv3 or
21ab4e
-  later), or the GNU General Public License, version 2 (GPLv2), in all
21ab4e
-  cases as published by the Free Software Foundation.
21ab4e
-*/
21ab4e
-
21ab4e
-#ifndef __TW_H__
21ab4e
-#define __TW_H__
21ab4e
-
21ab4e
-#include "xlator.h"
21ab4e
-#include "glusterfs.h"
21ab4e
-
21ab4e
-int
21ab4e
-glusterfs_global_timer_wheel_init (glusterfs_ctx_t *);
21ab4e
-
21ab4e
-struct tvec_base *
21ab4e
-glusterfs_global_timer_wheel (xlator_t *);
21ab4e
-
21ab4e
-#endif /* __TW_H__ */
21ab4e
diff --git a/xlators/features/bit-rot/src/bitd/bit-rot.c b/xlators/features/bit-rot/src/bitd/bit-rot.c
21ab4e
index ab32925..6d60ca4 100644
21ab4e
--- a/xlators/features/bit-rot/src/bitd/bit-rot.c
21ab4e
+++ b/xlators/features/bit-rot/src/bitd/bit-rot.c
21ab4e
@@ -21,8 +21,6 @@
21ab4e
 #include <pthread.h>
21ab4e
 #include "bit-rot-bitd-messages.h"
21ab4e
 
21ab4e
-#include "tw.h"
21ab4e
-
21ab4e
 #define BR_HASH_CALC_READ_SIZE  (128 * 1024)
21ab4e
 
21ab4e
 typedef int32_t (br_child_handler)(xlator_t *, br_child_t *);
21ab4e
@@ -1976,7 +1974,7 @@ init (xlator_t *this)
21ab4e
         INIT_LIST_HEAD (&priv->bricks);
21ab4e
         INIT_LIST_HEAD (&priv->signing);
21ab4e
 
21ab4e
-        priv->timer_wheel = glusterfs_global_timer_wheel (this);
21ab4e
+        priv->timer_wheel = glusterfs_ctx_tw_get (this->ctx);
21ab4e
         if (!priv->timer_wheel) {
21ab4e
                 gf_msg (this->name, GF_LOG_ERROR, 0,
21ab4e
                         BRB_MSG_TIMER_WHEEL_UNAVAILABLE,
21ab4e
@@ -2044,6 +2042,8 @@ fini (xlator_t *this)
21ab4e
         this->private = NULL;
21ab4e
 	GF_FREE (priv);
21ab4e
 
21ab4e
+        glusterfs_ctx_tw_put (this->ctx);
21ab4e
+
21ab4e
 	return;
21ab4e
 }
21ab4e
 
21ab4e
diff --git a/xlators/features/leases/src/leases.c b/xlators/features/leases/src/leases.c
21ab4e
index 3e04600..faffa0e 100644
21ab4e
--- a/xlators/features/leases/src/leases.c
21ab4e
+++ b/xlators/features/leases/src/leases.c
21ab4e
@@ -957,19 +957,11 @@ leases_init_priv (xlator_t *this)
21ab4e
         GF_ASSERT (priv);
21ab4e
 
21ab4e
         if (!priv->timer_wheel) {
21ab4e
-                if (!glusterfs_global_timer_wheel (this)) {
21ab4e
-                        gf_msg_debug (this->name, 0, "Initing the global "
21ab4e
-                                      "timer wheel");
21ab4e
-                        ret = glusterfs_global_timer_wheel_init (this->ctx);
21ab4e
-                        if (ret) {
21ab4e
-                                gf_msg (this->name, GF_LOG_ERROR, 0,
21ab4e
-                                        LEASE_MSG_NO_TIMER_WHEEL,
21ab4e
-                                        "Initing the global timer "
21ab4e
-                                        "wheel failed");
21ab4e
-                                goto out;
21ab4e
-                        }
21ab4e
+                priv->timer_wheel = glusterfs_ctx_tw_get (this->ctx);
21ab4e
+                if (!priv->timer_wheel) {
21ab4e
+                        ret = -1;
21ab4e
+                        goto out;
21ab4e
                 }
21ab4e
-                priv->timer_wheel = glusterfs_global_timer_wheel (this);
21ab4e
         }
21ab4e
 
21ab4e
         if (!priv->inited_recall_thr) {
21ab4e
@@ -1076,6 +1068,8 @@ fini (xlator_t *this)
21ab4e
 
21ab4e
         GF_FREE (priv);
21ab4e
 
21ab4e
+        glusterfs_ctx_tw_put (this->ctx);
21ab4e
+
21ab4e
         return 0;
21ab4e
 }
21ab4e
 
21ab4e
diff --git a/xlators/features/leases/src/leases.h b/xlators/features/leases/src/leases.h
21ab4e
index df5e8be..a6cc34f 100644
21ab4e
--- a/xlators/features/leases/src/leases.h
21ab4e
+++ b/xlators/features/leases/src/leases.h
21ab4e
@@ -26,7 +26,6 @@
21ab4e
 #include "lkowner.h"
21ab4e
 #include "locking.h"
21ab4e
 #include "upcall-utils.h"
21ab4e
-#include "tw.h"
21ab4e
 #include "timer-wheel.h"
21ab4e
 #include "leases-mem-types.h"
21ab4e
 #include "leases-messages.h"
21ab4e
diff --git a/xlators/performance/nl-cache/src/nl-cache.c b/xlators/performance/nl-cache/src/nl-cache.c
21ab4e
index a34b752..92dbccb 100644
21ab4e
--- a/xlators/performance/nl-cache/src/nl-cache.c
21ab4e
+++ b/xlators/performance/nl-cache/src/nl-cache.c
21ab4e
@@ -12,7 +12,6 @@
21ab4e
 #include "nl-cache.h"
21ab4e
 #include "statedump.h"
21ab4e
 #include "upcall-utils.h"
21ab4e
-#include "tw.h"
21ab4e
 
21ab4e
 static void
21ab4e
 nlc_dentry_op (call_frame_t *frame, xlator_t *this, gf_boolean_t multilink)
21ab4e
@@ -625,6 +624,8 @@ nlc_priv_dump (xlator_t *this)
21ab4e
 void
21ab4e
 fini (xlator_t *this)
21ab4e
 {
21ab4e
+        glusterfs_ctx_tw_put (this->ctx);
21ab4e
+
21ab4e
         return;
21ab4e
 }
21ab4e
 
21ab4e
@@ -700,17 +701,12 @@ init (xlator_t *this)
21ab4e
         INIT_LIST_HEAD (&conf->lru);
21ab4e
         time (&conf->last_child_down);
21ab4e
 
21ab4e
-        if (!glusterfs_global_timer_wheel (this)) {
21ab4e
-                gf_msg_debug (this->name, 0, "Initing the global timer wheel");
21ab4e
-                ret = glusterfs_global_timer_wheel_init (this->ctx);
21ab4e
-                if (ret) {
21ab4e
-                        gf_msg (this->name, GF_LOG_ERROR, 0,
21ab4e
-                                NLC_MSG_NO_TIMER_WHEEL,
21ab4e
-                                "Initing the global timer wheel failed");
21ab4e
-                                goto out;
21ab4e
-                }
21ab4e
+        conf->timer_wheel = glusterfs_ctx_tw_get (this->ctx);
21ab4e
+        if (!conf->timer_wheel) {
21ab4e
+                gf_msg (this->name, GF_LOG_ERROR, 0, NLC_MSG_NO_TIMER_WHEEL,
21ab4e
+                        "Initing the global timer wheel failed");
21ab4e
+                        goto out;
21ab4e
         }
21ab4e
-        conf->timer_wheel = glusterfs_global_timer_wheel (this);
21ab4e
 
21ab4e
         this->private = conf;
21ab4e
 
21ab4e
-- 
21ab4e
1.8.3.1
21ab4e