Class: OsCtl::Lib::CpuMask

Inherits:
Object
  • Object
show all
Defined in:
lib/libosctl/cpu_mask.rb

Overview

Interface for CPU masks

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(mask) ⇒ CpuMask

Returns a new instance of CpuMask.

Parameters:

  • mask (String, Array<Integer>)


13
14
15
16
17
18
19
20
21
22
# File 'lib/libosctl/cpu_mask.rb', line 13

def initialize(mask)
  @cpu_list =
    if mask.is_a?(String)
      parse_cpus(mask)
    elsif mask.is_a?(Array)
      mask
    else
      raise ArgumentError, 'mask can be either a string or a list of integers'
    end
end

Class Method Details

.format(mask) ⇒ String

Parameters:

  • mask (Array<Integer>)

Returns:

  • (String)


8
9
10
# File 'lib/libosctl/cpu_mask.rb', line 8

def self.format(mask)
  new(mask).to_s
end

Instance Method Details

#&(other) ⇒ CpuMask

Return an intersection of self with other mask as a new mask

Parameters:

Returns:



38
39
40
# File 'lib/libosctl/cpu_mask.rb', line 38

def &(other)
  self.class.new(to_a & other.to_a)
end

#all_rangeObject (protected)



104
105
106
# File 'lib/libosctl/cpu_mask.rb', line 104

def all_range
  @all_range ||= (0..(Etc.nprocessors - 1))
end

#each {|cpu| ... } ⇒ Object

Yield Parameters:

  • cpu (Integer)


53
54
55
56
57
58
59
# File 'lib/libosctl/cpu_mask.rb', line 53

def each(&)
  if @cpu_list == :all
    all_range.each(&)
  else
    @cpu_list.each(&)
  end
end

#format(cpu_list) ⇒ Object (protected)



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/libosctl/cpu_mask.rb', line 108

def format(cpu_list)
  return if cpu_list.empty?

  groups = []
  acc = []
  prev = nil

  cpu_list.each do |cpu|
    if prev.nil? || cpu == prev + 1
      prev = cpu
      acc << cpu
    else
      groups << format_range(acc)
      prev = nil
      acc = [cpu]
    end
  end

  groups << format_range(acc)
  groups.join(',')
end

#format_range(acc) ⇒ Object (protected)



130
131
132
133
134
135
136
137
138
139
140
# File 'lib/libosctl/cpu_mask.rb', line 130

def format_range(acc)
  len = acc.length

  if len == 1
    acc.first
  elsif len == 2
    acc.join(',')
  elsif len > 2
    "#{acc.first}-#{acc.last}"
  end
end

#include?(cpu) ⇒ Boolean

Check if the mask includes a particular CPU

Parameters:

  • cpu (Integer)

Returns:

  • (Boolean)


27
28
29
30
31
32
33
# File 'lib/libosctl/cpu_mask.rb', line 27

def include?(cpu)
  if @cpu_list == :all
    true
  else
    @cpu_list.include?(cpu)
  end
end

#parse_cpus(str) ⇒ Object (protected)



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/libosctl/cpu_mask.rb', line 78

def parse_cpus(str)
  return :all if str == '*'

  ret = []

  str.split(',').each do |grp|
    sep = grp.index('-')

    if sep
      parts = grp.split('-')

      if parts.length != 2
        raise ArgumentError, "invalid cpu mask #{str.inspect}"
      end

      first, last = parts
      ret.concat((first.to_i..last.to_i).to_a)
    else
      ret << grp.to_i
    end
  end

  ret.sort!
  ret
end

#sizeInteger

Return the number of CPUs

Returns:

  • (Integer)


44
45
46
47
48
49
50
# File 'lib/libosctl/cpu_mask.rb', line 44

def size
  if @cpu_list == :all
    all_range.size
  else
    @cpu_list.size
  end
end

#to_aArray<Integer>

Return CPUs as an array

Returns:

  • (Array<Integer>)


63
64
65
66
67
68
69
# File 'lib/libosctl/cpu_mask.rb', line 63

def to_a
  if @cpu_list == :all
    all_range.to_a
  else
    @cpu_list
  end
end

#to_sString

Returns:

  • (String)


72
73
74
# File 'lib/libosctl/cpu_mask.rb', line 72

def to_s
  @to_s ||= format(@cpu_list == :all ? all_range : @cpu_list)
end