2005-03-30 14:09:01 +00:00
|
|
|
/* GStreamer
|
|
|
|
* Copyright (C) 2005 Andy Wingo <wingo at pobox.com>
|
|
|
|
*
|
|
|
|
* This library is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU Library General Public
|
|
|
|
* License as published by the Free Software Foundation; either
|
|
|
|
* version 2 of the License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This library is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
* Library General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this library; if not, write to the
|
|
|
|
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
|
|
* Boston, MA 02111-1307, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <glib.h>
|
2005-04-01 10:14:45 +00:00
|
|
|
#include <gmodule.h>
|
2005-03-30 14:09:01 +00:00
|
|
|
#include <gst/gstmemchunk.h>
|
|
|
|
|
2002-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
#define MAX_THREADS 100
|
2005-03-30 14:09:01 +00:00
|
|
|
#define CHUNK_SIZE 32
|
|
|
|
#define GMEMCHUNK_THREADSAFE
|
|
|
|
|
|
|
|
|
|
|
|
typedef gpointer (*alloc_func_t) (void);
|
|
|
|
typedef void (*free_func_t) (gpointer);
|
2002-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
|
|
|
|
static gint num_allocs;
|
|
|
|
static gint num_threads;
|
2005-03-30 14:09:01 +00:00
|
|
|
static alloc_func_t _alloc = NULL;
|
|
|
|
static free_func_t _free = NULL;
|
|
|
|
|
|
|
|
static GMemChunk *_gmemchunk;
|
|
|
|
static GMutex *_gmemchunklock;
|
|
|
|
static GstMemChunk *_gstmemchunk;
|
|
|
|
|
|
|
|
static GCond *ready_cond;
|
|
|
|
static GCond *start_cond;
|
|
|
|
static GMutex *sync_mutex;
|
|
|
|
|
|
|
|
|
|
|
|
static gdouble
|
|
|
|
get_current_time (void)
|
|
|
|
{
|
|
|
|
GTimeVal tv;
|
|
|
|
|
|
|
|
g_get_current_time (&tv);
|
|
|
|
return tv.tv_sec + ((gdouble) tv.tv_usec) / G_USEC_PER_SEC;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GMemChunk implementation
|
|
|
|
*/
|
2002-01-05 01:25:28 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static gpointer
|
2005-03-30 14:09:01 +00:00
|
|
|
gmemchunk_alloc (void)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
gpointer ret;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2005-03-30 14:09:01 +00:00
|
|
|
#ifdef GMEMCHUNK_THREADSAFE
|
|
|
|
g_mutex_lock (_gmemchunklock);
|
|
|
|
#endif
|
|
|
|
ret = g_mem_chunk_alloc (_gmemchunk);
|
|
|
|
#ifdef GMEMCHUNK_THREADSAFE
|
|
|
|
g_mutex_unlock (_gmemchunklock);
|
|
|
|
#endif
|
2002-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
static void
|
2005-03-30 14:09:01 +00:00
|
|
|
gmemchunk_free (gpointer chunk)
|
|
|
|
{
|
|
|
|
#ifdef GMEMCHUNK_THREADSAFE
|
|
|
|
g_mutex_lock (_gmemchunklock);
|
|
|
|
#endif
|
|
|
|
g_mem_chunk_free (_gmemchunk, chunk);
|
|
|
|
#ifdef GMEMCHUNK_THREADSAFE
|
|
|
|
g_mutex_unlock (_gmemchunklock);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* GstMemChunk implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
gstmemchunk_alloc (void)
|
|
|
|
{
|
|
|
|
return gst_mem_chunk_alloc (_gstmemchunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
gstmemchunk_free (gpointer chunk)
|
|
|
|
{
|
|
|
|
gst_mem_chunk_free (_gstmemchunk, chunk);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Normal (malloc/free) implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
static gpointer
|
|
|
|
normal_alloc (void)
|
|
|
|
{
|
|
|
|
return g_malloc (CHUNK_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
normal_free (gpointer chunk)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
2005-03-30 14:09:01 +00:00
|
|
|
g_free (chunk);
|
2002-01-05 01:25:28 +00:00
|
|
|
}
|
|
|
|
|
2005-04-01 10:14:45 +00:00
|
|
|
/*
|
|
|
|
* Normal (malloc/free) implementation
|
|
|
|
*/
|
|
|
|
|
|
|
|
void *(*_google_malloc) (gsize) = NULL;
|
|
|
|
void (*_google_free) (void *) = NULL;
|
|
|
|
static gpointer
|
|
|
|
google_alloc (void)
|
|
|
|
{
|
|
|
|
return _google_malloc (CHUNK_SIZE);
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
google_free (gpointer chunk)
|
|
|
|
{
|
|
|
|
_google_free (chunk);
|
|
|
|
}
|
|
|
|
|
2005-03-30 14:09:01 +00:00
|
|
|
/*
|
|
|
|
* The test
|
|
|
|
*/
|
2002-01-05 01:25:28 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
void *
|
2005-03-30 14:09:01 +00:00
|
|
|
worker_thread (void *threadid)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
|
|
|
gint i;
|
|
|
|
gpointer chunk;
|
2003-06-29 14:05:49 +00:00
|
|
|
|
2005-03-30 14:09:01 +00:00
|
|
|
g_mutex_lock (sync_mutex);
|
|
|
|
g_cond_signal (ready_cond);
|
|
|
|
g_cond_wait (start_cond, sync_mutex);
|
|
|
|
g_mutex_unlock (sync_mutex);
|
2002-01-05 01:25:28 +00:00
|
|
|
|
2004-03-13 15:27:01 +00:00
|
|
|
for (i = 0; i < num_allocs; i++) {
|
2005-03-30 14:09:01 +00:00
|
|
|
chunk = _alloc ();
|
|
|
|
_free (chunk);
|
2002-01-05 01:25:28 +00:00
|
|
|
}
|
|
|
|
|
2002-11-01 22:17:20 +00:00
|
|
|
return NULL;
|
2002-01-05 01:25:28 +00:00
|
|
|
}
|
|
|
|
|
2005-03-30 14:09:01 +00:00
|
|
|
gdouble
|
|
|
|
run_test (alloc_func_t alloc_func, free_func_t free_func)
|
2002-01-05 01:25:28 +00:00
|
|
|
{
|
2005-03-30 14:09:01 +00:00
|
|
|
gdouble start, end;
|
2002-11-01 22:17:20 +00:00
|
|
|
GThread *threads[MAX_THREADS];
|
|
|
|
GError *error;
|
|
|
|
int t;
|
2004-03-13 15:27:01 +00:00
|
|
|
|
2005-03-30 14:09:01 +00:00
|
|
|
_alloc = alloc_func;
|
|
|
|
_free = free_func;
|
|
|
|
|
|
|
|
g_mutex_lock (sync_mutex);
|
|
|
|
for (t = 0; t < num_threads; t++) {
|
|
|
|
error = NULL;
|
|
|
|
threads[t] =
|
|
|
|
g_thread_create (worker_thread, GINT_TO_POINTER (t), TRUE, &error);
|
|
|
|
g_assert (threads[t]);
|
|
|
|
g_cond_wait (ready_cond, sync_mutex);
|
|
|
|
}
|
|
|
|
|
|
|
|
g_cond_broadcast (start_cond);
|
|
|
|
start = get_current_time ();
|
|
|
|
g_mutex_unlock (sync_mutex);
|
|
|
|
|
|
|
|
for (t = 0; t < num_threads; t++)
|
|
|
|
g_thread_join (threads[t]);
|
|
|
|
|
|
|
|
end = get_current_time ();
|
|
|
|
|
|
|
|
return end - start;
|
|
|
|
}
|
|
|
|
|
|
|
|
gint
|
|
|
|
main (gint argc, gchar * argv[])
|
|
|
|
{
|
|
|
|
gdouble time;
|
2005-04-01 10:14:45 +00:00
|
|
|
GModule *google_lib;
|
2005-03-30 14:09:01 +00:00
|
|
|
|
|
|
|
g_thread_init (NULL);
|
|
|
|
|
|
|
|
ready_cond = g_cond_new ();
|
|
|
|
start_cond = g_cond_new ();
|
|
|
|
sync_mutex = g_mutex_new ();
|
2002-01-05 01:25:28 +00:00
|
|
|
|
|
|
|
if (argc != 3) {
|
|
|
|
g_print ("usage: %s <num_threads> <num_allocs>\n", argv[0]);
|
|
|
|
exit (-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
num_threads = atoi (argv[1]);
|
|
|
|
num_allocs = atoi (argv[2]);
|
2005-03-30 14:09:01 +00:00
|
|
|
g_assert (num_threads > 0);
|
|
|
|
g_assert (num_allocs > 0);
|
|
|
|
|
|
|
|
_gmemchunk =
|
2005-03-31 15:00:11 +00:00
|
|
|
g_mem_chunk_new ("test", CHUNK_SIZE, CHUNK_SIZE * 16, G_ALLOC_ONLY);
|
2005-03-30 14:09:01 +00:00
|
|
|
_gmemchunklock = g_mutex_new ();
|
|
|
|
_gstmemchunk =
|
2005-03-31 15:00:11 +00:00
|
|
|
gst_mem_chunk_new ("test", CHUNK_SIZE, CHUNK_SIZE * 16, G_ALLOC_ONLY);
|
2005-03-30 14:09:01 +00:00
|
|
|
|
|
|
|
g_print ("%d alloc+frees X %d threads\n", num_allocs, num_threads);
|
|
|
|
time = run_test (gmemchunk_alloc, gmemchunk_free);
|
2005-03-31 10:10:21 +00:00
|
|
|
g_print ("%fs (%fs/thread) - GMemChunk\n", time, time / num_threads);
|
2005-03-30 14:09:01 +00:00
|
|
|
time = run_test (gstmemchunk_alloc, gstmemchunk_free);
|
2005-03-31 10:10:21 +00:00
|
|
|
g_print ("%fs (%fs/thread) - GstMemChunk\n", time, time / num_threads);
|
2005-03-30 14:09:01 +00:00
|
|
|
time = run_test (normal_alloc, normal_free);
|
2005-03-31 10:10:21 +00:00
|
|
|
g_print ("%fs (%fs/thread) - g_malloc/g_free\n", time, time / num_threads);
|
2005-03-30 14:09:01 +00:00
|
|
|
|
2005-04-01 10:14:45 +00:00
|
|
|
google_lib = g_module_open ("libtcmalloc.so", G_MODULE_BIND_LOCAL);
|
|
|
|
if (google_lib) {
|
|
|
|
gpointer sym;
|
|
|
|
|
|
|
|
g_module_symbol (google_lib, "malloc", &sym);
|
|
|
|
g_assert (sym);
|
|
|
|
_google_malloc = sym;
|
|
|
|
g_module_symbol (google_lib, "free", &sym);
|
|
|
|
g_assert (sym);
|
|
|
|
_google_free = sym;
|
|
|
|
time = run_test (google_alloc, google_free);
|
|
|
|
g_print ("%fs (%fs/thread) - google malloc/free\n", time,
|
|
|
|
time / num_threads);
|
|
|
|
} else {
|
|
|
|
g_print ("google malloc unavailable: %s\n", g_module_error ());
|
|
|
|
}
|
|
|
|
|
2005-03-30 14:09:01 +00:00
|
|
|
/* g_mem_chunk_info (); */
|
2002-11-01 22:17:20 +00:00
|
|
|
return 0;
|
2002-01-05 01:25:28 +00:00
|
|
|
}
|