1. Joseph Poirier
  2. cores

Commits

jpoirier  committed 6e9496b

renamed package from cores to nproc, exported function are Onln, Conf, and Max

  • Participants
  • Parent commits fec8366
  • Branches default

Comments (0)

Files changed (7)

File Makefile

View file
 include $(GOROOT)/src/Make.inc
-
-
-CGOFILES=\
-	cores.go
+
+CGOFILES=nproc.go
 
 ifeq ($(GOOS),darwin)
+OSXVER=$shell(sw_vers | awk '/ProductVersion/ {print $2}')
 CGO_CFLAGS:=-D__DARWIN__
 else ifeq ($(GOOS),freebsd)
 CGO_CFLAGS:=-D__FREEBSD__

File README

View file
 Description
 ------------
-    A Go package to programmatically determine the core count.
+    A Go package to programmatically determine the processor/core count.
 
 Compiling
 ---------
 
 Examples
 --------
-    See main.go
+    See main.go
+
+    Windows:
+        nproc.Onln()
+        nproc.Conf() --> returns nproc.Onln()
+        nproc.Max() --> returns nproc.Conf()
+
+    Darwin and FreeBSD:
+        nproc.Onln()
+        nproc.Conf()
+        nproc.Max() --> returns nproc.Conf()
+
+    Linux:
+        nproc.Onln()
+        nproc.Conf()
+        nproc.Max()
+
+Caveats
+-------
+    OS X requires version 10.3 or greater.
+    Windows version doesn't currently work, due to cgo problems?

File cores.c

-#if defined(__DARWIN__) || defined(__FREEBSD__)
-#include <sys/param.h>
-#include <sys/sysctl.h>
-#elif defined(__WINDOWS__)
-#include <windows.h>
-#elif defined(__LINUX__)
-#include <unistd.h>
-#else
-#error "Invalid GOOS: must be darwin, freebsd, linux, or windows"
-#endif
-
-// _SC_NPROCESSORS_ONLN  available starting in darwin 10.3
-// _SC_NPROCESSORS_CONF, _SC_NPROCESSORS_MAX
-
-int cnt(void) {
-#if defined(__DARWIN__) 
-	int mib[2]; uint32_t cnt; size_t len = 4;
-	mib[0] = CTL_HW; mib[1] = HW_AVAILCPU;
-	sysctl(mib, 2, &cnt, &len, NULL, 0);
-        if(cnt < 1) {
-		mib[1] = HW_NCPU;
-		sysctl(mib, 2, &cnt, &len, NULL, 0);
-		if(cnt < 1) { cnt = 1; }
-        }
-        return cnt;
-#elif  defined(__FREEBSD__)
-	int cnt; size_t len = sizeof(int);
-	if(sysctlbyname("hw.ncpu", &cnt, &len, NULL, 0) != 0) { cnt = 1; }
-	return cnt;
-#elif defined(__WINDOWS__)
-	SYSTEM_INFO sysinfo;
-	GetSystemInfo(&sysinfo);
-	return sysinfo.dwNumberOfProcessors;
-#elif defined(__LINUX__)
-	return sysconf(_SC_NPROCESSORS_ONLN);
-#endif
-}

File cores.go

-
-
-package cores
-
-/*
-#include "cores.c"
-*/
-import "C"
-
-func Cnt() int {
-	return int(C.cnt())
-}

File main.go

View file
 
 import (
 	"runtime"
-	"cores"
+	"nproc"
 	"fmt"
 )
 
 func main() {
-	cnt := cores.Cnt()
+	cnt := nproc.Onln()
 	fmt.Println("previous: ", runtime.GOMAXPROCS(cnt))
 	fmt.Println("current: ", cnt)
+	fmt.Println("Conf: ", nproc.Conf())
+	fmt.Println("Max: ", nproc.Max())
 }

File nproc.c

View file
+#if defined(__WINDOWS__)
+#include <windows.h>
+#elif defined(__LINUX__) || defined(__DARWIN__) || defined(__FREEBSD__)
+#include <unistd.h>
+#else
+#error "Invalid GOOS: must be darwin, freebsd, linux, or windows"
+#endif
+
+// _SC_NPROCESSORS_ONLN  Number of online processors (available starting in darwin 10.3)
+// _SC_NPROCESSORS_CONF  Number of configured processors
+// _SC_NPROCESSORS_MAX   Max number of processors supported by platform
+// _SC_NPROCESSES	      Number of processes per real user id
+
+int onln(void) {
+#if defined(__WINDOWS__)
+	SYSTEM_INFO sysinfo;
+	GetSystemInfo(&sysinfo);
+	return sysinfo.dwNumberOfProcessors;
+#else
+	return sysconf(_SC_NPROCESSORS_ONLN);
+#endif
+}
+
+int conf(void) {
+#if defined(__WINDOWS__)
+	return onln();
+#else
+	return sysconf(_SC_NPROCESSORS_CONF);
+#endif
+}
+
+
+int max(void) {
+#if defined(__LINUX__)
+	return sysconf(_SC_NPROCESSORS_MAX);
+#else
+	return conf();
+#endif
+}

File nproc.go

View file
+
+package nproc
+
+/*
+#include "nproc.c"
+*/
+import "C"
+
+
+// returns the number of online processors
+func Onln() int {
+	return int(C.onln())
+}
+
+// returns the number of configured processors
+func Conf() int {
+	return int(C.conf())
+}
+
+// returns the maximum number of processors supported by platform
+func Max() int {
+	return int(C.max())
+}
+