| 1 |
|
| #/* -*- Mode: C -*- */ |
| 2 |
|
| #/* Author: Wilson Snyder <wsnyder@wsnyder.org> */ |
| 3 |
|
| #/* IRIX & FreeBSD port by: Daniel Gustafson <daniel@hobbit.se> */ |
| 4 |
|
| #/*##################################################################### */ |
| 5 |
|
| #/* */ |
| 6 |
|
| #/* Copyright 1999-2017 by Wilson Snyder. This program is free software; */ |
| 7 |
|
| #/* you can redistribute it and/or modify it under the terms of either the GNU */ |
| 8 |
|
| #/* Lesser General Public License Version 3 or the Perl Artistic License Version 2.0. */ |
| 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 |
|
| #/*##################################################################### */ |
| 16 |
|
| |
| 17 |
|
| #include "EXTERN.h" |
| 18 |
|
| #include "perl.h" |
| 19 |
|
| #include "XSUB.h" |
| 20 |
|
| |
| 21 |
|
| #include <unistd.h> |
| 22 |
|
| #include <stdio.h> |
| 23 |
|
| #include <string.h> |
| 24 |
|
| #include <sys/types.h> |
| 25 |
|
| |
| 26 |
|
| #if defined(_AIX) |
| 27 |
|
| # define AIX |
| 28 |
|
| #endif |
| 29 |
|
| |
| 30 |
|
| #if defined(hpux) || defined(__hpux) |
| 31 |
|
| # define HPUX |
| 32 |
|
| #endif |
| 33 |
|
| |
| 34 |
|
| #if defined(__osf__) && (defined(__alpha) || defined(__alpha__)) |
| 35 |
|
| # define OSF_ALPHA |
| 36 |
|
| #endif |
| 37 |
|
| |
| 38 |
|
| #if defined(__mips) |
| 39 |
|
| # define MIPS |
| 40 |
|
| # if defined(sgi) |
| 41 |
|
| # define IRIX |
| 42 |
|
| # endif |
| 43 |
|
| #endif |
| 44 |
|
| |
| 45 |
|
| #if defined(sun) || defined(__sun__) |
| 46 |
|
| # define SUNOS |
| 47 |
|
| #endif |
| 48 |
|
| |
| 49 |
|
| #ifdef AIX |
| 50 |
|
| # ifdef HAS_PMAPI |
| 51 |
|
| # include <pmapi.h> |
| 52 |
|
| # endif |
| 53 |
|
| # ifdef HAS_PERFSTAT |
| 54 |
|
| # include <libperfstat.h> |
| 55 |
|
| # endif |
| 56 |
|
| #endif |
| 57 |
|
| |
| 58 |
|
| #ifdef HPUX |
| 59 |
|
| #include <sys/param.h> |
| 60 |
|
| #include <sys/pstat.h> |
| 61 |
|
| struct pst_dynamic psd; |
| 62 |
|
| #endif |
| 63 |
|
| |
| 64 |
|
| #ifdef OSF_ALPHA |
| 65 |
|
| #include <sys/sysinfo.h> |
| 66 |
|
| #include <machine/hal_sysinfo.h> |
| 67 |
|
| #endif |
| 68 |
|
| |
| 69 |
|
| #if defined(MIPS) && !defined(__linux__) |
| 70 |
|
| #include <sys/systeminfo.h> |
| 71 |
|
| #endif |
| 72 |
|
| |
| 73 |
|
| #ifdef IRIX |
| 74 |
|
| #include <dirent.h> |
| 75 |
|
| #include <sys/invent.h> |
| 76 |
|
| #include <sys/pda.h> |
| 77 |
|
| #include <sys/sbd.h> |
| 78 |
|
| #include <sys/sysmp.h> |
| 79 |
|
| #include <sys/iograph.h> |
| 80 |
|
| #include <invent.h> |
| 81 |
|
| #include <sys/param.h> |
| 82 |
|
| #endif |
| 83 |
|
| |
| 84 |
|
| #ifdef SUNOS |
| 85 |
|
| #include <sys/processor.h> |
| 86 |
|
| #endif |
| 87 |
|
| |
| 88 |
|
| #if defined(__FreeBSD__) || defined(__APPLE__) |
| 89 |
|
| #include <stdlib.h> |
| 90 |
|
| #include <sys/types.h> |
| 91 |
|
| #include <sys/sysctl.h> |
| 92 |
|
| #include <sys/param.h> |
| 93 |
|
| #endif |
| 94 |
|
| |
| 95 |
|
| /* Missing in older headers */ |
| 96 |
|
| #ifndef P_POWEROFF |
| 97 |
|
| #define P_POWEROFF 5 |
| 98 |
|
| #endif |
| 99 |
|
| |
| 100 |
|
| typedef int CpuNumFromRef_t; |
| 101 |
|
| |
| 102 |
|
| #/**************************************************************/ |
| 103 |
|
| |
| 104 |
|
| #ifdef __linux__ |
| 105 |
56 |
| const char* proc_cpuinfo_field (const char *field) |
| 106 |
|
| /* Return string from a field of /proc/cpuinfo, NULL if not found */ |
| 107 |
|
| /* Comparison is case insensitive */ |
| 108 |
|
| /* If multiple matches, the last match is returned */ |
| 109 |
|
| { |
| 110 |
|
| FILE* fp; |
| 111 |
|
| static char line[1000]; |
| 112 |
|
| static char result[1000]; |
| 113 |
56 |
| int len = strlen(field); |
| 114 |
56 |
| char* resultp = NULL; |
| 115 |
56 |
▶ | if (NULL!=(fp = fopen ("/proc/cpuinfo", "r"))) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (NULL!=(fp = fopen ("/proc/cpuinfo", "r"))) { |
56 |
0 |
56 |
|
| 116 |
19768 |
▶ | while (!feof(fp)) { |
Branch: 1 branch
| Branch | True | False | Total |
| while (!feof(fp)) { |
19712 |
56 |
19768 |
|
| 117 |
19712 |
| const char* ok = fgets (line, 990, fp); |
| 118 |
19712 |
▶ | if (ok && 0==strncasecmp (field, line, len)) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (ok && 0==strncasecmp (field, line, len)) { |
19656 |
56 |
19712 |
| if (ok && 0==strncasecmp (field, line, len)) { |
728 |
18928 |
19656 |
|
| 119 |
728 |
| char *loc = strchr (line, ':'); |
| 120 |
728 |
▶ | if (loc) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (loc) { |
728 |
0 |
728 |
|
| 121 |
728 |
| strncpy(result,loc+2, 990); |
| 122 |
728 |
| resultp = result; |
| 123 |
728 |
| loc = strchr (result, '\n'); |
| 124 |
728 |
▶ | if (loc) *loc = '\0'; |
Branch: 1 branch
| Branch | True | False | Total |
| if (loc) *loc = '\0'; |
728 |
0 |
728 |
|
| 125 |
|
| /*printf("MATCH: %s: %s\n", field, resultp);*/ |
| 126 |
|
| } |
| 127 |
|
| } |
| 128 |
|
| } |
| 129 |
56 |
| fclose(fp); |
| 130 |
|
| } |
| 131 |
56 |
| return (resultp); |
| 132 |
|
| } |
| 133 |
|
| |
| 134 |
1 |
| int _proc_cpuinfo_clock_calc (void) |
| 135 |
|
| { |
| 136 |
|
| const char* value; |
| 137 |
1 |
| value = proc_cpuinfo_field ("cpu MHz"); |
| 138 |
1 |
▶ | if (value) return (atoi(value)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) return (atoi(value)); |
1 |
0 |
1 |
|
| 139 |
0 |
| value = proc_cpuinfo_field ("clock"); |
| 140 |
0 |
▶ | if (value) return (atoi(value)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) return (atoi(value)); |
0 |
0 |
0 |
|
| 141 |
0 |
| value = proc_cpuinfo_field ("bogomips"); |
| 142 |
0 |
▶ | if (value) return (atoi(value)); |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) return (atoi(value)); |
0 |
0 |
0 |
|
| 143 |
0 |
| return (0); |
| 144 |
|
| } |
| 145 |
|
| |
| 146 |
14 |
| int proc_cpuinfo_clock (void) |
| 147 |
|
| /* Return clock frequency */ |
| 148 |
|
| { |
| 149 |
|
| static int cache = 0; |
| 150 |
14 |
▶ | if (!cache) cache = _proc_cpuinfo_clock_calc(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cache) cache = _proc_cpuinfo_clock_calc(); |
1 |
13 |
14 |
|
| 151 |
14 |
| return cache; |
| 152 |
|
| } |
| 153 |
|
| |
| 154 |
|
| #endif |
| 155 |
|
| |
| 156 |
1 |
| int _proc_nthreaders_calc (void) |
| 157 |
|
| { |
| 158 |
1 |
| int num_cpus = 0; |
| 159 |
|
| |
| 160 |
|
| /* Determine how many processors are online and available */ |
| 161 |
|
| #ifdef HPUX |
| 162 |
|
| if (pstat_getdynamic(&psd, sizeof(psd), (size_t)1, 0) != -1) |
| 163 |
|
| num_cpus = psd.psd_proc_cnt; |
| 164 |
|
| #endif |
| 165 |
|
| |
| 166 |
|
| #ifdef OSF_ALPHA |
| 167 |
|
| getsysinfo(GSI_CPUS_IN_BOX,&num_cpus,sizeof(num_cpus),0,0) |
| 168 |
|
| #endif |
| 169 |
|
| |
| 170 |
|
| #if defined(MIPS) && !defined(__linux__) |
| 171 |
|
| char buf[16]; |
| 172 |
|
| if (sysinfo(_MIPS_SI_NUM_PROCESSORS, buf, 10) != -1) |
| 173 |
|
| num_cpus = atoi(buf); |
| 174 |
|
| #endif |
| 175 |
|
| |
| 176 |
|
| /* Generic linux defaults */ |
| 177 |
|
| #if defined(SUNOS) || defined(AIX) || defined(__APPLE__) || defined (__linux__) |
| 178 |
1 |
▶ | if (num_cpus < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_cpus < 1) |
1 |
0 |
1 |
|
| 179 |
1 |
| num_cpus = sysconf(_SC_NPROCESSORS_ONLN); |
| 180 |
|
| # ifdef __linux__ |
| 181 |
1 |
▶ | if (num_cpus < 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_cpus < 1) { |
0 |
1 |
1 |
|
| 182 |
|
| /* SPARC Linux has a bug where SC_NPROCESSORS is set to 0. */ |
| 183 |
0 |
| const char* value = proc_cpuinfo_field("ncpus active"); |
| 184 |
0 |
▶ | if (value) num_cpus = atoi(value); |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) num_cpus = atoi(value); |
0 |
0 |
0 |
|
| 185 |
|
| } |
| 186 |
|
| # endif |
| 187 |
|
| #endif |
| 188 |
|
| |
| 189 |
|
| #if defined(__FreeBSD__) || defined(HW_NCPU) |
| 190 |
|
| size_t len = sizeof(num_cpus); |
| 191 |
|
| sysctlbyname("hw.ncpu", &num_cpus, &len, NULL, 0); |
| 192 |
|
| #endif |
| 193 |
|
| |
| 194 |
1 |
▶ | if (num_cpus < 1) |
Branch: 1 branch
| Branch | True | False | Total |
| if (num_cpus < 1) |
0 |
1 |
1 |
|
| 195 |
0 |
| num_cpus=1; /* We're running this program, after all :-) */ |
| 196 |
1 |
| return (num_cpus); |
| 197 |
|
| } |
| 198 |
|
| |
| 199 |
156 |
| int proc_nthreaders (void) |
| 200 |
|
| /* Return number of processor threads */ |
| 201 |
|
| { |
| 202 |
|
| static int cache = 0; |
| 203 |
156 |
▶ | if (!cache) cache = _proc_nthreaders_calc(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cache) cache = _proc_nthreaders_calc(); |
1 |
155 |
156 |
|
| 204 |
156 |
| return cache; |
| 205 |
|
| } |
| 206 |
|
| |
| 207 |
|
| #ifdef IRIX |
| 208 |
|
| /* invent_cpuinfo_t irix_get_cpuinf(int cpuid); |
| 209 |
|
| * Returns an invent_cpuinfo_t regarding the requested cpuid. */ |
| 210 |
|
| invent_cpuinfo_t irix_get_cpuinf(int cpuid) { |
| 211 |
|
| union { |
| 212 |
|
| invent_generic_t generic; |
| 213 |
|
| invent_cpuinfo_t cpu; |
| 214 |
|
| } hw_inv; |
| 215 |
|
| |
| 216 |
|
| int attr_len = sizeof(hw_inv); |
| 217 |
|
| DIR *hw_graph; |
| 218 |
|
| struct dirent *hw_entry; |
| 219 |
|
| char *hw_entry_buf = (char *)malloc(MAXPATHLEN); |
| 220 |
|
| char *hw_filename = (char *)malloc(MAXPATHLEN); |
| 221 |
|
| |
| 222 |
|
| if ((hw_graph = opendir("/hw/cpunum")) != NULL) { |
| 223 |
|
| while ((hw_entry = readdir(hw_graph)) != NULL) { |
| 224 |
|
| |
| 225 |
|
| if ((strcmp(hw_entry->d_name, ".") != 0) && (strcmp(hw_entry->d_name, "..") != 0)) { |
| 226 |
|
| strcpy(hw_filename, "/hw/cpunum/"); |
| 227 |
|
| strncat(hw_filename, hw_entry->d_name, 1); |
| 228 |
|
| |
| 229 |
|
| if (realpath(hw_filename, hw_entry_buf) != NULL) { |
| 230 |
|
| if (attr_get(hw_entry_buf, INFO_LBL_DETAIL_INVENT, (char *)&hw_inv, &attr_len, 0) == 0) { |
| 231 |
|
| if (hw_inv.generic.ig_invclass == INV_PROCESSOR) { |
| 232 |
|
| if (hw_inv.cpu.ic_cpuid == cpuid) { |
| 233 |
|
| break; |
| 234 |
|
| } |
| 235 |
|
| } |
| 236 |
|
| } |
| 237 |
|
| } |
| 238 |
|
| } |
| 239 |
|
| } |
| 240 |
|
| } |
| 241 |
|
| |
| 242 |
|
| closedir(hw_graph); |
| 243 |
|
| return(hw_inv.cpu); |
| 244 |
|
| } |
| 245 |
|
| #endif |
| 246 |
|
| |
| 247 |
0 |
| int logical_per_physical_cpu() { |
| 248 |
0 |
| int logical_per = 1; |
| 249 |
|
| |
| 250 |
|
| #ifdef __linux__ |
| 251 |
0 |
| const char* flags = proc_cpuinfo_field ("flags"); |
| 252 |
|
| /* flags: ... ht ... indicates hyperthreading enabled on a cpu */ |
| 253 |
0 |
▶ | if (flags && strstr (flags, " ht ")) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (flags && strstr (flags, " ht ")) { |
0 |
0 |
0 |
| if (flags && strstr (flags, " ht ")) { |
0 |
0 |
0 |
|
| 254 |
|
| /* HACK: Current linux under hyperthreading always makes 2 logical CPUs per physical CPU */ |
| 255 |
0 |
| logical_per = 2; |
| 256 |
|
| } |
| 257 |
|
| #endif |
| 258 |
|
| #ifdef __FreeBSD__ |
| 259 |
|
| int hlt_htt_cpu = 0; |
| 260 |
|
| size_t len = sizeof(hlt_htt_cpu); |
| 261 |
|
| if (sysctlbyname("machdep.hlt_logical_cpus", |
| 262 |
|
| &hlt_htt_cpu, &len, NULL, 0) == 0) { |
| 263 |
|
| if (hlt_htt_cpu == 0) { |
| 264 |
|
| /* HACK: Current FreeBSD under hyperthreading always makes 2 logical CPUs per physical CPU */ |
| 265 |
|
| logical_per = 2; |
| 266 |
|
| } |
| 267 |
|
| } |
| 268 |
|
| #endif |
| 269 |
|
| |
| 270 |
0 |
| return logical_per; |
| 271 |
|
| } |
| 272 |
|
| |
| 273 |
2 |
| int proc_num_physical_ids() { |
| 274 |
|
| #ifdef __linux__ |
| 275 |
|
| { |
| 276 |
2 |
| const char* value = proc_cpuinfo_field ("physical id"); |
| 277 |
2 |
▶ | if (value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) { |
2 |
0 |
2 |
|
| 278 |
2 |
| int phys = atoi(value)+1; |
| 279 |
2 |
| return phys; |
| 280 |
|
| } |
| 281 |
|
| } |
| 282 |
|
| #endif |
| 283 |
0 |
| return 0; |
| 284 |
|
| } |
| 285 |
1 |
| int _proc_ncores_calc() { |
| 286 |
|
| /* Can't have more cores than threaders */ |
| 287 |
1 |
| int num = proc_nthreaders(); |
| 288 |
|
| #ifdef __linux__ |
| 289 |
|
| { |
| 290 |
1 |
| int phys = proc_num_physical_ids(); |
| 291 |
1 |
| const char* cvalue = proc_cpuinfo_field ("cpu cores"); |
| 292 |
1 |
▶ | if (phys && cvalue) { |
Branch: 2 branches
| Branch | True | False | Total |
| if (phys && cvalue) { |
1 |
0 |
1 |
| if (phys && cvalue) { |
1 |
0 |
1 |
|
| 293 |
1 |
| int cores_per_phys = atoi(cvalue); |
| 294 |
1 |
| int cores = phys * cores_per_phys; |
| 295 |
1 |
▶ | if (cores && (cores <= num)) return cores; |
Branch: 2 branches
| Branch | True | False | Total |
| if (cores && (cores <= num)) return cores; |
1 |
0 |
1 |
| if (cores && (cores <= num)) return cores; |
1 |
0 |
1 |
|
| 296 |
|
| } |
| 297 |
|
| } |
| 298 |
|
| #endif |
| 299 |
|
| { |
| 300 |
|
| /* If hyperthreading, the threader count is too high */ |
| 301 |
0 |
▶ | if (num > 1) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (num > 1) { |
0 |
0 |
0 |
|
| 302 |
0 |
| num /= logical_per_physical_cpu(); |
| 303 |
|
| } |
| 304 |
0 |
| return num; |
| 305 |
|
| } |
| 306 |
|
| } |
| 307 |
|
| |
| 308 |
2 |
| int proc_ncores() { |
| 309 |
|
| /* Return number of processor cores */ |
| 310 |
|
| static int cache = 0; |
| 311 |
2 |
▶ | if (!cache) cache = _proc_ncores_calc(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cache) cache = _proc_ncores_calc(); |
1 |
1 |
2 |
|
| 312 |
2 |
| return cache; |
| 313 |
|
| } |
| 314 |
|
| |
| 315 |
1 |
| int _proc_nsockets_calc() { |
| 316 |
1 |
| int num = proc_ncores(); |
| 317 |
|
| /* default to core count, trim downwards if we can */ |
| 318 |
1 |
| int phys = proc_num_physical_ids(); |
| 319 |
1 |
▶ | if (phys && ((phys <= num))) num = phys; |
Branch: 2 branches
| Branch | True | False | Total |
| if (phys && ((phys <= num))) num = phys; |
1 |
0 |
1 |
| if (phys && ((phys <= num))) num = phys; |
1 |
0 |
1 |
|
| 320 |
1 |
| return num; |
| 321 |
|
| } |
| 322 |
|
| |
| 323 |
1 |
| int proc_nsockets() { |
| 324 |
|
| /* Return number of processor sockets */ |
| 325 |
|
| static int cache = 0; |
| 326 |
1 |
▶ | if (!cache) cache = _proc_nsockets_calc(); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!cache) cache = _proc_nsockets_calc(); |
1 |
0 |
1 |
|
| 327 |
1 |
| return cache; |
| 328 |
|
| } |
| 329 |
|
| |
| 330 |
|
| |
| 331 |
|
| MODULE = Unix::Processors PACKAGE = Unix::Processors |
| 332 |
|
| |
| 333 |
|
| #/**********************************************************************/ |
| 334 |
|
| #/* class->max_online() */ |
| 335 |
|
| #/* Self is a argument, but we don't need it */ |
| 336 |
|
| #/* We use sysconf, as that is more portable */ |
| 337 |
|
| #/* Other packages also provide sysconf, but saves downloading them... */ |
| 338 |
|
| |
| 339 |
|
| long |
| 340 |
|
| max_online(self) |
| 341 |
|
| SV *self; |
| 342 |
|
| CODE: |
| 343 |
|
| { |
| 344 |
|
| if (self) {} /* Prevent unused warning */ |
| 345 |
1 |
▶ | RETVAL = proc_nthreaders(); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = proc_nthreaders(); |
0 |
1 |
1 |
|
| 346 |
|
| } |
| 347 |
|
| OUTPUT: RETVAL |
| 348 |
|
| |
| 349 |
|
| #/**********************************************************************/ |
| 350 |
|
| #/* class->max_physical() */ |
| 351 |
|
| #/* Self is a argument, but we don't need it */ |
| 352 |
|
| |
| 353 |
|
| long |
| 354 |
|
| max_physical(self) |
| 355 |
|
| SV *self; |
| 356 |
|
| CODE: |
| 357 |
|
| { |
| 358 |
|
| if (self) {} /* Prevent unused warning */ |
| 359 |
1 |
▶ | RETVAL = proc_ncores(); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = proc_ncores(); |
0 |
1 |
1 |
|
| 360 |
|
| } |
| 361 |
|
| OUTPUT: RETVAL |
| 362 |
|
| |
| 363 |
|
| #/**********************************************************************/ |
| 364 |
|
| #/* class->max_socket() */ |
| 365 |
|
| #/* Self is a argument, but we don't need it */ |
| 366 |
|
| |
| 367 |
|
| long |
| 368 |
|
| max_socket(self) |
| 369 |
|
| SV *self; |
| 370 |
|
| CODE: |
| 371 |
|
| { |
| 372 |
|
| if (self) {} /* Prevent unused warning */ |
| 373 |
1 |
▶ | RETVAL = proc_nsockets(); |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = proc_nsockets(); |
0 |
1 |
1 |
|
| 374 |
|
| } |
| 375 |
|
| OUTPUT: RETVAL |
| 376 |
|
| |
| 377 |
|
| #/**********************************************************************/ |
| 378 |
|
| #/* class->max_clock() */ |
| 379 |
|
| #/* Self is a argument, but we don't need it */ |
| 380 |
|
| |
| 381 |
|
| int |
| 382 |
|
| max_clock(self) |
| 383 |
|
| SV *self; |
| 384 |
|
| CODE: |
| 385 |
|
| { |
| 386 |
1 |
| int clock = 0; |
| 387 |
|
| #ifdef AIX |
| 388 |
|
| # if defined(HAS_PERFSTAT) |
| 389 |
|
| perfstat_cpu_total_t data; |
| 390 |
|
| if (perfstat_cpu_total (0, &data, sizeof(data), 1)) { |
| 391 |
|
| clock = data.processorHZ / 1000000; |
| 392 |
|
| } |
| 393 |
|
| # elif defined(HAS_PMAPI) |
| 394 |
|
| /* pm_cycles uses an approximation to arrive at cycle time |
| 395 |
|
| * so we round up to the nearest Mhz */ |
| 396 |
|
| clock = (int)((pm_cycles() + 500000) / 1000000); |
| 397 |
|
| # endif |
| 398 |
|
| #endif |
| 399 |
|
| #ifdef HPUX |
| 400 |
|
| /* all processors have the same clock on HP - just report the first one */ |
| 401 |
|
| struct pst_processor psp; |
| 402 |
|
| if (pstat_getprocessor(&psp, sizeof(psp), 1, 0)) { |
| 403 |
|
| clock = psp.psp_iticksperclktick / 10000; |
| 404 |
|
| } |
| 405 |
|
| #endif |
| 406 |
|
| #ifdef SUNOS |
| 407 |
|
| int cpu; |
| 408 |
|
| int last_cpu = 0; |
| 409 |
|
| processor_info_t info, *infop=&info; |
| 410 |
|
| for (cpu=0; cpu < last_cpu+16; cpu++) { |
| 411 |
|
| if (processor_info (cpu, infop)==0 |
| 412 |
|
| && infop->pi_state == P_ONLINE) { |
| 413 |
|
| if (clock < infop->pi_clock) { |
| 414 |
|
| clock = infop->pi_clock; |
| 415 |
|
| } |
| 416 |
|
| last_cpu = cpu; |
| 417 |
|
| } |
| 418 |
|
| } |
| 419 |
|
| #endif |
| 420 |
|
| #ifdef IRIX |
| 421 |
|
| int num_cpus = proc_nthreaders(); |
| 422 |
|
| if ((num_cpus > 0) && (num_cpus < 3)) { |
| 423 |
|
| inventory_t *sys_invent; |
| 424 |
|
| if (setinvent() != -1) { |
| 425 |
|
| for (sys_invent = getinvent(); (sys_invent); sys_invent = getinvent()) { |
| 426 |
|
| if ((sys_invent->inv_class == INV_PROCESSOR) && (sys_invent->inv_type == INV_CPUBOARD)) { |
| 427 |
|
| clock = sys_invent->inv_controller; |
| 428 |
|
| break; |
| 429 |
|
| } |
| 430 |
|
| } |
| 431 |
|
| endinvent(); |
| 432 |
|
| } |
| 433 |
|
| } |
| 434 |
|
| else { |
| 435 |
|
| invent_cpuinfo_t cpu_info; |
| 436 |
|
| int i; |
| 437 |
|
| for (i = 0; i < proc_nthreaders(); i++) { |
| 438 |
|
| cpu_info = irix_get_cpuinf(i); |
| 439 |
|
| if (cpu_info.ic_cpuid == i) |
| 440 |
|
| if (cpu_info.ic_cpu_info.cpufq > clock) |
| 441 |
|
| clock = cpu_info.ic_cpu_info.cpufq; |
| 442 |
|
| } |
| 443 |
|
| } |
| 444 |
|
| #endif |
| 445 |
|
| #if (defined(__FreeBSD__) && (__FreeBSD_version >= 503105)) |
| 446 |
|
| int value = 0; |
| 447 |
|
| size_t len = sizeof(value); |
| 448 |
|
| /* |
| 449 |
|
| * Even if the frequency is modified using cpu_freq(3), all cpus |
| 450 |
|
| * have the same value why we can request CPU 0 for max_clock. |
| 451 |
|
| */ |
| 452 |
|
| if (sysctlbyname("dev.cpu.0.freq", &value, &len, NULL, 0) == 0) { |
| 453 |
|
| clock = value; |
| 454 |
|
| } |
| 455 |
|
| #elif defined(HW_CPU_FREQ) |
| 456 |
|
| long long value = 0; |
| 457 |
|
| size_t len = sizeof(value); |
| 458 |
|
| if (sysctlbyname("hw.cpufrequency", &value, &len, NULL, 0) == 0) { |
| 459 |
|
| clock = value; |
| 460 |
|
| } |
| 461 |
|
| #endif |
| 462 |
|
| #ifdef __linux__ |
| 463 |
1 |
| int value = proc_cpuinfo_clock(); |
| 464 |
1 |
▶ | if (value) clock = value; |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) clock = value; |
1 |
0 |
1 |
|
| 465 |
|
| #endif |
| 466 |
|
| |
| 467 |
|
| if (self) {} /* Prevent unused warning */ |
| 468 |
1 |
▶ | RETVAL = clock; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = clock; |
0 |
1 |
1 |
|
| 469 |
|
| } |
| 470 |
|
| OUTPUT: RETVAL |
| 471 |
|
| |
| 472 |
|
| #/**********************************************************************/ |
| 473 |
|
| #/**********************************************************************/ |
| 474 |
|
| #/**********************************************************************/ |
| 475 |
|
| #/**********************************************************************/ |
| 476 |
|
| #/**********************************************************************/ |
| 477 |
|
| |
| 478 |
|
| MODULE = Unix::Processors PACKAGE = Unix::Processors::Info |
| 479 |
|
| |
| 480 |
|
| #/**********************************************************************/ |
| 481 |
|
| #/* class->id() */ |
| 482 |
|
| |
| 483 |
|
| int |
| 484 |
|
| id (cpu) |
| 485 |
|
| CpuNumFromRef_t cpu |
| 486 |
|
| PROTOTYPE: $ |
| 487 |
|
| CODE: |
| 488 |
|
| { |
| 489 |
13 |
▶ | RETVAL = cpu; |
Branch: 1 branch
| Branch | True | False | Total |
| RETVAL = cpu; |
0 |
13 |
13 |
|
| 490 |
|
| } |
| 491 |
|
| OUTPUT: RETVAL |
| 492 |
|
| |
| 493 |
|
| #/**********************************************************************/ |
| 494 |
|
| #/* class->clock() */ |
| 495 |
|
| |
| 496 |
|
| SV * |
| 497 |
|
| clock (cpu) |
| 498 |
|
| CpuNumFromRef_t cpu |
| 499 |
|
| PROTOTYPE: $ |
| 500 |
|
| CODE: |
| 501 |
|
| { |
| 502 |
13 |
| int value = 0; |
| 503 |
|
| #ifdef AIX |
| 504 |
|
| int num_cpus = proc_nthreaders(); |
| 505 |
|
| if (cpu < num_cpus) { |
| 506 |
|
| # if defined(HAS_PERFSTAT) |
| 507 |
|
| perfstat_cpu_total_t data; |
| 508 |
|
| if (perfstat_cpu_total (0, &data, sizeof(data), 1)) { |
| 509 |
|
| value = data.processorHZ / 1000000; |
| 510 |
|
| } |
| 511 |
|
| # elif defined(HAS_PMAPI) |
| 512 |
|
| /* pm_cycles uses an approximation to arrive at cycle time |
| 513 |
|
| * so we round up to the nearest Mhz */ |
| 514 |
|
| clock = (int)((pm_cycles() + 500000) / 1000000); |
| 515 |
|
| # endif |
| 516 |
|
| } |
| 517 |
|
| #endif |
| 518 |
|
| #ifdef HPUX |
| 519 |
|
| int num_cpus = proc_nthreaders(); |
| 520 |
|
| if (cpu < num_cpus) { |
| 521 |
|
| /* all processors have the same clock on HP - just report the first one */ |
| 522 |
|
| struct pst_processor psp; |
| 523 |
|
| if (pstat_getprocessor(&psp, sizeof(psp), 1, 0)) { |
| 524 |
|
| value = psp.psp_iticksperclktick / 10000; |
| 525 |
|
| } |
| 526 |
|
| } |
| 527 |
|
| #endif |
| 528 |
|
| #ifdef SUNOS |
| 529 |
|
| processor_info_t info, *infop=&info; |
| 530 |
|
| if (processor_info (cpu, infop)==0) { |
| 531 |
|
| value = infop->pi_clock; |
| 532 |
|
| } |
| 533 |
|
| #endif |
| 534 |
|
| #ifdef IRIX |
| 535 |
|
| int num_cpus = proc_nthreaders(); |
| 536 |
|
| if ((num_cpus > 0) && (num_cpus < 3)) { |
| 537 |
|
| inventory_t *sys_invent; |
| 538 |
|
| if (setinvent() != -1) { |
| 539 |
|
| while ((sys_invent = getinvent()) != NULL) { |
| 540 |
|
| if ((sys_invent->inv_class == INV_PROCESSOR) && (sys_invent->inv_type == INV_CPUBOARD)) { |
| 541 |
|
| value = sys_invent->inv_controller; |
| 542 |
|
| break; |
| 543 |
|
| } |
| 544 |
|
| } |
| 545 |
|
| endinvent(); |
| 546 |
|
| } |
| 547 |
|
| } |
| 548 |
|
| else { |
| 549 |
|
| invent_cpuinfo_t cpu_info; |
| 550 |
|
| cpu_info = irix_get_cpuinf(cpu); |
| 551 |
|
| if (cpu_info.ic_cpuid == cpu) |
| 552 |
|
| value = cpu_info.ic_cpu_info.cpufq; |
| 553 |
|
| } |
| 554 |
|
| #endif |
| 555 |
|
| #if (defined(__FreeBSD__) && (__FreeBSD_version >= 503105)) |
| 556 |
|
| int cpu_freq = 0; |
| 557 |
|
| size_t len = sizeof(cpu_freq); |
| 558 |
|
| char cpu_freq_req[16]; |
| 559 |
|
| snprintf(cpu_freq_req, 16, "dev.cpu.%d.freq", cpu); |
| 560 |
|
| if (sysctlbyname(cpu_freq_req, &cpu_freq, &len, NULL, 0) == 0) { |
| 561 |
|
| value = cpu_freq; |
| 562 |
|
| } |
| 563 |
|
| #endif |
| 564 |
|
| #ifdef __linux__ |
| 565 |
|
| /* Cheat... Same clock for every CPU */ |
| 566 |
13 |
| value = proc_cpuinfo_clock(); |
| 567 |
|
| #endif |
| 568 |
13 |
▶ | if (value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) { |
13 |
0 |
13 |
|
| 569 |
13 |
| ST(0) = sv_newmortal(); |
| 570 |
13 |
| sv_setiv (ST(0), value); |
| 571 |
|
| } else { |
| 572 |
0 |
| ST(0) = &PL_sv_undef; |
| 573 |
|
| } |
| 574 |
|
| } |
| 575 |
|
| |
| 576 |
|
| #/**********************************************************************/ |
| 577 |
|
| #/* class->state() */ |
| 578 |
|
| |
| 579 |
|
| SV * |
| 580 |
|
| state (cpu) |
| 581 |
|
| CpuNumFromRef_t cpu |
| 582 |
|
| PROTOTYPE: $ |
| 583 |
|
| CODE: |
| 584 |
|
| { |
| 585 |
26 |
| char *value = NULL; |
| 586 |
|
| #ifdef SUNOS |
| 587 |
|
| processor_info_t info, *infop=&info; |
| 588 |
|
| if (processor_info (cpu, infop)==0) { |
| 589 |
|
| switch (infop->pi_state) { |
| 590 |
|
| case P_ONLINE: |
| 591 |
|
| value = "online"; |
| 592 |
|
| break; |
| 593 |
|
| case P_OFFLINE: |
| 594 |
|
| value = "offline"; |
| 595 |
|
| break; |
| 596 |
|
| case P_POWEROFF: |
| 597 |
|
| value = "poweroff"; |
| 598 |
|
| break; |
| 599 |
|
| } |
| 600 |
|
| } |
| 601 |
|
| #endif |
| 602 |
|
| #ifdef IRIX |
| 603 |
|
| int num_cpus; |
| 604 |
|
| if ((num_cpus = sysmp(MP_NPROCS)) != -1) { |
| 605 |
|
| struct pda_stat proc_info[num_cpus]; |
| 606 |
|
| if (sysmp(MP_STAT, proc_info) != -1) { |
| 607 |
|
| if (proc_info[cpu].p_flags == PDAF_MASTER) |
| 608 |
|
| value = "MASTER"; |
| 609 |
|
| else if (proc_info[cpu].p_flags == PDAF_CLOCK) |
| 610 |
|
| value = "CLOCK"; |
| 611 |
|
| else if (proc_info[cpu].p_flags == PDAF_ENABLED) |
| 612 |
|
| value = "ENABLED"; |
| 613 |
|
| else if (proc_info[cpu].p_flags == PDAF_FASTCLOCK) |
| 614 |
|
| value = "FASTCLOCK"; |
| 615 |
|
| else if (proc_info[cpu].p_flags == PDAF_ISOLATED) |
| 616 |
|
| value = "ISOLATED"; |
| 617 |
|
| else if (proc_info[cpu].p_flags == PDAF_BROADCAST_OFF) |
| 618 |
|
| value = "BROADCAST_OFF"; |
| 619 |
|
| else if (proc_info[cpu].p_flags == PDAF_NONPREEMPTIVE) |
| 620 |
|
| value = "NONPREEMPTIVE"; |
| 621 |
|
| else if (proc_info[cpu].p_flags == PDAF_NOINTR) |
| 622 |
|
| value = "NOINTR"; |
| 623 |
|
| else if (proc_info[cpu].p_flags == PDAF_ITHREADSOK) |
| 624 |
|
| value = "ITHREADSOK"; |
| 625 |
|
| else if (proc_info[cpu].p_flags == PDAF_DISABLE_CPU) |
| 626 |
|
| value = "DISABLE_CPU"; |
| 627 |
|
| else if (proc_info[cpu].p_flags == PDAF_EXCLUDED) |
| 628 |
|
| value = "EXCLUDED"; |
| 629 |
|
| else |
| 630 |
|
| /* |
| 631 |
|
| * No p_flags value is specified for uniprocessor |
| 632 |
|
| * systems. Return ONLINE. |
| 633 |
|
| */ |
| 634 |
|
| value = "ONLINE"; |
| 635 |
|
| } |
| 636 |
|
| } |
| 637 |
|
| #endif |
| 638 |
|
| /* Cheat... Assume all online */ |
| 639 |
26 |
▶ | if (value == NULL) |
Branch: 1 branch
| Branch | True | False | Total |
| if (value == NULL) |
26 |
0 |
26 |
|
| 640 |
26 |
| value = "online"; |
| 641 |
|
| /* Return it */ |
| 642 |
26 |
▶ | if (value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) { |
26 |
0 |
26 |
|
| 643 |
26 |
| ST(0) = sv_newmortal(); |
| 644 |
26 |
| sv_setpv (ST(0), value); |
| 645 |
|
| } else { |
| 646 |
0 |
| ST(0) = &PL_sv_undef; |
| 647 |
|
| } |
| 648 |
|
| } |
| 649 |
|
| |
| 650 |
|
| #/**********************************************************************/ |
| 651 |
|
| #/* class->type() */ |
| 652 |
|
| |
| 653 |
|
| SV * |
| 654 |
|
| type (cpu) |
| 655 |
|
| CpuNumFromRef_t cpu |
| 656 |
|
| PROTOTYPE: $ |
| 657 |
|
| CODE: |
| 658 |
|
| { |
| 659 |
154 |
| const char* value = NULL; |
| 660 |
|
| #ifdef AIX |
| 661 |
|
| # if defined(HAS_PERFSTAT) |
| 662 |
|
| int num_cpus = proc_nthreaders(); |
| 663 |
|
| if (cpu < num_cpus) { |
| 664 |
|
| perfstat_cpu_total_t data; |
| 665 |
|
| if (perfstat_cpu_total (0, &data, sizeof(data), 1)) { |
| 666 |
|
| value = data.description; |
| 667 |
|
| } |
| 668 |
|
| } |
| 669 |
|
| # endif |
| 670 |
|
| #endif |
| 671 |
|
| #ifdef HPUX |
| 672 |
|
| int num_cpus = proc_nthreaders(); |
| 673 |
|
| if (cpu < num_cpus) { |
| 674 |
|
| switch(sysconf(_SC_CPU_VERSION)) { |
| 675 |
|
| case CPU_PA_RISC1_0: |
| 676 |
|
| value = "HP PA-RISC 1.0"; |
| 677 |
|
| break; |
| 678 |
|
| case CPU_PA_RISC1_1: |
| 679 |
|
| value = "HP PA-RISC 1.1"; |
| 680 |
|
| break; |
| 681 |
|
| case CPU_PA_RISC1_2: |
| 682 |
|
| value = "HP PA-RISC 1.2"; |
| 683 |
|
| break; |
| 684 |
|
| case CPU_PA_RISC2_0: |
| 685 |
|
| value = "HP PA-RISC 2.0"; |
| 686 |
|
| break; |
| 687 |
|
| } |
| 688 |
|
| } |
| 689 |
|
| #endif |
| 690 |
|
| #ifdef SUNOS |
| 691 |
|
| processor_info_t info, *infop=&info; |
| 692 |
|
| if (processor_info (cpu, infop)==0) { |
| 693 |
|
| value = infop->pi_processor_type; |
| 694 |
|
| } |
| 695 |
|
| #endif |
| 696 |
|
| #ifdef __linux__ |
| 697 |
154 |
| int ncpu = proc_nthreaders(); |
| 698 |
154 |
▶ | if (cpu < ncpu) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (cpu < ncpu) { |
52 |
102 |
154 |
|
| 699 |
52 |
| value = proc_cpuinfo_field ("model name"); |
| 700 |
52 |
▶ | if (!value) value = proc_cpuinfo_field ("machine"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!value) value = proc_cpuinfo_field ("machine"); |
0 |
52 |
52 |
|
| 701 |
52 |
▶ | if (!value) value = proc_cpuinfo_field ("family"); |
Branch: 1 branch
| Branch | True | False | Total |
| if (!value) value = proc_cpuinfo_field ("family"); |
0 |
52 |
52 |
|
| 702 |
|
| } |
| 703 |
|
| #endif |
| 704 |
|
| #if defined(MIPS) && !defined(__linux__) |
| 705 |
|
| if (cpu < proc_nthreaders()) { |
| 706 |
|
| if ((value = (char *)malloc(64)) != NULL) { |
| 707 |
|
| sysinfo(SI_MACHINE, value, 64); |
| 708 |
|
| } |
| 709 |
|
| } |
| 710 |
|
| #endif |
| 711 |
|
| #ifdef IRIX |
| 712 |
|
| if (cpu < proc_nthreaders()) { |
| 713 |
|
| int cpu_data = 0; |
| 714 |
|
| int num_cpus = proc_nthreaders(); |
| 715 |
|
| if ((num_cpus > 0) && (num_cpus < 3)) { |
| 716 |
|
| inventory_t *sys_invent; |
| 717 |
|
| if (setinvent() != -1) { |
| 718 |
|
| while ((sys_invent = getinvent()) != NULL) { |
| 719 |
|
| if ((sys_invent->inv_class == INV_PROCESSOR) && (sys_invent->inv_type == INV_CPUCHIP)) { |
| 720 |
|
| cpu_data = sys_invent->inv_state; |
| 721 |
|
| break; |
| 722 |
|
| } |
| 723 |
|
| } |
| 724 |
|
| endinvent(); |
| 725 |
|
| } |
| 726 |
|
| } |
| 727 |
|
| else { |
| 728 |
|
| invent_cpuinfo_t cpu_info; |
| 729 |
|
| cpu_info = irix_get_cpuinf(cpu); |
| 730 |
|
| if (cpu_info.ic_cpuid == cpu) |
| 731 |
|
| cpu_data = cpu_info.ic_cpu_info.cpuflavor; |
| 732 |
|
| } |
| 733 |
|
| if (cpu_data != 0) { |
| 734 |
|
| if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R2000A) |
| 735 |
|
| strcat(value, " MIPS R2000A"); |
| 736 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R2000) |
| 737 |
|
| strcat(value, " MIPS R2000A"); |
| 738 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R3000A) |
| 739 |
|
| strcat(value, " MIPS R3000A"); |
| 740 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R3000) |
| 741 |
|
| strcat(value, " MIPS R3000"); |
| 742 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R4000) { |
| 743 |
|
| if (((cpu_data&C0_MAJREVMASK)>>C0_MAJREVSHIFT) >= C0_MAJREVMIN_R4400) |
| 744 |
|
| strcat(value, " MIPS R4400"); |
| 745 |
|
| else |
| 746 |
|
| strcat(value, " MIPS R4000"); |
| 747 |
|
| } |
| 748 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R4650) |
| 749 |
|
| strcat(value, " MIPS R4650"); |
| 750 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R4700) |
| 751 |
|
| strcat(value, " MIPS R4700"); |
| 752 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R4600) |
| 753 |
|
| strcat(value, " MIPS R4600"); |
| 754 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R5000) |
| 755 |
|
| strcat(value, " MIPS R5000"); |
| 756 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_RM5271) |
| 757 |
|
| strcat(value, " MIPS RM5271"); |
| 758 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R6000A) |
| 759 |
|
| strcat(value, " MIPS R6000A"); |
| 760 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R6000) |
| 761 |
|
| strcat(value, " MIPS R6000"); |
| 762 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_RM7000) |
| 763 |
|
| strcat(value, " MIPS RM7000"); |
| 764 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R8000) |
| 765 |
|
| strcat(value, " MIPS R8000"); |
| 766 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R10000) |
| 767 |
|
| strcat(value, " MIPS R10000"); |
| 768 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R12000) |
| 769 |
|
| strcat(value, " MIPS R12000"); |
| 770 |
|
| else if ((cpu_data >> C0_IMPSHIFT) == C0_IMP_R14000) |
| 771 |
|
| strcat(value, " MIPS R14000"); |
| 772 |
|
| else |
| 773 |
|
| strcat(value, " Undefined MIPS"); |
| 774 |
|
| sprintf(value, "%s Chip Rev: %x.%x", value, ((cpu_data&C0_MAJREVMASK)>>C0_MAJREVSHIFT), ((cpu_data&C0_MINREVMASK)>>C0_MINREVSHIFT)); |
| 775 |
|
| } |
| 776 |
|
| } |
| 777 |
|
| #endif |
| 778 |
|
| #ifdef __FreeBSD__ |
| 779 |
|
| if (cpu < proc_nthreaders()) { |
| 780 |
|
| if ((value = (char *)malloc(64)) != NULL) { |
| 781 |
|
| size_t len = 64; |
| 782 |
|
| sysctlbyname("hw.machine_arch", value, &len, NULL, 0); |
| 783 |
|
| } |
| 784 |
|
| } |
| 785 |
|
| #endif |
| 786 |
|
| |
| 787 |
154 |
▶ | if (value) { |
Branch: 1 branch
| Branch | True | False | Total |
| if (value) { |
52 |
102 |
154 |
|
| 788 |
52 |
| ST(0) = sv_newmortal(); |
| 789 |
52 |
| sv_setpv (ST(0), value); |
| 790 |
|
| } else { |
| 791 |
102 |
| ST(0) = &PL_sv_undef; |
| 792 |
|
| } |
| 793 |
|
| } |