Merge pull request #1844 from jrw972/monterey
[ACE_TAO.git] / TAO / tests / CDR / allocator.cpp
blob52446b05ebc404ee4526be45b4f2f02d874aab42
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
37 public:
38 /// Constructor, limits the amount of fragmentation and memory this
39 /// class takes.
40 Application_Simulator (int max_fragments,
41 int max_fragment_size);
43 /// Destructor, releases any memory left behind.
44 ~Application_Simulator (void);
46 /**
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);
53 private:
54 /// The allocated buffers.
55 char** buffers_;
57 /// The size of the <buffers_> array.
58 int max_fragments_;
60 /// The maximum size of any element of <buffers_>
61 int max_fragment_size_;
64 int
65 ACE_TMAIN(int argc, ACE_TCHAR *argv[])
67 try
69 CORBA::ORB_var orb =
70 CORBA::ORB_init (argc, argv);
72 int tss = 0;
73 int iterations = 500;
74 int repeat = 100;
75 int max_fragments = 2048;
76 int max_fragment_size = 1024;
77 int max_arguments = 16;
78 int max_argument_size = 1024;
79 int quiet = 0;
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"));
83 int opt;
85 while ((opt = get_opt ()) != EOF)
87 switch (opt)
89 case 't':
90 tss = 1;
91 break;
92 case 'n':
93 iterations = ACE_OS::atoi (get_opt.opt_arg ());
94 break;
95 case 'r':
96 repeat = ACE_OS::atoi (get_opt.opt_arg ());
97 break;
98 case 'f':
99 max_fragments = ACE_OS::atoi (get_opt.opt_arg ());
100 break;
101 case 'm':
102 max_fragment_size = ACE_OS::atoi (get_opt.opt_arg ());
103 break;
104 case 's':
105 seed = ACE_OS::atoi (get_opt.opt_arg ());
106 break;
107 case 'a':
108 max_arguments = ACE_OS::atoi (get_opt.opt_arg ());
109 break;
110 case 'b':
111 max_argument_size = ACE_OS::atoi (get_opt.opt_arg ());
112 break;
113 case 'q':
114 quiet = 1;
115 break;
116 case '?':
117 default:
118 ACE_DEBUG ((LM_DEBUG,
119 "Usage: %s "
120 "-n iterations "
121 "-n repeat "
122 "-f max_fragments "
123 "-m max_fragment_size "
124 "-s seed "
125 "-a max_arguments "
126 "-b max_argument_size "
127 "\n",
128 argv[0]));
129 return -1;
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 ();
139 if (tss)
141 buffer_allocator =
142 TAO_ORB_Core_instance ()->output_cdr_buffer_allocator ();
143 dblock_allocator =
144 TAO_ORB_Core_instance ()->output_cdr_dblock_allocator ();
147 Application_Simulator simulator (max_fragments,
148 max_fragment_size);
149 char* argument_buffer;
150 ACE_NEW_RETURN (argument_buffer, char[max_argument_size], 1);
152 int* argument_sizes;
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,
172 buffer_allocator,
173 dblock_allocator);
175 for (int k = 0; k < n; ++k)
177 cdr.write_char_array (argument_buffer,
178 argument_sizes[k]);
181 cdr_encoding.stop_incr ();
184 ACE_Time_Value tv;
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);
188 usecs += tv.usec ();
189 double average =
190 static_cast<double> (ACE_HRTIME_CONVERSION(usecs)) / repeat;
192 if (!quiet)
193 ACE_OS::printf ("AVE: %d %f\n",
194 i, average);
198 catch (const CORBA::Exception& ex)
200 ex._tao_print_exception ("Caught unexpected CORBA exception:");
202 return 1;
204 return 0;
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_;
215 ++i)
216 *i = 0;
219 Application_Simulator::~Application_Simulator (void)
221 for (char** i = this->buffers_;
222 i != this->buffers_ + this->max_fragments_;
223 ++i)
225 if (*i != 0)
227 delete[] *i;
228 *i = 0;
231 delete[] this->buffers_;
232 this->buffers_ = 0;
235 void
236 Application_Simulator::upcall (unsigned int* seed)
238 for (char** i = this->buffers_;
239 i != this->buffers_ + this->max_fragments_;
240 ++i)
242 if (*i != 0)
244 if (ACE_OS::rand_r (seed) % 10000 < 5000)
246 delete[] *i;
247 *i = 0;
250 else
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]);