2024-07-09 15:15:35 +02:00
|
|
|
/* Copyright (c) 2024 Barcelona Supercomputing Center (BSC)
|
|
|
|
* SPDX-License-Identifier: MIT
|
|
|
|
* Author: Rodrigo Arias Mallo <rodrigo.arias@bsc.es> */
|
|
|
|
|
|
|
|
/* This is just a small tool to exercise the memory which attempts to
|
|
|
|
* stress the virtual memory, in a crude attempt to reproduce the hangs
|
|
|
|
* that we were observing while booting NixOS. */
|
|
|
|
|
2024-07-10 11:01:32 +02:00
|
|
|
/* Changelog:
|
|
|
|
* v0.0.1 (2024-07-10): Start version with "chain" and "fill" tests.
|
|
|
|
*/
|
|
|
|
|
2024-07-09 15:15:35 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <stdint.h>
|
2024-07-10 11:01:32 +02:00
|
|
|
#include <string.h>
|
2024-07-09 15:15:35 +02:00
|
|
|
|
|
|
|
#define MAX_SIZE (1024L * 1024L)
|
|
|
|
|
|
|
|
struct block {
|
|
|
|
struct block *next;
|
|
|
|
size_t size;
|
|
|
|
uint32_t data[];
|
|
|
|
};
|
|
|
|
|
2024-07-10 11:01:32 +02:00
|
|
|
struct chain {
|
|
|
|
struct block *front;
|
|
|
|
struct block *tail;
|
|
|
|
long maxsize;
|
|
|
|
long nbytes;
|
|
|
|
long nblocks;
|
|
|
|
};
|
2024-07-09 15:15:35 +02:00
|
|
|
|
|
|
|
static int
|
2024-07-10 11:01:32 +02:00
|
|
|
allocate(struct chain *chain)
|
2024-07-09 15:15:35 +02:00
|
|
|
{
|
2024-07-09 17:58:12 +02:00
|
|
|
/* Constraint the number of elements based on the maxsize */
|
2024-07-10 11:01:32 +02:00
|
|
|
long maxn = chain->maxsize / sizeof(uint32_t);
|
2024-07-09 17:58:12 +02:00
|
|
|
long n = (long) rand() % maxn;
|
2024-07-09 15:15:35 +02:00
|
|
|
|
|
|
|
size_t size = sizeof(struct block) + n * sizeof(uint32_t);
|
|
|
|
|
2024-07-09 22:12:55 +02:00
|
|
|
printf("allocating...\n");
|
|
|
|
|
2024-07-09 15:15:35 +02:00
|
|
|
struct block *b = malloc(size);
|
|
|
|
|
|
|
|
/* No mem */
|
|
|
|
if (b == NULL)
|
|
|
|
return -1;
|
|
|
|
|
|
|
|
b->size = size;
|
|
|
|
b->next = NULL;
|
|
|
|
|
|
|
|
/* Populate the block with some data */
|
2024-07-09 22:12:55 +02:00
|
|
|
printf("filling...\n");
|
2024-07-09 15:15:35 +02:00
|
|
|
for (long i = 0; i < n; i++)
|
|
|
|
b->data[i] = rand();
|
|
|
|
|
|
|
|
/* Add it to the chain */
|
2024-07-10 11:01:32 +02:00
|
|
|
if (chain->tail)
|
|
|
|
chain->tail->next = b;
|
2024-07-09 15:15:35 +02:00
|
|
|
|
2024-07-10 11:01:32 +02:00
|
|
|
chain->tail = b;
|
2024-07-09 15:15:35 +02:00
|
|
|
|
|
|
|
/* And to the front if it is the first */
|
2024-07-10 11:01:32 +02:00
|
|
|
if (!chain->front)
|
|
|
|
chain->front = b;
|
2024-07-09 15:15:35 +02:00
|
|
|
|
2024-07-10 11:01:32 +02:00
|
|
|
chain->nblocks++;
|
|
|
|
chain->nbytes += size;
|
2024-07-09 15:15:35 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int
|
2024-07-10 11:01:32 +02:00
|
|
|
deallocate(struct chain *chain)
|
2024-07-09 15:15:35 +02:00
|
|
|
{
|
|
|
|
/* May run out of blocks */
|
2024-07-10 11:01:32 +02:00
|
|
|
if (!chain->front)
|
2024-07-09 15:15:35 +02:00
|
|
|
return -1;
|
|
|
|
|
2024-07-10 11:01:32 +02:00
|
|
|
struct block *b = chain->front;
|
2024-07-09 15:15:35 +02:00
|
|
|
|
2024-07-10 11:01:32 +02:00
|
|
|
chain->front = b->next;
|
2024-07-09 15:15:35 +02:00
|
|
|
|
|
|
|
/* Last block */
|
2024-07-10 11:01:32 +02:00
|
|
|
if (chain->tail == b)
|
|
|
|
chain->tail = NULL;
|
2024-07-09 15:15:35 +02:00
|
|
|
|
2024-07-10 11:01:32 +02:00
|
|
|
chain->nblocks--;
|
|
|
|
chain->nbytes -= b->size;
|
2024-07-09 15:15:35 +02:00
|
|
|
|
2024-07-09 22:12:55 +02:00
|
|
|
printf("deallocating...\n");
|
2024-07-09 15:15:35 +02:00
|
|
|
free(b);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
2024-07-10 11:01:32 +02:00
|
|
|
do_chain(int argc, char *argv[])
|
2024-07-09 15:15:35 +02:00
|
|
|
{
|
2024-07-10 11:01:32 +02:00
|
|
|
struct chain chain = {0};
|
|
|
|
|
|
|
|
/* Default 1 MiB */
|
|
|
|
chain.maxsize = 1024L * 1024L;
|
|
|
|
|
|
|
|
if (argc > 0)
|
|
|
|
chain.maxsize = atol(argv[0]);
|
|
|
|
|
|
|
|
printf("mode chain: maxsize=%ldK\n", chain.maxsize / 1024);
|
|
|
|
|
2024-07-09 15:15:35 +02:00
|
|
|
srand(123);
|
|
|
|
|
|
|
|
for (long iter = 0; ; iter++) {
|
|
|
|
int p = rand() % 100;
|
|
|
|
int is_alloc = (p > 10);
|
|
|
|
int ret = 0;
|
|
|
|
char c;
|
|
|
|
if (is_alloc) {
|
2024-07-10 11:01:32 +02:00
|
|
|
if (allocate(&chain) == 0)
|
2024-07-09 15:15:35 +02:00
|
|
|
c = 'A';
|
|
|
|
else
|
|
|
|
c = '-';
|
|
|
|
} else {
|
2024-07-10 11:01:32 +02:00
|
|
|
if (deallocate(&chain) == 0)
|
2024-07-09 15:15:35 +02:00
|
|
|
c = 'D';
|
|
|
|
else
|
|
|
|
c = '-';
|
|
|
|
}
|
|
|
|
|
2024-07-09 17:58:12 +02:00
|
|
|
printf("iter=%ld nblocks=%ld allocated=%ldK (%c)\n",
|
2024-07-10 11:01:32 +02:00
|
|
|
iter, chain.nblocks, chain.nbytes / 1024,
|
2024-07-09 15:15:35 +02:00
|
|
|
c);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-07-10 11:01:32 +02:00
|
|
|
static void
|
|
|
|
do_fill(int argc, char *argv[])
|
|
|
|
{
|
|
|
|
/* Default: 256 MiB */
|
|
|
|
long nbytes = 256L * 1024L * 1024L;
|
|
|
|
|
|
|
|
if (argc > 0)
|
|
|
|
nbytes = atol(argv[0]);
|
|
|
|
|
|
|
|
long n = nbytes / sizeof(int);
|
|
|
|
|
|
|
|
printf("mode fill: nbytes=%ldM, n=%ld\n",
|
|
|
|
nbytes / (1024L * 1024L), n);
|
|
|
|
|
|
|
|
int *buf = malloc(nbytes);
|
|
|
|
|
|
|
|
if (!buf) {
|
|
|
|
perror("malloc failed");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
|
|
|
for (long i = 0; i < n; i++) {
|
|
|
|
buf[i] = i;
|
|
|
|
if ((i % (1024L * 1024L)) == 0)
|
|
|
|
printf("written=%ldK, addr=%p OK\n",
|
|
|
|
i * sizeof(int) / 1024L,
|
|
|
|
&buf[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
free(buf);
|
|
|
|
|
|
|
|
printf("fill test OK\n");
|
|
|
|
}
|
|
|
|
|
|
|
|
static void
|
|
|
|
usage(void)
|
|
|
|
{
|
|
|
|
printf(
|
|
|
|
"Usage: memtool <command> [<options>...]\n"
|
|
|
|
"\n"
|
|
|
|
"Available commands:\n"
|
|
|
|
" chain [<maxsize>]\n"
|
|
|
|
" Creates a chain of blocks of random size, each up to maxsize\n"
|
|
|
|
" or 1MiB if not given. Blocks are freed with 10% probability\n"
|
|
|
|
" starting from the oldest.\n"
|
|
|
|
"\n"
|
|
|
|
" fill [<size>]\n"
|
|
|
|
" Allocates a vector of the given size (or 256 MiB if not given)\n"
|
|
|
|
" and initializes it with a increasing value per element.\n"
|
|
|
|
"\n");
|
|
|
|
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2024-07-09 15:15:35 +02:00
|
|
|
int main(int argc, char *argv[])
|
|
|
|
{
|
2024-07-10 11:01:32 +02:00
|
|
|
printf("memtool v0.0.1 - Rodrigo Arias Mallo <rodrigo.arias@bsc.es>\n");
|
|
|
|
|
|
|
|
if (argc < 2)
|
|
|
|
usage();
|
|
|
|
|
|
|
|
/* Skip program name */
|
|
|
|
argc--; argv++;
|
|
|
|
|
|
|
|
const char *mode = argv[0];
|
2024-07-09 17:58:12 +02:00
|
|
|
|
2024-07-10 11:01:32 +02:00
|
|
|
/* Skip mode */
|
|
|
|
argc--; argv++;
|
2024-07-09 17:58:12 +02:00
|
|
|
|
2024-07-10 11:01:32 +02:00
|
|
|
if (strcmp(mode, "chain") == 0)
|
|
|
|
do_chain(argc, argv);
|
|
|
|
else if (strcmp(mode, "fill") == 0)
|
|
|
|
do_fill(argc, argv);
|
|
|
|
else
|
|
|
|
usage();
|
2024-07-09 17:58:12 +02:00
|
|
|
|
2024-07-09 15:15:35 +02:00
|
|
|
return 0;
|
|
|
|
}
|