| 1 |
/** |
| 2 |
* collectd - src/numa.c |
| 3 |
* Copyright (C) 2012 Florian Forster |
| 4 |
* |
| 5 |
* Permission is hereby granted, free of charge, to any person obtaining a |
| 6 |
* copy of this software and associated documentation files (the "Software"), |
| 7 |
* to deal in the Software without restriction, including without limitation |
| 8 |
* the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 9 |
* and/or sell copies of the Software, and to permit persons to whom the |
| 10 |
* Software is furnished to do so, subject to the following conditions: |
| 11 |
* |
| 12 |
* The above copyright notice and this permission notice shall be included in |
| 13 |
* all copies or substantial portions of the Software. |
| 14 |
* |
| 15 |
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 16 |
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 17 |
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 18 |
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 19 |
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING |
| 20 |
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER |
| 21 |
* DEALINGS IN THE SOFTWARE. |
| 22 |
* |
| 23 |
* Authors: |
| 24 |
* Florian Forster <octo at collectd.org> |
| 25 |
**/ |
| 26 |
|
| 27 |
#include "collectd.h" |
| 28 |
|
| 29 |
#include "plugin.h" |
| 30 |
#include "utils/common/common.h" |
| 31 |
|
| 32 |
#if !KERNEL_LINUX |
| 33 |
#error "No applicable input method." |
| 34 |
#endif |
| 35 |
|
| 36 |
#ifndef NUMA_ROOT_DIR |
| 37 |
#define NUMA_ROOT_DIR "/sys/devices/system/node" |
| 38 |
#endif |
| 39 |
|
| 40 |
static int max_node = -1; |
| 41 |
|
| 42 |
static void numa_dispatch_value(int node, /* {{{ */ |
| 43 |
const char *type_instance, value_t v) { |
| 44 |
value_list_t vl = VALUE_LIST_INIT; |
| 45 |
|
| 46 |
vl.values = &v; |
| 47 |
vl.values_len = 1; |
| 48 |
|
| 49 |
sstrncpy(vl.plugin, "numa", sizeof(vl.plugin)); |
| 50 |
snprintf(vl.plugin_instance, sizeof(vl.plugin_instance), "node%i", node); |
| 51 |
sstrncpy(vl.type, "vmpage_action", sizeof(vl.type)); |
| 52 |
sstrncpy(vl.type_instance, type_instance, sizeof(vl.type_instance)); |
| 53 |
|
| 54 |
plugin_dispatch_values(&vl); |
| 55 |
} /* }}} void numa_dispatch_value */ |
| 56 |
|
| 57 |
static int numa_read_node(int node) /* {{{ */ |
| 58 |
{ |
| 59 |
char path[PATH_MAX]; |
| 60 |
FILE *fh; |
| 61 |
char buffer[128]; |
| 62 |
int status; |
| 63 |
int success; |
| 64 |
|
| 65 |
snprintf(path, sizeof(path), NUMA_ROOT_DIR "/node%i/numastat", node); |
| 66 |
|
| 67 |
fh = fopen(path, "r"); |
| 68 |
if (fh == NULL) { |
| 69 |
ERROR("numa plugin: Reading node %i failed: open(%s): %s", node, path, |
| 70 |
STRERRNO); |
| 71 |
return -1; |
| 72 |
} |
| 73 |
|
| 74 |
success = 0; |
| 75 |
while (fgets(buffer, sizeof(buffer), fh) != NULL) { |
| 76 |
char *fields[4]; |
| 77 |
value_t v; |
| 78 |
|
| 79 |
status = strsplit(buffer, fields, STATIC_ARRAY_SIZE(fields)); |
| 80 |
if (status != 2) { |
| 81 |
WARNING("numa plugin: Ignoring line with unexpected " |
| 82 |
"number of fields (node %i).", |
| 83 |
node); |
| 84 |
continue; |
| 85 |
} |
| 86 |
|
| 87 |
v.derive = 0; |
| 88 |
status = parse_value(fields[1], &v, DS_TYPE_DERIVE); |
| 89 |
if (status != 0) |
| 90 |
continue; |
| 91 |
|
| 92 |
numa_dispatch_value(node, fields[0], v); |
| 93 |
success++; |
| 94 |
} |
| 95 |
|
| 96 |
fclose(fh); |
| 97 |
return success ? 0 : -1; |
| 98 |
} /* }}} int numa_read_node */ |
| 99 |
|
| 100 |
static int numa_read(void) /* {{{ */ |
| 101 |
{ |
| 102 |
int i; |
| 103 |
int status; |
| 104 |
int success; |
| 105 |
|
| 106 |
if (max_node < 0) { |
| 107 |
WARNING("numa plugin: No NUMA nodes were detected."); |
| 108 |
return -1; |
| 109 |
} |
| 110 |
|
| 111 |
success = 0; |
| 112 |
for (i = 0; i <= max_node; i++) { |
| 113 |
status = numa_read_node(i); |
| 114 |
if (status == 0) |
| 115 |
success++; |
| 116 |
} |
| 117 |
|
| 118 |
return success ? 0 : -1; |
| 119 |
} /* }}} int numa_read */ |
| 120 |
|
| 121 |
static int numa_init(void) /* {{{ */ |
| 122 |
{ |
| 123 |
/* Determine the number of nodes on this machine. */ |
| 124 |
while (42) { |
| 125 |
char path[PATH_MAX]; |
| 126 |
struct stat statbuf = {0}; |
| 127 |
int status; |
| 128 |
|
| 129 |
snprintf(path, sizeof(path), NUMA_ROOT_DIR "/node%i", max_node + 1); |
| 130 |
|
| 131 |
status = stat(path, &statbuf); |
| 132 |
if (status == 0) { |
| 133 |
max_node++; |
| 134 |
continue; |
| 135 |
} else if (errno == ENOENT) { |
| 136 |
break; |
| 137 |
} else /* ((status != 0) && (errno != ENOENT)) */ |
| 138 |
{ |
| 139 |
ERROR("numa plugin: stat(%s) failed: %s", path, STRERRNO); |
| 140 |
return -1; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
DEBUG("numa plugin: Found %i nodes.", max_node + 1); |
| 145 |
return 0; |
| 146 |
} /* }}} int numa_init */ |
| 147 |
|
| 148 |
void module_register(void) { |
| 149 |
plugin_register_init("numa", numa_init); |
| 150 |
plugin_register_read("numa", numa_read); |
| 151 |
} /* void module_register */ |