Skip to content

Commit d98c70e

Browse files
committed
Remove the oldest entry when Ringbuffer is full
add ringbuffer full strategy to control whether remove the oldest log entry or discard new message on dlt-daemon side in dlt.conf when RingbufferFullStrategy = 0: discard the new message when RingbufferFullStrategy = 1: remove the oldest entry in the ringbuffer on application side export DLT_USER_BUFFER_FULL_STRATEGY=0 to discard new message export DLT_USER_BUFFER_FULL_STRATEGY=1 to remove the oldest entry in the ringbuffer
1 parent 2118762 commit d98c70e

16 files changed

+342
-229
lines changed

include/dlt/dlt_common.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -412,6 +412,14 @@ typedef enum
412412
DLT_RECEIVE_FD
413413
} DltReceiverType;
414414

415+
/**
416+
* Type to specify what action to handle when ringbuffer is full
417+
*/
418+
typedef enum {
419+
DLT_RINGBUFFER_DISCARD_NEW_MESSAGE,
420+
DLT_RINGBUFFER_REMOVE_OLDEST_MESSAGE
421+
} DltRingBufferFullStrategy;
422+
415423
/**
416424
* The definition of the serial header containing the characters "DLS" + 0x01.
417425
*/
@@ -795,6 +803,7 @@ typedef struct
795803
uint32_t min_size; /**< Minimum size of buffer */
796804
uint32_t max_size; /**< Maximum size of buffer */
797805
uint32_t step_size; /**< Step size of buffer */
806+
DltRingBufferFullStrategy full_strategy; /**< strategy when ringbuffer is full */
798807
} DltBuffer;
799808

800809
typedef struct
@@ -1321,9 +1330,10 @@ DltReturnValue dlt_check_rcv_data_size(int received, int required);
13211330
* @param buf Pointer to ringbuffer structure
13221331
* @param ptr Ptr to ringbuffer memory
13231332
* @param size Maximum size of buffer in bytes
1333+
* @param full_strategy the strategy when ringbuffer is full
13241334
* @return negative value if there was an error
13251335
*/
1326-
DltReturnValue dlt_buffer_init_static_server(DltBuffer *buf, const unsigned char *ptr, uint32_t size);
1336+
DltReturnValue dlt_buffer_init_static_server(DltBuffer *buf, const unsigned char *ptr, uint32_t size, DltRingBufferFullStrategy full_strategy);
13271337

13281338
/**
13291339
* Initialize static ringbuffer with a size of size.
@@ -1332,9 +1342,10 @@ DltReturnValue dlt_buffer_init_static_server(DltBuffer *buf, const unsigned char
13321342
* @param buf Pointer to ringbuffer structure
13331343
* @param ptr Ptr to ringbuffer memory
13341344
* @param size Maximum size of buffer in bytes
1345+
* @param full_strategy the strategy when ringbuffer is full
13351346
* @return negative value if there was an error
13361347
*/
1337-
DltReturnValue dlt_buffer_init_static_client(DltBuffer *buf, const unsigned char *ptr, uint32_t size);
1348+
DltReturnValue dlt_buffer_init_static_client(DltBuffer *buf, const unsigned char *ptr, uint32_t size, DltRingBufferFullStrategy full_strategy);
13381349

13391350
/**
13401351
* Initialize dynamic ringbuffer with a size of size.
@@ -1346,9 +1357,10 @@ DltReturnValue dlt_buffer_init_static_client(DltBuffer *buf, const unsigned char
13461357
* @param min_size Minimum size of buffer in bytes
13471358
* @param max_size Maximum size of buffer in bytes
13481359
* @param step_size size of which ringbuffer is increased
1360+
* @param full_strategy the strategy when ringbuffer is full
13491361
* @return negative value if there was an error
13501362
*/
1351-
DltReturnValue dlt_buffer_init_dynamic(DltBuffer *buf, uint32_t min_size, uint32_t max_size, uint32_t step_size);
1363+
DltReturnValue dlt_buffer_init_dynamic(DltBuffer *buf, uint32_t min_size, uint32_t max_size, uint32_t step_size, DltRingBufferFullStrategy full_strategy);
13521364

13531365
/**
13541366
* Deinitilaise usage of static ringbuffer

include/dlt/dlt_shm.h

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -87,19 +87,21 @@ typedef struct
8787
* This function must be called before using further shm functions.
8888
* @param buf pointer to shm structure
8989
* @param name the name of the shm, must be the same for server and client
90+
* @param full_strategy the strategy when ringbuffer is full
9091
* @return negative value if there was an error
9192
*/
92-
extern DltReturnValue dlt_shm_init_client(DltShm *buf, const char *name);
93+
extern DltReturnValue dlt_shm_init_client(DltShm *buf, const char *name, DltRingBufferFullStrategy full_strategy);
9394

9495
/**
9596
* Initialise the shared memory on the server side.
9697
* This function must be called before using further shm functions.
9798
* @param buf pointer to shm structure
9899
* @param name the name of the shm, must be the same for server and client
99100
* @param size the requested size of the shm
101+
* @param full_strategy the strategy when ringbuffer is full
100102
* @return negative value if there was an error
101103
*/
102-
extern DltReturnValue dlt_shm_init_server(DltShm *buf, const char *name, int size);
104+
extern DltReturnValue dlt_shm_init_server(DltShm *buf, const char *name, int size, DltRingBufferFullStrategy full_strategy);
103105

104106
/**
105107
* Push data from client onto the shm.

include/dlt/dlt_types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,7 @@ typedef unsigned int speed_t;
8383
*/
8484
typedef enum
8585
{
86+
DLT_RETURN_NO_INCREASE_SIZE = -9,
8687
DLT_RETURN_FILESZERR = -8,
8788
DLT_RETURN_LOGGING_DISABLED = -7,
8889
DLT_RETURN_USER_BUFFER_FULL = -6,

src/daemon/dlt-daemon.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,7 @@ int option_file_parser(DltDaemonLocal *daemon_local)
386386
daemon_local->RingbufferMinSize = DLT_DAEMON_RINGBUFFER_MIN_SIZE;
387387
daemon_local->RingbufferMaxSize = DLT_DAEMON_RINGBUFFER_MAX_SIZE;
388388
daemon_local->RingbufferStepSize = DLT_DAEMON_RINGBUFFER_STEP_SIZE;
389+
daemon_local->ringbufferFullStrategy = DLT_RINGBUFFER_DISCARD_NEW_MESSAGE;
389390
daemon_local->daemonFifoSize = 0;
390391
daemon_local->flags.sendECUSoftwareVersion = 0;
391392
memset(daemon_local->flags.pathToECUSoftwareVersion, 0, sizeof(daemon_local->flags.pathToECUSoftwareVersion));
@@ -592,6 +593,10 @@ int option_file_parser(DltDaemonLocal *daemon_local)
592593
value, &(daemon_local->RingbufferStepSize)) < 0)
593594
return -1;
594595
}
596+
else if (strcmp(token, "RingbufferFullStrategy") == 0)
597+
{
598+
daemon_local->ringbufferFullStrategy = (DltRingBufferFullStrategy)atoi(value);
599+
}
595600
else if (strcmp(token, "SharedMemorySize") == 0)
596601
{
597602
daemon_local->flags.sharedMemorySize = atoi(value);
@@ -1428,7 +1433,7 @@ int dlt_daemon_local_init_p2(DltDaemon *daemon, DltDaemonLocal *daemon_local, in
14281433

14291434
/* Daemon data */
14301435
if (dlt_daemon_init(daemon, daemon_local->RingbufferMinSize, daemon_local->RingbufferMaxSize,
1431-
daemon_local->RingbufferStepSize, daemon_local->flags.ivalue,
1436+
daemon_local->RingbufferStepSize, daemon_local->ringbufferFullStrategy, daemon_local->flags.ivalue,
14321437
daemon_local->flags.contextLogLevel,
14331438
daemon_local->flags.contextTraceStatus, daemon_local->flags.enforceContextLLAndTS,
14341439
daemon_local->flags.vflag) == -1) {
@@ -1477,7 +1482,7 @@ int dlt_daemon_local_init_p2(DltDaemon *daemon, DltDaemonLocal *daemon_local, in
14771482

14781483
/* init shared memory */
14791484
if (dlt_shm_init_server(&(daemon_local->dlt_shm), daemon_local->flags.dltShmName,
1480-
daemon_local->flags.sharedMemorySize) == DLT_RETURN_ERROR) {
1485+
daemon_local->flags.sharedMemorySize, daemon_local->ringbufferFullStrategy) == DLT_RETURN_ERROR) {
14811486
dlt_log(LOG_ERR, "Could not initialize shared memory\n");
14821487
return -1;
14831488
}

src/daemon/dlt-daemon.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ typedef struct
169169
unsigned long RingbufferMinSize;
170170
unsigned long RingbufferMaxSize;
171171
unsigned long RingbufferStepSize;
172+
DltRingBufferFullStrategy ringbufferFullStrategy; /**< (DltRingBufferFullStrategy) indicates what action to handle when hte ringbuffer is full*/
172173
unsigned long daemonFifoSize;
173174
#ifdef UDP_CONNECTION_SUPPORT
174175
int UDPConnectionSetup; /* enable/disable the UDP connection */

src/daemon/dlt.conf

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ RingbufferMaxSize = 10000000
7171
# The step size the Ringbuffer is increased, used for storing temporary DLT messages, until client is connected (Default: 500000)
7272
RingbufferStepSize = 500000
7373

74+
# Indicate whether remove the oldest entry in the RingBuffer when ringBuffer is full (Default: 0)
75+
# 0 = discard new messages
76+
# 1 = remove the oldest message in the ringbuffer
77+
RingbufferFullStrategy = 0
78+
7479
# The size of Daemon FIFO (/tmp/dlt) (Default: 65536, MinSize: depend on pagesize of system, MaxSize: please check /proc/sys/fs/pipe-max-size)
7580
# This is only supported for Linux.
7681
# DaemonFIFOSize = 65536

src/daemon/dlt_daemon_common.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ int dlt_daemon_init(DltDaemon *daemon,
287287
unsigned long RingbufferMinSize,
288288
unsigned long RingbufferMaxSize,
289289
unsigned long RingbufferStepSize,
290+
DltRingBufferFullStrategy ringBufferFullStrategy,
290291
const char *runtime_directory,
291292
int InitialContextLogLevel,
292293
int InitialContextTraceStatus,
@@ -325,7 +326,8 @@ int dlt_daemon_init(DltDaemon *daemon,
325326
if (dlt_buffer_init_dynamic(&(daemon->client_ringbuffer),
326327
(uint32_t) RingbufferMinSize,
327328
(uint32_t) RingbufferMaxSize,
328-
(uint32_t) RingbufferStepSize) < DLT_RETURN_OK)
329+
(uint32_t) RingbufferStepSize,
330+
ringBufferFullStrategy) < DLT_RETURN_OK)
329331
return -1;
330332

331333
daemon->storage_handle = NULL;

src/daemon/dlt_daemon_common.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,7 @@ typedef struct
213213
* @param RingbufferMinSize ringbuffer size
214214
* @param RingbufferMaxSize ringbuffer size
215215
* @param RingbufferStepSize ringbuffer size
216+
* @param fullStrategy ringbuffer full strategy
216217
* @param runtime_directory Directory of persistent configuration
217218
* @param InitialContextLogLevel loglevel to be sent to context when those register with loglevel default, read from dlt.conf
218219
* @param InitialContextTraceStatus tracestatus to be sent to context when those register with tracestatus default, read from dlt.conf
@@ -224,6 +225,7 @@ int dlt_daemon_init(DltDaemon *daemon,
224225
unsigned long RingbufferMinSize,
225226
unsigned long RingbufferMaxSize,
226227
unsigned long RingbufferStepSize,
228+
DltRingBufferFullStrategy fullStrategy,
227229
const char *runtime_directory,
228230
int InitialContextLogLevel,
229231
int InitialContextTraceStatus,

src/lib/dlt_user.c

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,8 @@ static DltReturnValue dlt_user_log_write_sized_string_utils_attr(DltContextData
226226

227227
static DltReturnValue dlt_unregister_app_util(bool force_sending_messages);
228228

229+
static DltRingBufferFullStrategy dlt_env_get_ringbuffer_full_strategy();
230+
229231
DltReturnValue dlt_user_check_library_version(const char *user_major_version, const char *user_minor_version)
230232
{
231233
char lib_major_version[DLT_USER_MAX_LIB_VERSION_LENGTH];
@@ -501,7 +503,7 @@ DltReturnValue dlt_init(void)
501503
memset(&(dlt_user.dlt_shm), 0, sizeof(DltShm));
502504

503505
/* init shared memory */
504-
if (dlt_shm_init_client(&(dlt_user.dlt_shm), dltShmName) < DLT_RETURN_OK)
506+
if (dlt_shm_init_client(&(dlt_user.dlt_shm), dltShmName, dlt_env_get_ringbuffer_full_strategy()) < DLT_RETURN_OK)
505507
dlt_vnlog(LOG_WARNING, DLT_USER_BUFFER_LENGTH, "Logging disabled,"
506508
" Shared memory %s cannot be created!\n", dltShmName);
507509

@@ -883,7 +885,8 @@ DltReturnValue dlt_init_common(void)
883885
if (dlt_buffer_init_dynamic(&(dlt_user.startup_buffer),
884886
buffer_min,
885887
buffer_max,
886-
buffer_step) == DLT_RETURN_ERROR) {
888+
buffer_step,
889+
dlt_env_get_ringbuffer_full_strategy()) == DLT_RETURN_ERROR) {
887890
dlt_user_init_state = INIT_UNITIALIZED;
888891
DLT_SEM_FREE();
889892
return DLT_RETURN_ERROR;
@@ -4920,7 +4923,7 @@ void dlt_user_log_reattach_to_daemon(void)
49204923
#ifdef DLT_SHM_ENABLE
49214924

49224925
/* init shared memory */
4923-
if (dlt_shm_init_client(&dlt_user.dlt_shm, dltShmName) < DLT_RETURN_OK)
4926+
if (dlt_shm_init_client(&dlt_user.dlt_shm, dltShmName,dlt_env_get_ringbuffer_full_strategy()) < DLT_RETURN_OK)
49244927
dlt_vnlog(LOG_WARNING, DLT_USER_BUFFER_LENGTH, "Logging disabled,"
49254928
" Shared memory %s cannot be created!\n", dltShmName);
49264929

@@ -5200,3 +5203,23 @@ DltReturnValue dlt_user_log_out_error_handling(void *ptr1, size_t len1, void *pt
52005203

52015204
return ret;
52025205
}
5206+
5207+
DltRingBufferFullStrategy dlt_env_get_ringbuffer_full_strategy()
5208+
{
5209+
char *env_buffer_full_strategy = NULL;
5210+
DltRingBufferFullStrategy full_strategy = DLT_RINGBUFFER_DISCARD_NEW_MESSAGE;
5211+
5212+
env_buffer_full_strategy = getenv(DLT_USER_ENV_BUFFER_FULL_STRATEGY);
5213+
if (env_buffer_full_strategy != NULL) {
5214+
full_strategy = (DltRingBufferFullStrategy)strtol(env_buffer_full_strategy, NULL, 10);
5215+
5216+
if ((errno == EINVAL) || (errno == ERANGE)) {
5217+
dlt_vlog(LOG_ERR,
5218+
"Wrong value specified for %s. Using default\n",
5219+
DLT_USER_ENV_BUFFER_FULL_STRATEGY);
5220+
full_strategy = DLT_RINGBUFFER_DISCARD_NEW_MESSAGE;
5221+
}
5222+
}
5223+
5224+
return full_strategy;
5225+
}

src/lib/dlt_user_cfg.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@
8282
#define DLT_USER_ENV_BUFFER_MIN_SIZE "DLT_USER_BUFFER_MIN"
8383
#define DLT_USER_ENV_BUFFER_MAX_SIZE "DLT_USER_BUFFER_MAX"
8484
#define DLT_USER_ENV_BUFFER_STEP_SIZE "DLT_USER_BUFFER_STEP"
85+
#define DLT_USER_ENV_BUFFER_FULL_STRATEGY "DLT_USER_BUFFER_FULL_STRATEGY"
8586

8687
/* Temporary buffer length */
8788
#define DLT_USER_BUFFER_LENGTH 255

0 commit comments

Comments
 (0)