-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
executable file
·376 lines (357 loc) · 10.4 KB
/
main.cpp
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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
#if (defined _WIN32) && (!defined __GNUC__)
#define _CRT_SECURE_NO_WARNINGS
#include <winsock2.h>
#ifdef small
#undef small
#endif
#define required_argument NULL
#define no_argument NULL
#define optarg "NULL"
#define optind NULL
#define getopt_long(_1,_2,_3,_4,_5) {}
struct option { const char* _1; void* _2; void* _3; char _4; };
#else
#include <unistd.h>
#include <getopt.h>
#include <sys/time.h>
#include <sys/types.h>
#endif
#include <cstdlib>
#include <cstdint>
#include <cstring>
#include <cerrno>
#include <string>
#include <thread>
#include <vector>
#if (defined _WIN32) && (!defined __GNUC__)
static const unsigned __int64 epoch = ((unsigned __int64)116444736000000000ULL);
static void gettimeofday(struct timeval* tp, struct timezone* tzp)
{
FILETIME file_time;
SYSTEMTIME system_time;
ULARGE_INTEGER ularge;
GetSystemTime(&system_time);
SystemTimeToFileTime(&system_time, &file_time);
ularge.LowPart = file_time.dwLowDateTime;
ularge.HighPart = file_time.dwHighDateTime;
tp->tv_sec = (long)((ularge.QuadPart - epoch) / 10000000L);
tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
}
static void ussleep(unsigned long usec)
{
LARGE_INTEGER interval;
interval.QuadPart = -long(10 * usec);
HANDLE time = CreateWaitableTimer(NULL, TRUE, NULL);
if (time != NULL) {
SetWaitableTimer(time, &interval, 0, NULL, NULL, 0);
WaitForSingleObject(time, INFINITE);
CloseHandle(time);
}
}
#endif
struct Runtime {
bool kmg;
float prog;
uint64_t bytes;
uint64_t total;
};
namespace {
const char* g_file = "test";
uint64_t g_total = 0x100000;
int g_bits = 32;
bool g_decrease = 0;
int g_endian = 0;
int g_interval = 1;
std::vector<uint64_t> g_begins;
Runtime g_runtime = {};
};
union Number {
// uint8_t _8v;
uint16_t _16v;
uint32_t _32v;
uint64_t _64v;
};
void parse_args(int argc, char** argv);
void usage_exit(const char* argv0 = "");
uint64_t gettime4usec();
bool isSmallEndian();
void byteSwap16(uint16_t* val);
void byteSwap32(uint32_t* val);
uint64_t size2bytes(const std::string& value);
void msleep(unsigned long ms);
template<class T>
std::vector<T> string2vector(const std::string& str, const char* split = ",")
{
std::vector<T> vec;
char* s = const_cast<char*>(str.c_str());
char* p = strtok(s, split);
T a;
while (p != nullptr) {
int v = sscanf(p, "%llx", &a);
if (v <= 0) {
break;
}
vec.push_back(a);
p = strtok(nullptr, split);
}
return vec;
}
int main(int argc, char* argv[])
{
#if (defined _WIN32) && (!defined __GNUC__)
fprintf(stdout, "\nOptions: file '%s' size=%lld, %d bits, interval=%d, %s, %s.\n", g_file, g_total, g_bits, g_interval,
g_decrease ? "decrease" : "increase", g_endian ? "bigendian" : "small-endian");
#else
if (argc <= 1) {
usage_exit(argv[0]);
}
parse_args(argc, argv);
#endif
std::thread task(
[&]()->void {
while (true) {
msleep(10);
if (!g_runtime.kmg) {
continue;
}
g_runtime.prog = g_runtime.bytes * 100.f / g_runtime.total;
fprintf(stdout, "\r%.3f %%", g_runtime.prog);
fflush(stdout);
}
}
);
if (task.joinable()) {
task.detach();
}
size_t size = 0;
bool small = isSmallEndian();
bool byteswap = !small;
if (g_endian == 0) {
byteswap = small;
}
switch (g_bits) {
case 8:
size = sizeof(uint8_t);
break;
case 16:
size = sizeof(uint16_t);
break;
case 64:
size = sizeof(uint64_t);
break;
case 32:
default:
size = sizeof(uint32_t);
break;
}
FILE* fp = fopen(g_file, "wb+");
if (fp == nullptr) {
fprintf(stderr, "fwrite open '%s' failed: %s\n", g_file, strerror(errno));
return -1;
}
size_t length = g_begins.size();
std::vector<uint64_t> values = g_begins;
if (length == 0) {
values.push_back(0);
length++;
}
g_runtime.total = g_total * length;
if (g_total < size) {
fprintf(stderr, "Total size must upper than unit size, actually: %llu < %zu.\n", g_total, size);
usage_exit(argv[0]);
} else {
g_runtime.kmg = true;
}
uint64_t count = g_total / size;
uint64_t start = gettime4usec();
for (uint64_t i = 0; i < count; i++) {
for (size_t i = 0; i < length; i++) {
Number number;
number._64v = values[i];
if (byteswap) {
switch (size) {
case sizeof(uint16_t) :
byteSwap16(reinterpret_cast<uint16_t*>(&number._16v));
break;
case sizeof(uint64_t) :
#ifdef __GNUC__
number._64v = __builtin_bswap64(reinterpret_cast<uint64_t>(number._64v));
#else
if (small) {
number._64v = htonll(number._64v);
} else {
number._64v = ntohll(number._64v);
}
#endif
break;
case sizeof(uint32_t) :
default:
byteSwap32(reinterpret_cast<uint32_t*>(&number._32v));
break;
}
}
int64_t wroteSize = fwrite(&number, size, 1, fp);
if (wroteSize != 1) {
fprintf(stderr, "fwrite(file=%s) failed: %s\n", g_file, strerror(errno));
return -2;
}
g_runtime.bytes += size;
if (!g_decrease) {
values[i] += g_interval;
} else {
values[i] -= g_interval;
}
}
}
fclose(fp);
fprintf(stdout, "\n%llu bytes write done, average speed %.3f M/s\n", static_cast<uint64_t>(length * count * size),
(g_runtime.total * 1.f) / (gettime4usec() - start) * 0x100000 / 1000000.f);
}
uint64_t gettime4usec()
{
struct timeval tv;
gettimeofday(&tv, NULL);
return (tv.tv_sec * 1000000ULL + tv.tv_usec);
}
bool isSmallEndian()
{
union {
int i;
char c;
} v;
v.i = 1;
return (!(v.c == 1));
}
void byteSwap16(uint16_t* val)
{
uint16_t v1 = (*val & 0xff00) >> 8;
uint16_t v0 = (*val & 0x00ff) << 8;
*val = (v1 | v0);
}
void byteSwap24(uint32_t* val)
{
uint32_t v1 = ((uint32_t)(*val) & 0xff) << 16;
uint32_t v0 = ((uint32_t)(*val) & 0xff0000) >> 16;
*val = (uint32_t)(v0 | v1);
}
void byteSwap32(uint32_t* val)
{
uint32_t v = *val;
*val = (((v & 0xff00) << 24)
| ((v & 0xff00) << 8) | ((v & 0xff0000) >> 8)
| ((v >> 24) & 0xff));
}
uint64_t size2bytes(const std::string& value)
{
uint64_t u64_size = 0;
for (size_t i = 0; i < value.size(); ++i) {
if (value[i] >= '0' && value[i] <= '9') {
u64_size = u64_size * 10 + value[i] - '0';
} else if (value[i] == 'T') {
u64_size = u64_size * 1024 * 1024 * 1024 * 1024;
} else if (value[i] == 'G') {
u64_size *= 1024 * 1024 * 1024;
} else if (value[i] == 'M') {
u64_size *= 1024 * 1024;
} else if (value[i] == 'K') {
u64_size *= 1024;
} else {
continue;
}
}
return u64_size;
}
void usage_exit(const char* argv0)
{
fprintf(stderr,
"\nUsage: %s [option] ARGUMENT\n"
"\n"
"-f | --file FILENAME Name of the file to save, required.\n"
"-n | --total SIZE(K/M/G) Number of total size to write, required.\n"
"-b | --bits 8/16/32/64 Bit width of every number. (default: 32)\n"
"-d | --decrease 0/1 Number to be increasing(0) or decreasing(1). (default: 0)\n"
"-e | --endian 1/0 Big endian(1) or small endian(0). (default: 0)\n"
"-i | --interval VALUE Interval value between next number. (default: 1)\n"
"-s | --start HEX Start number value 0x123. (default: 0x0)\n"
" Multi-channels if separate by ','. (eg.: 0x0,0x321,0xff)\n"
"\n",
argv0
);
exit(0);
}
void parse_args(int argc, char** argv)
{
static struct option opts[] = {
{ "file", required_argument, NULL, 'f' },
{ "total", required_argument, NULL, 'n' },
{ "bits", no_argument, NULL, 'b' },
{ "decrease", no_argument, NULL, 'd' },
{ "endian", no_argument, NULL, 'e' },
{ "interval", no_argument, NULL, 'i' },
{ "start", no_argument, NULL, 's' },
{ 0 }
};
while (1) {
int idx;
char* tail;
int c = getopt_long(argc, argv, "f:n:b:d:e:i:s:", opts, &idx);
if (c == -1) break;
switch (c) {
case 'f':
g_file = optarg;
break;
case 'n':
if (std::string(optarg).find("0x") == 0) {
g_total = strtoul(optarg, &tail, 16);
if (*tail) {
fprintf(stderr,
"invalid argument to start: %s\n",
optarg);
usage_exit(argv[0]);
}
} else {
g_total = size2bytes(optarg);
}
break;
case 'b':
g_bits = atoi(optarg);
break;
case 'd':
g_decrease = (atoi(optarg) > 0 ? 1 : 0);
break;
case 'e':
g_endian = (atoi(optarg) > 0 ? 1 : 0);
break;
case 'i':
g_interval = atoi(optarg);
break;
case 's':
g_begins = string2vector<uint64_t>(optarg);
break;
case '?':
usage_exit(argv[0]);
case 0:
break;
default:
abort();
}
}
argv += optind;
argc -= optind;
if (argc > 1) {
fprintf(stderr, "Too many arguments.\n");
usage_exit(argv[0]);
}
}
void msleep(unsigned long ms)
{
#if (defined _WIN32) && (!defined __GNUC__)
ussleep(ms * 1000);
#else
struct timespec ts = {
.tv_sec = static_cast<long>(ms / 1000),
.tv_nsec = static_cast<long>((ms % 1000) * 1000000ul)
};
nanosleep(&ts, 0);
#endif // _WIN32
}