-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3358 from JuliaLang/jn/sysinfo
add a Sys module and start to move some system constants out of base
- Loading branch information
Showing
11 changed files
with
178 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
#ifndef OS_DETECT_H | ||
#define OS_DETECT_H | ||
#include "platform.h" | ||
|
||
/* This file is used by Julia */ | ||
|
||
#if defined(_OS_WINDOWS_) | ||
#define OS_CURRENT Windows | ||
#elif defined(__linux__) | ||
#define OS_CURRENT Linux | ||
#elif defined(__FreeBSD__) | ||
#define OS_CURRENT FreeBSD | ||
#elif defined(__APPLE__) | ||
#define OS_CURRENT Darwin | ||
#else | ||
#define OS_CURRENT Unknown | ||
#warning OS_CURRENT is Unknown | ||
#endif | ||
const OS_NAME = :OS_CURRENT | ||
|
||
#define XSTR(x) STR(x) | ||
#define STR(x) #x | ||
const ARCH = :_ARCH | ||
const MACHINE = XSTR(_MACHINE) | ||
|
||
#endif // OS_DETECT_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -13,6 +13,7 @@ export | |
GMP, | ||
QuadGK, | ||
Sort, | ||
Sys, | ||
Test, | ||
Pkg, | ||
Operators, | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,133 @@ | ||
module Sys | ||
|
||
export CPU_CORES, | ||
OS_NAME, | ||
WORD_SIZE, | ||
ARCH, | ||
MACHINE, | ||
cpu_info, | ||
uptime, | ||
loadavg, | ||
free_memory, | ||
total_memory | ||
|
||
import ..Base: WORD_SIZE, OS_NAME, ARCH, MACHINE, UV_error_t | ||
import ..Base: show, repl_show | ||
|
||
function init() | ||
# set CPU core count | ||
global const CPU_CORES = int( | ||
haskey(ENV,"JULIA_CPU_CORES") ? | ||
ENV["JULIA_CPU_CORES"] : | ||
ccall(:jl_cpu_cores, Int32, ()) | ||
) | ||
global const SC_CLK_TCK = ccall(:jl_SC_CLK_TCK, Clong, ()) | ||
end | ||
|
||
type UV_cpu_info_t | ||
model::Ptr{Uint8} | ||
speed::Int32 | ||
cpu_times!user::Uint64 | ||
cpu_times!nice::Uint64 | ||
cpu_times!sys::Uint64 | ||
cpu_times!idle::Uint64 | ||
cpu_times!irq::Uint64 | ||
end | ||
type CPUinfo | ||
model::ASCIIString | ||
speed::Int32 | ||
cpu_times!user::Uint64 | ||
cpu_times!nice::Uint64 | ||
cpu_times!sys::Uint64 | ||
cpu_times!idle::Uint64 | ||
cpu_times!irq::Uint64 | ||
CPUinfo(model,speed,u,n,s,id,ir)=new(model,speed,u,n,s,id,ir) | ||
end | ||
CPUinfo(info::UV_cpu_info_t) = CPUinfo(bytestring(info.model), info.speed, | ||
info.cpu_times!user, info.cpu_times!nice, info.cpu_times!sys, | ||
info.cpu_times!idle, info.cpu_times!irq) | ||
|
||
show(io::IO, cpu::CPUinfo) = show(io, cpu, true, " ") | ||
function show(io::IO, info::CPUinfo, header::Bool, prefix::String) | ||
tck = SC_CLK_TCK | ||
if header | ||
println(io, info.model, ": ") | ||
print(" "^length(prefix)) | ||
if tck > 0 | ||
@printf io " %5s %9s %9s %9s %9s %9s\n" "speed" "user" "nice" "sys" "idle" "irq" | ||
else | ||
@printf io " %5s %9s %9s %9s %9s %9s ticks\n" "speed" "user" "nice" "sys" "idle" "irq" | ||
end | ||
end | ||
print(prefix) | ||
if tck > 0 | ||
@printf io "%5d MHz %9d s %9d s %9d s %9d s %9d s" info.speed info.cpu_times!user/tck info.cpu_times!nice/tck info.cpu_times!sys/tck info.cpu_times!idle/tck info.cpu_times!irq/tck | ||
else | ||
@printf io "%5d MHz %9d %9d %9d %9d %9d ticks" info.speed info.cpu_times!user info.cpu_times!nice info.cpu_times!sys info.cpu_times!idle info.cpu_times!irq | ||
end | ||
end | ||
function cpu_summary(io::IO, cpu::Array{CPUinfo}, i, j) | ||
if j-i < 9 | ||
header = true | ||
for x = i:j | ||
if header == false println() end | ||
show(io,cpu[x],header,"#$(x-i+1) ") | ||
header = false | ||
end | ||
else | ||
summary = CPUinfo(cpu[i].model,0,0,0,0,0,0) | ||
count = j-i+1 | ||
for x = i:j | ||
summary.speed += cpu[i].speed | ||
summary.cpu_times!user += cpu[x].cpu_times!user | ||
summary.cpu_times!nice += cpu[x].cpu_times!nice | ||
summary.cpu_times!sys += cpu[x].cpu_times!sys | ||
summary.cpu_times!idle += cpu[x].cpu_times!idle | ||
summary.cpu_times!irq += cpu[x].cpu_times!irq | ||
end | ||
summary.speed = div(summary.speed,count) | ||
show(io,summary,true,"#1-$(count) ") | ||
end | ||
end | ||
function show(io::IO, cpu::Array{CPUinfo}) | ||
model = cpu[1].model | ||
first = 1 | ||
for i = 2:length(cpu) | ||
if model != cpu[i].model | ||
cpu_summary(io,cpu,first,i-1) | ||
first = i | ||
end | ||
end | ||
cpu_summary(io,cpu,first,length(cpu)) | ||
end | ||
repl_show(io::IO, cpu::Array{CPUinfo}) = show(io, cpu) | ||
function cpu_info() | ||
UVcpus = Array(Ptr{UV_cpu_info_t},1) | ||
count = Array(Int32,1) | ||
uv_error("uv_cpu_info",ccall(:uv_cpu_info, UV_error_t, (Ptr{Ptr{UV_cpu_info_t}}, Ptr{Int32}), UVcpus, count)) | ||
cpus = Array(CPUinfo,count[1]) | ||
for i = 1:length(cpus) | ||
cpus[i] = CPUinfo(unsafe_load(UVcpus[1],i)) | ||
end | ||
ccall(:uv_free_cpu_info, Void, (Ptr{UV_cpu_info_t}, Int32), UVcpus[1], count[1]) | ||
cpus | ||
end | ||
|
||
function uptime() | ||
uptime_ = Array(Float64,1) | ||
uv_error("uv_uptime",ccall(:uv_uptime, UV_error_t, (Ptr{Float64},), uptime_)) | ||
return uptime_[1] | ||
end | ||
|
||
function loadavg() | ||
loadavg_ = Array(Float64,3) | ||
ccall(:uv_loadavg, Void, (Ptr{Float64},), loadavg_) | ||
return loadavg_ | ||
end | ||
|
||
free_memory() = ccall(:uv_get_free_memory, Uint64, ()) | ||
total_memory() = ccall(:uv_get_total_memory, Uint64, ()) | ||
|
||
|
||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.