Skip to content

Commit

Permalink
trace-load: improve trace load lookup
Browse files Browse the repository at this point in the history
store trace load in context
instead of doing a search on each message

Signed-off-by: Alexander Mohr <[email protected]>
  • Loading branch information
alexmohr committed Oct 16, 2024
1 parent 4e7ecb4 commit d4b2bf7
Show file tree
Hide file tree
Showing 9 changed files with 135 additions and 109 deletions.
3 changes: 3 additions & 0 deletions include/dlt/dlt_user.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,9 @@ typedef struct
/* Log Level changed callback */
void (*log_level_changed_callback)(char context_id[DLT_ID_SIZE], uint8_t log_level, uint8_t trace_status);

#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
DltTraceLoadSettings *trace_load_settings; /**< trace load setting for the context */
#endif
} dlt_ll_ts_type;

/**
Expand Down
38 changes: 17 additions & 21 deletions src/daemon/dlt-daemon.c
Original file line number Diff line number Diff line change
Expand Up @@ -1636,6 +1636,13 @@ int main(int argc, char *argv[])
dlt_gateway_deinit(&daemon_local.pGateway, daemon_local.flags.vflag);

dlt_daemon_free(&daemon, daemon_local.flags.vflag);
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
if (daemon.preconfigured_trace_load_settings != NULL) {
free(daemon.preconfigured_trace_load_settings);
daemon.preconfigured_trace_load_settings = NULL;
}
pthread_rwlock_destroy(&trace_load_rw_lock);
#endif

dlt_log(LOG_NOTICE, "Leaving DLT daemon\n");

Expand Down Expand Up @@ -2747,11 +2754,6 @@ int dlt_daemon_process_client_connect(DltDaemon *daemon,
/* send new log state to all applications */
daemon->connectionState = 1;
dlt_daemon_user_send_all_log_state(daemon, verbose);

#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
/* Reset number of received bytes from FIFO */
daemon->bytes_recv = 0;
#endif
}

return 0;
Expand Down Expand Up @@ -3143,14 +3145,6 @@ int dlt_daemon_process_user_messages(DltDaemon *daemon,
return -1;
}

#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
/* Count up number of received bytes from FIFO */
if (receiver->bytesRcvd > receiver->lastBytesRcvd)
{
daemon->bytes_recv += receiver->bytesRcvd - receiver->lastBytesRcvd;
}
#endif

/* look through buffer as long as data is in there */
while ((receiver->bytesRcvd >= min_size) && run_loop) {
dlt_daemon_process_user_message_func func = NULL;
Expand Down Expand Up @@ -3976,24 +3970,26 @@ bool trace_load_keep_message(DltDaemonApplication *app,
app->apid,
};

DltTraceLoadSettings *trace_load_settings =
dlt_find_runtime_trace_load_settings(
app->trace_load_settings, app->trace_load_settings_count,
app->apid, msg->extendedheader->ctid);
DltDaemonContext *context = dlt_daemon_context_find(
daemon,
app->apid,
msg->extendedheader->ctid,
daemon->ecuid,
verbose);

if (trace_load_settings != NULL) {
if (context != NULL) {
pthread_rwlock_wrlock(&trace_load_rw_lock);
keep_message = dlt_check_trace_load(
trace_load_settings, mtin, msg->headerextra.tmsp, size,
context->trace_load_settings, mtin, msg->headerextra.tmsp, size,
dlt_daemon_output_internal_msg, (void *)(&params));
pthread_rwlock_unlock(&trace_load_rw_lock);
}
else {
dlt_vlog(
LOG_ERR,
"Failed to lookup trace load limits for %s, "
"Failed to find context for app %.4s and context %.4s "
"dropping message, likely app was not registered properly\n",
app->apid);
app->apid, msg->extendedheader->ctid);
keep_message = false;
}

Expand Down
8 changes: 0 additions & 8 deletions src/daemon/dlt_daemon_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -158,14 +158,6 @@ static int dlt_daemon_client_send_all_multiple(DltDaemon *daemon,
sent = 1;
} /* for */

#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
if (sent)
{
const uint32_t serial_header = daemon->sendserialheader ? sizeof(dltSerialHeader) : 0;
daemon->bytes_sent += size1 + size2 + serial_header;
}
#endif

return sent;
}

Expand Down
27 changes: 18 additions & 9 deletions src/daemon/dlt_daemon_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -388,8 +388,6 @@ int dlt_daemon_init(DltDaemon *daemon,

#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
daemon->preconfigured_trace_load_settings = NULL;
daemon->bytes_sent = 0;
daemon->bytes_recv = 0;
#endif

daemon->sendserialheader = 0;
Expand Down Expand Up @@ -442,13 +440,6 @@ int dlt_daemon_free(DltDaemon *daemon, int verbose)
free(daemon->app_id_log_level_settings);
}
#endif
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
if (daemon->preconfigured_trace_load_settings != NULL) {
free(daemon->preconfigured_trace_load_settings);
daemon->preconfigured_trace_load_settings = NULL;
}
pthread_rwlock_destroy(&trace_load_rw_lock);
#endif

if (app_recv_buffer)
free(app_recv_buffer);
Expand Down Expand Up @@ -1133,6 +1124,9 @@ DltDaemonContext *dlt_daemon_context_add(DltDaemon *daemon,

dlt_set_id(context->apid, apid);
dlt_set_id(context->ctid, ctid);
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
context->trace_load_settings = NULL;
#endif

application->num_contexts++;
new_context = 1;
Expand Down Expand Up @@ -1213,6 +1207,21 @@ DltDaemonContext *dlt_daemon_context_add(DltDaemon *daemon,
context->log_level_pos = log_level_pos;
context->user_handle = user_handle;

#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
DltTraceLoadSettings* tl_settings = dlt_find_runtime_trace_load_settings(
application->trace_load_settings,
application->trace_load_settings_count,
application->apid,
context->ctid);
if (tl_settings == NULL) {
dlt_vlog(LOG_WARNING, "failed to find trace load settings for application %s context %s\n",
application->apid, context->ctid);
} else {
context->trace_load_settings = tl_settings;
}
#endif


/* In case a context is loaded from runtime config file,
* the user_handle is 0 and we mark that context as predefined.
*/
Expand Down
5 changes: 3 additions & 2 deletions src/daemon/dlt_daemon_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,9 @@ typedef struct
char *context_description; /**< context description */
int8_t storage_log_level; /**< log level set for offline logstorage */
bool predefined; /**< set to true if this context is predefined by runtime configuration file */
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
DltTraceLoadSettings* trace_load_settings; /**< trace load setting for the context */
#endif
} DltDaemonContext;

/*
Expand Down Expand Up @@ -215,8 +218,6 @@ typedef struct
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
DltTraceLoadSettings *preconfigured_trace_load_settings; /**< Settings for trace load */
int preconfigured_trace_load_settings_count; /** < count of trace load settings */
int bytes_sent;
int bytes_recv;
#endif
} DltDaemon;

Expand Down
85 changes: 43 additions & 42 deletions src/lib/dlt_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,7 +229,6 @@ static DltReturnValue dlt_unregister_app_util(bool force_sending_messages);
static DltReturnValue dlt_user_output_internal_msg(DltLogLevelType loglevel, const char *text, void* params);
DltTraceLoadSettings* trace_load_settings = NULL;
uint32_t trace_load_settings_count = 0;
pthread_rwlock_t trace_load_rw_lock = PTHREAD_RWLOCK_INITIALIZER;
#endif

DltReturnValue dlt_user_check_library_version(const char *user_major_version, const char *user_minor_version)
Expand Down Expand Up @@ -513,8 +512,8 @@ DltReturnValue dlt_init(void)

#endif
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
pthread_rwlock_wrlock(&trace_load_rw_lock);

DLT_SEM_LOCK();
trace_load_settings = malloc(sizeof(DltTraceLoadSettings));
if (trace_load_settings == NULL) {
dlt_vlog(LOG_ERR, "Failed to allocate memory for trace load settings\n");
Expand All @@ -526,8 +525,7 @@ DltReturnValue dlt_init(void)
trace_load_settings[0].hard_limit = DLT_TRACE_LOAD_CLIENT_HARD_LIMIT_DEFAULT;
strncpy(trace_load_settings[0].apid, dlt_user.appID, DLT_ID_SIZE);
trace_load_settings_count = 1;

pthread_rwlock_unlock(&trace_load_rw_lock);
DLT_SEM_FREE();

#endif
#ifdef DLT_LIB_USE_UNIX_SOCKET_IPC
Expand Down Expand Up @@ -958,10 +956,6 @@ void dlt_user_atexit_handler(void)
/* Cleanup */
/* Ignore return value */
dlt_free();

#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
pthread_rwlock_destroy(&trace_load_rw_lock);
#endif
}

int dlt_user_atexit_blow_out_user_buffer(void)
Expand Down Expand Up @@ -1543,8 +1537,20 @@ DltReturnValue dlt_register_context_ll_ts_llccb(DltContext *handle,

dlt_user.dlt_ll_ts_num_entries++;

DLT_SEM_FREE();
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
DltTraceLoadSettings* settings = dlt_find_runtime_trace_load_settings(
trace_load_settings,
trace_load_settings_count,
dlt_user.appID,
ctx_entry->contextID);
if (settings == NULL) {
dlt_vlog(LOG_WARNING, "No trace load settings found for %s.%s\n", dlt_user.appID, log.handle->contextID);
} else {
ctx_entry->trace_load_settings = settings;
}

#endif
DLT_SEM_FREE();
return dlt_user_log_send_register_context(&log);
}

Expand Down Expand Up @@ -4169,19 +4175,16 @@ DltReturnValue dlt_user_log_send_log(DltContextData *log, const int mtype, int *
/* check trace load before output */
if (!sent_size)
{
pthread_rwlock_wrlock(&trace_load_rw_lock);
DltTraceLoadSettings* settings =
dlt_find_runtime_trace_load_settings(
trace_load_settings, trace_load_settings_count, dlt_user.appID, log->handle->contextID);
DLT_SEM_LOCK();
const bool trace_load_in_limits = dlt_check_trace_load(
settings,
dlt_user.dlt_ll_ts[log->handle->log_level_pos].trace_load_settings,
log->log_level, time_stamp,
sizeof(DltUserHeader)
+ msg.headersize - sizeof(DltStorageHeader)
+ log->size,
dlt_user_output_internal_msg,
NULL);
pthread_rwlock_unlock(&trace_load_rw_lock);
DLT_SEM_FREE();
if (!trace_load_in_limits){
return DLT_RETURN_LOAD_EXCEEDED;
}
Expand Down Expand Up @@ -4403,7 +4406,6 @@ DltReturnValue dlt_user_log_send_register_context(DltContextData *log)
usercontext.description_length);

return DLT_RETURN_OK;

}

DltReturnValue dlt_user_log_send_unregister_context(DltContextData *log)
Expand Down Expand Up @@ -4882,18 +4884,16 @@ DltReturnValue dlt_user_log_check_user_message(void)
trace_load_settings_user_messages =
(DltUserControlMsgTraceSettingMsg *)(receiver->buf + sizeof(DltUserHeader) + sizeof(uint32_t));

pthread_rwlock_wrlock(&trace_load_rw_lock);
DLT_SEM_LOCK();

// Remove the default created at startup
if (trace_load_settings != NULL) {
free(trace_load_settings);
}

char msg[255];
trace_load_settings_alloc_size = sizeof(DltTraceLoadSettings) * trace_load_settings_user_messages_count;
trace_load_settings = malloc(trace_load_settings_alloc_size);
if (trace_load_settings == NULL) {
pthread_rwlock_unlock(&trace_load_rw_lock);
dlt_log(LOG_ERR, "Failed to allocate memory for trace load settings\n");
return DLT_RETURN_ERROR;
}
Expand All @@ -4904,29 +4904,33 @@ DltReturnValue dlt_user_log_check_user_message(void)
trace_load_settings[i].soft_limit = trace_load_settings_user_messages[i].soft_limit;
trace_load_settings[i].hard_limit = trace_load_settings_user_messages[i].hard_limit;
}

trace_load_settings_count = trace_load_settings_user_messages_count;
pthread_rwlock_unlock(&trace_load_rw_lock);
for (i = 0; i < dlt_user.dlt_ll_ts_num_entries; ++i) {
dlt_ll_ts_type* ctx_entry = &dlt_user.dlt_ll_ts[i];
ctx_entry->trace_load_settings = dlt_find_runtime_trace_load_settings(
trace_load_settings, trace_load_settings_count, dlt_user.appID, ctx_entry->contextID);
}
DLT_SEM_FREE();

// must be sent with unlocked trace_load_rw_lock
for (i = 0; i < trace_load_settings_user_messages_count; i++) {
if (trace_load_settings[i].ctid[0] == '\0') {
snprintf(
msg, sizeof(msg),
"Received trace load settings: apid=%.4s, soft_limit=%u, hard_limit=%u\n",
trace_load_settings[i].apid,
trace_load_settings[i].soft_limit,
trace_load_settings[i].hard_limit);
} else {
snprintf(
msg, sizeof(msg),
"Received trace load settings: apid=%.4s, ctid=%.4s, soft_limit=%u, hard_limit=%u\n",
trace_load_settings[i].apid,
trace_load_settings[i].ctid,
trace_load_settings[i].soft_limit,
trace_load_settings[i].hard_limit);
}
dlt_user_output_internal_msg(DLT_LOG_INFO, msg, NULL);
// The log messages only can be produced safely when
// the trace load settings are set up fully.
char msg[255];
if (trace_load_settings[i].ctid[0] == '\0') {
snprintf(msg, sizeof(msg), "Received trace load settings: apid=%.4s, soft_limit=%u, hard_limit=%u\n",
trace_load_settings[i].apid,
trace_load_settings[i].soft_limit,
trace_load_settings[i].hard_limit);
} else {
snprintf(
msg, sizeof(msg),
"Received trace load settings: apid=%.4s, ctid=%.4s, soft_limit=%u, hard_limit=%u\n",
trace_load_settings[i].apid,
trace_load_settings[i].ctid,
trace_load_settings[i].soft_limit,
trace_load_settings[i].hard_limit);
}
dlt_user_output_internal_msg(DLT_LOG_INFO, msg, NULL);

/* keep not read data in buffer */
if (dlt_receiver_remove(receiver, trace_load_settings_user_message_bytes_required)
Expand Down Expand Up @@ -5349,9 +5353,6 @@ static void dlt_fork_child_fork_handler()
g_dlt_is_child = 1;
dlt_user_init_state = INIT_UNITIALIZED;
dlt_user.dlt_log_handle = -1;
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
pthread_rwlock_unlock(&trace_load_rw_lock);
#endif
}


Expand Down
6 changes: 6 additions & 0 deletions src/shared/dlt_common.c
Original file line number Diff line number Diff line change
Expand Up @@ -4453,6 +4453,12 @@ bool dlt_check_trace_load(
return true;
}

if (tl_settings == NULL)
{
internal_dlt_log(DLT_LOG_ERROR, "tl_settings is NULL", internal_dlt_log_params);
return false;
}

if (size < 0)
{
dlt_vlog(LOG_ERR, "Invalid size: %d", size);
Expand Down
Loading

0 comments on commit d4b2bf7

Please sign in to comment.