Class: LeakProfiler::MemoryUsage

Inherits:
Object
  • Object
show all
Defined in:
lib/leak_profiler/memory_usage.rb,
ext/leak_profiler/leak_profiler.c

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(output_dir:, interval:, filename: nil) ⇒ MemoryUsage

Returns a new instance of MemoryUsage.



12
13
14
15
16
# File 'lib/leak_profiler/memory_usage.rb', line 12

def initialize(output_dir:, interval:, filename: nil)
  @output_dir = output_dir
  @interval = interval
  @filename = filename || "memory-usage-#{Process.pid}.csv"
end

Instance Attribute Details

#threadObject (readonly)

Returns the value of attribute thread.



7
8
9
# File 'lib/leak_profiler/memory_usage.rb', line 7

def thread
  @thread
end

Class Method Details

.rssObject



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'ext/leak_profiler/leak_profiler.c', line 36

static VALUE leak_profiler_rss(VALUE self)
{
    long rss = 0;

    FILE *file = fopen("/proc/self/statm", "r");
    if (!file) {
        rb_sys_fail("/proc/self/statm");
    }
    if (fscanf(file, "%*s%ld", &rss) != 1) {
        fclose(file);
        rb_sys_fail("fscanf");
    }
    fclose(file);
    return LONG2NUM(rss * sysconf(_SC_PAGESIZE) / 1024);
}

Instance Method Details

#reportObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/leak_profiler/memory_usage.rb', line 18

def report
  @thread = Thread.start do
    i = 0
    File.open(File.expand_path(File.join(@output_dir, @filename)), 'w') do |f|
      f.puts('elapsed [sec],memory usage (rss) [MB]')

      loop do
        rss = LeakProfiler::MemoryUsage.rss / 1024.0
        f.puts("#{i},#{rss}")
        f.fsync
        i += @interval
        sleep(@interval)
      end
    end
  end
end