Skip to content
This repository has been archived by the owner on Nov 29, 2023. It is now read-only.

Commit

Permalink
Make more robust bench Makefile, add crypto bench
Browse files Browse the repository at this point in the history
  • Loading branch information
Ubuntu authored and archshift committed Nov 24, 2020
1 parent 1aeca1d commit 9074d2d
Show file tree
Hide file tree
Showing 2 changed files with 120 additions and 9 deletions.
30 changes: 21 additions & 9 deletions bench/Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
CFLAGS := -I . -g -lm -O3 -Wall -Wextra
CFLAGS := -I . -g -O3 -Wall -Wextra
LDFLAGS := -lm
BENCH_FLAGS := -DUSE_FREEMEM -DUSE_PAGING -DUSE_PAGE_HASH -DUSE_PAGE_CRYPTO -D__riscv_xlen=64

.PHONY: bencher clean
.PHONY: benches clean

bencher:
$(CC) $(CFLAGS) \
-DUSE_FREEMEM -DUSE_PAGING -DUSE_PAGE_HASH -D__riscv_xlen=64 \
bencher.c merkle.c \
../merkle.c ../sha256.c \
-o $@
benches: merkle crypto

MODULES := merkle.o sha256.o aes.o bench/bencher.o bench/crypto.o bench/merkle.o
OBJ_PREFIX := objs/

$(addprefix $(OBJ_PREFIX),$(MODULES)) :: $(OBJ_PREFIX)%.o : ../%.c
mkdir -p $(shell dirname $@)
$(CC) $(CFLAGS) $(BENCH_FLAGS) -c -o $@ $^

MERKLE_MODULES := merkle.o sha256.o bench/bencher.o bench/merkle.o
merkle: $(addprefix $(OBJ_PREFIX),$(MERKLE_MODULES))
$(CC) $(CFLAGS) $(BENCH_FLAGS) $^ $(LDFLAGS) -o $@

CRYPTO_MODULES := sha256.o aes.o bench/bencher.o bench/crypto.o
crypto: $(addprefix $(OBJ_PREFIX),$(CRYPTO_MODULES))
$(CC) $(CFLAGS) $(BENCH_FLAGS) $^ $(LDFLAGS) -o $@

clean:
rm -rf bencher
rm -rf merkle crypto objs/
99 changes: 99 additions & 0 deletions bench/crypto.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#define _GNU_SOURCE

#include <../aes.h>
#include <../sha256.h>
#include <assert.h>
#include <bencher.h>
#include <stdint.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mman.h>
#include <time.h>

#define RAND_BOOK_LEN 1024

typedef struct bench_ctx {
int* rand_book;
int rand_idx;
} bench_ctx_t;

void
bench_ctx__init(bench_ctx_t* ctx) {
ctx->rand_book = (int*)malloc(sizeof(int) * RAND_BOOK_LEN);
for (int i = 0; i < RAND_BOOK_LEN; i++) {
ctx->rand_book[i] = rand();
}
}

int
nop(void* _ctx) {
(void)_ctx;
return 0;
}

int
bench_sha_1byte(void* _ctx) {
SHA256_CTX sha;
volatile uint8_t byte = 0;
uint8_t hash[32];

sha256_init(&sha);
sha256_update(&sha, &byte, 1);
sha256_final(&sha, hash);

__asm__ __volatile__ ("" :: "r"(hash));
return 0;
}

int
bench_sha_32byte(void* _ctx) {
SHA256_CTX sha;
uint8_t hash[32];
bench_ctx_t *ctx = (bench_ctx_t *)_ctx;

sha256_init(&sha);
sha256_update(&sha, ctx->rand_book, 32);
sha256_final(&sha, hash);

__asm__ __volatile__ ("" :: "r"(hash));
return 0;
}

int
bench_sha_page(void* _ctx) {
SHA256_CTX sha;
uint8_t hash[32];
bench_ctx_t *ctx = (bench_ctx_t *)_ctx;
assert(sizeof(int) * RAND_BOOK_LEN >= 4096);

sha256_init(&sha);
sha256_update(&sha, ctx->rand_book, 4096);
sha256_final(&sha, hash);

__asm__ __volatile__ ("" :: "r"(hash));
return 0;
}

int
main(int argc, char** argv) {
srand(time(NULL));
bench_ctx_t ctx = {};
bench_ctx__init(&ctx);

struct bench benches[] = {
{.name = "sha 1 byte",
.init = nop,
.deinit = nop,
.iter = bench_sha_1byte},
{.name = "sha 32 byte",
.init = nop,
.deinit = nop,
.iter = bench_sha_32byte},
{.name = "sha 4096 bytes",
.init = nop,
.deinit = nop,
.iter = bench_sha_page},
};
struct bench_opts opts = bench_argp(argc, argv);
run_benches(&opts, benches, sizeof(benches) / sizeof(struct bench), &ctx);
}

0 comments on commit 9074d2d

Please sign in to comment.