#include <config.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <unistd.h>
#include <sys/stat.h>
#include "distcc.h"
#include "trace.h"
#include "exitcode.h"
#if defined(__hpux__) || defined(__hpux)
#include <sys/param.h>
#include <sys/pstat.h>
int dcc_ncpus(int *ncpus)
{
struct pst_dynamic psd;
if (pstat_getdynamic(&psd, sizeof(psd), 1, 0) != -1) {
*ncpus = psd.psd_proc_cnt;
return 0;
} else {
rs_log_error("pstat_getdynamic failed: %s", strerror(errno));
*ncpus = 1;
return EXIT_DISTCC_FAILED;
}
}
#elif defined(__VOS__)
#ifdef __GNUC__
#define $shortmap
#endif
#include <module_info.h>
extern void s$get_module_info (char_varying *module_name, void *mip,
short int *code);
int dcc_ncpus(int *ncpus)
{
short int code;
module_info mi;
char_varying(66) module_name;
strcpy_vstr_nstr (&module_name, "");
mi.version = MODULE_INFO_VERSION_1;
s$get_module_info ((char_varying *)&module_name, (void *)&mi, &code);
if (code != 0)
*ncpus = 1;
else *ncpus = mi.n_user_cpus;
return 0;
}
#elif defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__APPLE__) || defined(__bsdi__)
#include <sys/types.h>
#include <sys/param.h>
#include <sys/sysctl.h>
int dcc_ncpus(int *ncpus)
{
int mib[2];
size_t len = sizeof(*ncpus);
mib[0] = CTL_HW;
mib[1] = HW_NCPU;
if (sysctl(mib, 2, ncpus, &len, NULL, 0) == 0)
return 0;
else {
rs_log_error("sysctl(CTL_HW:HW_NCPU) failed: %s",
strerror(errno));
*ncpus = 1;
return EXIT_DISTCC_FAILED;
}
}
#else
int dcc_ncpus(int *ncpus)
{
#if defined(_SC_NPROCESSORS_ONLN)
*ncpus = sysconf(_SC_NPROCESSORS_ONLN);
#elif defined(_SC_NPROC_ONLN)
*ncpus = sysconf(_SC_NPROC_ONLN);
#else
#warning "Please port this function"
*ncpus = -1;
#endif
if (*ncpus == -1) {
rs_log_error("sysconf(_SC_NPROCESSORS_ONLN) failed: %s",
strerror(errno));
*ncpus = 1;
return EXIT_DISTCC_FAILED;
} else if (*ncpus == 0) {
*ncpus = 1;
}
return 0;
}
#endif
#ifdef XCODE_INTEGRATION
int dcc_cpuspeed(unsigned long long *speed)
{
#if defined(__APPLE__)
size_t len = sizeof(*speed);
if (sysctlbyname("hw.cpufrequency", speed, &len, NULL, 0) == 0)
return 0;
rs_log_error("sysctlbyname(\"hw.cpufrequency\") failed: %s",
strerror(errno));
*speed = 1;
return EXIT_DISTCC_FAILED;
#elif defined(linux)
FILE *f;
long long khz;
int rv;
f = fopen("/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq", "r");
if (!f) {
rs_log_error("open cpuinfo_max_freq failed: %s", strerror(errno));
*speed = 1;
return EXIT_DISTCC_FAILED;
}
rv = fscanf(f, "%lld", &khz);
fclose(f);
if (rv != 1 || khz <= 0) {
rs_log_error("cpuinfo_max_freq makes no sense");
*speed = 1;
return EXIT_DISTCC_FAILED;
}
*speed = khz * 1000;
return 0;
#else
#warning "Please port this function"
*speed = 1;
return EXIT_DISTCC_FAILED;
#endif
}
#endif