Move other tools to tools/

This commit is contained in:
2024-09-06 08:11:24 +02:00
parent 08a304a711
commit e2c770208e
8 changed files with 31 additions and 68 deletions

4
tools/.gitignore vendored Normal file
View File

@@ -0,0 +1,4 @@
plictool
csrtool
memtool
*.bin

20
tools/Makefile Normal file
View File

@@ -0,0 +1,20 @@
CFLAGS=-static
PREFIX?=/usr/local
bin=plictool csrtool memtool unalign
all: $(bin)
clean:
rm -f $(bin)
install:
mkdir -p $(PREFIX)/bin
cp -a $(bin) $(PREFIX)/bin
plictool: plictool.c
csrtool: csrtool.c
memtool: memtool.c
unalign: unalign.c

34
tools/csrtool.c Normal file
View File

@@ -0,0 +1,34 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main(int argc, char *argv[])
{
/* Print */
if (argc > 1) {
// Wait for all memory operations to finish
__asm__ volatile ("fence");
if (strcmp(argv[1], "mem-in-order") == 0) {
__asm__ volatile ("fence");
__asm__ volatile ("csrwi 0x801, 2");
} else if (strcmp(argv[1], "all-in-order") == 0) {
__asm__ volatile ("fence");
__asm__ volatile ("csrwi 0x801, 7");
} else if (strcmp(argv[1], "all-out-of-order") == 0) {
__asm__ volatile ("fence");
__asm__ volatile ("csrwi 0x801, 0");
} else {
fprintf(stderr, "unknown '%s', use: mem-in-order, all-in-order or all-out-of-order\n", argv[1]);
exit(1);
}
}
// Wait for all memory operations to finish
__asm__ volatile ("fence");
unsigned result;
asm("csrr %0, 0x801" : "=r"(result) : );
printf("CSR 0x801 = %xu\n", result);
return 0;
}

214
tools/memtool.c Normal file
View File

@@ -0,0 +1,214 @@
/* 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. */
/* Changelog:
* v0.0.1 (2024-07-10): Start version with "chain" and "fill" tests.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define MAX_SIZE (1024L * 1024L)
struct block {
struct block *next;
size_t size;
uint32_t data[];
};
struct chain {
struct block *front;
struct block *tail;
long maxsize;
long nbytes;
long nblocks;
};
static int
allocate(struct chain *chain)
{
/* Constraint the number of elements based on the maxsize */
long maxn = chain->maxsize / sizeof(uint32_t);
long n = (long) rand() % maxn;
size_t size = sizeof(struct block) + n * sizeof(uint32_t);
printf("allocating...\n");
struct block *b = malloc(size);
/* No mem */
if (b == NULL)
return -1;
b->size = size;
b->next = NULL;
/* Populate the block with some data */
printf("filling...\n");
for (long i = 0; i < n; i++)
b->data[i] = rand();
/* Add it to the chain */
if (chain->tail)
chain->tail->next = b;
chain->tail = b;
/* And to the front if it is the first */
if (!chain->front)
chain->front = b;
chain->nblocks++;
chain->nbytes += size;
return 0;
}
static int
deallocate(struct chain *chain)
{
/* May run out of blocks */
if (!chain->front)
return -1;
struct block *b = chain->front;
chain->front = b->next;
/* Last block */
if (chain->tail == b)
chain->tail = NULL;
chain->nblocks--;
chain->nbytes -= b->size;
printf("deallocating...\n");
free(b);
return 0;
}
static void
do_chain(int argc, char *argv[])
{
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);
srand(123);
for (long iter = 0; ; iter++) {
int p = rand() % 100;
int is_alloc = (p > 10);
int ret = 0;
char c;
if (is_alloc) {
if (allocate(&chain) == 0)
c = 'A';
else
c = '-';
} else {
if (deallocate(&chain) == 0)
c = 'D';
else
c = '-';
}
printf("iter=%ld nblocks=%ld allocated=%ldK (%c)\n",
iter, chain.nblocks, chain.nbytes / 1024,
c);
}
}
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);
}
int main(int argc, char *argv[])
{
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];
/* Skip mode */
argc--; argv++;
if (strcmp(mode, "chain") == 0)
do_chain(argc, argv);
else if (strcmp(mode, "fill") == 0)
do_fill(argc, argv);
else
usage();
return 0;
}

239
tools/unalign.c Normal file
View File

@@ -0,0 +1,239 @@
/*
* unalign_check - check the CPU behaviour on different alignments
* Copyright (C) 2021 Matteo Croce <mcroce@linux.microsoft.com>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <time.h>
#include <sys/mman.h>
#define ACT_READ 0
#define ACT_WRITE 1
#define ACT_XOR 2
#define ACT_COPY 3
#define READ(SIZE) \
case SIZE / 8: { \
volatile uint##SIZE##_t *buf2 = (uint##SIZE##_t *)buf; \
int i; \
for (i = 0; i < count; i++) \
(void)buf2[i]; \
break; \
}
#define WRITE(SIZE) \
case SIZE / 8: { \
volatile uint##SIZE##_t *buf2 = (uint##SIZE##_t *)buf; \
int i; \
for (i = 0; i < count; i++) \
buf2[i] = (uint##SIZE##_t)0xaabbccdd11223344; \
break; \
}
#define XOR(SIZE) \
case SIZE / 8: { \
volatile uint##SIZE##_t *buf2 = (uint##SIZE##_t *)buf; \
int i; \
for (i = 0; i < count; i++) \
buf2[i] = ~buf2[i]; \
break; \
}
#define COPY(SIZE) \
case SIZE / 8: { \
volatile uint##SIZE##_t *buf2 = (uint##SIZE##_t *)buf; \
int i; \
for (i = 0; i < count / 2; i++) \
buf2[i] = buf2[i + count / 2]; \
for (i = count / 2; i < count; i++) \
buf2[i] = buf2[i - count / 2]; \
break; \
}
static void do_read(void *buf, size_t count, int size)
{
switch (size) {
READ(8);
READ(16);
READ(32);
READ(64);
}
}
static void do_write(void *buf, size_t count, int size)
{
switch (size) {
WRITE(8);
WRITE(16);
WRITE(32);
WRITE(64);
}
}
static void do_xor(void *buf, size_t count, int size)
{
switch (size) {
XOR(8);
XOR(16);
XOR(32);
XOR(64);
}
}
static void do_copy(void *buf, size_t count, int size)
{
switch (size) {
COPY(8);
COPY(16);
COPY(32);
COPY(64);
}
}
static uint64_t time_sub(struct timespec *since, struct timespec *to)
{
if (to->tv_sec == since->tv_sec)
return to->tv_nsec - since->tv_nsec;
return (to->tv_sec - since->tv_sec) * 1000000000 + to->tv_nsec - since->tv_nsec;
}
static void __attribute__ ((noreturn)) usage(char *argv0, int ret)
{
fprintf(ret ? stderr : stdout,
"usage: %s [-rwxc1234h] [-l length] [-u unalignment]\n"
"\n"
"Options:\n"
" -r read memory (default)\n"
" -w write memory\n"
" -x xor memory\n"
" -c copy memory\n"
" -l SIZE use SIZE Mb for the test (default 100)\n"
" -u BYTE unalign buffer by BYTE bytes (default 0)\n"
" -1 read 1 byte at time\n"
" -2 read 2 bytes at time\n"
" -4 read 4 bytes at time (default)\n"
" -8 read 8 bytes at time\n"
" -h this help\n",
argv0);
exit(ret);
}
static const char *actions[] = {
"read",
"write",
"xor",
"copy",
};
int main(int argc, char *argv[])
{
struct timespec before, after;
uint64_t elapsed;
int action = ACT_READ;
size_t len = 100 * 1024 * 1024;
int shift = 0;
int size = sizeof(long);
char *buf;
int c;
while((c = getopt(argc, argv, "hrwxc1248l:u:")) != -1) {
switch (c) {
case 'r':
action = ACT_READ;
break;
case 'w':
action = ACT_WRITE;
break;
case 'x':
action = ACT_XOR;
break;
case 'c':
action = ACT_COPY;
break;
case 'l':
len = atol(optarg) * 1024 * 1024;
if (len <= 0) {
fprintf(stderr, "Invalid size %s\n", optarg);
return 1;
}
break;
case 'u':
shift = atoi(optarg);
break;
case '1':
case '2':
case '4':
case '8':
size = c - '0';
break;
case 'h':
default:
usage(argv[0], c != 'h');
}
}
shift %= size;
if (optind != argc)
usage(argv[0], 1);
buf = malloc(len);
if (!buf) {
perror("malloc");
return 1;
}
if (mlock(buf, len)) {
perror("mlock");
return 1;
}
clock_gettime(CLOCK_MONOTONIC, &before);
switch (action) {
case ACT_READ:
do_read(buf + shift, (len - shift) / size, size);
break;
case ACT_WRITE:
do_write(buf + shift, (len - shift) / size, size);
break;
case ACT_XOR:
do_xor(buf + shift, (len - shift) / size, size);
break;
case ACT_COPY:
do_copy(buf + shift, (len - shift) / size, size);
break;
}
clock_gettime(CLOCK_MONOTONIC, &after);
elapsed = time_sub(&before, &after);
printf( "size: %lu Mb\n"
"%s size: %d bit\n"
"unalignment: %d byte\n"
"elapsed time: %.2f sec\n"
"throughput: %.2f Mb/s\n",
len / 1024 / 1024,
actions[action], size * 8,
shift,
elapsed / 1E9,
(len / 1024 / 1024) / (elapsed / 1E9));
return 0;
}