proceso 0.1.1 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 5ba2fac203e9bf0d1bec8ad20defc3ff07582c5f
4
- data.tar.gz: a40f55f136b868bd641129c5c8680feb2e3855d6
3
+ metadata.gz: a49a83d9db144438ccca258b165644e93e14183a
4
+ data.tar.gz: 93e182fa67298287a5402c2a553376c5d1c07f6e
5
5
  SHA512:
6
- metadata.gz: c9d453e1d265a4d3625328733ceadc4cbc26a34cd22c90cf131512ca1f57caf5fcabb54da937953a4c14802c17221f59f149bd588249541a3ebaa02b4c548dfd
7
- data.tar.gz: d914eddb8fe44c1fdafaf6c73b689481f7a94c9b06b26e1ee66d738d4120dc910958b227227a4a7b528e4994abb7f06a0d593a35874dc35a24e17c949b16872a
6
+ metadata.gz: 93e7588f3a630b729754c764fe238307353c8401f6de24575a19b12082821a91be4c9e67bbc4bd0b123e601e07373884a0a622084a270d71c507087d5dcf7757
7
+ data.tar.gz: 9c56ba0567d6901430dd26fee225b93fef0fe93a7bdc3445d1c210011725bb2b13e113781f6b7368e621bb587ff77bdeea5094fcd5e63b46a9f7744cea21fde0
data/ext/proceso/darwin.c CHANGED
@@ -37,33 +37,4 @@ int rb_process_info(int pid, int flavor, void *pti, int size) {
37
37
  }
38
38
  }
39
39
 
40
- /* List of Processes */
41
- int * rb_process_list() {
42
- int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_ALL, 0};
43
- size_t buf_size;
44
- int ret;
45
- int list[4096];
46
- ret = sysctl(mib, 4, NULL, &buf_size, NULL, 0);
47
- if (ret >= 0) {
48
- struct kinfo_proc *processes = NULL;
49
- int i, nb_entries;
50
- nb_entries = buf_size / sizeof(struct kinfo_proc);
51
- processes = (struct kinfo_proc*) malloc(buf_size);
52
- if (processes != NULL) {
53
- ret = sysctl(mib, 4, processes, &buf_size, NULL, 0);
54
- if (ret >= 0) {
55
- for (i = 0; i < nb_entries; i++) {
56
- int pid = processes[i].kp_proc.p_pid;
57
- list[i] = pid;
58
- }
59
- free(processes);
60
- return list;
61
- }
62
- free(processes);
63
- return 0;
64
- }
65
- }
66
- return 0;
67
- }
68
-
69
40
  #endif
data/ext/proceso/darwin.h CHANGED
@@ -14,5 +14,4 @@
14
14
 
15
15
  int rb_sysctl_kern_argmax();
16
16
  int rb_sysctl_kern_procargs2(int pid, char **process_name, int argmax, size_t buf_size);
17
- int rb_process_info(int pid, int flavor, void *pti, int size);
18
- int * rb_process_list();
17
+ int rb_process_info(int pid, int flavor, void *pti, int size);
data/ext/proceso/pid.c CHANGED
@@ -57,19 +57,6 @@ proceso__process_system_cpu(VALUE self) {
57
57
  return rb_float_new(val);
58
58
  }
59
59
 
60
- /* PID CPU Usage */
61
- static VALUE
62
- proceso__process_cpu_usage(VALUE self) {
63
- int ncpu = rb_hw_ncpu();
64
- float u1, u2;
65
- float usage;
66
- u1 = rb_process_cpu_times(iv2pid(self), FCPU_USR);
67
- usleep(100000);
68
- u2 = rb_process_cpu_times(iv2pid(self), FCPU_USR);
69
- usage = ncpu * ((u2 - u1) * 100);
70
- return rb_float_new(usage);
71
- }
72
-
73
60
  void Init__proceso_pid() {
74
61
 
75
62
  VALUE rb_mProceso = rb_define_module("Proceso");
@@ -82,6 +69,5 @@ void Init__proceso_pid() {
82
69
  rb_define_method(rb_cProcesoPID, "virtual_size", proceso__process_vms, 0);
83
70
  rb_define_method(rb_cProcesoPID, "user_cpu_times", proceso__process_user_cpu, 0);
84
71
  rb_define_method(rb_cProcesoPID, "system_cpu_times", proceso__process_system_cpu, 0);
85
- rb_define_method(rb_cProcesoPID, "cpu_usage", proceso__process_cpu_usage, 0);
86
72
 
87
73
  }
@@ -6,30 +6,12 @@ int iv2pid(VALUE self) {
6
6
  return FIX2INT(rb_iv_get(self, "@pid"));
7
7
  }
8
8
 
9
- #ifdef __APPLE__
10
- /* Process PIDs */
11
- static VALUE
12
- proceso__pids(VALUE self) {
13
- int *pids = rb_process_list();
14
- VALUE processes = rb_ary_new();
15
- int i;
16
- for(i = 0; i < sizeof(pids); i++) {
17
- rb_ary_push(processes, INT2NUM(pids[i]));
18
- }
19
- return processes;
20
- }
21
- #endif
22
-
23
9
  void Init_proceso() {
24
10
 
25
11
  rb_mProceso = rb_define_module("Proceso");
26
12
 
27
13
  rb_define_const(rb_mProceso, "NCPU", INT2NUM(rb_hw_ncpu()));
28
14
 
29
- #ifdef __APPLE__
30
- rb_define_singleton_method(rb_mProceso, "pids", proceso__pids, 0);
31
- #endif
32
-
33
15
  Init__proceso_pid();
34
16
  }
35
17
 
@@ -1,3 +1,3 @@
1
1
  module Proceso
2
- VERSION = "0.1.1"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/proceso.rb CHANGED
@@ -4,26 +4,8 @@ require 'proceso/pid'
4
4
  module Proceso
5
5
  extend self
6
6
 
7
- if RUBY_PLATFORM =~ /linux/
8
- def pids
9
- Dir["/proc/*"].select do
10
- |x| x =~ /\/\d+$/
11
- end.map(&File.method(:basename)).map(&:to_i)
12
- end
13
- end
14
-
15
- def processes
16
- pids.map do |i|
17
- Proceso::PID.new(i)
18
- end
19
- end
20
-
21
7
  def current
22
8
  @current ||= Proceso::PID.new(Process.pid)
23
9
  end
24
10
 
25
11
  end
26
-
27
- if defined?(Rails) || defined?(Rack)
28
- require 'proceso/middleware'
29
- end
@@ -40,12 +40,6 @@ describe Proceso::PID do
40
40
  process.virtual_size.should == 100
41
41
  end
42
42
 
43
- it "should return cpu usage" do
44
- process.cpu_usage.should be_kind_of(Float)
45
- process.stub(:cpu_usage).and_return(0.1)
46
- process.cpu_usage.should == 0.1
47
- end
48
-
49
43
  it "should return user cpu times" do
50
44
  process.user_cpu_times.should be_kind_of(Float)
51
45
  process.stub(:user_cpu_times).and_return(0.1)
data/spec/proceso_spec.rb CHANGED
@@ -6,15 +6,6 @@ describe Proceso do
6
6
  Proceso::NCPU.should_not == 0
7
7
  end
8
8
 
9
- it "should return list of pids" do
10
- Proceso.pids.should be_kind_of(Array)
11
- end
12
-
13
- it "should return list of Proceso::PID objects" do
14
- Proceso.processes.should be_kind_of(Array)
15
- Proceso.processes.last.should be_kind_of(Proceso::PID)
16
- end
17
-
18
9
  it "should return current process" do
19
10
  Proceso.current.should_not be_nil
20
11
  Proceso.current.pid.should == Process.pid
data/spec/spec_helper.rb CHANGED
@@ -2,4 +2,5 @@ require 'bundler/setup'
2
2
  lib_path = File.join(File.dirname(__FILE__), "..", "lib")
3
3
  $LOAD_PATH.unshift(lib_path) unless $LOAD_PATH.include?(lib_path)
4
4
 
5
+ require 'debugger'
5
6
  require "proceso"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: proceso
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bryan Goines
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-12-22 00:00:00.000000000 Z
11
+ date: 2014-05-09 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rack
@@ -88,7 +88,6 @@ extensions:
88
88
  - ext/proceso/extconf.rb
89
89
  extra_rdoc_files: []
90
90
  files:
91
- - lib/proceso/middleware.rb
92
91
  - lib/proceso/pid.rb
93
92
  - lib/proceso/version.rb
94
93
  - lib/proceso.rb
@@ -126,7 +125,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
125
  version: '0'
127
126
  requirements: []
128
127
  rubyforge_project:
129
- rubygems_version: 2.1.11
128
+ rubygems_version: 2.0.14
130
129
  signing_key:
131
130
  specification_version: 4
132
131
  summary: Simple process information gather
@@ -1,71 +0,0 @@
1
- require 'rack'
2
-
3
- module Proceso
4
- class Middleware
5
-
6
- SUBSCRIPTION = 'proceso.usage'
7
-
8
- def self.report
9
- subscribe do |name, start, finish, id, payload|
10
- puts
11
- puts "******* PROCESO INFORMATION *******"
12
- mem_used = (payload[:mem_used].to_f / 1024.0).round(1)
13
- cpu_used = payload[:cpu_used].to_f.round(1)
14
- path = payload[:request].path_info
15
- resp_time = payload[:resp_time]
16
- puts "MEM: #{mem_used}KB CPU: #{cpu_used} RESP: #{resp_time}ms PATH: #{path}"
17
- puts "******* END OF PROCESO INFORMATION *******"
18
- puts
19
- end
20
- end
21
-
22
- def self.subscribe(&blk)
23
- return unless defined?(ActiveSupport::Notifications)
24
- ActiveSupport::Notifications.subscribe(SUBSCRIPTION, &blk)
25
- end
26
-
27
- attr_reader :app, :pid, :notifier
28
-
29
- def initialize(app)
30
- @app = app
31
- @notifier ||= ActiveSupport::Notifications if defined?(ActiveSupport::Notifications)
32
- @pid = Process.pid
33
- end
34
-
35
- def call(env)
36
- measure_process_usage(env) do
37
- @app.call(env)
38
- end
39
- end
40
-
41
- def measure_process_usage(env)
42
- mem_1 = process.mem_size
43
- cpu_1 = process.user_cpu_times
44
- resp_1 = Time.now.to_i
45
- response = yield
46
- mem_2 = process.mem_size
47
- cpu_2 = process.user_cpu_times
48
- resp_2 = Time.now.to_i
49
- process_usage = process_info(env, (mem_2 - mem_1), (cpu_2 - cpu_1), (resp_2 - resp_1))
50
- notifier.instrument(SUBSCRIPTION, process_usage)
51
- response
52
- end
53
-
54
- def process_info(env, mem_used, cpu_used, resp_time)
55
- req = Rack::Request.new(env)
56
- {
57
- pid: process.pid,
58
- mem_used: mem_used,
59
- cpu_used: cpu_used,
60
- resp_time: resp_time,
61
- request: req
62
- }
63
- end
64
-
65
- def process
66
- @process ||= Proceso::PID.new(pid)
67
- end
68
-
69
- end
70
- end
71
-