summaryrefslogtreecommitdiff
path: root/TAO/examples/RTScheduling/Fixed_Priority_Scheduler/test.cpp
blob: e88c41a27ebed0db31c3b8b59517a33ef068159a (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
//$Id$

#include "test.h"
#include "tao/RTScheduling/RTScheduler_Manager.h"
#include "tao/ORB_Core.h"
#include "ace/Arg_Shifter.h"
#include "../Thread_Task.h"
#include "../Task_Stats.h"
#include "../Synch_i.h"
#include "ace/OS_NS_errno.h"
#include "ace/Argv_Type_Converter.h"

DT_Test::DT_Test (void)
{
  base_t = ACE_OS::gethrtime ();
}

void
DT_Test::check_supported_priorities (void)
{
  // Check that we have sufficient priority range to run this
  // test, i.e., more than 1 priority level.

  this->thr_sched_policy_ = orb_->orb_core ()->orb_params ()->sched_policy ();
  this->thr_scope_policy_ = orb_->orb_core ()->orb_params ()->scope_policy ();

  if (thr_sched_policy_ == THR_SCHED_RR)
    {
      //if (TAO_debug_level > 0)
      ACE_DEBUG ((LM_DEBUG, "Sched policy = THR_SCHED_RR\n"));

      sched_policy_ = ACE_SCHED_RR;
    }
  else
  if (thr_sched_policy_ == THR_SCHED_FIFO)
    {
      // if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG, "Sched policy = THR_SCHED_FIFO\n"));

      sched_policy_ = ACE_SCHED_FIFO;
    }
  else
    {
      if (TAO_debug_level > 0)
        ACE_DEBUG ((LM_DEBUG, "Sched policy = THR_SCHED_OTHER\n"));

      sched_policy_ = ACE_SCHED_OTHER;
    }

  if (thr_sched_policy_ == THR_SCHED_RR || thr_sched_policy_ == THR_SCHED_FIFO)
    {
      max_priority_ = ACE_Sched_Params::priority_max (sched_policy_);
      min_priority_ = ACE_Sched_Params::priority_min (sched_policy_);

      if (max_priority_ == min_priority_)
	{
	  ACE_DEBUG ((LM_DEBUG,
		      "Not enough priority levels on this platform"
		      " to run the test, aborting \n"));
	  ACE_OS::exit (2);
	}
      else ACE_DEBUG ((LM_DEBUG, "max_priority = %d, min_priority = %d\n",
		       max_priority_, min_priority_));
    }
}

int
DT_Test::init (int argc, char *argv []
	       ACE_ENV_ARG_DECL)
{
  orb_ = CORBA::ORB_init (argc,
			  argv,
			  ""
			  ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  this->check_supported_priorities ();

  dt_creator_->orb (orb_.in ());

  TASK_STATS::instance ()->init (dt_creator_->total_load ());

  CORBA::Object_ptr manager_obj = orb_->resolve_initial_references ("RTSchedulerManager"
								   ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  TAO_RTScheduler_Manager_var manager = TAO_RTScheduler_Manager::_narrow (manager_obj
									  ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);


  ACE_NEW_RETURN (scheduler_,
		  Fixed_Priority_Scheduler (orb_.in ()),
                  -1);

  manager->rtscheduler (scheduler_);

  CORBA::Object_var object =
    orb_->resolve_initial_references ("RTScheduler_Current"
				      ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);

  current_  =
    RTScheduling::Current::_narrow (object.in () ACE_ENV_ARG_PARAMETER);
  ACE_CHECK_RETURN (-1);


  if (sched_policy_ != ACE_SCHED_OTHER)
    {

        //Set the main thread to max priority...
        if (ACE_OS::sched_params (ACE_Sched_Params (sched_policy_,
  						  55,
  						  ACE_SCOPE_PROCESS)) != 0)
  	{
  	  if (ACE_OS::last_error () == EPERM)
  	    {
  	      ACE_DEBUG ((LM_DEBUG,
  			  "(%P|%t): user is not superuser, "
  			  "test runs in time-shared class\n"));
  	    }
  	  else
  	    ACE_ERROR ((LM_ERROR,
  			"(%P|%t): sched_params failed\n"));
  	}
    }

  return 0;
}

void
DT_Test::run (int argc, char* argv []
	      ACE_ENV_ARG_DECL)
{
  init (argc,argv
	ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (this->dt_creator_->resolve_naming_service (ACE_ENV_SINGLE_ARG_PARAMETER) == -1)
    return;
  ACE_CHECK;


  //TASK_STATS::instance ()->init (this->dt_creator_->dt_count () * 100);


  this->dt_creator_->activate_root_poa (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  this->dt_creator_->activate_poa_list (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
  this->dt_creator_->activate_job_list (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;
  this->dt_creator_->activate_schedule (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  DT_Creator* dt_creator = this->dt_creator_;
  dt_creator->register_synch_obj (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;

  ACE_DEBUG ((LM_DEBUG,
	      "Registered Synch Object\n"));

  /*
  dt_creator_->create_distributable_threads (current_.in ()
					     ACE_ENV_ARG_PARAMETER);
  ACE_TRY_CHECK;
  */

  this->activate_task ();

  char msg [BUFSIZ];
  ACE_OS::sprintf (msg, "ORB RUN\n");
  dt_creator_->log_msg (msg);

  //ACE_Thread_Manager::instance ()->wait ();
  orb_->run (ACE_ENV_SINGLE_ARG_PARAMETER);
  ACE_CHECK;


}


void
DT_Test::dt_creator (FP_DT_Creator* dt_creator)
{
  this->dt_creator_ = dt_creator;
}

FP_DT_Creator*
DT_Test::dt_creator (void)
{
  return this->dt_creator_;
}


Fixed_Priority_Scheduler*
DT_Test::scheduler (void)
{
  return this->scheduler_;
}

int
DT_Test::activate_task (void)
{

  ACE_DEBUG ((LM_DEBUG,
	      "Test Activate Task\n"));

  long flags;
  flags = THR_NEW_LWP | THR_JOINABLE;
  flags |=
    orb_->orb_core ()->orb_params ()->scope_policy () |
    orb_->orb_core ()->orb_params ()->sched_policy ();

  if (this->activate (flags,
		      1,
		      0,
		      50) == -1)
    {
      if (ACE_OS::last_error () == EPERM)
	ACE_ERROR_RETURN ((LM_ERROR,
			   ACE_TEXT ("Insufficient privilege to run this test.\n")),
			  -1);

    }
  return 0;
}

int
DT_Test::svc (void)
{
  ACE_TRY_NEW_ENV
    {
      ACE_DEBUG ((LM_DEBUG,
		  "In test::svc\n"));

      dt_creator_->create_distributable_threads (current_.in ()
						 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;


    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Caught exception:");
      return -1;
    }
  ACE_ENDTRY;

  return 0;
}


CORBA::ORB_ptr
DT_Test::orb (void)
{
  return this->orb_.in ();
}

int
ACE_TMAIN (int argc, ACE_TCHAR* argv [])
{
  ACE_Argv_Type_Converter convert (argc, argv);
  ACE_TRY_NEW_ENV
    {
      ACE_Service_Config::static_svcs ()->insert (&ace_svc_desc_FP_DT_Creator);

      DT_TEST::instance ()->run (convert.get_argc(), convert.get_ASCII_argv()
				 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Caught exception:");
      return 1;
    }
  ACE_ENDTRY;

  return 0;
}

#if defined (ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION)
template ACE_Singleton<DT_Test, ACE_Thread_Mutex> *ACE_Singleton<DT_Test, ACE_Thread_Mutex>::singleton_;
#endif /* ACE_HAS_EXPLICIT_STATIC_TEMPLATE_MEMBER_INSTANTIATION */