Merge pull request #2301 from sonndinh/remove-dup-reactor-functions
[ACE_TAO.git] / TAO / tests / CDR / allocator.cpp
blobcb1b4c686deb797427d162fc5906b74f2a25a871
2 //=============================================================================
3 /**
4 * @file allocator.cpp
6 * Compares the performance of a TSS allocator, with no locks, to
7 * the global allocator (with locks) even in the abscence of
8 * contention.
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"
19 #include "tao/ORB.h"
21 #include "ace/Get_Opt.h"
22 #include "ace/High_Res_Timer.h"
24 #define DEFAULT_BUFFER_SIZE 512
26 /**
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
32 * memory.
34 class Application_Simulator
36 public:
37 /// Constructor, limits the amount of fragmentation and memory this
38 /// class takes.
39 Application_Simulator (int max_fragments,
40 int max_fragment_size);
42 /// Destructor, releases any memory left behind.
43 ~Application_Simulator ();
45 /**
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);
52 private:
53 /// The allocated buffers.
54 char** buffers_;
56 /// The size of the <buffers_> array.
57 int max_fragments_;
59 /// The maximum size of any element of <buffers_>
60 int max_fragment_size_;
63 int
64 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
66 try
68 CORBA::ORB_var orb =
69 CORBA::ORB_init (argc, argv);
71 int tss = 0;
72 int iterations = 500;
73 int repeat = 100;
74 int max_fragments = 2048;
75 int max_fragment_size = 1024;
76 int max_arguments = 16;
77 int max_argument_size = 1024;
78 int quiet = 0;
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"));
82 int opt;
84 while ((opt = get_opt ()) != EOF)
86 switch (opt)
88 case 't':
89 tss = 1;
90 break;
91 case 'n':
92 iterations = ACE_OS::atoi (get_opt.opt_arg ());
93 break;
94 case 'r':
95 repeat = ACE_OS::atoi (get_opt.opt_arg ());
96 break;
97 case 'f':
98 max_fragments = ACE_OS::atoi (get_opt.opt_arg ());
99 break;
100 case 'm':
101 max_fragment_size = ACE_OS::atoi (get_opt.opt_arg ());
102 break;
103 case 's':
104 seed = ACE_OS::atoi (get_opt.opt_arg ());
105 break;
106 case 'a':
107 max_arguments = ACE_OS::atoi (get_opt.opt_arg ());
108 break;
109 case 'b':
110 max_argument_size = ACE_OS::atoi (get_opt.opt_arg ());
111 break;
112 case 'q':
113 quiet = 1;
114 break;
115 case '?':
116 default:
117 ACE_DEBUG ((LM_DEBUG,
118 "Usage: %s "
119 "-n iterations "
120 "-n repeat "
121 "-f max_fragments "
122 "-m max_fragment_size "
123 "-s seed "
124 "-a max_arguments "
125 "-b max_argument_size "
126 "\n",
127 argv[0]));
128 return -1;
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 ();
138 if (tss)
140 buffer_allocator =
141 TAO_ORB_Core_instance ()->output_cdr_buffer_allocator ();
142 dblock_allocator =
143 TAO_ORB_Core_instance ()->output_cdr_dblock_allocator ();
146 Application_Simulator simulator (max_fragments,
147 max_fragment_size);
148 char* argument_buffer;
149 ACE_NEW_RETURN (argument_buffer, char[max_argument_size], 1);
151 int* argument_sizes;
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,
171 buffer_allocator,
172 dblock_allocator);
174 for (int k = 0; k < n; ++k)
176 cdr.write_char_array (argument_buffer,
177 argument_sizes[k]);
180 cdr_encoding.stop_incr ();
183 ACE_Time_Value tv;
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);
187 usecs += tv.usec ();
188 double average =
189 static_cast<double> (ACE_HRTIME_CONVERSION(usecs)) / repeat;
191 if (!quiet)
192 ACE_OS::printf ("AVE: %d %f\n",
193 i, average);
196 catch (const CORBA::Exception& ex)
198 ex._tao_print_exception ("Caught unexpected CORBA exception:");
200 return 1;
202 return 0;
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_;
213 ++i)
214 *i = 0;
217 Application_Simulator::~Application_Simulator ()
219 for (char** i = this->buffers_;
220 i != this->buffers_ + this->max_fragments_;
221 ++i)
223 if (*i != 0)
225 delete[] *i;
226 *i = 0;
229 delete[] this->buffers_;
230 this->buffers_ = 0;
233 void
234 Application_Simulator::upcall (unsigned int* seed)
236 for (char** i = this->buffers_;
237 i != this->buffers_ + this->max_fragments_;
238 ++i)
240 if (*i != 0)
242 if (ACE_OS::rand_r (seed) % 10000 < 5000)
244 delete[] *i;
245 *i = 0;
248 else
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]);