2 //=============================================================================
6 * Compares the performance of a TSS allocator, with no locks, to
7 * the global allocator (with locks) even in the abscence of
9 * The idea behind this test is to measure the predictability of
10 * each allocator, specially under the light of potential
11 * fragmentation in the main allocator.
13 * @author Carlos O'Ryan
15 //=============================================================================
18 #include "tao/ORB_Core.h"
21 #include "ace/Get_Opt.h"
22 #include "ace/High_Res_Timer.h"
24 #define DEFAULT_BUFFER_SIZE 512
27 * @class Application_Simulator
29 * Tries to simulate the behavior of an application: it randomly
30 * acquires and releases memory, of variable sizes.
31 * The intention is to produce some level of fragmentation in main
34 class Application_Simulator
38 /// Constructor, limits the amount of fragmentation and memory this
40 Application_Simulator (int max_fragments
,
41 int max_fragment_size
);
43 /// Destructor, releases any memory left behind.
44 ~Application_Simulator (void);
47 * Simulate an upcall. The class allocates some memory and then
48 * releases some memory too, the amount of memory allocated and the
49 * number of allocations is random.
51 void upcall (unsigned int* seed
);
54 /// The allocated buffers.
57 /// The size of the <buffers_> array.
60 /// The maximum size of any element of <buffers_>
61 int max_fragment_size_
;
65 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
70 CORBA::ORB_init (argc
, argv
);
75 int max_fragments
= 2048;
76 int max_fragment_size
= 1024;
77 int max_arguments
= 16;
78 int max_argument_size
= 1024;
80 unsigned int seed
= static_cast<unsigned int> (ACE_OS::time(0));
82 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("tn:f:m:s:a:b:r:q"));
85 while ((opt
= get_opt ()) != EOF
)
93 iterations
= ACE_OS::atoi (get_opt
.opt_arg ());
96 repeat
= ACE_OS::atoi (get_opt
.opt_arg ());
99 max_fragments
= ACE_OS::atoi (get_opt
.opt_arg ());
102 max_fragment_size
= ACE_OS::atoi (get_opt
.opt_arg ());
105 seed
= ACE_OS::atoi (get_opt
.opt_arg ());
108 max_arguments
= ACE_OS::atoi (get_opt
.opt_arg ());
111 max_argument_size
= ACE_OS::atoi (get_opt
.opt_arg ());
118 ACE_DEBUG ((LM_DEBUG
,
123 "-m max_fragment_size "
126 "-b max_argument_size "
133 ACE_DEBUG ((LM_DEBUG
, "SEED = %d\n", seed
));
135 ACE_Allocator
* buffer_allocator
=
136 ACE_Allocator::instance ();
137 ACE_Allocator
* dblock_allocator
=
138 ACE_Allocator::instance ();
142 TAO_ORB_Core_instance ()->output_cdr_buffer_allocator ();
144 TAO_ORB_Core_instance ()->output_cdr_dblock_allocator ();
147 Application_Simulator
simulator (max_fragments
,
149 char* argument_buffer
;
150 ACE_NEW_RETURN (argument_buffer
, char[max_argument_size
], 1);
153 ACE_NEW_RETURN (argument_sizes
, int[max_arguments
], 1);
155 int n
= ACE_OS::rand_r (&seed
) % max_arguments
+ 1;
156 for (int k
= 0; k
< n
; ++k
)
157 argument_sizes
[k
] = ACE_OS::rand_r (&seed
) % max_argument_size
+ 1;
159 for (int i
= 0; i
< iterations
; ++i
)
161 simulator
.upcall (&seed
);
163 // @@ TODO this is the place to put the other allocators.
164 ACE_High_Res_Timer cdr_encoding
;
165 for (int j
= 0; j
< repeat
; ++j
)
167 cdr_encoding
.start_incr ();
169 char buffer
[DEFAULT_BUFFER_SIZE
];
170 ACE_OutputCDR
cdr (buffer
, sizeof(buffer
),
171 TAO_ENCAP_BYTE_ORDER
,
175 for (int k
= 0; k
< n
; ++k
)
177 cdr
.write_char_array (argument_buffer
,
181 cdr_encoding
.stop_incr ();
185 cdr_encoding
.elapsed_time_incr (tv
);
186 ACE_hrtime_t usecs
= tv
.sec ();
187 usecs
*= static_cast<ACE_UINT32
> (ACE_ONE_SECOND_IN_USECS
);
190 static_cast<double> (ACE_HRTIME_CONVERSION(usecs
)) / repeat
;
193 ACE_OS::printf ("AVE: %d %f\n",
198 catch (const CORBA::Exception
& ex
)
200 ex
._tao_print_exception ("Caught unexpected CORBA exception:");
207 Application_Simulator::Application_Simulator (int max_fragments
,
208 int max_fragment_size
)
209 : max_fragments_ (max_fragments
),
210 max_fragment_size_ (max_fragment_size
)
212 ACE_NEW (buffers_
, char*[this->max_fragments_
]);
213 for (char** i
= this->buffers_
;
214 i
!= this->buffers_
+ this->max_fragments_
;
219 Application_Simulator::~Application_Simulator (void)
221 for (char** i
= this->buffers_
;
222 i
!= this->buffers_
+ this->max_fragments_
;
231 delete[] this->buffers_
;
236 Application_Simulator::upcall (unsigned int* seed
)
238 for (char** i
= this->buffers_
;
239 i
!= this->buffers_
+ this->max_fragments_
;
244 if (ACE_OS::rand_r (seed
) % 10000 < 5000)
252 if (ACE_OS::rand_r (seed
) % 10000 < 5000)
254 int size
= ACE_OS::rand_r (seed
) %
255 this->max_fragment_size_
+ 1;
256 ACE_NEW (*i
, char[size
]);