Class: Distem::Resource::CPU

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

Overview

Abstract representation of a physical CPU resource

Defined Under Namespace

Classes: Core

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (CPU) initialize

Create a new CPU



46
47
48
49
50
51
# File 'lib/distem/resource/cpu.rb', line 46

def initialize()
  @cores = {}
  @cores_alloc = {}
  @critical_cache_links = []
  @cache_links_size = nil
end

Instance Attribute Details

The size of a cached linked core “group”



43
44
45
# File 'lib/distem/resource/cpu.rb', line 43

def cache_links_size
  @cache_links_size
end

- (Object) cores (readonly)

The CPU cores list



37
38
39
# File 'lib/distem/resource/cpu.rb', line 37

def cores
  @cores
end

- (Object) cores_alloc (readonly)

The CPU cores allocation list (each core can be allocated to a VNode)



39
40
41
# File 'lib/distem/resource/cpu.rb', line 39

def cores_alloc
  @cores_alloc
end

Cores critical cache links list



41
42
43
# File 'lib/distem/resource/cpu.rb', line 41

def critical_cache_links
  @critical_cache_links
end

Instance Method Details

- (Object) add_core(physicalid, coreid, freq, freqs)

Add a new Core to the CPU

Attributes

  • physicalid The physical (unique) id of the Core (cgroups, cpufreq, field processor in /proc/cpuinfo)

  • coreid The logical id of the Core (used by some softwares)

  • freq The actually set (KHz)

  • freqs Available frequencies of that core (CPU throttling)



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

def add_core(physicalid,coreid,freq,freqs)
  raise Lib::AlreadyExistingResourceError if @cores[physicalid]
  @cores[physicalid.to_i] = Core.new(physicalid,coreid,freq,freqs)
end

Add a critical cache link between several cores (critical because e.g. that cores have to change their frequency together)

Attributes

  • cores The Array containing the Cores to be linked



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'lib/distem/resource/cpu.rb', line 106

def add_critical_cache_link(cores)
  raise Lib::InvalidParameterError, cores unless cores.is_a?(Array)
  cores.collect! do |core|
    unless core.is_a?(Core)
      core = get_core(core)
      raise Lib::InvalidParameterError, core unless core
    end
    core
  end

  cores.each do |core|
    core = get_core(core) unless core.is_a?(Core)
    raise raise Lib::InvalidParameterError, core unless core

    tmpcores = cores.dup
    tmpcores.delete(core)
    core.cache_links = tmpcores
  end
  @cache_links_size = cores.size unless @cache_links_size
  @critical_cache_links << cores
end

- (Object) alloc_cores(vnode, corenb = 1, cache_linked = false)

Allocate Cores to a VNode

Attributes

  • vnode The VNode object

  • corenb The number of Cores to allocate

  • cache_linked Specify if the allocated Cores should be or not cache linked

Exceptions

  • UnavailableResourceError If there is no more cores available (or if there it's not possible to find corenb cache linked Cores to allocate)



136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
# File 'lib/distem/resource/cpu.rb', line 136

def alloc_cores(vnode,corenb=1,cache_linked=false)
  freecores = get_free_cores
  raise Lib::UnavailableResourceError, "Core x#{corenb}" \
    if freecores.empty? or freecores.size < corenb
  if cache_linked
    clsize = (@cache_links_size ? @cache_links_size : 1)
    corelknb = (corenb.to_f / clsize).ceil
    realcorenb = corelknb*clsize
    raise Lib::UnavailableResourceError, "CoreLinked x#{realcorenb}" \
      if freecores.empty? or freecores.size < realcorenb

    toallocate = []
    i = 0
    corelknb.times do
      allrelatedfree = false
      curcores = []
      freecores.each do |curcore|
        allrelatedfree = true
        curcores = [curcore]
        curcore.cache_links.each do |core|
          i += 1
          #core = get_core(coreid)
          if allocated_core?(core)
            allrelatedfree = false
            break
          end
          curcores << core
        end
        break if allrelatedfree
      end
      if allrelatedfree
        toallocate = toallocate + curcores
        freecores = freecores - curcores
      else
        raise Lib::UnavailableResourceError, "CoreLinked x#{realcorenb} #{i}"
      end
    end
    toallocate.each { |core| @cores_alloc[core] = vnode }
    cores = toallocate
  else
    cores = freecores[0..corenb-1]
  end

  cores.each { |core| @cores_alloc[core] = vnode }

  return cores[0..corenb-1]
end

- (Boolean) allocated_core?(core)

Check if a specified Core is already allocated to a VNode

Attributes

  • vnode The Core object

Returns

Boolean value

Returns:

  • (Boolean)

Raises:



198
199
200
201
202
# File 'lib/distem/resource/cpu.rb', line 198

def allocated_core?(core)
  core = get_core(core) if core.is_a?(Numeric)
  raise Lib::InvalidParameterError, core unless core.is_a?(CPU::Core)
  return (@cores_alloc[core] != nil)
end

- (Object) DELETE physicalid.to_i



99
# File 'lib/distem/resource/cpu.rb', line 99

@cores.delete(physicalid.to_i)

- (Object) free_cores(vnode)

“Desallocate” the Cores that was associated to a VNode

Attributes

  • vnode The VNode object



188
189
190
# File 'lib/distem/resource/cpu.rb', line 188

def free_cores(vnode)
  @cores_alloc.delete_if { |core,vnod| vnod == vnode }
end

- (Object) get_allocated_cores(vnode)

Get cores that are associated with a VNode

Attributes

  • vnode The VNode object

Returns

Array of Core objects



81
82
83
84
# File 'lib/distem/resource/cpu.rb', line 81

def get_allocated_cores(vnode)
  # Ruby Bug Hash.select should return an Hash
  return @cores_alloc.select{ |core,node| vnode == node }.collect{|v| v[0]}
end

- (Object) get_core(physicalid)

Get the specified Core

Attributes

  • physicalid The physicalid of the core to be returned

Returns

Core object



71
72
73
# File 'lib/distem/resource/cpu.rb', line 71

def get_core(physicalid)
  return @cores[physicalid.to_i]
end

- (Object) get_free_cores

Get cores that are not associated with any VNode at the moment

Returns

Array of Core objects



90
91
92
# File 'lib/distem/resource/cpu.rb', line 90

def get_free_cores
  return @cores.values - @cores_alloc.keys
end

- (Object) remove_core(physicalid)

Dettach a Core from this CPU

Attributes

  • physicalid The physical id of the Core to dettach



98
99
100
# File 'lib/distem/resource/cpu.rb', line 98

def remove_core(physicalid)
  @cores.delete(physicalid.to_i)
end