Class: Cuboid::System::Slots

Inherits:
Object
  • Object
show all
Defined in:
lib/cuboid/system/slots.rb

Instance Method Summary collapse

Constructor Details

#initialize(system) ⇒ Slots



6
7
8
9
# File 'lib/cuboid/system/slots.rb', line 6

def initialize( system )
    @system = system
    @pids   = Set.new
end

Instance Method Details

#availableInteger



23
24
25
26
27
28
29
30
31
32
# File 'lib/cuboid/system/slots.rb', line 23

def available
    # Manual mode, user gave us a value.
    if (max_slots = Options.system.max_slots)
        max_slots - used

    # Auto-mode, pick the safest restriction, RAM vs CPU.
    else
        available_auto
    end
end

#available_autoInteger



37
38
39
# File 'lib/cuboid/system/slots.rb', line 37

def available_auto
    [ available_in_memory, available_in_cpu, available_in_disk ].min
end

#available_in_cpuInteger



69
70
71
# File 'lib/cuboid/system/slots.rb', line 69

def available_in_cpu
    @system.cpu_count - used
end

#available_in_diskInteger



78
79
80
81
# File 'lib/cuboid/system/slots.rb', line 78

def available_in_disk
    return Float::INFINITY if disk_space == 0
    (unallocated_disk_space / disk_space).to_i
end

#available_in_memoryInteger



59
60
61
62
# File 'lib/cuboid/system/slots.rb', line 59

def available_in_memory
    return Float::INFINITY if memory_size == 0
    (unallocated_memory / memory_size).to_i
end

#disk_spaceObject



129
130
131
132
# File 'lib/cuboid/system/slots.rb', line 129

def disk_space
    return 0 if !Cuboid::Application.application
    Cuboid::Application.application.max_disk.to_i
end

#memory_sizeFixnum



136
137
138
139
# File 'lib/cuboid/system/slots.rb', line 136

def memory_size
    return 0 if !Cuboid::Application.application
    Cuboid::Application.application.max_memory.to_i
end

#remaining_disk_space_for(pid) ⇒ Integer



110
111
112
# File 'lib/cuboid/system/slots.rb', line 110

def remaining_disk_space_for( pid )
    [disk_space - @system.disk_space_for_process( pid ), 0].max
end

#remaining_memory_for(pid) ⇒ Integer



87
88
89
# File 'lib/cuboid/system/slots.rb', line 87

def remaining_memory_for( pid )
    [memory_size - @system.memory_for_process_group( pid ), 0].max
end

#resetObject



11
12
13
# File 'lib/cuboid/system/slots.rb', line 11

def reset
    @pids.clear
end

#totalInteger



50
51
52
# File 'lib/cuboid/system/slots.rb', line 50

def total
    used + available
end

#unallocated_disk_spaceInteger



116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/cuboid/system/slots.rb', line 116

def unallocated_disk_space
    # Available space right now.
    available_space = @system.disk_space_free

    # Remove allocated space to figure out how much we can really spare.
    @pids.each do |pid|
        # Mark the remaining allocated space as unavailable.
        available_space -= remaining_disk_space_for( pid )
    end

    available_space
end

#unallocated_memoryInteger



93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/cuboid/system/slots.rb', line 93

def unallocated_memory
    # Available memory right now.
    available_mem = @system.memory_free

    # Remove allocated memory to figure out how much we can really spare.
    @pids.each do |pid|
        # Mark the remaining allocated memory as unavailable.
        available_mem -= remaining_memory_for( pid )
    end

    available_mem
end

#use(pid) ⇒ Object



15
16
17
18
# File 'lib/cuboid/system/slots.rb', line 15

def use( pid )
    @pids << pid
    pid
end

#usedInteger



43
44
45
46
# File 'lib/cuboid/system/slots.rb', line 43

def used
    @pids.select! { |pid| Processes::Manager.alive? pid }
    @pids.size
end