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