summaryrefslogtreecommitdiff
path: root/TAO/performance-tests/Cubit/TAO/MT_Cubit/server.cpp
blob: cbd698f6a6e0c59ccace2c07a49f8b6364183170 (plain)
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
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390

//=============================================================================
/**
 *  @file    server.cpp
 *
 *  $Id$
 *
 *  @author Andy Gokhale
 *  @author Sumedh Mungee
 *  @author Sergio Flores-Gaitan
 *  @author and Nagarajan Surendran
 */
//=============================================================================


#include "ace/config-all.h"

#if defined (ACE_VXWORKS) && !defined (__RTP__)
# undef ACE_MAIN
# define ACE_MAIN server
#endif /* ACE_VXWORKS && !__RTP__ */

#include "server.h"
#include "ace/OS_NS_string.h"
#include "ace/Sched_Params.h"
#include "tao/Strategies/advanced_resource.h"

// FUZZ: disable check_for_streams_include
#include "ace/streams.h"

#include "ace/Barrier.h"

#if defined (ACE_HAS_QUANTIFY)
# include "quantify.h"
#endif /* ACE_HAS_QUANTIFY */

Server::Server (void)
  :argc_ (0),
   argv_ (0),
   cubits_ (0),
   high_priority_task_ (0),
   low_priority_tasks_ (0),
   high_argv_ (0),
   low_argv_ (0)
{
}

int
Server::init (int argc, ACE_TCHAR **argv)
{
  int result;

  result = GLOBALS::instance ()->sched_fifo_init ();
  if (result == -1)
    return result;

  this->argc_ = argc;
  this->argv_ = argv;

  VX_VME_INIT;
  FORCE_ARGV (this->argc_,this->argv_);
  // Make sure we've got plenty of socket handles.  This call will use
  // the default maximum.
  ACE::set_handle_limit ();
  return 0;
}

int
Server::run (void)
{
  STOP_QUANTIFY;
  CLEAR_QUANTIFY;
  START_QUANTIFY;

  if (this->start_servants () != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error creating the servants\n"),
                      -1);
  ACE_DEBUG ((LM_DEBUG,
              "Wait for all the threads to exit\n"));
  // Wait for all the threads to exit.
  this->servant_manager_.wait ();
  STOP_QUANTIFY;
  return 0;
}

void
Server::prelim_args_process (void)
{
  int i;

  for (i = 0; i < this->argc_ ; i++)
    {
      if (ACE_OS::strcmp (this->argv_[i], ACE_TEXT("-e")) == 0 &&
          i - 1 < this->argc_)
        ACE_OS::strcpy (GLOBALS::instance ()->endpoint,
                        this->argv_[i+1]);
    }
}

void
Server::init_low_priority (void)
{
  ACE_Sched_Priority prev_priority = this->high_priority_;

  // Drop the priority.
  if (GLOBALS::instance ()->thread_per_rate == 1
      || GLOBALS::instance ()->use_multiple_priority == 1)
    this->low_priority_ =
      this->priority_.get_low_priority (this->num_low_priority_,
                                        prev_priority,
                                        1);
  else
    this->low_priority_ =
      this->priority_.get_low_priority (this->num_low_priority_,
                                        prev_priority,
                                        0);

  this->num_priorities_ = this->priority_.number_of_priorities ();
  this->grain_ = this->priority_.grain ();
  this->counter_ = 0;
}

// Write the ior's to a file so the client can read them.

int
Server::write_iors (void)
{
  u_int j;

  // By this time the num of objs should be set properly.
  ACE_NEW_RETURN (this->cubits_,
                  char* [GLOBALS::instance ()->num_of_objs],
                  -1);

  this->cubits_[0] = ACE_OS::strdup (this->high_priority_task_->get_servant_ior (0));

  for (j = 1;
       j < GLOBALS::instance ()->num_of_objs;
       ++j)
    this->cubits_[j] =
      ACE_OS::strdup (this->low_priority_tasks_[j-1]->get_servant_ior (0));

  FILE *ior_f = 0;

  if (GLOBALS::instance ()->ior_file != 0)
    ior_f = ACE_OS::fopen (GLOBALS::instance ()->ior_file,
                           "w");

  for (j = 0;
       j < GLOBALS::instance ()->num_of_objs;
       ++j)
    {
      if (ior_f != 0)
        {
          ACE_OS::fprintf (ior_f,
                           "%s\n",
                           this->cubits_[j]);
          ACE_DEBUG ((LM_DEBUG,
                      "this->cubits_[%d] ior = %s\n",
                      j,
                      this->cubits_[j]));
        }
    }

  if (ior_f != 0)
    ACE_OS::fclose (ior_f);

  return 0;
}

int
Server::activate_high_servant (void)
{
  ACE_TCHAR orbendpoint[BUFSIZ];

  ACE_OS::strcpy (orbendpoint, ACE_TEXT ("-ORBEndpoint "));
  ACE_OS::strcat (orbendpoint, GLOBALS::instance ()->endpoint);

  ACE_TCHAR *high_second_argv[] =
    { orbendpoint,
      const_cast<ACE_TCHAR *> (ACE_TEXT ("-ORBSndSock 32768 ")),
      const_cast<ACE_TCHAR *> (ACE_TEXT ("-ORBRcvSock 32768 ")),
      ACE_TEXT (0)
    };
  ACE_NEW_RETURN (this->high_argv_,
                  ACE_ARGV (this->argv_, high_second_argv),
                  -1);
  ACE_NEW_RETURN (this->high_priority_task_,
                  Cubit_Task (this->high_argv_->buf (),
                              "internet",
                              1,
                              &this->servant_manager_,
                              0), //task id 0.
                  -1);

  // Make the high priority task an active object.
  if (this->high_priority_task_->activate
      (GLOBALS::instance ()->thr_create_flags,
       1,
       0,
       this->high_priority_) == -1)
    ACE_DEBUG ((LM_ERROR,
                ACE_TEXT ("(%P|%t) task activation at priority %d ")
                ACE_TEXT (" failed, exiting!\n%a"),
                this->high_priority_,
                -1));

  ACE_MT (ACE_GUARD_RETURN (TAO_SYNCH_MUTEX,
                            ready_mon,
                            GLOBALS::instance ()->ready_mtx_,
                            -1));

  // Wait on the condition variable for the high priority client to
  // finish parsing the arguments.

  while (!GLOBALS::instance ()->ready_)
    GLOBALS::instance ()->ready_cnd_.wait ();

  // Default return success.
  return 0;
}

int
Server::activate_low_servants (void)
{
  if (static_cast<int> (this->num_low_priority_) > 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "Creating %d servants starting at priority %d\n",
                  this->num_low_priority_,
                  this->low_priority_));

      // Create the low priority servants.
      ACE_NEW_RETURN (this->low_priority_tasks_,
                      Cubit_Task *[GLOBALS::instance ()->num_of_objs],
                      -1);
    }

  // Strip the provided addr from the endpoint and make the ORB
  // choose the remaining endpoints based on the protocol used in
  // the endpoint.
  //
  // e.g.: orignal endpoint:    iiop://foobar:10001
  //       random endpoint      iiop://

  const ACE_TCHAR protocol_delimiter[] = ACE_TEXT("://");

  ACE_TCHAR *addr = ACE_OS::strstr (GLOBALS::instance ()->endpoint,
                                    protocol_delimiter);

  size_t random_endpoint_length =
    ACE_OS::strlen (GLOBALS::instance ()->endpoint) -
    ACE_OS::strlen (addr) +
    ACE_OS::strlen (protocol_delimiter);

  ACE_TCHAR random_endpoint[BUFSIZ];

  ACE_OS::sprintf (random_endpoint, ACE_TEXT("-ORBEndpoint "));

  ACE_OS::strncat (random_endpoint,
                   GLOBALS::instance ()->endpoint,
                   random_endpoint_length);

  for (int i = this->num_low_priority_;
       i > 0;
       i--)
    {
      ACE_TCHAR *low_second_argv[] =
        {random_endpoint,
         const_cast<ACE_TCHAR *> (ACE_TEXT("-ORBSndSock 32768 ")),
         const_cast<ACE_TCHAR *> (ACE_TEXT("-ORBRcvSock 32768 ")),
         0};
      ACE_NEW_RETURN (this->low_argv_,
                      ACE_ARGV (this->argv_,
                                low_second_argv),
                      -1);

      ACE_NEW_RETURN (this->low_priority_tasks_ [i - 1],
                      Cubit_Task (this->low_argv_->buf (),
                                  "internet",
                                  1,
                                  &this->servant_manager_,
                                  i),
                      -1);

      // Make the low priority task an active object.
      if (this->low_priority_tasks_[i - 1]->activate
          (GLOBALS::instance ()->thr_create_flags,
           1,
           0,
           this->low_priority_) == -1)
        ACE_DEBUG ((LM_ERROR,
                    ACE_TEXT ("(%P|%t) task activation at priority %d ")
                    ACE_TEXT (" failed, exiting!\n%a"),
                    this->low_priority_,
                    -1));

      ACE_DEBUG ((LM_DEBUG,
                  "Created servant %d with priority %d\n",
                  i,
                  this->low_priority_));

      // Use different priorities on thread per rate or multiple
      // priority.
      if (GLOBALS::instance ()->use_multiple_priority == 1
          || GLOBALS::instance ()->thread_per_rate == 1)
        {
          this->counter_ = (this->counter_ + 1) % this->grain_;

          if (this->counter_ == 0
               //Just so when we distribute the priorities among the
               //threads, we make sure we don't go overboard.
              && this->num_priorities_ * this->grain_ > this->num_low_priority_ - (i - 1))
            // Get the next higher priority.
            this->low_priority_ = ACE_Sched_Params::next_priority
              (ACE_SCHED_FIFO,
               this->low_priority_,
               ACE_SCOPE_THREAD);
        }
    } /* end of for() */

  // default return success.
  return 0;
}

int
Server::start_servants (void)
{
  // Do the preliminary argument processing for options -p and -h.
  this->prelim_args_process ();

  // Find the priority for the high priority servant.
  this->high_priority_ = this->priority_.get_high_priority ();

  ACE_DEBUG ((LM_DEBUG,
              "Creating servant 0 with high priority %d\n",
              this->high_priority_));

  // Activate the high priority servant task
  if (this->activate_high_servant () < 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Failure in activating high priority servant\n"),
                      -1);

  this->num_low_priority_ =
    GLOBALS::instance ()->num_of_objs - 1;

  // Initialize the priority of the low priority servants.
  this->init_low_priority ();

  // Activate the low priority servants.
  if (this->activate_low_servants () < 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Failure in activating low priority servant\n"),
                      -1);

  // Wait in the barrier.
  GLOBALS::instance ()->barrier_->wait ();

  int result = this->write_iors ();
  if (result != 0)
    return result;
  return 0;
}


int
ACE_TMAIN(int argc, ACE_TCHAR *argv[])
{
  int result;

  Server server;

  result = server.init (argc, argv);
  if (result != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error in Initialization\n"),
                      1);

  // run the server.
  result = server.run ();
  ACE_DEBUG ((LM_DEBUG,
              "%s:%d\n", __FILE__, __LINE__));
  if (result != 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Error while running the servants\n"),
                      2);
  return 0;
}