Coverage for python/lsst/utils/usage.py: 47%

Shortcuts on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

17 statements  

1# This file is part of utils. 

2# 

3# Developed for the LSST Data Management System. 

4# This product includes software developed by the LSST Project 

5# (https://www.lsst.org). 

6# See the COPYRIGHT file at the top-level directory of this distribution 

7# for details of code ownership. 

8# 

9# Use of this source code is governed by a 3-clause BSD-style 

10# license that can be found in the LICENSE file. 

11 

12"""Utilities for measuring resource consumption. 

13""" 

14 

15import platform 

16import resource 

17from typing import Tuple 

18 

19import psutil 

20from astropy import units as u 

21 

22__all__ = ["get_current_mem_usage", "get_peak_mem_usage"] 

23 

24 

25def get_current_mem_usage() -> Tuple[u.Quantity, u.Quantity]: 

26 """Report current memory usage. 

27 

28 Returns 

29 ------- 

30 usage_main : `astropy.units.Quantity` 

31 Current memory usage of the calling process expressed in bytes. 

32 usage_child : `astropy.units.Quantity` 

33 Current memory usage of the child processes (zero if there are none) 

34 expressed in bytes. 

35 

36 Notes 

37 ----- 

38 Function reports current memory usage using resident set size as a proxy. 

39 As such the values it reports are capped at available physical RAM and may 

40 not reflect the actual memory allocated to the process and its children. 

41 """ 

42 proc = psutil.Process() 

43 usage_main = proc.memory_info().rss * u.byte 

44 usage_child = sum([child.memory_info().rss for child in proc.children()]) * u.byte 

45 return usage_main, usage_child 

46 

47 

48def get_peak_mem_usage() -> Tuple[u.Quantity, u.Quantity]: 

49 """Report peak memory usage. 

50 

51 Returns 

52 ------- 

53 peak_main: `astropy.units.Quantity` 

54 Peak memory usage (maximum resident set size) of the calling process. 

55 peak_child: `astropy.units.Quantity` 

56 Peak memory usage (resident set size) of the largest child process. 

57 

58 Notes 

59 ----- 

60 Function reports peak memory usage using the maximum resident set size as 

61 a proxy. As such the value it reports is capped at available physical RAM 

62 and may not reflect the actual maximal value. 

63 """ 

64 # Units getrusage(2) uses to report the maximum resident set size are 

65 # platform dependent (kilobytes on Linux, bytes on OSX). 

66 unit = u.kibibyte if platform.system() == "Linux" else u.byte 

67 

68 peak_main = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss * unit 

69 peak_child = resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss * unit 

70 return peak_main, peak_child