summaryrefslogtreecommitdiff
path: root/tests/Upgradable_RW_Test.cpp
blob: 4489c29ae5645baaeacb310778a064cf5ca65ad1 (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
402
403
404
405
406
407
// $Id$

// ============================================================================
//
// = LIBRARY
//    tests
//
// = FILENAME
//    Reader_Writer_Test.cpp
//
// = DESCRIPTION
//      This test program verifies the functionality of the ACE_OS
//      implementation of readers/writer locks on Win32 and Posix
//      pthreads.
//
// = AUTHOR
//    Michael Kircher <mk1@cs.wustl.edu>, Prashant Jain, and Doug C. Schmidt
//
// ============================================================================

#include "test_config.h"
#include "ace/Synch.h"
#include "ace/Thread.h"
#include "ace/Thread_Manager.h"
#include "ace/Get_Opt.h"
#include "ace/SString.h"
#include "ace/Hash_Map_Manager.h"
#include "ace/Profile_Timer.h"

#if defined (__BORLANDC__) && __BORLANDC__ >= 0x0530
USELIB ("..\ace\aced.lib");
//---------------------------------------------------------------------------
#endif /* defined (__BORLANDC__) && __BORLANDC__ >= 0x0530 */

#if defined (ACE_HAS_THREADS)

// Default number of iterations.
#if defined (ACE_HAS_WINCE)
static size_t n_iterations = 50;
#else
static size_t n_iterations = 50;
#endif /* ACE_HAS_WINCE */

#define MAX_STRING_SIZE 200

// Default number of loops.
static size_t n_loops = 100;

// Default number of readers.
static size_t n_readers = 6;

// Default number of writers.
static size_t n_writers = 4;

// Thread id of last writer.
static ACE_thread_t shared_data;

// Lock for shared_data.
static ACE_RW_Thread_Mutex rw_mutex;

// Number of entries in the hash map
static u_int n_entries = 10;

// Try to upgrade to a write lock, by default don't try.
static u_int use_try_upgrade = 0;

static u_int upgraded = 0;
static u_int not_upgraded = 0;

// Count of the number of readers and writers.
static ACE_Atomic_Op<ACE_Thread_Mutex, int> current_readers;
static ACE_Atomic_Op<ACE_Thread_Mutex, int> current_writers;

typedef ACE_Hash_Map_Entry<ACE_CString, ACE_CString> 
        Hash_Map_Entry;
typedef ACE_Hash_Map_Manager<ACE_CString, ACE_CString, ACE_Null_Mutex> 
        Hash_Map;
typedef ACE_Hash_Map_Iterator<ACE_CString, ACE_CString, ACE_Null_Mutex> 
        Hash_Map_Iterator;

Hash_Map *hash_Map_ptr;

static int 
find (const char *search_string)
{
  ACE_CString cString1 (search_string);
  ACE_CString cString2;

  if (hash_Map_ptr->find (cString1, cString2) == 0)
    return 0;
  else 
    return 1;

#if 0
  // @@ Michael, do we need this code here?
    Hash_Map_Entry *entry;
    u_int i = 0;

    for (Hash_Map_Iterator hash_Map_Iterator (hash_Map);
         hash_Map_Iterator.next (entry) != 0;
         hash_Map_Iterator.advance (), i++)
      {
        ACE_DEBUG ((LM_DEBUG, 
                    ASYS_TEXT ("iterating (%d): [%s, %s]\n"),
                    i,
                    (ASYS_TCHAR *) entry->ext_id_.c_str (),
                    (ASYS_TCHAR *) entry->int_id_.c_str ()));

        if (entry->ext_id_.compare (ACE_CString (search_string)) == 0)
          return 0;
    }

  return 1;
#endif /* 0 */
}

// Explain usage and exit.
static void
print_usage_and_die (void)
{
  ACE_DEBUG ((LM_DEBUG,
              ASYS_TEXT ("usage: %n [-r n_readers] [-w n_writers]\n"
                         "   [-e max_entries] [-u try update] [-n iteration_count]\n")));
  ACE_OS::exit (1);
}

static void
parse_args (int argc, ASYS_TCHAR *argv[])
{
  ACE_Get_Opt get_opt (argc, argv, ASYS_TEXT ("e:r:w:n:u"));

  int c;

  while ((c = get_opt ()) != -1)
    switch (c)
    {
    case 'e':
      n_entries = ACE_OS::atoi (get_opt.optarg);
      break;
    case 'r':
      n_readers = ACE_OS::atoi (get_opt.optarg);
      break;
    case 'w':
      n_writers = ACE_OS::atoi (get_opt.optarg);
      break;
    case 'n':
      n_iterations = ACE_OS::atoi (get_opt.optarg);
      break;
    case 'u':
      use_try_upgrade = 1;
      break;
    default:
      print_usage_and_die ();
      break;
  }
}

// Iterate <n_iterations> each time checking that nobody modifies the data
// while we have a read lock.

static void *
reader (void *)
{
  // ACE_DEBUG ((LM_DEBUG, ASYS_TEXT ("(%t) reader starting\n")));

  // We use a random pause, around 2msec with 1msec jittering.
  int usecs = 1000;
  ACE_Time_Value pause (0, usecs);

  for (size_t iterations = 1;
       iterations <= n_iterations;
       iterations++)
    {
      //    ACE_OS::sleep (pause);
      int result;
      {
        ACE_Read_Guard<ACE_RW_Thread_Mutex> g (rw_mutex);

        find ("100");

        result = rw_mutex.tryacquire_write_upgrade ();
     
        // True, when we were able to upgrade.
        if (result == 0 && use_try_upgrade) 
          {
            find ("100");
            // ACE_DEBUG ((LM_DEBUG,
            //            "(%t) upgraded to write lock!\n"));
            upgraded++;

            // We were a writer.
            return 0;
          }
      }

      ACE_Thread::yield ();      

      if (result == -1 && errno == EBUSY || !use_try_upgrade)
        {
          ACE_Write_Guard<ACE_RW_Thread_Mutex> g (rw_mutex);

          not_upgraded++;

          find ("100");

          // ACE_DEBUG ((LM_DEBUG,
          //            ASYS_TEXT ("(%t) could not upgrade to write lock!\n")));
        }
      else // result == -1
        {
          ACE_ERROR ((LM_ERROR,
                      ASYS_TEXT ("(%t) failure in upgrading to write lock!\n"),
                      1));
        }
      // ACE_DEBUG ((LM_DEBUG,
      //             ASYS_TEXT ("(%t) done with reading guarded data\n")));

      // ACE_DEBUG ((LM_DEBUG,
      //            ASYS_TEXT ("(%t) reader finished %d iterations at %T\n"),
      //            iterations)); *
    }

  return 0;
}

// Iterate <n_iterations> each time modifying the global data and
// checking that nobody steps on it while we can write it.

static void *
writer (void *)
{
  // ACE_DEBUG ((LM_DEBUG,
  //             ASYS_TEXT ("(%t) writer starting\n")));

  // We use a random pause, around 2msec with 1msec jittering.
  int usecs = 1000;
  ACE_Time_Value pause (0, usecs);

  for (size_t iterations = 1;
       iterations <= n_iterations;
       iterations++)
    {
      // ACE_OS::sleep (pause);

      ACE_Write_Guard<ACE_RW_Thread_Mutex> g (rw_mutex);

      find ("100");
      ACE_Thread::yield ();

      current_writers--;
       
      // ACE_DEBUG ((LM_DEBUG,
      //             ASYS_TEXT ("(%t) write %d done at %T\n"), iterations));
    }
  return 0;
}

static void
print_stats (ACE_Profile_Timer::ACE_Elapsed_Time &elapsed_time,
             int iterations)
{
  if (iterations > 0)
    {
      elapsed_time.real_time *= ACE_ONE_SECOND_IN_MSECS;
      elapsed_time.user_time *= ACE_ONE_SECOND_IN_MSECS;
      elapsed_time.system_time *= ACE_ONE_SECOND_IN_MSECS;

      elapsed_time.real_time /= iterations;
      elapsed_time.user_time /= iterations;
      elapsed_time.system_time /= iterations;

      double tmp = 1000 / elapsed_time.real_time;

      ACE_DEBUG ((LM_DEBUG,"\n"
		  "\treal_time\t = %0.06f ms, \n"
		  "\tuser_time\t = %0.06f ms, \n"
		  "\tsystem_time\t = %0.06f ms, \n"
		  "\t%0.00f calls/second\n",
		  elapsed_time.real_time   < 0.0 ? 0.0 : elapsed_time.real_time,
		  elapsed_time.user_time   < 0.0 ? 0.0 : elapsed_time.user_time,
		  elapsed_time.system_time < 0.0 ? 0.0 : elapsed_time.system_time,
		  tmp < 0.0 ? 0.0 : tmp));
    }
  else
    ACE_ERROR ((LM_ERROR,
		"\tNo time stats printed.  Zero iterations or error ocurred.\n"));
}

int 
init (u_int kind)
{
  ACE_NEW_RETURN (hash_Map_ptr, 
                  Hash_Map (n_entries), 
                  -1);
  switch (kind)
    {
    default:
    case 0:
      // The space will be reused several times, We can do this
      // because the map copies the strings
      char entry[MAX_STRING_SIZE]; 
      ACE_CString cString1;
      ACE_CString cString2;

      for (u_int i = 0; i < n_entries; i++)
        {
          ACE_OS::sprintf(entry,"%d",i);
          cString1.set (/*entry*/"100");
          cString2.set (ACE_OS::strcat (entry,"X"));
          hash_Map_ptr->bind (cString1, cString2);
        }    
      break;                     
    }
  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Atomic_Op<ACE_Thread_Mutex, int>;
template class ACE_Read_Guard<ACE_RW_Mutex>;
template class ACE_Write_Guard<ACE_RW_Mutex>;
template class ACE_Guard<ACE_RW_Mutex>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Atomic_Op<ACE_Thread_Mutex, int>
#pragma instantiate ACE_Read_Guard<ACE_RW_Mutex>
#pragma instantiate ACE_Write_Guard<ACE_RW_Mutex>
#pragma instantiate ACE_Guard<ACE_RW_Mutex>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */

#endif /* ACE_HAS_THREADS */

// Spawn off threads.

int main (int argc, ASYS_TCHAR *argv[])
{
  ACE_START_TEST (ASYS_TEXT ("Reader_Writer_Test"));

#if defined (ACE_HAS_THREADS)
  parse_args (argc, argv);

  current_readers = 0; // Possibly already done
  current_writers = 0; // Possibly already done

  init (0);

  ACE_DEBUG ((LM_DEBUG, 
              ASYS_TEXT ("(%t) main thread starting\n")));

  ACE_Profile_Timer timer;
  ACE_Profile_Timer::ACE_Elapsed_Time elapsed_time;

  // We start an ACE_Profile_Timer here...
  timer.start ();

  if (ACE_Thread_Manager::instance ()->spawn_n (n_readers,
                                                ACE_THR_FUNC (reader),
                                                0,
                                                THR_NEW_LWP) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       ASYS_TEXT ("%p\n"), 
                       ASYS_TEXT ("spawn_n")), 1);
  else if (ACE_Thread_Manager::instance ()->spawn_n (n_writers,
                                                     ACE_THR_FUNC (writer),
                                                     0,
                                                     THR_NEW_LWP) == -1)
    ACE_ERROR_RETURN ((LM_ERROR, 
                       ASYS_TEXT ("%p\n"), 
                       ASYS_TEXT ("spawn_n")), 1);

  ACE_Thread_Manager::instance ()->wait ();

  // Stop the timer.
  timer.stop ();
  timer.elapsed_time (elapsed_time);

  // compute average time.
  print_stats (elapsed_time, 1);

  if (not_upgraded != 0 || upgraded != 0)
    ACE_DEBUG ((LM_DEBUG,
                ASYS_TEXT ("upgraded to not upgraded ratio = %f \n"),
                (float) upgraded/ (float) (not_upgraded + upgraded)));

  ACE_DEBUG ((LM_DEBUG, 
              ASYS_TEXT ("(%t) exiting main thread\n")));

  delete hash_Map_ptr;
#else
  ACE_UNUSED_ARG (argc);
  ACE_UNUSED_ARG (argv);
  ACE_ERROR ((LM_ERROR, 
              ASYS_TEXT ("threads not supported on this platform\n")));
#endif /* ACE_HAS_THREADS */
  ACE_END_TEST;
  return 0;
}

#if defined (ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION)
template class ACE_Hash_Map_Entry<MAP_STRING, MAP_STRING>;
template class ACE_Hash_Map_Manager<MAP_STRING, MAP_STRING, ACE_Null_Mutex>;
template class ACE_Hash_Map_Iterator<MAP_STRING, MAP_STRING, ACE_Null_Mutex>;
template class ACE_Hash_Map_Iterator_Base<MAP_STRING, MAP_STRING, ACE_Null_Mutex>;
#elif defined (ACE_HAS_TEMPLATE_INSTANTIATION_PRAGMA)
#pragma instantiate ACE_Hash_Map_Entry<MAP_STRING, MAP_STRING>
#pragma instantiate ACE_Hash_Map_Manager<MAP_STRING, MAP_STRING, ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Iterator<MAP_STRING, MAP_STRING, ACE_Null_Mutex>
#pragma instantiate ACE_Hash_Map_Iterator_Base<MAP_STRING, MAP_STRING, ACE_Null_Mutex>
#endif /* ACE_HAS_EXPLICIT_TEMPLATE_INSTANTIATION */