vanadin_tasks/modules/
sys.rs1use rquickjs::module::{Declarations, Exports, ModuleDef};
2use rquickjs::{Ctx, Function};
3use sysinfo::{CpuRefreshKind, MemoryRefreshKind, RefreshKind, System};
4
5pub struct Module;
6
7impl ModuleDef for Module {
8 #[inline(always)]
9 fn declare(declarations: &mut Declarations) -> rquickjs::Result<()> {
10 declarations.declare("cpuCount")?;
11 declarations.declare("cpuBrands")?;
12 declarations.declare("cpuNames")?;
13 declarations.declare("cpuFrequencies")?;
14 declarations.declare("cpuUsages")?;
15 declarations.declare("availableMemory")?;
16 declarations.declare("freeMemory")?;
17 declarations.declare("totalMemory")?;
18 declarations.declare("usedMemory")?;
19
20 Ok(())
21 }
22
23 #[inline(always)]
24 fn evaluate<'js>(ctx: &Ctx<'js>, exports: &mut Exports<'js>) -> rquickjs::Result<()> {
25 exports.export("cpuCount", Function::new(ctx.clone(), Self::cpu_count))?;
26 exports.export("cpuBrands", Function::new(ctx.clone(), Self::cpu_brands))?;
27 exports.export("cpuNames", Function::new(ctx.clone(), Self::cpu_names))?;
28 exports.export(
29 "cpuFrequencies",
30 Function::new(ctx.clone(), Self::cpu_frequencies),
31 )?;
32 exports.export("cpuUsages", Function::new(ctx.clone(), Self::cpu_usage))?;
33 exports.export(
34 "availableMemory",
35 Function::new(ctx.clone(), Self::available_memory),
36 )?;
37 exports.export("freeMemory", Function::new(ctx.clone(), Self::free_memory))?;
38 exports.export(
39 "totalMemory",
40 Function::new(ctx.clone(), Self::total_memory),
41 )?;
42 exports.export("usedMemory", Function::new(ctx.clone(), Self::used_memory))?;
43
44 Ok(())
45 }
46}
47
48impl Module {
49 fn cpu_count() -> Option<usize> {
50 System::new_with_specifics(RefreshKind::new().with_cpu(CpuRefreshKind::new()))
51 .physical_core_count()
52 }
53
54 #[inline(always)]
55 fn cpu_brands() -> Vec<String> {
56 System::new_with_specifics(RefreshKind::new().with_cpu(CpuRefreshKind::new()))
57 .cpus()
58 .iter()
59 .map(|e| e.brand().to_string())
60 .collect()
61 }
62
63 #[inline(always)]
64 fn cpu_names() -> Vec<String> {
65 System::new_with_specifics(RefreshKind::new().with_cpu(CpuRefreshKind::new()))
66 .cpus()
67 .iter()
68 .map(|e| e.name().to_string())
69 .collect()
70 }
71
72 #[inline(always)]
73 fn cpu_frequencies() -> Vec<u64> {
74 System::new_with_specifics(
75 RefreshKind::new().with_cpu(CpuRefreshKind::new().with_frequency()),
76 )
77 .cpus()
78 .iter()
79 .map(|e| e.frequency())
80 .collect()
81 }
82
83 #[inline(always)]
84 fn cpu_usage() -> Vec<f32> {
85 System::new_with_specifics(RefreshKind::new().with_cpu(CpuRefreshKind::new()))
86 .cpus()
87 .iter()
88 .map(|e| e.cpu_usage())
89 .collect()
90 }
91
92 #[inline(always)]
93 fn available_memory() -> u64 {
94 System::new_with_specifics(
95 RefreshKind::new().with_memory(MemoryRefreshKind::new().with_ram()),
96 )
97 .available_memory()
98 }
99
100 #[inline(always)]
101 fn free_memory() -> u64 {
102 System::new_with_specifics(
103 RefreshKind::new().with_memory(MemoryRefreshKind::new().with_ram()),
104 )
105 .free_memory()
106 }
107
108 #[inline(always)]
109 fn total_memory() -> u64 {
110 System::new_with_specifics(
111 RefreshKind::new().with_memory(MemoryRefreshKind::new().with_ram()),
112 )
113 .total_memory()
114 }
115
116 #[inline(always)]
117 fn used_memory() -> u64 {
118 System::new_with_specifics(
119 RefreshKind::new().with_memory(MemoryRefreshKind::new().with_ram()),
120 )
121 .used_memory()
122 }
123}