summaryrefslogtreecommitdiff
path: root/ext/mysqlnd/mysqlnd_alloc.c
blob: e54f80c6908eee3273bafa89809de29a782c2574 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
/*
  +----------------------------------------------------------------------+
  | PHP Version 6                                                        |
  +----------------------------------------------------------------------+
  | Copyright (c) 2006-2009 The PHP Group                                |
  +----------------------------------------------------------------------+
  | This source file is subject to version 3.01 of the PHP license,      |
  | that is bundled with this package in the file LICENSE, and is        |
  | available through the world-wide-web at the following url:           |
  | http://www.php.net/license/3_01.txt                                  |
  | If you did not receive a copy of the PHP license and are unable to   |
  | obtain it through the world-wide-web, please send a note to          |
  | license@php.net so we can mail you a copy immediately.               |
  +----------------------------------------------------------------------+
  | Authors: Georg Richter <georg@mysql.com>                             |
  |          Andrey Hristov <andrey@mysql.com>                           |
  |          Ulf Wendel <uwendel@mysql.com>                              |
  +----------------------------------------------------------------------+

*/

/* $Id: mysqlnd_alloc.c 272370 2008-12-31 11:15:49Z sebastian $ */
#include "php.h"
#include "mysqlnd.h"
#include "mysqlnd_priv.h"
#include "mysqlnd_palloc.h"


#define MYSQLND_SILENT
#define MYSQLND_DONT_DUMP_STATS

#define MYSQLND_ZVALS_MAX_CACHE 5000


#if A0
/* Caching zval allocator */
zval * 				mysqlnd_alloc_get_zval(MYSQLND_ZVAL_CACHE * const cache);
void 				mysqlnd_alloc_zval_ptr_dtor(zval **zv, MYSQLND_ZVAL_CACHE * const cache);
MYSQLND_ZVAL_CACHE* mysqlnd_alloc_init_cache();
MYSQLND_ZVAL_CACHE* mysqlnd_alloc_get_cache_reference(MYSQLND_ZVAL_CACHE *cache);
void				mysqlnd_alloc_free_cache_reference(MYSQLND_ZVAL_CACHE **cache);
#endif


/*
 The cache line is a big contiguous array of zval pointers.
 Because the CPU cache will cache starting from an address, and not
 before it, then we have to organize our structure according to this.
 Thus, if 'last_added' is valid pointer (not NULL) then last_added is
 increased. When zval is cached, if there is room, last_added is decreased
 and then the zval pointer will be assigned to it. This means that some
 positions may become hot points and stay in the cache.
 Imagine we have 5 pointers in a line
 1. last_added = list_item->ptr_line + cache->max_items;
 2. get_zval -> *last_added = NULL. Use MAKE_STD_ZVAL
 3. get_zval -> *last_added = NULL. Use MAKE_STD_ZVAL
 4. get_zval -> *last_added = NULL. Use MAKE_STD_ZVAL
    0x0
	0x0
	0x0
	0x0 
	0x0
	---
    empty_position, always 0x0 <-- last_added

 5. free_zval -> if (free_items++ != max_items) {// we can add more
                   *(--last_added) = zval_ptr;
				}
	(memory addresses increase downwards)
    0x0
	0x0
	0x0
	0x0 
	0xA <-- last_added
	---
	0x0

 6. free_zval -> if (free_items++ != max_items) {// we can add more
                   *(--last_added) = zval_ptr;
				}
    0x0
	0x0
	0x0
	0xB <-- last_added
	0xA 
	---
	0x0

 7. free_zval -> if (free_items++ != max_items) {// we can add more
                   *(--last_added) = zval_ptr;
				}
    0x0
	0x0
	0xC <-- last_added
	0xB 
	0xA 
	---
	0x0

 8. get_zval -> *last_added != NULL. -> p = *last_added; *last_added++ = NULL;
    0x0
	0x0
	0x0 
	0xB <-- last_added
	0xA 
	---
	0x0

 9. get_zval -> *last_added != NULL. -> p = *last_added; *last_added++ = NULL;
    0x0
	0x0
	0x0 
	0x0 
	0xA <-- last_added
	---
	0x0

10. get_zval -> *last_added != NULL. -> p = *last_added; *last_added++ = NULL;
    0x0
	0x0
	0x0 
	0x0 
	0x0 
	---
	0x0 <-- last_added

*/


zval * mysqlnd_alloc_get_zval(MYSQLND_ZVAL_CACHE * const cache)
{
	zval *ret = NULL;

#ifndef MYSQLND_SILENT
	php_printf("[mysqlnd_alloc_get_zval %p] *last_added=%p free_items=%d  ", cache, cache? cache->free_list->last_added:NULL, cache->free_items);
#endif

	if (cache) {
		if ((ret = *cache->free_list->last_added)) {
			*cache->free_list->last_added++ = NULL;

			--cache->free_items;
			++cache->get_hits;
		} else {
			++cache->get_misses;
		}
	}
	if (!ret) {
		ALLOC_ZVAL(ret);
	}
	INIT_PZVAL(ret);

#ifndef MYSQLND_SILENT
	php_printf("ret=%p\n", ret);
#endif
	return ret;
}

static
void mysqlnd_alloc_cache_prealloc(MYSQLND_ZVAL_CACHE * const cache, unsigned int count)
{
	zval *data;
	cache->free_items = count;
	while (count--) {
		MAKE_STD_ZVAL(data);
		ZVAL_NULL(data);
#ifndef MYSQLND_SILENT
		php_printf("[mysqlnd_alloc_prealloc %p] items=%d data=%p\n", cache, cache->free_items, data);
#endif
		
		*(--cache->free_list->last_added) = data;
	}
}

void mysqlnd_alloc_zval_ptr_dtor(zval **zv, MYSQLND_ZVAL_CACHE * const cache)
{
	if (!cache || Z_REFCOUNT_PP(zv) > 1 || cache->max_items == cache->free_items) {
#ifndef MYSQLND_SILENT
		php_printf("[mysqlnd_alloc_zval_ptr_dtor %p]1 last_added-1=%p *zv=%p\n", cache->free_list->last_added, *zv);
#endif
		/* We can't cache zval's with refcount > 1 */
		zval_ptr_dtor(zv);
		if (cache) {
			if (cache->max_items == cache->free_items) {
				++cache->put_full_misses;
			} else {
				++cache->put_refcount_misses;
			}
		}
	} else {
		/* refcount is 1 and there is place. Go, cache it! */
		++cache->free_items;
		zval_dtor(*zv);
		ZVAL_NULL(*zv);
		*(--cache->free_list->last_added) = *zv;
		++cache->put_hits;
	}
#ifndef MYSQLND_SILENT
	php_printf("[mysqlnd_alloc_zval_ptr_dtor %p] free_items=%d\n", cache, cache->free_items);
#endif
}


MYSQLND_ZVAL_CACHE* mysqlnd_alloc_init_cache(void)
{
	MYSQLND_ZVAL_CACHE *ret = ecalloc(1, sizeof(MYSQLND_ZVAL_CACHE));

#ifndef MYSQLND_SILENT
	php_printf("[mysqlnd_alloc_init_cache %p]\n", ret);
#endif

	ret->max_items = MYSQLND_ZVALS_MAX_CACHE;
	ret->free_items = 0;
	ret->references = 1;

	/* Let's have always one, so we don't need to do a check in get_zval */
	ret->free_list		= ecalloc(1, sizeof(struct st_mysqlnd_zval_list));

	/* One more for empty position of last_added */
	ret->free_list->ptr_line = ecalloc(ret->max_items + 1, sizeof(zval *));
	ret->free_list->last_added = ret->free_list->ptr_line + ret->max_items;

	mysqlnd_alloc_cache_prealloc(ret, (ret->max_items / 100) * 100);

	return ret;
}


MYSQLND_ZVAL_CACHE* mysqlnd_alloc_get_cache_reference(MYSQLND_ZVAL_CACHE *cache)
{
	if (cache) {
		cache->references++;
	}
	return cache;
}


static
void mysqlnd_alloc_free_cache(MYSQLND_ZVAL_CACHE *cache)
{
#ifndef MYSQLND_SILENT
	uint i = 0;
	php_printf("[mysqlnd_alloc_free_cache %p]\n", cache);
#endif

	while (*cache->free_list->last_added) {
#ifndef MYSQLND_SILENT
		php_printf("\t[free_item %d  %p]\n", i++, *cache->free_list->last_added);
#endif
		zval_ptr_dtor(cache->free_list->last_added);
		cache->free_list->last_added++;
	}
#ifndef MYSQLND_DONT_DUMP_STATS
	php_printf("CACHE STATS:\n\tGET\n\t\tHITS:%lu\n\t\tMISSES=%lu\n\t\tHIT RATIO=%1.3f\n\t"
				"PUT\n\t\tHITS:%lu\n\t\tFULL_MISS=%lu\n\t\tREFC_MISS=%lu\n\t\tHIT RATIO=%1.3f\n\n",
		cache->get_hits, cache->get_misses, (1.0*cache->get_hits/(cache->get_hits + cache->get_misses)),
		cache->put_hits, cache->put_full_misses, cache->put_refcount_misses,
		(1.0 * cache->put_hits / (cache->put_hits + cache->put_full_misses + cache->put_refcount_misses)));
#endif
	efree(cache->free_list->ptr_line);
	efree(cache->free_list);
	efree(cache);
}



void mysqlnd_alloc_free_cache_reference(MYSQLND_ZVAL_CACHE **cache)
{
#ifndef MYSQLND_SILENT
	php_printf("[mysqlnd_alloc_free_cache_reference %p] refs=%d\n", *cache, (*cache)->references);
#endif
	if (*cache && --(*cache)->references == 0) {
		mysqlnd_alloc_free_cache(*cache);
	}
	*cache = NULL;
}

/*
 * Local variables:
 * tab-width: 4
 * c-basic-offset: 4
 * End:
 * vim600: noet sw=4 ts=4 fdm=marker
 * vim<600: noet sw=4 ts=4
 */