diff mbox

[v3,16/16] api: timer: Added timeout alloc and free

Message ID 1421853343-22138-17-git-send-email-petri.savolainen@linaro.org
State New
Headers show

Commit Message

Petri Savolainen Jan. 21, 2015, 3:15 p.m. UTC
* Timeout is an event that has a dedicated pool type. Added
timeout_alloc, _free and _to_event functions to avoid
timeout <-> buffer conversions (which must not be done
anymore).

* Added timeout pool parameters (num). Pool was modified minimally
(re-uses still buf.xxx params).

Signed-off-by: Petri Savolainen <petri.savolainen@linaro.org>
---
 example/timer/odp_timer_test.c                 | 26 +++++++++----------
 platform/linux-generic/include/api/odp_pool.h  |  5 +++-
 platform/linux-generic/include/api/odp_timer.h | 35 ++++++++++++++++++++++++++
 platform/linux-generic/odp_buffer_pool.c       | 10 ++++++--
 platform/linux-generic/odp_timer.c             | 19 ++++++++++++++
 test/validation/buffer/odp_buffer_pool_test.c  | 17 ++++++++++---
 test/validation/odp_timer.c                    | 20 ++++++---------
 7 files changed, 100 insertions(+), 32 deletions(-)
diff mbox

Patch

diff --git a/example/timer/odp_timer_test.c b/example/timer/odp_timer_test.c
index 808b582..fafa0a4 100644
--- a/example/timer/odp_timer_test.c
+++ b/example/timer/odp_timer_test.c
@@ -26,7 +26,7 @@ 
 
 
 #define MAX_WORKERS           32            /**< Max worker threads */
-#define MSG_NUM_BUFS          10000         /**< Number of timers */
+#define NUM_TMOS              10000         /**< Number of timers */
 
 
 /** Test arguments */
@@ -43,7 +43,7 @@  typedef struct {
 /** @private Barrier for test synchronisation */
 static odp_barrier_t test_barrier;
 
-/** @private Buffer pool handle */
+/** @private Pool handle */
 static odp_pool_t pool;
 
 /** @private Timer pool handle */
@@ -86,7 +86,7 @@  static void test_abs_timeouts(int thr, test_args_t *args)
 	odp_queue_t queue;
 	uint64_t tick;
 	struct test_timer *ttp;
-	odp_buffer_t buf;
+	odp_timeout_t tmo;
 
 	EXAMPLE_DBG("  [%i] test_timeouts\n", thr);
 
@@ -107,12 +107,12 @@  static void test_abs_timeouts(int thr, test_args_t *args)
 		EXAMPLE_ERR("Failed to allocate timer\n");
 		return;
 	}
-	buf = odp_buffer_alloc(pool);
-	if (buf == ODP_BUFFER_INVALID) {
-		EXAMPLE_ERR("Failed to allocate buffer\n");
+	tmo = odp_timeout_alloc(pool);
+	if (tmo == ODP_TIMEOUT_INVALID) {
+		EXAMPLE_ERR("Failed to allocate timeout\n");
 		return;
 	}
-	ttp->ev = odp_buffer_to_event(buf);
+	ttp->ev = odp_timeout_to_event(tmo);
 	tick = odp_timer_current_tick(tp);
 
 	while ((int)odp_atomic_load_u32(&remain) > 0) {
@@ -167,7 +167,7 @@  static void test_abs_timeouts(int thr, test_args_t *args)
 	/* Cancel and free last timer used */
 	(void)odp_timer_cancel(ttp->tim, &ttp->ev);
 	if (ttp->ev != ODP_EVENT_INVALID)
-		odp_buffer_free(odp_buffer_from_event(ttp->ev));
+		odp_timeout_free(odp_timeout_from_event(ttp->ev));
 	else
 		EXAMPLE_ERR("Lost timeout event at timer cancel\n");
 	/* Since we have cancelled the timer, there is no timeout event to
@@ -195,7 +195,7 @@  static void *run_thread(void *ptr)
 	printf("Thread %i starts on cpu %i\n", thr, odp_thread_cpu());
 
 	/*
-	 * Find the buffer pool
+	 * Find the pool
 	 */
 	msg_pool = odp_pool_lookup("msg_pool");
 
@@ -371,17 +371,15 @@  int main(int argc, char *argv[])
 	printf("timeouts:           %i\n", args.tmo_count);
 
 	/*
-	 * Create buffer pool for timeouts
+	 * Create pool for timeouts
 	 */
-	params.buf.size  = 0;
-	params.buf.align = 0;
-	params.buf.num   = MSG_NUM_BUFS;
+	params.tmo.num   = NUM_TMOS;
 	params.type      = ODP_POOL_TIMEOUT;
 
 	pool = odp_pool_create("msg_pool", ODP_SHM_NULL, &params);
 
 	if (pool == ODP_POOL_INVALID) {
-		EXAMPLE_ERR("Buffer pool create failed.\n");
+		EXAMPLE_ERR("Pool create failed.\n");
 		return -1;
 	}
 
diff --git a/platform/linux-generic/include/api/odp_pool.h b/platform/linux-generic/include/api/odp_pool.h
index d1623f1..aded64a 100644
--- a/platform/linux-generic/include/api/odp_pool.h
+++ b/platform/linux-generic/include/api/odp_pool.h
@@ -57,9 +57,12 @@  typedef struct odp_pool_param_t {
 			uint32_t seg_align;
 			uint32_t num;
 		} pkt;
+*/
 		struct {
+			uint32_t __res1; /* Keep struct identical to buf, */
+			uint32_t __res2; /* until pool implementation is fixed*/
+			uint32_t num;    /**< Number of timeouts in the pool */
 		} tmo;
-*/
 	};
 
 	int type;  /**< Pool type */
diff --git a/platform/linux-generic/include/api/odp_timer.h b/platform/linux-generic/include/api/odp_timer.h
index 075263a..ff61276 100644
--- a/platform/linux-generic/include/api/odp_timer.h
+++ b/platform/linux-generic/include/api/odp_timer.h
@@ -66,6 +66,11 @@  typedef void *odp_timeout_t;
 #define ODP_TIMER_INVALID ((uint32_t)~0U)
 
 /**
+ * Invalid timeout handle (platform dependent).
+ */
+#define ODP_TIMEOUT_INVALID NULL
+
+/**
  * Return values of timer set calls.
  */
 typedef enum {
@@ -310,6 +315,15 @@  int odp_timer_cancel(odp_timer_t tim, odp_event_t *tmo_ev);
 odp_timeout_t odp_timeout_from_event(odp_event_t ev);
 
 /**
+ * Convert timeout handle to event handle
+ *
+ * @param tmo Timeout handle
+ *
+ * @return Event handle
+ */
+odp_event_t odp_timeout_to_event(odp_timeout_t tmo);
+
+/**
  * Check for fresh timeout
  * If the corresponding timer has been reset or cancelled since this timeout
  * was enqueued, the timeout is stale (not fresh).
@@ -358,6 +372,27 @@  uint64_t odp_timeout_tick(odp_timeout_t tmo);
 void *odp_timeout_user_ptr(odp_timeout_t tmo);
 
 /**
+ * Timeout alloc
+ *
+ * Allocates timeout from pool. Pool must be created with ODP_POOL_TIMEOUT type.
+ *
+ * @param pool Pool handle
+ *
+ * @return Timeout handle
+ * @retval ODP_TIMEOUT_INVALID  Timeout could not be allocated
+ */
+odp_timeout_t odp_timeout_alloc(odp_pool_t);
+
+/**
+ * Timeout free
+ *
+ * Frees the timeout back to the pool it was allocated from.
+ *
+ * @param tmo Timeout handle
+ */
+void odp_timeout_free(odp_timeout_t tmo);
+
+/**
  * @}
  */
 
diff --git a/platform/linux-generic/odp_buffer_pool.c b/platform/linux-generic/odp_buffer_pool.c
index dcb1e5e..ac12057 100644
--- a/platform/linux-generic/odp_buffer_pool.c
+++ b/platform/linux-generic/odp_buffer_pool.c
@@ -100,13 +100,19 @@  int odp_buffer_pool_init_global(void)
  */
 
 odp_pool_t odp_pool_create(const char *name,
-					 odp_shm_t shm,
-					 odp_pool_param_t *params)
+			   odp_shm_t shm,
+			   odp_pool_param_t *params)
 {
 	odp_pool_t pool_hdl = ODP_POOL_INVALID;
 	pool_entry_t *pool;
 	uint32_t i, headroom = 0, tailroom = 0;
 
+	/* Default size and align for timeouts */
+	if (params->type == ODP_POOL_TIMEOUT) {
+		params->buf.size  = 0; /* tmo.__res1 */
+		params->buf.align = 0; /* tmo.__res2 */
+	}
+
 	/* Default initialization paramters */
 	static _odp_buffer_pool_init_t default_init_params = {
 		.udata_size = 0,
diff --git a/platform/linux-generic/odp_timer.c b/platform/linux-generic/odp_timer.c
index a09d189..11fc69e 100644
--- a/platform/linux-generic/odp_timer.c
+++ b/platform/linux-generic/odp_timer.c
@@ -804,6 +804,13 @@  odp_timeout_t odp_timeout_from_event(odp_event_t ev)
 	return (odp_timeout_t)timeout_hdr_from_buf(odp_buffer_from_event(ev));
 }
 
+odp_event_t odp_timeout_to_event(odp_timeout_t tmo)
+{
+	odp_timeout_hdr_t *tmo_hdr = (odp_timeout_hdr_t *)tmo;
+	odp_buffer_t buf = odp_hdr_to_buf(&tmo_hdr->buf_hdr);
+	return odp_buffer_to_event(buf);
+}
+
 int odp_timeout_fresh(odp_timeout_t tmo)
 {
 	const odp_timeout_hdr_t *hdr = (odp_timeout_hdr_t *)tmo;
@@ -835,6 +842,18 @@  void *odp_timeout_user_ptr(odp_timeout_t tmo)
 	return hdr->user_ptr;
 }
 
+odp_timeout_t odp_timeout_alloc(odp_pool_t pool)
+{
+	odp_buffer_t buf = odp_buffer_alloc(pool);
+	return odp_timeout_from_event(odp_buffer_to_event(buf));
+}
+
+void odp_timeout_free(odp_timeout_t tmo)
+{
+	odp_event_t ev = odp_timeout_to_event(tmo);
+	odp_buffer_free(odp_buffer_from_event(ev));
+}
+
 int odp_timer_init_global(void)
 {
 #ifndef ODP_ATOMIC_U128
diff --git a/test/validation/buffer/odp_buffer_pool_test.c b/test/validation/buffer/odp_buffer_pool_test.c
index 18a5756..c018b59 100644
--- a/test/validation/buffer/odp_buffer_pool_test.c
+++ b/test/validation/buffer/odp_buffer_pool_test.c
@@ -121,6 +121,7 @@  static void pool_alloc_type(int type)
 	const size_t size = 1500;
 	odp_buffer_t buffer[num];
 	odp_packet_t packet[num];
+	odp_timeout_t tmo[num];
 	odp_event_t ev;
 	int index;
 	char wrong_type = 0, wrong_size = 0;
@@ -132,7 +133,6 @@  static void pool_alloc_type(int type)
 	for (index = 0; index < num; index++) {
 		switch (type) {
 		case ODP_POOL_BUFFER:
-		case ODP_POOL_TIMEOUT:
 			buffer[index] = odp_buffer_alloc(pool);
 
 			if (buffer[index] == ODP_BUFFER_INVALID)
@@ -146,7 +146,6 @@  static void pool_alloc_type(int type)
 			if (wrong_type || wrong_size)
 				odp_buffer_print(buffer[index]);
 			break;
-
 		case ODP_POOL_PACKET:
 			packet[index] = odp_packet_alloc(pool, size);
 
@@ -157,7 +156,16 @@  static void pool_alloc_type(int type)
 			if (odp_event_type(ev) != ODP_EVENT_PACKET)
 				wrong_type = 1;
 			break;
+		case ODP_POOL_TIMEOUT:
+			tmo[index] = odp_timeout_alloc(pool);
 
+			if (tmo[index] == ODP_TIMEOUT_INVALID)
+				break;
+
+			ev = odp_timeout_to_event(tmo[index]);
+			if (odp_event_type(ev) != ODP_EVENT_TIMEOUT)
+				wrong_type = 1;
+			break;
 		default:
 			break;
 		}
@@ -175,7 +183,6 @@  static void pool_alloc_type(int type)
 
 	switch (type) {
 	case ODP_POOL_BUFFER:
-	case ODP_POOL_TIMEOUT:
 		for (; index >= 0; index--)
 			odp_buffer_free(buffer[index]);
 		break;
@@ -183,6 +190,10 @@  static void pool_alloc_type(int type)
 		for (; index >= 0; index--)
 			odp_packet_free(packet[index]);
 		break;
+	case ODP_POOL_TIMEOUT:
+		for (; index >= 0; index--)
+			odp_timeout_free(tmo[index]);
+		break;
 	default:
 		break;
 	}
diff --git a/test/validation/odp_timer.c b/test/validation/odp_timer.c
index 3783531..c0b6f67 100644
--- a/test/validation/odp_timer.c
+++ b/test/validation/odp_timer.c
@@ -22,7 +22,7 @@ 
 /** @private Barrier for thread synchronisation */
 static odp_barrier_t test_barrier;
 
-/** @private Timeout buffer pool handle used by all threads */
+/** @private Timeout pool handle used by all threads */
 static odp_pool_t tbp;
 
 /** @private Timer pool handle used by all threads */
@@ -123,11 +123,9 @@  static void *worker_entrypoint(void *arg)
 		tt[i].tim = odp_timer_alloc(tp, queue, &tt[i]);
 		if (tt[i].tim == ODP_TIMER_INVALID)
 			CU_FAIL_FATAL("Failed to allocate timer");
-		/* Timeout alloc is needed.
-		 * With this alloc call pool/event type should be buffer. */
-		tt[i].ev = odp_buffer_to_event(odp_buffer_alloc(tbp));
+		tt[i].ev = odp_timeout_to_event(odp_timeout_alloc(tbp));
 		if (tt[i].ev == ODP_EVENT_INVALID)
-			CU_FAIL_FATAL("Failed to allocate timeout buffer");
+			CU_FAIL_FATAL("Failed to allocate timeout");
 		tt[i].ev2 = tt[i].ev;
 		tt[i].tick = TICK_INVALID;
 	}
@@ -216,7 +214,7 @@  static void *worker_entrypoint(void *arg)
 		tt[i].tick = TICK_INVALID;
 		if (tt[i].ev == ODP_EVENT_INVALID)
 			/* Cancel too late, timer already expired and
-			 * timoeut buffer enqueued */
+			 * timeout enqueued */
 			nstale++;
 		if (odp_timer_free(tt[i].tim) != ODP_EVENT_INVALID)
 			CU_FAIL("odp_timer_free");
@@ -262,14 +260,12 @@  static void test_odp_timer_all(void)
 	 * @TODO move to test/performance */
 	int num_workers = min(odp_sys_cpu_count()-1, MAX_WORKERS);
 
-	/* Create timeout buffer pools */
-	params.buf.size  = 0;
-	params.buf.align = ODP_CACHE_LINE_SIZE;
-	params.buf.num   = (NTIMERS + 1) * num_workers;
-	params.type      = ODP_POOL_TIMEOUT;
+	/* Create timeout pools */
+	params.tmo.num = (NTIMERS + 1) * num_workers;
+	params.type    = ODP_POOL_TIMEOUT;
 	tbp = odp_pool_create("tmo_pool", ODP_SHM_INVALID, &params);
 	if (tbp == ODP_POOL_INVALID)
-		CU_FAIL_FATAL("Timeout buffer pool create failed");
+		CU_FAIL_FATAL("Timeout pool create failed");
 
 #define NAME "timer_pool"
 #define RES (10 * ODP_TIME_MSEC / 3)