summaryrefslogtreecommitdiff
path: root/TAO/CIAO/tests/RTCCM/Priority_Test/Controllers/Rate_Controller.cpp
blob: 840c47e1ec2948198656bd417602f024a2ffa139 (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
// $Id$

#include "Rate_Controller.h"
#include "CIAO_common.h"

/// Allocate sample history to take up to 5 minutes of samples.
const int MyImpl::Rate_Controller_i::sample_time_ = 5;

/// Default constructor.
MyImpl::Rate_Controller_i::Rate_Controller_i (CORBA::Long hertz)
  : rate_ (hertz),
    pulser_ (this),
    sampler_ (0)
{
}

/// Default destructor.
MyImpl::Rate_Controller_i::~Rate_Controller_i ()
{
  delete this->sampler_;
}

// Operations from Priority_Test::Controller
void
MyImpl::Rate_Controller_i::start (CORBA::Long arg
                                  ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  // This simply performs some measurements and print out the result.
  ACE_DEBUG ((LM_DEBUG, "PERFORM TEST (%dhz)\n", this->rate_));

  if (this->pulser_.active())
    ACE_THROW (CORBA::BAD_INV_ORDER ());

  this->work_ = arg;
  this->start_time_ = ACE_OS::gethrtime ();
  delete this->sampler_;
#if defined (COLLECT_AND_DUMP_SAMPLE_HISTORY)
  size_t niterations = this->rate_ * 60 * MyImpl::Rate_Controller_i::sample_time_;
  this->sampler_ = new SAMPLER (niterations);
#else
  this->sampler_ = new SAMPLER;
#endif /* COLLECT_AND_DUMP_SAMPLE_HISTORY */

  // @@ Start the rate generator
  this->pulser_.start (this->rate_);
}

void
MyImpl::Rate_Controller_i::stop (ACE_ENV_SINGLE_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException))
{
  if (! this->pulser_.active ())
    ACE_THROW (CORBA::BAD_INV_ORDER ());

  // @@ stop the rate generator
  this->pulser_.stop ();
  ACE_hrtime_t stop_test = ACE_OS::gethrtime ();

  ACE_DEBUG ((LM_DEBUG, "test finished (%dhz)\n", this->rate_));

  const int len = 1024;
  char title [len];

  ACE_OS::snprintf (title, len, "Total (work=%4d @ %2dhz)",
                    this->work_,
                    this->rate_);
  ACE_UINT32 gsf = ACE_High_Res_Timer::global_scale_factor ();

  size_t count = 0;

#if defined (COLLECT_AND_DUMP_SAMPLE_HISTORY)
  this->sampler_->dump_samples (title, gsf);

  ACE_Basic_Stats stats;
  this->sampler_->collect_basic_stats (stats);
  stats.dump_results (title, gsf);
  count = stats.samples_count ();
#else
  this->sampler_->dump_results (title, gsf);
  count = this->sampler_->samples_count ();
#endif /* COLLECT_BASIC_STATS */

  ACE_Throughput_Stats::dump_throughput (title, gsf,
                                         stop_test - this->start_time_,
                                         count);
}

// Operations from Components::SessionComponent
void
MyImpl::Rate_Controller_i::set_session_context (Components::SessionContext_ptr ctx
                                                ACE_ENV_ARG_DECL)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::CCMException))
{
  if (CIAO::debug_level () > 0)
    ACE_DEBUG ((LM_DEBUG, "MyImpl::Rate_Controller_i::set_session_context (%dhz)\n",
                this->rate_));

  this->context_ =
    Priority_Test::CCM_Controller_Context::_narrow (ctx
                                                    ACE_ENV_ARG_PARAMETER);
  ACE_CHECK;

  if (CORBA::is_nil (this->context_.in ()))
    ACE_THROW (CORBA::INTERNAL ());
  // Urm, we actually discard exceptions thown from this operation.
}

void
MyImpl::Rate_Controller_i::ciao_preactivate (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::CCMException))
{
}

void
MyImpl::Rate_Controller_i::ciao_postactivate (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::CCMException))
{
}

void
MyImpl::Rate_Controller_i::ccm_activate (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::CCMException))
{
  if (CIAO::debug_level () > 0)
    ACE_DEBUG ((LM_DEBUG, "MyImpl::Rate_Controller_i::ccm_activate (%dhz)\n",
                this->rate_));

  this->pulser_.open ();
}

void
MyImpl::Rate_Controller_i::ccm_passivate (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::CCMException))
{
  if (CIAO::debug_level () > 0)
    ACE_DEBUG ((LM_DEBUG, "MyImpl::Rate_Controller_i::ccm_passivate(%dhz)\n",
                this->rate_));

  this->pulser_.close ();
}

void
MyImpl::Rate_Controller_i::ccm_remove (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::CCMException))
{
  if (CIAO::debug_level () > 0)
    ACE_DEBUG ((LM_DEBUG, "MyImpl::Rate_Controller_i::ccm_remove (%dhz)\n",
                this->rate_));
}

void
MyImpl::Rate_Controller_i::pulse (void)
{
  ACE_TRY_NEW_ENV
    {
      if (CIAO::debug_level () > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "Pulse (%dhz)!\n",
                    this->rate_));

      ACE_hrtime_t start = ACE_OS::gethrtime ();

      Priority_Test::Common_Ops_var device =
        this->context_->get_connection_worker (ACE_ENV_SINGLE_ARG_PARAMETER);

      (void) device->do_work (this->work_, 0 ACE_ENV_ARG_PARAMETER);
      ACE_TRY_CHECK;

      ACE_hrtime_t now = ACE_OS::gethrtime ();
      this->sampler_->sample (now - start);
    }
  ACE_CATCHANY
    {
      // >= 0 meaning always print out the exception.
      if (CIAO::debug_level () >= 0)
        ACE_PRINT_EXCEPTION (ACE_ANY_EXCEPTION,
                             "Rate_Controller pulse exception\n");
    }
  ACE_ENDTRY;
}

/// Default ctor.
MyImpl::Rate_Controller_Home_i::Rate_Controller_Home_i (CORBA::Long hertz)
  : hertz_ (hertz)
{
}

/// Default dtor.
MyImpl::Rate_Controller_Home_i::~Rate_Controller_Home_i ()
{
}

// Explicit home operations.

// Implicit home operations.

::Components::EnterpriseComponent_ptr
MyImpl::Rate_Controller_Home_i::create (ACE_ENV_SINGLE_ARG_DECL_NOT_USED)
  ACE_THROW_SPEC ((CORBA::SystemException,
                   Components::CCMException))
{
  return new MyImpl::Rate_Controller_i (this->hertz_);
}


extern "C" CONTROLLER_EXEC_Export ::Components::HomeExecutorBase_ptr
createControllerHome25_Impl (void)
{
  if (CIAO::debug_level () > 0)
    ACE_DEBUG ((LM_DEBUG, "extern \"C\" ::createControllerHome_Impl (25hz)\n"));
  return new MyImpl::Rate_Controller_Home_i (25);
}

extern "C" CONTROLLER_EXEC_Export ::Components::HomeExecutorBase_ptr
createControllerHome50_Impl (void)
{
  if (CIAO::debug_level () > 0)
    ACE_DEBUG ((LM_DEBUG, "extern \"C\" ::createControllerHome_Impl (50hz)\n"));
  return new MyImpl::Rate_Controller_Home_i (50);
}

extern "C" CONTROLLER_EXEC_Export ::Components::HomeExecutorBase_ptr
createControllerHome75_Impl (void)
{
  if (CIAO::debug_level () > 0)
    ACE_DEBUG ((LM_DEBUG, "extern \"C\" ::createControllerHome_Impl (75hz)\n"));
  return new MyImpl::Rate_Controller_Home_i (75);
}