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
37 /// Constructor, limits the amount of fragmentation and memory this
39 Application_Simulator (int max_fragments
,
40 int max_fragment_size
);
42 /// Destructor, releases any memory left behind.
43 ~Application_Simulator ();
46 * Simulate an upcall. The class allocates some memory and then
47 * releases some memory too, the amount of memory allocated and the
48 * number of allocations is random.
50 void upcall (unsigned int* seed
);
53 /// The allocated buffers.
56 /// The size of the <buffers_> array.
59 /// The maximum size of any element of <buffers_>
60 int max_fragment_size_
;
64 ACE_TMAIN(int argc
, ACE_TCHAR
*argv
[])
69 CORBA::ORB_init (argc
, argv
);
74 int max_fragments
= 2048;
75 int max_fragment_size
= 1024;
76 int max_arguments
= 16;
77 int max_argument_size
= 1024;
79 unsigned int seed
= static_cast<unsigned int> (ACE_OS::time(0));
81 ACE_Get_Opt
get_opt (argc
, argv
, ACE_TEXT("tn:f:m:s:a:b:r:q"));
84 while ((opt
= get_opt ()) != EOF
)
92 iterations
= ACE_OS::atoi (get_opt
.opt_arg ());
95 repeat
= ACE_OS::atoi (get_opt
.opt_arg ());
98 max_fragments
= ACE_OS::atoi (get_opt
.opt_arg ());
101 max_fragment_size
= ACE_OS::atoi (get_opt
.opt_arg ());
104 seed
= ACE_OS::atoi (get_opt
.opt_arg ());
107 max_arguments
= ACE_OS::atoi (get_opt
.opt_arg ());
110 max_argument_size
= ACE_OS::atoi (get_opt
.opt_arg ());
117 ACE_DEBUG ((LM_DEBUG
,
122 "-m max_fragment_size "
125 "-b max_argument_size "
132 ACE_DEBUG ((LM_DEBUG
, "SEED = %d\n", seed
));
134 ACE_Allocator
* buffer_allocator
=
135 ACE_Allocator::instance ();
136 ACE_Allocator
* dblock_allocator
=
137 ACE_Allocator::instance ();
141 TAO_ORB_Core_instance ()->output_cdr_buffer_allocator ();
143 TAO_ORB_Core_instance ()->output_cdr_dblock_allocator ();
146 Application_Simulator
simulator (max_fragments
,
148 char* argument_buffer
;
149 ACE_NEW_RETURN (argument_buffer
, char[max_argument_size
], 1);
152 ACE_NEW_RETURN (argument_sizes
, int[max_arguments
], 1);
154 int n
= ACE_OS::rand_r (&seed
) % max_arguments
+ 1;
155 for (int k
= 0; k
< n
; ++k
)
156 argument_sizes
[k
] = ACE_OS::rand_r (&seed
) % max_argument_size
+ 1;
158 for (int i
= 0; i
< iterations
; ++i
)
160 simulator
.upcall (&seed
);
162 // @@ TODO this is the place to put the other allocators.
163 ACE_High_Res_Timer cdr_encoding
;
164 for (int j
= 0; j
< repeat
; ++j
)
166 cdr_encoding
.start_incr ();
168 char buffer
[DEFAULT_BUFFER_SIZE
];
169 ACE_OutputCDR
cdr (buffer
, sizeof(buffer
),
170 TAO_ENCAP_BYTE_ORDER
,
174 for (int k
= 0; k
< n
; ++k
)
176 cdr
.write_char_array (argument_buffer
,
180 cdr_encoding
.stop_incr ();
184 cdr_encoding
.elapsed_time_incr (tv
);
185 ACE_hrtime_t usecs
= tv
.sec ();
186 usecs
*= static_cast<ACE_UINT32
> (ACE_ONE_SECOND_IN_USECS
);
189 static_cast<double> (ACE_HRTIME_CONVERSION(usecs
)) / repeat
;
192 ACE_OS::printf ("AVE: %d %f\n",
196 catch (const CORBA::Exception
& ex
)
198 ex
._tao_print_exception ("Caught unexpected CORBA exception:");
205 Application_Simulator::Application_Simulator (int max_fragments
,
206 int max_fragment_size
)
207 : max_fragments_ (max_fragments
),
208 max_fragment_size_ (max_fragment_size
)
210 ACE_NEW (buffers_
, char*[this->max_fragments_
]);
211 for (char** i
= this->buffers_
;
212 i
!= this->buffers_
+ this->max_fragments_
;
217 Application_Simulator::~Application_Simulator ()
219 for (char** i
= this->buffers_
;
220 i
!= this->buffers_
+ this->max_fragments_
;
229 delete[] this->buffers_
;
234 Application_Simulator::upcall (unsigned int* seed
)
236 for (char** i
= this->buffers_
;
237 i
!= this->buffers_
+ this->max_fragments_
;
242 if (ACE_OS::rand_r (seed
) % 10000 < 5000)
250 if (ACE_OS::rand_r (seed
) % 10000 < 5000)
252 int size
= ACE_OS::rand_r (seed
) %
253 this->max_fragment_size_
+ 1;
254 ACE_NEW (*i
, char[size
]);