[asterisk-commits] rizzo: branch rizzo/astobj2 r47403 - /team/rizzo/astobj2/main/

asterisk-commits at lists.digium.com asterisk-commits at lists.digium.com
Thu Nov 9 19:48:42 MST 2006


Author: rizzo
Date: Thu Nov  9 20:48:42 2006
New Revision: 47403

URL: http://svn.digium.com/view/asterisk?view=rev&rev=47403
Log:
improve stats on astobj2 so we have a better idea of what goes on:

*CLI> astobj2 stats
Objects    : 103
Containers : 1
Memory     : 558964
Locked     : 0
Refs       : 173


Modified:
    team/rizzo/astobj2/main/asterisk.c
    team/rizzo/astobj2/main/astobj2.c

Modified: team/rizzo/astobj2/main/asterisk.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/main/asterisk.c?view=diff&rev=47403&r1=47402&r2=47403
==============================================================================
--- team/rizzo/astobj2/main/asterisk.c (original)
+++ team/rizzo/astobj2/main/asterisk.c Thu Nov  9 20:48:42 2006
@@ -1473,7 +1473,6 @@
 
 #define ASTERISK_PROMPT2 "%s*CLI> "
 
-int handle_astobj2_test(int fd, int argc, char *argv[]);
 static struct ast_cli_entry cli_asterisk[] = {
 	{ { "abort", "halt", NULL },
 	handle_abort_halt, "Cancel a running halt",
@@ -1533,10 +1532,6 @@
 
 	{ { "core", "clear", "profile", NULL },
 	handle_show_profile, "Clear profiling info",
-	NULL },
-
-	{ { "astobj2", "test", NULL },
-	handle_astobj2_test, "Test astobj2",
 	NULL },
 #endif /* ! LOW_MEMORY */
 };
@@ -2659,6 +2654,10 @@
 	srand((unsigned int) getpid() + (unsigned int) time(NULL));
 	initstate((unsigned int) getpid() * 65536 + (unsigned int) time(NULL), randompool, sizeof(randompool));
 
+	{ int astobj2_init(void);
+	astobj2_init();
+	}
+
 	if (init_logger()) {		/* Start logging subsystem */
 		printf(term_quit());
 		exit(1);

Modified: team/rizzo/astobj2/main/astobj2.c
URL: http://svn.digium.com/view/asterisk/team/rizzo/astobj2/main/astobj2.c?view=diff&rev=47403&r1=47402&r2=47403
==============================================================================
--- team/rizzo/astobj2/main/astobj2.c (original)
+++ team/rizzo/astobj2/main/astobj2.c Thu Nov  9 20:48:42 2006
@@ -41,6 +41,7 @@
         ast_mutex_t     lock;
         int             ref_counter;
         ao2_destructor_fn destructor_fn;
+	int data_size;	/* for stats */
 	uint32_t magic;	/* magic number */
 };
 
@@ -55,7 +56,15 @@
         void *user_data[0];
 };
 
-volatile int ao2_total_objects;
+struct ao2_stats {
+	volatile int total_objects;
+	volatile int total_mem;
+	volatile int total_containers;
+	volatile int total_refs;
+	volatile int total_locked;
+};
+
+static struct ao2_stats ao2;
 
 #ifndef HAVE_BKTR	/* backtrace support */
 static void bt(void) {}
@@ -108,6 +117,7 @@
 	struct astobj2 *p = INTERNAL_OBJ(user_data);
 	if (p == NULL)
 		return -1;
+	ast_atomic_fetchadd_int(&ao2.total_locked, 1);
 	return ast_mutex_lock( &p->priv_data.lock );
 }
 
@@ -116,6 +126,7 @@
 	struct astobj2 *p = INTERNAL_OBJ(user_data);
 	if (p == NULL)
 		return -1;
+	ast_atomic_fetchadd_int(&ao2.total_locked, -1);
 	return ast_mutex_unlock( &p->priv_data.lock );
 }
 
@@ -140,6 +151,7 @@
 
 	/* we modify with an atomic operation the reference counter */
 	ret = ast_atomic_fetchadd_int( &obj->priv_data.ref_counter, delta );
+	ast_atomic_fetchadd_int(&ao2.total_refs, delta);
 	current_value = ret + delta;
 	// ast_log(LOG_NOTICE, "refcount %d on object %p\n", current_value, user_data);
 	/* this case must never happen */
@@ -154,14 +166,16 @@
 		if (obj->priv_data.destructor_fn != NULL) 
 			obj->priv_data.destructor_fn(user_data);
 
+		/* XXX should check if locked ? */
 		ast_mutex_destroy(&obj->priv_data.lock);
+		ast_atomic_fetchadd_int(&ao2.total_mem, - obj->priv_data.data_size);
 		/* for safety, zero-out the astobj2 header and also the
 		 * first word of the user-data, which we make sure is always
 		 * allocated.
 		 */
 		bzero(obj, sizeof(struct astobj2 *) + sizeof(void *) );
 		free(obj);
-		ast_atomic_fetchadd_int(&ao2_total_objects, -1);
+		ast_atomic_fetchadd_int(&ao2.total_objects, -1);
 	}
 
 	return ret;
@@ -188,9 +202,12 @@
 	/* init */
 	ast_mutex_init(&obj->priv_data.lock);
 	obj->priv_data.magic = AO2_MAGIC ^ (uint32_t)(obj);
+	obj->priv_data.data_size = data_size;
 	obj->priv_data.ref_counter = 1;
 	obj->priv_data.destructor_fn = destructor_fn;	/* can be NULL */
-	ast_atomic_fetchadd_int(&ao2_total_objects, 1);
+	ast_atomic_fetchadd_int(&ao2.total_objects, 1);
+	ast_atomic_fetchadd_int(&ao2.total_mem, data_size);
+	ast_atomic_fetchadd_int(&ao2.total_refs, 1);
 
 	/* return a pointer to the user data */
 	return EXTERNAL_OBJ(obj);
@@ -258,6 +275,7 @@
 		c->n_buckets = n_buckets;
 		c->hash_fn = hash_fn ? hash_fn : hash_zero;
 		c->cmp_fn = cmp_fn;
+		ast_atomic_fetchadd_int(&ao2.total_containers, 1);
 	}
 	ast_verbose("ao2_container_alloc %d returns %p\n", n_buckets, c);
 	return c;
@@ -547,8 +565,9 @@
 static void container_destruct(void *_c)
 {
 	ao2_container *c = _c;
-	
+
 	ao2_callback(c, OBJ_UNLINK, cd_cb, NULL);
+	ast_atomic_fetchadd_int(&ao2.total_containers, -1);
 }
 
 static int print_cb(void *obj, void *arg, int flag)
@@ -561,11 +580,22 @@
 }
 
 /*
+ * Print stats
+ */
+static int handle_astobj2_stats(int fd, int argc, char *argv[])
+{
+	ast_cli(fd, "Objects    : %d\n", ao2.total_objects);
+	ast_cli(fd, "Containers : %d\n", ao2.total_containers);
+	ast_cli(fd, "Memory     : %d\n", ao2.total_mem);
+	ast_cli(fd, "Locked     : %d\n", ao2.total_locked);
+	ast_cli(fd, "Refs       : %d\n", ao2.total_refs);
+	return 0;
+}
+
+/*
  * This is testing code for astobj
  */
-int handle_astobj2_test(int fd, int argc, char *argv[]);
-int handle_astobj2_test(int fd, int argc, char *argv[])
-
+static int handle_astobj2_test(int fd, int argc, char *argv[])
 {
 	ao2_container *c1;
 	int i, lim;
@@ -579,8 +609,7 @@
 	lim = atoi(argv[2]);
 	ast_cli(fd, "called astobj_test\n");
 
-	ast_verbose("at the beginning have %d objects\n",
-		ao2_total_objects);
+	handle_astobj2_stats(fd, 0, NULL);
 	/*
 	 * allocate a container with no default callback, and no hash function.
 	 * No hash means everything goes in the same bucket.
@@ -631,7 +660,22 @@
 
 	ast_cli(fd, "destroy container\n");
 	ao2_ref(c1, -1);	/* destroy container */
-	ast_verbose("at the end have %d objects\n",
-		ao2_total_objects);
+	handle_astobj2_stats(fd, 0, NULL);
 	return 0;
 }
+
+static struct ast_cli_entry cli_astobj2[] = {
+	{ { "astobj2", "stats", NULL },
+	handle_astobj2_stats, "Print astobj2 statistics",
+	NULL },
+	{ { "astobj2", "test", NULL },
+	handle_astobj2_test, "Test astobj2",
+	NULL },
+};
+
+int astobj2_init(void);
+int astobj2_init(void)
+{
+        ast_cli_register_multiple(cli_astobj2, sizeof(cli_astobj2) / sizeof(struct ast_cli_entry));
+	return 0;
+}



More information about the asterisk-commits mailing list