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

Returns a new instance of 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

Returns Amount of new scans that can be safely run in parallel, currently. User option will override decision based on system resources.

Returns:

  • (Integer)

    Amount of new scans that can be safely run in parallel, currently. User option will override decision based on system resources.



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

Returns Amount of new scans that can be safely run in parallel, currently. The decision is based on the available resources alone.

Returns:

  • (Integer)

    Amount of new scans that can be safely run in parallel, currently. The decision is based on the available resources alone.



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

Returns Amount of CPU cores that are available.

Well, they may not be really available, other stuff on the machine could be using them to a considerable extent, but we can only do so much.

Returns:

  • (Integer)

    Amount of CPU cores that are available.

    Well, they may not be really available, other stuff on the machine could be using them to a considerable extent, but we can only do so much.



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

def available_in_cpu
    @system.cpu_count - used
end

#available_in_diskInteger

Returns Amount of scans we can fit into the available disk space.

Works based on slots, available space isn’t currently available OS disk space but space that is unallocated.

Returns:

  • (Integer)

    Amount of scans we can fit into the available disk space.

    Works based on slots, available space isn’t currently available OS disk space but space that is unallocated.



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

Returns Amount of scans we can fit into the available memory.

Works based on slots, available memory isn’t currently available OS memory but memory that is unallocated.

Returns:

  • (Integer)

    Amount of scans we can fit into the available memory.

    Works based on slots, available memory isn’t currently available OS memory but memory that is unallocated.



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

Returns Amount of memory (in bytes) to allocate to each scan.

Returns:

  • (Fixnum)

    Amount of memory (in bytes) to allocate to each scan.



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

Returns Remaining disk space for the scan, in bytes.

Parameters:

  • pid (Integer)

Returns:

  • (Integer)

    Remaining disk space for the scan, in bytes.



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

Returns Remaining memory for the scan, in bytes.

Parameters:

  • pid (Integer)

Returns:

  • (Integer)

    Remaining memory for the scan, in bytes.



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

Returns Amount of scans that can be safely run in parallel, in total.

Returns:

  • (Integer)

    Amount of scans that can be safely run in parallel, in total.



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

def total
    used + available
end

#unallocated_disk_spaceInteger

Returns Amount of disk space (in bytes) available for future scans.

Returns:

  • (Integer)

    Amount of disk space (in bytes) available for future scans.



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

Returns Amount of memory (in bytes) available for future scans.

Returns:

  • (Integer)

    Amount of memory (in bytes) available for future scans.



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

Returns Amount of instances that are currently alive.

Returns:

  • (Integer)

    Amount of instances that are currently alive.



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

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