Skip to content

Commit

Permalink
i#58 MacOS: get_uname() and get_num_processors()
Browse files Browse the repository at this point in the history
Use SYS___sysctl on Mac where we use SYS_uname on Linux.
Also use SYS___sysctl to replace the sysctlbyname library call for
get_num_processors().

SVN-Revision: 2423
  • Loading branch information
derekbruening committed Dec 5, 2013
1 parent 6726ebc commit c766779
Showing 1 changed file with 31 additions and 9 deletions.
40 changes: 31 additions & 9 deletions core/unix/os.c
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
#include <limits.h>

#ifdef MACOS
# include <sys/sysctl.h> /* for sysctlbyname */
# include <sys/sysctl.h> /* for sysctl */
#endif

#ifdef LINUX
Expand Down Expand Up @@ -620,16 +620,43 @@ kernel_is_64bit(void)
return kernel_64bit;
}

#ifdef MACOS
/* XXX: if we get enough of these, move to os_macos.c or sthg */
static bool
sysctl_query(int level0, int level1, void *buf, size_t bufsz)
{
int res;
int name[2];
size_t len = bufsz;
name[0] = level0;
name[1] = level1;
res = dynamorio_syscall(SYS___sysctl, 6, &name, 2, buf, &len, NULL, 0);
return (res >= 0);
}
#endif

static void
get_uname(void)
{
/* assumption: only called at init, so we don't need any synch
* or .data unprot
*/
static struct utsname uinfo; /* can be large, avoid stack overflow */
#ifdef MACOS
if (!sysctl_query(CTL_KERN, KERN_OSTYPE, &uinfo.sysname, sizeof(uinfo.sysname)) ||
!sysctl_query(CTL_KERN, KERN_HOSTNAME, &uinfo.nodename,
sizeof(uinfo.nodename)) ||
!sysctl_query(CTL_KERN, KERN_OSRELEASE, &uinfo.release, sizeof(uinfo.release)) ||
!sysctl_query(CTL_KERN, KERN_VERSION, &uinfo.version, sizeof(uinfo.version)) ||
!sysctl_query(CTL_HW, HW_MACHINE, &uinfo.machine, sizeof(uinfo.machine))) {
ASSERT(false && "sysctl queries failed");
return;
}
#else
DEBUG_DECLARE(int res =)
dynamorio_syscall(SYS_uname, 1, (ptr_uint_t)&uinfo);
ASSERT(res >= 0);
#endif
LOG(GLOBAL, LOG_TOP, 1, "uname:\n\tsysname: %s\n", uinfo.sysname);
LOG(GLOBAL, LOG_TOP, 1, "\tnodename: %s\n", uinfo.nodename);
LOG(GLOBAL, LOG_TOP, 1, "\trelease: %s\n", uinfo.release);
Expand Down Expand Up @@ -2773,14 +2800,9 @@ get_num_processors(void)
static uint num_cpu = 0; /* cached value */
if (!num_cpu) {
#ifdef MACOS
/* FIXME i#58: use raw syscalls (and remove #include above).
* Initially could use libSystem's mach interface (host_info())
* which should be lower-level than sysctlbyname(), and replace
* that w/ its straightforward raw syscall underneath later.
*/
DEBUG_DECLARE(int res = )
sysctlbyname("hw.ncpu", NULL, NULL, &num_cpu, sizeof(num_cpu));
ASSERT(res == 0);
DEBUG_DECLARE(bool ok =)
sysctl_query(CTL_HW, HW_NCPU, &num_cpu, sizeof(num_cpu));
ASSERT(ok);
#else
/* We used to use get_nprocs_conf, but that's in libc, so now we just
* look at the /sys filesystem ourselves, which is what glibc does.
Expand Down

0 comments on commit c766779

Please sign in to comment.