summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorGravatar Gediminas Jakutis <gediminas@varciai.lt> 2020-02-25 09:57:14 +0200
committerGravatar Gediminas Jakutis <gediminas@varciai.lt> 2020-02-25 09:57:14 +0200
commitad1bc59382e3cda63ce507cd7f56fc2a201c11e9 (patch)
treef57c6c7c672eb2e62bc9ba61a9f32265b4abf8a8 /src
parent1bcfe887515845678f8f648c6dfecffd01813b0f (diff)
downloadalgos-ld1-ad1bc59382e3cda63ce507cd7f56fc2a201c11e9.tar.gz
algos-ld1-ad1bc59382e3cda63ce507cd7f56fc2a201c11e9.tar.bz2
algos-ld1-ad1bc59382e3cda63ce507cd7f56fc2a201c11e9.zip
implement reading from dev-you-random for datagen.
Signed-off-by: Gediminas Jakutis <gediminas@varciai.lt>
Diffstat (limited to 'src')
-rw-r--r--src/datagen.c25
-rw-r--r--src/datagen.h8
-rw-r--r--src/defs.h46
-rw-r--r--src/io.c23
-rw-r--r--src/io.h29
-rw-r--r--src/main.c42
-rw-r--r--src/meson.build3
7 files changed, 113 insertions, 63 deletions
diff --git a/src/datagen.c b/src/datagen.c
new file mode 100644
index 0000000..9efe3b3
--- /dev/null
+++ b/src/datagen.c
@@ -0,0 +1,25 @@
+#include <unistd.h>
+#include <stddef.h>
+#include <errno.h>
+#include "datagen.h"
+#include "defs.h"
+
+int gen_get(struct stream *in, size_t idx, struct entry_l *data, int tag)
+{
+ int ret = 0;
+ (void) idx;
+
+ if (tag) {
+ data->prev = 0;
+ data->next = 0;
+ }
+
+ in->prev_idx = -1;
+ ret = read(in->fd, &data->val, sizeof(data->val));
+
+ if (ret != sizeof(data->val)) {
+ ret = ret > 0 ? EAGAIN : errno;
+ }
+
+ return ret;
+}
diff --git a/src/datagen.h b/src/datagen.h
new file mode 100644
index 0000000..7e753ab
--- /dev/null
+++ b/src/datagen.h
@@ -0,0 +1,8 @@
+#ifndef ALGOS_DATAGEN_H_INCLUDED
+#define ALGOS_DATAGEN_H_INCLUDED
+
+#include "defs.h"
+
+int gen_get(struct stream *in, size_t idx, struct entry_l *data, int tag);
+
+#endif /* ALGOS_DATAGEN_H_INCLUDED */
diff --git a/src/defs.h b/src/defs.h
new file mode 100644
index 0000000..b06e181
--- /dev/null
+++ b/src/defs.h
@@ -0,0 +1,46 @@
+#ifndef ALGOS_DEFS_H_INCLUDED
+#define ALGOS_DEFS_H_INCLUDED
+
+#include <stddef.h>
+#include <stdint.h>
+#include <sys/types.h>
+
+/* for array implementation */
+struct entry {
+ uint64_t val;
+};
+
+/* for linked list implementation */
+struct entry_l {
+ struct entry;
+ uint32_t next; /* """pointer""" to the next element. */
+ uint32_t prev; /* """pointer""" to the previous element. */
+};
+
+enum opmode {
+ mode_normal,
+ mode_fetch,
+ mode_generate
+};
+
+struct stream {
+ size_t n;
+ ssize_t prev_idx;
+ int fd;
+ int out;
+ char *name;
+};
+
+struct settings {
+ size_t ss;
+ size_t to;
+ size_t stride;
+ char *filein;
+ char *fileout;
+ unsigned int flags;
+ enum opmode opmode;
+ int (*get)(struct stream, size_t, struct entry_l *, int);
+ int (*put)(struct stream, size_t, struct entry_l *, int);
+};
+
+#endif /* ALGOS_DEFS_H_INCLUDED */
diff --git a/src/io.c b/src/io.c
index f10269c..edcda17 100644
--- a/src/io.c
+++ b/src/io.c
@@ -9,24 +9,25 @@
#include <string.h>
#include <libgen.h>
#include "io.h"
+#include "defs.h"
-static int stream_open_in(struct stream * const in);
-static int stream_open_out(struct stream * const in);
+static int stream_open_in(struct stream * const in, const struct settings * const s);
+static int stream_open_out(struct stream * const in, const struct settings * const s);
static int stream_open_special(struct stream * const in);
-int stream_open(struct stream * const in)
+int stream_open(struct stream * const in, const struct settings * const s)
{
int ret = 0;
- if (!in || in->fd > 0 || !in->name) {
+ if (!in || in->fd > 0 || !in->name || !s) {
ret = EINVAL;
goto err;
}
if (in->out == 1) {
- ret = stream_open_out(in);
+ ret = stream_open_out(in, s);
} else if (!in->out) {
- ret = stream_open_in(in);
+ ret = stream_open_in(in, s);
} else {
ret = stream_open_special(in);
}
@@ -82,7 +83,7 @@ early_err:
return ret;
}
-static int stream_open_out(struct stream * const in)
+static int stream_open_out(struct stream * const in, const struct settings * const s)
{
struct stat st;
mode_t mode = S_IRUSR | S_IWUSR | S_IRGRP;
@@ -124,7 +125,7 @@ static int stream_open_out(struct stream * const in)
goto err;
}
- if (ftruncate(in->fd, in->stride * in->n)) {
+ if (ftruncate(in->fd, s->stride * in->n)) {
ret = errno;
/* TODO: error message */
goto err;
@@ -135,7 +136,7 @@ err:
return ret;
}
-static int stream_open_in(struct stream * const in)
+static int stream_open_in(struct stream * const in, const struct settings * const s)
{
struct stat st;
int ret = 0;
@@ -144,12 +145,12 @@ static int stream_open_in(struct stream * const in)
ret = errno;
/* TODO: error message */
goto err;
- } else if (!(st.st_mode & S_IFREG) || !st.st_size || (st.st_size % in->stride)) {
+ } else if (!(st.st_mode & S_IFREG) || !st.st_size || (st.st_size % s->stride)) {
ret = EINVAL;
/* TODO: error message */
goto err;
} else {
- in->n = st.st_size / in->stride;
+ in->n = st.st_size / s->stride;
}
in->fd = open(in->name, O_RDONLY | O_NOATIME);
diff --git a/src/io.h b/src/io.h
index 18e8e65..d76f2b3 100644
--- a/src/io.h
+++ b/src/io.h
@@ -1,32 +1,11 @@
#ifndef ALGOS_IO_H_INCLUDED
#define ALGOS_IO_H_INCLUDED
-#include <stddef.h>
-#include <stdint.h>
+#include "defs.h"
-struct stream {
- size_t n;
- size_t stride;
- ssize_t last_idx;
- int fd;
- int out;
- char *name;
-};
-
-/* for array implementation */
-struct entry_a {
- uint64_t val;
-};
-
-/* for linked list implementation */
-struct entry_l {
- uint32_t i; /* "pointer" to the next element. */
- uint64_t val;
-};
-
-int stream_open(struct stream * const in);
+int stream_open(struct stream * const in, const struct settings * const s);
int stream_close(struct stream * const in);
-int stream_get(struct stream *in, size_t idx, void *data);
-int stream_put(struct stream *in, size_t idx, void *data);
+int stream_get(struct stream *in, size_t idx, struct entry_l *data, int tag);
+int stream_put(struct stream *in, size_t idx, struct entry_l *data, int tag);
#endif /* ALGOS_IO_H_INCLUDED */
diff --git a/src/main.c b/src/main.c
index ee0ad52..ca3b600 100644
--- a/src/main.c
+++ b/src/main.c
@@ -5,30 +5,17 @@
#include <string.h>
#include "io.h"
-enum opmode {
- mode_normal,
- mode_fetch,
- mode_generate
-};
-
static const unsigned int FLAG_DATA_FORMAT = (1 << 0);
static const unsigned int FLAG_DATA_ACCESS = (1 << 1);
static char randfile[] = "/dev/urandom";
-static struct settings_s {
- size_t ss;
- size_t to;
- char *filein;
- char *fileout;
- unsigned int flags;
- enum opmode opmode;
-} settings = {0};
+static struct settings settings = {0};
-static struct stream file_in = {.last_idx = -1, .fd = -1};
-static struct stream file_out = {.last_idx = -1, .fd = -1, .out = 1};
+static struct stream file_in = {.prev_idx = -1, .fd = -1};
+static struct stream file_out = {.prev_idx = -1, .fd = -1, .out = 1};
-static int parseargs(int argc, char **argv, struct settings_s * settings);
+static int parseargs(int argc, char **argv, struct settings * settings);
void printhelp(const char * const name);
int main(int argc, char **argv)
@@ -42,21 +29,19 @@ int main(int argc, char **argv)
if (settings.opmode == mode_generate) {
file_in.name = randfile;
file_in.out = -1;
+ file_in.n = settings.to;
+ file_out.n = settings.to;
} else {
file_in.name = settings.filein;
}
file_out.name = settings.fileout ? settings.fileout : settings.filein;
- file_in.stride = settings.opmode & FLAG_DATA_FORMAT ? sizeof(struct entry_l) : sizeof(struct entry_a);
- if ((ret = stream_open(&file_in))) {
+ if ((ret = stream_open(&file_in, &settings))) {
goto out;
}
- file_out.n = file_in.n;
- file_out.stride = file_in.stride;
-
- if ((ret = stream_open(&file_out))) {
+ if ((ret = stream_open(&file_out, &settings))) {
goto out;
}
@@ -66,9 +51,9 @@ out:
return ret;
}
-static int parseargs(int argc, char **argv, struct settings_s * settings)
+static int parseargs(int argc, char **argv, struct settings * settings)
{
- struct settings_s s = {0};
+ struct settings s = {0};
ssize_t i;
int ret = 0;
@@ -124,13 +109,16 @@ static int parseargs(int argc, char **argv, struct settings_s * settings)
s.fileout = s.filein;
}
- s.to = s.ss + s.to;
- /* we always generate in-memory for speed */
if (s.opmode == mode_generate) {
+ /* we always generate in-memory for speed */
s.opmode &= ~FLAG_DATA_ACCESS;
+ } else if (s.opmode == mode_fetch) {
+ s.to = s.ss + s.to;
}
+ s.stride = s.opmode & FLAG_DATA_FORMAT ? sizeof(struct entry_l) : sizeof(struct entry);
+
*settings = s;
while (0) {
diff --git a/src/meson.build b/src/meson.build
index 13ff4d7..f353e54 100644
--- a/src/meson.build
+++ b/src/meson.build
@@ -1,10 +1,13 @@
source_files = [
'main.c',
'io.c',
+ 'datagen.c',
]
header_files = [
'io.h',
+ 'defs.h',
+ 'datagen.h',
]
sources = files(source_files)