1use crate::get_local_timestamp;
2use crate::ChimesError;
3use serde_derive::{Deserialize, Serialize};
4use std::cell::RefCell;
5use std::sync::{atomic::AtomicU64, Mutex};
6
7#[derive(Debug, Clone, Default, Deserialize, Serialize)]
8pub struct ChimesPerformanceInfo {
9 pub timestamp: u64, pub cpu_cores: u32, pub kernel_cpu_usages: f64, pub user_cpu_usages: f64, pub idle_cpu_usages: f64, pub now_cpu_time: f64, pub memory_used: u64, pub memory_total: u64, pub disk_read_total: u64, pub disk_write_total: u64, pub network_recv_total: u64, pub network_send_total: u64, pub threads: u64, pub handlers: u64, pub success: bool, pub counter: CustomCounterInfo,
25}
26
27#[derive(Debug, Clone, Default, Deserialize, Serialize)]
28pub struct CustomCounterInfo {
29 pub task_1_count: u64,
30 pub task_2_count: u64,
31 pub task_3_count: u64,
32 pub task_4_count: u64,
33 pub task_5_count: u64,
34 pub task_6_count: u64,
35 pub task_7_count: u64,
36 pub task_8_count: u64,
37 pub task_9_count: u64,
38 pub task_10_count: u64,
39}
40
41#[derive(Default)]
42pub struct CustomCounter {
43 pub task_1_count: AtomicU64,
44 pub task_2_count: AtomicU64,
45 pub task_3_count: AtomicU64,
46 pub task_4_count: AtomicU64,
47 pub task_5_count: AtomicU64,
48 pub task_6_count: AtomicU64,
49 pub task_7_count: AtomicU64,
50 pub task_8_count: AtomicU64,
51 pub task_9_count: AtomicU64,
52 pub task_10_count: AtomicU64,
53}
54
55impl CustomCounter {
56 pub fn into_counting(&self) -> CustomCounterInfo {
57 CustomCounterInfo {
58 task_1_count: self.task_1_count.load(std::sync::atomic::Ordering::Acquire),
59 task_2_count: self.task_2_count.load(std::sync::atomic::Ordering::Acquire),
60 task_3_count: self.task_3_count.load(std::sync::atomic::Ordering::Acquire),
61 task_4_count: self.task_4_count.load(std::sync::atomic::Ordering::Acquire),
62 task_5_count: self.task_5_count.load(std::sync::atomic::Ordering::Acquire),
63 task_6_count: self.task_6_count.load(std::sync::atomic::Ordering::Acquire),
64 task_7_count: self.task_7_count.load(std::sync::atomic::Ordering::Acquire),
65 task_8_count: self.task_8_count.load(std::sync::atomic::Ordering::Acquire),
66 task_9_count: self.task_9_count.load(std::sync::atomic::Ordering::Acquire),
67 task_10_count: self
68 .task_10_count
69 .load(std::sync::atomic::Ordering::Acquire),
70 }
71 }
72
73 pub fn get_task_count(&self, t: u32) -> u64 {
74 match t {
75 1 => self.task_1_count.load(std::sync::atomic::Ordering::Acquire),
76 2 => self.task_2_count.load(std::sync::atomic::Ordering::Acquire),
77 3 => self.task_3_count.load(std::sync::atomic::Ordering::Acquire),
78 4 => self.task_4_count.load(std::sync::atomic::Ordering::Acquire),
79 5 => self.task_5_count.load(std::sync::atomic::Ordering::Acquire),
80 6 => self.task_6_count.load(std::sync::atomic::Ordering::Acquire),
81 7 => self.task_7_count.load(std::sync::atomic::Ordering::Acquire),
82 8 => self.task_8_count.load(std::sync::atomic::Ordering::Acquire),
83 9 => self.task_9_count.load(std::sync::atomic::Ordering::Acquire),
84 10 => self
85 .task_10_count
86 .load(std::sync::atomic::Ordering::Acquire),
87 _ => 0u64,
88 }
89 }
90}
91
92lazy_static! {
93 pub static ref CUSTOM_PERFORMANCE_COUNTER: Mutex<RefCell<CustomCounter>> =
94 Mutex::new(RefCell::new(CustomCounter::default()));
95}
96
97#[allow(dead_code)]
98pub fn get_custom_performance_counter() -> &'static CustomCounter {
99 unsafe { &*CUSTOM_PERFORMANCE_COUNTER.lock().unwrap().as_ptr() }
100}
101
102#[allow(dead_code)]
103pub fn custom_performance_counter_increase(it: i32) {
104 match it {
105 1 => CUSTOM_PERFORMANCE_COUNTER
106 .lock()
107 .unwrap()
108 .borrow_mut()
109 .task_1_count
110 .fetch_add(1u64, std::sync::atomic::Ordering::Release),
111 2 => CUSTOM_PERFORMANCE_COUNTER
112 .lock()
113 .unwrap()
114 .borrow_mut()
115 .task_2_count
116 .fetch_add(1u64, std::sync::atomic::Ordering::Release),
117 3 => CUSTOM_PERFORMANCE_COUNTER
118 .lock()
119 .unwrap()
120 .borrow_mut()
121 .task_3_count
122 .fetch_add(1u64, std::sync::atomic::Ordering::Release),
123 4 => CUSTOM_PERFORMANCE_COUNTER
124 .lock()
125 .unwrap()
126 .borrow_mut()
127 .task_4_count
128 .fetch_add(1u64, std::sync::atomic::Ordering::Release),
129 5 => CUSTOM_PERFORMANCE_COUNTER
130 .lock()
131 .unwrap()
132 .borrow_mut()
133 .task_5_count
134 .fetch_add(1u64, std::sync::atomic::Ordering::Release),
135 6 => CUSTOM_PERFORMANCE_COUNTER
136 .lock()
137 .unwrap()
138 .borrow_mut()
139 .task_6_count
140 .fetch_add(1u64, std::sync::atomic::Ordering::Release),
141 7 => CUSTOM_PERFORMANCE_COUNTER
142 .lock()
143 .unwrap()
144 .borrow_mut()
145 .task_7_count
146 .fetch_add(1u64, std::sync::atomic::Ordering::Release),
147 8 => CUSTOM_PERFORMANCE_COUNTER
148 .lock()
149 .unwrap()
150 .borrow_mut()
151 .task_8_count
152 .fetch_add(1u64, std::sync::atomic::Ordering::Release),
153 9 => CUSTOM_PERFORMANCE_COUNTER
154 .lock()
155 .unwrap()
156 .borrow_mut()
157 .task_9_count
158 .fetch_add(1u64, std::sync::atomic::Ordering::Release),
159 10 => CUSTOM_PERFORMANCE_COUNTER
160 .lock()
161 .unwrap()
162 .borrow_mut()
163 .task_10_count
164 .fetch_add(1u64, std::sync::atomic::Ordering::Release),
165 _ => 0u64,
166 };
167}
168
169#[allow(dead_code)]
170pub fn custom_performance_counter_add(it: i32, val: u64) {
171 match it {
172 1 => CUSTOM_PERFORMANCE_COUNTER
173 .lock()
174 .unwrap()
175 .borrow_mut()
176 .task_1_count
177 .fetch_add(val, std::sync::atomic::Ordering::Release),
178 2 => CUSTOM_PERFORMANCE_COUNTER
179 .lock()
180 .unwrap()
181 .borrow_mut()
182 .task_2_count
183 .fetch_add(val, std::sync::atomic::Ordering::Release),
184 3 => CUSTOM_PERFORMANCE_COUNTER
185 .lock()
186 .unwrap()
187 .borrow_mut()
188 .task_3_count
189 .fetch_add(val, std::sync::atomic::Ordering::Release),
190 4 => CUSTOM_PERFORMANCE_COUNTER
191 .lock()
192 .unwrap()
193 .borrow_mut()
194 .task_4_count
195 .fetch_add(val, std::sync::atomic::Ordering::Release),
196 5 => CUSTOM_PERFORMANCE_COUNTER
197 .lock()
198 .unwrap()
199 .borrow_mut()
200 .task_5_count
201 .fetch_add(val, std::sync::atomic::Ordering::Release),
202 6 => CUSTOM_PERFORMANCE_COUNTER
203 .lock()
204 .unwrap()
205 .borrow_mut()
206 .task_6_count
207 .fetch_add(val, std::sync::atomic::Ordering::Release),
208 7 => CUSTOM_PERFORMANCE_COUNTER
209 .lock()
210 .unwrap()
211 .borrow_mut()
212 .task_7_count
213 .fetch_add(val, std::sync::atomic::Ordering::Release),
214 8 => CUSTOM_PERFORMANCE_COUNTER
215 .lock()
216 .unwrap()
217 .borrow_mut()
218 .task_8_count
219 .fetch_add(val, std::sync::atomic::Ordering::Release),
220 9 => CUSTOM_PERFORMANCE_COUNTER
221 .lock()
222 .unwrap()
223 .borrow_mut()
224 .task_9_count
225 .fetch_add(val, std::sync::atomic::Ordering::Release),
226 10 => CUSTOM_PERFORMANCE_COUNTER
227 .lock()
228 .unwrap()
229 .borrow_mut()
230 .task_10_count
231 .fetch_add(val, std::sync::atomic::Ordering::Release),
232 _ => 0u64,
233 };
234}
235
236#[allow(dead_code)]
237pub fn custom_performance_counter_reset(it: i32) {
238 match it {
239 1 => CUSTOM_PERFORMANCE_COUNTER
240 .lock()
241 .unwrap()
242 .borrow_mut()
243 .task_1_count
244 .store(0u64, std::sync::atomic::Ordering::Release),
245 2 => CUSTOM_PERFORMANCE_COUNTER
246 .lock()
247 .unwrap()
248 .borrow_mut()
249 .task_2_count
250 .store(0u64, std::sync::atomic::Ordering::Release),
251 3 => CUSTOM_PERFORMANCE_COUNTER
252 .lock()
253 .unwrap()
254 .borrow_mut()
255 .task_3_count
256 .store(0u64, std::sync::atomic::Ordering::Release),
257 4 => CUSTOM_PERFORMANCE_COUNTER
258 .lock()
259 .unwrap()
260 .borrow_mut()
261 .task_4_count
262 .store(0u64, std::sync::atomic::Ordering::Release),
263 5 => CUSTOM_PERFORMANCE_COUNTER
264 .lock()
265 .unwrap()
266 .borrow_mut()
267 .task_5_count
268 .store(0u64, std::sync::atomic::Ordering::Release),
269 6 => CUSTOM_PERFORMANCE_COUNTER
270 .lock()
271 .unwrap()
272 .borrow_mut()
273 .task_6_count
274 .store(0u64, std::sync::atomic::Ordering::Release),
275 7 => CUSTOM_PERFORMANCE_COUNTER
276 .lock()
277 .unwrap()
278 .borrow_mut()
279 .task_7_count
280 .store(0u64, std::sync::atomic::Ordering::Release),
281 8 => CUSTOM_PERFORMANCE_COUNTER
282 .lock()
283 .unwrap()
284 .borrow_mut()
285 .task_8_count
286 .store(0u64, std::sync::atomic::Ordering::Release),
287 9 => CUSTOM_PERFORMANCE_COUNTER
288 .lock()
289 .unwrap()
290 .borrow_mut()
291 .task_9_count
292 .store(0u64, std::sync::atomic::Ordering::Release),
293 10 => CUSTOM_PERFORMANCE_COUNTER
294 .lock()
295 .unwrap()
296 .borrow_mut()
297 .task_10_count
298 .store(0u64, std::sync::atomic::Ordering::Release),
299 _ => {}
300 };
301}
302
303impl ChimesPerformanceInfo {
304 #[cfg(target_os = "windows")]
305 pub fn get_performance_info() -> Result<Self, ChimesError> {
306 use super::windows_performance::WindowsPerformance;
307
308 let hc = WindowsPerformance::get_handle_count();
309 let tc = WindowsPerformance::get_thread_count(WindowsPerformance::get_current_process_id());
310 let (kcpu, ucpu, now) = WindowsPerformance::get_process_times();
311 let (ttl, mem) = WindowsPerformance::get_memory_usages();
312 let (dr, dw) = WindowsPerformance::get_io_counter();
313 let cores = WindowsPerformance::get_cpu_cores();
314 let (inrc, wrc) = WindowsPerformance::get_network_io_counter();
315 let now_time = get_local_timestamp();
316
317 let newitem = Self {
318 timestamp: now_time,
319 kernel_cpu_usages: kcpu,
320 user_cpu_usages: ucpu,
321 cpu_cores: cores,
322 idle_cpu_usages: 0f64,
323 now_cpu_time: now,
324 memory_used: mem,
325 memory_total: ttl,
326 disk_read_total: dr,
327 disk_write_total: dw,
328 network_recv_total: inrc,
329 network_send_total: wrc,
330 threads: tc as u64,
331 handlers: hc as u64,
332 success: true,
333 counter: get_custom_performance_counter().into_counting(),
334 };
335
336 Ok(newitem)
337 }
338
339 #[cfg(not(target_os = "windows"))]
340 pub fn get_performance_info() -> Result<Self, ChimesError> {
341 use super::linux_performance::LinuxPerformance;
342
343 let hc = LinuxPerformance::get_handle_count();
344 let tc = LinuxPerformance::get_thread_count(LinuxPerformance::get_current_process_id());
345 let (kcpu, ucpu, idle) = LinuxPerformance::get_process_times();
346 let (ttl, mem) = LinuxPerformance::get_memory_usages();
347 let (dr, dw) = LinuxPerformance::get_io_counter();
348 let cores = LinuxPerformance::get_cpu_cores();
349 let (inrc, wrc) = LinuxPerformance::get_network_io_counter();
350 let now_time = get_local_timestamp();
351
352 let newitem = Self {
353 timestamp: now_time,
354 kernel_cpu_usages: kcpu,
355 user_cpu_usages: ucpu,
356 idle_cpu_usages: idle,
357 cpu_cores: cores,
358 now_cpu_time: now_time as f64,
359 memory_used: mem as u64,
360 memory_total: ttl as u64,
361 disk_read_total: dr,
362 disk_write_total: dw,
363 network_recv_total: inrc,
364 network_send_total: wrc,
365 threads: tc as u64,
366 handlers: hc as u64,
367 success: true,
368 counter: get_custom_performance_counter().into_counting(),
369 };
370 Ok(newitem)
371 }
372}