-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Put memory measuring functions in a new module
Moved functions related to measuring memory consumption in a separate module lsst.utils.usage.
- Loading branch information
Showing
6 changed files
with
194 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
# This file is part of utils. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (https://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# Use of this source code is governed by a 3-clause BSD-style | ||
# license that can be found in the LICENSE file. | ||
|
||
"""Utilities for measuring resource consumption. | ||
""" | ||
|
||
import platform | ||
import resource | ||
from typing import Tuple | ||
|
||
import psutil | ||
from astropy import units as u | ||
|
||
__all__ = ["get_current_mem_usage", "get_peak_mem_usage"] | ||
|
||
|
||
def get_current_mem_usage() -> Tuple[u.Quantity, u.Quantity]: | ||
"""Report current memory usage. | ||
Returns | ||
------- | ||
usage_main : `astropy.units.Quantity` | ||
Current memory usage of the calling process expressed in bytes. | ||
usage_child : `astropy.units.Quantity` | ||
Current memory usage of the child processes (zero if there are none) | ||
expressed in bytes. | ||
Notes | ||
----- | ||
Function reports current memory usage using resident set size as a proxy. | ||
As such the values it reports are capped at available physical RAM and may | ||
not reflect the actual memory allocated to the process and its children. | ||
""" | ||
proc = psutil.Process() | ||
usage_main = proc.memory_info().rss * u.byte | ||
usage_child = sum([child.memory_info().rss for child in proc.children()]) * u.byte | ||
return usage_main, usage_child | ||
|
||
|
||
def get_peak_mem_usage() -> Tuple[u.Quantity, u.Quantity]: | ||
"""Report peak memory usage. | ||
Returns | ||
------- | ||
peak_main: `astropy.units.Quantity` | ||
Peak memory usage (maximum resident set size) of the calling process, | ||
in bytes. | ||
peak_child: `astropy.units.Quantity` | ||
Peak memory usage (resident set size) of the largest child process, | ||
in bytes. | ||
Notes | ||
----- | ||
Function reports peak memory usage using the maximum resident set size as | ||
a proxy. As such the value it reports is capped at available physical RAM | ||
and may not reflect the actual maximal value. | ||
""" | ||
# Units getrusage(2) uses to report the maximum resident set size are | ||
# platform dependent (kilobytes on Linux, bytes on OSX). | ||
unit = u.kibibyte if platform.system() == "Linux" else u.byte | ||
|
||
peak_main = resource.getrusage(resource.RUSAGE_SELF).ru_maxrss * unit | ||
peak_child = resource.getrusage(resource.RUSAGE_CHILDREN).ru_maxrss * unit | ||
return peak_main.to(u.byte), peak_child.to(u.byte) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,3 +2,4 @@ numpy >= 1.17 | |
psutil >= 5.7 | ||
deprecated >= 1.2 | ||
pyyaml >5.1 | ||
astropy >= 5.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
# This file is part of utils. | ||
# | ||
# Developed for the LSST Data Management System. | ||
# This product includes software developed by the LSST Project | ||
# (https://www.lsst.org). | ||
# See the COPYRIGHT file at the top-level directory of this distribution | ||
# for details of code ownership. | ||
# | ||
# Use of this source code is governed by a 3-clause BSD-style | ||
# license that can be found in the LICENSE file. | ||
|
||
import unittest | ||
|
||
from astropy import units as u | ||
from lsst.utils.usage import get_current_mem_usage, get_peak_mem_usage | ||
|
||
|
||
class UsageTestCase(unittest.TestCase): | ||
def testGetCurrentMemUsage(self): | ||
main1, children1 = get_current_mem_usage() | ||
self.assertGreater(main1, 0 * u.byte) | ||
self.assertGreaterEqual(children1.value, 0) | ||
|
||
self.assertEqual(main1.unit.name, "byte") | ||
self.assertEqual(children1.unit.name, "byte") | ||
|
||
# Allocate some memory. | ||
arr = [None] * 1_000_000 # noqa: F841 | ||
|
||
main2, children2 = get_current_mem_usage() | ||
self.assertGreater(main2, main1) | ||
self.assertGreaterEqual(children2, children1) | ||
|
||
def testGetPeakMemUsage(self): | ||
main1, child1 = get_peak_mem_usage() | ||
self.assertGreater(main1, 0 * u.byte) | ||
self.assertGreaterEqual(child1, 0 * u.byte) | ||
|
||
self.assertEqual(main1.unit.name, "byte") | ||
self.assertEqual(child1.unit.name, "byte") | ||
|
||
# Allocate some memory. | ||
arr = [None] * 2_000_000 # noqa: F841 | ||
|
||
main2, child2 = get_peak_mem_usage() | ||
self.assertGreater(main2, main1) | ||
self.assertGreaterEqual(child2, child1) |