| 1 |
/** |
| 2 |
* collectd - src/xencpu.c |
| 3 |
* Copyright (C) 2016 Pavel Rochnyak |
| 4 |
* |
| 5 |
* This program is free software; you can redistribute it and/or modify it |
| 6 |
* under the terms of the GNU General Public License as published by the |
| 7 |
* Free Software Foundation; only version 2 of the License is applicable. |
| 8 |
* |
| 9 |
* This program is distributed in the hope that it will be useful, but |
| 10 |
* WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 |
* General Public License for more details. |
| 13 |
* |
| 14 |
* You should have received a copy of the GNU General Public License along |
| 15 |
* with this program; if not, write to the Free Software Foundation, Inc., |
| 16 |
* 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 17 |
* |
| 18 |
* Authors: |
| 19 |
* Pavel Rochnyak <pavel2000 ngs.ru> |
| 20 |
**/ |
| 21 |
|
| 22 |
#include "collectd.h" |
| 23 |
|
| 24 |
#include "plugin.h" |
| 25 |
#include "utils/common/common.h" |
| 26 |
|
| 27 |
#include <xenctrl.h> |
| 28 |
|
| 29 |
#ifdef XENCTRL_HAS_XC_INTERFACE |
| 30 |
|
| 31 |
// Xen-4.1+ |
| 32 |
#define XC_INTERFACE_INIT_ARGS NULL, NULL, 0 |
| 33 |
xc_interface *xc_handle; |
| 34 |
|
| 35 |
#else /* XENCTRL_HAS_XC_INTERFACE */ |
| 36 |
|
| 37 |
// For xen-3.4/xen-4.0 |
| 38 |
#include <string.h> |
| 39 |
#define xc_strerror(xc_interface, errcode) strerror(errcode) |
| 40 |
#define XC_INTERFACE_INIT_ARGS |
| 41 |
typedef int xc_interface; |
| 42 |
xc_interface xc_handle = 0; |
| 43 |
|
| 44 |
#endif /* XENCTRL_HAS_XC_INTERFACE */ |
| 45 |
|
| 46 |
uint32_t num_cpus = 0; |
| 47 |
xc_cpuinfo_t *cpu_info; |
| 48 |
static value_to_rate_state_t *cpu_states; |
| 49 |
|
| 50 |
static int xencpu_init(void) { |
| 51 |
xc_handle = xc_interface_open(XC_INTERFACE_INIT_ARGS); |
| 52 |
if (!xc_handle) { |
| 53 |
ERROR("xencpu: xc_interface_open() failed"); |
| 54 |
return -1; |
| 55 |
} |
| 56 |
|
| 57 |
xc_physinfo_t *physinfo; |
| 58 |
|
| 59 |
physinfo = calloc(1, sizeof(*physinfo)); |
| 60 |
if (physinfo == NULL) { |
| 61 |
ERROR("xencpu plugin: calloc() for physinfo failed."); |
| 62 |
xc_interface_close(xc_handle); |
| 63 |
return ENOMEM; |
| 64 |
} |
| 65 |
|
| 66 |
if (xc_physinfo(xc_handle, physinfo) < 0) { |
| 67 |
ERROR("xencpu plugin: xc_physinfo() failed"); |
| 68 |
xc_interface_close(xc_handle); |
| 69 |
free(physinfo); |
| 70 |
return -1; |
| 71 |
} |
| 72 |
|
| 73 |
num_cpus = physinfo->nr_cpus; |
| 74 |
free(physinfo); |
| 75 |
|
| 76 |
INFO("xencpu plugin: Found %" PRIu32 " processors.", num_cpus); |
| 77 |
|
| 78 |
cpu_info = calloc(num_cpus, sizeof(*cpu_info)); |
| 79 |
if (cpu_info == NULL) { |
| 80 |
ERROR("xencpu plugin: calloc() for num_cpus failed."); |
| 81 |
xc_interface_close(xc_handle); |
| 82 |
return ENOMEM; |
| 83 |
} |
| 84 |
|
| 85 |
cpu_states = calloc(num_cpus, sizeof(*cpu_states)); |
| 86 |
if (cpu_states == NULL) { |
| 87 |
ERROR("xencpu plugin: calloc() for cpu_states failed."); |
| 88 |
xc_interface_close(xc_handle); |
| 89 |
free(cpu_info); |
| 90 |
return ENOMEM; |
| 91 |
} |
| 92 |
|
| 93 |
return 0; |
| 94 |
} /* static int xencpu_init */ |
| 95 |
|
| 96 |
static int xencpu_shutdown(void) { |
| 97 |
free(cpu_states); |
| 98 |
free(cpu_info); |
| 99 |
xc_interface_close(xc_handle); |
| 100 |
|
| 101 |
return 0; |
| 102 |
} /* static int xencpu_shutdown */ |
| 103 |
|
| 104 |
static void submit_value(int cpu_num, gauge_t value) { |
| 105 |
value_list_t vl = VALUE_LIST_INIT; |
| 106 |
|
| 107 |
vl.values = &(value_t){.gauge = value}; |
| 108 |
vl.values_len = 1; |
| 109 |
|
| 110 |
sstrncpy(vl.plugin, "xencpu", sizeof(vl.plugin)); |
| 111 |
sstrncpy(vl.type, "percent", sizeof(vl.type)); |
| 112 |
sstrncpy(vl.type_instance, "load", sizeof(vl.type_instance)); |
| 113 |
|
| 114 |
if (cpu_num >= 0) { |
| 115 |
snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "%i", cpu_num); |
| 116 |
} |
| 117 |
plugin_dispatch_values(&vl); |
| 118 |
} /* static void submit_value */ |
| 119 |
|
| 120 |
static int xencpu_read(void) { |
| 121 |
cdtime_t now = cdtime(); |
| 122 |
|
| 123 |
int rc, nr_cpus; |
| 124 |
|
| 125 |
rc = xc_getcpuinfo(xc_handle, num_cpus, cpu_info, &nr_cpus); |
| 126 |
if (rc < 0) { |
| 127 |
ERROR("xencpu: xc_getcpuinfo() Failed: %d %s\n", rc, |
| 128 |
xc_strerror(xc_handle, errno)); |
| 129 |
return -1; |
| 130 |
} |
| 131 |
|
| 132 |
int status; |
| 133 |
for (int cpu = 0; cpu < nr_cpus; cpu++) { |
| 134 |
gauge_t rate = NAN; |
| 135 |
|
| 136 |
status = value_to_rate(&rate, (value_t){.derive = cpu_info[cpu].idletime}, |
| 137 |
DS_TYPE_DERIVE, now, &cpu_states[cpu]); |
| 138 |
if (status == 0) { |
| 139 |
submit_value(cpu, 100 - rate / 10000000); |
| 140 |
} |
| 141 |
} |
| 142 |
|
| 143 |
return 0; |
| 144 |
} /* static int xencpu_read */ |
| 145 |
|
| 146 |
void module_register(void) { |
| 147 |
plugin_register_init("xencpu", xencpu_init); |
| 148 |
plugin_register_read("xencpu", xencpu_read); |
| 149 |
plugin_register_shutdown("xencpu", xencpu_shutdown); |
| 150 |
} /* void module_register */ |