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
|
//////////////////////////////////////////////////////////////////////////////
//
// (C) Copyright Ion Gaztanaga 2007-2013. Distributed under the Boost
// Software License, Version 1.0. (See accompanying file
// LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See http://www.boost.org/libs/container for documentation.
//
//////////////////////////////////////////////////////////////////////////////
#ifdef _MSC_VER
#pragma warning (disable : 4512)
#endif
#include <boost/container/detail/dlmalloc.hpp>
#define BOOST_INTERPROCESS_VECTOR_ALLOC_STATS
#include <iostream> //std::cout, std::endl
#include <typeinfo> //typeid
#include <cassert> //assert
#include <boost/timer/timer.hpp>
using boost::timer::cpu_timer;
using boost::timer::cpu_times;
using boost::timer::nanosecond_type;
using namespace boost::container;
template <class POD>
void allocation_timing_test(unsigned int num_iterations, unsigned int num_elements)
{
size_t capacity = 0;
unsigned int numalloc = 0, numexpand = 0;
std::cout
<< " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"
<< " Iterations/Elements: " << num_iterations << "/" << num_elements << '\n'
<< " ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ \n"
<< std::endl;
allocation_type malloc_types[] = { BOOST_CONTAINER_EXPAND_BWD, BOOST_CONTAINER_EXPAND_FWD, BOOST_CONTAINER_ALLOCATE_NEW };
const char * malloc_names[] = { "Backwards expansion", "Forward expansion", "New allocation" };
for(size_t i = 0; i < sizeof(malloc_types)/sizeof(allocation_type); ++i){
numalloc = 0; numexpand = 0;
const allocation_type m_mode = malloc_types[i];
const char *malloc_name = malloc_names[i];
cpu_timer timer;
timer.resume();
for(unsigned int r = 0; r != num_iterations; ++r){
void *first_mem = 0;
if(m_mode != BOOST_CONTAINER_EXPAND_FWD)
first_mem = dlmalloc_malloc(sizeof(POD)*num_elements*3/2);
void *addr = dlmalloc_malloc(1*sizeof(POD));
if(m_mode == BOOST_CONTAINER_EXPAND_FWD)
first_mem = dlmalloc_malloc(sizeof(POD)*num_elements*3/2);
capacity = dlmalloc_size(addr)/sizeof(POD);
dlmalloc_free(first_mem);
++numalloc;
try{
dlmalloc_command_ret_t ret;
for(size_t e = capacity + 1; e < num_elements; ++e){
size_t received_size;
size_t min = (capacity+1)*sizeof(POD);
size_t max = (capacity*3/2)*sizeof(POD);
if(min > max)
max = min;
ret = dlmalloc_allocation_command
( m_mode, sizeof(POD)
, min, max, &received_size, addr);
if(!ret.first){
std::cout << "(!ret.first)!" << std::endl;
throw int(0);
}
if(!ret.second){
assert(m_mode == BOOST_CONTAINER_ALLOCATE_NEW);
if(m_mode != BOOST_CONTAINER_ALLOCATE_NEW){
std::cout << "m_mode != BOOST_CONTAINER_ALLOCATE_NEW!" << std::endl;
return;
}
dlmalloc_free(addr);
addr = ret.first;
++numalloc;
}
else{
assert(m_mode != BOOST_CONTAINER_ALLOCATE_NEW);
if(m_mode == BOOST_CONTAINER_ALLOCATE_NEW){
std::cout << "m_mode == BOOST_CONTAINER_ALLOCATE_NEW!" << std::endl;
return;
}
++numexpand;
}
capacity = received_size/sizeof(POD);
addr = ret.first;
e = capacity + 1;
}
dlmalloc_free(addr);
}
catch(...){
dlmalloc_free(addr);
throw;
}
}
assert( dlmalloc_allocated_memory() == 0);
if(dlmalloc_allocated_memory()!= 0){
std::cout << "Memory leak!" << std::endl;
return;
}
timer.stop();
nanosecond_type nseconds = timer.elapsed().wall;
std::cout << " Malloc type: " << malloc_name
<< std::endl
<< " allocation ns: "
<< float(nseconds)/(num_iterations*num_elements)
<< std::endl
<< " capacity - alloc calls (new/expand): "
<< (unsigned int)capacity << " - "
<< (float(numalloc) + float(numexpand))/num_iterations
<< "(" << float(numalloc)/num_iterations << "/" << float(numexpand)/num_iterations << ")"
<< std::endl << std::endl;
dlmalloc_trim(0);
}
}
template<unsigned N>
struct char_holder
{
char ints_[N];
};
template<class POD>
int allocation_loop()
{
std::cout << std::endl
<< "-------------------------------------------\n"
<< "-------------------------------------------\n"
<< " Type(sizeof): " << typeid(POD).name() << " (" << sizeof(POD) << ")\n"
<< "-------------------------------------------\n"
<< "-------------------------------------------\n"
<< std::endl;
//#define SINGLE_TEST
#define SIMPLE_IT
#ifdef SINGLE_TEST
#ifdef NDEBUG
unsigned int numrep [] = { 50000 };
#else
unsigned int numrep [] = { 5000 };
#endif
unsigned int numele [] = { 100 };
#elif defined(SIMPLE_IT)
unsigned int numrep [] = { 3 };
unsigned int numele [] = { 100 };
#else
#ifdef NDEBUG
unsigned int numrep [] = { /*10000, */10000, 100000, 1000000 };
#else
unsigned int numrep [] = { /*10000, */1000, 10000, 100000 };
#endif
unsigned int numele [] = { /*10000, */1000, 100, 10 };
#endif
for(unsigned int i = 0; i < sizeof(numele)/sizeof(numele[0]); ++i){
allocation_timing_test<POD>(numrep[i], numele[i]);
}
return 0;
}
int main()
{
dlmalloc_mallopt( (-3)//M_MMAP_THRESHOLD
, 100*10000000);
//allocation_loop<char_holder<4> >();
//allocation_loop<char_holder<6> >();
allocation_loop<char_holder<8> >();
allocation_loop<char_holder<12> >();
//allocation_loop<char_holder<14> >();
allocation_loop<char_holder<24> >();
return 0;
}
|