Skip to content

Commit efa0a80

Browse files
committed
doc + minorities
1 parent b1551e4 commit efa0a80

File tree

6 files changed

+156
-40
lines changed

6 files changed

+156
-40
lines changed

fio-stl.h

Lines changed: 58 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -3136,7 +3136,7 @@ Memory allocation macros
31363136
/** Frees allocated memory. */
31373137
#define FIO_MEM_FREE(ptr, size) fio_free((ptr))
31383138
/** Set to true of internall allocator is used (memory returned set to zero). */
3139-
#define FIO_MEM_REALLOC_IS_SAFE 1
3139+
#define FIO_MEM_REALLOC_IS_SAFE fio_realloc_is_safe()
31403140

31413141
#else /* H___FIO_MALLOC___H */
31423142
/** Reallocates memory, copying (at least) `copy_len` if necessary. */
@@ -27626,9 +27626,10 @@ FIO_SFUNC void FIO_NAME_TEST(stl, FIO_MAP_NAME)(void) {
2762627626
stderr,
2762727627
"* Testing map " FIO_MACRO2STR(FIO_MAP_NAME) " with key " FIO_MACRO2STR(
2762827628
FIO_MAP_KEY) " (=> " FIO_MACRO2STR(FIO_MAP_VALUE) ").\n");
27629+
size_t test_len_limit = (1UL << (FIO_MAP_ARRAY_LOG_LIMIT + 15));
2762927630
{ /* test set / get overwrite , FIO_MAP_EACH and evict */
2763027631
FIO_NAME(FIO_MAP_NAME, s) map = FIO_MAP_INIT;
27631-
for (size_t i = 1; i < (1UL << (FIO_MAP_ARRAY_LOG_LIMIT + 5)); ++i) {
27632+
for (size_t i = 1; i < test_len_limit; ++i) {
2763227633
FIO_NAME(FIO_MAP_NAME, set)
2763327634
(&map, FIO___M_HASH(i) i FIO___M_VAL(i) FIO___M_OLD);
2763427635
FIO_ASSERT(FIO_NAME(FIO_MAP_NAME, count)(&map) == i,
@@ -27668,6 +27669,11 @@ FIO_SFUNC void FIO_NAME_TEST(stl, FIO_MAP_NAME)(void) {
2766827669
"map FIO_MAP_EACH ordering broken? %zu != %zu",
2766927670
(size_t)(i.key),
2767027671
(size_t)(loop_test));
27672+
#else
27673+
FIO_ASSERT(i.key < test_len_limit,
27674+
"map FIO_MAP_EACH invalid data? %zu !< %zu",
27675+
(size_t)(i.key),
27676+
(size_t)(test_len_limit));
2767127677
#endif
2767227678
}
2767327679
FIO_ASSERT(loop_test == count,
@@ -27729,22 +27735,25 @@ FIO_SFUNC void FIO_NAME_TEST(stl, FIO_MAP_NAME)(void) {
2772927735
"map reserve error? %zu != %zu",
2773027736
(size_t)FIO_NAME(FIO_MAP_NAME, capa)(&map),
2773127737
4096);
27732-
for (size_t i = 1; i < 4096; ++i) {
27738+
for (size_t i = 1; i < test_len_limit; ++i) {
2773327739
FIO_NAME(FIO_MAP_NAME, set)
2773427740
(&map, FIO___M_HASH(i) i FIO___M_VAL(i) FIO___M_OLD);
2773527741
FIO_ASSERT(FIO_NAME(FIO_MAP_NAME, count)(&map) == i, "insertion failed?");
2773627742
}
27737-
for (size_t i = 1; i < 4096; ++i) {
27743+
for (size_t i = 1; i < test_len_limit; ++i) {
2773827744
FIO_ASSERT(FIO_NAME(FIO_MAP_NAME, get)(&map, FIO___M_HASH(i) i),
2773927745
"key missing?");
27746+
size_t count = FIO_NAME(FIO_MAP_NAME, count)(&map);
2774027747
FIO_NAME(FIO_MAP_NAME, remove)
2774127748
(&map, FIO___M_HASH(i) i, NULL);
2774227749
FIO_ASSERT(!FIO_NAME(FIO_MAP_NAME, get)(&map, FIO___M_HASH(i) i),
2774327750
"map_remove error?");
27744-
FIO_ASSERT(FIO_NAME(FIO_MAP_NAME, count)(&map) == 4095 - i,
27751+
FIO_ASSERT(FIO_NAME(FIO_MAP_NAME, count)(&map) == count - 1,
2774527752
"map count error after removal? %zu != %zu",
2774627753
(size_t)FIO_NAME(FIO_MAP_NAME, count)(&map),
27747-
i);
27754+
count - 1);
27755+
/* see if removal produces errors while rehashing */
27756+
FIO_NAME(FIO_MAP_NAME, compact)(&map);
2774827757
}
2774927758
FIO_NAME(FIO_MAP_NAME, destroy)(&map);
2775027759
}
@@ -37192,6 +37201,9 @@ HTTP Handle Settings
3719237201
#define FIO_HTTP_STATIC_FILE_COMPLETION 1
3719337202
#endif
3719437203

37204+
#ifndef FIO_HTTP_LOG_X_REQUEST_START
37205+
#define FIO_HTTP_LOG_X_REQUEST_START 1
37206+
#endif
3719537207
/* *****************************************************************************
3719637208
HTTP Handle Type
3719737209
***************************************************************************** */
@@ -39888,21 +39900,11 @@ SFUNC void fio___http_write_pid(fio_str_info_s *dest) {
3988839900
SFUNC void fio_http_write_log(fio_http_s *h) {
3988939901
FIO_STR_INFO_TMP_VAR(buf, 1023);
3989039902
intptr_t bytes_sent = h->sent;
39891-
uint64_t time_start, time_end;
39903+
uint64_t time_start, time_end, time_proxy = 0;
3989239904
time_start = h->received_at;
3989339905
time_end = fio_http_get_timestump();
3989439906
fio_str_info_s date = fio_http_date(time_end / FIO___HTTP_TIME_DIV);
39895-
fio___http_write_pid(&buf);
39896-
buf.buf[buf.len++] = ' ';
39897-
fio_http_from(&buf, h);
39898-
FIO_MEMCPY(buf.buf + buf.len, " - - ", 5);
39899-
FIO_MEMCPY(buf.buf + buf.len + 5, date.buf, date.len);
39900-
buf.len += date.len + 6;
39901-
buf.buf[buf.len++] = ' ';
39902-
buf.buf[buf.len++] = '\"';
39903-
fio_string_write2(
39904-
&buf,
39905-
NULL,
39907+
fio_string_write_s to_write[16] = {
3990639908
FIO_STRING_WRITE_STR_INFO(fio_keystr_info(&h->method)),
3990739909
FIO_STRING_WRITE_STR2((const char *)" ", 1),
3990839910
FIO_STRING_WRITE_STR_INFO(fio_keystr_info(&h->path)),
@@ -39915,11 +39917,48 @@ SFUNC void fio_http_write_log(fio_http_s *h) {
3991539917
: (FIO_STRING_WRITE_STR2((const char *)"---", 3))),
3991639918
FIO_STRING_WRITE_STR2((const char *)" ", 1),
3991739919
FIO_STRING_WRITE_NUM(time_end - time_start),
39918-
FIO_STRING_WRITE_STR2((const char *)(FIO___HTTP_TIME_UNIT "\r\n"), 4));
39920+
FIO_STRING_WRITE_STR2((const char *)(FIO___HTTP_TIME_UNIT "\r\n"), 4),
39921+
};
39922+
if (FIO_HTTP_LOG_X_REQUEST_START) {
39923+
/* log request wait time using x-request-start header */
39924+
fio_str_info_s xstart =
39925+
fio_http_request_header(h,
39926+
FIO_STR_INFO2((char *)"x-request-start", 15),
39927+
0);
39928+
unsigned step =
39929+
(xstart.len > 1 && (xstart.buf[0] | 32) == 't' && xstart.buf[1] == '=');
39930+
step <<= 1;
39931+
xstart.buf += step;
39932+
xstart.len -= step;
39933+
time_proxy = fio_atol(&xstart.buf);
39934+
time_proxy *= (FIO___HTTP_TIME_DIV / 1000); /* assumes info in ms */
39935+
time_proxy = time_start - time_proxy;
39936+
if (time_proxy < (512 * FIO___HTTP_TIME_DIV)) { /* was ms? */
39937+
to_write[11] =
39938+
FIO_STRING_WRITE_STR2((const char *)(FIO___HTTP_TIME_UNIT " (wait "),
39939+
9);
39940+
to_write[12] = FIO_STRING_WRITE_NUM(time_proxy);
39941+
to_write[13] =
39942+
FIO_STRING_WRITE_STR2((const char *)(FIO___HTTP_TIME_UNIT ")\r\n"),
39943+
5);
39944+
}
39945+
}
39946+
39947+
/* Write log line to buffer */
39948+
fio___http_write_pid(&buf);
39949+
buf.buf[buf.len++] = ' ';
39950+
fio_http_from(&buf, h);
39951+
FIO_MEMCPY(buf.buf + buf.len, " - - ", 5);
39952+
FIO_MEMCPY(buf.buf + buf.len + 5, date.buf, date.len);
39953+
buf.len += date.len + 6;
39954+
buf.buf[buf.len++] = ' ';
39955+
buf.buf[buf.len++] = '\"';
39956+
fio_string_write2 FIO_NOOP(&buf, NULL, to_write);
3991939957

3992039958
if (buf.buf[buf.len - 1] != '\n')
3992139959
buf.buf[buf.len++] = '\n'; /* log was truncated, data too long */
3992239960

39961+
/* Write log line to STDOUT */
3992339962
fwrite(buf.buf, 1, buf.len, stdout);
3992439963
h->received_at = time_end;
3992539964
}

fio-stl.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10604,7 +10604,13 @@ Returns a human readable string related to the HTTP status number.
1060410604
void fio_http_write_log(fio_http_s *h);
1060510605
```
1060610606

10607-
Logs an HTTP (response) to STDOUT.
10607+
Logs an HTTP (response) to STDOUT using common log format:
10608+
10609+
```txt
10610+
[PID] ADDR - - [DATE/TIME] REQ RES_CODE BYTES_SENT TIME_SPENT_IN_APP <(wait PROXY_DELAY)>
10611+
```
10612+
10613+
See also the `FIO_HTTP_LOG_X_REQUEST_START` and `FIO_HTTP_EXACT_LOGGING` compilation flags.
1060810614

1060910615
### HTTP WebSocket / SSE Helpers
1061010616

@@ -10908,6 +10914,19 @@ On the other hand, `FIO_HTTP_EXACT_LOGGING` collects exact time stamps to measur
1090810914

1090910915
Due to the preference to err on the side of higher performance, fuzzy time-stamping is the default.
1091010916

10917+
10918+
#### `FIO_HTTP_LOG_X_REQUEST_START`
10919+
10920+
```c
10921+
#ifndef FIO_HTTP_LOG_X_REQUEST_START
10922+
#define FIO_HTTP_LOG_X_REQUEST_START 1
10923+
#endif
10924+
```
10925+
10926+
If set, logs will react to an `X-Request-Start` header that provides time in milliseconds.
10927+
10928+
An additional `(wait XXms)` data point will be provided in the logs to inform of the delay between the proxy server's `X-Request-Start` start time and the application's start time.
10929+
1091110930
#### `FIO_HTTP_BODY_RAM_LIMIT`
1091210931

1091310932
```c

fio-stl/001 header.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Memory allocation macros
4343
/** Frees allocated memory. */
4444
#define FIO_MEM_FREE(ptr, size) fio_free((ptr))
4545
/** Set to true of internall allocator is used (memory returned set to zero). */
46-
#define FIO_MEM_REALLOC_IS_SAFE 1
46+
#define FIO_MEM_REALLOC_IS_SAFE fio_realloc_is_safe()
4747

4848
#else /* H___FIO_MALLOC___H */
4949
/** Reallocates memory, copying (at least) `copy_len` if necessary. */

fio-stl/210 map.h

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1895,9 +1895,10 @@ FIO_SFUNC void FIO_NAME_TEST(stl, FIO_MAP_NAME)(void) {
18951895
stderr,
18961896
"* Testing map " FIO_MACRO2STR(FIO_MAP_NAME) " with key " FIO_MACRO2STR(
18971897
FIO_MAP_KEY) " (=> " FIO_MACRO2STR(FIO_MAP_VALUE) ").\n");
1898+
size_t test_len_limit = (1UL << (FIO_MAP_ARRAY_LOG_LIMIT + 15));
18981899
{ /* test set / get overwrite , FIO_MAP_EACH and evict */
18991900
FIO_NAME(FIO_MAP_NAME, s) map = FIO_MAP_INIT;
1900-
for (size_t i = 1; i < (1UL << (FIO_MAP_ARRAY_LOG_LIMIT + 5)); ++i) {
1901+
for (size_t i = 1; i < test_len_limit; ++i) {
19011902
FIO_NAME(FIO_MAP_NAME, set)
19021903
(&map, FIO___M_HASH(i) i FIO___M_VAL(i) FIO___M_OLD);
19031904
FIO_ASSERT(FIO_NAME(FIO_MAP_NAME, count)(&map) == i,
@@ -1937,6 +1938,11 @@ FIO_SFUNC void FIO_NAME_TEST(stl, FIO_MAP_NAME)(void) {
19371938
"map FIO_MAP_EACH ordering broken? %zu != %zu",
19381939
(size_t)(i.key),
19391940
(size_t)(loop_test));
1941+
#else
1942+
FIO_ASSERT(i.key < test_len_limit,
1943+
"map FIO_MAP_EACH invalid data? %zu !< %zu",
1944+
(size_t)(i.key),
1945+
(size_t)(test_len_limit));
19401946
#endif
19411947
}
19421948
FIO_ASSERT(loop_test == count,
@@ -1998,22 +2004,25 @@ FIO_SFUNC void FIO_NAME_TEST(stl, FIO_MAP_NAME)(void) {
19982004
"map reserve error? %zu != %zu",
19992005
(size_t)FIO_NAME(FIO_MAP_NAME, capa)(&map),
20002006
4096);
2001-
for (size_t i = 1; i < 4096; ++i) {
2007+
for (size_t i = 1; i < test_len_limit; ++i) {
20022008
FIO_NAME(FIO_MAP_NAME, set)
20032009
(&map, FIO___M_HASH(i) i FIO___M_VAL(i) FIO___M_OLD);
20042010
FIO_ASSERT(FIO_NAME(FIO_MAP_NAME, count)(&map) == i, "insertion failed?");
20052011
}
2006-
for (size_t i = 1; i < 4096; ++i) {
2012+
for (size_t i = 1; i < test_len_limit; ++i) {
20072013
FIO_ASSERT(FIO_NAME(FIO_MAP_NAME, get)(&map, FIO___M_HASH(i) i),
20082014
"key missing?");
2015+
size_t count = FIO_NAME(FIO_MAP_NAME, count)(&map);
20092016
FIO_NAME(FIO_MAP_NAME, remove)
20102017
(&map, FIO___M_HASH(i) i, NULL);
20112018
FIO_ASSERT(!FIO_NAME(FIO_MAP_NAME, get)(&map, FIO___M_HASH(i) i),
20122019
"map_remove error?");
2013-
FIO_ASSERT(FIO_NAME(FIO_MAP_NAME, count)(&map) == 4095 - i,
2020+
FIO_ASSERT(FIO_NAME(FIO_MAP_NAME, count)(&map) == count - 1,
20142021
"map count error after removal? %zu != %zu",
20152022
(size_t)FIO_NAME(FIO_MAP_NAME, count)(&map),
2016-
i);
2023+
count - 1);
2024+
/* see if removal produces errors while rehashing */
2025+
FIO_NAME(FIO_MAP_NAME, compact)(&map);
20172026
}
20182027
FIO_NAME(FIO_MAP_NAME, destroy)(&map);
20192028
}

fio-stl/431 http handle.h

Lines changed: 43 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,9 @@ HTTP Handle Settings
8787
#define FIO_HTTP_STATIC_FILE_COMPLETION 1
8888
#endif
8989

90+
#ifndef FIO_HTTP_LOG_X_REQUEST_START
91+
#define FIO_HTTP_LOG_X_REQUEST_START 1
92+
#endif
9093
/* *****************************************************************************
9194
HTTP Handle Type
9295
***************************************************************************** */
@@ -2783,21 +2786,11 @@ SFUNC void fio___http_write_pid(fio_str_info_s *dest) {
27832786
SFUNC void fio_http_write_log(fio_http_s *h) {
27842787
FIO_STR_INFO_TMP_VAR(buf, 1023);
27852788
intptr_t bytes_sent = h->sent;
2786-
uint64_t time_start, time_end;
2789+
uint64_t time_start, time_end, time_proxy = 0;
27872790
time_start = h->received_at;
27882791
time_end = fio_http_get_timestump();
27892792
fio_str_info_s date = fio_http_date(time_end / FIO___HTTP_TIME_DIV);
2790-
fio___http_write_pid(&buf);
2791-
buf.buf[buf.len++] = ' ';
2792-
fio_http_from(&buf, h);
2793-
FIO_MEMCPY(buf.buf + buf.len, " - - ", 5);
2794-
FIO_MEMCPY(buf.buf + buf.len + 5, date.buf, date.len);
2795-
buf.len += date.len + 6;
2796-
buf.buf[buf.len++] = ' ';
2797-
buf.buf[buf.len++] = '\"';
2798-
fio_string_write2(
2799-
&buf,
2800-
NULL,
2793+
fio_string_write_s to_write[16] = {
28012794
FIO_STRING_WRITE_STR_INFO(fio_keystr_info(&h->method)),
28022795
FIO_STRING_WRITE_STR2((const char *)" ", 1),
28032796
FIO_STRING_WRITE_STR_INFO(fio_keystr_info(&h->path)),
@@ -2810,11 +2803,48 @@ SFUNC void fio_http_write_log(fio_http_s *h) {
28102803
: (FIO_STRING_WRITE_STR2((const char *)"---", 3))),
28112804
FIO_STRING_WRITE_STR2((const char *)" ", 1),
28122805
FIO_STRING_WRITE_NUM(time_end - time_start),
2813-
FIO_STRING_WRITE_STR2((const char *)(FIO___HTTP_TIME_UNIT "\r\n"), 4));
2806+
FIO_STRING_WRITE_STR2((const char *)(FIO___HTTP_TIME_UNIT "\r\n"), 4),
2807+
};
2808+
if (FIO_HTTP_LOG_X_REQUEST_START) {
2809+
/* log request wait time using x-request-start header */
2810+
fio_str_info_s xstart =
2811+
fio_http_request_header(h,
2812+
FIO_STR_INFO2((char *)"x-request-start", 15),
2813+
0);
2814+
unsigned step =
2815+
(xstart.len > 1 && (xstart.buf[0] | 32) == 't' && xstart.buf[1] == '=');
2816+
step <<= 1;
2817+
xstart.buf += step;
2818+
xstart.len -= step;
2819+
time_proxy = fio_atol(&xstart.buf);
2820+
time_proxy *= (FIO___HTTP_TIME_DIV / 1000); /* assumes info in ms */
2821+
time_proxy = time_start - time_proxy;
2822+
if (time_proxy < (512 * FIO___HTTP_TIME_DIV)) { /* was ms? */
2823+
to_write[11] =
2824+
FIO_STRING_WRITE_STR2((const char *)(FIO___HTTP_TIME_UNIT " (wait "),
2825+
9);
2826+
to_write[12] = FIO_STRING_WRITE_NUM(time_proxy);
2827+
to_write[13] =
2828+
FIO_STRING_WRITE_STR2((const char *)(FIO___HTTP_TIME_UNIT ")\r\n"),
2829+
5);
2830+
}
2831+
}
2832+
2833+
/* Write log line to buffer */
2834+
fio___http_write_pid(&buf);
2835+
buf.buf[buf.len++] = ' ';
2836+
fio_http_from(&buf, h);
2837+
FIO_MEMCPY(buf.buf + buf.len, " - - ", 5);
2838+
FIO_MEMCPY(buf.buf + buf.len + 5, date.buf, date.len);
2839+
buf.len += date.len + 6;
2840+
buf.buf[buf.len++] = ' ';
2841+
buf.buf[buf.len++] = '\"';
2842+
fio_string_write2 FIO_NOOP(&buf, NULL, to_write);
28142843

28152844
if (buf.buf[buf.len - 1] != '\n')
28162845
buf.buf[buf.len++] = '\n'; /* log was truncated, data too long */
28172846

2847+
/* Write log line to STDOUT */
28182848
fwrite(buf.buf, 1, buf.len, stdout);
28192849
h->received_at = time_end;
28202850
}

fio-stl/439 http.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -809,7 +809,13 @@ Returns a human readable string related to the HTTP status number.
809809
void fio_http_write_log(fio_http_s *h);
810810
```
811811

812-
Logs an HTTP (response) to STDOUT.
812+
Logs an HTTP (response) to STDOUT using common log format:
813+
814+
```txt
815+
[PID] ADDR - - [DATE/TIME] REQ RES_CODE BYTES_SENT TIME_SPENT_IN_APP <(wait PROXY_DELAY)>
816+
```
817+
818+
See also the `FIO_HTTP_LOG_X_REQUEST_START` and `FIO_HTTP_EXACT_LOGGING` compilation flags.
813819

814820
### HTTP WebSocket / SSE Helpers
815821

@@ -1113,6 +1119,19 @@ On the other hand, `FIO_HTTP_EXACT_LOGGING` collects exact time stamps to measur
11131119

11141120
Due to the preference to err on the side of higher performance, fuzzy time-stamping is the default.
11151121

1122+
1123+
#### `FIO_HTTP_LOG_X_REQUEST_START`
1124+
1125+
```c
1126+
#ifndef FIO_HTTP_LOG_X_REQUEST_START
1127+
#define FIO_HTTP_LOG_X_REQUEST_START 1
1128+
#endif
1129+
```
1130+
1131+
If set, logs will react to an `X-Request-Start` header that provides time in milliseconds.
1132+
1133+
An additional `(wait XXms)` data point will be provided in the logs to inform of the delay between the proxy server's `X-Request-Start` start time and the application's start time.
1134+
11161135
#### `FIO_HTTP_BODY_RAM_LIMIT`
11171136

11181137
```c

0 commit comments

Comments
 (0)