summaryrefslogtreecommitdiff
path: root/lib/tools/c_src/erl_memory_trace_block_table.c
blob: ca7cb45a0ee9b0d8071d5ab42c3b6ac02d7a4b36 (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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
/* ``Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 * The Initial Developer of the Original Code is Ericsson Utvecklings AB.
 * Portions created by Ericsson are Copyright 1999, Ericsson Utvecklings
 * AB. All Rights Reserved.''
 * 
 *     $Id$
 */


/*
 * Description:	
 *
 * Author: 	Rickard Green
 */

/* Headers to include ... */

#ifdef HAVE_CONFIG_H
#	include "config.h"
#endif

#include "erl_memory_trace_block_table.h"
#include <errno.h>

#undef HARD_DEBUG
#undef REALLY_HARD_DEBUG
#ifdef DEBUG
#  define HARD_DEBUG 0
#  define REALLY_HARD_DEBUG 0
#else
#  define HARD_DEBUG 0
#  define REALLY_HARD_DEBUG 0
#endif

/* Some system specific defines ... */
#if defined(__WIN32__) && !defined(__GNUC__)
#	define INLINE __forceinline
#else
#	ifdef __GNUC__
#		define INLINE __inline__
#	else
#		define INLINE
#	endif
#endif

/* Our own assert() ... */
#ifdef DEBUG
#define ASSERT(A) ((void) ((A) ? 1 : assert_failed(__FILE__, __LINE__, #A)))
#include <stdio.h>
static int assert_failed(char *f, int l, char *a)
{
    fprintf(stderr, "%s:%d: Assertion failed: %s\n", f, l, a);
    abort();
    return 0;
}

#else
#define ASSERT(A) ((void) 1)
#endif


#define EMTBT_BLOCKS_PER_POOL 1000

typedef struct emtbt_block_pool_ {
    struct emtbt_block_pool_ *next;
    emtbt_block blocks[1];
} emtbt_block_pool;

struct emtbt_table_ {
    void * (*alloc)(size_t);
    void * (*realloc)(void *, size_t);
    void   (*free)(void *);
    int is_64_bit;
    int no_blocks;
    int no_of_buckets;
    int max_used_buckets;
    int min_used_buckets;
    int used_buckets;
    int current_size_index;
    emtbt_block *blocks;
    emtbt_block ** buckets;


    /* Fixed size allocation of blocks */
    emtbt_block_pool *block_pools;
    emtbt_block *free_blocks;
    int blocks_per_pool;

};


static emtbt_block null_blk = {0};

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *\
 * Block table                                                             *
 *                                                                         *
\*                                                                         */

#if HARD_DEBUG
static void check_table(emtbt_table *table);
#endif

static emtbt_block *
block_alloc_new_pool(emtbt_table *tab)
{
    size_t size;
    emtbt_block_pool *poolp;

    size = sizeof(emtbt_block_pool) - sizeof(emtbt_block);
    size += tab->blocks_per_pool*sizeof(emtbt_block);

    poolp = (*tab->alloc)(size);

    if (poolp) {
	int i;
	emtbt_block *blks;

	poolp->next = tab->block_pools;
	tab->block_pools = poolp;

	blks = (emtbt_block *) poolp->blocks;

	for (i = 1; i < tab->blocks_per_pool - 1; i++)
	    blks[i].next = &blks[i + 1];
	blks[tab->blocks_per_pool - 1].next = NULL;
	tab->free_blocks = &blks[1];

	return &blks[0];
    }
    return NULL;
}

static INLINE emtbt_block *
block_alloc(emtbt_table *tab)
{
    emtbt_block *res;
#if HARD_DEBUG
    check_table(tab);
#endif

    if (tab->free_blocks) {
	res = tab->free_blocks;
	tab->free_blocks = tab->free_blocks->next;
    }
    else {
	res = block_alloc_new_pool(tab);
    }

#ifdef DEBUG
    res->next = ((emtbt_block *) 0xfffffff0);
    res->prev = ((emtbt_block *) 0xfffffff0);
    res->bucket = ((emtbt_block **) 0xfffffff0);
#endif

#if HARD_DEBUG
    check_table(tab);
#endif

    return res;
}

static INLINE void
block_free(emtbt_table *tab, emtbt_block *bp)
{

#if HARD_DEBUG
    check_table(tab);
#endif

    bp->next = tab->free_blocks;
    tab->free_blocks = bp;

#if HARD_DEBUG
    check_table(tab);
#endif


}

#define PRIME0 ((usgnd_int_32) 268438039)
#define PRIME1 ((usgnd_int_32) 268440479)
#define PRIME2 ((usgnd_int_32) 268439161)
#define PRIME3 ((usgnd_int_32) 268437017)

#define MK_HASH(H, P, IS64)						\
do {									\
    (H) = (P) & 0xff;							\
    (H) *= PRIME0;							\
    (H) += ((P) >> 8) & 0xff;						\
    (H) *= PRIME1;							\
    (H) += ((P) >> 16) & 0xff;						\
    (H) *= PRIME2;							\
    (H) += ((P) >> 24) & 0xff;						\
    (H) *= PRIME3;							\
    if ((IS64)) {							\
	(H) += ((P) >> 32) & 0xff;					\
	(H) *= PRIME0;							\
	(H) += ((P) >> 40) & 0xff;					\
	(H) *= PRIME1;							\
	(H) += ((P) >> 48) & 0xff;					\
	(H) *= PRIME2;							\
	(H) += ((P) >> 56) & 0xff;					\
	(H) *= PRIME3;							\
    }									\
} while (0)

static const int table_sizes[] = {
    3203,
    4813,
    6421,
    9643,
    12853,
    19289,
    25717,
    51437,
    102877,
    205759,
    411527,
    823117,
    1646237,
    3292489,
    6584983,
    13169977,
    26339969,
    52679969
};

#if HARD_DEBUG

static void
check_table(emtbt_table *table)
{
    int no_blocks;
    emtbt_block *block, *prev_block;

    no_blocks = 0;
    block = table->blocks;
    ASSERT(!block || !block->prev);
    prev_block = NULL;
    while (block) {
	usgnd_int_32 hash;
	MK_HASH(hash, block->pointer, table->is_64_bit);
	ASSERT(hash == block->hash);
	ASSERT(block->bucket - table->buckets
	       == hash % table->no_of_buckets);
	ASSERT(!prev_block || prev_block == block->prev);
	prev_block = block;
	block = block->next;
	no_blocks++;
	ASSERT(table->no_blocks >= no_blocks);
    }

    ASSERT(table->no_blocks == no_blocks);

#if REALLY_HARD_DEBUG
    {
	int i;
	for (i = 0; i < table->no_of_buckets; i++) {
	    int bucket_end_found;
	    emtbt_block **bucket;
	    if (!table->buckets[i])
		continue;
	    bucket_end_found = 0;
	    bucket = &table->buckets[i];
	    for (block = table->blocks; block; block = block->next) {
		if (block->bucket == bucket) {
		    if (!block->prev || block->prev->bucket != bucket)
			ASSERT(*bucket == block);
		    if (!block->next || block->next->bucket != bucket)
			bucket_end_found++;
		}
	    }
	    ASSERT(bucket_end_found);
	}
    }
#endif

}

#endif

static INLINE void
link_block(emtbt_table *table, emtbt_block **bucket, emtbt_block *block)
{
    ASSERT(bucket);

    block->bucket = bucket;
    if (*bucket) {
	block->next = *bucket;
	block->prev = (*bucket)->prev;
	if (block->prev)
	    block->prev->next = block;
	else
	    table->blocks = block;
	block->next->prev = block;
    }
    else {
	block->next = table->blocks;
	block->prev = NULL;
	if (table->blocks)
	    table->blocks->prev = block;
	table->blocks = block;
	table->used_buckets++;

    }
    *bucket = block;
    table->no_blocks++;

#if HARD_DEBUG
    check_table(table);
#endif

}

static int
resize_table(emtbt_table *table, int new_no_of_buckets)
{
#ifdef DEBUG
    int org_no_blocks;
#endif
    int i;
    emtbt_block *block;
    emtbt_block **buckets;

    if (new_no_of_buckets < table->no_of_buckets) {
	/* shrink never fails */
	buckets = (emtbt_block **) (*table->realloc)(table->buckets,
						     (sizeof(emtbt_block *)
						      * new_no_of_buckets));
	if (!buckets)
	    return 1;
    }
    else if (new_no_of_buckets > table->no_of_buckets) {
	(*table->free)((void *) table->buckets);
	buckets = (emtbt_block **) (*table->alloc)((sizeof(emtbt_block *)
						    * new_no_of_buckets));
	if (!buckets)
	    return 0;
    }
    else
	return 1;

    table->buckets = buckets;
    table->no_of_buckets = new_no_of_buckets;
    table->max_used_buckets = (4*new_no_of_buckets)/5;
    table->min_used_buckets = new_no_of_buckets/5;
    table->used_buckets = 0;

#ifdef DEBUG
    org_no_blocks = table->no_blocks;
#endif

    table->no_blocks = 0;
    

    for (i = 0; i < new_no_of_buckets; i++)
	buckets[i] = NULL;

    block = table->blocks;
    table->blocks = NULL;

    while (block) {
	emtbt_block *next_block = block->next;
	link_block(table,&table->buckets[block->hash%new_no_of_buckets],block);
	block = next_block;
    }

    ASSERT(org_no_blocks == table->no_blocks);

    return 1;
}

static INLINE int
grow_table(emtbt_table *table)
{
    if (table->current_size_index < sizeof(table_sizes)/sizeof(int)) {
	int new_size;
	table->current_size_index++;
	new_size = table_sizes[table->current_size_index];
	ASSERT(new_size > 0);
	return resize_table(table, new_size);
    }
    return 1;
}

static INLINE void
shrink_table(emtbt_table *table)
{
    if (table->current_size_index > 0) {
	int new_size;
	table->current_size_index--;
	new_size = table_sizes[table->current_size_index];
	ASSERT(new_size > 0);
	(void) resize_table(table, new_size);
    }
}

static INLINE emtbt_block *
peek_block(emtbt_table *table, usgnd_int_max ptr)
{
    emtbt_block **bucket;
    emtbt_block *block;
    usgnd_int_32 hash;

    MK_HASH(hash, ptr, table->is_64_bit);

    bucket = &table->buckets[hash % table->no_of_buckets];
    block = *bucket;
    if (!block)
	return NULL;

    while (block->bucket == bucket) {
	ASSERT(block);
	if (block->pointer == ptr)
	    return block;
	if (!block->next)
	    break;
	block = block->next;
    }
    return NULL;
}

static INLINE int
insert_block(emtbt_table *table, emtbt_block *block)
{
    emtbt_block **bucket;
    emtbt_block *tmp_block;
    usgnd_int_32 hash;
    usgnd_int_max p;

#if HARD_DEBUG
    check_table(table);
#endif

    if (table->used_buckets >= table->max_used_buckets) {
	if(!grow_table(table))
	    return -1;
    }

    p = block->pointer;

    MK_HASH(hash, p, table->is_64_bit);
    block->hash = hash;

    bucket = &table->buckets[hash % table->no_of_buckets];
    tmp_block = *bucket;
    if (tmp_block) {
	while (tmp_block->bucket == bucket) {
	    if (tmp_block->pointer == p)
		return 0;
	    if (!tmp_block->next)
		break;
	    tmp_block = tmp_block->next;
	}
    }

    link_block(table, bucket, block);

    ASSERT(block == peek_block(table, p));


    return 1;
}

static INLINE void
delete_block(emtbt_table *table, emtbt_block *block)
{
    emtbt_block **bucket;

    if (!block)
	return;

#if HARD_DEBUG
    check_table(table);
#endif

    bucket = block->bucket;
    ASSERT(bucket);

    if (block->prev)
	block->prev->next = block->next;
    else
	table->blocks = block->next;

    if (block->next)
	block->next->prev = block->prev;

    if (block == *bucket) {
	ASSERT(!block->prev || block->prev->bucket != bucket);
	if (block->next && block->next->bucket == bucket)
	    *bucket = block->next;
	else {
	    ASSERT(table->used_buckets > 0);
	    *bucket = NULL;
	    table->used_buckets--;
	}
    }
#ifdef DEBUG

    block->next = ((emtbt_block *) 0xfffffff0);
    block->prev = ((emtbt_block *) 0xfffffff0);
    block->bucket = ((emtbt_block **) 0xfffffff0);
#endif

    ASSERT(table->no_blocks > 0);
    table->no_blocks--;

    if (table->used_buckets < table->min_used_buckets)
	shrink_table(table);

#if HARD_DEBUG
    check_table(table);
#endif

}

static INLINE emtbt_block *
fetch_block(emtbt_table *table, usgnd_int_max ptr)
{
    emtbt_block *block;

    block = peek_block(table, ptr);
    delete_block(table, block);
    return block;
}


const char *emtbt_error_string(int error)
{
    switch (error) {
    case EMTBT_ALLOC_XBLK_ERROR:
	return "Allocation to an already existing block";
    case EMTBT_REALLOC_NOBLK_ERROR:
	return "Reallocation of non-existing block";
    case EMTBT_REALLOC_XBLK_ERROR:
	return "Reallocation to an already existing block";
    case EMTBT_REALLOC_BLK_TYPE_MISMATCH:
	return "Block types mismatch when reallocating";
    case EMTBT_FREE_NOBLK_ERROR:
	return "Deallocation of non-existing block";
    case EMTBT_FREE_BLK_TYPE_MISMATCH:
	return "Block types mismatch when deallocating";
    case EMTBT_INTERNAL_ERROR:
	return "Block table internal error";
    default:
	return NULL;
    }


}


emtbt_table *
emtbt_new_table(int is_64_bit,
		void * (*alloc)(size_t),
		void * (*realloc)(void *, size_t),
		void   (*free)(void *))
{
    emtbt_table *tab = (*alloc)(sizeof(emtbt_table));
    if (tab) {
	tab->alloc = alloc;
	tab->realloc = realloc;
	tab->free = free;
	tab->is_64_bit = is_64_bit;
	tab->no_blocks = 0;
	tab->no_of_buckets = 0;
	tab->max_used_buckets = 0;
	tab->min_used_buckets = 0;
	tab->used_buckets = 0;
	tab->current_size_index = 0;
	tab->blocks = NULL;
	tab->buckets = NULL;

	tab->block_pools = NULL;
	tab->free_blocks = NULL;
	tab->blocks_per_pool = EMTBT_BLOCKS_PER_POOL;

    }
    return tab;
}

void
emtbt_destroy_table(emtbt_table *tab)
{
    void (*freep)(void *);
    emtbt_block_pool *poolp1, *poolp2;

    freep = tab->free;

    /* Free block pools */
    poolp1 = tab->block_pools;
    while (poolp1) {
	poolp2 = poolp1;
	poolp1 = poolp1->next;
	(*freep)((void *) poolp2);
    }

    if (tab->buckets)
	(*freep)((void *) tab->buckets);

    (*freep)((void *) tab);
}


#define CP_BLK(TO, FROM)						\
do {									\
    (TO)->time.secs	= (FROM)->time.secs;				\
    (TO)->time.usecs	= (FROM)->time.usecs;				\
    (TO)->type		= (FROM)->type;					\
    (TO)->pointer	= (FROM)->pointer;				\
    (TO)->size		= (FROM)->size;					\
} while (0)

int
emtbt_alloc_op(emtbt_table *tab, emtp_operation *op)
{
    int res;
    emtbt_block *blk;

    blk = block_alloc(tab);
    if (!blk)
	return ENOMEM;
	
    blk->time.secs	= op->time.secs;
    blk->time.usecs	= op->time.usecs;
    blk->type		= op->u.block.type;
    blk->pointer	= op->u.block.new_ptr;
    blk->size		= op->u.block.new_size;

    res = insert_block(tab, blk);
    if (res < 0)
	return ENOMEM;
    else if (res == 0)
	return EMTBT_ALLOC_XBLK_ERROR;
    return 0;
}

int
emtbt_realloc_op(emtbt_table *tab, emtp_operation *op, emtbt_block *old_blk)
{
    int res;
    emtbt_block *blk;

    if (!op->u.block.new_size) {
	/* freed block */

	blk = fetch_block(tab, op->u.block.prev_ptr);
	if (!blk)
	    return EMTBT_REALLOC_NOBLK_ERROR;

	CP_BLK(old_blk, blk);
	block_free(tab, blk);
    }
    else {

	if (!op->u.block.new_ptr) {
	    /* failed operation */
	    if (!op->u.block.prev_ptr)
		CP_BLK(old_blk, &null_blk);
	    else {
		blk = peek_block(tab, op->u.block.prev_ptr);
		if (!blk)
		    return EMTBT_REALLOC_NOBLK_ERROR;
		CP_BLK(old_blk, blk);
#if 0
		if (blk->type != op->u.block.type)
		    return EMTBT_REALLOC_BLK_TYPE_MISMATCH;
#endif
	    }
	}
	else if (!op->u.block.prev_ptr) {
	    /* new block */

	    CP_BLK(old_blk, &null_blk);
	    blk = block_alloc(tab);
	    if (!blk)
		return ENOMEM;
	    blk->type		= op->u.block.type;
	    blk->pointer	= op->u.block.new_ptr;
	    blk->time.secs	= op->time.secs;
	    blk->time.usecs	= op->time.usecs;
	    blk->size		= op->u.block.new_size;

	    res = insert_block(tab, blk);
	    if (res < 0)
		return ENOMEM;
	    else if (res == 0)
		return EMTBT_REALLOC_XBLK_ERROR;
	}
	else if (op->u.block.new_ptr == op->u.block.prev_ptr) {
	    /* resized block */
	    blk = peek_block(tab, op->u.block.prev_ptr);
	    if (!blk)
		return EMTBT_REALLOC_NOBLK_ERROR;
	    CP_BLK(old_blk, blk);
	    blk->time.secs	= op->time.secs;
	    blk->time.usecs	= op->time.usecs;
	    blk->size		= op->u.block.new_size;
#if 0
	    if (blk->type != op->u.block.type)
		return EMTBT_REALLOC_BLK_TYPE_MISMATCH;
#endif
	}
	else {
	    /* moved block */
	    blk = fetch_block(tab, op->u.block.prev_ptr);
	    if (!blk)
		return EMTBT_REALLOC_NOBLK_ERROR;
	    CP_BLK(old_blk, blk);
	    blk->time.secs	= op->time.secs;
	    blk->time.usecs	= op->time.usecs;
	    blk->pointer 	= op->u.block.new_ptr;
	    blk->size		= op->u.block.new_size;
	    res = insert_block(tab, blk);
	    if (res < 0)
		return ENOMEM;
	    else if (res == 0)
		return EMTBT_REALLOC_XBLK_ERROR;
#if 0
	    if (blk->type != op->u.block.type)
		return EMTBT_REALLOC_BLK_TYPE_MISMATCH;
#endif
	}
    }   
    return 0;

}


int
emtbt_free_op(emtbt_table *tab, emtp_operation *op, emtbt_block *old_blk)
{
    emtbt_block *blk;

    if (!op->u.block.prev_ptr)
	CP_BLK(old_blk, &null_blk);
    else {

	blk = fetch_block(tab, op->u.block.prev_ptr);
	if (!blk)
	    return EMTBT_FREE_NOBLK_ERROR;

	CP_BLK(old_blk, blk);
	block_free(tab, blk);
#if 0
	if (blk->type != op->u.block.type)
	    return EMTBT_FREE_BLK_TYPE_MISMATCH;
#endif
    }
    return 0;
}