summaryrefslogtreecommitdiff
path: root/performance-tests/Misc/childbirth_time.cpp
blob: 8ca0de1a0f6e0b230aca559c605b0a5fffbb22d4 (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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
// $Id$

// ============================================================================
//
// = LIBRARY
//    (none)
//
// = FILENAME
//    childbirth_time.cpp
//
// = DESCRIPTION
//   This program is used to measure various child-creation mechanisms
//   on various platforms.  By default, the program measure the time
//   to 'fork' a new process using ACE_Process.spawn ().  Other tests
//   are possible as described below.   James Hu provides the idea to
//   batch measuring threads creation.
//
//   Usage: childbirth_time [-n ###] [-l ###] [-p|-f|-t|-a|-m|-x] [-h] [-e]
//
//          -n ###: Specify number of iteration in tens.  If this
//                  option is not specified, the default is
//                  MULTIPLY_FACTOR * (100 iterations,) which is
//                  equivalent to -n 10.
//
//          -l ###: Specify MULTIPLY_FACTOR.  Default is 10.
//
//             *-p: Measure the performance of forking a child process
//                  and exec an "empty" program.  This test uses
//                  ACE_Process.spawn ().  (Default)
//
//              -f: Measure the performance of native "fork" function
//                  call.  Notice that there is no equivalent NT
//                  function calls and this option is only available
//                  on UN*X platform.
//
//              -t: Measure the performance of native thread creation
//                  mechanisms.  On Solaris, this is thr_create ().
//                  On NT, this is CreateThread ().  Currently, only
//                  these two platforms are implemented.
//
//              -m: Measure the performance of Thread_Manager::spawn_n
//                  method.
//
//              -x: Test the baseline performance of ACE_Thread_Mutex.
//                  This really doesn't belong here
//
//              -a: Measure the performance of thread creation using
//                  ACE_OS::thr_create ().
//
//              -h: Use High Resolution Timer if supported by platform.
//
//              -e: Exec a program after fork ().  This option has no
//                  effect on NT.
//
//   = CREATION DATE
//      June 29, 1997
//
//   = AUTHOR
//      Nanbor Wang
//
// ============================================================================

// Process Creation profiling

#include "ace/OS.h"
#include "ace/Get_Opt.h"
#include "ace/Process.h"
#include "ace/Profile_Timer.h"
#include "ace/Thread_Manager.h"

ACE_RCSID(Misc, childbirth_time, "$Id$")

#define  ACE_STOP_SIGN  ACE_OS::sleep (0)

#define MAX_NO_ITERATION  10000
#if defined (ACE_WIN32)
#define SUBPROGRAM "date.exe"
#else
#define SUBPROGRAM "date"
#endif

size_t MULTIPLY_FACTOR = 10;
typedef double (*Profiler)(size_t);
static int do_exec_after_fork = 0;

/// do nothing thread function
extern "C" void *ace_empty (void*)
{
  return 0;
}

static double
prof_ace_process (size_t iteration)
{
  if (iteration != 0)
    {
      ACE_Process_Options popt;
      ACE_Process aProcess;

      popt.command_line (SUBPROGRAM);

      iteration *= MULTIPLY_FACTOR;

      if (do_exec_after_fork == 0)
        popt.creation_flags (ACE_Process_Options::NO_EXEC);

      ACE_Profile_Timer ptimer;
      ACE_Profile_Timer::ACE_Elapsed_Time et;
      double time = 0;
      pid_t result;

      for (size_t c = 0; c < iteration; c++)
        {
          ACE_STOP_SIGN;
          ptimer.start ();
          result = aProcess.spawn (popt);
          ptimer.stop ();

          if (result == -1)
            ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "process.spawn"), -1);
          else if (do_exec_after_fork == 0 && result == 0)
            ACE_OS::exit (0) ;
          else
            {
              ptimer.elapsed_time (et);
              time += et.real_time;
            }
        }

      return time / iteration;
    }
  else
    return -1.0;
}

static double
prof_fork (size_t iteration)
{
#if !defined (ACE_LACKS_EXEC)
  if (iteration != 0)
    {
      ACE_Profile_Timer ptimer;
      ACE_Profile_Timer::ACE_Elapsed_Time et;
      double time = 0;

      iteration *= MULTIPLY_FACTOR;

      for (size_t i = 0; i < iteration; i++)
        {
          ACE_STOP_SIGN;
          ptimer.start ();
          switch (ACE_OS::fork ())
            {
            case -1:
              ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "process.spawn"), -1);
              /* NOTREACHED */
            case 0:
              exit (0);
              /* NOTREACHED */
              break;
            default:
              ptimer.stop ();
              ptimer.elapsed_time (et);
              time += et.real_time;
            }
        }
      return time / iteration;
    }
  else
    return -1.0;
#else
  ACE_UNUSED_ARG (iteration);
  ACE_ERROR_RETURN ((LM_ERROR, "fork () is not supported on this platform."), -1);
#endif
}

static double
prof_native_thread (size_t iteration)
{
#if defined (ACE_HAS_THREADS)  && (defined (ACE_HAS_WTHREADS) || defined (ACE_HAS_STHREADS))
  if (iteration != 0)
    {
      ACE_Profile_Timer ptimer;
      ACE_Profile_Timer::ACE_Elapsed_Time et;
      double time = 0;

      for (size_t i = 0; i < iteration; i++)
        {
          ACE_STOP_SIGN;
          ptimer.start ();
          for (size_t j = 0; j < MULTIPLY_FACTOR; j++)
            {
#if defined (ACE_HAS_WTHREADS)
              if (::CreateThread (NULL,
                                  0,
                                  LPTHREAD_START_ROUTINE (ace_empty),
                                  0,
                                  CREATE_SUSPENDED,
                                  0) == NULL)
#elif defined (ACE_HAS_STHREADS)
                if (::thr_create (NULL,
                                  0,
                                  &ace_empty,
                                  0,
                                  THR_SUSPENDED,
                                  NULL) != 0)
#endif
                  ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "CreateThread"), -1);
            }
          ptimer.stop ();
          ptimer.elapsed_time (et);
          time += et.real_time;
        }
      iteration *= MULTIPLY_FACTOR;
      return time / iteration;
    }
  else
    return -1.0;
#else
  ACE_UNUSED_ARG (iteration);
  ACE_ERROR_RETURN ((LM_ERROR, "Testing of native threads is not supported on this platform.\n"), -1);
#endif
}

static double
prof_ace_os_thread (size_t iteration)
{
#if defined (ACE_HAS_THREADS)
  if (iteration != 0)
    {
      ACE_Profile_Timer ptimer;
      ACE_Profile_Timer::ACE_Elapsed_Time et;
      double time = 0;

      for (size_t i = 0; i < iteration; i++)
        {
          ACE_STOP_SIGN;
          ptimer.start ();

          for (size_t j = 0; j < MULTIPLY_FACTOR; j++)
            if (ACE_OS::thr_create ((ACE_THR_FUNC) ace_empty,
                                    0,
                                    THR_SUSPENDED,
                                    NULL) == -1)
              ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "CreateThread"), -1);

          ptimer.stop ();
          ptimer.elapsed_time (et);
          time += et.real_time;
        }
      iteration *= MULTIPLY_FACTOR;
      return time / iteration;
    }
  else
    return -1.0;
#else
  ACE_UNUSED_ARG (iteration);
  ACE_ERROR_RETURN ((LM_ERROR, "Threads are not supported on this platform.\n"), -1);
#endif
}

static double
prof_tm_thread (size_t iteration)
{
#if defined (ACE_HAS_THREADS)
  if (iteration != 0)
    {
      ACE_Profile_Timer ptimer;
      ACE_Profile_Timer::ACE_Elapsed_Time et;
      double time = 0;

      for (size_t i = 0; i < iteration; i++)
        {
          ACE_STOP_SIGN;
          ptimer.start ();

          if (ACE_Thread_Manager::instance ()->spawn_n (MULTIPLY_FACTOR,
                                                        (ACE_THR_FUNC) ace_empty,
                                                        0,
                                                        THR_SUSPENDED) == -1)
            ACE_ERROR_RETURN ((LM_ERROR, "%p\n", "CreateThread"), -1);

          ptimer.stop ();
          ptimer.elapsed_time (et);
          time += et.real_time;
        }
      iteration *= MULTIPLY_FACTOR;
      return time / iteration;
    }
  else
    return -1.0;
#else
  ACE_UNUSED_ARG (iteration);
  ACE_ERROR_RETURN ((LM_ERROR, "Threads are not supported on this platform."), -1);
#endif
}

static double
prof_mutex_base (size_t iteration)
{
#if defined (ACE_HAS_THREADS)
  ACE_Thread_Mutex plain;
  if (iteration != 0)
    {
      ACE_Profile_Timer ptimer;
      ACE_Profile_Timer::ACE_Elapsed_Time et;
      double time = 0;

      for (size_t i = 0; i < iteration; i++)
        {
          ACE_STOP_SIGN;
          ptimer.start ();

          for (size_t j=0; j < MULTIPLY_FACTOR; j++)
            {
              plain.acquire ();
              plain.release ();
            }

          ptimer.stop ();
          ptimer.elapsed_time (et);
          time += et.real_time;
        }
      iteration *= MULTIPLY_FACTOR;
      return time / iteration;
    }
  else
    return -1.0;
#else
  ACE_UNUSED_ARG (iteration);
  ACE_ERROR_RETURN ((LM_ERROR, "Threads are not supported on this platform."), -1);
#endif
}

int
ACE_TMAIN (int argc, ACE_TCHAR* argv[])
{
  ACE_Get_Opt get_opt (argc, argv, ACE_TEXT("n:l:pftahmxe"));
  int c;
  size_t iteration = 10;
  Profiler profiler = 0;
  const char *profile_name = 0 ;

  while ((c=get_opt ()) != -1)
    {
      switch (c)
        {
        case 'n':
          iteration = ACE_OS::atoi (get_opt.opt_arg ());
          break;
        case 'l':
          MULTIPLY_FACTOR = ACE_static_cast (size_t,
                                             ACE_OS::atoi (get_opt.opt_arg ()));
          break;
        case 'p':                       // test ACE_Process.spawn ()
          profiler = prof_ace_process;
          profile_name = "ACE_Process.spawn ()";
          break;
        case 'f':                       // test fork ()
          profiler = prof_fork;
          profile_name = "fork ()";
          break;
        case 't':                       // test native thread creation
          profiler = prof_native_thread;
          profile_name = "native threads";
          break;
        case 'a':                       // test ACE_OS::thr_create
          profiler = prof_ace_os_thread;
          profile_name = "ACE_OS::thr_create ()";
          break;
        case 'm':
          profiler = prof_tm_thread;
          profile_name = "ACE_Thread_Manager::spawn_n ()";
          break;
        case 'x':
          profiler = prof_mutex_base;
          profile_name = "ACE_Thread_Mutex Baseline";
          break;
        case 'h':                       // use high resolution timer
          ACE_High_Res_Timer::get_env_global_scale_factor ();
          break;
        case 'e':
          do_exec_after_fork = 1;
          break;
        default:
          break;
        }
    }

  if (profiler == 0)
    ACE_ERROR_RETURN ((LM_ERROR, "Usage: childbirth_time {-p|-f|-t|-a|-m|-x} [-n ###] [-L ###] [-h] [-e]\n"), 1);
  else
    {
      double time = profiler (iteration);
      if (time > 0)
        ACE_DEBUG ((LM_DEBUG,
                    "Average performance of %d iterations of %s: %.0f usec\n",
                    iteration * MULTIPLY_FACTOR, profile_name, time * 1e6));
    }
  return 0;
}