]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
[MINOR] task: keep a task count and clean up task creators
authorWilly Tarreau <w@1wt.eu>
Sat, 21 Mar 2009 17:13:21 +0000 (18:13 +0100)
committerWilly Tarreau <w@1wt.eu>
Sat, 21 Mar 2009 17:13:21 +0000 (18:13 +0100)
It's sometimes useful at least for statistics to keep a task count.
It's easy to do by forcing the rare task creators to always use the
same functions to create/destroy a task.

include/proto/task.h
src/appsession.c
src/checks.c
src/client.c
src/proto_uxst.c
src/task.c

index 86a3c3d8151d712912a93f45f4f1070b93a7ef2d..54418b64edd80025eb7236b86613ff7abda065ae 100644 (file)
@@ -78,6 +78,7 @@
 #define TIMER_LOOK_BACK       (1U << 31)
 
 /* a few exported variables */
+extern unsigned int nb_tasks;     /* total number of tasks */
 extern unsigned int run_queue;    /* run queue size */
 extern unsigned int niced_tasks;  /* number of niced tasks in the run queue */
 extern struct pool_head *pool2_task;
@@ -160,8 +161,9 @@ static inline struct task *task_delete(struct task *t)
 }
 
 /*
- * Initialize a new task. The bare minimum is performed (queue pointers and state).
- * The task is returned.
+ * Initialize a new task. The bare minimum is performed (queue pointers and
+ * state).  The task is returned. This function should not be used outside of
+ * task_new().
  */
 static inline struct task *task_init(struct task *t)
 {
@@ -173,11 +175,28 @@ static inline struct task *task_init(struct task *t)
 }
 
 /*
- * frees a task. Its context must have been freed since it will be lost.
+ * Allocate and initialise a new task. The new task is returned, or NULL in
+ * case of lack of memory. The task count is incremented. Tasks should only
+ * be allocated this way, and must be freed using task_free().
+ */
+static inline struct task *task_new(void)
+{
+       struct task *t = pool_alloc2(pool2_task);
+       if (t) {
+               nb_tasks++;
+               task_init(t);
+       }
+       return t;
+}
+
+/*
+ * Free a task. Its context must have been freed since it will be lost.
+ * The task count is decremented.
  */
 static inline void task_free(struct task *t)
 {
        pool_free2(pool2_task, t);
+       nb_tasks--;
 }
 
 /* Place <task> into the wait queue, where it may already be. If the expiration
index a1c1c886aa449bebbaf056ddae0219aec2308c17..754e261601f865075260b5cc781eacfa6e649923 100644 (file)
@@ -87,10 +87,9 @@ int appsession_task_init(void)
 {
        static int initialized = 0;
        if (!initialized) {
-               if ((appsess_refresh = pool_alloc2(pool2_task)) == NULL)
+               if ((appsess_refresh = task_new()) == NULL)
                        return -1;
 
-               task_init(appsess_refresh);
                appsess_refresh->context = NULL;
                appsess_refresh->expire = tick_add(now_ms, MS_TO_TICKS(TBLCHKINT));
                appsess_refresh->process = appsession_refresh;
index f1e8120a79566faeaf4c1a94ec5084f03f1b730e..50b464f6cd1de5488f7b7f8f31cf0d21c8ab04a3 100644 (file)
@@ -837,14 +837,12 @@ int start_checks() {
                        if (!(s->state & SRV_CHECKED))
                                continue;
 
-                       if ((t = pool_alloc2(pool2_task)) == NULL) {
+                       if ((t = task_new()) == NULL) {
                                Alert("Starting [%s:%s] check: out of memory.\n", px->id, s->id);
                                return -1;
                        }
 
                        s->check = t;
-
-                       task_init(t);
                        t->process = process_chk;
                        t->context = s;
 
index f5ce3c61d0c71aa9b7f8e48de321d3118c1fa1d7..765a54b374d347843558d7058cb5f545ab7ca0e5 100644 (file)
@@ -134,7 +134,7 @@ int event_accept(int fd) {
                        s->flags |= SN_MONITOR;
                }
 
-               if ((t = pool_alloc2(pool2_task)) == NULL) { /* disable this proxy for a while */
+               if ((t = task_new()) == NULL) { /* disable this proxy for a while */
                        Alert("out of memory in event_accept().\n");
                        EV_FD_CLR(fd, DIR_RD);
                        p->state = PR_STIDLE;
@@ -160,7 +160,6 @@ int event_accept(int fd) {
                if (p->options & PR_O_TCP_NOLING)
                        setsockopt(cfd, SOL_SOCKET, SO_LINGER, (struct linger *) &nolinger, sizeof(struct linger));
 
-               task_init(t);
                t->process = l->handler;
                t->context = s;
 
@@ -473,7 +472,7 @@ int event_accept(int fd) {
        pool_free2(p->req_cap_pool, txn->req.cap);
  out_fail_reqcap:
  out_free_task:
-       pool_free2(pool2_task, t);
+       task_free(t);
  out_free_session:
        LIST_DEL(&s->list);
        pool_free2(pool2_session, s);
index 1b38774105f447087e65cee998e6c9b0b53ef728..baf91ef1e67264bf01d04b5e29ebb56c6a9c9a67 100644 (file)
@@ -414,7 +414,7 @@ int uxst_event_accept(int fd) {
                s->flags = 0;
                s->term_trace = 0;
 
-               if ((t = pool_alloc2(pool2_task)) == NULL) {
+               if ((t = task_new()) == NULL) {
                        Alert("out of memory in uxst_event_accept().\n");
                        goto out_free_session;
                }
@@ -432,7 +432,6 @@ int uxst_event_accept(int fd) {
                        goto out_free_task;
                }
 
-               task_init(t);
                t->process = l->handler;
                t->context = s;
                t->nice = -64;  /* we want to boost priority for local stats */
@@ -550,7 +549,7 @@ int uxst_event_accept(int fd) {
  out_free_req:
        pool_free2(pool2_buffer, s->req);
  out_free_task:
-       pool_free2(pool2_task, t);
+       task_free(t);
  out_free_session:
        LIST_DEL(&s->list);
        pool_free2(pool2_session, s);
index 72e3a93e5c8171e21e9d23fc8a2e9e7b62ef0672..8374c127490235578a16f643561638760342bbf8 100644 (file)
@@ -25,6 +25,7 @@
 
 struct pool_head *pool2_task;
 
+unsigned int nb_tasks = 0;
 unsigned int run_queue = 0;
 unsigned int niced_tasks = 0;      /* number of niced tasks in the run queue */
 struct eb32_node *last_timer = NULL;  /* optimization: last queued timer */