Files
ubicloud/prog/learn_memory.rb
Enes Cakir 8357856622 Add an identifier to all Prog label methods
While reviewing and reading code, label and helper methods are mixed in
Prog files. It's hard to identify sometimes without in-depth looking.

I propose to define labels with `self.label` class method.
One of clover's predecessor has `work_` prefix, and I think it
was useful.

It has some benefits:
1. It helps to identify label methods easily
2. It helps to keep list of valid labels, `hop` can check target label is
   valid or not
3. It generates `hop_#{label}` methods. Instead of freehand `hop :label`
   calls, we can call `hop_label` method similar to `incr_#{name}`
   semaphore methods.

(2) and (3) have some overlapped benefits. They help to stay in valid
label world in different ways.
2023-08-23 12:09:57 +03:00

28 lines
909 B
Ruby

# frozen_string_literal: true
class Prog::LearnMemory < Prog::Base
subject_is :sshable
def parse_sum(s)
s.each_line.filter_map do |line|
next unless line =~ /\A\s*Size: (\d+) (\w+)/
# Fail noisily if unit is not in gigabytes
fail "BUG: unexpected dmidecode unit" unless $2 == "GB"
Integer($1)
end.sum
end
label def start
# Use dmidecode to get an integral amount of system memory.
# Generally, there is a gigabyte or so less available to
# applications than installed as reported by /proc/meminfo.
#
# Thus, in practice we can't run VMs at 100% system size anyway,
# we'll have to leave padding, but to keep the ratios neat for the
# customer, we compute CPU memory allocation ratio against
# physical memory.
mem_gib = parse_sum(sshable.cmd("sudo /usr/sbin/dmidecode -t memory | fgrep Size:"))
pop mem_gib: mem_gib
end
end