mirror of
https://git.proxmox.com/git/mirror_frr
synced 2025-08-15 11:30:30 +00:00
lib: cache PID & TID in zlog code
glibc removed its pid cache a while back, and grabbing this from TLS is faster than repeatedly calling the kernel... Also use `intmax_t` which is more appropriate for both PID & TID. Signed-off-by: David Lamparter <equinox@diac24.net>
This commit is contained in:
parent
8b94cb4388
commit
78598fd019
35
lib/zlog.c
35
lib/zlog.c
@ -220,8 +220,15 @@ static inline void zlog_tls_set(struct zlog_tls *val)
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef CAN_DO_TLS
|
#ifdef CAN_DO_TLS
|
||||||
static long zlog_gettid(void)
|
static intmax_t zlog_gettid(void)
|
||||||
{
|
{
|
||||||
|
#ifndef __OpenBSD__
|
||||||
|
/* accessing a TLS variable is much faster than a syscall */
|
||||||
|
static thread_local intmax_t cached_tid = -1;
|
||||||
|
if (cached_tid != -1)
|
||||||
|
return cached_tid;
|
||||||
|
#endif
|
||||||
|
|
||||||
long rv = -1;
|
long rv = -1;
|
||||||
#ifdef HAVE_PTHREAD_GETTHREADID_NP
|
#ifdef HAVE_PTHREAD_GETTHREADID_NP
|
||||||
rv = pthread_getthreadid_np();
|
rv = pthread_getthreadid_np();
|
||||||
@ -241,6 +248,10 @@ static long zlog_gettid(void)
|
|||||||
rv = mach_thread_self();
|
rv = mach_thread_self();
|
||||||
mach_port_deallocate(mach_task_self(), rv);
|
mach_port_deallocate(mach_task_self(), rv);
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
#ifndef __OpenBSD__
|
||||||
|
cached_tid = rv;
|
||||||
|
#endif
|
||||||
return rv;
|
return rv;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -260,7 +271,7 @@ void zlog_tls_buffer_init(void)
|
|||||||
for (i = 0; i < array_size(zlog_tls->msgp); i++)
|
for (i = 0; i < array_size(zlog_tls->msgp); i++)
|
||||||
zlog_tls->msgp[i] = &zlog_tls->msgs[i];
|
zlog_tls->msgp[i] = &zlog_tls->msgs[i];
|
||||||
|
|
||||||
snprintfrr(mmpath, sizeof(mmpath), "logbuf.%ld", zlog_gettid());
|
snprintfrr(mmpath, sizeof(mmpath), "logbuf.%jd", zlog_gettid());
|
||||||
|
|
||||||
mmfd = openat(zlog_tmpdirfd, mmpath,
|
mmfd = openat(zlog_tmpdirfd, mmpath,
|
||||||
O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
|
O_RDWR | O_CREAT | O_EXCL | O_CLOEXEC, 0600);
|
||||||
@ -327,7 +338,7 @@ void zlog_tls_buffer_fini(void)
|
|||||||
zlog_tls_free(zlog_tls);
|
zlog_tls_free(zlog_tls);
|
||||||
zlog_tls_set(NULL);
|
zlog_tls_set(NULL);
|
||||||
|
|
||||||
snprintfrr(mmpath, sizeof(mmpath), "logbuf.%ld", zlog_gettid());
|
snprintfrr(mmpath, sizeof(mmpath), "logbuf.%jd", zlog_gettid());
|
||||||
if (do_unlink && unlinkat(zlog_tmpdirfd, mmpath, 0))
|
if (do_unlink && unlinkat(zlog_tmpdirfd, mmpath, 0))
|
||||||
zlog_err("unlink logbuf: %s (%d)", strerror(errno), errno);
|
zlog_err("unlink logbuf: %s (%d)", strerror(errno), errno);
|
||||||
}
|
}
|
||||||
@ -342,6 +353,24 @@ void zlog_tls_buffer_fini(void)
|
|||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
void zlog_msg_pid(struct zlog_msg *msg, intmax_t *pid, intmax_t *tid)
|
||||||
|
{
|
||||||
|
#ifndef __OpenBSD__
|
||||||
|
static thread_local intmax_t cached_pid = -1;
|
||||||
|
if (cached_pid != -1)
|
||||||
|
*pid = cached_pid;
|
||||||
|
else
|
||||||
|
cached_pid = *pid = (intmax_t)getpid();
|
||||||
|
#else
|
||||||
|
*pid = (intmax_t)getpid();
|
||||||
|
#endif
|
||||||
|
#ifdef CAN_DO_TLS
|
||||||
|
*tid = zlog_gettid();
|
||||||
|
#else
|
||||||
|
*tid = *pid;
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
static inline void zlog_tls_free(void *arg)
|
static inline void zlog_tls_free(void *arg)
|
||||||
{
|
{
|
||||||
struct zlog_tls *zlog_tls = arg;
|
struct zlog_tls *zlog_tls = arg;
|
||||||
|
@ -178,6 +178,11 @@ extern size_t zlog_msg_ts(struct zlog_msg *msg, struct fbuf *out,
|
|||||||
extern size_t zlog_msg_ts_3164(struct zlog_msg *msg, struct fbuf *out,
|
extern size_t zlog_msg_ts_3164(struct zlog_msg *msg, struct fbuf *out,
|
||||||
uint32_t flags);
|
uint32_t flags);
|
||||||
|
|
||||||
|
/* currently just returns the current PID/TID since we never write another
|
||||||
|
* thread's messages
|
||||||
|
*/
|
||||||
|
extern void zlog_msg_pid(struct zlog_msg *msg, intmax_t *pid, intmax_t *tid);
|
||||||
|
|
||||||
/* This list & struct implements the actual logging targets. It is accessed
|
/* This list & struct implements the actual logging targets. It is accessed
|
||||||
* lock-free from all threads, and thus MUST only be changed atomically, i.e.
|
* lock-free from all threads, and thus MUST only be changed atomically, i.e.
|
||||||
* RCU.
|
* RCU.
|
||||||
|
Loading…
Reference in New Issue
Block a user