summaryrefslogtreecommitdiff
path: root/TAO/tests/Cubit/TAO/MT_Cubit/client.cpp
blob: dcb0fb9a5a444ddb03aee90cfffbeb5aa7228758 (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
// $Id$

// ============================================================================
//
// = LIBRARY
//    TAO/tests
//
// = FILENAME
//    client.cpp
//
// = AUTHOR
//    Andy Gokhale, Brian Mendel, Sumedh Mungee, and Sergio Flores-Gaitan
//
// ============================================================================

#include "client.h"

int
initialize (void)
{
#if defined (VXWORKS)
    hostAdd ("mv2604d", "130.38.183.132");
#if defined (VME_DRIVER)
    STATUS status = vmeDrv ();
    if (status != OK)
      printf ("ERROR on call to vmeDrv()\n");
    status = vmeDevCreate ("/vme");
    if (status != OK)
      printf ("ERROR on call to vmeDevCreate()\n");
#endif /* defined (VME_DRIVER) */
#endif   /* defined (VXWORKS) */

    // Make sure we've got plenty of socket handles.  This call will use
    // the default maximum.
    ACE::set_handle_limit ();

    return 0;
}

int
do_priority_inversion_test (Task_State &ts)
{
  // Create the clients
  Client high_priority_client (&ts);
  Client low_priority_client (&ts);

  // Create the daemon thread in its own <ACE_Thread_Manager>.
  ACE_Thread_Manager thr_mgr;

  Util_Thread util_thread (&ts, &thr_mgr);

  ACE_Sched_Priority priority =
    ACE_Sched_Params::priority_min (ACE_SCHED_FIFO,
                                    ACE_SCOPE_THREAD);

  // First activate the Utilization thread.  It will wait until all
  // threads have finished binding.
  util_thread.activate (THR_BOUND,
                        1,
                        0,
                        priority);

  // Now activate the high priority client.

  // @@ The ifdef here is temporarily placed here until
  // I figure out how to map NT's thread priorities
  // into pthread's priorities.
#if defined (ACE_THR_PRI_FIFO_DEF)
  priority = ACE_THR_PRI_FIFO_DEF;
#else
  priority = ACE_DEFAULT_THREAD_PRIORITY;
#endif

  if (high_priority_client.activate (THR_BOUND | ACE_SCHED_FIFO,
                                     1,
                                     0,
                                     priority) == -1)

    ACE_ERROR ((LM_ERROR,
                "%p\n",
                "activate failed"));

  // @@ Sumedh, can you please document why we need this VxWorks
  // #ifdef?  Is there a way to make this more general so we don't
  // need the #ifdef?  In other words, please check with David or
  // Brian about this.

#if !defined (VXWORKS)
  priority =
    ACE_Sched_Params::previous_priority (ACE_SCHED_FIFO,
                                         priority,
                                         ACE_SCOPE_THREAD);
#else
  priority = 65;
#endif /* !defined (VXWORKS) */

  ACE_DEBUG ((LM_DEBUG,
              "Creating %d clients with low priority\n",
              ts.thread_count_ - 1));

  for (u_int i = 0; i < ts.thread_count_ - 1; i++)
    {
      // The first thread starts at min + 1, since the minimum
      // priority thread is the utilization thread.

      // get the next higher priority
      priority = ACE_Sched_Params::previous_priority (ACE_SCHED_FIFO,
                                                      priority,
                                                      ACE_SCOPE_THREAD);
      if (low_priority_client.activate (THR_BOUND,
                                        1,
                                        1,
                                        priority) == -1)
        ACE_ERROR ((LM_ERROR,
                    "%p\n",
                    "activate failed"));
    }
    // Wait for all the threads to exit.
    ACE_Thread_Manager::instance ()->wait ();

#if defined (VXWORKS)
    ACE_OS::printf ("Test done.\n"
                    "High priority client latency : %d usec\n"
                    "Low priority client latency : %d usec\n",
                    high_priority_client.get_high_priority_latency (),
                    low_priority_client.get_low_priority_latency ());
#else  /* !defined (VXWORKS) */
    ACE_DEBUG ((LM_DEBUG, "Test done.\n"
                "High priority client latency : %f msec, jitter: %f msec\n"
                "Low priority client latency : %f msec, jitter: %f msec\n",
                high_priority_client.get_high_priority_latency (),
                high_priority_client.get_high_priority_jitter (),
                low_priority_client.get_low_priority_latency (),
                low_priority_client.get_low_priority_jitter ()));
#endif /* !defined (VXWORKS) */
 util_thread.done_ = 1;
 thr_mgr.wait ();
 ACE_DEBUG ((LM_DEBUG, "utilization task performed %g computations\n",
                        util_thread.get_number_of_computations ()));
 return 0;
}

int
do_thread_per_rate_test (Task_State &ts)
{
  // First activate the high priority client.
    Client CB_40Hz_client (&ts);
    Client CB_20Hz_client (&ts);
    Client CB_10Hz_client (&ts);
    Client CB_5Hz_client (&ts);
    Client CB_1Hz_client (&ts);

    ACE_Sched_Priority priority =
      ACE_Sched_Params::priority_max (ACE_SCHED_FIFO,
                                      ACE_SCOPE_THREAD);

    // VxWorks priority of 0 causes problems.
    priority = 10;
    ACE_DEBUG ((LM_DEBUG, "Creating 40 Hz client with priority %d\n", priority));
    if (CB_40Hz_client.activate (THR_BOUND, 1, 0, priority++) == -1)
      ACE_ERROR ((LM_ERROR, "%p\n", "activate failed"));

    ACE_DEBUG ((LM_DEBUG, "Creating 20 Hz client with priority %d\n", priority));
    if (CB_20Hz_client.activate (THR_BOUND, 1, 0, priority++) == -1)
      ACE_ERROR ((LM_ERROR, "%p\n", "activate failed"));

    ACE_DEBUG ((LM_DEBUG, "Creating 10 Hz client with priority %d\n", priority));
    if (CB_10Hz_client.activate (THR_BOUND, 1, 0, priority++) == -1)
      ACE_ERROR ((LM_ERROR, "%p\n", "activate failed"));

    ACE_DEBUG ((LM_DEBUG, "Creating 5 Hz client with priority %d\n", priority));
    if (CB_5Hz_client.activate (THR_BOUND, 1, 0, priority++) == -1)
      ACE_ERROR ((LM_ERROR, "%p\n", "activate failed"));

    ACE_DEBUG ((LM_DEBUG, "Creating 1 Hz client with priority %d\n", priority));
    if (CB_1Hz_client.activate (THR_BOUND, 1, 0, priority++) == -1)
      ACE_ERROR ((LM_ERROR, "%p\n", "activate failed"));

    // Wait for all the threads to exit.
    ACE_Thread_Manager::instance ()->wait ();

    ACE_OS::printf ("Test done.\n"
                    "40Hz client latency : %d usec\n"
                    "20Hz client latency : %d usec\n"
                    "10Hz client latency : %d usec\n"
                    "5Hz client latency : %d usec\n"
                    "1Hz client latency : %d usec\n",
                    CB_40Hz_client.get_latency (0),
                    CB_20Hz_client.get_latency (1),
                    CB_10Hz_client.get_latency (2),
                    CB_5Hz_client.get_latency (3),
                 CB_1Hz_client.get_latency (4));
    return 0;
}

// This is the main routine of the client, where we create a high
// priority and a low priority client. we then activate the clients
// with the appropriate priority threads, and wait for them to
// finish. After they aer done, we compute the latency and jitter
// metrics and print them.

int
main (int argc, char *argv [])
{
#if defined (ACE_HAS_THREADS)

#if defined (FORCE_ARGS)
    int argc = 7;
    char *argv[] = {"main",
                    "-d",
                    "3",   // Data Type
                    "-t",
                    "10",   // Thread Count
                    "-h",
                    "mv2604d"};  // Host name
#endif   /* defined (FORCE_ARGS) */

    initialize ();

    Task_State ts (argc, argv);
    if (ts.thread_per_rate_ == 0)
      do_priority_inversion_test (ts);
    else
      do_thread_per_rate_test (ts);
#else
    ACE_DEBUG ((LM_DEBUG, "Test not run.  This platform doesn't seem to have threads.\n"));
#endif /* ACE_HAS_THREADS */

    return 0;
}