aboutsummaryrefslogtreecommitdiffstats
path: root/src/vppinfra/test_mheap.c
blob: 6bc36b89ac3b681abe8683d11e410ce1c9d065b5 (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
/*
 * Copyright (c) 2015 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
/*
  Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus

  Permission is hereby granted, free of charge, to any person obtaining
  a copy of this software and associated documentation files (the
  "Software"), to deal in the Software without restriction, including
  without limitation the rights to use, copy, modify, merge, publish,
  distribute, sublicense, and/or sell copies of the Software, and to
  permit persons to whom the Software is furnished to do so, subject to
  the following conditions:

  The above copyright notice and this permission notice shall be
  included in all copies or substantial portions of the Software.

  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/

#ifdef CLIB_LINUX_KERNEL
#include <linux/unistd.h>
#endif

#ifdef CLIB_UNIX
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>		/* scanf */
#endif

#include <vppinfra/mheap.h>
#include <vppinfra/format.h>
#include <vppinfra/random.h>

static int verbose = 0;
#define if_verbose(format,args...) \
  if (verbose) { clib_warning(format, ## args); }

int
test_mheap_main (unformat_input_t * input)
{
  int i, j, k, n_iterations;
  void *h, *h_mem;
  uword *objects = 0;
  u32 objects_used, really_verbose, n_objects, max_object_size;
  u32 check_mask, seed, trace, use_vm;
  u32 print_every = 0;
  u32 *data;
  mheap_t *mh;

  /* Validation flags. */
  check_mask = 0;
#define CHECK_VALIDITY 1
#define CHECK_DATA     2
#define CHECK_ALIGN    4

  n_iterations = 10;
  seed = 0;
  max_object_size = 100;
  n_objects = 1000;
  trace = 0;
  really_verbose = 0;
  use_vm = 0;

  while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT)
    {
      if (0 == unformat (input, "iter %d", &n_iterations)
	  && 0 == unformat (input, "count %d", &n_objects)
	  && 0 == unformat (input, "size %d", &max_object_size)
	  && 0 == unformat (input, "seed %d", &seed)
	  && 0 == unformat (input, "print %d", &print_every)
	  && 0 == unformat (input, "validdata %|",
			    &check_mask, CHECK_DATA | CHECK_VALIDITY)
	  && 0 == unformat (input, "valid %|",
			    &check_mask, CHECK_VALIDITY)
	  && 0 == unformat (input, "verbose %=", &really_verbose, 1)
	  && 0 == unformat (input, "trace %=", &trace, 1)
	  && 0 == unformat (input, "vm %=", &use_vm, 1)
	  && 0 == unformat (input, "align %|", &check_mask, CHECK_ALIGN))
	{
	  clib_warning ("unknown input `%U'", format_unformat_error, input);
	  return 1;
	}
    }

  /* Zero seed means use default. */
  if (!seed)
    seed = random_default_seed ();

  if_verbose
    ("testing %d iterations, %d %saligned objects, max. size %d, seed %d",
     n_iterations, n_objects, (check_mask & CHECK_ALIGN) ? "randomly " : "un",
     max_object_size, seed);

  vec_resize (objects, n_objects);
  if (vec_bytes (objects))	/* stupid warning be gone */
    memset (objects, ~0, vec_bytes (objects));
  objects_used = 0;

  /* Allocate initial heap. */
  {
    uword size =
      max_pow2 (2 * n_objects * max_object_size * sizeof (data[0]));

    h_mem = clib_mem_alloc (size);
    if (!h_mem)
      return 0;

    h = mheap_alloc (h_mem, size);
  }

  if (trace)
    mheap_trace (h, trace);

  mh = mheap_header (h);

  if (use_vm)
    mh->flags &= ~MHEAP_FLAG_DISABLE_VM;
  else
    mh->flags |= MHEAP_FLAG_DISABLE_VM;

  if (check_mask & CHECK_VALIDITY)
    mh->flags |= MHEAP_FLAG_VALIDATE;

  for (i = 0; i < n_iterations; i++)
    {
      while (1)
	{
	  j = random_u32 (&seed) % vec_len (objects);
	  if (objects[j] != ~0 || i + objects_used < n_iterations)
	    break;
	}

      if (objects[j] != ~0)
	{
	  mheap_put (h, objects[j]);
	  objects_used--;
	  objects[j] = ~0;
	}
      else
	{
	  uword size, align, align_offset;

	  size = (random_u32 (&seed) % max_object_size) * sizeof (data[0]);
	  align = align_offset = 0;
	  if (check_mask & CHECK_ALIGN)
	    {
	      align = 1 << (random_u32 (&seed) % 10);
	      align_offset = round_pow2 (random_u32 (&seed) & (align - 1),
					 sizeof (u32));
	    }

	  h = mheap_get_aligned (h, size, align, align_offset, &objects[j]);

	  if (align > 0)
	    ASSERT (0 == ((objects[j] + align_offset) & (align - 1)));

	  ASSERT (objects[j] != ~0);
	  objects_used++;

	  /* Set newly allocated object with test data. */
	  if (check_mask & CHECK_DATA)
	    {
	      uword len;

	      data = (void *) h + objects[j];
	      len = mheap_len (h, data);

	      ASSERT (size <= mheap_data_bytes (h, objects[j]));

	      data[0] = len;
	      for (k = 1; k < len; k++)
		data[k] = objects[j] + k;
	    }
	}

      /* Verify that all used objects have correct test data. */
      if (check_mask & 2)
	{
	  for (j = 0; j < vec_len (objects); j++)
	    if (objects[j] != ~0)
	      {
		u32 *data = h + objects[j];
		uword len = data[0];
		for (k = 1; k < len; k++)
		  ASSERT (data[k] == objects[j] + k);
	      }
	}
      if (print_every != 0 && i > 0 && (i % print_every) == 0)
	fformat (stderr, "iteration %d: %U\n", i, format_mheap, h,
		 really_verbose);
    }

  if (verbose)
    fformat (stderr, "%U\n", format_mheap, h, really_verbose);
  mheap_free (h);
  clib_mem_free (h_mem);
  vec_free (objects);

  return 0;
}

#ifdef CLIB_UNIX
int
main (int argc, char *argv[])
{
  unformat_input_t i;
  int ret;

  verbose = (argc > 1);
  unformat_init_command_line (&i, argv);
  ret = test_mheap_main (&i);
  unformat_free (&i);

  return ret;
}
#endif /* CLIB_UNIX */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
ass="p">) { return (void *) b->opaque2; } /** \brief Get pointer to the end of buffer's data * @param b pointer to the buffer * @return pointer to tail of packet's data */ always_inline u8 * vlib_buffer_get_tail (vlib_buffer_t * b) { return b->data + b->current_data + b->current_length; } /** \brief Append uninitialized data to buffer * @param b pointer to the buffer * @param size number of uninitialized bytes * @return pointer to beginning of uninitialized data */ always_inline void * vlib_buffer_put_uninit (vlib_buffer_t * b, u8 size) { void *p = vlib_buffer_get_tail (b); /* XXX make sure there's enough space */ b->current_length += size; return p; } /** \brief Prepend uninitialized data to buffer * @param b pointer to the buffer * @param size number of uninitialized bytes * @return pointer to beginning of uninitialized data */ always_inline void * vlib_buffer_push_uninit (vlib_buffer_t * b, u8 size) { ASSERT (b->current_data + VLIB_BUFFER_PRE_DATA_SIZE >= size); b->current_data -= size; b->current_length += size; return vlib_buffer_get_current (b); } /** \brief Make head room, typically for packet headers * @param b pointer to the buffer * @param size number of head room bytes * @return pointer to start of buffer (current data) */ always_inline void * vlib_buffer_make_headroom (vlib_buffer_t * b, u8 size) { ASSERT (b->current_data + VLIB_BUFFER_PRE_DATA_SIZE >= size); b->current_data += size; return vlib_buffer_get_current (b); } /** \brief Retrieve bytes from buffer head * @param b pointer to the buffer * @param size number of bytes to pull * @return pointer to start of buffer (current data) */ always_inline void * vlib_buffer_pull (vlib_buffer_t * b, u8 size) { if (b->current_length + VLIB_BUFFER_PRE_DATA_SIZE < size) return 0; void *data = vlib_buffer_get_current (b); vlib_buffer_advance (b, size); return data; } /* Forward declaration. */ struct vlib_main_t; typedef struct vlib_buffer_free_list_t { /* Template buffer used to initialize first 16 bytes of buffers allocated on this free list. */ vlib_buffer_t buffer_init_template; /* Our index into vlib_main_t's buffer_free_list_pool. */ u32 index; /* Number of data bytes for buffers in this free list. */ u32 n_data_bytes; /* Number of buffers to allocate when we need to allocate new buffers from physmem heap. */ u32 min_n_buffers_each_physmem_alloc; /* Total number of buffers allocated from this free list. */ u32 n_alloc; /* Vector of free buffers. Each element is a byte offset into I/O heap. */ u32 *buffers; /* Memory chunks allocated for this free list recorded here so they can be freed when free list is deleted. */ void **buffer_memory_allocated; /* Free list name. */ u8 *name; /* Callback functions to initialize newly allocated buffers. If null buffers are zeroed. */ void (*buffer_init_function) (struct vlib_main_t * vm, struct vlib_buffer_free_list_t * fl, u32 * buffers, u32 n_buffers); /* Callback function to announce that buffers have been added to the freelist */ void (*buffers_added_to_freelist_function) (struct vlib_main_t * vm, struct vlib_buffer_free_list_t * fl); uword buffer_init_function_opaque; } __attribute__ ((aligned (16))) vlib_buffer_free_list_t; typedef struct { u32 (*vlib_buffer_alloc_cb) (struct vlib_main_t * vm, u32 * buffers, u32 n_buffers); u32 (*vlib_buffer_alloc_from_free_list_cb) (struct vlib_main_t * vm, u32 * buffers, u32 n_buffers, u32 free_list_index); void (*vlib_buffer_free_cb) (struct vlib_main_t * vm, u32 * buffers, u32 n_buffers); void (*vlib_buffer_free_no_next_cb) (struct vlib_main_t * vm, u32 * buffers, u32 n_buffers); void (*vlib_packet_template_init_cb) (struct vlib_main_t * vm, void *t, void *packet_data, uword n_packet_data_bytes, uword min_n_buffers_each_physmem_alloc, u8 * name); void (*vlib_buffer_delete_free_list_cb) (struct vlib_main_t * vm, u32 free_list_index); } vlib_buffer_callbacks_t; extern vlib_buffer_callbacks_t *vlib_buffer_callbacks; typedef struct { CLIB_CACHE_LINE_ALIGN_MARK (cacheline0); /* Virtual memory address and size of buffer memory, used for calculating buffer index */ uword buffer_mem_start; uword buffer_mem_size; /* Buffer free callback, for subversive activities */ u32 (*buffer_free_callback) (struct vlib_main_t * vm, u32 * buffers, u32 n_buffers, u32 follow_buffer_next); /* Pool of buffer free lists. Multiple free lists exist for packet generator which uses separate free lists for each packet stream --- so as to avoid initializing static data for each packet generated. */ vlib_buffer_free_list_t *buffer_free_list_pool; #define VLIB_BUFFER_DEFAULT_FREE_LIST_INDEX (0) #define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES VLIB_BUFFER_DATA_SIZE /* Hash table mapping buffer size (rounded to next unit of sizeof (vlib_buffer_t)) to free list index. */ uword *free_list_by_size; /* Hash table mapping buffer index into number 0 => allocated but free, 1 => allocated and not-free. If buffer index is not in hash table then this buffer has never been allocated. */ uword *buffer_known_hash; clib_spinlock_t buffer_known_hash_lockp; /* List of free-lists needing Blue Light Special announcements */ vlib_buffer_free_list_t **announce_list; /* Callbacks */ vlib_buffer_callbacks_t cb; int callbacks_registered; } vlib_buffer_main_t; void vlib_buffer_add_mem_range (struct vlib_main_t *vm, uword start, uword size); void vlib_buffer_cb_init (struct vlib_main_t *vm); typedef struct { struct vlib_main_t *vlib_main; u32 first_buffer, last_buffer; union { struct { /* Total accumulated bytes in chain starting with first_buffer. */ u32 n_total_data_bytes; /* Max number of bytes to accumulate in chain starting with first_buffer. As this limit is reached buffers are enqueued to next node. */ u32 max_n_data_bytes_per_chain; /* Next node to enqueue buffers to relative to current process node. */ u32 next_index; /* Free list to use to allocate new buffers. */ u32 free_list_index; } tx; struct { /* CLIB fifo of buffer indices waiting to be unserialized. */ u32 *buffer_fifo; /* Event type used to signal that RX buffers have been added to fifo. */ uword ready_one_time_event; } rx; }; } vlib_serialize_buffer_main_t; void serialize_open_vlib_buffer (serialize_main_t * m, struct vlib_main_t *vm, vlib_serialize_buffer_main_t * sm); void unserialize_open_vlib_buffer (serialize_main_t * m, struct vlib_main_t *vm, vlib_serialize_buffer_main_t * sm); u32 serialize_close_vlib_buffer (serialize_main_t * m); void unserialize_close_vlib_buffer (serialize_main_t * m); void *vlib_set_buffer_free_callback (struct vlib_main_t *vm, void *fp); always_inline u32 serialize_vlib_buffer_n_bytes (serialize_main_t * m) { serialize_stream_t *s = &m->stream; vlib_serialize_buffer_main_t *sm = uword_to_pointer (m->stream.data_function_opaque, vlib_serialize_buffer_main_t *); return sm->tx.n_total_data_bytes + s->current_buffer_index + vec_len (s->overflow_buffer); } /* */ /** \brief Compile time buffer trajectory tracing option Turn this on if you run into "bad monkey" contexts, and you want to know exactly which nodes they've visited... See vlib/main.c... */ #define VLIB_BUFFER_TRACE_TRAJECTORY 0 #if VLIB_BUFFER_TRACE_TRAJECTORY > 0 #define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b) (b)->pre_data[0]=0 #else #define VLIB_BUFFER_TRACE_TRAJECTORY_INIT(b) #endif /* VLIB_BUFFER_TRACE_TRAJECTORY */ #endif /* included_vlib_buffer_h */ #define VLIB_BUFFER_REGISTER_CALLBACKS(x,...) \ __VA_ARGS__ vlib_buffer_callbacks_t __##x##_buffer_callbacks; \ static void __vlib_add_buffer_callbacks_t_##x (void) \ __attribute__((__constructor__)) ; \ static void __vlib_add_buffer_callbacks_t_##x (void) \ { \ if (vlib_buffer_callbacks) \ clib_panic ("vlib buffer callbacks already registered"); \ vlib_buffer_callbacks = &__##x##_buffer_callbacks; \ } \ __VA_ARGS__ vlib_buffer_callbacks_t __##x##_buffer_callbacks /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */