glib/tests/slice-color.c
changeset 18 47c74d1534e1
equal deleted inserted replaced
0:e4d67989cc36 18:47c74d1534e1
       
     1 /* GLIB sliced memory - fast threaded memory chunk allocator
       
     2  * Copyright (C) 2005 Tim Janik
       
     3  * Portions copyright (c) 2009 Nokia Corporation.  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 #include <string.h>
       
    21 
       
    22 #define ALIGN(size, base)       ((base) * (gsize) (((size) + (base) - 1) / (base)))
       
    23 
       
    24 #ifdef __SYMBIAN32__
       
    25 #include "mrt2_glib2_test.h"
       
    26 #endif //__SYMBIAN32__
       
    27 
       
    28 static gdouble parse_memsize (const gchar *cstring);
       
    29 static void    usage         (void);
       
    30 
       
    31 static void
       
    32 fill_memory (guint **mem,
       
    33              guint   n,
       
    34              guint   val)
       
    35 {
       
    36   guint j, o = 0;
       
    37   for (j = 0; j < n; j++)
       
    38     mem[j][o] = val;
       
    39 }
       
    40 
       
    41 static guint64
       
    42 access_memory3 (guint  **mema,
       
    43                 guint  **memb,
       
    44                 guint  **memd,
       
    45                 guint    n,
       
    46                 guint64  repeats)
       
    47 {
       
    48   guint64 accu = 0, i, j;
       
    49   const guint o = 0;
       
    50   for (i = 0; i < repeats; i++)
       
    51     {
       
    52       for (j = 1; j < n; j += 2)
       
    53         memd[j][o] = mema[j][o] + memb[j][o];
       
    54     }
       
    55   for (i = 0; i < repeats; i++)
       
    56     for (j = 0; j < n; j++)
       
    57       accu += memd[j][o];
       
    58   return accu;
       
    59 }
       
    60 
       
    61 static void
       
    62 touch_mem (guint64 block_size,
       
    63            guint64 n_blocks,
       
    64            guint64 repeats)
       
    65 {
       
    66   guint64 j, accu, n = n_blocks;
       
    67   GTimer *timer;
       
    68   guint **memc;
       
    69   guint **memb;
       
    70   guint **mema = g_new (guint*, n);
       
    71   for (j = 0; j < n; j++)
       
    72     mema[j] = g_slice_alloc (block_size);
       
    73   memb = g_new (guint*, n);
       
    74   for (j = 0; j < n; j++)
       
    75     memb[j] = g_slice_alloc (block_size);
       
    76   memc = g_new (guint*, n);
       
    77   for (j = 0; j < n; j++)
       
    78     memc[j] = g_slice_alloc (block_size);
       
    79 
       
    80   timer = g_timer_new();
       
    81   fill_memory (mema, n, 2);
       
    82   fill_memory (memb, n, 3);
       
    83   fill_memory (memc, n, 4);
       
    84   access_memory3 (mema, memb, memc, n, 3);
       
    85   g_timer_start (timer);
       
    86   accu = access_memory3 (mema, memb, memc, n, repeats);
       
    87   g_timer_stop (timer);
       
    88 
       
    89   g_print ("Access-time = %fs\n", g_timer_elapsed (timer, NULL));
       
    90   g_assert (accu / repeats == (2 + 3) * n / 2 + 4 * n / 2);
       
    91 
       
    92   for (j = 0; j < n; j++)
       
    93     {
       
    94       g_slice_free1 (block_size, mema[j]);
       
    95       g_slice_free1 (block_size, memb[j]);
       
    96       g_slice_free1 (block_size, memc[j]);
       
    97     }
       
    98   g_timer_destroy (timer);
       
    99   g_free (mema);
       
   100   g_free (memb);
       
   101   g_free (memc);
       
   102 }
       
   103 
       
   104 static void
       
   105 usage (void)
       
   106 {
       
   107   g_print ("Usage: slice-color <block-size> [memory-size] [repeats] [colorization]\n");
       
   108 }
       
   109 
       
   110 int
       
   111 main (int   argc,
       
   112       char *argv[])
       
   113 {
       
   114   guint64 block_size = 512, area_size = 1024, n_blocks, repeats = 10;
       
   115 
       
   116   #ifdef __SYMBIAN32__
       
   117   g_log_set_handler (NULL,  G_LOG_FLAG_FATAL| G_LOG_FLAG_RECURSION | G_LOG_LEVEL_CRITICAL | G_LOG_LEVEL_WARNING | G_LOG_LEVEL_MESSAGE | G_LOG_LEVEL_INFO | G_LOG_LEVEL_DEBUG, &mrtLogHandler, NULL);
       
   118   g_set_print_handler(mrtPrintHandler);
       
   119   #endif /*__SYMBIAN32__*/
       
   120   
       
   121   if (argc > 1)
       
   122     block_size = parse_memsize (argv[1]);
       
   123   else
       
   124     {
       
   125       usage();
       
   126       block_size = 512;
       
   127     }
       
   128   if (argc > 2)
       
   129     area_size = parse_memsize (argv[2]);
       
   130   if (argc > 3)
       
   131     repeats = parse_memsize (argv[3]);
       
   132   if (argc > 4)
       
   133     g_slice_set_config (G_SLICE_CONFIG_COLOR_INCREMENT, parse_memsize (argv[4]));
       
   134 
       
   135   /* figure number of blocks from block and area size.
       
   136    * divide area by 3 because touch_mem() allocates 3 areas
       
   137    */
       
   138   n_blocks = area_size / 3 / ALIGN (block_size, sizeof (gsize) * 2);
       
   139 
       
   140   /* basic sanity checks */
       
   141   if (!block_size || !n_blocks || block_size >= area_size)
       
   142     {
       
   143       g_printerr ("Invalid arguments: block-size=%llu memory-size=%llu\n", block_size, area_size);
       
   144       usage();
       
   145       return 1;
       
   146     }
       
   147 
       
   148   g_print ("Will allocate and touch %llu blocks of %llu bytes (= %llu bytes) %llu times with color increment: 0x%08llx\n",
       
   149               n_blocks, block_size, n_blocks * block_size, repeats, g_slice_get_config (G_SLICE_CONFIG_COLOR_INCREMENT));
       
   150   touch_mem (block_size, n_blocks, repeats);
       
   151   
       
   152   
       
   153   #ifdef __SYMBIAN32__
       
   154   testResultXml("slice-color");
       
   155   #endif //__SYMBIAN32__
       
   156   
       
   157   
       
   158   
       
   159   return 0;
       
   160 }
       
   161 
       
   162 static gdouble
       
   163 parse_memsize (const gchar *cstring)
       
   164 {
       
   165   gchar *mem = g_strdup (cstring);
       
   166   gchar *string = g_strstrip (mem);
       
   167   guint l = strlen (string);
       
   168   gdouble f = 0;
       
   169   gchar *derr = NULL;
       
   170   gdouble msize;
       
   171 
       
   172   switch (l ? string[l - 1] : 0)
       
   173     {
       
   174     case 'k':   f = 1000;               break;
       
   175     case 'K':   f = 1024;               break;
       
   176     case 'm':   f = 1000000;            break;
       
   177     case 'M':   f = 1024 * 1024;        break;
       
   178     case 'g':   f = 1000000000;         break;
       
   179     case 'G':   f = 1024 * 1024 * 1024; break;
       
   180     }
       
   181   if (f)
       
   182     string[l - 1] = 0;
       
   183   msize = g_ascii_strtod (string, &derr);
       
   184   g_free (mem);
       
   185 /*  
       
   186   if (derr && *derr)
       
   187     {
       
   188       g_printerr ("failed to parse number at: %s\n", derr);
       
   189       msize = 0;
       
   190     }*/
       
   191   if (f)
       
   192     msize *= f;
       
   193   return msize;
       
   194 }