1 /*
2 * Copyright (c) 2019 Intel Corporation
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6 #include <zephyr/sys/sys_heap.h>
7 #include <zephyr/sys/util.h>
8 #include <zephyr/kernel.h>
9 #include "heap.h"
10
11 struct z_heap_stress_rec {
12 void *(*alloc_fn)(void *arg, size_t bytes);
13 void (*free_fn)(void *arg, void *p);
14 void *arg;
15 size_t total_bytes;
16 struct z_heap_stress_block *blocks;
17 size_t nblocks;
18 size_t blocks_alloced;
19 size_t bytes_alloced;
20 uint32_t target_percent;
21 };
22
23 struct z_heap_stress_block {
24 void *ptr;
25 size_t sz;
26 };
27
28 /* Very simple LCRNG (from https://nuclear.llnl.gov/CNP/rng/rngman/node4.html)
29 *
30 * Here to guarantee cross-platform test repeatability.
31 */
rand32(void)32 static uint32_t rand32(void)
33 {
34 static uint64_t state = 123456789; /* seed */
35
36 state = state * 2862933555777941757UL + 3037000493UL;
37
38 return (uint32_t)(state >> 32);
39 }
40
rand_alloc_choice(struct z_heap_stress_rec * sr)41 static bool rand_alloc_choice(struct z_heap_stress_rec *sr)
42 {
43 /* Edge cases: no blocks allocated, and no space for a new one */
44 if (sr->blocks_alloced == 0) {
45 return true;
46 } else if (sr->blocks_alloced >= sr->nblocks) {
47 return false;
48 } else {
49
50 /* The way this works is to scale the chance of choosing to
51 * allocate vs. free such that it's even odds when the heap is
52 * at the target percent, with linear tapering on the low
53 * slope (i.e. we choose to always allocate with an empty
54 * heap, allocate 50% of the time when the heap is exactly at
55 * the target, and always free when above the target). In
56 * practice, the operations aren't quite symmetric (you can
57 * always free, but your allocation might fail), and the units
58 * aren't matched (we're doing math based on bytes allocated
59 * and ignoring the overhead) but this is close enough. And
60 * yes, the math here is coarse (in units of percent), but
61 * that's good enough and fits well inside 32 bit quantities.
62 * (Note precision issue when heap size is above 40MB
63 * though!).
64 */
65 __ASSERT(sr->total_bytes < 0xffffffffU / 100, "too big for u32!");
66 uint32_t full_pct = (100 * sr->bytes_alloced) / sr->total_bytes;
67 uint32_t target = sr->target_percent ? sr->target_percent : 1;
68 uint32_t free_chance = 0xffffffffU;
69
70 if (full_pct < sr->target_percent) {
71 free_chance = full_pct * (0x80000000U / target);
72 }
73
74 return rand32() > free_chance;
75 }
76 }
77
78 /* Chooses a size of block to allocate, logarithmically favoring
79 * smaller blocks (i.e. blocks twice as large are half as frequent
80 */
rand_alloc_size(struct z_heap_stress_rec * sr)81 static size_t rand_alloc_size(struct z_heap_stress_rec *sr)
82 {
83 ARG_UNUSED(sr);
84
85 /* Min scale of 4 means that the half of the requests in the
86 * smallest size have an average size of 8
87 */
88 int scale = 4 + __builtin_clz(rand32());
89
90 return rand32() & BIT_MASK(scale);
91 }
92
93 /* Returns the index of a randomly chosen block to free */
rand_free_choice(struct z_heap_stress_rec * sr)94 static size_t rand_free_choice(struct z_heap_stress_rec *sr)
95 {
96 return rand32() % sr->blocks_alloced;
97 }
98
99 /* General purpose heap stress test. Takes function pointers to allow
100 * for testing multiple heap APIs with the same rig. The alloc and
101 * free functions are passed back the argument as a context pointer.
102 * The "log" function is for readable user output. The total_bytes
103 * argument should reflect the size of the heap being tested. The
104 * scratch array is used to store temporary state and should be sized
105 * about half as large as the heap itself. Returns true on success.
106 */
sys_heap_stress(void * (* alloc_fn)(void * arg,size_t bytes),void (* free_fn)(void * arg,void * p),void * arg,size_t total_bytes,uint32_t op_count,void * scratch_mem,size_t scratch_bytes,int target_percent,struct z_heap_stress_result * result)107 void sys_heap_stress(void *(*alloc_fn)(void *arg, size_t bytes),
108 void (*free_fn)(void *arg, void *p),
109 void *arg, size_t total_bytes,
110 uint32_t op_count,
111 void *scratch_mem, size_t scratch_bytes,
112 int target_percent,
113 struct z_heap_stress_result *result)
114 {
115 struct z_heap_stress_rec sr = {
116 .alloc_fn = alloc_fn,
117 .free_fn = free_fn,
118 .arg = arg,
119 .total_bytes = total_bytes,
120 .blocks = scratch_mem,
121 .nblocks = scratch_bytes / sizeof(struct z_heap_stress_block),
122 .target_percent = target_percent,
123 };
124
125 *result = (struct z_heap_stress_result) {0};
126
127 for (uint32_t i = 0; i < op_count; i++) {
128 if (rand_alloc_choice(&sr)) {
129 size_t sz = rand_alloc_size(&sr);
130 void *p = sr.alloc_fn(sr.arg, sz);
131
132 result->total_allocs++;
133 if (p != NULL) {
134 result->successful_allocs++;
135 sr.blocks[sr.blocks_alloced].ptr = p;
136 sr.blocks[sr.blocks_alloced].sz = sz;
137 sr.blocks_alloced++;
138 sr.bytes_alloced += sz;
139 }
140 } else {
141 int b = rand_free_choice(&sr);
142 void *p = sr.blocks[b].ptr;
143 size_t sz = sr.blocks[b].sz;
144
145 result->total_frees++;
146 sr.blocks[b] = sr.blocks[sr.blocks_alloced - 1];
147 sr.blocks_alloced--;
148 sr.bytes_alloced -= sz;
149 sr.free_fn(sr.arg, p);
150 }
151 result->accumulated_in_use_bytes += sr.bytes_alloced;
152 }
153 }
154