REORG: includes: create tinfo.h for the thread_info struct

The thread_info struct is convenient to store various per-thread info
without having to resort to a painful thread_local storage which is
slow and painful to initialize.

The problem is, by having this one in thread.h it's very difficult to
add more entries there because everyone already includes thread.h so
conversely thread.h cannot reference certain types.

There's no point in having this there, instead let's create a new pair
of files, tinfo{,-t}.h, which declare the structure. This way it will
become possible to extend them with other includes and have certain
files store their own types there.
This commit is contained in:
Willy Tarreau 2020-06-29 09:57:23 +02:00
parent 739879a23b
commit e4d1505c83
4 changed files with 81 additions and 23 deletions

View File

@ -26,7 +26,6 @@
#ifdef USE_THREAD
#include <pthread.h>
#endif
#include <time.h>
#include <haproxy/defaults.h>
@ -75,26 +74,6 @@
/*** Common parts below ***/
/* thread info flags, for ha_thread_info[].flags */
#define TI_FL_STUCK 0x00000001
/* This structure describes all the per-thread info we need. When threads are
* disabled, it contains the same info for the single running thread (except
* the pthread identifier which does not exist).
*/
struct thread_info {
__decl_thread(pthread_t pthread);
clockid_t clock_id;
timer_t wd_timer; /* valid timer or TIMER_INVALID if not set */
uint64_t prev_cpu_time; /* previous per thread CPU time */
uint64_t prev_mono_time; /* previous system wide monotonic time */
unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
unsigned int flags; /* thread info flags, TI_FL_* */
/* pad to cache line (64B) */
char __pad[0]; /* unused except to check remaining room */
char __end[0] __attribute__((aligned(64)));
};
/* storage types used by spinlocks and RW locks */
#define __HA_SPINLOCK_T unsigned long
#define __HA_RWLOCK_T unsigned long

View File

@ -31,6 +31,7 @@
#include <haproxy/api.h>
#include <haproxy/thread-t.h>
#include <haproxy/tinfo.h>
/* Note: this file mainly contains 5 sections:
@ -47,8 +48,6 @@
int parse_nbthread(const char *arg, char **err);
int thread_get_default_count();
extern int thread_cpus_enabled_at_boot;
extern struct thread_info ha_thread_info[MAX_THREADS];
extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
#ifndef USE_THREAD

48
include/haproxy/tinfo-t.h Normal file
View File

@ -0,0 +1,48 @@
/*
* include/haproxy/tinfo-t.h
* Definitions of the thread_info structure.
*
* Copyright (C) 2020 Willy Tarreau - w@1wt.eu
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, version 2.1
* exclusively.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _HAPROXY_TINFO_T_H
#define _HAPROXY_TINFO_T_H
#include <time.h>
#include <haproxy/api-t.h>
/* thread info flags, for ha_thread_info[].flags */
#define TI_FL_STUCK 0x00000001
/* This structure describes all the per-thread info we need. When threads are
* disabled, it contains the same info for the single running thread (except
* the pthread identifier which does not exist).
*/
struct thread_info {
__decl_thread(pthread_t pthread);
clockid_t clock_id;
timer_t wd_timer; /* valid timer or TIMER_INVALID if not set */
uint64_t prev_cpu_time; /* previous per thread CPU time */
uint64_t prev_mono_time; /* previous system wide monotonic time */
unsigned int idle_pct; /* idle to total ratio over last sample (percent) */
unsigned int flags; /* thread info flags, TI_FL_* */
/* pad to cache line (64B) */
char __pad[0]; /* unused except to check remaining room */
char __end[0] __attribute__((aligned(64)));
};
#endif /* _HAPROXY_TINFO_T_H */

32
include/haproxy/tinfo.h Normal file
View File

@ -0,0 +1,32 @@
/*
* include/haproxy/tinfo.h
* Export of ha_thread_info[] and ti pointer.
*
* Copyright (C) 2020 Willy Tarreau - w@1wt.eu
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation, version 2.1
* exclusively.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifndef _HAPROXY_TINFO_H
#define _HAPROXY_TINFO_H
#include <haproxy/api.h>
#include <haproxy/tinfo-t.h>
/* the struct is in thread.c */
extern struct thread_info ha_thread_info[MAX_THREADS];
extern THREAD_LOCAL struct thread_info *ti; /* thread_info for the current thread */
#endif /* _HAPROXY_TINFO_H */