Skip to content

Commit

Permalink
Add cpu_count to System module (#4449)
Browse files Browse the repository at this point in the history
Signed-off-by: Mike Fiedler <miketheman@gmail.com>
  • Loading branch information
miketheman authored and Martin Verzilli committed May 24, 2017
1 parent 5aa3b15 commit 0e73cf4
Show file tree
Hide file tree
Showing 11 changed files with 71 additions and 45 deletions.
8 changes: 8 additions & 0 deletions spec/std/system_spec.cr
Expand Up @@ -10,4 +10,12 @@ describe System do
hostname.should eq(shell_hostname)
end
end

describe "cpu_count" do
it "returns current CPU count" do
shell_cpus = `getconf _NPROCESSORS_ONLN || nproc --all || grep -c '^processor' /proc/cpuinfo`.to_i
cpu_count = System.cpu_count
cpu_count.should eq(shell_cpus)
end
end
end
11 changes: 6 additions & 5 deletions src/lib_c/aarch64-linux-gnu/c/unistd.cr
Expand Up @@ -2,11 +2,12 @@ require "./sys/types"
require "./stdint"

lib LibC
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
SC_NPROCESSORS_ONLN = 84

fun access(name : Char*, type : Int) : Int
fun chdir(path : Char*) : Int
Expand Down
11 changes: 6 additions & 5 deletions src/lib_c/amd64-unknown-openbsd/c/unistd.cr
Expand Up @@ -2,11 +2,12 @@ require "./sys/types"
require "./stdint"

lib LibC
F_OK = 0
R_OK = 0x04
W_OK = 0x02
X_OK = 0x01
SC_CLK_TCK = 3
F_OK = 0
R_OK = 0x04
W_OK = 0x02
X_OK = 0x01
SC_CLK_TCK = 3
SC_NPROCESSORS_ONLN = 58

fun access(x0 : Char*, x1 : Int) : Int
fun chdir(x0 : Char*) : Int
Expand Down
11 changes: 6 additions & 5 deletions src/lib_c/arm-linux-gnueabihf/c/unistd.cr
Expand Up @@ -2,11 +2,12 @@ require "./sys/types"
require "./stdint"

lib LibC
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
SC_NPROCESSORS_ONLN = 58

fun access(name : Char*, type : Int) : Int
fun chdir(path : Char*) : Int
Expand Down
11 changes: 6 additions & 5 deletions src/lib_c/i686-linux-gnu/c/unistd.cr
Expand Up @@ -2,11 +2,12 @@ require "./sys/types"
require "./stdint"

lib LibC
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
SC_NPROCESSORS_ONLN = 84

fun access(name : Char*, type : Int) : Int
fun chdir(path : Char*) : Int
Expand Down
11 changes: 6 additions & 5 deletions src/lib_c/i686-linux-musl/c/unistd.cr
Expand Up @@ -2,11 +2,12 @@ require "./sys/types"
require "./stdint"

lib LibC
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
SC_NPROCESSORS_ONLN = 84

fun access(x0 : Char*, x1 : Int) : Int
fun chdir(x0 : Char*) : Int
Expand Down
11 changes: 6 additions & 5 deletions src/lib_c/x86_64-linux-gnu/c/unistd.cr
Expand Up @@ -2,11 +2,12 @@ require "./sys/types"
require "./stdint"

lib LibC
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
SC_NPROCESSORS_ONLN = 84

fun access(name : Char*, type : Int) : Int
fun chdir(path : Char*) : Int
Expand Down
11 changes: 6 additions & 5 deletions src/lib_c/x86_64-linux-musl/c/unistd.cr
Expand Up @@ -2,11 +2,12 @@ require "./sys/types"
require "./stdint"

lib LibC
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
F_OK = 0
R_OK = 4
W_OK = 2
X_OK = 1
SC_CLK_TCK = 2
SC_NPROCESSORS_ONLN = 84

fun access(x0 : Char*, x1 : Int) : Int
fun chdir(x0 : Char*) : Int
Expand Down
11 changes: 6 additions & 5 deletions src/lib_c/x86_64-macosx-darwin/c/unistd.cr
Expand Up @@ -2,11 +2,12 @@ require "./sys/types"
require "./stdint"

lib LibC
F_OK = 0
R_OK = 1 << 2
W_OK = 1 << 1
X_OK = 1 << 0
SC_CLK_TCK = 3
F_OK = 0
R_OK = 1 << 2
W_OK = 1 << 1
X_OK = 1 << 0
SC_CLK_TCK = 3
SC_NPROCESSORS_ONLN = 58

fun access(x0 : Char*, x1 : Int) : Int
fun chdir(x0 : Char*) : Int
Expand Down
11 changes: 6 additions & 5 deletions src/lib_c/x86_64-portbld-freebsd/c/unistd.cr
Expand Up @@ -2,11 +2,12 @@ require "./sys/types"
require "./stdint"

lib LibC
F_OK = 0
R_OK = 0x04
W_OK = 0x02
X_OK = 0x01
SC_CLK_TCK = 3
F_OK = 0
R_OK = 0x04
W_OK = 0x02
X_OK = 0x01
SC_CLK_TCK = 3
SC_NPROCESSORS_ONLN = 58

fun access(x0 : Char*, x1 : Int) : Int
fun chdir(x0 : Char*) : Int
Expand Down
9 changes: 9 additions & 0 deletions src/system.cr
Expand Up @@ -18,4 +18,13 @@ module System
{len, len}
end
end

# Returns the number of logical processors available to the system.
#
# ```
# System.cpu_count # => 4
# ```
def self.cpu_count
LibC.sysconf(LibC::SC_NPROCESSORS_ONLN)
end
end

0 comments on commit 0e73cf4

Please sign in to comment.