1 /*
2 ** Zabbix
3 ** Copyright (C) 2001-2021 Zabbix SIA
4 **
5 ** This program is free software; you can redistribute it and/or modify
6 ** it under the terms of the GNU General Public License as published by
7 ** the Free Software Foundation; either version 2 of the License, or
8 ** (at your option) any later version.
9 **
10 ** This program is distributed in the hope that it will be useful,
11 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
12 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 ** GNU General Public License for more details.
14 **
15 ** You should have received a copy of the GNU General Public License
16 ** along with this program; if not, write to the Free Software
17 ** Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18 **/
19 
20 #ifndef ZABBIX_CPUSTAT_H
21 #define ZABBIX_CPUSTAT_H
22 
23 #include "sysinfo.h"
24 #include "zbxalgo.h"
25 
26 #ifdef _WINDOWS
27 #	include "perfmon.h"
28 
29 typedef struct
30 {
31 	zbx_perf_counter_data_t	**cpu_counter;
32 	zbx_perf_counter_data_t	*queue_counter;
33 	int			count;
34 }
35 ZBX_CPUS_STAT_DATA;
36 
37 #define CPU_COLLECTOR_STARTED(collector)	((collector) && (collector)->cpus.queue_counter)
38 
39 int	get_cpu_perf_counter_value(int cpu_num, int interval, double *value, char **error);
40 
41 #else	/* not _WINDOWS */
42 
43 typedef struct
44 {
45 	zbx_uint64_t	h_counter[ZBX_CPU_STATE_COUNT][MAX_COLLECTOR_HISTORY];
46 	unsigned char	h_status[MAX_COLLECTOR_HISTORY];
47 #if (MAX_COLLECTOR_HISTORY % 8) > 0
48 	unsigned char	padding0[8 - (MAX_COLLECTOR_HISTORY % 8)];	/* for 8-byte alignment */
49 #endif
50 	int		h_first;
51 	int		h_count;
52 	int		cpu_num;
53 	int		padding1;	/* for 8-byte alignment */
54 }
55 ZBX_SINGLE_CPU_STAT_DATA;
56 
57 typedef struct
58 {
59 	ZBX_SINGLE_CPU_STAT_DATA	*cpu;
60 	int				count;
61 }
62 ZBX_CPUS_STAT_DATA;
63 
64 #ifdef _AIX
65 /* collector layout is designed to be cache-friendly for updates and large number of CPUs */
66 typedef struct
67 {
68 	unsigned char	status;
69 	/* data comes from IBM AIX 'perfstat_cpu_util_t' (percents of utilization) */
70 	zbx_uint64_t	user_pct;	/* user mode */
71 	zbx_uint64_t	kern_pct;	/* kernel mode */
72 	zbx_uint64_t	idle_pct;	/* idle mode */
73 	zbx_uint64_t	wait_pct;	/* wait mode */
74 }
75 ZBX_CPU_UTIL_PCT_AIX;
76 
77 typedef struct
78 {
79 	ZBX_CPU_UTIL_PCT_AIX	*counters;	/* pointer to 2D array with history data */
80 	int			row_num;	/* number of rows (MAX_COLLECTOR_HISTORY) */
81 	int			column_num;	/* number of columns (number of CPUs) */
82 	int			h_latest;	/* the index of the most recent entry in the history data */
83 	int			h_count;	/* the number of entries in the history data */
84 }
85 ZBX_CPUS_UTIL_DATA_AIX;
86 #endif /* _AIX */
87 
88 #define CPU_COLLECTOR_STARTED(collector)	(collector)
89 
90 void	collect_cpustat(ZBX_CPUS_STAT_DATA *pcpus);
91 int	get_cpustat(AGENT_RESULT *result, int cpu_num, int state, int mode);
92 #ifdef _AIX
93 void	collect_cpustat_physical(ZBX_CPUS_UTIL_DATA_AIX *cpus_phys_util);
94 int	get_cpustat_physical(AGENT_RESULT *result, int cpu_num, int state, int mode);
95 #endif
96 
97 #endif	/* _WINDOWS */
98 
99 int	init_cpu_collector(ZBX_CPUS_STAT_DATA *pcpus);
100 void	free_cpu_collector(ZBX_CPUS_STAT_DATA *pcpus);
101 
102 #define ZBX_CPUNUM_UNDEF	-1	/* unidentified yet CPUs */
103 #define ZBX_CPUNUM_ALL		-2	/* request data for all CPUs */
104 
105 #define ZBX_CPU_STATUS_ONLINE	0
106 #define ZBX_CPU_STATUS_OFFLINE	1
107 #define ZBX_CPU_STATUS_UNKNOWN	2
108 
109 int	get_cpus(zbx_vector_uint64_pair_t *vector);
110 
111 #endif
112