Source
xxxxxxxxxx
/* https://stackoverflow.com/questions/28098082/unable-to-use-more-than-one-processor-group-for-my-threads-in-a-c-sharp-app */
/*
** 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/>.
**/
/* StringCchPrintf */
/* <sys/dkstat.h> removed in OpenBSD 5.7, only <sys/sched.h> with the same CP_* definitions remained */
static zbx_mutex_t cpustats_lock = ZBX_MUTEX_NULL;
static kstat_ctl_t *kc = NULL;
static kid_t kc_id = 0;
static kstat_t *(*ksp)[] = NULL; /* array of pointers to "cpu_stat" elements in kstat chain */
static int refresh_kstat(ZBX_CPUS_STAT_DATA *pcpus)
{
static int cpu_over_count_prev = 0;
int cpu_over_count = 0, inserted;
kid_t id;
kstat_t *k;
zabbix_log(LOG_LEVEL_DEBUG, "In %s()", __func__);
for (int i = 0; i < pcpus->count; i++)
(*ksp)[i] = NULL;
/* kstat_chain_update() can return: */
/* - -1 (error), */
/* - a new kstat chain ID (chain successfully updated), */
/* - 0 (kstat chain was up-to-date). We ignore this case to make refresh_kstat() */
/* usable for first-time initialization as the kstat chain is up-to-date after */
/* kstat_open(). */
if (-1 == (id = kstat_chain_update(kc)))
{
zabbix_log(LOG_LEVEL_ERR, "%s: kstat_chain_update() failed", __func__);
return FAIL;
}
if (0 != id)
kc_id = id;
for (k = kc->kc_chain; NULL != k; k = k->ks_next) /* traverse all kstat chain */
{
if (0 == strcmp("cpu_stat", k->ks_module))
{
inserted = 0;
for (int i = 1; i <= pcpus->count; i++) /* search in our array of ZBX_SINGLE_CPU_STAT_DATAs */
{
if (pcpus->cpu[i].cpu_num == k->ks_instance) /* CPU instance found */
{
(*ksp)[i - 1] = k;
inserted = 1;
break;
}
if (ZBX_CPUNUM_UNDEF == pcpus->cpu[i].cpu_num)
{
/* free slot found, most likely first-time initialization */
pcpus->cpu[i].cpu_num = k->ks_instance;
(*ksp)[i - 1] = k;
inserted = 1;