memory_monitor 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +7 -0
- data/Gemfile +4 -0
- data/LICENSE +21 -0
- data/README.md +58 -0
- data/Rakefile +6 -0
- data/bin/memory_monitor +35 -0
- data/lib/memory_monitor.rb +89 -0
- data/lib/memory_monitor/version.rb +3 -0
- data/memory_monitor.gemspec +26 -0
- metadata +97 -0
checksums.yaml
ADDED
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
---
|
|
2
|
+
SHA1:
|
|
3
|
+
metadata.gz: a4f3b3846408e492e0139a3e62e08a15d92778f9
|
|
4
|
+
data.tar.gz: 4e4b2e68dbae6f5725f5bc451f9fc58cc1c5a179
|
|
5
|
+
SHA512:
|
|
6
|
+
metadata.gz: 66d6f6b773d4297d3210e4e89370f84681f382f0a6668ead38d81e5ab2a6e78aabaff00b8f613bedd94a76af8600118be11f957e05f5f154cd43b9cfe2a14777
|
|
7
|
+
data.tar.gz: c4c3f92ee07b84939cb2a073cef23144ef44f68e3d886f89bd43edc2c0ea7665ed9a26488f27d5098c62ace43cea8c6d4f87c2e53c6a9061e5b727e1c39fddf8
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2016 Loco2 Ltd.
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
data/README.md
ADDED
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
# `memory_monitor`
|
|
2
|
+
|
|
3
|
+
This program is used to run a command with a memory limit.
|
|
4
|
+
|
|
5
|
+
If the command exceeds the memory limit, it will receive a `SIGTERM` to
|
|
6
|
+
exit gracefully. If it fails to exit gracefully within a timeout, it
|
|
7
|
+
will receive a `SIGKILL` to exit ungracefully.
|
|
8
|
+
|
|
9
|
+
The memory measurement used is [*resident set
|
|
10
|
+
size*](https://en.wikichip.org/wiki/resident_set_size).
|
|
11
|
+
|
|
12
|
+
## Example
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
```
|
|
16
|
+
$ bin/memory_monitor --limit 20 --interval 0.1 --timeout 0.1 ruby -e 'trap("TERM", "IGNORE"); a=[]; loop { a << "foo" * 500000; puts "hi"; sleep 0.01 }'
|
|
17
|
+
hi
|
|
18
|
+
hi
|
|
19
|
+
hi
|
|
20
|
+
hi
|
|
21
|
+
hi
|
|
22
|
+
hi
|
|
23
|
+
hi
|
|
24
|
+
[memory_monitor] process memory 22.0MB exceeded limit 20MB, sending SIGTERM
|
|
25
|
+
hi
|
|
26
|
+
hi
|
|
27
|
+
hi
|
|
28
|
+
hi
|
|
29
|
+
hi
|
|
30
|
+
hi
|
|
31
|
+
hi
|
|
32
|
+
hi
|
|
33
|
+
hi
|
|
34
|
+
[memory_monitor] process failed to stop after 0.1s, sending SIGKILL
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
## Intended use
|
|
38
|
+
|
|
39
|
+
We are using this to monitor our Docker container processes to ensure
|
|
40
|
+
they get restarted after a memory leak occurs. Within the container, we
|
|
41
|
+
simply want the program to exit when its memory usage is too high. The
|
|
42
|
+
container orchestration system will then notice that the container has
|
|
43
|
+
died, and start a new one.
|
|
44
|
+
|
|
45
|
+
## Treatment of subprocesses
|
|
46
|
+
|
|
47
|
+
If the command spawns any subprocesses, they will also be checked. We
|
|
48
|
+
don't check the total memory usage of the process and its subprocesses,
|
|
49
|
+
we check each one individually. If either the main process or any of its
|
|
50
|
+
subprocesses exceeds the limit, the main process gets killed.
|
|
51
|
+
(Signalling individual subprocesses while keeping the main process alive
|
|
52
|
+
would be more complex and is not worth it.)
|
|
53
|
+
|
|
54
|
+
## Bugs/features
|
|
55
|
+
|
|
56
|
+
* Memory usage is measured by shelling out to the `ps` command; the
|
|
57
|
+
current implementation may be incompatible with some systems.
|
|
58
|
+
* Tested with Ruby 2.3, but may work on prior versions too.
|
data/Rakefile
ADDED
data/bin/memory_monitor
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env ruby
|
|
2
|
+
|
|
3
|
+
require "optparse"
|
|
4
|
+
require_relative "../lib/memory_monitor"
|
|
5
|
+
|
|
6
|
+
options = {}
|
|
7
|
+
|
|
8
|
+
prev = nil
|
|
9
|
+
args = ARGV.take_while { |arg|
|
|
10
|
+
val = arg.start_with?("-") || prev && prev.start_with?("-")
|
|
11
|
+
prev = arg
|
|
12
|
+
val
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
unless args.include?("-l") || args.include?("--limit")
|
|
16
|
+
args = ["--help"]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
OptionParser.new { |opts|
|
|
20
|
+
opts.banner = "Usage: #{$0} [OPTIONS] [COMMAND]\n\n"
|
|
21
|
+
|
|
22
|
+
opts.on("-l", "--limit LIMIT", "Memory limit for any one process (MB, required)") do |v|
|
|
23
|
+
options[:limit] = v.to_i
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
opts.on("-t", "--timeout TIMEOUT", "Timeout (seconds, default: 2)") do |v|
|
|
27
|
+
options[:timeout] = v.to_f
|
|
28
|
+
end
|
|
29
|
+
|
|
30
|
+
opts.on("-i", "--interval INTERVAL", "Memory check interval (seconds, default: 1)") do |v|
|
|
31
|
+
options[:interval] = v.to_f
|
|
32
|
+
end
|
|
33
|
+
}.parse(args)
|
|
34
|
+
|
|
35
|
+
exit MemoryMonitor.run(ARGV.drop(args.length), options)
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
require_relative "memory_monitor/version"
|
|
2
|
+
|
|
3
|
+
class MemoryMonitor
|
|
4
|
+
attr_reader :command, :limit, :timeout, :interval, :pid, :pgid,
|
|
5
|
+
:memory_in_kilobytes, :memory_in_megabytes
|
|
6
|
+
|
|
7
|
+
SIGNALS = %w(HUP INT QUIT USR1 USR2 TERM)
|
|
8
|
+
|
|
9
|
+
def self.run(*args)
|
|
10
|
+
new(*args).run
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
def initialize(command, limit:, timeout: 2, interval: 1)
|
|
14
|
+
@command = command
|
|
15
|
+
@limit = limit
|
|
16
|
+
@timeout = timeout
|
|
17
|
+
@interval = interval
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
def run
|
|
21
|
+
forward_signals
|
|
22
|
+
|
|
23
|
+
@pid = Process.spawn(*command, pgroup: true)
|
|
24
|
+
@pgid = Process.getpgid(pid)
|
|
25
|
+
|
|
26
|
+
Thread.abort_on_exception = true
|
|
27
|
+
Thread.new { monitor }
|
|
28
|
+
|
|
29
|
+
Process.wait pid
|
|
30
|
+
|
|
31
|
+
if $?.success?
|
|
32
|
+
$?.exitstatus
|
|
33
|
+
else
|
|
34
|
+
false
|
|
35
|
+
end
|
|
36
|
+
end
|
|
37
|
+
|
|
38
|
+
def limit_in_kilobytes
|
|
39
|
+
limit * 1024
|
|
40
|
+
end
|
|
41
|
+
|
|
42
|
+
def monitor
|
|
43
|
+
loop do
|
|
44
|
+
@memory_in_kilobytes =
|
|
45
|
+
`ps -o pgrp= -o rss=`
|
|
46
|
+
.split("\n")
|
|
47
|
+
.map { |line| line.split(" ").map(&:to_i) }
|
|
48
|
+
.select { |pgid, _| pgid == self.pgid }
|
|
49
|
+
.map { |_, size| size }
|
|
50
|
+
.max
|
|
51
|
+
|
|
52
|
+
@memory_in_megabytes = (memory_in_kilobytes / 1024).round(1)
|
|
53
|
+
|
|
54
|
+
if memory_in_kilobytes > limit_in_kilobytes
|
|
55
|
+
term
|
|
56
|
+
sleep timeout
|
|
57
|
+
kill
|
|
58
|
+
end
|
|
59
|
+
|
|
60
|
+
sleep interval
|
|
61
|
+
end
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def term
|
|
65
|
+
log "process memory #{memory_in_megabytes}MB exceeded limit #{limit}MB, sending SIGTERM"
|
|
66
|
+
Process.kill("TERM", -pgid)
|
|
67
|
+
end
|
|
68
|
+
|
|
69
|
+
def kill
|
|
70
|
+
log "process failed to stop after #{timeout}s, sending SIGKILL"
|
|
71
|
+
Process.kill("KILL", -pgid)
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def log(message)
|
|
75
|
+
$stderr.puts "[memory_monitor] #{message}"
|
|
76
|
+
end
|
|
77
|
+
|
|
78
|
+
def forward_signals
|
|
79
|
+
SIGNALS.each do |signal|
|
|
80
|
+
Signal.trap(signal) do
|
|
81
|
+
begin
|
|
82
|
+
Process.kill(signal, pid) if pid
|
|
83
|
+
rescue Errno::ESRCH
|
|
84
|
+
# pid doesn't exist
|
|
85
|
+
end
|
|
86
|
+
end
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
# coding: utf-8
|
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
|
4
|
+
require 'memory_monitor/version'
|
|
5
|
+
|
|
6
|
+
Gem::Specification.new do |spec|
|
|
7
|
+
spec.name = "memory_monitor"
|
|
8
|
+
spec.version = MemoryMonitor::VERSION
|
|
9
|
+
spec.authors = ["Jon Leighton"]
|
|
10
|
+
spec.email = ["jon@loco2.com"]
|
|
11
|
+
|
|
12
|
+
spec.summary = %q{Restart a process when memory usage is too high}
|
|
13
|
+
spec.description = %q{Restart a process when memory usage is too high}
|
|
14
|
+
spec.homepage = "https://github.com/loco2/memory_monitor"
|
|
15
|
+
|
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject do |f|
|
|
17
|
+
f.match(%r{^(test|spec|features)/})
|
|
18
|
+
end
|
|
19
|
+
spec.bindir = "bin"
|
|
20
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
|
21
|
+
spec.require_paths = ["lib"]
|
|
22
|
+
|
|
23
|
+
spec.add_development_dependency "bundler", "~> 1.13"
|
|
24
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
|
25
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
|
26
|
+
end
|
metadata
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
|
2
|
+
name: memory_monitor
|
|
3
|
+
version: !ruby/object:Gem::Version
|
|
4
|
+
version: 0.1.0
|
|
5
|
+
platform: ruby
|
|
6
|
+
authors:
|
|
7
|
+
- Jon Leighton
|
|
8
|
+
autorequire:
|
|
9
|
+
bindir: bin
|
|
10
|
+
cert_chain: []
|
|
11
|
+
date: 2016-11-10 00:00:00.000000000 Z
|
|
12
|
+
dependencies:
|
|
13
|
+
- !ruby/object:Gem::Dependency
|
|
14
|
+
name: bundler
|
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
|
16
|
+
requirements:
|
|
17
|
+
- - "~>"
|
|
18
|
+
- !ruby/object:Gem::Version
|
|
19
|
+
version: '1.13'
|
|
20
|
+
type: :development
|
|
21
|
+
prerelease: false
|
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
23
|
+
requirements:
|
|
24
|
+
- - "~>"
|
|
25
|
+
- !ruby/object:Gem::Version
|
|
26
|
+
version: '1.13'
|
|
27
|
+
- !ruby/object:Gem::Dependency
|
|
28
|
+
name: rake
|
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
|
30
|
+
requirements:
|
|
31
|
+
- - "~>"
|
|
32
|
+
- !ruby/object:Gem::Version
|
|
33
|
+
version: '10.0'
|
|
34
|
+
type: :development
|
|
35
|
+
prerelease: false
|
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
37
|
+
requirements:
|
|
38
|
+
- - "~>"
|
|
39
|
+
- !ruby/object:Gem::Version
|
|
40
|
+
version: '10.0'
|
|
41
|
+
- !ruby/object:Gem::Dependency
|
|
42
|
+
name: rspec
|
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
|
44
|
+
requirements:
|
|
45
|
+
- - "~>"
|
|
46
|
+
- !ruby/object:Gem::Version
|
|
47
|
+
version: '3.0'
|
|
48
|
+
type: :development
|
|
49
|
+
prerelease: false
|
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
|
51
|
+
requirements:
|
|
52
|
+
- - "~>"
|
|
53
|
+
- !ruby/object:Gem::Version
|
|
54
|
+
version: '3.0'
|
|
55
|
+
description: Restart a process when memory usage is too high
|
|
56
|
+
email:
|
|
57
|
+
- jon@loco2.com
|
|
58
|
+
executables:
|
|
59
|
+
- memory_monitor
|
|
60
|
+
extensions: []
|
|
61
|
+
extra_rdoc_files: []
|
|
62
|
+
files:
|
|
63
|
+
- ".gitignore"
|
|
64
|
+
- ".rspec"
|
|
65
|
+
- ".travis.yml"
|
|
66
|
+
- Gemfile
|
|
67
|
+
- LICENSE
|
|
68
|
+
- README.md
|
|
69
|
+
- Rakefile
|
|
70
|
+
- bin/memory_monitor
|
|
71
|
+
- lib/memory_monitor.rb
|
|
72
|
+
- lib/memory_monitor/version.rb
|
|
73
|
+
- memory_monitor.gemspec
|
|
74
|
+
homepage: https://github.com/loco2/memory_monitor
|
|
75
|
+
licenses: []
|
|
76
|
+
metadata: {}
|
|
77
|
+
post_install_message:
|
|
78
|
+
rdoc_options: []
|
|
79
|
+
require_paths:
|
|
80
|
+
- lib
|
|
81
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
|
82
|
+
requirements:
|
|
83
|
+
- - ">="
|
|
84
|
+
- !ruby/object:Gem::Version
|
|
85
|
+
version: '0'
|
|
86
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
87
|
+
requirements:
|
|
88
|
+
- - ">="
|
|
89
|
+
- !ruby/object:Gem::Version
|
|
90
|
+
version: '0'
|
|
91
|
+
requirements: []
|
|
92
|
+
rubyforge_project:
|
|
93
|
+
rubygems_version: 2.5.1
|
|
94
|
+
signing_key:
|
|
95
|
+
specification_version: 4
|
|
96
|
+
summary: Restart a process when memory usage is too high
|
|
97
|
+
test_files: []
|