summaryrefslogtreecommitdiff
path: root/TAO/performance-tests/Cubit/TAO/MT_Cubit/Globals.cpp
blob: 100120db159428e3af75484b279a95f4b6d4daf2 (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
#include "Globals.h"
#include "ace/Log_Msg.h"
#include "ace/OS_NS_string.h"
#include "ace/OS_NS_errno.h"
#include "ace/Null_Mutex.h"

Globals::Globals ()
  : thr_create_flags (0),
    default_priority (0),
    ior_file (0),
    num_of_objs (2),
    thread_per_rate (0),
    use_multiple_priority (0),
    ready_ (0),
    ready_cnd_ (ready_mtx_),
    barrier_ (0)
{
  const ACE_TCHAR default_endpoint[] = ACE_TEXT("iiop://");
  // Default to iiop

  ACE_OS::strcpy (endpoint, default_endpoint);
}

int
Globals::parse_args (int argc, ACE_TCHAR *argv[])
{
  ACE_Get_Opt opts (argc, argv, ACE_TEXT("e:t:f:rm"));
  int c;

  while ((c = opts ()) != -1)
    {
      switch (c)
      {
      case 'm':
        use_multiple_priority = 1;
        break;
      case 'r':
        thread_per_rate = 1;
        break;
      case 'f':
        ACE_NEW_RETURN (ior_file,
                        ACE_TCHAR[BUFSIZ],
                        -1);
        ACE_OS::strcpy (ior_file,
                        opts.opt_arg ());
        break;
      case 'e':
        ACE_OS::strcpy (endpoint,
                        opts.opt_arg ());
        break;
      case 't':
        num_of_objs = ACE_OS::atoi (opts.opt_arg ());
        break;
      case '?':
      default:
        ACE_ERROR_RETURN ((LM_ERROR,
                           "usage:  %s \t"
                           "[-e <endpoint>]                // starting endpoint                  \n\t\t\t"
                           "[-t <number_of_servants>]      // # of servant threads to create     \n\t\t\t"
                           "[-f <ior_file> ]               // specify a file to output all ior's \n\t\t\t"
                           "[-m ]                          // Use multiple priorities for threads\n\t\t\t"
                           "[-r ]                          // Run the thread-per-rate test      \n"
                           ,argv [0]),
                          -1);
      }
    }

  if (thread_per_rate == 1)
    num_of_objs = THREAD_PER_RATE_OBJS;

  // Indicates successful parsing of the command-line.
  return 0;
}

int
Globals::sched_fifo_init ()
{
#if defined (ACE_HAS_THREADS)
  // Enable FIFO scheduling, e.g., RT scheduling class on Solaris.
# if defined (__APPLE__) || defined (BSD)
  int scope = ACE_SCOPE_THREAD;
# else
  int scope = ACE_SCOPE_PROCESS;
# endif /* __APPLE__ || BSD */

  if (ACE_OS::sched_params (ACE_Sched_Params (ACE_SCHED_FIFO,
                                              SCHED_PRIORITY,
                                              scope)))
    {
      if (ACE_OS::last_error () == EPERM)
        {
          ACE_DEBUG ((LM_MAX,
                      "User is not superuser, "
                      "so remain in time-sharing class\n"));
          ACE_SET_BITS (GLOBALS::instance ()->thr_create_flags, THR_NEW_LWP);
          GLOBALS::instance ()->default_priority = ACE_THR_PRI_OTHER_DEF;
          return 1;
        }
      else
        ACE_ERROR_RETURN ((LM_ERROR,
                           "%n: ACE_OS::sched_params failed\n%a"),
                          -1);
    }

  ACE_SET_BITS (GLOBALS::instance ()->thr_create_flags, THR_BOUND);
  ACE_SET_BITS (GLOBALS::instance ()->thr_create_flags, THR_SCHED_FIFO);
  GLOBALS::instance ()->default_priority = ACE_THR_PRI_FIFO_DEF;

  return 0;
#else

  ACE_ERROR_RETURN ((LM_ERROR,
                     "Test will not run.  This platform doesn't seem to have threads.\n"),
                    -1);
#endif /* ACE_HAS_THREADS */
}

MT_Priority::MT_Priority ()
  : num_priorities_ (0),
    grain_ (0)
{
}

MT_Priority::~MT_Priority ()
{
}

ACE_Sched_Priority
MT_Priority::get_high_priority ()
{
  ACE_Sched_Priority high_priority;

#if defined (VXWORKS)
  high_priority = GLOBALS::instance ()->default_priority;
#elif defined (ACE_WIN32)
  high_priority =
    ACE_Sched_Params::priority_max (ACE_SCHED_FIFO,
                                    ACE_SCOPE_THREAD);
#else
  high_priority = GLOBALS::instance ()->default_priority;
#endif /* VXWORKS */
  return high_priority;
}

ACE_Sched_Priority
MT_Priority::get_low_priority (u_int num_low_priority,
                               ACE_Sched_Priority prev_priority,
                               u_int use_multiple_priority)
{
#if !defined (ACE_HAS_THREADS)
  ACE_UNUSED_ARG (num_low_priority);
  ACE_UNUSED_ARG (prev_priority);
  ACE_UNUSED_ARG (use_multiple_priority);
  return -1;
#else
  ACE_Sched_Priority low_priority = ACE_THR_PRI_FIFO_DEF;
  int policy = ACE_SCHED_FIFO;

  if (!ACE_BIT_ENABLED (GLOBALS::instance ()->thr_create_flags,
                        THR_SCHED_FIFO))
    {
      low_priority = ACE_THR_PRI_OTHER_DEF;
      policy = ACE_SCHED_OTHER;
    }

  // Drop the priority.
  if (use_multiple_priority)
    {
      this->num_priorities_ = 0;

      for (ACE_Sched_Priority_Iterator priority_iterator
             (policy, ACE_SCOPE_THREAD);
           priority_iterator.more ();
           priority_iterator.next ())
        this->num_priorities_++;

      // 1 priority is exclusive for the high priority client.
      this->num_priorities_--;

      // Drop the priority, so that the priority of clients will
      // increase with increasing client number.
      for (u_int j = 0;
           j < num_low_priority;
           j++)
        {
          low_priority =
            ACE_Sched_Params::previous_priority (policy,
                                                 prev_priority,
                                                 ACE_SCOPE_THREAD);
          prev_priority = low_priority;
        }
      // Granularity of the assignment of the priorities.  Some OSs
      // have fewer levels of priorities than we have threads in our
      // test, so with this mechanism we assign priorities to groups
      // of threads when there are more threads than priorities.
      this->grain_ = num_low_priority / this->num_priorities_;

      if (this->grain_ <= 0)
        this->grain_ = 1;
    }
  else
    low_priority =
      ACE_Sched_Params::previous_priority (policy,
                                           prev_priority,
                                           ACE_SCOPE_THREAD);
  return low_priority;
#endif /* ACE_HAS_THREADS */
}

u_int
MT_Priority::number_of_priorities ()
{
  return this->num_priorities_;
}

u_int
MT_Priority::grain ()
{
  return this->grain_;
}


ACE_SINGLETON_TEMPLATE_INSTANTIATE(ACE_Singleton, Globals,  ACE_Null_Mutex);