Merge pull request #2303 from jwillemsen/jwi-803
[ACE_TAO.git] / TAO / examples / RTScheduling / Fixed_Priority_Scheduler / FP_Task.cpp
blob812c8e5be2317f139891a27953da295b590e2a0f
1 #include "FP_Task.h"
2 #include "test.h"
3 #include "tao/RTCORBA/Priority_Mapping_Manager.h"
4 #include "tao/RTCORBA/Priority_Mapping.h"
5 #include "ace/High_Res_Timer.h"
6 #include "ace/OS_NS_errno.h"
7 #include "ace/Countdown_Time.h"
9 FP_Task::FP_Task (int importance,
10 time_t start_time,
11 int load,
12 int iter,
13 int dist,
14 char *job_name,
15 DT_Creator *dt_creator)
17 this->load_ = load;
18 this->iter_ = iter;
19 this->start_time_ = start_time;
20 this->importance_ = importance;
21 this->dt_creator_ = dt_creator;
22 this->dist_ = dist;
23 this->job_name_ = CORBA::string_dup (job_name);
26 int
27 FP_Task::activate_task (RTScheduling::Current_ptr current,
28 CORBA::Policy_ptr sched_param,
29 long flags,
30 ACE_Time_Value* base_time)
32 if (TAO_debug_level > 0)
33 ACE_DEBUG ((LM_DEBUG,
34 "FP_Task::activate %d\n",
35 importance_));
37 char msg [BUFSIZ];
38 ACE_OS::sprintf (msg, "FP_Task::activate task\n");
39 dt_creator_->log_msg (msg);
41 base_time_ = base_time;
43 current_ = RTScheduling::Current::_narrow (current);
45 sched_param_ = CORBA::Policy::_duplicate (sched_param);
47 pre_activate ();
49 if (this->activate (flags,
52 this->importance_) == -1)
54 if (ACE_OS::last_error () == EPERM)
55 ACE_ERROR_RETURN ((LM_ERROR,
56 ACE_TEXT ("Insufficient privilege to run this test.\n")),
57 -1);
60 return 0;
63 void
64 FP_Task::pre_activate ()
66 CORBA::Object_var object =
67 DT_TEST::instance ()->orb ()->resolve_initial_references ("PriorityMappingManager");
69 RTCORBA::PriorityMappingManager_var mapping_manager =
70 RTCORBA::PriorityMappingManager::_narrow (object.in ());
72 RTCORBA::PriorityMapping *pm = mapping_manager->mapping ();
74 CORBA::Short native_priority;
75 if (pm->to_native (this->importance_, native_priority) == 0)
76 ACE_ERROR ((LM_ERROR,
77 "Cannot convert native priority %d to corba priority\n",
78 native_priority));
79 if (TAO_debug_level > 0)
80 ACE_DEBUG ((LM_DEBUG,
81 "Native Priority = %d\n",
82 native_priority));
83 char msg [BUFSIZ];
84 ACE_OS::sprintf (msg, "Native Priority %d\n",
85 native_priority);
86 dt_creator_->log_msg (msg);
88 this->importance_ = native_priority;
91 int
92 FP_Task::perform_task ()
94 static CORBA::ULong prime_number = 9619;
96 for (int j = 0; j < this->iter_; j++)
98 ACE_Time_Value run_time = ACE_OS::gettimeofday () - *base_time_;
99 TASK_STATS::instance ()->sample (run_time.sec (), count_);
101 ACE_Time_Value count_down_time (1);
102 ACE_Countdown_Time count_down (&count_down_time);
104 while (count_down_time > ACE_Time_Value::zero)
106 ACE::is_prime (prime_number,
108 prime_number / 2);
110 count_down.update ();
114 if (TAO_debug_level > 0)
115 ACE_DEBUG ((LM_DEBUG,
116 "%d\n",
117 count_));
119 if (this->dist_)
121 job_->work (this->load_, this->importance_);
124 for (int j = 0; j < this->iter_; j++)
126 ACE_Time_Value run_time = ACE_OS::gettimeofday () - *base_time_;
127 TASK_STATS::instance ()->sample (run_time.sec (), count_);
129 ACE_Time_Value count_down_time (1);
130 ACE_Countdown_Time count_down (&count_down_time);
132 while (count_down_time > ACE_Time_Value::zero)
134 if (TAO_debug_level > 0)
135 ACE_DEBUG ((LM_DEBUG,
136 "Waiting on count down\n"));
139 ACE::is_prime (prime_number,
141 prime_number / 2);
142 count_down.update ();
146 if (TAO_debug_level > 0)
147 ACE_DEBUG ((LM_DEBUG,
148 "%d\n",
149 count_));
152 if (dist_)
153 job_->shutdown ();
154 return 0;