Skip to content

Commit

Permalink
Disable memory consumption watcher by default
Browse files Browse the repository at this point in the history
  • Loading branch information
Remi Hakim committed May 15, 2013
1 parent 8c66eaa commit f2304b9
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 7 deletions.
3 changes: 2 additions & 1 deletion agent.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,8 @@ def _get_emitters(self, agentConfig):
def _get_watchdog(self, check_freq, agentConfig):
watchdog = None
if agentConfig.get("watchdog", True):
watchdog = Watchdog(check_freq * WATCHDOG_MULTIPLIER)
watchdog = Watchdog(check_freq * WATCHDOG_MULTIPLIER,
max_mem_mb=agentConfig.get('limit_memory_consumption', None))
watchdog.reset()
return watchdog

Expand Down
6 changes: 6 additions & 0 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,12 @@ def get_config(parse_args=True, cfg_path=None, options=None):
for key, value in config.items('WMI'):
agentConfig['WMI'][key] = value

if config.has_option("Main", "limit_memory_consumption") and \
config.get("Main", "limit_memory_consumption") is not None:

This comment has been minimized.

Copy link
@olidb2

olidb2 May 16, 2013

Member

Maybe add a method for that, since it seems useful just about everywhere?

agentConfig["limit_memory_consumption"] = int(config.get("Main", "limit_memory_consumption"))
else:
agentConfig["limit_memory_consumption"] = None

except ConfigParser.NoSectionError, e:
sys.stderr.write('Config file not found or incorrectly formatted.\n')
sys.exit(2)
Expand Down
3 changes: 2 additions & 1 deletion ddagent.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,8 @@ def __init__(self, port, agentConfig, watchdog=True):
self._watchdog = None
if watchdog:
watchdog_timeout = TRANSACTION_FLUSH_INTERVAL * WATCHDOG_INTERVAL_MULTIPLIER
self._watchdog = Watchdog(watchdog_timeout)
self._watchdog = Watchdog(watchdog_timeout,
max_mem_mb=agentConfig.get('limit_memory_consumption', None))

def log_request(self, handler):
""" Override the tornado logging method.
Expand Down
14 changes: 9 additions & 5 deletions util.py
Original file line number Diff line number Diff line change
Expand Up @@ -258,17 +258,21 @@ class Watchdog(object):
Can only be invoked once per process, so don't use with multiple threads.
If you instantiate more than one, you're also asking for trouble.
"""
def __init__(self, duration, max_mem_mb = 2000):
def __init__(self, duration, max_mem_mb = None):
import resource

#Set the duration
self._duration = int(duration)
signal.signal(signal.SIGALRM, Watchdog.self_destruct)

# cap memory usage
self._max_mem_kb = 1024 * max_mem_mb
max_mem_bytes = 1024 * self._max_mem_kb
resource.setrlimit(resource.RLIMIT_AS, (max_mem_bytes, max_mem_bytes))
if max_mem_mb is not None:
self._max_mem_kb = 1024 * max_mem_mb
max_mem_bytes = 1024 * self._max_mem_kb
resource.setrlimit(resource.RLIMIT_AS, (max_mem_bytes, max_mem_bytes))
self.memory_limit_enabled = True
else:
self.memory_limit_enabled = False

@staticmethod
def self_destruct(signum, frame):
Expand All @@ -283,7 +287,7 @@ def self_destruct(signum, frame):
def reset(self):
# self destruct if using too much memory, as tornado will swallow MemoryErrors
mem_usage_kb = int(os.popen('ps -p %d -o %s | tail -1' % (os.getpid(), 'rss')).read())
if mem_usage_kb > (0.95 * self._max_mem_kb):
if self.memory_limit_enabled and mem_usage_kb > (0.95 * self._max_mem_kb):
Watchdog.self_destruct(signal.SIGKILL, sys._getframe(0))

log.debug("Resetting watchdog for %d" % self._duration)
Expand Down

0 comments on commit f2304b9

Please sign in to comment.