diff options
author | 2021-02-15 13:08:54 +0200 | |
---|---|---|
committer | 2021-02-15 13:08:54 +0200 | |
commit | 9569e629c8e73dcdc3f93b5fc6156db827d6b7b1 (patch) | |
tree | 43453709b52f3a1d3f74059c22d85a4d89a54aca /src | |
parent | 3f0c21826b4a4e59c937dc8435edb5be7100b078 (diff) | |
download | algos-ld1-9569e629c8e73dcdc3f93b5fc6156db827d6b7b1.tar.gz algos-ld1-9569e629c8e73dcdc3f93b5fc6156db827d6b7b1.tar.bz2 algos-ld1-9569e629c8e73dcdc3f93b5fc6156db827d6b7b1.zip |
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 <gediminas@varciai.lt>
Diffstat (limited to 'src')
-rw-r--r-- | src/cache.c | 4 | ||||
-rw-r--r-- | src/datagen.c | 11 | ||||
-rw-r--r-- | src/defs.h | 2 | ||||
-rw-r--r-- | src/io.c | 22 | ||||
-rw-r--r-- | 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 <unistd.h> -#include <stddef.h> #include <errno.h> -#include <stdlib.h> #include <string.h> -#include <sys/types.h> +#include <sys/random.h> #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; @@ -93,7 +93,7 @@ enum streamtype { stream_in, stream_out, stream_outlite, - stream_chardev + stream_randread }; struct stream { @@ -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; @@ -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; |