circular_buffer.h 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. // SPDX-License-Identifier: GPL-3.0-or-later
  2. /** @file circular_buffer.h
  3. * @brief Header of circular_buffer.c
  4. */
  5. #ifndef CIRCULAR_BUFFER_H_
  6. #define CIRCULAR_BUFFER_H_
  7. #include <stdio.h>
  8. #include <stdlib.h>
  9. #include <string.h>
  10. #include <uv.h>
  11. #include "defaults.h"
  12. #include "query.h"
  13. #include "file_info.h"
  14. // Forward declaration to break circular dependency
  15. struct File_info;
  16. typedef enum {
  17. CIRC_BUFF_ITEM_STATUS_UNPROCESSED = 0,
  18. CIRC_BUFF_ITEM_STATUS_PARSED = 1,
  19. CIRC_BUFF_ITEM_STATUS_STREAMED = 2,
  20. CIRC_BUFF_ITEM_STATUS_DONE = 3 // == CIRC_BUFF_ITEM_STATUS_PARSED | CIRC_BUFF_ITEM_STATUS_STREAMED
  21. } circ_buff_item_status_t;
  22. typedef struct Circ_buff_item {
  23. circ_buff_item_status_t status; /**< Denotes if item is unprocessed, in processing or processed **/
  24. msec_t timestamp; /**< Epoch datetime of when data was collected **/
  25. char *data; /**< Base of buffer to store both uncompressed and compressed logs **/
  26. size_t text_size; /**< Size of uncompressed logs **/
  27. char *text_compressed; /**< Pointer offset within *data that points to start of compressed logs **/
  28. size_t text_compressed_size; /**< Size of compressed logs **/
  29. size_t data_max_size; /**< Allocated size of *data **/
  30. unsigned long num_lines; /**< Number of log records in item */
  31. } Circ_buff_item_t;
  32. typedef struct Circ_buff {
  33. int num_of_items; /**< Number of preallocated items in the buffer **/
  34. Circ_buff_item_t *items; /**< Array of all circular buffer items **/
  35. Circ_buff_item_t *in; /**< Circular buffer item to write new data into **/
  36. int head; /**< Position of next item insertion **/
  37. int read; /**< Index between tail and head, used to read items out of Circ_buff **/
  38. int tail; /**< Last valid item in Circ_buff **/
  39. int parse; /**< Points to next item in buffer to be parsed **/
  40. int full; /**< When head == tail, this indicates if buffer is full or empty **/
  41. uv_rwlock_t buff_realloc_rwlock; /**< RW lock to lock buffer operations when reallocating or expanding buffer **/
  42. unsigned int buff_realloc_cnt; /**< Counter of how any buffer reallocations have occurred **/
  43. size_t total_cached_mem; /**< Total memory allocated for Circ_buff (excluding *in) **/
  44. size_t total_cached_mem_max; /**< Maximum allowable size for total_cached_mem **/
  45. int allow_dropped_logs; /**< Boolean to indicate whether logs are allowed to be dropped if buffer is full */
  46. size_t text_size_total; /**< Total size of items[]->text_size **/
  47. size_t text_compressed_size_total; /**< Total size of items[]->text_compressed_size **/
  48. int compression_ratio; /**< text_size_total / text_compressed_size_total **/
  49. } Circ_buff_t;
  50. void circ_buff_search(logs_query_params_t *const p_query_params, struct File_info *const p_file_infos[]);
  51. size_t circ_buff_prepare_write(Circ_buff_t *const buff, size_t const requested_text_space);
  52. int circ_buff_insert(Circ_buff_t *const buff);
  53. Circ_buff_item_t *circ_buff_read_item(Circ_buff_t *const buff);
  54. void circ_buff_read_done(Circ_buff_t *const buff);
  55. Circ_buff_t *circ_buff_init(const int num_of_items, const size_t max_size, const int allow_dropped_logs);
  56. void circ_buff_destroy(Circ_buff_t *buff);
  57. #endif // CIRCULAR_BUFFER_H_