glib/tsrc/BC/tests/slice-test.c
changeset 0 e4d67989cc36
child 18 47c74d1534e1
equal deleted inserted replaced
-1:000000000000 0:e4d67989cc36
       
     1 /* GLIB sliced memory - fast threaded memory chunk allocator
       
     2  * Copyright (C) 2005 Tim Janik
       
     3  * Portion Copyright © 2008-09 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
       
     4  * This library is free software; you can redistribute it and/or
       
     5  * modify it under the terms of the GNU Lesser General Public
       
     6  * License as published by the Free Software Foundation; either
       
     7  * version 2 of the License, or (at your option) any later version.
       
     8  *
       
     9  * This library is distributed in the hope that it will be useful,
       
    10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
       
    11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
       
    12  * Lesser General Public License for more details.
       
    13  *
       
    14  * You should have received a copy of the GNU Lesser General Public
       
    15  * License along with this library; if not, write to the
       
    16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
       
    17  * Boston, MA 02111-1307, USA.
       
    18  */
       
    19 #include <glib.h>
       
    20 
       
    21 #include <stdio.h>
       
    22 #include <string.h>
       
    23 #include <sys/time.h> // gettimeofday
       
    24 
       
    25 #define quick_rand32()  (rand_accu = 1664525 * rand_accu + 1013904223, rand_accu)
       
    26 static guint    prime_size = 1021; // 769; // 509
       
    27 static gboolean clean_memchunks = FALSE;
       
    28 static guint    number_of_blocks = 10000;          /* total number of blocks allocated */
       
    29 static guint    number_of_repetitions = 10000;     /* number of alloc+free repetitions */
       
    30 
       
    31 /* --- old memchunk prototypes (memchunks.c) --- */
       
    32 void            old_mem_chunks_init     (void);
       
    33 GMemChunk*      old_mem_chunk_new       (const gchar  *name,
       
    34                                          gint          atom_size,
       
    35                                          gulong        area_size,
       
    36                                          gint          type);
       
    37 void            old_mem_chunk_destroy   (GMemChunk *mem_chunk);
       
    38 gpointer        old_mem_chunk_alloc     (GMemChunk *mem_chunk);
       
    39 gpointer        old_mem_chunk_alloc0    (GMemChunk *mem_chunk);
       
    40 void            old_mem_chunk_free      (GMemChunk *mem_chunk,
       
    41                                          gpointer   mem);
       
    42 void            old_mem_chunk_clean     (GMemChunk *mem_chunk);
       
    43 void            old_mem_chunk_reset     (GMemChunk *mem_chunk);
       
    44 void            old_mem_chunk_print     (GMemChunk *mem_chunk);
       
    45 void            old_mem_chunk_info      (void);
       
    46 #ifndef G_ALLOC_AND_FREE
       
    47 #define G_ALLOC_AND_FREE  2
       
    48 #endif
       
    49 
       
    50 /* --- functions --- */
       
    51 static inline gpointer
       
    52 memchunk_alloc (GMemChunk **memchunkp,
       
    53                 guint       size)
       
    54 {
       
    55   size = MAX (size, 1);
       
    56   if (G_UNLIKELY (!*memchunkp))
       
    57     *memchunkp = old_mem_chunk_new ("", size, 4096, G_ALLOC_AND_FREE);
       
    58   return old_mem_chunk_alloc (*memchunkp);
       
    59 }
       
    60 
       
    61 static inline void
       
    62 memchunk_free (GMemChunk *memchunk,
       
    63                gpointer   chunk)
       
    64 {
       
    65   old_mem_chunk_free (memchunk, chunk);
       
    66   if (clean_memchunks)
       
    67     old_mem_chunk_clean (memchunk);
       
    68 }
       
    69 
       
    70 static gpointer
       
    71 test_memchunk_thread (gpointer data)
       
    72 {
       
    73   GMemChunk **memchunks;
       
    74   guint i, j;
       
    75   guint8 **ps;
       
    76   guint   *ss;
       
    77   guint32 rand_accu = 2147483563;
       
    78   /* initialize random numbers */
       
    79   if (data)
       
    80     rand_accu = *(guint32*) data;
       
    81   else
       
    82     {
       
    83       struct timeval rand_tv;
       
    84       gettimeofday (&rand_tv, NULL);
       
    85       rand_accu = rand_tv.tv_usec + (rand_tv.tv_sec << 16);
       
    86     }
       
    87 
       
    88   /* prepare for memchunk creation */
       
    89   memchunks = g_alloca (sizeof (memchunks[0]) * prime_size);
       
    90   memset (memchunks, 0, sizeof (memchunks[0]) * prime_size);
       
    91 
       
    92   ps = g_new (guint8*, number_of_blocks);
       
    93   ss = g_new (guint, number_of_blocks);
       
    94   /* create number_of_blocks random sizes */
       
    95   for (i = 0; i < number_of_blocks; i++)
       
    96     ss[i] = quick_rand32() % prime_size;
       
    97   /* allocate number_of_blocks blocks */
       
    98   for (i = 0; i < number_of_blocks; i++)
       
    99     ps[i] = memchunk_alloc (&memchunks[ss[i]], ss[i]);
       
   100   for (j = 0; j < number_of_repetitions; j++)
       
   101     {
       
   102       /* free number_of_blocks/2 blocks */
       
   103       for (i = 0; i < number_of_blocks; i += 2)
       
   104         memchunk_free (memchunks[ss[i]], ps[i]);
       
   105       /* allocate number_of_blocks/2 blocks with new sizes */
       
   106       for (i = 0; i < number_of_blocks; i += 2)
       
   107         {
       
   108           ss[i] = quick_rand32() % prime_size;
       
   109           ps[i] = memchunk_alloc (&memchunks[ss[i]], ss[i]);
       
   110         }
       
   111     }
       
   112   /* free number_of_blocks blocks */
       
   113   for (i = 0; i < number_of_blocks; i++)
       
   114     memchunk_free (memchunks[ss[i]], ps[i]);
       
   115   /* alloc and free many equally sized chunks in a row */
       
   116   for (i = 0; i < number_of_repetitions; i++)
       
   117     {
       
   118       guint sz = quick_rand32() % prime_size;
       
   119       guint k = number_of_blocks / 100;
       
   120       for (j = 0; j < k; j++)
       
   121         ps[j] = memchunk_alloc (&memchunks[sz], sz);
       
   122       for (j = 0; j < k; j++)
       
   123         memchunk_free (memchunks[sz], ps[j]);
       
   124     }
       
   125   /* cleanout memchunks */
       
   126   for (i = 0; i < prime_size; i++)
       
   127     if (memchunks[i])
       
   128       old_mem_chunk_destroy (memchunks[i]);
       
   129   g_free (ps);
       
   130   g_free (ss);
       
   131 
       
   132   return NULL;
       
   133 }
       
   134 
       
   135 static gpointer
       
   136 test_sliced_mem_thread (gpointer data)
       
   137 {
       
   138 guint i, j;
       
   139 guint   *ss; 
       
   140 
       
   141 guint8 **ps;
       
   142   guint32 rand_accu = 2147483563;
       
   143   /* initialize random numbers */
       
   144   if (data)
       
   145     rand_accu = *(guint32*) data;
       
   146   else
       
   147     {
       
   148       struct timeval rand_tv;
       
   149       gettimeofday (&rand_tv, NULL);
       
   150       rand_accu = rand_tv.tv_usec + (rand_tv.tv_sec << 16);
       
   151     }
       
   152 
       
   153 //guint i,j;
       
   154 /* guint8*/ ps = g_new (guint8*, number_of_blocks);
       
   155 /* guint*/  ss = g_new (guint, number_of_blocks);
       
   156   /* create number_of_blocks random sizes */
       
   157   for (i = 0; i < number_of_blocks; i++)
       
   158     ss[i] = quick_rand32() % prime_size;
       
   159   /* allocate number_of_blocks blocks */
       
   160   for (i = 0; i < number_of_blocks; i++)
       
   161     ps[i] = g_slice_alloc (ss[i]);
       
   162   for (j = 0; j < number_of_repetitions; j++)
       
   163     {
       
   164       /* free number_of_blocks/2 blocks */
       
   165       for (i = 0; i < number_of_blocks; i += 2)
       
   166         g_slice_free1 (ss[i], ps[i]);
       
   167       /* allocate number_of_blocks/2 blocks with new sizes */
       
   168       for (i = 0; i < number_of_blocks; i += 2)
       
   169         {
       
   170           ss[i] = quick_rand32() % prime_size;
       
   171           ps[i] = g_slice_alloc (ss[i]);
       
   172         }
       
   173     }
       
   174   /* free number_of_blocks blocks */
       
   175   for (i = 0; i < number_of_blocks; i++)
       
   176     g_slice_free1 (ss[i], ps[i]);
       
   177   /* alloc and free many equally sized chunks in a row */
       
   178   for (i = 0; i < number_of_repetitions; i++)
       
   179     {
       
   180       guint sz = quick_rand32() % prime_size;
       
   181       guint k = number_of_blocks / 100;
       
   182       for (j = 0; j < k; j++)
       
   183         ps[j] = g_slice_alloc (sz);
       
   184       for (j = 0; j < k; j++)
       
   185         g_slice_free1 (sz, ps[j]);
       
   186     }
       
   187   g_free (ps);
       
   188   g_free (ss);
       
   189 
       
   190   return NULL;
       
   191 }
       
   192 
       
   193 static void
       
   194 usage (void)
       
   195 {
       
   196   g_print ("Usage: slice-test [n_threads] [G|S|M|O][f][c] [maxblocksize] [seed]\n");
       
   197 }
       
   198 
       
   199 int
       
   200 main (int   argc,
       
   201       char *argv[])
       
   202 {
       
   203   gchar strseed[64] = "<random>";
       
   204    
       
   205   guint seed32, *seedp = NULL;
       
   206   gboolean ccounters = FALSE, use_memchunks = FALSE;
       
   207   guint n_threads = 1;
       
   208   guint i;
       
   209   GThread *threads[1];
       
   210   const gchar *mode = "slab allocator + magazine cache", *emode = " ";
       
   211   if (argc > 1)
       
   212     n_threads = g_ascii_strtoull (argv[1], NULL, 10);
       
   213   if (argc > 2)
       
   214     {
       
   215       guint i, l = strlen (argv[2]);
       
   216       for (i = 0; i < l; i++)
       
   217         switch (argv[2][i])
       
   218           {
       
   219           case 'G': /* GLib mode */
       
   220             g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, FALSE);
       
   221             g_slice_set_config (G_SLICE_CONFIG_BYPASS_MAGAZINES, FALSE);
       
   222             mode = "slab allocator + magazine cache";
       
   223             break;
       
   224           case 'S': /* slab mode */
       
   225             g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, FALSE);
       
   226             g_slice_set_config (G_SLICE_CONFIG_BYPASS_MAGAZINES, TRUE);
       
   227             mode = "slab allocator";
       
   228             break;
       
   229           case 'M': /* malloc mode */
       
   230             g_slice_set_config (G_SLICE_CONFIG_ALWAYS_MALLOC, TRUE);
       
   231             mode = "system malloc";
       
   232             break;
       
   233           case 'O': /* old memchunks */
       
   234             use_memchunks = TRUE;
       
   235             mode = "old memchunks";
       
   236             break;
       
   237           case 'f': /* eager freeing */
       
   238             g_slice_set_config (G_SLICE_CONFIG_WORKING_SET_MSECS, 0);
       
   239             clean_memchunks = TRUE;
       
   240             emode = " with eager freeing";
       
   241             break;
       
   242           case 'c': /* print contention counters */
       
   243             ccounters = TRUE;
       
   244             break;
       
   245           default:
       
   246             usage();
       
   247             return 1;
       
   248           }
       
   249     }
       
   250   if (argc > 3)
       
   251     prime_size = g_ascii_strtoull (argv[3], NULL, 10);
       
   252   if (argc > 4)
       
   253     {
       
   254       seed32 = g_ascii_strtoull (argv[4], NULL, 10);
       
   255       seedp = &seed32;
       
   256     }
       
   257 
       
   258   g_thread_init (NULL);
       
   259 
       
   260   if (argc <= 1)
       
   261     usage();
       
   262 
       
   263  // gchar strseed[64] = "<random>";
       
   264   if (seedp)
       
   265     g_snprintf (strseed, 64, "%u", *seedp);
       
   266   g_print ("Starting %d threads allocating random blocks <= %u bytes with seed=%s using %s%s\n", n_threads, prime_size, strseed, mode, emode);
       
   267   
       
   268 //  GThread *threads[n_threads];
       
   269 //  guint i;
       
   270   if (!use_memchunks)
       
   271     for (i = 0; i < n_threads; i++)
       
   272       threads[i] = g_thread_create_full (test_sliced_mem_thread, seedp, 0, TRUE, FALSE, 0, NULL);
       
   273   else
       
   274     {
       
   275       old_mem_chunks_init();
       
   276       for (i = 0; i < n_threads; i++)
       
   277         threads[i] = g_thread_create_full (test_memchunk_thread, seedp, 0, TRUE, FALSE, 0, NULL);
       
   278     }
       
   279   for (i = 0; i < n_threads; i++)
       
   280     g_thread_join (threads[i]);
       
   281   
       
   282   if (ccounters)
       
   283     {
       
   284       guint n, n_chunks = g_slice_get_config (G_SLICE_CONFIG_CHUNK_SIZES);
       
   285       g_print ("    ChunkSize | MagazineSize | Contention\n");
       
   286       for (i = 0; i < n_chunks; i++)
       
   287         {
       
   288           gint64 *vals = g_slice_get_config_state (G_SLICE_CONFIG_CONTENTION_COUNTER, i, &n);
       
   289           g_print ("  %9llu   |  %9llu   |  %9llu\n", vals[0], vals[2], vals[1]);
       
   290           g_free (vals);
       
   291         }
       
   292     }
       
   293   else
       
   294     g_print ("Done.\n");
       
   295   return 0;
       
   296 }