From f7a9a4bb7bf43bd916efa876e02c39a22f3bc49d Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Tue, 11 Jun 2013 18:43:42 -0400 Subject: [PATCH] add a Sys module and start to move some system constants out of base (also closes #3331) --- Makefile | 2 +- base/Makefile | 4 +- base/build.h | 26 +++++++++ base/client.jl | 11 +--- base/deprecated.jl | 2 + base/exports.jl | 1 + base/sysimg.jl | 1 + base/sysinfo.jl | 133 +++++++++++++++++++++++++++++++++++++++++++++ base/util.jl | 120 +++------------------------------------- base/version.jl | 4 +- src/os_detect.h | 83 ---------------------------- 11 files changed, 178 insertions(+), 209 deletions(-) create mode 100644 base/build.h create mode 100644 base/sysinfo.jl delete mode 100644 src/os_detect.h diff --git a/Makefile b/Makefile index 985bfc534db21..3c92038dce37f 100644 --- a/Makefile +++ b/Makefile @@ -92,7 +92,7 @@ ifeq ($(USE_SYSTEM_LIBUV),0) cp -a $(BUILD)/lib/libuv.a $(PREFIX)/$(JL_PRIVATE_LIBDIR) cp -a $(BUILD)/include/uv* $(PREFIX)/include/julia endif - cp -a src/julia.h $(PREFIX)/include/julia + cp -a src/julia.h src/support/*.h $(PREFIX)/include/julia # Copy system image cp $(BUILD)/$(JL_PRIVATE_LIBDIR)/sys.ji $(PREFIX)/$(JL_PRIVATE_LIBDIR) # Copy in all .jl sources as well diff --git a/base/Makefile b/base/Makefile index 8cb55d676cabc..ee95f73ed5f0e 100644 --- a/base/Makefile +++ b/base/Makefile @@ -17,8 +17,8 @@ file_constants.jl: ../src/file_constants.h uv_constants.jl: ../src/uv_constants.h $(QUIET_PERL) ${CC} -E -P "-I$(LIBUV_INC)" -DJULIA ../src/uv_constants.h | tail -n 5 > $@ -build_h.jl: ../Make.inc ../src/os_detect.h - $(QUIET_PERL) $(CC) -E -P -DJULIA ../src/os_detect.h | grep OS_NAME > $@ +build_h.jl: ../Make.inc build.h + $(QUIET_PERL) $(CC) -E -P -D_ARCH=$(ARCH) -D_MACHINE=$(shell $(CC) -dumpmachine) build.h -I../src/support | grep . > $@ @echo "const libm_name = \"$(LIBMNAME)\"" >> $@ @echo "const libblas_name = \"$(LIBBLASNAME)\"" >> $@ @echo "const liblapack_name = \"$(LIBLAPACKNAME)\"" >> $@ diff --git a/base/build.h b/base/build.h new file mode 100644 index 0000000000000..7113e7b7d47e2 --- /dev/null +++ b/base/build.h @@ -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 diff --git a/base/client.jl b/base/client.jl index 8bbf8574ceaae..d3b8fc5e2f1f5 100644 --- a/base/client.jl +++ b/base/client.jl @@ -206,7 +206,7 @@ function process_options(args::Array{Any,1}) elseif args[i]=="-p" i+=1 if i > length(args) || !isdigit(args[i][1]) - np = CPU_CORES + np = Sys.CPU_CORES else np = int(args[i]) end @@ -301,6 +301,8 @@ function _start() if Base.libblas_name == "libopenblas" check_openblas() end + Sys.init() + global const CPU_CORES = Sys.CPU_CORES # set default local address global bind_addr = getipaddr() @@ -315,13 +317,6 @@ function _start() end end - # set CPU core count - global const CPU_CORES = int( - haskey(ENV,"JULIA_CPU_CORES") ? - ENV["JULIA_CPU_CORES"] : - ccall(:jl_cpu_cores, Int32, ()) - ) - #atexit(()->flush(STDOUT)) try init_sched() diff --git a/base/deprecated.jl b/base/deprecated.jl index db443f4585801..6399d79f54068 100644 --- a/base/deprecated.jl +++ b/base/deprecated.jl @@ -224,5 +224,7 @@ export ref const assign = setindex! export assign +# will be removed from exports (moved into Base.Sys): OS_NAME, WORD_SIZE, CPU_CORES + typealias ComplexPair Complex export ComplexPair diff --git a/base/exports.jl b/base/exports.jl index ab482b77672f6..14793bbac3e54 100644 --- a/base/exports.jl +++ b/base/exports.jl @@ -13,6 +13,7 @@ export GMP, QuadGK, Sort, + Sys, Test, Pkg, Operators, diff --git a/base/sysimg.jl b/base/sysimg.jl index e250fa5829cc6..82d553941c589 100644 --- a/base/sysimg.jl +++ b/base/sysimg.jl @@ -148,6 +148,7 @@ include("darray2.jl") include("mmap.jl") # utilities - version, timing, help, edit, metaprogramming +include("sysinfo.jl") include("version.jl") include("datafmt.jl") include("deepcopy.jl") diff --git a/base/sysinfo.jl b/base/sysinfo.jl new file mode 100644 index 0000000000000..f83e5dc1c6937 --- /dev/null +++ b/base/sysinfo.jl @@ -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 diff --git a/base/util.jl b/base/util.jl index c74bb3f67bfb9..0cba365b223a6 100644 --- a/base/util.jl +++ b/base/util.jl @@ -227,20 +227,20 @@ function versioninfo(io::IO, verbose::Bool) println(io, "Julia $version_string") println(io, commit_string) println(io, "Platform Info:") - println(io, " OS_NAME: ", OS_NAME) - println(io, " WORD_SIZE: ", WORD_SIZE) + println(io, " System: ", Sys.OS_NAME, " (", Sys.MACHINE, ")") + println(io, " WORD_SIZE: ", Sys.WORD_SIZE) if verbose lsb = readchomp(ignorestatus(`lsb_release -ds`) .> SpawnNullStream()) if lsb != "" println(io, " ", lsb) end println(io, " uname: ",readchomp(`uname -mprsv`)) - println(io, "Memory: $(total_memory()/2^30) GB ($(free_memory()/2^20) MB free)") - try println(io, "Uptime: $(uptime()) sec") catch end + println(io, "Memory: $(Sys.total_memory()/2^30) GB ($(Sys.free_memory()/2^20) MB free)") + try println(io, "Uptime: $(Sys.uptime()) sec") catch end print(io, "Load Avg: ") - print_matrix(io, Base.loadavg()') + print_matrix(io, Sys.loadavg()') println(io ) - println(io, cpu_info()) + println(io, Sys.cpu_info()) end if Base.libblas_name == "libopenblas" openblas_config = openblas_get_config() @@ -258,118 +258,12 @@ function versioninfo(io::IO, verbose::Bool) end end println(io ) + println(io, "Package Directory: ", Pkg.dir()) println(io, "Packages Installed:") Pkg.status(io ) end 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 - SC_CLK_TCK::Int - CPUinfo(model,speed,u,n,s,id,ir,ticks)=new(model,speed,u,n,s,id,ir,ticks) -end -CPUinfo(info::UV_cpu_info_t, ticks) = 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, ticks) - -show(io::IO, cpu::CPUinfo) = show(io, cpu, true, " ") -function show(io::IO, info::CPUinfo, header::Bool, prefix::String) - tck = info.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,cpu[i].SC_CLK_TCK) - 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() - SC_CLK_TCK = ccall(:jl_SC_CLK_TCK, Clong, ()) - 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),SC_CLK_TCK) - 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, ()) - - # `methodswith` -- shows a list of methods using the type given function methodswith(io::IO, t::Type, m::Module, showparents::Bool) diff --git a/base/version.jl b/base/version.jl index 0bf93577c267e..d11f8e6ebc7c4 100644 --- a/base/version.jl +++ b/base/version.jl @@ -247,7 +247,7 @@ const banner_plain = | | | | | | |/ _` | | | | |_| | | | (_| | | $version_string _/ |\\__'_|_|_|\\__'_| | $commit_string -|__/ | +|__/ | $(Sys.MACHINE) """ local tx = "\033[0m\033[1m" # text @@ -264,7 +264,7 @@ const banner_color = $(jl)| | | | | | |/ _` |$(tx) | $(jl)| | |_| | | | (_| |$(tx) | $version_string $(jl)_/ |\\__'_|_|_|\\__'_|$(tx) | $commit_string -$(jl)|__/$(tx) | +$(jl)|__/$(tx) | $(Sys.MACHINE) \033[0m" end # begin diff --git a/src/os_detect.h b/src/os_detect.h deleted file mode 100644 index 5180c5211e4ac..0000000000000 --- a/src/os_detect.h +++ /dev/null @@ -1,83 +0,0 @@ -#ifndef OS_DETECT_H -#define OS_DETECT_H -#include "support/platform.h" - -/* This file uses is used by both Julia and C - After a major refactor, the C parts are now no longer necessary - and have thus been removed. Should you want to add them again, they - are avaiable as src/os_detect.h in git commit fbf1348369cb5c79810ff3015ac711c9dcdef2ca */ - -/* LOGIC PRIMITIVES */ - -/* These logic primitives may be used to do basic if/else in the C preprocessor - This can be useful for creating if/else type structures in C and Julia. - Currently there's support for up to 10 OSs. If you need more add a correspondig JL_BOOL_# */ - -#define JL_BOOL_0 0 -#define JL_BOOL_1 1 -#define JL_BOOL_2 1 -#define JL_BOOL_3 1 -#define JL_BOOL_4 1 -#define JL_BOOL_5 1 -#define JL_BOOL_6 1 -#define JL_BOOL_7 1 -#define JL_BOOL_8 1 -#define JL_BOOL_9 1 -#define JL_TF_0 false -#define JL_TF_1 true -#define JL_TF(x) JL_TF2(x) -#define JL_TF2(x) JL_TF_##x -#define JL_BOOL(x) JL_BOOL_##x -#define I(x) x - -#define JL_IF_0(T,F) F -#define JL_IF_1(T,F) T -#define JL_IF2(C,T,F) JL_IF_##C(T,F) -#define JL_IF(C,T,F) JL_IF2(C,T,F) - - -/* OS MAP - to add an OS just append entry to map. -All other functions will be updated automagically, but detection by variables must be added to jl_current_os -X(NUM,C-Var,Julia name) - General INFO -XX(ISUNIX) - OS Traits - */ -#define NUM_OS = 4 -#define NOP(x) -#define JL_OS_MAP2(X,XX) \ - X(0,Windows) XX(0) \ - X(1,Linux) XX(1) \ - X(2,FreeBSD) XX(1) \ - X(3,Darwin) XX(1) -#define JL_OS_MAP(X) JL_OS_MAP2(X,NOP) -#define OS_INDEX_MAP(x) x - -#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 - -#ifndef JULIA -/** REMOVED - SEE ABOVE COMMENT */ -#else - -const OS_NAME = :OS_CURRENT - -#define OS_NAME_IFELSE(NUM,NAME) JL_IF(JL_BOOL(NUM),elseif,if) (os==:NAME) return -#define ATTR(IS_UNIX) JL_TF(JL_BOOL(IS_UNIX)); \n -function is_unix(os::Symbol) -JL_OS_MAP2(OS_NAME_IFELSE,ATTR) -else -error("Unknown Operating System") -end -end - -#endif -#endif // OS_DETECT_H