weasel-the-memory-watcher 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
data/.document ADDED
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
data/.rvmrc ADDED
@@ -0,0 +1 @@
1
+ rvm use ruby-1.8.7-p299@memory_watch --create
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ # A sample Gemfile
2
+ source "http://rubygems.org"
3
+ gem 'wrong'
4
+ gem 'jeweler'
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Curtis Schofield
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.rdoc ADDED
@@ -0,0 +1,17 @@
1
+ = memory_watch
2
+
3
+ Description goes here.
4
+
5
+ == Note on Patches/Pull Requests
6
+
7
+ * Fork the project.
8
+ * Make your feature addition or bug fix.
9
+ * Add tests for it. This is important so I don't break it in a
10
+ future version unintentionally.
11
+ * Commit, do not mess with rakefile, version, or history.
12
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
13
+ * Send me a pull request. Bonus points for topic branches.
14
+
15
+ == Copyright
16
+
17
+ Copyright (c) 2010 Curtis Schofield. See LICENSE for details.
data/Rakefile ADDED
@@ -0,0 +1,58 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "weasel-the-memory-watcher"
8
+ gem.summary = %Q{Weasel : The memory wacher}
9
+ gem.description = %Q{Dedicated to my dead cat and goodreads.com}
10
+ gem.email = "github.com@robotarmyma.de"
11
+ gem.homepage = "http://github.com/robotarmy/weasel"
12
+ gem.authors = ["Curtis Schofield", "www.goodreads.com"]
13
+ gem.add_development_dependency "bundler", ">= 0"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ Jeweler::GemcutterTasks.new
17
+ rescue LoadError
18
+ puts "Jeweler (or a dependency) not available. Install it with: gem install jeweler"
19
+ end
20
+
21
+ require 'rake/testtask'
22
+ Rake::TestTask.new(:test) do |test|
23
+ test.libs << 'lib' << 'test'
24
+ test.pattern = 'test/**/test_*.rb'
25
+ test.verbose = true
26
+ end
27
+
28
+ begin
29
+ require 'rcov/rcovtask'
30
+ Rcov::RcovTask.new do |test|
31
+ test.libs << 'test'
32
+ test.pattern = 'test/**/test_*.rb'
33
+ test.verbose = true
34
+ end
35
+ rescue LoadError
36
+ task :rcov do
37
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
38
+ end
39
+ end
40
+ task :check_dependencies do
41
+ puts '..bundler'
42
+ `bundle check || bundle install`
43
+ end
44
+
45
+ task :test => :check_dependencies
46
+
47
+
48
+ task :default => :test
49
+
50
+ require 'rake/rdoctask'
51
+ Rake::RDocTask.new do |rdoc|
52
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
53
+
54
+ rdoc.rdoc_dir = 'rdoc'
55
+ rdoc.title = "weasel #{version}"
56
+ rdoc.rdoc_files.include('README*')
57
+ rdoc.rdoc_files.include('lib/**/*.rb')
58
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 1.0.0
data/bin/weasel ADDED
@@ -0,0 +1,39 @@
1
+ #!/bin/sh
2
+ exec ruby -x "$0" "$@"
3
+ #!ruby -w
4
+ require 'weasel'
5
+ if __FILE__ == $0
6
+ require 'optparse'
7
+ options = {}
8
+ OptionParser.new do |opts|
9
+ opts.banner = "Usage: #{__FILE__} [options]"
10
+
11
+ opts.on("--callback rubycode",String, "ruby code to exectute - has \#{pid} avalible") do |v|
12
+ options[:callback] = lambda {|pid| # may be evil
13
+ eval(v)
14
+ }
15
+ end
16
+
17
+ opts.on("--num_over_marks N",String, "how many times the proces memory must go over to be a candidate") do |v|
18
+ options[:num_over_marks] = v.to_i
19
+ end
20
+
21
+ opts.on("--high_water_mb N", "the number of megabytes to be considered 'too much'") do |v|
22
+ options[:high_water_mb] = v.to_i
23
+ end
24
+
25
+ opts.on("--watch S",String, "process identifying string") do |v|
26
+ options[:watch] = v
27
+ end
28
+
29
+ opts.on("--num_cycles N", "total executions before exit") do |v|
30
+ options[:num_cycles] = v.to_i
31
+ end
32
+
33
+ opts.on("--delay F",Float, "delay between cycles") do |v|
34
+ options[:delay] = v
35
+ end
36
+
37
+ end.parse!
38
+ MemoryWatch.new(options).cycle
39
+ end
@@ -0,0 +1,46 @@
1
+
2
+ class MemoryWatch
3
+ attr_accessor :watch_string,:high_water_pids,:callback,:high_water_mb,:delay,:num_cycles,:num_over_marks,:pids
4
+ def initialize(options)
5
+ self.callback = options[:callback] || lambda {|pid| p pid}
6
+ self.watch_string = (options[:watch] || "this poem is a pomme").strip
7
+ self.delay = options[:delay] || 60
8
+ self.num_cycles = options[:num_cycles] || 3
9
+ self.num_over_marks = options[:num_over_marks] || 2
10
+ self.high_water_mb = options[:high_water_mb] || 700
11
+ self.high_water_pids = {}
12
+ end
13
+ public
14
+ def cycle
15
+ self.num_cycles.times do
16
+ _check_pids
17
+ sleep self.delay
18
+ end
19
+ _trigger_callback
20
+ end
21
+
22
+ private
23
+ def _trigger_callback
24
+ self.high_water_pids.each do |pid,over_marks|
25
+ if over_marks.size >= self.num_over_marks
26
+ self.callback.call(pid)
27
+ end
28
+ end
29
+ end
30
+ def _run
31
+ p cmd = "ps x |grep '#{self.watch_string}' |grep -v grep | awk '{print $1}'"
32
+ self.pids = %x{#{cmd}}.split()
33
+ self.pids
34
+ end
35
+ def _check_pids
36
+ _run.each { |pid|
37
+ p memory_usage = %x{ps -o rss= -p #{pid}}.to_i # KB
38
+ if memory_usage > self.high_water_mb * 1024 # 750MB
39
+ puts "WARNING - Process #{pid} hit high water mark"
40
+ self.high_water_pids[pid] ||= []
41
+ self.high_water_pids[pid] << memory_usage
42
+ end
43
+ }
44
+ end
45
+ end
46
+
data/lib/weasel.rb ADDED
@@ -0,0 +1 @@
1
+ require 'memory_watch'
data/test/helper.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require "bundler/setup"
4
+ require 'wrong/adapters/test_unit'
5
+
6
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
7
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
8
+ require 'memory_watch'
9
+
10
+ class Test::Unit::TestCase
11
+ end
@@ -0,0 +1,32 @@
1
+ require 'helper'
2
+ class TestMemoryWatch < Test::Unit::TestCase
3
+ def test_watches_self
4
+ cmd = %q{ruby ./lib/memory_watch.rb --delay 999999 --num_cycles 9900000099}
5
+ pid = fork { %x{#{cmd}} }
6
+ Process.detach(pid)
7
+ wm = MemoryWatch.new(:watch => cmd , :high_water_mb => 0.01,:delay => 0.1, :num_cycles => 1)
8
+ wm.cycle
9
+ assert {
10
+ wm.high_water_pids.size == 1
11
+ }
12
+ wm.pids.each do |pid|
13
+ %x{kill -9 #{pid}}
14
+ end
15
+ end
16
+ def test_runs_callback
17
+ test_magiggy = false
18
+ wm = MemoryWatch.new(:watch => File.basename(__FILE__),
19
+ :high_water_mb => 0,
20
+ :num_cycles => 1,
21
+ :delay => 0.01,
22
+ :num_over_marks => 0,
23
+ :callback => lambda {|pid|
24
+ test_magiggy = pid
25
+ })
26
+ wm.cycle
27
+ assert {
28
+ wm.high_water_pids.include? test_magiggy
29
+ }
30
+ end
31
+ end
32
+
metadata ADDED
@@ -0,0 +1,122 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: weasel-the-memory-watcher
3
+ version: !ruby/object:Gem::Version
4
+ hash: 23
5
+ prerelease: false
6
+ segments:
7
+ - 1
8
+ - 0
9
+ - 0
10
+ version: 1.0.0
11
+ platform: ruby
12
+ authors:
13
+ - Curtis Schofield
14
+ - www.goodreads.com
15
+ autorequire:
16
+ bindir: bin
17
+ cert_chain: []
18
+
19
+ date: 2010-11-19 00:00:00 -08:00
20
+ default_executable: weasel
21
+ dependencies:
22
+ - !ruby/object:Gem::Dependency
23
+ type: :runtime
24
+ prerelease: false
25
+ name: wrong
26
+ version_requirements: &id001 !ruby/object:Gem::Requirement
27
+ none: false
28
+ requirements:
29
+ - - ">="
30
+ - !ruby/object:Gem::Version
31
+ hash: 3
32
+ segments:
33
+ - 0
34
+ version: "0"
35
+ requirement: *id001
36
+ - !ruby/object:Gem::Dependency
37
+ type: :runtime
38
+ prerelease: false
39
+ name: jeweler
40
+ version_requirements: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ hash: 3
46
+ segments:
47
+ - 0
48
+ version: "0"
49
+ requirement: *id002
50
+ - !ruby/object:Gem::Dependency
51
+ type: :development
52
+ prerelease: false
53
+ name: bundler
54
+ version_requirements: &id003 !ruby/object:Gem::Requirement
55
+ none: false
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ hash: 3
60
+ segments:
61
+ - 0
62
+ version: "0"
63
+ requirement: *id003
64
+ description: Dedicated to my dead cat and goodreads.com
65
+ email: github.com@robotarmyma.de
66
+ executables:
67
+ - weasel
68
+ extensions: []
69
+
70
+ extra_rdoc_files:
71
+ - LICENSE
72
+ - README.rdoc
73
+ files:
74
+ - .document
75
+ - .rvmrc
76
+ - Gemfile
77
+ - LICENSE
78
+ - README.rdoc
79
+ - Rakefile
80
+ - VERSION
81
+ - bin/weasel
82
+ - lib/memory_watch.rb
83
+ - lib/weasel.rb
84
+ - test/helper.rb
85
+ - test/test_memory_watch.rb
86
+ has_rdoc: true
87
+ homepage: http://github.com/robotarmy/weasel
88
+ licenses: []
89
+
90
+ post_install_message:
91
+ rdoc_options: []
92
+
93
+ require_paths:
94
+ - lib
95
+ required_ruby_version: !ruby/object:Gem::Requirement
96
+ none: false
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ hash: 3
101
+ segments:
102
+ - 0
103
+ version: "0"
104
+ required_rubygems_version: !ruby/object:Gem::Requirement
105
+ none: false
106
+ requirements:
107
+ - - ">="
108
+ - !ruby/object:Gem::Version
109
+ hash: 3
110
+ segments:
111
+ - 0
112
+ version: "0"
113
+ requirements: []
114
+
115
+ rubyforge_project:
116
+ rubygems_version: 1.3.7
117
+ signing_key:
118
+ specification_version: 3
119
+ summary: "Weasel : The memory wacher"
120
+ test_files:
121
+ - test/helper.rb
122
+ - test/test_memory_watch.rb