diff options
Diffstat (limited to 'libnetdata/buffer')
-rw-r--r-- | libnetdata/buffer/README.md | 4 | ||||
-rw-r--r-- | libnetdata/buffer/buffer.c | 60 | ||||
-rw-r--r-- | libnetdata/buffer/buffer.h | 3 |
3 files changed, 40 insertions, 27 deletions
diff --git a/libnetdata/buffer/README.md b/libnetdata/buffer/README.md index c5f66e6e3..6a84fd8a3 100644 --- a/libnetdata/buffer/README.md +++ b/libnetdata/buffer/README.md @@ -1,6 +1,10 @@ <!-- title: "BUFFER" custom_edit_url: https://github.com/netdata/netdata/edit/master/libnetdata/buffer/README.md +sidebar_label: "BUFFER library" +learn_status: "Published" +learn_topic_type: "Tasks" +learn_rel_path: "Developers/libnetdata libraries" --> # BUFFER diff --git a/libnetdata/buffer/buffer.c b/libnetdata/buffer/buffer.c index d0940588f..eeb283209 100644 --- a/libnetdata/buffer/buffer.c +++ b/libnetdata/buffer/buffer.c @@ -442,28 +442,28 @@ void buffer_date(BUFFER *wb, int year, int month, int day, int hours, int minute buffer_need_bytes(wb, 36); char *b = &wb->buffer[wb->len]; - char *p = b; - - *p++ = '0' + year / 1000; year %= 1000; - *p++ = '0' + year / 100; year %= 100; - *p++ = '0' + year / 10; - *p++ = '0' + year % 10; - *p++ = '-'; - *p++ = '0' + month / 10; - *p++ = '0' + month % 10; - *p++ = '-'; - *p++ = '0' + day / 10; - *p++ = '0' + day % 10; - *p++ = ' '; - *p++ = '0' + hours / 10; - *p++ = '0' + hours % 10; - *p++ = ':'; - *p++ = '0' + minutes / 10; - *p++ = '0' + minutes % 10; - *p++ = ':'; - *p++ = '0' + seconds / 10; - *p++ = '0' + seconds % 10; - *p = '\0'; + char *p = b; + + *p++ = '0' + year / 1000; year %= 1000; + *p++ = '0' + year / 100; year %= 100; + *p++ = '0' + year / 10; + *p++ = '0' + year % 10; + *p++ = '-'; + *p++ = '0' + month / 10; + *p++ = '0' + month % 10; + *p++ = '-'; + *p++ = '0' + day / 10; + *p++ = '0' + day % 10; + *p++ = ' '; + *p++ = '0' + hours / 10; + *p++ = '0' + hours % 10; + *p++ = ':'; + *p++ = '0' + minutes / 10; + *p++ = '0' + minutes % 10; + *p++ = ':'; + *p++ = '0' + seconds / 10; + *p++ = '0' + seconds % 10; + *p = '\0'; wb->len += (size_t)(p - b); @@ -472,7 +472,7 @@ void buffer_date(BUFFER *wb, int year, int month, int day, int hours, int minute buffer_overflow_check(wb); } -BUFFER *buffer_create(size_t size) +BUFFER *buffer_create(size_t size, size_t *statistics) { BUFFER *b; @@ -483,9 +483,13 @@ BUFFER *buffer_create(size_t size) b->buffer[0] = '\0'; b->size = size; b->contenttype = CT_TEXT_PLAIN; + b->statistics = statistics; buffer_overflow_init(b); buffer_overflow_check(b); + if(b->statistics) + __atomic_add_fetch(b->statistics, b->size + sizeof(BUFFER) + sizeof(BUFFER_OVERFLOW_EOF) + 2, __ATOMIC_RELAXED); + return(b); } @@ -496,6 +500,9 @@ void buffer_free(BUFFER *b) { debug(D_WEB_BUFFER, "Freeing web buffer of size %zu.", b->size); + if(b->statistics) + __atomic_sub_fetch(b->statistics, b->size + sizeof(BUFFER) + sizeof(BUFFER_OVERFLOW_EOF) + 2, __ATOMIC_RELAXED); + freez(b->buffer); freez(b); } @@ -510,9 +517,7 @@ void buffer_increase(BUFFER *b, size_t free_size_required) { size_t minimum = WEB_DATA_LENGTH_INCREASE_STEP; if(minimum > wanted) wanted = minimum; - size_t optimal = b->size; - if(b->size > 5*1024*1024) optimal = b->size / 2; - + size_t optimal = (b->size > 5*1024*1024) ? b->size / 2 : b->size; if(optimal > wanted) wanted = optimal; debug(D_WEB_BUFFER, "Increasing data buffer from size %zu to %zu.", b->size, b->size + wanted); @@ -520,6 +525,9 @@ void buffer_increase(BUFFER *b, size_t free_size_required) { b->buffer = reallocz(b->buffer, b->size + wanted + sizeof(BUFFER_OVERFLOW_EOF) + 2); b->size += wanted; + if(b->statistics) + __atomic_add_fetch(b->statistics, wanted, __ATOMIC_RELAXED); + buffer_overflow_init(b); buffer_overflow_check(b); } diff --git a/libnetdata/buffer/buffer.h b/libnetdata/buffer/buffer.h index ce6f52899..0fa3495b4 100644 --- a/libnetdata/buffer/buffer.h +++ b/libnetdata/buffer/buffer.h @@ -15,6 +15,7 @@ typedef struct web_buffer { uint8_t options; // options related to the content time_t date; // the timestamp this content has been generated time_t expires; // the timestamp this content expires + size_t *statistics; } BUFFER; // options @@ -61,7 +62,7 @@ void buffer_rrd_value(BUFFER *wb, NETDATA_DOUBLE value); void buffer_date(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds); void buffer_jsdate(BUFFER *wb, int year, int month, int day, int hours, int minutes, int seconds); -BUFFER *buffer_create(size_t size); +BUFFER *buffer_create(size_t size, size_t *statistics); void buffer_free(BUFFER *b); void buffer_increase(BUFFER *b, size_t free_size_required); |