summaryrefslogtreecommitdiff
path: root/TAO/tests/RTCORBA/Linear_Priority/readers.cpp
blob: 8733eb3a27603833c7019dc5ebde571594d930c1 (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
// $Id$

#include "ace/Read_Buffer.h"
#include "ace/Array_Base.h"

typedef ACE_Array_Base<CORBA::Short> Short_Array;

int
get_priority_bands (const char *test_type,
                    const char *bands_file,
                    RTCORBA::RTORB_ptr rt_orb,
                    CORBA::PolicyList &policies
                    ACE_ENV_ARG_DECL)
{
  //
  // Read bands from a file.
  //
  FILE* file =
    ACE_OS::fopen (bands_file, "r");

  if (file == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Cannot open file %s\n",
                       bands_file),
                      -1);

  ACE_Read_Buffer reader (file, 1);

  char *string =
    reader.read (EOF, ' ', '\0');

  // Check for empty bands file.
  if (string == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "\n%s: No bands set!\n\n",
                  test_type));
      return 0;
    }

  CORBA::ULong bands_length =
    (reader.replaced () + 1) / 2;

  RTCORBA::PriorityBands bands;
  bands.length (bands_length);

  ACE_DEBUG ((LM_DEBUG,
              "\n%s: There are %d bands: ",
              test_type,
              bands_length));

  int result = 1;
  char* working_string = string;
  for (CORBA::ULong i = 0; i < bands_length; ++i)
    {
      result = ::sscanf (working_string,
                         "%hd",
                         &bands[i].low);
      if (result == 0 || result == EOF)
        break;

      working_string += ACE_OS::strlen (working_string);
      working_string += 1;

      result = ::sscanf (working_string,
                         "%hd",
                         &bands[i].high);
      if (result == 0 || result == EOF)
        break;

      working_string += ACE_OS::strlen (working_string);
      working_string += 1;

      ACE_DEBUG ((LM_DEBUG,
                  "[%d %d] ",
                  bands[i].low,
                  bands[i].high));
    }

  reader.alloc ()->free (string);

  if (result == 0 || result == EOF)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Parsing error in file %s\n",
                       bands_file),
                      -1);

  ACE_DEBUG ((LM_DEBUG,
              "\n\n"));

  CORBA::Policy_var banded_connection_policy =
    rt_orb->create_priority_banded_connection_policy (bands
                                                      ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  policies.length (policies.length () + 1);
  policies[policies.length () - 1] =
    banded_connection_policy;

  return 0;
}

int
get_values (const char *test_type,
            const char *file_name,
            const char *name,
            Short_Array &values)
{
  //
  // Read lanes from a file.
  //
  FILE* file =
    ACE_OS::fopen (file_name, "r");

  if (file == 0)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Cannot open file %s\n",
                       file_name),
                      -1);

  ACE_Read_Buffer reader (file, 1);

  char *string =
    reader.read (EOF, ' ', '\0');

  // Check for empty lanes file.
  if (string == 0)
    {
      ACE_DEBUG ((LM_DEBUG,
                  "\n%s: No %s set!\n\n",
                  test_type,
                  name));
      return 0;
    }

  size_t length =
    reader.replaced () + 1;

  values.size (length);

  ACE_DEBUG ((LM_DEBUG,
              "\n%s: There are %d %s: ",
              test_type,
              length,
              name));

  int result = 1;
  char* working_string = string;
  for (CORBA::ULong i = 0; i < length; ++i)
    {
      result = ::sscanf (working_string,
                         "%hd",
                         &values[i]);
      if (result == 0 || result == EOF)
        break;

      working_string += ACE_OS::strlen (working_string);
      working_string += 1;

      ACE_DEBUG ((LM_DEBUG,
                  "[%d] ",
                  values[i]));
    }

  reader.alloc ()->free (string);

  if (result == 0 || result == EOF)
    ACE_ERROR_RETURN ((LM_ERROR,
                       "Parsing error in file %s\n",
                       file_name),
                      -1);

  ACE_DEBUG ((LM_DEBUG,
              "\n\n"));

  return 0;
}

int
get_priority_lanes (const char *test_type,
                    const char *lanes_file,
                    RTCORBA::RTORB_ptr rt_orb,
                    CORBA::ULong stacksize,
                    CORBA::ULong static_threads,
                    CORBA::ULong dynamic_threads,
                    CORBA::Boolean allow_request_buffering,
                    CORBA::ULong max_buffered_requests,
                    CORBA::ULong max_request_buffer_size,
                    CORBA::Boolean allow_borrowing,
                    CORBA::PolicyList &policies
                    ACE_ENV_ARG_DECL)
{
  Short_Array priorities;
  int result =
    get_values (test_type,
                lanes_file,
                "lanes",
                priorities);
  if (result != 0 ||
      priorities.size () == 0)
    return result;

  RTCORBA::ThreadpoolLanes lanes;
  lanes.length (priorities.size ());

  for (CORBA::ULong i = 0;
       i < priorities.size ();
       ++i)
    {
      lanes[i].lane_priority = priorities[i];
      lanes[i].static_threads = static_threads;
      lanes[i].dynamic_threads = dynamic_threads;
    }

  RTCORBA::ThreadpoolId threadpool_id =
    rt_orb->create_threadpool_with_lanes (stacksize,
                                          lanes,
                                          allow_borrowing,
                                          allow_request_buffering,
                                          max_buffered_requests,
                                          max_request_buffer_size
                                          ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  CORBA::Policy_var threadpool_policy =
    rt_orb->create_threadpool_policy (threadpool_id
                                      ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  policies.length (policies.length () + 1);
  policies[policies.length () - 1] =
    threadpool_policy;

  return 0;
}

void
get_auto_priority_lanes_and_bands (CORBA::ULong number_of_lanes,
                                   RTCORBA::RTORB_ptr rt_orb,
                                   CORBA::ULong stacksize,
                                   CORBA::ULong static_threads,
                                   CORBA::ULong dynamic_threads,
                                   CORBA::Boolean allow_request_buffering,
                                   CORBA::ULong max_buffered_requests,
                                   CORBA::ULong max_request_buffer_size,
                                   CORBA::Boolean allow_borrowing,
                                   CORBA::PolicyList &policies
                                   ACE_ENV_ARG_DECL)
{
  RTCORBA::ThreadpoolLanes lanes;
  lanes.length (number_of_lanes);

  RTCORBA::PriorityBands bands;
  bands.length (number_of_lanes);

  CORBA::Short priority_range =
    RTCORBA::maxPriority - RTCORBA::minPriority;

  ACE_DEBUG ((LM_DEBUG,
              "\nUsing %d lanes\n",
              number_of_lanes));

  for (CORBA::ULong i = 0;
       i < number_of_lanes;
       ++i)
    {
      CORBA::Short high_priority =
        CORBA::Short (
                      ACE_OS::floor ((priority_range /
                                      double (number_of_lanes)) *
                                     (i + 1)));

      CORBA::Short low_priority =
        CORBA::Short (
                      ACE_OS::ceil ((priority_range /
                                     double (number_of_lanes)) *
                                    i));

      lanes[i].lane_priority = high_priority;
      lanes[i].static_threads = static_threads;
      lanes[i].dynamic_threads = dynamic_threads;

      bands[i].high = high_priority;
      bands[i].low = low_priority;

      ACE_DEBUG ((LM_DEBUG,
                  "%d: [%d %d] ",
                  i + 1,
                  low_priority,
                  high_priority));
    }

  ACE_DEBUG ((LM_DEBUG,
              "\n\n"));

  RTCORBA::ThreadpoolId threadpool_id =
    rt_orb->create_threadpool_with_lanes (stacksize,
                                          lanes,
                                          allow_borrowing,
                                          allow_request_buffering,
                                          max_buffered_requests,
                                          max_request_buffer_size
                                          ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  policies.length (policies.length () + 1);
  policies[policies.length () - 1] =
    rt_orb->create_priority_banded_connection_policy (bands
                                                      ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  policies.length (policies.length () + 1);
  policies[policies.length () - 1] =
    rt_orb->create_threadpool_policy (threadpool_id
                                      ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Array_Base<CORBA::Short>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Array_Base<CORBA::Short>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */