hyper_simple_server/
profiling.rs1
2
3#[ allow (unused_imports) ]
4use crate::prelude::*;
5
6
7
8
9#[ cfg (feature = "cpuprofiler") ]
10pub struct ProfilingSession {
11 path_final : path::PathBuf,
12 path_temporary : path::PathBuf,
13 active : usize,
14}
15
16
17#[ cfg (feature = "cpuprofiler") ]
18impl ProfilingSession {
19
20
21 pub fn new (_path : &path::Path) -> ServerResult<Self> {
22
23 let _path_final = _path.to_owned ();
24
25 let _path_temporary = {
26 let mut _path_temporary = _path_final.clone () .into_os_string ();
27 _path_temporary.push (format! (".{}.tmp", process::id ()));
28 _path_temporary.into ()
29 };
30
31 let _self = Self {
32 path_final : _path_final,
33 path_temporary : _path_temporary,
34 active : 0,
35 };
36
37 Ok (_self)
38 }
39
40
41 pub fn new_and_start (_path : &path::Path) -> ServerResult<Self> {
42 let mut _self = Self::new (_path) ?;
43 _self.start () ?;
44 Ok (_self)
45 }
46
47
48 pub fn start (&mut self) -> ServerResult {
49
50 if self.active == 0 {
51 profiling_start (&self.path_temporary) ?;
52 }
53
54 self.active += 1;
55
56 Ok (())
57 }
58
59
60 pub fn stop (&mut self) -> ServerResult {
61
62 if self.active == 0 {
63 return Err (error_with_code (0x628def32));
64 }
65
66 self.active -= 1;
67
68 if self.active == 0 {
69
70 profiling_stop () ?;
71
72 fs::rename (&self.path_temporary, &self.path_final) .or_wrap (0xfc22794e) ?;
73 }
74
75 Ok (())
76 }
77
78
79 pub fn stop_and_drop (mut self) -> ServerResult {
80 self.drop_0 ()
81 }
82
83 fn drop_0 (&mut self) -> ServerResult {
84 if self.active == 0 {
85 return Ok (());
86 }
87 self.active = 1;
88 return self.stop ();
89 }
90}
91
92
93#[ cfg (feature = "cpuprofiler") ]
94impl Drop for ProfilingSession {
95
96 fn drop (&mut self) -> () {
97 self.drop_0 () .or_panic (0xaebddcc0);
98 }
99}
100
101
102
103
104#[ cfg (feature = "cpuprofiler") ]
105fn profiling_start (_path : &path::Path) -> ServerResult {
106
107 let _path = _path.to_str () .or_wrap (0x977d8538) ?;
108 let _path = _path.to_owned () .into_bytes ();
109
110 #[ cfg (debug_assertions) ]
111 eprintln! ("[ii] [1c05ae71] starting `cpuprofiler` tracing...");
112
113 let mut _profiler = ::cpuprofiler::PROFILER.lock () .or_wrap (0xd30eee91) ?;
114
115 if _profiler.state () != ::cpuprofiler::ProfilerState::NotActive {
116 return Err (error_with_code (0x1bd8ceb5));
117 }
118
119 _profiler.start (_path) .or_wrap (0x57e487d1) ?;
120
121 Ok (())
122}
123
124
125#[ cfg (feature = "cpuprofiler") ]
126fn profiling_stop () -> ServerResult {
127
128 #[ cfg (debug_assertions) ]
129 eprintln! ("[ii] [27a3b301] stopping `cpuprofiler` tracing...");
130
131 let mut _profiler = ::cpuprofiler::PROFILER.lock () .or_wrap (0x678aa104) ?;
132
133 if _profiler.state () != ::cpuprofiler::ProfilerState::Active {
134 return Err (error_with_code (0x5dff5e52));
135 }
136
137 _profiler.stop () .or_wrap (0x39363dfd) ?;
138
139 Ok (())
140}
141