@@ -132,18 +132,18 @@ impl OutOfMemoryEstimator {
132132}
133133
134134#[ cfg( target_os = "linux" ) ]
135- fn get_cgroup_paths < ' a > ( proc_cgroups : & ' a str ) -> Vec < & ' a str > {
135+ fn get_cgroup_paths ( proc_cgroups : & str ) -> Option < Vec < & str > > {
136136 let mut result = vec ! [ ] ;
137137 for line in proc_cgroups. lines ( ) {
138138 // TODO better error handling?
139- let mut parts = line. splitn ( 3 , ":" ) ;
140- let subsystems = parts. nth ( 1 ) . unwrap ( ) ;
141- if ( subsystems == "" ) || subsystems. split ( "," ) . any ( |s| s == "memory" ) {
142- let cgroup_path = parts. nth ( 0 ) . unwrap ( ) . strip_prefix ( "/" ) . unwrap ( ) ;
139+ let mut parts = line. splitn ( 3 , ':' ) ;
140+ let subsystems = parts. nth ( 1 ) ? ;
141+ if subsystems. is_empty ( ) || subsystems. split ( ',' ) . any ( |s| s == "memory" ) {
142+ let cgroup_path = parts. next ( ) ? . strip_prefix ( '/' ) ? ;
143143 result. push ( cgroup_path) ;
144144 }
145145 }
146- result
146+ Some ( result)
147147}
148148
149149/// Real system information.
@@ -156,9 +156,9 @@ pub struct RealMemoryInfo {
156156 cgroup : Option < cgroups_rs:: Cgroup > ,
157157}
158158
159- impl RealMemoryInfo {
159+ impl Default for RealMemoryInfo {
160160 #[ cfg( target_os = "linux" ) ]
161- pub fn new ( ) -> Self {
161+ fn default ( ) -> Self {
162162 let get_cgroup = || {
163163 let contents = match read_to_string ( "/proc/self/cgroup" ) {
164164 Ok ( contents) => contents,
@@ -167,14 +167,14 @@ impl RealMemoryInfo {
167167 return None ;
168168 }
169169 } ;
170- let cgroup_paths = get_cgroup_paths ( & contents) ;
171- for path in cgroup_paths {
170+ let cgroup_paths = get_cgroup_paths ( & contents) ? ;
171+ if let Some ( path) = cgroup_paths. into_iter ( ) . next ( ) {
172172 let h = cgroups_rs:: hierarchies:: auto ( ) ;
173173 let cgroup = cgroups_rs:: Cgroup :: load ( h, path) ;
174174 // Make sure memory_stat() works. Sometimes it doesn't
175175 // (https://github.com/pythonspeed/filprofiler/issues/147). If
176176 // it doesn't, this'll panic.
177- let mem: & cgroups_rs:: memory:: MemController = cgroup. controller_of ( ) . unwrap ( ) ;
177+ let mem: & cgroups_rs:: memory:: MemController = cgroup. controller_of ( ) ? ;
178178 let _mem = mem. memory_stat ( ) ;
179179 return Some ( cgroup) ;
180180 }
@@ -190,18 +190,20 @@ impl RealMemoryInfo {
190190 }
191191 } ;
192192 Self {
193- cgroup : cgroup ,
193+ cgroup,
194194 process : psutil:: process:: Process :: current ( ) . ok ( ) ,
195195 }
196196 }
197197
198198 #[ cfg( target_os = "macos" ) ]
199- pub fn new ( ) -> Self {
199+ fn default ( ) -> Self {
200200 Self {
201201 process : psutil:: process:: Process :: current ( ) . ok ( ) ,
202202 }
203203 }
204+ }
204205
206+ impl RealMemoryInfo {
205207 #[ cfg( target_os = "linux" ) ]
206208 pub fn get_cgroup_available_memory ( & self ) -> usize {
207209 let mut result = std:: usize:: MAX ;
@@ -231,14 +233,18 @@ impl RealMemoryInfo {
231233
232234impl MemoryInfo for RealMemoryInfo {
233235 fn total_memory ( & self ) -> usize {
234- psutil:: memory:: virtual_memory ( ) . unwrap ( ) . total ( ) as usize
236+ psutil:: memory:: virtual_memory ( )
237+ . map ( |vm| vm. total ( ) as usize )
238+ . unwrap_or ( 0 )
235239 }
236240
237241 /// Return how much free memory we have, as bytes.
238242 fn get_available_memory ( & self ) -> usize {
239243 // This will include memory that can become available by syncing
240244 // filesystem buffers to disk, which is probably what we want.
241- let available = psutil:: memory:: virtual_memory ( ) . unwrap ( ) . available ( ) as usize ;
245+ let available = psutil:: memory:: virtual_memory ( )
246+ . map ( |vm| vm. available ( ) as usize )
247+ . unwrap_or ( std:: usize:: MAX ) ;
242248 let cgroup_available = self . get_cgroup_available_memory ( ) ;
243249 std:: cmp:: min ( available, cgroup_available)
244250 }
@@ -261,8 +267,10 @@ impl MemoryInfo for RealMemoryInfo {
261267 eprintln ! (
262268 "=fil-profile= cgroup (e.g. container) memory info: {:?}" ,
263269 if let Some ( cgroup) = & self . cgroup {
264- let mem: & cgroups_rs:: memory:: MemController = cgroup. controller_of( ) . unwrap( ) ;
265- Some ( mem. memory_stat( ) )
270+ cgroup
271+ . controller_of:: <cgroups_rs:: memory:: MemController >( )
272+ . as_ref( )
273+ . map( |mem| mem. memory_stat( ) )
266274 } else {
267275 None
268276 }
@@ -369,7 +377,7 @@ mod tests {
369377 proptest ! {
370378 // Random allocations don't break invariants
371379 #[ test]
372- fn not_oom( allocated_sizes in prop:: collection:: vec( 1 ..1000 as usize , 10 ..2000 ) ) {
380+ fn not_oom( allocated_sizes in prop:: collection:: vec( 1 ..1000usize , 10 ..2000 ) ) {
373381 let ( mut estimator, memory_info) = setup_estimator( ) ;
374382 let mut allocated = 0 ;
375383 for size in allocated_sizes {
0 commit comments