[asterisk-commits] tilghman: branch tilghman/sched-fu r137499 - /team/tilghman/sched-fu/main/

SVN commits to the Asterisk project asterisk-commits at lists.digium.com
Wed Aug 13 15:19:10 CDT 2008


Author: tilghman
Date: Wed Aug 13 15:19:10 2008
New Revision: 137499

URL: http://svn.digium.com/view/asterisk?view=rev&rev=137499
Log:
Optimize searches

Modified:
    team/tilghman/sched-fu/main/sched.c

Modified: team/tilghman/sched-fu/main/sched.c
URL: http://svn.digium.com/view/asterisk/team/tilghman/sched-fu/main/sched.c?view=diff&rev=137499&r1=137498&r2=137499
==============================================================================
--- team/tilghman/sched-fu/main/sched.c (original)
+++ team/tilghman/sched-fu/main/sched.c Wed Aug 13 15:19:10 2008
@@ -171,6 +171,18 @@
 		ao2_ref(tmp, -1);
 }
 
+static int sched_wait_cb(void *obj, void *arg, int flags)
+{
+	int *result = arg;
+	struct sched *s = obj;
+
+	if ((*result = ast_tvdiff_ms(ast_tvnow(), s->when)) < 60000) {
+		return CMP_STOP;
+	}
+	*result = INT_MAX;
+	return 0;
+}
+
 /*! \brief
  * Return the number of milliseconds 
  * until the next scheduled event
@@ -179,46 +191,25 @@
 {
 	int ms = INT_MAX, i;
 	struct timeval now = ast_tvnow();
-	int slot = CALC_SLOT(now);
-	struct ao2_iterator aoi;
-	struct sched *s;
+	int start = 0, end = 600;
 
 	DEBUG(ast_debug(1, "ast_sched_wait()\n"));
 
-	if (slot == con->last_executed + 1) {
-		/* Haven't executed for 599 cycles? Search all. */
-		slot++;
-	}
-
-	if (con->last_executed == -1) {
-		/* first time through, search all */
-		for (i = 0; i < 600; i++) {
-			if (con->sched[i]) {
-				aoi = ao2_iterator_init(con->sched[i], 0);
-				while ((s = ao2_iterator_next(&aoi))) {
-					int tmp;
-					if ((tmp = ast_tvdiff_ms(s->when, now)) < ms && tmp < 60000) {
-						ao2_ref(s, -1);
-						ms = tmp;
-						break;
-					}
-					ao2_ref(s, -1);
-				}
-			}
-		}
-	} else {
-		for (i = con->last_executed; i != (slot == 599 ? 1 : slot + 1); i = (i == 599) ? 0 : i + 1) {
-			if (con->sched[i]) {
-				aoi = ao2_iterator_init(con->sched[i], 0);
-				while ((s = ao2_iterator_next(&aoi))) {
-					int tmp;
-					if ((tmp = ast_tvdiff_ms(s->when, now)) < ms && tmp < 60000) {
-						ao2_ref(s, -1);
-						ms = tmp;
-						break;
-					}
-					ao2_ref(s, -1);
-				}
+	if (con->last_executed > -1) {
+		start = (con->last_executed == 599) ? 0 : con->last_executed + 1;
+		end = CALC_SLOT(now);
+		if (start == end) {
+			/* Search all */
+			end = (end == 0) ? 599 : end - 1;
+		}
+	}
+
+	for (i = start; i != end; i = (i == 599 && end != 600) ? 0 : i + 1) {
+		if (con->sched[i]) {
+			int interval = 0;
+			ao2_callback(con->sched[i], 0, sched_wait_cb, &interval);
+			if (interval < ms) {
+				ms = interval;
 			}
 		}
 	}
@@ -439,13 +430,24 @@
 }
 #endif
 
-	
+static int sched_dump_cb(void *obj, void *arg, int flags)
+{
+	struct sched *q = obj;
+	struct timeval *when = arg;
+	struct timeval delta = ast_tvsub(q->when, *when);
+	ast_debug(1, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", 
+		q->id,
+		q->callback,
+		q->data,
+		(long)delta.tv_sec,
+		(long int)delta.tv_usec);
+	return 0;
+}
+
 /*! \brief Dump the contents of the scheduler to LOG_DEBUG */
 void ast_sched_dump(const struct sched_context *con)
 {
-	struct sched *q;
 	struct timeval when = ast_tvnow();
-	struct ao2_iterator aoi;
 	int i;
 #ifdef SCHED_MAX_CACHE
 	ast_debug(1, "Asterisk Schedule Dump (%d in Q, %d Total, %d Cache, %d high-water)\n", con->schedcnt, con->eventcnt - 1, con->schedccnt, con->highwater);
@@ -457,17 +459,7 @@
 	ast_debug(1, "|ID    Callback          Data              Time  (sec:ms)   |\n");
 	ast_debug(1, "+-----+-----------------+-----------------+-----------------+\n");
 	for (i = 0; i < 600; i++) {
-		aoi = ao2_iterator_init(con->sched[i], 0);
-		while ((q = ao2_iterator_next(&aoi))) {
-			struct timeval delta = ast_tvsub(q->when, when);
-
-			ast_debug(1, "|%.4d | %-15p | %-15p | %.6ld : %.6ld |\n", 
-				q->id,
-				q->callback,
-				q->data,
-				(long)delta.tv_sec,
-				(long int)delta.tv_usec);
-		}
+		ao2_callback(con->sched[i], 0, sched_dump_cb, &when);
 	}
 	ast_debug(1, "=============================================================\n");
 }
@@ -480,21 +472,16 @@
 	struct sched *current;
 	struct timeval now = ast_tvnow();
 	int numevents = 0;
-	int res, i, start = 0, end = 600, slot = CALC_SLOT(now);
+	int res, i, start = 0, end = 600;
 
 	DEBUG(ast_debug(1, "ast_sched_runq()\n"));
-		
-	if (slot == con->last_executed + 1) {
-		/* Haven't executed for 599 cycles? Search all. */
-		slot++;
-	}
 
 	if (con->last_executed > -1) {
 		start = con->last_executed + 1;
 		end = CALC_SLOT(now);
 	}
 
-	for (i = start; i != end; i = (i == 599) ? 0 : i + 1) {
+	for (i = start; i != end; i = (i == 599 && end != 600) ? 0 : i + 1) {
 		if (con->sched[i]) {
 			struct ao2_iterator aoi = ao2_iterator_init(con->sched[i], 0);
 			while ((current = ao2_iterator_next(&aoi))) {
@@ -544,6 +531,7 @@
 		if (CALC_SLOT(now) != end) {
 			end = CALC_SLOT(now);
 		}
+		usleep(1);
 	}
 
 	return numevents;




More information about the asterisk-commits mailing list