summaryrefslogtreecommitdiff
path: root/TAO/examples/RTCORBA/Activity/Thread_Task.cpp
blob: 167d1bca360d8d0143f399e6a378d814221e28b0 (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
//$Id$
#include "Thread_Task.h"

#include "ace/High_Res_Timer.h"
#include "tao/debug.h"
#include "tao/ORB_Core.h"

#include "Activity.h"
#include "Task_Stats.h"

Thread_Task::Thread_Task (void)
{
}

int
Thread_Task::activate_task (ACE_Barrier* barrier)
{
  barrier_ = barrier;

  long flags = THR_NEW_LWP | THR_JOINABLE;

  flags |=
    ACTIVITY::instance()->scope_policy () |
    ACTIVITY::instance()->sched_policy ();

  // Become an active object.
  if (this->ACE_Task <ACE_SYNCH>::activate (flags,
                                            1,
                                            0,
                                            this->task_priority_) == -1)
       {
         if (ACE_OS::last_error () == EPERM)
           ACE_ERROR_RETURN ((LM_ERROR,
                              ACE_TEXT ("Insufficient privilege to run this test.\n")),
                             -1);
         else
           ACE_DEBUG ((LM_ERROR,
                       ACE_TEXT ("(%t) task activation at priority %d failed, ")
                        ACE_TEXT ("exiting!\n%a"),
                       this->task_priority_,
                       -1));
       }
  return 0;
}

void
Thread_Task::change_network_priority (void)
{
  ACE_DEBUG ((LM_DEBUG,
	      "Change Network Priority\n"));

  ACE_TRY_NEW_ENV
    {

      RTCORBA::RTORB_var rt_orb = ACTIVITY::instance ()->rt_orb ();

      //  if (check_for_nil (rt_orb.in (), "RTORB") == -1)
      //          ACE_ERROR ((LM_ERROR,
      //                      "RTORB is nil\n"));
      
      //Set the tcp protocol protperties
      RTCORBA::TCPProtocolProperties_var tcp_properties =
        rt_orb->create_tcp_protocol_properties (ACE_DEFAULT_MAX_SOCKET_BUFSIZ,
                                                ACE_DEFAULT_MAX_SOCKET_BUFSIZ,
						1,
                                                0,
                                                1,
						1
                                                ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      RTCORBA::ProtocolList protocols;
      protocols.length (1);
      protocols[0].protocol_type = 0;
      protocols[0].transport_protocol_properties =
        RTCORBA::ProtocolProperties::_duplicate (tcp_properties.in ());
      protocols[0].orb_protocol_properties =
        RTCORBA::ProtocolProperties::_nil ();

      CORBA::PolicyList policy_list;
      policy_list.length (1);
      policy_list[0] =
        rt_orb->create_client_protocol_policy (protocols
                                               ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;


      ACTIVITY::instance ()->orb ()->orb_core ()->policy_current ().set_policy_overrides (policy_list,
											  CORBA::SET_OVERRIDE
											  ACE_ENV_ARG_PARAMETER);
      
      ACE_TRY_CHECK;
    }
  ACE_CATCHANY
    {
      ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                           "Caught exception: change_network_priority\n");
    }
  ACE_ENDTRY;
}

int
Thread_Task::svc (void)
{
  ACE_DEBUG ((LM_DEBUG,
	      "Thread_Task::svc\n"));

  if (this->enable_network_priority_)
    this->change_network_priority ();

  // if debugging, dump the priority that we're actually at.
  if (TAO_debug_level > 0)
    {
      ACE_DECLARE_NEW_CORBA_ENV;
      // Get the priority of the current thread.
      RTCORBA::Priority prio =
        ACTIVITY::instance()->current ()->the_priority (ACE_ENV_SINGLE_ARG_PARAMETER);
      ACE_CHECK_RETURN (-1);

      if (prio == this->task_priority_)
        ACE_DEBUG ((LM_DEBUG,
                    ACE_TEXT ("(%t) actual prio of %d equals desired priority\n"),
                    prio));
      else
        {
          ACE_DEBUG ((LM_ERROR,
                      ACE_TEXT ("(%t) actual prio = %d, desired priority_ = %d!\n"),
                      prio,
                      this->task_priority_));
        }
    }

  if (TAO_debug_level > 0)
    ACE_DEBUG ((LM_DEBUG, "Thread_Task (%t) - wait\n"));

  // First, wait for other threads.
  this->barrier_->wait ();

  // first thread here inits the Base_Time.
  task_stats_->base_time (BASE_TIME::instance ()->base_time_);

  // now wait till the phase_ period expires.
  ACE_OS::sleep (ACE_Time_Value (0, phase_));

  ACE_UINT32 gsf = ACE_High_Res_Timer::global_scale_factor ();

  ACE_hrtime_t before, after = 0, total_time = 0;

  Job::data payload;
  payload.length (1500);
  
  total_time = ACE_OS::gethrtime () + ACE_UINT64 (exec_time_ / 1000000 * gsf * ACE_HR_SCALE_CONVERSION);

  int i = 0;
  while ( i < iter_ )
    {
      i++;
      
      before = ACE_OS::gethrtime ();
      
      job_->work (load_,
		  payload);
      
      after = ACE_OS::gethrtime ();

      if (after <= total_time || iter_ > 0)
	task_stats_->sample (before, after);
      
      if (period_ != 0) // blast mode, no sleep.
	{
          // convert to microseconds
#if !defined ACE_LACKS_LONGLONG_T
	  
          ACE_UINT32 elapsed_microseconds = ACE_UINT32((after - before) / gsf);
	  
#else  /* ! ACE_LACKS_LONGLONG_T */

          ACE_UINT32 elapsed_microseconds = (after - before) / gsf;

#endif /* ! ACE_LACKS_LONGLONG_T */

#if defined (ACE_WIN32)
          elapsed_microseconds*=1000; // convert to uSec on Win32
#endif /* ACE_WIN32 */

	  // did we miss any deadlines?
	  
          int missed =
            elapsed_microseconds > period_ ? elapsed_microseconds/period_ : 0;
	  
          long sleep_time = (missed + 1)*period_ ;
          sleep_time -= elapsed_microseconds;
	  
          if (TAO_debug_level > 0)
            ACE_DEBUG ((LM_DEBUG, "(%t) sleep time = %d\n", sleep_time));
	  
          ACE_Time_Value t_sleep (0, sleep_time);
          ACE_OS::sleep (t_sleep);
        } /* period != 0 */
    } /* for */
  
  task_stats_->end_time (ACE_OS::gethrtime ());

  job_->shutdown (); // tell the job that we're done.

  ACTIVITY::instance ()->task_ended (this);

  return 0;
}