---
name: "sg_comp_destroy"
section: "3"
platform: "linux"
package: "libstatgrab"
version: "0.92.1-2"
release: "Arch Linux 2026-11-01"
platforms: ["linux"]
url: "https://new.carta.tech/sg_comp_destroy"
---

# sg\_comp\_destroy(3)

managing system statistics delivery

## NAME

sg\_comp\_init, sg\_comp\_destroy, sg\_comp\_get\_tls, sg\_global\_lock, sg\_global\_unlock - managing system statistics delivery

## SYNOPSIS

```
FC#include "tools.h"F[0]
```

FCvoid \***get\_global\_static**F\[0\]  FC(unsigned int *id*);F\[0\]

FCsg\_error **sg\_global\_lock**F\[0\]  FC(void);F\[0\]

FCsg\_error **sg\_global\_unlock**F\[0\]  FC(void);F\[0\]

## DESCRIPTION

FC**sg\_comp\_init**F\[0\]() is called by FC**sg\_init**F\[0\]() to run the initialisation for the globals component and each registered libstatgrab component. This registration is done statically by appending a component initialisation information structure (instance-of FCsg\_comp\_infoF\[0\]) to the FCcomp\_infoF\[0\] list in FC*src/libstatgrab/globals.c*F\[0\]. The instance of FCsg\_comp\_infoF\[0\] is usually defined by using one of FC**EASY\_COMP\_SETUP**F\[0\]() or FC**EXTENDED\_COMP\_SETUP**F\[0\](), respectively.

FC**sg\_comp\_destroy**F\[0\]() is called by FC**sg\_shutdown**F\[0\]() 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 FC**sg\_comp\_destroy**F\[0\]() is invoked.

FC**sg\_comp\_get\_tls**F\[0\]() 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 FC**GLOBAL\_GET\_TLS**F\[0\]().

## NOTES

Delivering system statistics is the job of libstatgrab, managing the delivery is the job of the globals component. To fulfil this job, the components to manage must be prepared:

- 1\.

  declare component's global and TLS data structure (probably only on paper, not in code)

- 2\.

  define global initialisation, thread destruction and process destruction functions (if required by 1.)

- 3\.

  define component information structure using FC**\*\_COMP\_SETUP**F\[0\]()

- 4\.

  define component accessors using one or more of

  FC**EASY\_COMP\_ACCESS**F\[0\]()\
   FC**EASY\_COMP\_DIFF**F\[0\]()\
   FC**MULTI\_COMP\_ACCESS**F\[0\]()\
   FC**MULTI\_COMP\_DIFF**F\[0\]()

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 FC{ &sg\_cpu\_init, 0 }F\[0\] to above named FCcomp\_infoF\[0\] list.

Component initialisation information in detail:

```
FC
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;
};
      F[0]
```

Components which do not need something special can rely on FC**EASY\_COMP\_SETUP**F\[0\]():

**Initialising memory component**

```
FC
EASY_COMP_SETUP(mem,1,NULL);
        F[0]
```

When own initialisation is needed, doing it is a bit more complex:

**Initialising network component**

```
FC
#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)
        F[0]
```

### 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.

FCvoid **DEFAULT\_INIT\_COMP**F\[0\]  FC(identifier *comp*, *...*);F\[0\]

FCvoid **EASY\_COMP\_SETUP**F\[0\]  FC(identifier *comp*, size\_t *nvect*, *...*);F\[0\]

FCvoid **EXTENDED\_COMP\_SETUP**F\[0\]  FC(identifier *comp*, size\_t *nvect*, *...*);F\[0\]

FCvoid **GLOBAL\_SET\_ID**F\[0\]  FC(identifier *comp*, unsigned int *id*);F\[0\]

FCstruct sg\_##comp##\_glob \***GLOBAL\_GET\_TLS**F\[0\]  FC(identifier *comp*);F\[0\]

FCvoid **EASY\_COMP\_INIT\_FN**F\[0\]  FC(identifier *comp*);F\[0\]

FCvoid **EASY\_COMP\_DESTROY\_FN**F\[0\]  FC(identifier *comp*);F\[0\]

FCvoid **EASY\_COMP\_CLEANUP\_FN**F\[0\]  FC(identifier *comp*, size\_t *nvect*);F\[0\]

FCvoid **EASY\_COMP\_ACCESS**F\[0\]  FC(identifier *fn*, identifier *comp*, identifier *stat*, size\_t *idx*);F\[0\]

FCvoid **MULTI\_COMP\_ACCESS**F\[0\]  FC(identifier *fn*, identifier *comp*, identifier *stat*, size\_t *idx*);F\[0\]

FCvoid **EASY\_COMP\_DIFF**F\[0\]  FC(identifier *fn*, identifier *getfn*, identifier *comp*, identifier *stat*, size\_t *diffidx*, size\_t *nowidx*);F\[0\]

FCvoid **MULTI\_COMP\_DIFF**F\[0\]  FC(identifier *fn*, identifier *getfn*, identifier *comp*, identifier *stat*, size\_t *diffidx*, size\_t *nowidx*);F\[0\]

FC**EASY\_COMP\_SETUP**F\[0\]() cares about anything to be automatically done for instantiating a component information structure for the specified component FCcompF\[0\]. The created TLS storage structure will hold FCnvectF\[0\] pointer elements and that's it. All initialisation, destruction and cleanup-routines are created as needed using FC**EASY\_COMP\_INIT\_FN**F\[0\](), FC**EASY\_COMP\_DESTROY\_FN**F\[0\]() and FC**EASY\_COMP\_CLEANUP\_FN**F\[0\](). After the amount of required vector pointers to be stored the list of required mutexes must be specified, finished with a NULL pointer.

FC**EXTENDED\_COMP\_SETUP**F\[0\]() cares about anything to be automatically done for instantiating an component information structure for the specified component FCcompF\[0\] but the required definition of the initialisation, destruction and cleanup routines. The created TLS storage structure will hold FCnvectF\[0\] pointer 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 a NULL pointer. All standard routines can be created semi-automatically using FC**EASY\_COMP\_INIT\_FN**F\[0\](), FC**EASY\_COMP\_DESTROY\_FN**F\[0\]() and FC**EASY\_COMP\_CLEANUP\_FN**F\[0\]().

FC**DEFAULT\_INIT\_COMP**F\[0\]() 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.

FC**GLOBAL\_GET\_TLS**F\[0\]() returns the pointer to the component's *thread local storage*.

FC**GLOBAL\_SET\_ID**F\[0\]() stores the component identifier, required eg. to access its TLS.

FC**EASY\_COMP\_INIT\_FN**F\[0\]() defines a default component initialisation routine. It stores the component identifier and returns with SG\_ERROR\_NONE.

FC**EASY\_COMP\_DESTROY\_FN**F\[0\]() defines a default component destructor, called at the end of the entire process (or when the last FC**sg\_shutdown**F\[0\]() is called). The default destructor does nothing and usually an individual initialisation routine requires an individual destructor, too.

FC**EASY\_COMP\_CLEANUP\_FN**F\[0\]() defines a default TLS cleanup routine, always called when a thread ends to free vectors held in *thread local storage*.

FC**EASY\_COMP\_ACCESS**F\[0\]() defines accessors to a specific statistic containing one element provided by the component: the functions FC**fn**F\[0\]() and the FC**fn##\_r**F\[0\](). The following function must exists: FCsg\_error **fn##\_int**F\[0\]  FC(sg\_vector \**name##\_vect*);F\[0\] It accesses the vector idx from TLS of component FCcompF\[0\] and returns sg\_##name##\_stats. It manages all standard things like memory and error management, return value etc.

FC**EASY\_COMP\_DIFF**F\[0\]() 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 by FCnowidxF\[0\] and the resulting vector of FC**getfn**F\[0\](). The result is stored in the vector FCdiffidxF\[0\]. If there is no current result, simply the result of FC**getfn**F\[0\]() is returned.

FC**MULTI\_COMP\_ACCESS**F\[0\]() defines accessors to a specific statistic containing 0..n elements provided by the component: the functions FC**fn**F\[0\]() and the FC**fn##\_r**F\[0\](). The following function must exists: FCsg\_error **fn##\_int**F\[0\]  FC(sg\_vector \*\**name##\_vect*);F\[0\] It accesses the vector idx from TLS of component FCcompF\[0\] and returns sg\_##name##\_stats. It manages all standard things like memory and error anagement, return values, FCentriesF\[0\] update, etc.

FC**MULTI\_COMP\_DIFF**F\[0\]() does the same as FC**EASY\_COMP\_DIFF**F\[0\]() but for vectors with more than one element.

## SEE ALSO

[**libstatgrab**(3)](/libstatgrab/3.md) [**sg\_intro**(3)](/sg_intro/3.md) [**sg\_set\_error**(3)](/sg_set_error/3.md) [**sg\_comp\_init**(3)](/sg_comp_init/3.md) [**sg\_vector\_create**(3)](/sg_vector_create/3.md)

## WEBSITE

⟨https://libstatgrab.org/⟩
