Beispiel #1
0
// The ``runtime.GetCPUCount`` function tries to detect the number of CPUs on
// the current machine.
func GetCPUCount() (count int) {
	// On BSD systems, it should be possible to use ``sysctl -n hw.ncpu`` to
	// figure this out.
	if (Platform == "darwin") || (Platform == "freebsd") {
		output, err := command.GetOutput(
			[]string{"/usr/sbin/sysctl", "-n", "hw.ncpu"},
		)
		if err != nil {
			return 1
		}
		count, err = strconv.Atoi(strings.TrimSpace(output))
		if err != nil {
			return 1
		}
		// Linux systems provide introspection via ``/proc/cpuinfo``.
	} else if Platform == "linux" {
		output, err := command.GetOutput([]string{"/bin/cat", "/proc/cpuinfo"})
		if err != nil {
			return 1
		}
		for _, line := range strings.Split(output, "\n") {
			if strings.HasPrefix(line, "processor") {
				count += 1
			}
		}
	}
	// For unknown platforms, we assume that there's just a single processor.
	if count == 0 {
		return 1
	}
	return count
}
Beispiel #2
0
// Try and figure out the number of CPUs on the current machine
func GetCPUCount() (count int) {
	if (Platform == "darwin") || (Platform == "freebsd") {
		output, err := command.GetOutput([]string{"/usr/sbin/sysctl", "-n", "hw.ncpu"})
		if err != nil {
			return 1
		}
		count, err = strconv.Atoi(strings.TrimSpace(output))
		if err != nil {
			return 1
		}
	} else if Platform == "linux" {
		output, err := command.GetOutput([]string{"/bin/cat", "/proc/cpuinfo"})
		if err != nil {
			return 1
		}
		for _, line := range strings.Split(output, "\n", 0) {
			if strings.HasPrefix(line, "processor") {
				count += 1
			}
		}
	}
	if count == 0 {
		return 1
	}
	return count
}
Beispiel #3
0
func TestCPUCount(t *testing.T) {
	cpus := GetCPUCount()
	output, err := command.GetOutput(
		[]string{
			os.Getenv("AMPIFY_ROOT") + "/environ/local/bin/python",
			"-c",
			"import multiprocessing; print multiprocessing.cpu_count()"})
	if err != nil {
		t.Errorf("Couldn't call Python:\n%v", err)
		return
	}
	expected, err := strconv.Atoi(strings.TrimSpace(output))
	if err != nil {
		t.Errorf("Couldn't parse the output from Python:\n%v", err)
		return
	}
	if cpus != expected {
		t.Errorf("Got mis-matched CPU Counts: %d vs. %d", cpus, expected)
	}
}