<commit_before>require 'rubinius/agent'
require 'socket'
require 'readline'
require 'tempfile'

class Console
  def initialize(port, pid, path)
    @port = port.to_i
    @pid =  pid
    @path = path
  end

  def connect(notice=true)
    @agent = Rubinius::Agent.connect "localhost", @port

    if notice
      puts "Connected to localhost:#{@port}, host type: #{@agent.handshake[1]}"
    end
  end

  def take_commands
    while true
      str = Readline.readline("console> ")

      cmd, args = str.split(/\s+/, 2)

      case cmd
      when "set"
        set_config(args)
      when "get"
        get_config(args)
      when "memory", "mem"
        show_memory
      when "pid"
        show_pid
      when "q", "quit", "exit"
        quit
        return
      when "bt", "backtrace"
        backtrace(args)
      when "gdb"
        start_gdb(args)
      when "help"
        help
      else
        if cmd
          if !args or args.empty?
            get_config(cmd)
          else
            puts "Unknown command: #{cmd}"
          end
        end
      end
    end
  rescue Errno::EPIPE, IOError
    puts "VM has disconnected"
  end

  def help
    puts <<-STR
set <var> <value> - Set a variable
get <var>         - Get a variable
backtrace         - Show backtraces of all Threads
gdb               - Connect to process via gdb
pid               - Show process ID
help              - You're lookin' at it
    STR
  end

  def quit
    response = @agent.close

    unless response == :bye
      puts "Unexpected response: #{response.inspect}"
    end
  end

  def set_config(args)
    var, val = args.split(/\s+/, 2)

    response = @agent.request :set_config, var, val

    case response
    when :ok
      puts "Set var '#{var}' ok."
    when :unknown_key
      puts "Unknown var '#{var}'."
    when :error
      puts "Error setting variable."
    end
  end

  def get_config(args)
    var = args.strip

    begin
      kind, val = get(var)
    rescue Rubinius::Agent::GetError => e
      puts "Error: #{e.message}"
      return
    end

    if val.kind_of? Array
      puts "var #{var} = ["
      val.each do |x|
        puts "  #{x.inspect},"
      end
      puts "]"
    else
      puts "var #{var} = #{val.inspect}"
    end
  end

  def get(var)
    @agent.get(var)
  end

  def human_number(number)
    k = number >> 10
    if k > 1024
      "%.2fMb" % (number.to_f / (1024 * 1024))
    elsif k == 0
      "#{number}b"
    else
      "#{k}Kb"
    end
  end

  def show_pid
    code, val = get "system.pid"
    puts "PID: #{val}"
  end

  def show_memory
    code, val = get "system.memory.young"
    puts "  Young: #{val}"

    total = val

    code, val = get "system.memory.mature"
    puts " Mature: #{val}"

    total += val

    code, val = get "system.memory.large"
    puts "  Large: #{val}"

    total += val

    code, val = get "system.memory.code"
    puts "   Code: #{val}"

    total += val

    code, val = get "system.memory.symbols"
    puts "Symbols: #{val}"

    total += val

    puts " Total: #{human_number(total)} (#{total})"
  end

  def backtrace(args)
    if args =~ /--vm/
      run_gdb "thread apply all bt"
    else
      kind, val = get "system.backtrace"
<commit_msg>Added 'memory' to console help<commit_after>require 'rubinius/agent'
require 'socket'
require 'readline'
require 'tempfile'

class Console
  def initialize(port, pid, path)
    @port = port.to_i
    @pid =  pid
    @path = path
  end

  def connect(notice=true)
    @agent = Rubinius::Agent.connect "localhost", @port

    if notice
      puts "Connected to localhost:#{@port}, host type: #{@agent.handshake[1]}"
    end
  end

  def take_commands
    while true
      str = Readline.readline("console> ")

      cmd, args = str.split(/\s+/, 2)

      case cmd
      when "set"
        set_config(args)
      when "get"
        get_config(args)
      when "memory", "mem"
        show_memory
      when "pid"
        show_pid
      when "q", "quit", "exit"
        quit
        return
      when "bt", "backtrace"
        backtrace(args)
      when "gdb"
        start_gdb(args)
      when "help"
        help
      else
        if cmd
          if !args or args.empty?
            get_config(cmd)
          else
            puts "Unknown command: #{cmd}"
          end
        end
      end
    end
  rescue Errno::EPIPE, IOError
    puts "VM has disconnected"
  end

  def help
    puts <<-STR
set <var> <value> - Set a variable
get <var>         - Get a variable
backtrace         - Show backtraces of all Threads
gdb               - Connect to process via gdb
pid               - Show process ID
memory, mem       - Show memory statistics
help              - You're lookin' at it
    STR
  end

  def quit
    response = @agent.close

    unless response == :bye
      puts "Unexpected response: #{response.inspect}"
    end
  end

  def set_config(args)
    var, val = args.split(/\s+/, 2)

    response = @agent.request :set_config, var, val

    case response
    when :ok
      puts "Set var '#{var}' ok."
    when :unknown_key
      puts "Unknown var '#{var}'."
    when :error
      puts "Error setting variable."
    end
  end

  def get_config(args)
    var = args.strip

    begin
      kind, val = get(var)
    rescue Rubinius::Agent::GetError => e
      puts "Error: #{e.message}"
      return
    end

    if val.kind_of? Array
      puts "var #{var} = ["
      val.each do |x|
        puts "  #{x.inspect},"
      end
      puts "]"
    else
      puts "var #{var} = #{val.inspect}"
    end
  end

  def get(var)
    @agent.get(var)
  end

  def human_number(number)
    k = number >> 10
    if k > 1024
      "%.2fMb" % (number.to_f / (1024 * 1024))
    elsif k == 0
      "#{number}b"
    else
      "#{k}Kb"
    end
  end

  def show_pid
    code, val = get "system.pid"
    puts "PID: #{val}"
  end

  def show_memory
    code, val = get "system.memory.young"
    puts "  Young: #{val}"

    total = val

    code, val = get "system.memory.mature"
    puts " Mature: #{val}"

    total += val

    code, val = get "system.memory.large"
    puts "  Large: #{val}"

    total += val

    code, val = get "system.memory.code"
    puts "   Code: #{val}"

    total += val

    code, val = get "system.memory.symbols"
    puts "Symbols: #{val}"

    total += val

    puts "  Total: #{human_number(total)} (#{total})"
  end

  def backtrace(args)
    if args =~ /--vm/
      run_gdb "thread apply all bt"
    else
      kind, val = get "system.backtrace"

{'fraction_non_alphanumeric': 0.12004320320938126, 'fraction_numerical': 0.005863292701743558, 'mean_word_length': 1.8643393725143615, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 19, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6393040', 'n_tokens_mistral': 2243, 'n_tokens_neox': 2184, 'n_words': 661}