Skip to content

Commit 6a59396

Browse files
add a tester app and fix optimizer
1 parent 58571d8 commit 6a59396

3 files changed

Lines changed: 387 additions & 21 deletions

File tree

staticmemory/memory-bucket-optimizer/optimizer/memory_bucket_optimizer.c

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,9 @@ int calculate_total_overhead(int num_buckets) {
8282
}
8383

8484
/* Function to parse memory allocation logs with concurrent usage tracking */
85-
int parse_memory_logs(const char* filename, AllocSize* alloc_sizes, int* num_sizes, int* peak_heap_usage) {
85+
int parse_memory_logs(const char* filename, AllocSize* alloc_sizes,
86+
int* num_sizes, int* peak_heap_usage)
87+
{
8688
FILE* file = fopen(filename, "r");
8789
if (!file) {
8890
printf("Error: Could not open file %s\n", filename);
@@ -298,8 +300,7 @@ void optimize_buckets(AllocSize* alloc_sizes, int num_sizes, int* buckets,
298300
}
299301
}
300302

301-
/* Use actual concurrent usage, but minimum of 1 */
302-
dist[*num_buckets] = (largest_concurrent > 0) ? largest_concurrent : 1;
303+
dist[*num_buckets] = largest_concurrent;
303304
(*num_buckets)++;
304305

305306
/* Add significant allocation sizes, considering padding overhead */
@@ -357,24 +358,10 @@ void optimize_buckets(AllocSize* alloc_sizes, int num_sizes, int* buckets,
357358
}
358359
}
359360

360-
/* Use concurrent usage as base, but scale based on frequency */
361-
int base_dist = (concurrent > 0) ? concurrent : 1;
361+
dist[*num_buckets] = concurrent;
362362

363-
/* Scale distribution based on frequency */
364-
if (count > total_allocations / 10) { /* >10% of allocations */
365-
dist[*num_buckets] = base_dist * 2; /* Double for high frequency */
366-
} else if (count > total_allocations / 20) { /* >5% of allocations */
367-
dist[*num_buckets] = base_dist + 2; /* Add 2 for medium frequency */
368-
} else if (count > total_allocations / 50) { /* >2% of allocations */
369-
dist[*num_buckets] = base_dist + 1; /* Add 1 for low frequency */
370-
} else {
371-
dist[*num_buckets] = base_dist; /* Use concurrent usage as is */
372-
}
373-
374-
/* Cap distribution at reasonable maximum */
375-
if (dist[*num_buckets] > 16) {
376-
dist[*num_buckets] = 16;
377-
}
363+
/* Ensure we have enough buckets for maximum concurrent usage */
364+
/* Don't cap arbitrarily - let the buffer size calculation handle memory constraints */
378365

379366
(*num_buckets)++;
380367
}
@@ -518,12 +505,14 @@ void calculate_memory_efficiency(AllocSize* alloc_sizes, int num_sizes,
518505

519506
/* Calculate total memory needed for buckets */
520507
int total_bucket_memory = 0;
508+
int total_num_buckets = 0;
521509
for (i = 0; i < num_buckets; i++) {
522510
total_bucket_memory += buckets[i] * dist[i];
511+
total_num_buckets += dist[i];
523512
}
524513

525514
/* Calculate total overhead */
526-
int total_overhead = calculate_total_overhead(num_buckets);
515+
int total_overhead = calculate_total_overhead(total_num_buckets);
527516
int total_memory_needed = total_bucket_memory + total_overhead;
528517

529518
printf("Total bucket memory: %d bytes\n", total_bucket_memory);
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# Makefile for memory bucket tester
2+
#
3+
# Copyright (C) 2025 wolfSSL Inc.
4+
#
5+
# This file is part of wolfSSL.
6+
#
7+
# wolfSSL is free software; you can redistribute it and/or modify
8+
# it under the terms of the GNU General Public License as published by
9+
# the Free Software Foundation; either version 3 of the License, or
10+
# (at your option) any later version.
11+
#
12+
# wolfSSL is distributed in the hope that it will be useful,
13+
# but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
# GNU General Public License for more details.
16+
#
17+
# You should have received a copy of the GNU General Public License
18+
# along with this program; if not, write to the Free Software
19+
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1335, USA
20+
21+
CC = gcc
22+
CFLAGS = -Wall -Wextra -O2 -g
23+
LIBS = -lwolfssl
24+
25+
TARGET = memory_bucket_tester
26+
SOURCE = memory_bucket_tester.c
27+
28+
.PHONY: all clean test
29+
30+
all: $(TARGET)
31+
32+
$(TARGET): $(SOURCE)
33+
$(CC) $(CFLAGS) $(INCLUDES) -o $@ $< $(LIBS)
34+
35+
clean:
36+
rm -f $(TARGET)
37+

0 commit comments

Comments
 (0)