1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
|
/* SPDX-License-Identifier: LGPL-2.1-only */
/* Copyright (C) 2020 Gediminas Jakutis */
#include <errno.h>
#include <stddef.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <rin/diagnostic.h>
#include "io.h"
#include "defs.h"
#include "cache.h"
#include "datagen.h"
static char randfile[] = "/dev/urandom";
static struct settings settings = {0};
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 * settings);
int prime_io_functions(struct settings const * const s, struct stream * const in);
void printhelp(const char * const name);
static int stub_get(struct stream * const restrict in, ssize_t idx, struct entry_l * const data);
static int stub_put(struct stream * const restrict in, ssize_t idx, struct entry_l const * const data);
int main(int argc, char **argv)
{
int ret = 0;
rin_diag_init();
try_s((ret = parseargs(argc, argv, &settings)), early_out);
if (settings.opmode == mode_generate) {
file_in.name = randfile;
file_in.out = -1;
file_in.n = settings.to;
} else {
file_in.name = settings.filein;
}
ret = prime_io_functions(&settings, &file_in);
ret = prime_io_functions(&settings, &file_out);
file_out.name = settings.fileout ? settings.fileout : settings.filein;
try_s((ret = stream_open(&file_in, &settings)), out);
file_out.n = file_in.n;
try_s((ret = stream_open(&file_out, &settings)), out);
out:
stream_close(&file_in);
stream_close(&file_out);
early_out:
return ret;
}
static int parseargs(int argc, char **argv, struct settings * settings)
{
struct settings s = {0};
ssize_t i;
int ret = 0;
if (argc < 2) {
goto err;
}
for (i = 1; i < argc - 1; ++i) {
if (!(strcmp(argv[i], "--sort"))) {
s.opmode = mode_normal;
} else if (!(strcmp(argv[i], "--fetch"))) {
s.opmode = mode_fetch;
} else if (!(strcmp(argv[i], "--generate"))) {
s.opmode = mode_generate;
} else if (!(strcmp(argv[i], "--array"))) {
s.format = array;
} else if (!(strcmp(argv[i], "--list"))) {
s.format = list;
} else if (!(strcmp(argv[i], "--cache"))) {
s.access = cached;
} else if (!(strcmp(argv[i], "--no-cache"))) {
s.access = direct;
} else if (!(strncmp(argv[i], "--position=", 11))) {
if (strlen(argv[i]) > 11) {
s.ss = strtoul(argv[i] + 11, NULL, 10);
} else {
goto err;
}
} else if (!(strncmp(argv[i], "--num=", 6))) {
if (strlen(argv[i]) > 6) {
s.to = strtoul(argv[i] + 11, NULL, 10);
} else {
goto err;
}
} else if (!(strncmp(argv[i], "--out=", 6))) {
if (strlen(argv[i]) > 6) {
s.fileout = argv[i] + 6;
} else {
goto err;
}
} else {
goto err;
}
}
if (!strncmp(argv[i], "--", 2)) {
goto err;
} else {
s.filein = argv[i];
}
if (!s.fileout) {
s.fileout = s.filein;
}
if (s.opmode == mode_generate) {
/* we always generate in-memory for PERFORMANCE */
s.access = cached;
if (s.format == list) {
/* we need to generate the empty header, too */
++s.to;
}
} else if (s.opmode == mode_fetch) {
s.to = s.ss + s.to;
}
s.stride = s.format == list ? sizeof(struct entry_l) : sizeof(struct entry);
*settings = s;
while (0) {
err:
ret = EINVAL;
printhelp(*argv);
}
return ret;
}
int prime_io_functions(struct settings const * const s, struct stream * const in)
{
int ret = 0;
if (in->out == 1) {
if (s->format == array) {
in->get_element = stub_get;
in->put_element = stub_put;
in->get_element_cache = cached_get_array;
in->put_element_cache = cached_put_array;
} else {
in->get_element = stub_get;
in->put_element = stub_put;
in->get_element_cache = cached_get_list;
in->put_element_cache = cached_put_list;
}
} else if (!in->out) { /* reading streams do not support dumping */
if (s->format == array) {
in->get_element = stub_get;
in->put_element = stub_put;
in->get_element_cache = stub_get;
in->put_element_cache = stub_put;
} else {
in->get_element = stub_get;
in->put_element = stub_put;
in->get_element_cache = stub_get;
in->put_element_cache = stub_put;
}
} else { /* data generation streams do not support dumping nor any cache I/O beyond initial data generation */
if (s->format == array) {
in->get_element = gen_get_array;
in->put_element = stub_put;
in->get_element_cache = stub_get;
in->put_element_cache = stub_put;
} else {
in->get_element = gen_get_list;
in->put_element = stub_put;
in->get_element_cache = stub_get;
in->put_element_cache = stub_put;
}
}
return ret;
}
void printhelp(const char * const name)
{
printf( "This is a mergesort program and such\n"
"\n"
"usage:\t%s [options]\n FILE"
"\n"
"Options:\n"
" --sort sort mode (default)\n"
" --fetch do not sort, fetch element(s) instead\n"
" --generate generate an input dataset\n"
" --array use an array data format (default)\n"
" --list use a linked list data format\n"
" --[no]-cache cache data in memory (default) or process data in-place\n"
" --position=<num> fetch element from position <num>\n"
" --num=<num> <num> of elements to fetch/generate\n"
" --out=<name> write output to <name> file\n"
"\n"
"In case more than one same option is provided, the last one take precedence\n"
"\n",
name);
return;
}
static int stub_get(struct stream * const restrict in, ssize_t idx, struct entry_l * const data)
{
(void) in;
(void) idx;
(void) data;
rin_warn("stub!");
return ENOSYS;
}
static int stub_put(struct stream * const restrict in, ssize_t idx, struct entry_l const * const data)
{
(void) in;
(void) idx;
(void) data;
rin_warn("stub!");
return ENOSYS;
}
|