Class: Proceso::PID

Inherits:
Object
  • Object
show all
Defined in:
lib/proceso/pid.rb,
ext/proceso/pid.c

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(pid) ⇒ Object



5
6
7
8
9
# File 'ext/proceso/pid.c', line 5

static VALUE
proceso__process_init(VALUE self, VALUE pid) {
  rb_iv_set(self, "@pid", pid);
  return self;
}

Instance Attribute Details

#pidObject

Returns the value of attribute pid.



4
5
6
# File 'lib/proceso/pid.rb', line 4

def pid
  @pid
end

Instance Method Details

#commandObject

PID command line



23
24
25
26
27
28
29
30
# File 'ext/proceso/pid.c', line 23

static VALUE
proceso__process_command(VALUE self) {
  int pid = iv2pid(self);
  char *process_cmd = rb_process_command(pid);
  if (process_cmd == NULL)
    return Qnil;
  return rb_str_new2(process_cmd);
}

#cpu_usageObject

PID CPU Usage



61
62
63
64
65
66
67
68
69
70
71
# File 'ext/proceso/pid.c', line 61

static VALUE
proceso__process_cpu_usage(VALUE self) {
  int ncpu = rb_hw_ncpu();
  float u1, u2;
  float usage;
  u1 = rb_process_cpu_times(iv2pid(self), FCPU_USR);
  usleep(100000);
  u2 = rb_process_cpu_times(iv2pid(self), FCPU_USR);
  usage = ncpu * ((u2 - u1) * 100);
  return rb_float_new(usage);
}

#executableObject



10
11
12
# File 'lib/proceso/pid.rb', line 10

def executable
  File.basename(command)
end

#mem_size(format = :bytes) ⇒ Object



14
15
16
17
18
19
20
21
22
23
# File 'lib/proceso/pid.rb', line 14

def mem_size(format = :bytes)
  rss = resident_size
  rss *= 1024 if RUBY_PLATFORM =~ /linux/
  case format.to_s
  when "bytes" then rss
  when "kb", "kilobytes" then rss.to_f / 1024.0
  when "mb", "megabytes" then rss.to_f / 1024.0 / 1024.0
  when "gb", "gigabytes" then rss.to_f / 1024.0 / 1024.0 / 1024.0
  end
end

#pathObject



6
7
8
# File 'lib/proceso/pid.rb', line 6

def path
  File.dirname(command)
end

#resident_sizeObject

PID Resident Size (bytes)



33
34
35
36
37
# File 'ext/proceso/pid.c', line 33

static VALUE
proceso__process_rss(VALUE self) {
  int ret = rb_process_memory_size(iv2pid(self), PROCESS_RSS);
  return INT2NUM(ret);
}

#running?Boolean

PID running?

Returns:

  • (Boolean)


12
13
14
15
16
17
18
19
20
# File 'ext/proceso/pid.c', line 12

static VALUE
proceso__process_running(VALUE self) {
  int pid = iv2pid(self);
  rb_pid_t i = getpgid(pid);
  if (i < 0) {
    return Qfalse;
  }
  return Qtrue;
}

#system_cpu_timesObject

PID System CPU



54
55
56
57
58
# File 'ext/proceso/pid.c', line 54

static VALUE
proceso__process_system_cpu(VALUE self) {
  float val = rb_process_cpu_times(iv2pid(self), FCPU_SYS);
  return rb_float_new(val);
}

#user_cpu_timesObject

PID User CPU



47
48
49
50
51
# File 'ext/proceso/pid.c', line 47

static VALUE
proceso__process_user_cpu(VALUE self) {
  float val = rb_process_cpu_times(iv2pid(self), FCPU_USR);
  return rb_float_new(val);
}

#virtual_sizeObject

PID Virtual Size (bytes)



40
41
42
43
44
# File 'ext/proceso/pid.c', line 40

static VALUE
proceso__process_vms(VALUE self) {
  int ret = rb_process_memory_size(iv2pid(self), PROCESS_VMS);
  return INT2NUM(ret);
}