Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (c) 2015 Cisco and/or its affiliates. |
| 3 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | * you may not use this file except in compliance with the License. |
| 5 | * You may obtain a copy of the License at: |
| 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
| 9 | * Unless required by applicable law or agreed to in writing, software |
| 10 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | * See the License for the specific language governing permissions and |
| 13 | * limitations under the License. |
| 14 | */ |
| 15 | /* |
| 16 | Copyright (c) 2001, 2002, 2003 Eliot Dresselhaus |
| 17 | |
| 18 | Permission is hereby granted, free of charge, to any person obtaining |
| 19 | a copy of this software and associated documentation files (the |
| 20 | "Software"), to deal in the Software without restriction, including |
| 21 | without limitation the rights to use, copy, modify, merge, publish, |
| 22 | distribute, sublicense, and/or sell copies of the Software, and to |
| 23 | permit persons to whom the Software is furnished to do so, subject to |
| 24 | the following conditions: |
| 25 | |
| 26 | The above copyright notice and this permission notice shall be |
| 27 | included in all copies or substantial portions of the Software. |
| 28 | |
| 29 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 30 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
| 31 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 32 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
| 33 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
| 34 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 35 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 36 | */ |
| 37 | |
| 38 | #include <unistd.h> |
| 39 | #include <stdlib.h> |
| 40 | |
| 41 | #include <vppinfra/mem.h> |
| 42 | #include <vppinfra/heap.h> |
| 43 | #include <vppinfra/format.h> |
| 44 | |
| 45 | static int verbose; |
| 46 | #define if_verbose(format,args...) \ |
| 47 | if (verbose) { clib_warning(format, ## args); } |
| 48 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 49 | int |
| 50 | main (int argc, char *argv[]) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 51 | { |
| 52 | word i, j, k, n, check_mask; |
| 53 | u32 seed; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 54 | u32 *h = 0; |
| 55 | uword *objects = 0; |
| 56 | uword *handles = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 57 | uword objects_used; |
| 58 | uword align, fixed_size; |
| 59 | |
| 60 | n = 10; |
| 61 | seed = (u32) getpid (); |
| 62 | check_mask = 0; |
| 63 | fixed_size = 0; |
| 64 | |
| 65 | if (argc > 1) |
| 66 | { |
| 67 | n = atoi (argv[1]); |
| 68 | verbose = 1; |
| 69 | } |
| 70 | if (argc > 2) |
| 71 | { |
| 72 | word i = atoi (argv[2]); |
| 73 | if (i) |
| 74 | seed = i; |
| 75 | } |
| 76 | if (argc > 3) |
| 77 | check_mask = atoi (argv[3]); |
| 78 | |
| 79 | align = 0; |
| 80 | if (argc > 4) |
| 81 | align = 1 << atoi (argv[4]); |
| 82 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 83 | if_verbose ("testing %wd iterations seed %wd\n", n, seed); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 84 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 85 | if (verbose) |
| 86 | fformat (stderr, "%U\n", format_clib_mem_usage, /* verbose */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 87 | |
| 88 | vec_resize (objects, 1000); |
Ole Troan | 3288ed7 | 2017-12-06 17:00:05 +0100 | [diff] [blame] | 89 | if (vec_bytes (objects) > 0) /* stupid warning be gone */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 90 | memset (objects, ~0, vec_bytes (objects)); |
| 91 | vec_resize (handles, vec_len (objects)); |
| 92 | |
| 93 | objects_used = 0; |
| 94 | |
| 95 | if (fixed_size) |
| 96 | { |
| 97 | uword max_len = 1024 * 1024; |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 98 | void *memory = clib_mem_alloc (max_len * sizeof (h[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 99 | h = heap_create_from_memory (memory, max_len, sizeof (h[0])); |
| 100 | } |
| 101 | |
| 102 | for (i = 0; i < n; i++) |
| 103 | { |
| 104 | while (1) |
| 105 | { |
| 106 | j = random_u32 (&seed) % vec_len (objects); |
| 107 | if (objects[j] != ~0 || i + objects_used < n) |
| 108 | break; |
| 109 | } |
| 110 | |
| 111 | if (objects[j] != ~0) |
| 112 | { |
| 113 | heap_dealloc (h, handles[j]); |
| 114 | objects_used--; |
| 115 | objects[j] = ~0; |
| 116 | } |
| 117 | else |
| 118 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 119 | u32 *data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 120 | uword size; |
| 121 | |
| 122 | size = 1 + (random_u32 (&seed) % 100); |
| 123 | objects[j] = heap_alloc_aligned (h, size, align, handles[j]); |
| 124 | objects_used++; |
| 125 | |
| 126 | if (align) |
| 127 | ASSERT (0 == (objects[j] & (align - 1))); |
| 128 | ASSERT (objects[j] < vec_len (h)); |
| 129 | ASSERT (size <= heap_len (h, handles[j])); |
| 130 | |
| 131 | /* Set newly allocated object with test data. */ |
| 132 | if (check_mask & 2) |
| 133 | { |
| 134 | data = h + objects[j]; |
| 135 | |
| 136 | for (k = 0; k < size; k++) |
| 137 | data[k] = objects[j] + k; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | if (check_mask & 1) |
| 142 | heap_validate (h); |
| 143 | |
| 144 | if (check_mask & 4) |
| 145 | { |
| 146 | /* Duplicate heap at each iteration. */ |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 147 | u32 *h1 = heap_dup (h); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | heap_free (h); |
| 149 | h = h1; |
| 150 | } |
| 151 | |
| 152 | /* Verify that all used objects have correct test data. */ |
| 153 | if (check_mask & 2) |
| 154 | { |
| 155 | for (j = 0; j < vec_len (objects); j++) |
| 156 | if (objects[j] != ~0) |
| 157 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 158 | u32 *data = h + objects[j]; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 159 | for (k = 0; k < heap_len (h, handles[j]); k++) |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 160 | ASSERT (data[k] == objects[j] + k); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 161 | } |
| 162 | } |
| 163 | } |
| 164 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 165 | if (verbose) |
| 166 | fformat (stderr, "%U\n", format_heap, h, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | |
| 168 | { |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 169 | u32 *h1 = heap_dup (h); |
| 170 | if (verbose) |
| 171 | fformat (stderr, "%U\n", format_heap, h1, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 172 | heap_free (h1); |
| 173 | } |
| 174 | |
| 175 | heap_free (h); |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 176 | if (verbose) |
| 177 | fformat (stderr, "%U\n", format_heap, h, 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 178 | ASSERT (objects_used == 0); |
| 179 | |
| 180 | vec_free (objects); |
| 181 | vec_free (handles); |
| 182 | |
| 183 | if (fixed_size) |
| 184 | vec_free_h (h, sizeof (heap_header_t)); |
| 185 | |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 186 | if (verbose) |
| 187 | fformat (stderr, "%U\n", format_clib_mem_usage, /* verbose */ 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 188 | |
| 189 | return 0; |
| 190 | } |
Dave Barach | c379999 | 2016-08-15 11:12:27 -0400 | [diff] [blame] | 191 | |
| 192 | /* |
| 193 | * fd.io coding-style-patch-verification: ON |
| 194 | * |
| 195 | * Local Variables: |
| 196 | * eval: (c-set-style "gnu") |
| 197 | * End: |
| 198 | */ |