Class: Async::Utilization::Observer

Inherits:
Object
  • Object
show all
Defined in:
lib/async/utilization/observer.rb

Overview

Shared memory observer for utilization metrics.

Writes metrics to shared memory using a schema to define the serialization layout. The schema is required to know how to serialize values efficiently.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(schema, buffer) ⇒ Observer

Initialize a new shared memory observer.



63
64
65
66
# File 'lib/async/utilization/observer.rb', line 63

def initialize(schema, buffer)
	@schema = schema
	@buffer = buffer
end

Instance Attribute Details

#bufferObject (readonly)

Returns the value of attribute buffer.



72
73
74
# File 'lib/async/utilization/observer.rb', line 72

def buffer
  @buffer
end

#schemaObject (readonly)

Returns the value of attribute schema.



69
70
71
# File 'lib/async/utilization/observer.rb', line 69

def schema
  @schema
end

#The mapped buffer for shared memory.(mappedbuffer) ⇒ Object (readonly)



72
# File 'lib/async/utilization/observer.rb', line 72

attr :buffer

Class Method Details

.open(schema, path, size, offset) ⇒ Object

Open a shared memory observer from a file.

Maps the shared memory file and creates an Observer instance. The file descriptor is closed after mapping - the memory mapping persists independently on Unix/Linux systems.

Note: mmap requires page-aligned offsets and sizes. This method handles non-page-aligned offsets by mapping from the nearest page boundary and adjusting field offsets accordingly.



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/async/utilization/observer.rb', line 31

def self.open(schema, path, size, offset)
	page_size = IO::Buffer::PAGE_SIZE
	
	# Round offset down to nearest page boundary:
	page_aligned_offset = (offset / page_size) * page_size
	offset_adjustment = offset - page_aligned_offset
	
	# Calculate how many pages we need to cover the segment:
	segment_end = offset + size
	page_aligned_end = ((segment_end + page_size - 1) / page_size) * page_size
	
	# Ensure we map at least one full page:
	map_size = [page_aligned_end - page_aligned_offset, page_size].max
	
	buffer = File.open(path, "r+b") do |file|
		mapped_buffer = IO::Buffer.map(file, map_size, page_aligned_offset)
		
		# If we had to adjust the offset, create a view into the buffer:
		if offset_adjustment > 0 || map_size > size
			mapped_buffer.slice(offset_adjustment, size)
		else
			mapped_buffer
		end
	end
	
	new(schema, buffer)
end

Instance Method Details

#The schema used for serialization.=(schemaused) ⇒ Object



69
# File 'lib/async/utilization/observer.rb', line 69

attr :schema