summaryrefslogtreecommitdiff
path: root/examples/Shared_Malloc/test_malloc.cpp
blob: 694e534251b5337f8bc53709e02773cbfdbc762f (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
// $Id$

// This program tests out all the various ACE_Malloc combinations and
// the ACE_Allocator_Adapter.

#include "ace/Thread.h"
#include "ace/Thread_Manager.h"
#include "Malloc.h"
#include "Options.h"

// Global thread manager.
static ACE_Thread_Manager thread_manager;

static int 
gen_size (void)
{
#if defined (ACE_HAS_THREADS)
  ACE_RANDR_TYPE seed = ACE_RANDR_TYPE (&seed);
  return (ACE_OS::rand_r (ACE_RANDR_TYPE (seed)) % Options::instance ()->max_msg_size ()) + 1;
#else
  return (ACE_OS::rand () % Options::instance ()->max_msg_size ()) + 1;
#endif /* ACE_HAS_THREADS */
}

// Recursively allocate and deallocate dynamic memory.

static int
malloc_recurse (int count)
{
  static char default_char = 0;

  if (count <= 0)
    {
      if (Options::instance ()->debug ())
	AMS (Malloc::instance ()->print_stats ());
    }
  else
    {
      int alloc_size = gen_size ();
      void *ptr = Malloc::instance ()->malloc (alloc_size);

      if (ptr == 0)
	ACE_ERROR ((LM_ERROR, "(%P|%t) *** malloc of size %d failed, %p\n%a", 
		   "malloc", alloc_size));
      else
	{
	  ACE_OS::memset (ptr, default_char++, alloc_size);

	  if (Options::instance ()->debug ()) 
	    ACE_DEBUG ((LM_INFO, "(%P|%t) %u (alloc), size = %d\n", ptr, alloc_size));

	  // Call ourselves recursively
	  malloc_recurse (count - 1);

	  if (Options::instance ()->debug ()) 
	    ACE_DEBUG ((LM_INFO, "(%P|%t) %u (free), size = %d\n", ptr, alloc_size));

	  Malloc::instance ()->free (ptr);
	}
    }
  return 0;
}

static void *
worker (void *arg)
{
  // Allocate a thread control object, which automatically removes the
  // thread from the thread manager on exit.
  ACE_Thread_Control tc (&thread_manager);

  malloc_recurse (int (arg));
  return 0;
}

// Create the appropriate type of process/thread.

static void
spawn (void)
{
  if (Options::instance ()->spawn_threads ())
    {
#if defined (ACE_HAS_THREADS)
      if (thread_manager.spawn (ACE_THR_FUNC (worker),
			 (void *) Options::instance ()->iteration_count (),
			 THR_BOUND) == -1)
	ACE_ERROR ((LM_ERROR, "%p\n%a", "thread create failed"));
#else
      if (Options::instance ()->spawn_count () > 1)
	ACE_ERROR ((LM_ERROR, "only one thread may be run in a process on this platform\n%a", 1));
#endif /* ACE_HAS_THREADS */	
    }
#if !defined (ACE_WIN32)
  else
    {
      if (ACE_OS::fork () == 0)
	{
	  if (Options::instance ()->exec_slave ())
	    {
	      char iterations[20];
	      char msg_size[20];
	      
	      ACE_OS::sprintf (iterations, "%d", Options::instance ()->iteration_count ());
	      ACE_OS::sprintf (msg_size, "%d", Options::instance ()->max_msg_size ());

	      char *argv[8];
	      argv[0] = (char *) Options::instance ()->slave_name ();
	      argv[1] = "-p";
	      argv[2] = "-n";
	      argv[3] = iterations;
	      argv[4] = "-L";
	      argv[5] = msg_size;
	      argv[6] = Options::instance ()->debug () ? "-d" : "";
	      argv[7] = (char *) 0;

	      if (ACE_OS::execv (Options::instance ()->program_name (), argv) == -1)
		ACE_ERROR ((LM_ERROR, "%p\n", "exec failed"));
	      ACE_OS::_exit (1);
	    }
	  else
	    {
	      ACE_LOG_MSG->sync (Options::instance ()->program_name ());

	      ACE_DEBUG ((LM_INFO,
			  "(%P|%t) about to recurse with iteration count = %d\n",
			  Options::instance ()->iteration_count ()));

	      malloc_recurse (Options::instance ()->iteration_count ());
	      ACE_OS::exit (0);
	    }
	}
    }
#endif /* ACE_WIN32 */
}

// Wait for all the child processes/threads to exit.

static void 
wait_for_children (void)
{
  if (Options::instance ()->spawn_threads ())
    {
#if defined (ACE_HAS_THREADS)      
      // Wait for the threads to terminate.
      thread_manager.wait ();
#else
      malloc_recurse (Options::instance ()->iteration_count ());
#endif /* ACE_HAS_THREADS */	
    }
#if !defined (ACE_WIN32)
  else
    {
      pid_t pid;

      while ((pid = ACE_OS::wait (0)) != -1)
	ACE_DEBUG ((LM_DEBUG, "(%P|%t) reaped pid = %d\n", pid));
    }
#endif /* ACE_WIN32 */
}

extern "C" void 
handler (int)
{
  Malloc::instance ()->remove ();
  ACE_ERROR ((LM_ERROR, "(%P|%t) removed handler\n%a", 0));
}

int
main (int argc, char *argv[])
{
  // Register a signal handler.
  ACE_Sig_Action sa ((ACE_SignalHandler) handler, SIGINT);

  Options::instance ()->parse_args (argc, argv);

#if !defined (ACE_WIN32)
  if (Options::instance ()->child ())
    {
      ACE_DEBUG ((LM_INFO,
		  "(%P|%t) about to recurse with iteration count = %d, debug = %d\n",
		  Options::instance ()->iteration_count ()));

      // We've been forked...
      malloc_recurse (Options::instance ()->iteration_count ());
    }
  else
#endif /* ACE_WIN32 */
    {
      for (size_t i = 0; i < Options::instance ()->spawn_count (); i++)
	spawn ();

      wait_for_children ();
      Malloc::instance ()->remove ();
    }
  return 0;
}