File: rss_tool.rb

package info (click to toggle)
ruby-test-prof 1.5.2%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 660 kB
  • sloc: ruby: 6,064; makefile: 4
file content (87 lines) | stat: -rw-r--r-- 1,839 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# frozen_string_literal: true

require "rbconfig"

module TestProf
  module MemoryProf
    class Tracker
      module RssTool
        class ProcFS
          def initialize
            @statm = File.open("/proc/#{$$}/statm", "r")
            @page_size = get_page_size
          end

          def track
            @statm.seek(0)
            @statm.gets.split(/\s/)[1].to_i * @page_size
          end

          private

          def get_page_size
            [
              -> {
                require "etc"
                Etc.sysconf(Etc::SC_PAGE_SIZE)
              },
              -> { `getconf PAGE_SIZE`.to_i },
              -> { 0x1000 }
            ].each do |strategy|
              page_size = begin
                strategy.call
              rescue
                next
              end
              return page_size
            end
          end
        end

        class PS
          def track
            `ps -o rss -p #{$$}`.strip.split.last.to_i * 1024
          end
        end

        class GetProcess
          def track
            command.strip.split.last.to_i
          end

          private

          def command
            `powershell -Command "Get-Process -Id #{$$} | select WS"`
          end
        end

        TOOLS = {
          linux: ProcFS,
          macosx: PS,
          unix: PS,
          windows: GetProcess
        }.freeze

        class << self
          def tool
            TOOLS[os_type]&.new
          end

          def os_type
            case RbConfig::CONFIG["host_os"]
            when /linux/
              :linux
            when /darwin|mac os/
              :macosx
            when /solaris|bsd/
              :unix
            when /mswin|msys|mingw|cygwin|bccwin|wince|emc/
              :windows
            end
          end
        end
      end
    end
  end
end