1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
//! System and Julia version information.
use std::{ffi::CStr, ptr::NonNull};
use jl_sys::{
jl_cpu_threads, jl_get_UNAME, jl_is_debugbuild, jl_n_threads, jl_ver_is_release, jl_ver_major,
jl_ver_minor, jl_ver_patch, jl_ver_string,
};
use crate::{
data::managed::{private::ManagedPriv, symbol::Symbol},
private::Private,
};
/// Global Julia information.
pub struct Info;
impl Info {
/// Number of threads the CPU supports.
#[inline]
pub fn n_cpu_threads() -> usize {
unsafe { jl_cpu_threads() as usize }
}
#[inline]
/// Number of threads Julia can use.
pub fn n_threads() -> usize {
unsafe { jl_n_threads.load(::std::sync::atomic::Ordering::Relaxed) as usize }
}
/// Number of threads per thread pool.
pub fn n_threads_per_pool() -> &'static [u32] {
unsafe {
let n_pools = jl_sys::jl_n_threadpools.get() as usize;
let n_threads_per_pool = jl_sys::jl_n_threads_per_pool.get();
std::slice::from_raw_parts(n_threads_per_pool as _, n_pools)
}
}
#[inline]
/// Number of GC threads Julia can use.
pub fn n_gc_threads() -> usize {
unsafe { jl_sys::jl_n_gcthreads as usize }
}
/// Returns `true` if a debug build of Julia is used.
#[inline]
pub fn is_debugbuild() -> bool {
unsafe { jl_is_debugbuild() != 0 }
}
/// Name and information of the kernel.
#[inline]
pub fn uname() -> StrOrBytes<'static> {
unsafe {
let cstr =
Symbol::wrap_non_null(NonNull::new_unchecked(jl_get_UNAME()), Private).as_cstr();
if let Ok(rstr) = cstr.to_str() {
Ok(rstr)
} else {
Err(cstr.to_bytes())
}
}
}
// /// The CPU architecture.
// #[inline]
// pub fn arch() -> StrOrBytes<'static> {
// unsafe {
// let cstr =
// Symbol::wrap_non_null(NonNull::new_unchecked(jl_get_ARCH()), Private).as_cstr();
// if let Ok(rstr) = cstr.to_str() {
// Ok(rstr)
// } else {
// Err(cstr.to_bytes())
// }
// }
// }
/// The major version of Julia.
#[inline]
pub fn major_version() -> isize {
unsafe { jl_ver_major() as isize }
}
/// The minor version of Julia.
#[inline]
pub fn minor_version() -> isize {
unsafe { jl_ver_minor() as isize }
}
/// The patch version of Julia.
#[inline]
pub fn patch_version() -> isize {
unsafe { jl_ver_patch() as isize }
}
/// Returns true if a release version of Julia is used.
#[inline]
pub fn is_release() -> bool {
unsafe { jl_ver_is_release() != 0 }
}
/// Returns the version string of the used version of Julia.
#[inline]
pub fn version_string() -> &'static str {
unsafe { CStr::from_ptr(jl_ver_string()).to_str().unwrap() }
}
}
/// Alias for a result that contains either a valid UTF8-encoded string slice, or the raw byte
/// slice if the contents are not valid UTF8.
pub type StrOrBytes<'scope> = Result<&'scope str, &'scope [u8]>;
#[cfg(test)]
mod test {
use super::Info;
#[test]
fn is_global() {
assert_eq!(Info::major_version(), 1);
assert_eq!(Info::n_threads(), 0);
}
}