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