From 9569e629c8e73dcdc3f93b5fc6156db827d6b7b1 Mon Sep 17 00:00:00 2001 From: Gediminas Jakutis Date: Mon, 15 Feb 2021 13:08:54 +0200 Subject: cease inefficient /dev/urandom fiddling. Instead of reading /dev/urandom like a file (i.e. overhead / extra syscalls, etc.), just use getrandom(2) directly, lol. Signed-off-by: Gediminas Jakutis --- src/cache.c | 4 ++-- src/datagen.c | 11 +++++------ src/defs.h | 2 +- src/io.c | 22 ++++------------------ src/main.c | 7 ++----- 5 files changed, 14 insertions(+), 32 deletions(-) diff --git a/src/cache.c b/src/cache.c index fe97fef..0c99322 100644 --- a/src/cache.c +++ b/src/cache.c @@ -35,8 +35,8 @@ int cache_populate(struct stream * const in) try(in->settings->access != cached, err, EINVAL, "cannot populate cache: stream is uncached"); - /* if reading a chardev, fall back to the one-element-at-a-time mode */ - if (in->type == stream_chardev) { + /* if reading a a randstream, fall back to the one-element-at-a-time mode */ + if (in->type == stream_randread) { for (i = 0; i < in->n && !ret; ++i) { errno = 0; tmp = in->get_next_element_direct(in); diff --git a/src/datagen.c b/src/datagen.c index 7eab23f..98a64fe 100644 --- a/src/datagen.c +++ b/src/datagen.c @@ -2,12 +2,9 @@ /* Copyright (C) 2020 Gediminas Jakutis */ -#include -#include #include -#include #include -#include +#include #include "datagen.h" #include "defs.h" @@ -18,12 +15,14 @@ struct entry_l *gen_get_array(struct stream * const in) struct entry_l *ret; int errn; - if (read(in->fd, &buf.val, sizeof(buf.val)) == sizeof(buf.val)) { + (void) in; /* lol says librin, lmao */ + + if (getrandom(&buf.val, sizeof(buf.val), 0ul) == sizeof(buf.val)) { ret = &buf; } else { errn = errno; ret = NULL; - rin_err("error reading /dev/urandom: %s", strerror(errn)); + rin_err("error reading urandom: %s", strerror(errn)); } return ret; diff --git a/src/defs.h b/src/defs.h index a7a678d..5f1a733 100644 --- a/src/defs.h +++ b/src/defs.h @@ -93,7 +93,7 @@ enum streamtype { stream_in, stream_out, stream_outlite, - stream_chardev + stream_randread }; struct stream { diff --git a/src/io.c b/src/io.c index 9dae2e1..682eea2 100644 --- a/src/io.c +++ b/src/io.c @@ -20,7 +20,6 @@ static int stream_open_out(struct stream * const in); static int stream_open_out_lite(struct stream * const in); static int stream_open_in(struct stream * const in); -static int stream_open_chardev(struct stream * const in); static int stream_flush(struct stream * const in); static int stream_flush_array(struct stream * const in); static int stream_flush_list(struct stream * const in); @@ -29,7 +28,7 @@ int stream_open(struct stream * const in) { int ret = 0; - try(!in || in->fd > 0 || !in->name, err, EINVAL, "invalid argunent"); + try(!in || in->fd > 0 || (!in->name && in->type != stream_randread), err, EINVAL, "invalid argunent"); switch (in->type) { case (stream_outlite): @@ -43,8 +42,8 @@ int stream_open(struct stream * const in) try(!in->name, err, EINVAL, "no filename given"); ret = stream_open_in(in); break; - case (stream_chardev): - ret = stream_open_chardev(in); + case (stream_randread): + ; /* NOOP */ break; default: try(0, err, EINVAL, "cannot open stream: stream is invalid"); @@ -58,7 +57,7 @@ int stream_close(struct stream * const in) { int ret = 0; - try(!in || in->fd < 0, early_err, EINVAL, "invalid argunent"); + try(!in || (in->fd < 0 && in->type != stream_randread), early_err, EINVAL, "invalid argunent"); if (in->type != stream_out) { goto out; @@ -166,19 +165,6 @@ err: return ret; } -static int stream_open_chardev(struct stream * const in) -{ - struct stat st; - int ret = 0; - - try(stat(in->name, &st), err, errno, "stat failed: %s", strerror(ret)); - in->fd = open(in->name, O_RDONLY); - try(in->fd < 0, err, errno, "failed opening input file: %s", strerror(ret)); - -err: - return ret; -} - int stream_flush(struct stream * const in) { int ret = 0; diff --git a/src/main.c b/src/main.c index e3455d1..390be6a 100644 --- a/src/main.c +++ b/src/main.c @@ -13,8 +13,6 @@ #include "cache.h" #include "datagen.h" -static char randfile[] = "/dev/urandom"; - static struct settings settings = {0}; static int parseargs(int argc, char **argv, struct settings * settings); @@ -38,8 +36,7 @@ int main(int argc, char **argv) try_s((ret = parseargs(argc, argv, &settings)), early_out); if (settings.opmode == mode_generate) { - file_in.name = randfile; - file_in.type = stream_chardev; + file_in.type = stream_randread; file_in.n = settings.to; } else { file_in.name = settings.filein; @@ -188,7 +185,7 @@ int load_io_functions(struct settings const * const s, struct stream * const in) } else { /* TODO */ } - } else if (in->type == stream_chardev) { /* data generation streams do not support dumping nor any cache I/O beyond initial data generation */ + } else if (in->type == stream_randread) { /* data generation streams do not support dumping nor any cache I/O beyond initial data generation */ if (s->format == array) { in->get_next_element_direct = gen_get_array; in->place_next_element_cache = cached_put_array; -- cgit v1.2.3