Hi

First patch from the mallocless branch.

- Lauri
>From 3c5783bd1201288d3360715533ce5ba7f2704745 Mon Sep 17 00:00:00 2001
From: Lauri Kasanen <[email protected]>
Date: Tue, 8 May 2012 20:02:07 +0300
Subject: [PATCH 1/2] First attempt at client session pool, two queues


Signed-off-by: Lauri Kasanen <[email protected]>
---
 src/include/mk_request.h   |    1 +
 src/include/mk_scheduler.h |    5 +++++
 src/mk_request.c           |   31 +++++++++++++++++++++++++++++--
 src/mk_scheduler.c         |    6 ++++++
 4 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/src/include/mk_request.h b/src/include/mk_request.h
index 77f535c..8e7f6ba 100644
--- a/src/include/mk_request.h
+++ b/src/include/mk_request.h
@@ -257,6 +257,7 @@ struct client_session
     struct session_request sr_fixed;
     struct mk_list request_list;
     struct mk_list _head;
+    struct mk_list pool_head;
 };
 
 pthread_key_t request_list;
diff --git a/src/include/mk_scheduler.h b/src/include/mk_scheduler.h
index de0bab4..a29bfae 100644
--- a/src/include/mk_scheduler.h
+++ b/src/include/mk_scheduler.h
@@ -52,12 +52,17 @@ struct sched_list_node
     struct mk_list busy_queue;
     struct mk_list av_queue;
 
+    struct mk_list sessions_busy;
+    struct mk_list sessions_free;
+
     short int idx;
     pthread_t tid;
     pid_t pid;
     int epoll_fd;
 
     struct client_session *request_handler;
+
+    struct client_session *session_pool;
 };
 
 struct sched_list_node *sched_list;
diff --git a/src/mk_request.c b/src/mk_request.c
index 2350b41..21cfa70 100644
--- a/src/mk_request.c
+++ b/src/mk_request.c
@@ -870,6 +870,33 @@ void mk_request_free_list(struct client_session *cs)
     }
 }
 
+/* Get a free client session from the pre-allocated pool */
+static struct client_session *mk_session_get_free(struct sched_list_node 
*sched)
+{
+
+    if (mk_list_is_empty(&sched->sessions_free) == 0)
+        return NULL;
+
+    struct mk_list *sessions_free = &sched->sessions_free;
+
+    struct client_session *cl = mk_list_entry_first(sessions_free,
+                                                    struct client_session,
+                                                    pool_head);
+
+    mk_list_del(&cl->pool_head);
+    mk_list_add(&cl->pool_head, &sched->sessions_busy);
+
+    return cl;
+}
+
+static void mk_session_mark_free(struct client_session *cl)
+{
+    struct sched_list_node *sched = mk_sched_get_thread_conf();
+
+    mk_list_del(&cl->pool_head);
+    mk_list_add(&cl->pool_head, &sched->sessions_free);
+}
+
 /* Create a client request struct and put it on the
  * main list
  */
@@ -886,7 +913,7 @@ struct client_session *mk_session_create(int socket, struct 
sched_list_node *sch
     }
 
     /* Alloc memory for node */
-    cs = mk_mem_malloc(sizeof(struct client_session));
+    cs = mk_session_get_free(sched);
 
     cs->pipelined = MK_FALSE;
     cs->counter_connections = 0;
@@ -956,7 +983,7 @@ void mk_session_remove(int socket)
             if (cs_node->body != cs_node->body_fixed) {
                 mk_mem_free(cs_node->body);
             }
-            mk_mem_free(cs_node);
+            mk_session_mark_free(cs_node);
             break;
         }
     }
diff --git a/src/mk_scheduler.c b/src/mk_scheduler.c
index 52d4777..a112dce 100644
--- a/src/mk_scheduler.c
+++ b/src/mk_scheduler.c
@@ -236,6 +236,11 @@ int mk_sched_register_thread(int efd)
     mk_list_init(&sl->busy_queue);
     mk_list_init(&sl->av_queue);
 
+    mk_list_init(&sl->sessions_busy);
+    mk_list_init(&sl->sessions_free);
+
+    sl->session_pool = mk_mem_malloc_z(sizeof(struct client_session) * 
config->worker_capacity);
+
     array = mk_mem_malloc_z(sizeof(struct sched_connection) * 
config->worker_capacity);
 
     for (i = 0; i < config->worker_capacity; i++) {
@@ -245,6 +250,7 @@ int mk_sched_register_thread(int efd)
         sched_conn->arrive_time = 0;
 
         mk_list_add(&sched_conn->_head, &sl->av_queue);
+        mk_list_add(&sl->session_pool[i].pool_head, &sl->sessions_free);
     }
     sl->request_handler = NULL;
 
-- 
1.7.2.1

_______________________________________________
Monkey mailing list
[email protected]
http://lists.monkey-project.com/listinfo/monkey

Reply via email to