Source
static void procstat_get_monitored_pids(zbx_vector_uint64_t *pids, const zbx_vector_ptr_t *queries, int pids_num)
/*
** Copyright (C) 2001-2025 Zabbix SIA
**
** This program is free software: you can redistribute it and/or modify it under the terms of
** the GNU Affero General Public License as published by the Free Software Foundation, version 3.
**
** This program 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 Affero General Public License for more details.
**
** You should have received a copy of the GNU Affero General Public License along with this program.
** If not, see <https://www.gnu.org/licenses/>.
**/
/*
* The process CPU statistics are stored using the following memory layout.
*
* .--------------------------------------.
* | header |
* | ------------------------------------ |
* | process cpu utilization queries |
* | and historical data |
* | ------------------------------------ |
* | free space |
* '--------------------------------------'
*
* Because the shared memory can be resized by other processes instead of
* using pointers (when allocating strings, building single linked lists)
* the memory offsets from the beginning of shared memory segment are used.
* 0 offset is interpreted similarly to NULL pointer.
*
* Currently integer values are used to store offsets to internally allocated
* memory which leads to 2GB total size limit.
*
* During every data collection cycle collector does the following:
* 1) acquires list of all processes running on system
* 2) builds a list of processes monitored by queries
* 3) reads total cpu utilization snapshot for the monitored processes
* 4) calculates cpu utilization difference by comparing with previous snapshot
* 5) updates cpu utilization values for queries.
* 6) saves the last cpu utilization snapshot
*
* Initialisation.
* * zbx_procstat_init() initialises procstat dshm structure but doesn't allocate memory from the system
* (zbx_dshm_create() called with size 0).
* * the first call of procstat_add() allocates the shared memory for the header and the first query
* via call to zbx_dshm_realloc().
* * The header is initialised in procstat_copy_data() which is called back from zbx_dshm_realloc().
*
* Memory allocation within dshm.
* * Ensure that memory segment has enough free space with procstat_dshm_has_enough_space() before
* allocating space within segment with procstat_alloc() or functions that use it.
* * Check how much of the allocated dshm is actually used by procstat by procstat_dshm_used_size().
* * Change the dshm size with zbx_dshm_realloc().
*
* Synchronisation.
* * agentd processes share a single instance of ZBX_COLLECTOR_DATA (*collector) containing reference
* to shared procstat memory segment.
* * Each agentd process also holds local reference to procstat shared memory segment.
* * The system keeps the shared memory segment until the last process detaches from it.
* * Synchronise both references with procstat_reattach() before using procstat shared memory segment.
*/
/* local reference to the procstat shared memory */
static zbx_dshm_ref_t procstat_ref;
typedef struct
{
/* a linked list of active queries (offset of the first active query) */
int queries;
/* the total size of the allocated queries and strings */
int size_allocated;
/* the total shared memory segment size */
size_t size;
}
zbx_procstat_header_t;