Skip to content

Commit fc8c165

Browse files
committed
ignore certification files +
performance test fio_memcpy##x primitives + Use pub/sub to monitor manager / root process (and shut down if lost) + WIP HTTP + WIP WebSockets
1 parent 7012cfa commit fc8c165

8 files changed

+993
-105
lines changed

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
# Specify filepatterns you want git to ignore.
22
tmp/*
33
.DS_Store
4+
*.pem

fio-stl.h

+500-57
Large diffs are not rendered by default.

fio-stl/001 header.h

+7
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,11 @@ extern "C" {
1212
#ifndef restrict
1313
#define restrict
1414
#endif
15+
/* C keyword - unavailable in C++ */
16+
#ifndef _Bool
17+
#define _Bool bool
18+
#endif
19+
1520
#endif
1621

1722
/* *****************************************************************************
@@ -650,10 +655,12 @@ Memory Copying Primitives
650655
#endif
651656
#endif /* FIO_MEMMOVE */
652657

658+
/** No-op. */
653659
FIO_SFUNC void *fio_memcpy0(void *restrict d, const void *restrict s) {
654660
((void)s);
655661
return d;
656662
}
663+
/** Copies 1 byte from `src` (`s`) to `dest` (`d`). */
657664
FIO_SFUNC void *fio_memcpy1(void *restrict d, const void *restrict s) {
658665
*(char *)d = *(const char *)s;
659666
return (void *)((uintptr_t)d + 1);

fio-stl/100 mem.h

+41-1
Original file line numberDiff line numberDiff line change
@@ -2534,7 +2534,47 @@ FIO_SFUNC void FIO_NAME_TEST(stl, mem_helper_speeds)(void) {
25342534
fprintf(stderr, "%zuus\n", (size_t)(end - start));
25352535
}
25362536
}
2537-
2537+
{
2538+
fprintf(stderr, "\n");
2539+
struct {
2540+
void *(*fn)(void *, const void *, size_t);
2541+
size_t bytes;
2542+
} tests[] = {
2543+
{fio_memcpy7x, 7},
2544+
{fio_memcpy15x, 15},
2545+
{fio_memcpy31x, 31},
2546+
{fio_memcpy63x, 63},
2547+
{fio_memcpy127x, 127},
2548+
{fio_memcpy255x, 255},
2549+
{fio_memcpy511x, 511},
2550+
{fio_memcpy1023x, 1023},
2551+
{fio_memcpy2047x, 2047},
2552+
{fio_memcpy4095x, 4095},
2553+
{NULL},
2554+
};
2555+
char buf[4096 * 2];
2556+
memset(buf, 0x80, 4096 * 2);
2557+
for (size_t i = 0; tests[i].bytes; ++i) {
2558+
start = fio_time_micro();
2559+
for (size_t r = 0; r < (base_repetitions << 4); ++r) {
2560+
tests[i].fn(buf, buf + 4096, ((tests[i].bytes + r) & tests[i].bytes));
2561+
FIO_COMPILER_GUARD;
2562+
}
2563+
end = fio_time_micro();
2564+
fprintf(stderr,
2565+
"\tfio_memcpy%zux\tmemcpy(a,b,%zu) \t%zuus\t",
2566+
tests[i].bytes,
2567+
tests[i].bytes,
2568+
(size_t)(end - start));
2569+
start = fio_time_micro();
2570+
for (size_t r = 0; r < (base_repetitions << 4); ++r) {
2571+
memcpy(buf, buf + 4096, ((tests[i].bytes + r) & tests[i].bytes));
2572+
FIO_COMPILER_GUARD;
2573+
}
2574+
end = fio_time_micro();
2575+
fprintf(stderr, "%zuus\n", (size_t)(end - start));
2576+
}
2577+
}
25382578
fprintf(stderr, "* Speed testing memset:\n");
25392579

25402580
for (size_t len_i = 5; len_i < 20; ++len_i) {

fio-stl/400 server.h

+16-11
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,8 @@ struct fio_listen_args {
114114
fio_queue_s *queue_for_accept;
115115
/** If the server is forked - listen on the root process or the workers? */
116116
uint8_t on_root;
117+
/** Hides "started/stopped listening" messages from log (if set). */
118+
uint8_t hide_from_log;
117119
};
118120

119121
/**
@@ -1681,12 +1683,14 @@ SFUNC void fio_srv_start(int workers) {
16811683
fio_signal_monitor(SIGPIPE, NULL, NULL);
16821684
#endif
16831685
fio___srvdata.tick = fio_time_milli();
1684-
if (workers)
1685-
FIO_LOG_DEBUG2("starting facil.io server using %d workers.", workers);
1686-
else
1687-
FIO_LOG_DEBUG2("starting facil.io server in single process mode.");
1688-
for (int i = 0; i < workers; ++i) {
1689-
fio___srv_spawn_worker(NULL, NULL);
1686+
if (workers) {
1687+
FIO_LOG_INFO("(%d) spawning %d workers.", fio___srvdata.root_pid, workers);
1688+
for (int i = 0; i < workers; ++i) {
1689+
fio___srv_spawn_worker(NULL, NULL);
1690+
}
1691+
} else {
1692+
FIO_LOG_DEBUG2("(%d) starting facil.io server in single process mode.",
1693+
fio___srvdata.root_pid);
16901694
}
16911695
fio___srv_work(!workers);
16921696
fio_signal_forget(SIGINT);
@@ -1863,10 +1867,10 @@ static void fio___srv_listen_on_data(fio_s *io) {
18631867
}
18641868
}
18651869
static void fio___srv_listen_on_close(void *settings_) {
1866-
struct fio_listen_args *s = (struct fio_listen_args *)settings_;
1867-
if ((!s->on_root && fio_srv_is_worker()) ||
1868-
(s->on_root && fio_srv_is_master()))
1869-
FIO_LOG_INFO("(%d) stopped listening on %s", fio___srvdata.pid, s->url);
1870+
struct fio_listen_args *l = (struct fio_listen_args *)settings_;
1871+
if (!l->hide_from_log && ((!l->on_root && fio_srv_is_worker()) ||
1872+
(l->on_root && fio_srv_is_master())))
1873+
FIO_LOG_INFO("(%d) stopped listening on %s", fio___srvdata.pid, l->url);
18701874
}
18711875

18721876
FIO_SFUNC void fio___srv_listen_cleanup_task(void *udata) {
@@ -1906,7 +1910,8 @@ FIO_SFUNC void fio___srv_listen_attach_task(void *udata) {
19061910
fio_attach_fd(fd, &FIO___LISTEN_PROTOCOL, l, NULL);
19071911
if (l->on_start)
19081912
l->on_start(l->udata);
1909-
FIO_LOG_INFO("(%d) started listening on %s", fio___srvdata.pid, l->url);
1913+
if (!l->hide_from_log)
1914+
FIO_LOG_INFO("(%d) started listening on %s", fio___srvdata.pid, l->url);
19101915
}
19111916

19121917
FIO_SFUNC void fio___srv_listen_attach_task_deferred(void *udata, void *ignr_) {

fio-stl/420 pubsub.h

+17-3
Original file line numberDiff line numberDiff line change
@@ -1154,6 +1154,14 @@ FIO_SFUNC void fio___letter_on_data_ipc_child(fio_s *io) {
11541154
FIO_SFUNC void fio___letter_on_close(void *p) {
11551155
fio_letter_parser_free((fio_letter_parser_s *)p);
11561156
}
1157+
FIO_SFUNC void fio___letter_on_close_in_child(void *p) {
1158+
fio___letter_on_close(p);
1159+
if (!fio_srv_is_running())
1160+
return;
1161+
fio_srv_stop();
1162+
FIO_LOG_FATAL("(%d) lost connection with manager process, shutting down!",
1163+
getpid());
1164+
}
11571165
FIO_SFUNC void fio___letter_on_timeout(fio_s *io) {
11581166
static const char ping_buf[FIO___LETTER_MINIMAL_LEN] = {0};
11591167
fio_write2(io, .buf = (char *)ping_buf, .len = FIO___LETTER_MINIMAL_LEN);
@@ -1180,7 +1188,7 @@ static fio_protocol_s FIO_LETTER_PROTOCOL_IPC_MASTER = {
11801188
static fio_protocol_s FIO_LETTER_PROTOCOL_IPC_CHILD = {
11811189
.on_attach = fio___letter_on_attach,
11821190
.on_data = fio___letter_on_data_ipc_child,
1183-
.on_close = fio___letter_on_close,
1191+
.on_close = fio___letter_on_close_in_child,
11841192
.on_timeout = fio___letter_on_timeout,
11851193
};
11861194

@@ -1192,6 +1200,11 @@ FIO_SFUNC void fio_letter_local_ipc_on_open(int fd, void *udata) {
11921200
fio_attach_fd(fd, (fio_protocol_s *)udata, NULL, NULL);
11931201
}
11941202

1203+
#if defined(DEBUG)
1204+
#define FIO___PUBSUB_HIDE_FROM_LOG 0
1205+
#else
1206+
#define FIO___PUBSUB_HIDE_FROM_LOG 1
1207+
#endif
11951208
/** Starts listening to IPC connections on a local socket. */
11961209
FIO_IFUNC void fio___pubsub_ipc_listen(void *ignr_) {
11971210
(void)ignr_;
@@ -1203,11 +1216,12 @@ FIO_IFUNC void fio___pubsub_ipc_listen(void *ignr_) {
12031216
FIO_ASSERT(!fio_listen(.url = FIO_POSTOFFICE.ipc_url,
12041217
.on_open = fio_letter_local_ipc_on_open,
12051218
.udata = (void *)&FIO_LETTER_PROTOCOL_IPC_MASTER,
1206-
.on_root = 1),
1219+
.on_root = 1,
1220+
.hide_from_log = FIO___PUBSUB_HIDE_FROM_LOG),
12071221
"(pub/sub) couldn't open a socket for "
12081222
"IPC.");
12091223
}
1210-
1224+
#undef FIO___PUBSUB_HIDE_FROM_LOG
12111225
/* *****************************************************************************
12121226
Letter Listening to Remote Connections - TODO!
12131227
***************************************************************************** */

fio-stl/431 http1 parser.h

+32-33
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/* ************************************************************************* */
22
#if !defined(FIO_INCLUDE_FILE) /* Dev test - ignore line */
33
#define FIO___DEV___ /* Development inclusion - ignore line */
4-
#define FIO_MODULE_NAME module /* Development inclusion - ignore line */
4+
#define FIO_HTTP1_PARSER /* Development inclusion - ignore line */
55
#include "./include.h" /* Development inclusion - ignore line */
66
#endif /* Development inclusion - ignore line */
77
/* *****************************************************************************
@@ -16,12 +16,12 @@
1616
1717
Copyright and License: see header file (000 copyright.h) or top of file
1818
***************************************************************************** */
19-
#if defined(FIO_HTTP1_PARSER) && !defined(H___FIO_HTTP1_PARSER___H)
20-
#define H___FIO_HTTP1_PARSER___H
19+
#if defined(FIO_HTTP1_PARSER) && !defined(H___FIO_HTTP1_PARSER___H) && \
20+
(defined(FIO_EXTERN_COMPLETE) || !defined(FIO_EXTERN))
2121
/* *****************************************************************************
2222
The HTTP/1.1 provides static functions only, always as part or implementation.
2323
***************************************************************************** */
24-
#if defined(FIO_EXTERN_COMPLETE) || !defined(FIO_EXTERN)
24+
#define H___FIO_HTTP1_PARSER___H
2525

2626
/* *****************************************************************************
2727
HTTP/1.x Parser API
@@ -171,7 +171,7 @@ static int fio_http1___start(fio_http1_parser_s *p,
171171
buf->buf = start;
172172
return fio_http1___finish(p, buf, udata);
173173
}
174-
char *eol = FIO_MEMCHR(start, '\n', buf->len);
174+
char *eol = (char *)FIO_MEMCHR(start, '\n', buf->len);
175175
if (!eol)
176176
return 1;
177177
if (start + 13 > eol) /* test for minimal data GET HTTP/1 or ### HTTP/1 */
@@ -186,35 +186,36 @@ static int fio_http1___start(fio_http1_parser_s *p,
186186
if (start[0] > ('0' - 1) && start[0] < ('9' + 1))
187187
goto parse_response_line;
188188
/* request: method path version */
189-
if (!(tmp = FIO_MEMCHR(start, ' ', eol - start)))
189+
if (!(tmp = (char *)FIO_MEMCHR(start, ' ', (size_t)(eol - start))))
190190
return -1;
191-
if (fio_http1_on_method(FIO_BUF_INFO2(start, tmp - start), udata))
191+
if (fio_http1_on_method(FIO_BUF_INFO2(start, (size_t)(tmp - start)), udata))
192192
return -1;
193193
start = tmp + 1;
194-
if (!(tmp = FIO_MEMCHR(start, ' ', eol - start)))
194+
if (!(tmp = (char *)FIO_MEMCHR(start, ' ', eol - start)))
195195
return -1;
196-
if (fio_http1_on_url(FIO_BUF_INFO2(start, tmp - start), udata))
196+
if (fio_http1_on_url(FIO_BUF_INFO2(start, (size_t)(tmp - start)), udata))
197197
return -1;
198198
start = tmp + 1;
199199
if (start >= eol)
200200
return -1;
201201
if (fio_http1_on_version(
202-
FIO_BUF_INFO2(start, ((eol - start) > 14) ? 14 : (eol - start)),
202+
FIO_BUF_INFO2(start,
203+
(size_t)(((eol - start) > 14) ? 14 : (eol - start))),
203204
udata))
204205
return -1;
205206
return (p->fn = fio_http1___read_header)(p, buf, udata);
206207

207208
parse_response_line:
208209
/* response: version code text */
209-
if (!(tmp = FIO_MEMCHR(start, ' ', eol - start)))
210+
if (!(tmp = (char *)FIO_MEMCHR(start, ' ', eol - start)))
210211
return -1;
211-
if (fio_http1_on_version(FIO_BUF_INFO2(start, (tmp - start)), udata))
212+
if (fio_http1_on_version(FIO_BUF_INFO2(start, (size_t)(tmp - start)), udata))
212213
return -1;
213214
start = tmp + 1;
214-
if (!(tmp = FIO_MEMCHR(start, ' ', eol - start)))
215+
if (!(tmp = (char *)FIO_MEMCHR(start, ' ', eol - start)))
215216
return -1;
216217
if (fio_http1_on_status(fio_atol10(&start),
217-
FIO_BUF_INFO2((tmp + 1), eol - tmp),
218+
FIO_BUF_INFO2((tmp + 1), (size_t)(eol - tmp)),
218219
udata))
219220
return -1;
220221
return (p->fn = fio_http1___read_header)(p, buf, udata);
@@ -290,19 +291,19 @@ static inline int fio_http1___on_trailer(fio_http1_parser_s *p,
290291
void *udata) {
291292
(void)p;
292293
fio_buf_info_s forbidden[] = {
293-
FIO_BUF_INFO1("authorization"),
294-
FIO_BUF_INFO1("cache-control"),
295-
FIO_BUF_INFO1("content-encoding"),
296-
FIO_BUF_INFO1("content-length"),
297-
FIO_BUF_INFO1("content-range"),
298-
FIO_BUF_INFO1("content-type"),
299-
FIO_BUF_INFO1("expect"),
300-
FIO_BUF_INFO1("host"),
301-
FIO_BUF_INFO1("max-forwards"),
302-
FIO_BUF_INFO1("set-cookie"),
303-
FIO_BUF_INFO1("te"),
304-
FIO_BUF_INFO1("trailer"),
305-
FIO_BUF_INFO1("transfer-encoding"),
294+
FIO_BUF_INFO1((char *)"authorization"),
295+
FIO_BUF_INFO1((char *)"cache-control"),
296+
FIO_BUF_INFO1((char *)"content-encoding"),
297+
FIO_BUF_INFO1((char *)"content-length"),
298+
FIO_BUF_INFO1((char *)"content-range"),
299+
FIO_BUF_INFO1((char *)"content-type"),
300+
FIO_BUF_INFO1((char *)"expect"),
301+
FIO_BUF_INFO1((char *)"host"),
302+
FIO_BUF_INFO1((char *)"max-forwards"),
303+
FIO_BUF_INFO1((char *)"set-cookie"),
304+
FIO_BUF_INFO1((char *)"te"),
305+
FIO_BUF_INFO1((char *)"trailer"),
306+
FIO_BUF_INFO1((char *)"transfer-encoding"),
306307
FIO_BUF_INFO2(NULL, 0),
307308
}; /* known forbidden headers in trailer */
308309
for (size_t i = 0; forbidden[i].buf; ++i) {
@@ -354,7 +355,7 @@ static inline int fio_http1___read_header_line(
354355
int r;
355356
for (;;) {
356357
char *start = buf->buf;
357-
char *eol = FIO_MEMCHR(start, '\n', buf->len);
358+
char *eol = (char *)FIO_MEMCHR(start, '\n', buf->len);
358359
char *div;
359360
fio_buf_info_s name, value;
360361
if (!eol)
@@ -369,15 +370,15 @@ static inline int fio_http1___read_header_line(
369370
div = fio_http1___seek_header_div(start);
370371
if (div[0] != ':')
371372
return -1;
372-
name = FIO_BUF_INFO2(start, (div - start));
373+
name = FIO_BUF_INFO2(start, (size_t)(div - start));
373374
do {
374375
++div;
375376
} while (*div == ' ' || *div == '\t');
376377

377378
if (div != eol)
378379
while (eol[-1] == ' ' || eol[-1] == '\t')
379380
--eol;
380-
value = FIO_BUF_INFO2((div == eol) ? NULL : div, (eol - div));
381+
value = FIO_BUF_INFO2((div == eol) ? NULL : div, (size_t)(eol - div));
381382
r = handler(p, name, value, udata);
382383
if (FIO_UNLIKELY(r))
383384
return r;
@@ -514,7 +515,5 @@ FIO_SFUNC void FIO_NAME_TEST(stl, FIO_MODULE_NAME)(void) {
514515
/* *****************************************************************************
515516
Cleanup
516517
***************************************************************************** */
517-
518-
#endif /* FIO_EXTERN_COMPLETE */
519518
#undef FIO_HTTP1_PARSER
520-
#endif /* FIO_HTTP1_PARSER */
519+
#endif /* FIO_HTTP1_PARSER && FIO_EXTERN_COMPLETE*/

0 commit comments

Comments
 (0)