Skip to content

Commit

Permalink
dlt-system: Avoid expensive getpid() syscalls
Browse files Browse the repository at this point in the history
Since glibc 2.25, getpid() no longer caches the PID, instead performing
a full syscall every time. This means we're performing a full syscall
roundtrip for *every* *single* *log* *message* on the system.

This patch caches the result of getpid() when appropriate, avoiding
unnessicary syscalls.

Signed-off-by: Aleix Pol Gonzalez <[email protected]>
  • Loading branch information
Victoria Fischer authored and Aleix Pol Gonzalez committed Jan 8, 2025
1 parent 91deea1 commit 34d8272
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
1 change: 1 addition & 0 deletions include/dlt/dlt_user.h.in
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ typedef struct
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
pthread_rwlock_t trace_load_limit_lock;
#endif
pid_t local_pid; /**< Local DltUser process identifier cache */
} DltUser;

typedef int (*dlt_injection_callback_id)(uint32_t, void *, uint32_t, void *);
Expand Down
9 changes: 8 additions & 1 deletion src/lib/dlt_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -771,6 +771,9 @@ DltReturnValue dlt_init_common(void)
/* set to unknown state of connected client */
dlt_user.log_state = -1;

/* set pid cache to none until we need it */
dlt_user.local_pid = -1;

dlt_user.dlt_log_handle = -1;
dlt_user.dlt_user_handle = DLT_FD_INIT;

Expand Down Expand Up @@ -4011,7 +4014,10 @@ DltReturnValue dlt_user_log_send_log(DltContextData *log, const int mtype, int *
/* send session id */
if (dlt_user.with_session_id) {
msg.standardheader->htyp |= DLT_HTYP_WSID;
msg.headerextra.seid = (uint32_t) getpid();
if (__builtin_expect(!!(dlt_user.local_pid == -1), false)) {
dlt_user.local_pid = getpid();
}
msg.headerextra.seid = (uint32_t) dlt_user.local_pid;
}

if (is_verbose_mode(dlt_user.verbose_mode, log))
Expand Down Expand Up @@ -5377,6 +5383,7 @@ static void dlt_fork_child_fork_handler()
g_dlt_is_child = 1;
dlt_user_init_state = INIT_UNITIALIZED;
dlt_user.dlt_log_handle = -1;
dlt_user.local_pid = -1;
#ifdef DLT_TRACE_LOAD_CTRL_ENABLE
pthread_rwlock_unlock(&trace_load_rw_lock);
#endif
Expand Down

0 comments on commit 34d8272

Please sign in to comment.