| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#ifndef _GNU_SOURCE |
| 22 |
# define _GNU_SOURCE |
| 23 |
#endif |
| 24 |
|
| 25 |
#include <sys/utsname.h> |
| 26 |
#include <ctype.h> |
| 27 |
#include <errno.h> |
| 28 |
#include <stdio.h> |
| 29 |
#include <stdlib.h> |
| 30 |
#include <string.h> |
| 31 |
|
| 32 |
#include "common.h" |
| 33 |
#include "cputopology.h" |
| 34 |
#include "string-macros.h" |
| 35 |
#include "messages.h" |
| 36 |
#include "procparser.h" |
| 37 |
#include "sysfsparser.h" |
| 38 |
#include "system.h" |
| 39 |
#include "xalloc.h" |
| 40 |
|
| 41 |
#define PATH_PROC_CPUINFO "/proc/cpuinfo" |
| 42 |
|
| 43 |
#define PATH_SYS_SYSTEM PATH_SYS "/devices/system" |
| 44 |
#define PATH_SYS_CPU PATH_SYS_SYSTEM "/cpu" |
| 45 |
|
| 46 |
bool |
| 47 |
get_processor_is_hot_pluggable (unsigned int cpu) |
| 48 |
{ |
| 49 |
return sysfsparser_path_exist (PATH_SYS_CPU "/cpu%u/online", cpu); |
| 50 |
} |
| 51 |
|
| 52 |
int |
| 53 |
get_processor_is_online (unsigned int cpu) |
| 54 |
{ |
| 55 |
bool path_exist = |
| 56 |
sysfsparser_path_exist (PATH_SYS_CPU "/cpu%u/online", cpu); |
| 57 |
|
| 58 |
if (false == path_exist) |
| 59 |
return -1; |
| 60 |
|
| 61 |
unsigned long long value; |
| 62 |
|
| 63 |
sysfsparser_getvalue (&value, PATH_SYS_CPU "/cpu%u/online", cpu); |
| 64 |
return value; |
| 65 |
} |
| 66 |
|
| 67 |
enum |
| 68 |
{ |
| 69 |
MODE_32BIT = (1 << 1), |
| 70 |
MODE_64BIT = (1 << 2) |
| 71 |
}; |
| 72 |
|
| 73 |
struct cpu_desc |
| 74 |
{ |
| 75 |
int refcount; |
| 76 |
|
| 77 |
char *arch; |
| 78 |
char *vendor; |
| 79 |
char *family; |
| 80 |
char *model; |
| 81 |
char *modelname; |
| 82 |
char *virtflag; |
| 83 |
char *mhz; |
| 84 |
char *flags; |
| 85 |
int mode; |
| 86 |
int ncpus; |
| 87 |
int ncpuspos; |
| 88 |
}; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
int |
| 93 |
cpu_desc_new (struct cpu_desc **cpudesc) |
| 94 |
{ |
| 95 |
struct cpu_desc *d; |
| 96 |
|
| 97 |
d = calloc (1, sizeof (struct cpu_desc)); |
| 98 |
if (!d) |
| 99 |
return -ENOMEM; |
| 100 |
|
| 101 |
d->refcount = 1; |
| 102 |
|
| 103 |
*cpudesc = d; |
| 104 |
return 0; |
| 105 |
} |
| 106 |
|
| 107 |
|
| 108 |
|
| 109 |
|
| 110 |
void |
| 111 |
cpu_desc_read (struct cpu_desc *cpudesc) |
| 112 |
{ |
| 113 |
char *line = NULL; |
| 114 |
FILE *fp; |
| 115 |
size_t len = 0; |
| 116 |
ssize_t chread; |
| 117 |
struct utsname utsbuf; |
| 118 |
|
| 119 |
if (cpudesc == NULL) |
| 120 |
return; |
| 121 |
|
| 122 |
if ((fp = fopen (PATH_PROC_CPUINFO, "r")) == NULL) |
| 123 |
plugin_error (STATE_UNKNOWN, errno, "error opening %s", PATH_PROC_CPUINFO); |
| 124 |
|
| 125 |
if (uname (&utsbuf) == -1) |
| 126 |
plugin_error (STATE_UNKNOWN, errno, "uname() failed"); |
| 127 |
cpudesc->arch = xstrdup (utsbuf.machine); |
| 128 |
|
| 129 |
cpudesc->ncpus = get_processor_number_total (); |
| 130 |
cpudesc->ncpuspos = get_processor_number_kernel_max (); |
| 131 |
|
| 132 |
cpudesc->mode = 0; |
| 133 |
#if defined(__alpha__) || defined(__ia64__) |
| 134 |
cpudesc->mode |= MODE_64BIT; |
| 135 |
#endif |
| 136 |
|
| 137 |
|
| 138 |
#if defined(__i386__) || defined(__x86_64__) || \ |
| 139 |
defined(__s390x__) || defined(__s390__) || defined(__sparc_v9__) |
| 140 |
cpudesc->mode |= MODE_32BIT; |
| 141 |
#endif |
| 142 |
|
| 143 |
while ((chread = getline (&line, &len, fp)) != -1) |
| 144 |
{ |
| 145 |
if (linelookup (line, "vendor", &cpudesc->vendor)); |
| 146 |
else if (linelookup (line, "vendor_id", &cpudesc->vendor)); |
| 147 |
else if (linelookup (line, "family", &cpudesc->family)); |
| 148 |
else if (linelookup (line, "cpu family", &cpudesc->family)); |
| 149 |
else if (linelookup (line, "model", &cpudesc->model)); |
| 150 |
else if (linelookup (line, "model name", &cpudesc->modelname)) ; |
| 151 |
else if (linelookup (line, "cpu MHz", &cpudesc->mhz)) ; |
| 152 |
else if (linelookup (line, "flags", &cpudesc->flags)) ; |
| 153 |
else |
| 154 |
continue; |
| 155 |
} |
| 156 |
|
| 157 |
if (cpudesc->flags) |
| 158 |
{ |
| 159 |
size_t buflen = strlen (cpudesc->flags) + 2; |
| 160 |
char *buf = xmalloc (buflen); |
| 161 |
|
| 162 |
snprintf (buf, buflen, " %s ", cpudesc->flags); |
| 163 |
if (strstr (buf, " svm ")) |
| 164 |
cpudesc->virtflag = xstrdup ("svm"); |
| 165 |
else if (strstr (buf, " vmx ")) |
| 166 |
cpudesc->virtflag = xstrdup ("vmx"); |
| 167 |
if (strstr (buf, " lm ")) |
| 168 |
cpudesc->mode |= MODE_32BIT | MODE_64BIT; |
| 169 |
if (strstr (buf, " zarch ")) |
| 170 |
cpudesc->mode |= MODE_32BIT | MODE_64BIT; |
| 171 |
if (strstr (buf, " sun4v ") || strstr (buf, " sun4u ")) |
| 172 |
cpudesc->mode |= MODE_32BIT | MODE_64BIT; |
| 173 |
|
| 174 |
free (buf); |
| 175 |
} |
| 176 |
|
| 177 |
free (line); |
| 178 |
} |
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
struct cpu_desc * |
| 183 |
cpu_desc_unref (struct cpu_desc *cpudesc) |
| 184 |
{ |
| 185 |
if (cpudesc == NULL) |
| 186 |
return NULL; |
| 187 |
|
| 188 |
cpudesc->refcount--; |
| 189 |
if (cpudesc->refcount > 0) |
| 190 |
return cpudesc; |
| 191 |
|
| 192 |
free (cpudesc); |
| 193 |
return NULL; |
| 194 |
} |
| 195 |
|
| 196 |
char * |
| 197 |
cpu_desc_get_architecture (struct cpu_desc *cpudesc) |
| 198 |
{ |
| 199 |
return cpudesc->arch; |
| 200 |
} |
| 201 |
|
| 202 |
char * |
| 203 |
cpu_desc_get_vendor (struct cpu_desc *cpudesc) |
| 204 |
{ |
| 205 |
return cpudesc->vendor; |
| 206 |
} |
| 207 |
|
| 208 |
char * |
| 209 |
cpu_desc_get_family (struct cpu_desc *cpudesc) |
| 210 |
{ |
| 211 |
return cpudesc->family; |
| 212 |
} |
| 213 |
|
| 214 |
char * |
| 215 |
cpu_desc_get_model (struct cpu_desc *cpudesc) |
| 216 |
{ |
| 217 |
return cpudesc->model; |
| 218 |
} |
| 219 |
|
| 220 |
char * |
| 221 |
cpu_desc_get_model_name (struct cpu_desc *cpudesc) |
| 222 |
{ |
| 223 |
return cpudesc->modelname; |
| 224 |
} |
| 225 |
|
| 226 |
char * |
| 227 |
cpu_desc_get_virtualization_flag (struct cpu_desc *cpudesc) |
| 228 |
{ |
| 229 |
if (cpudesc->virtflag == NULL) |
| 230 |
return NULL; |
| 231 |
|
| 232 |
if (STREQ (cpudesc->virtflag, "svm")) |
| 233 |
return "AMD-V"; |
| 234 |
else if (STREQ (cpudesc->virtflag, "vmx")) |
| 235 |
return "VT-x"; |
| 236 |
|
| 237 |
else |
| 238 |
return cpudesc->virtflag; |
| 239 |
} |
| 240 |
|
| 241 |
char * |
| 242 |
cpu_desc_get_mhz (struct cpu_desc *cpudesc) |
| 243 |
{ |
| 244 |
return cpudesc->mhz; |
| 245 |
} |
| 246 |
|
| 247 |
char * |
| 248 |
cpu_desc_get_flags (struct cpu_desc *cpudesc) |
| 249 |
{ |
| 250 |
return cpudesc->flags; |
| 251 |
} |
| 252 |
|
| 253 |
int |
| 254 |
cpu_desc_get_mode (struct cpu_desc *cpudesc) |
| 255 |
{ |
| 256 |
return cpudesc->mode; |
| 257 |
} |
| 258 |
|
| 259 |
int |
| 260 |
cpu_desc_get_ncpus (struct cpu_desc *cpudesc) |
| 261 |
{ |
| 262 |
return cpudesc->ncpus; |
| 263 |
} |
| 264 |
|
| 265 |
int |
| 266 |
cpu_desc_get_ncpuspos (struct cpu_desc *cpudesc) |
| 267 |
{ |
| 268 |
return cpudesc->ncpuspos; |
| 269 |
} |