sg_comp_init(3)
π sg_comp_init(3) β managing system statistics delivery π
okay bestie, this is the libstatgrab manual page for sg_comp_init(3), and it is literally about managing system statistics delivery. π«Ά
β¨ NAME β¨
The whole squad is here: sg_comp_init, sg_comp_destroy, sg_comp_get_tls, sg_global_lock, and sg_global_unlock β and their collective vibe is managing system statistics delivery. π
πΈ SYNOPSIS πΈ
#include "tools.h"
void *get_global_static (unsigned int id);
sg_error sg_global_lock (void);
sg_error sg_global_unlock (void);
π¦ DESCRIPTION π¦
sg_comp_init() is called by sg_init() to run the initialisation for the globals component and each registered libstatgrab component. That registration happens statically by appending a component initialisation information structure (an instance-of sg_comp_info) to the comp_info list in src/libstatgrab/globals.c. The instance of sg_comp_info is usually defined by using one of EASY_COMP_SETUP() or EXTENDED_COMP_SETUP(), respectively. π
sg_comp_destroy() is called by sg_shutdown() to destroy all global resources, eg. translation tables for device names or compiled regular expressions to match path names etc. Statistics resources are managed somewhere else and are freed (conceptually and usually) before sg_comp_destroy() is invoked. π
sg_comp_get_tls() is the internal function to access the thread local storage (formerly static globals) of the component. Usually it's easier to rely on the encapsulating macro GLOBAL_GET_TLS(). β¨
π§ NOTES π§
Delivering system statistics is the job of libstatgrab, and managing the delivery is the job of the globals component. To fulfil this job, the components to manage must be prepared:
- declare component's global and TLS data structure (probably only on paper, not in code)
- define global initialisation, thread destruction and process destruction functions (if required by 1.)
- define component information structure using
*_COMP_SETUP() - define component accessors using one or more of
EASY_COMP_ACCESS()
EASY_COMP_DIFF()
MULTI_COMP_ACCESS()
MULTI_COMP_DIFF()
When having done these steps, a new component delivering new statistics is born and needs to be "announced". Assuming the component is named cpu, append the line { &sg_cpu_init, 0 } to above named comp_info list. π
Component initialisation information in detail:
typedef sg_error (*comp_global_init_function)(unsigned id);
typedef void (*comp_global_destroy_function)(void);
typedef void (*comp_global_cleanup_function)(void *);
struct sg_comp_status {
sg_error init_error;
};
struct sg_comp_init {
comp_global_init_function init_fn;
comp_global_destroy_function destroy_fn;
comp_global_cleanup_function cleanup_fn;
size_t static_buf_size;
#if defined(ENABLE_THREADS) && defined(HAVE_PTHREAD)
const char **required_locks;
#endif
struct sg_comp_status *status;
};
Components which do not need something special can rely on EASY_COMP_SETUP():
Initialising memory component
EASY_COMP_SETUP(mem,1,NULL);
When own initialisation is needed, doing it is a bit more complex:
Initialising network component
#define SG_NETWORK_IO_NOW_IDX 0
#define SG_NETWORK_IO_DIFF_IDX 1
#define SG_NETWORK_IFACE_IDX 2
#define SG_NETWORK_MAX_IDX 3
EXTENDED_COMP_SETUP(network,SG_NETWORK_MAX_IDX,NULL);
#ifdef LINUX
static regex_t network_io_rx;
#define RX_MATCH_COUNT (8+1)
#endif
sg_error
sg_network_init_comp(unsigned id) {
GLOBAL_SET_ID(network,id);
#ifdef LINUX
if( regcomp( &network_io_rx, ..., REG_EXTENDED)!=0) {
return sg_set_error(SG_ERROR_PARSE, NULL);
}
#endif
return SG_ERROR_NONE;
}
void
sg_network_destroy_comp(void) {
#ifdef LINUX
regfree(&network_io_rx);
#endif
}
EASY_COMP_CLEANUP_FN(network,SG_NETWORK_MAX_IDX)
π MACROS TO WORK WITH THE COMPONENT MANAGER π
To simplify working with the component management functions, some preprocessor macros are available. They are shown here as if they were functions to ease understanding.
void DEFAULT_INIT_COMP (identifier comp, ...);
void EASY_COMP_SETUP (identifier comp, size_t nvect, ...);
void EXTENDED_COMP_SETUP (identifier comp, size_t nvect, ...);
void GLOBAL_SET_ID (identifier comp, unsigned int id);
struct sg_##comp##_glob *GLOBAL_GET_TLS (identifier comp);
void EASY_COMP_INIT_FN (identifier comp);
void EASY_COMP_DESTROY_FN (identifier comp);
void EASY_COMP_CLEANUP_FN (identifier comp, size_t nvect);
void EASY_COMP_ACCESS (identifier fn, identifier comp, identifier stat,
size_t idx);
void MULTI_COMP_ACCESS (identifier fn, identifier comp, identifier stat,
size_t idx);
void EASY_COMP_DIFF (identifier fn, identifier getfn, identifier comp,
identifier stat, size_t diffidx, size_t nowidx);
void MULTI_COMP_DIFF (identifier fn, identifier getfn, identifier comp,
identifier stat, size_t diffidx, size_t nowidx);
EASY_COMP_SETUP()cares about anything to be automatically done for instantiating a component information structure for the specified componentcomp. The created TLS storage structure will holdnvectpointer elements and that's it. All initialisation, destruction and cleanup-routines are created as needed usingEASY_COMP_INIT_FN(),EASY_COMP_DESTROY_FN()andEASY_COMP_CLEANUP_FN(). After the amount of required vector pointers to be stored the list of required mutexes must be specified, finished with aNULLpointer. πEXTENDED_COMP_SETUP()cares about anything to be automatically done for instantiating an component information structure for the specified componentcompbut the required definition of the initialisation, destruction and cleanup routines. The created TLS storage structure will holdnvectpointer elements and that's it. After the amount of required vector pointers to be stored, the list of required mutexes must be specified, finished with aNULLpointer. All standard routines can be created semi-automatically usingEASY_COMP_INIT_FN(),EASY_COMP_DESTROY_FN()andEASY_COMP_CLEANUP_FN(). πDEFAULT_INIT_COMP()just declares the prototypes for the initialisation, destruction and cleanup routines, defines the initialisation status buffer, lock-names list and finally fills the component initialisation structure. Use this when your TLS storage contains not only vector pointers. π¦GLOBAL_GET_TLS()returns the pointer to the component's thread local storage. β¨GLOBAL_SET_ID()stores the component identifier, required eg. to access its TLS. πEASY_COMP_INIT_FN()defines a default component initialisation routine. It stores the component identifier and returns withSG_ERROR_NONE. πΈEASY_COMP_DESTROY_FN()defines a default component destructor, called at the end of the entire process (or when the lastsg_shutdown()is called). The default destructor does nothing and usually an individual initialisation routine requires an individual destructor, too. π«ΆEASY_COMP_CLEANUP_FN()defines a default TLS cleanup routine, always called when a thread ends to free vectors held in thread local storage. πEASY_COMP_ACCESS()defines accessors to a specific statistic containing one element provided by the component: the functionsfn()and thefn##_r(). The following function must exists:sg_error fn##_int (sg_vector *name##_vect);It accesses the vectoridxfrom TLS of componentcompand returnssg_##name##_stats. It manages all standard things like memory and error management, return value etc. βEASY_COMP_DIFF()returns the difference between the two statistic collection runs. The variant dealing with statgrab owned statistics return the difference between the content currently in the vector specified bynowidxand the resulting vector ofgetfn(). The result is stored in the vectordiffidx. If there is no current result, simply the result ofgetfn()is returned. π₯MULTI_COMP_ACCESS()defines accessors to a specific statistic containing0..nelements provided by the component: the functionsfn()and thefn##_r(). The following function must exists:sg_error fn##_int (sg_vector **name##_vect);It accesses the vectoridxfrom TLS of componentcompand returnssg_##name##_stats. It manages all standard things like memory and error anagement, return values, entries update, etc. πMULTI_COMP_DIFF()does the same asEASY_COMP_DIFF()but for vectors with more than one element. π
π SEE ALSO π
libstatgrab(3) sg_intro(3) sg_set_error(3) sg_comp_init(3) sg_vector_create(3)
π WEBSITE π
β¨https://libstatgrab.org/β©
xoxo bestie, the footer tea is: this is libstatgrab, dated 2019-10-03, and the page is sg_comp_init(3). πβ¨π
π¦ where this page comes from
- alpine 3.24: libstatgrab-doc 0.92.1-r1 Β· LGPL-2.0-or-later
- arch rolling: libstatgrab 0.92.1-2 Β· LGPL AND GPL
- fedora 43: libstatgrab-devel 1:0.92.1-14.fc43 Β· LGPL-2.1-or-later
- fedora 43: libstatgrab-devel 1:0.92.1-14.fc43 Β· LGPL-2.1-or-later
- fedora 44: libstatgrab-devel 1:0.92.1-15.fc44 Β· LGPL-2.1-or-later
- fedora 44: libstatgrab-devel 1:0.92.1-15.fc44 Β· LGPL-2.1-or-later
- nixos 26.05: libstatgrab 0.92.1
- nixpkgs unstable: libstatgrab 0.92.1
- opensuse-leap 16.0: libstatgrab-devel 0.92.1-bp160.1.14 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libstatgrab-devel 0.92.1-bp160.1.14 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libstatgrab-devel 0.92.1-bp160.1.14 Β· LGPL-2.1-or-later
- opensuse-leap 16.0: libstatgrab-devel 0.92.1-bp160.1.14 Β· LGPL-2.1-or-later
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms