Class: Distem::Resource::Memory

Inherits:
Object
  • Object
show all
Defined in:
lib/distem/resource/memory.rb

Overview

Abstract representation of the physical memory resource

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Memory) initialize(capacity = 0, swap = 0)

Create a new Memory



16
17
18
19
20
21
22
23
# File 'lib/distem/resource/memory.rb', line 16

def initialize(capacity=0,swap=0)
  @capacity = capacity
  @swap = swap
  @allocated_capacity = 0
  @allocated_swap = 0
  @capacity_lock = Mutex.new
  @swap_lock = Mutex.new
end

Instance Attribute Details

- (Object) allocated_capacity

The allocated RAM (in MB)



11
12
13
# File 'lib/distem/resource/memory.rb', line 11

def allocated_capacity
  @allocated_capacity
end

- (Object) allocated_swap

The allocated Swap (in MB)



13
14
15
# File 'lib/distem/resource/memory.rb', line 13

def allocated_swap
  @allocated_swap
end

- (Object) capacity

The capacity of the RAM (in MB)



7
8
9
# File 'lib/distem/resource/memory.rb', line 7

def capacity
  @capacity
end

- (Object) swap

The capacity of the Swap (in MB)



9
10
11
# File 'lib/distem/resource/memory.rb', line 9

def swap
  @swap
end

Instance Method Details

- (Object) allocate(opts)



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/distem/resource/memory.rb', line 25

def allocate(opts)
  if ((opts[:mem] && (opts[:mem].to_i > (@capacity - @allocated_capacity))) ||
      (opts[:swap] && (opts[:swap].to_i > (@swap - @allocated_swap))))
    raise Lib::UnavailableResourceError
  end
  if opts[:mem]
    @capacity_lock.synchronize {
      @allocated_capacity += opts[:mem].to_i
    }
  end
  if opts[:swap]
    @swap_lock.synchronize {
      @allocated_swap += opts[:swap].to_i
    }
  end
end

- (Object) deallocate(opts)

Raises:



42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/distem/resource/memory.rb', line 42

def deallocate(opts)
  if opts[:mem]
    @capacity_lock.synchronize {
      @allocated_capacity -= opts[:mem].to_i
    }
  end
  if opts[:swap]
    @swap_lock.synchronize {
      @allocated_swap -= opts[:swap].to_i
    }
  end
  raise Lib::DistemError if ((@allocated_capacity < 0) || (@allocated_swap < 0))
end

- (Object) get_free_capacity



56
57
58
# File 'lib/distem/resource/memory.rb', line 56

def get_free_capacity
  return @capacity - @allocated_capacity
end

- (Object) get_free_swap



60
61
62
# File 'lib/distem/resource/memory.rb', line 60

def get_free_swap
  return @swap - @allocated_swap
end