glib/tsrc/BC/tests/slice-color1.c
changeset 0 e4d67989cc36
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 #include <string.h>
       
    21 
       
    22 
       
    23 #define ALIGN(size, base)       ((base) * (gsize) (((size) + (base) - 1) / (base)))
       
    24 
       
    25 #ifdef SYMBIAN
       
    26 #include "mrt2_glib2_test.h"
       
    27 #endif //SYMBIAN
       
    28 
       
    29 static gdouble parse_memsize (const gchar *cstring);
       
    30 static void    usage         (void);
       
    31 
       
    32 static void
       
    33 fill_memory (guint **mem,
       
    34              guint   n,
       
    35              guint   val)
       
    36 {
       
    37   guint j, o = 0;
       
    38   for (j = 0; j < n; j++)
       
    39     mem[j][o] = val;
       
    40 }
       
    41 
       
    42 static guint64
       
    43 access_memory3 (guint  **mema,
       
    44                 guint  **memb,
       
    45                 guint  **memd,
       
    46                 guint    n,
       
    47                 guint64  repeats)
       
    48 {
       
    49   guint64 accu = 0, i, j;
       
    50   const guint o = 0;
       
    51   for (i = 0; i < repeats; i++)
       
    52     {
       
    53       for (j = 1; j < n; j += 2)
       
    54         memd[j][o] = mema[j][o] + memb[j][o];
       
    55     }
       
    56   for (i = 0; i < repeats; i++)
       
    57     for (j = 0; j < n; j++)
       
    58       accu += memd[j][o];
       
    59   return accu;
       
    60 }
       
    61 
       
    62 static void
       
    63 touch_mem (guint64 block_size,
       
    64            guint64 n_blocks,
       
    65            guint64 repeats)
       
    66 {
       
    67   guint **mema,**memb,**memc ;
       
    68   guint64 j, accu, n = n_blocks;
       
    69   GTimer *timer;
       
    70   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 * 1024, n_blocks, repeats = 1000000;
       
   115   
       
   116   #ifdef SYMBIAN
       
   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 /*SYMBIAN*/
       
   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_printerr ("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 
       
   151   touch_mem (block_size, n_blocks, repeats);
       
   152   
       
   153   
       
   154   #ifdef SYMBIAN
       
   155   testResultXml("slice-color");
       
   156   #endif //SYMBIAN
       
   157   
       
   158   
       
   159   
       
   160   return 0;
       
   161 }
       
   162 
       
   163 static gdouble
       
   164 parse_memsize (const gchar *cstring)
       
   165 {
       
   166   gchar *derr;
       
   167   gdouble msize;
       
   168   gchar *mem = g_strdup (cstring);
       
   169   gchar *string = g_strstrip (mem);
       
   170   guint l = strlen (string);
       
   171   gdouble f = 0;
       
   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   derr = NULL;
       
   184   msize = g_ascii_strtod (string, &derr);
       
   185   g_free (mem);
       
   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 }