Skip to content

Commit

Permalink
docs + add the option to unify IO related allocations on new connections
Browse files Browse the repository at this point in the history
  • Loading branch information
boazsegev committed Nov 14, 2024
1 parent 0f259bc commit df312b4
Show file tree
Hide file tree
Showing 8 changed files with 192 additions and 118 deletions.
133 changes: 74 additions & 59 deletions fio-stl.h
Original file line number Diff line number Diff line change
Expand Up @@ -34386,6 +34386,12 @@ SFUNC fio_protocol_s *fio_protocol_set(fio_s *io, fio_protocol_s *protocol);
*/
SFUNC fio_protocol_s *fio_protocol(fio_s *io);

/** Returns the a pointer to the memory buffer required by the protocol. */
IFUNC void *fio_iomem(fio_s *io);

/** Returns the length of the `iomem` buffer. */
IFUNC size_t fio_iomem_len(fio_s *io);

/** Associates a new `udata` pointer with the IO, returning the old `udata` */
FIO_IFUNC void *fio_udata_set(fio_s *io, void *udata);

Expand Down Expand Up @@ -35065,6 +35071,7 @@ FIO_SFUNC void fio___srv_init_protocol(fio_protocol_s *pr, _Bool has_tls) {
pr->io_functions.finish = io_fn.finish;
if (!pr->io_functions.cleanup)
pr->io_functions.cleanup = io_fn.cleanup;
pr->iomem_size = ((pr->iomem_size + 15ULL) & (~15ULL));
}

/* the FIO___MOCK_PROTOCOL is used to manage hijacked / zombie connections. */
Expand Down Expand Up @@ -35407,7 +35414,6 @@ struct fio_s {
uint16_t state;
uint16_t pflags;
int fd;
int iomem_size;
/* TODO? peer address buffer */
};

Expand Down Expand Up @@ -35483,6 +35489,14 @@ FIO_SFUNC void fio_s_destroy(fio_s *io) {
#include FIO_INCLUDE_FILE
#undef FIO___RECURSIVE_INCLUDE

/** Returns the a pointer to the memory buffer required by the protocol. */
IFUNC void *fio_iomem(fio_s *io) { return (void *)(io + 1); }

/** Returns the length of the `iomem` buffer. */
IFUNC size_t fio_iomem_len(fio_s *io) {
return (size_t)fio_metadata_flex_len(io);
}

static void fio___protocol_set_task(void *io_, void *old_) {
fio_s *io = (fio_s *)io_;
fio_protocol_s *old = (fio_protocol_s *)old_;
Expand Down Expand Up @@ -35536,7 +35550,6 @@ SFUNC fio_s *fio_srv_attach_fd(int fd,
goto error;
io = fio_new2(protocol->iomem_size);
FIO_ASSERT_ALLOC(io);
io->iomem_size = protocol->iomem_size;
FIO_LOG_DDEBUG2("(%d) attaching fd %d to IO object %p",
fio___srvdata.pid,
fd,
Expand Down Expand Up @@ -44926,13 +44939,6 @@ typedef enum fio___http_protocol_selector_e {
FIO___HTTP_PROTOCOL_NONE
} fio___http_protocol_selector_e;

/** Returns a facil.io protocol object with the proper protocol callbacks. */
FIO_IFUNC fio_protocol_s fio___http_protocol_get(fio___http_protocol_selector_e,
int is_client);
/** Returns an http controller object with the proper protocol callbacks. */
FIO_IFUNC fio_http_controller_s
fio___http_controller_get(fio___http_protocol_selector_e, int is_client);

/* *****************************************************************************
HTTP Protocol Container (vtable + settings storage)
***************************************************************************** */
Expand Down Expand Up @@ -44965,58 +44971,12 @@ typedef struct {
FIO_SFUNC void fio___http_on_http_direct(void *h_, void *ignr);
FIO_SFUNC void fio___http_on_http_with_public_folder(void *h_, void *ignr);
FIO_SFUNC void fio___http_on_http_client(void *h_, void *ignr);
/* move init code here*/

FIO_IFUNC fio___http_protocol_s *fio___http_protocol_init(
fio___http_protocol_s *p,
const char *url,
fio_http_settings_s s,
bool is_client) {
int should_free_tls = !s.tls;
FIO_ASSERT_ALLOC(p);
for (size_t i = 0; i < FIO___HTTP_PROTOCOL_NONE + 1; ++i) {
p->state[i].protocol =
fio___http_protocol_get((fio___http_protocol_selector_e)i, is_client);
p->state[i].controller =
fio___http_controller_get((fio___http_protocol_selector_e)i, is_client);
}
for (size_t i = 0; i < FIO___HTTP_PROTOCOL_NONE; ++i)
p->state[i].protocol.timeout = (unsigned)s.ws_timeout * 1000U;
p->state[FIO___HTTP_PROTOCOL_SSE].protocol.timeout =
(unsigned)s.sse_timeout * 1000U;
p->state[FIO___HTTP_PROTOCOL_ACCEPT].protocol.timeout =
(unsigned)s.timeout * 1000U;
p->state[FIO___HTTP_PROTOCOL_HTTP1].protocol.timeout =
(unsigned)s.timeout * 1000U;
p->state[FIO___HTTP_PROTOCOL_NONE].protocol.timeout =
(unsigned)s.timeout * 1000U;
if (url) {
fio_url_s u = fio_url_parse(url, strlen(url));
s.tls = fio_tls_from_url(s.tls, u);
if (s.tls) {
s.tls = fio_tls_dup(s.tls);
/* fio_tls_alpn_add(s.tls, "h2", fio___http_on_select_h2); // not yet */
// fio_tls_alpn_add(s.tls, "http/1.1", fio___http_on_select_h1);
fio_io_functions_s tmp_fn = fio_tls_default_io_functions(NULL);
if (!s.tls_io_func)
s.tls_io_func = &tmp_fn;
for (size_t i = 0; i < FIO___HTTP_PROTOCOL_NONE + 1; ++i)
p->state[i].protocol.io_functions = *s.tls_io_func;
if (should_free_tls)
fio_tls_free(s.tls);
}
}
p->settings = s;
p->on_http_callback = is_client ? fio___http_on_http_client
: (p->settings.public_folder.len)
? fio___http_on_http_with_public_folder
: fio___http_on_http_direct;
p->settings.public_folder.buf = p->public_folder_buf;
p->queue = fio_srv_queue();

if (s.public_folder.len)
FIO_MEMCPY(p->public_folder_buf, s.public_folder.buf, s.public_folder.len);
return p;
}
bool is_client);
/* *****************************************************************************
HTTP Connection Container
***************************************************************************** */
Expand Down Expand Up @@ -46949,6 +46909,59 @@ fio___http_controller_get(fio___http_protocol_selector_e s, int is_client) {
}
}

FIO_IFUNC fio___http_protocol_s *fio___http_protocol_init(
fio___http_protocol_s *p,
const char *url,
fio_http_settings_s s,
bool is_client) {
int should_free_tls = !s.tls;
FIO_ASSERT_ALLOC(p);
for (size_t i = 0; i < FIO___HTTP_PROTOCOL_NONE + 1; ++i) {
p->state[i].protocol =
fio___http_protocol_get((fio___http_protocol_selector_e)i, is_client);
// p->state[i].protocol.iomem_size =
// sizeof(fio___http_connection_s) + s.max_line_len;
p->state[i].controller =
fio___http_controller_get((fio___http_protocol_selector_e)i, is_client);
}
for (size_t i = 0; i < FIO___HTTP_PROTOCOL_NONE; ++i)
p->state[i].protocol.timeout = (unsigned)s.ws_timeout * 1000U;
p->state[FIO___HTTP_PROTOCOL_SSE].protocol.timeout =
(unsigned)s.sse_timeout * 1000U;
p->state[FIO___HTTP_PROTOCOL_ACCEPT].protocol.timeout =
(unsigned)s.timeout * 1000U;
p->state[FIO___HTTP_PROTOCOL_HTTP1].protocol.timeout =
(unsigned)s.timeout * 1000U;
p->state[FIO___HTTP_PROTOCOL_NONE].protocol.timeout =
(unsigned)s.timeout * 1000U;
if (url) {
fio_url_s u = fio_url_parse(url, strlen(url));
s.tls = fio_tls_from_url(s.tls, u);
if (s.tls) {
s.tls = fio_tls_dup(s.tls);
/* fio_tls_alpn_add(s.tls, "h2", fio___http_on_select_h2); // not yet */
// fio_tls_alpn_add(s.tls, "http/1.1", fio___http_on_select_h1);
fio_io_functions_s tmp_fn = fio_tls_default_io_functions(NULL);
if (!s.tls_io_func)
s.tls_io_func = &tmp_fn;
for (size_t i = 0; i < FIO___HTTP_PROTOCOL_NONE + 1; ++i)
p->state[i].protocol.io_functions = *s.tls_io_func;
if (should_free_tls)
fio_tls_free(s.tls);
}
}
p->settings = s;
p->on_http_callback = is_client ? fio___http_on_http_client
: (p->settings.public_folder.len)
? fio___http_on_http_with_public_folder
: fio___http_on_http_direct;
p->settings.public_folder.buf = p->public_folder_buf;
p->queue = fio_srv_queue();

if (s.public_folder.len)
FIO_MEMCPY(p->public_folder_buf, s.public_folder.buf, s.public_folder.len);
return p;
}
/* *****************************************************************************
HTTP Helpers
***************************************************************************** */
Expand Down Expand Up @@ -47349,9 +47362,10 @@ static int ary____test_was_destroyed = 0;
/* *****************************************************************************
Environment printout
***************************************************************************** */

#ifndef FIO_PRINT_SIZE_OF
#define FIO_PRINT_SIZE_OF(T) \
fprintf(stderr, "\t%-19s%zu Bytes\n", #T, sizeof(T))
#endif

FIO_SFUNC void FIO_NAME_TEST(stl, type_sizes)(void) {
switch (sizeof(void *)) {
Expand Down Expand Up @@ -47395,7 +47409,6 @@ FIO_SFUNC void FIO_NAME_TEST(stl, type_sizes)(void) {
}
#endif /* FIO_OS_POSIX */
}
#undef FIO_PRINT_SIZE_OF
/* *****************************************************************************

***************************************************************************** */
Expand Down Expand Up @@ -52208,6 +52221,8 @@ Test Server Modules

FIO_SFUNC void FIO_NAME_TEST(stl, server)(void) {
fprintf(stderr, "* Testing fio_srv units (TODO).\n");
FIO_PRINT_SIZE_OF(fio_protocol_s);
FIO_PRINT_SIZE_OF(fio_s);
FIO_NAME_TEST(FIO_NAME_TEST(stl, server), env)();
FIO_NAME_TEST(FIO_NAME_TEST(stl, server), tls_helpers)();
}
Expand Down
22 changes: 22 additions & 0 deletions fio-stl.md
Original file line number Diff line number Diff line change
Expand Up @@ -8541,6 +8541,8 @@ This allows reference objects structures to include a flexible array of type `FI

The `members` variable passed to the constructor will also be available to the `FIO_REF_INIT` macro.

**Note**: using `FIO_REF_FLEX_TYPE` limits the reference counter to 32 bits (rather then the native word size which **may** be 64 bits).

#### `FIO_REF_METADATA`

If defined, should be type that will be available as "meta data".
Expand Down Expand Up @@ -9930,6 +9932,26 @@ Attaches the socket in `fd` to the facio.io engine (reactor).

Returns `NULL` on error. the `fio_s` pointer must NOT be used except within proper callbacks.

#### `fio_iomem`

```c
void *fio_iomem(fio_s *io);
```

Returns the a pointer to the memory buffer required by the original protocol used when calling `fio_srv_attach_fd`.

The memory is aligned on the system's native word size (8 bytes on 64 bit systems) and is always in blocks of 16 bytes.

For example, if the `fio_protocol_s` defines `.iomem_size = 5`, than `fio_iomem` will be `16` bytes long.

#### `fio_iomem_len`

```c
size_t fio_iomem_len(fio_s *io);
```

Returns the length of the allocated `fio_iomem` buffer.

#### `fio_srv_connect`

```c
Expand Down
2 changes: 2 additions & 0 deletions fio-stl/249 reference counter.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ This allows reference objects structures to include a flexible array of type `FI

The `members` variable passed to the constructor will also be available to the `FIO_REF_INIT` macro.

**Note**: using `FIO_REF_FLEX_TYPE` limits the reference counter to 32 bits (rather then the native word size which **may** be 64 bits).

#### `FIO_REF_METADATA`

If defined, should be type that will be available as "meta data".
Expand Down
17 changes: 15 additions & 2 deletions fio-stl/400 server.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,12 @@ SFUNC fio_protocol_s *fio_protocol_set(fio_s *io, fio_protocol_s *protocol);
*/
SFUNC fio_protocol_s *fio_protocol(fio_s *io);

/** Returns the a pointer to the memory buffer required by the protocol. */
IFUNC void *fio_iomem(fio_s *io);

/** Returns the length of the `iomem` buffer. */
IFUNC size_t fio_iomem_len(fio_s *io);

/** Associates a new `udata` pointer with the IO, returning the old `udata` */
FIO_IFUNC void *fio_udata_set(fio_s *io, void *udata);

Expand Down Expand Up @@ -910,6 +916,7 @@ FIO_SFUNC void fio___srv_init_protocol(fio_protocol_s *pr, _Bool has_tls) {
pr->io_functions.finish = io_fn.finish;
if (!pr->io_functions.cleanup)
pr->io_functions.cleanup = io_fn.cleanup;
pr->iomem_size = ((pr->iomem_size + 15ULL) & (~15ULL));
}

/* the FIO___MOCK_PROTOCOL is used to manage hijacked / zombie connections. */
Expand Down Expand Up @@ -1252,7 +1259,6 @@ struct fio_s {
uint16_t state;
uint16_t pflags;
int fd;
int iomem_size;
/* TODO? peer address buffer */
};

Expand Down Expand Up @@ -1328,6 +1334,14 @@ FIO_SFUNC void fio_s_destroy(fio_s *io) {
#include FIO_INCLUDE_FILE
#undef FIO___RECURSIVE_INCLUDE

/** Returns the a pointer to the memory buffer required by the protocol. */
IFUNC void *fio_iomem(fio_s *io) { return (void *)(io + 1); }

/** Returns the length of the `iomem` buffer. */
IFUNC size_t fio_iomem_len(fio_s *io) {
return (size_t)fio_metadata_flex_len(io);
}

static void fio___protocol_set_task(void *io_, void *old_) {
fio_s *io = (fio_s *)io_;
fio_protocol_s *old = (fio_protocol_s *)old_;
Expand Down Expand Up @@ -1381,7 +1395,6 @@ SFUNC fio_s *fio_srv_attach_fd(int fd,
goto error;
io = fio_new2(protocol->iomem_size);
FIO_ASSERT_ALLOC(io);
io->iomem_size = protocol->iomem_size;
FIO_LOG_DDEBUG2("(%d) attaching fd %d to IO object %p",
fio___srvdata.pid,
fd,
Expand Down
20 changes: 20 additions & 0 deletions fio-stl/400 server.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,26 @@ Attaches the socket in `fd` to the facio.io engine (reactor).

Returns `NULL` on error. the `fio_s` pointer must NOT be used except within proper callbacks.

#### `fio_iomem`

```c
void *fio_iomem(fio_s *io);
```
Returns the a pointer to the memory buffer required by the original protocol used when calling `fio_srv_attach_fd`.
The memory is aligned on the system's native word size (8 bytes on 64 bit systems) and is always in blocks of 16 bytes.
For example, if the `fio_protocol_s` defines `.iomem_size = 5`, than `fio_iomem` will be `16` bytes long.
#### `fio_iomem_len`
```c
size_t fio_iomem_len(fio_s *io);
```

Returns the length of the allocated `fio_iomem` buffer.

#### `fio_srv_connect`

```c
Expand Down
Loading

0 comments on commit df312b4

Please sign in to comment.