Class: Ori::Tracer
- Inherits:
-
Object
- Object
- Ori::Tracer
- Defined in:
- lib/ori/tracer.rb
Defined Under Namespace
Classes: Event, ScopeEvent
Constant Summary collapse
- TIMELINE_WIDTH =
80
Instance Method Summary collapse
- #generate_timeline_data ⇒ Object
-
#initialize ⇒ Tracer
constructor
A new instance of Tracer.
- #record(fiber_id, type, data = nil) ⇒ Object
- #record_scope(scope_id, type, data = nil) ⇒ Object
- #register_fiber(fiber_id, scope_id) ⇒ Object
- #register_scope(scope_id, parent_scope_id = nil, creating_fiber_id = nil, name: nil) ⇒ Object
- #visualize ⇒ Object
- #write_timeline_data(output_path) ⇒ Object
Constructor Details
#initialize ⇒ Tracer
Returns a new instance of Tracer.
12 13 14 15 16 17 18 19 20 |
# File 'lib/ori/tracer.rb', line 12 def initialize @events = [] @scope_events = [] @start_time = nil @fiber_names = {} @fiber_ids = Set.new @scope_hierarchy = {} @fiber_scopes = {} end |
Instance Method Details
#generate_timeline_data ⇒ Object
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/ori/tracer.rb', line 170 def generate_timeline_data # Get unique scope IDs scope_ids = @fiber_scopes.values.uniq.sort # Track nested groups for each parent nested_groups = Hash.new { |h, k| h[k] = [] } # First, handle scope hierarchy relationships scope_ids.each do |scope_id| next unless scope_id # If scope was created by a fiber, nest it under that fiber if @scope_creators&.[](scope_id) creating_fiber = @scope_creators[scope_id] group_id = "scope_#{scope_id}" nested_groups["fiber_#{creating_fiber}"] << group_id else # Otherwise use normal scope hierarchy parent_id = @scope_hierarchy.find { |_, children| children.include?(scope_id) }&.first group_id = "scope_#{scope_id}" parent_group = if parent_id "scope_#{parent_id}" else "main" end nested_groups[parent_group] << group_id end end # Then map remaining fibers to their parent scopes @fiber_ids.sort.each do |fiber_id| next if nested_groups.values.any? { |groups| groups.include?("fiber_#{fiber_id}") } scope_id = @fiber_scopes[fiber_id] parent_group = if scope_id "scope_#{scope_id}" else "main" end nested_groups[parent_group] << "fiber_#{fiber_id}" end # Generate groups data groups = [] # Add root scope group groups << { id: "main", content: "Root Scope", value: 1, className: "main-scope", nestedGroups: nested_groups["main"], showNested: true, } # Add scope groups scope_ids.each do |scope_id| next unless scope_id group_id = "scope_#{scope_id}" title = @scope_names[scope_id] || "Scope #{scope_id}" data = { id: group_id, order: scope_id, content: title, value: 2, className: "scope", showNested: false, } if nested_groups[group_id].any? data[:nestedGroups] = nested_groups[group_id] end groups << data end # Add fiber groups (including those that create scopes) @fiber_ids.sort.each do |fiber_id| data = { id: "fiber_#{fiber_id}", content: "Fiber #{fiber_id}", value: 3, className: "fiber", showNested: false, } if nested_groups["fiber_#{fiber_id}"].any? data[:nestedGroups] = nested_groups["fiber_#{fiber_id}"] end groups << data end # Generate dataset from both scope and fiber events dataset = [] # Add scope lifecycle events @scope_events.each do |event| group_id = if event.scope_id == "main" "main" else "scope_#{event.scope_id}" end item = { group: group_id, content: event.type.to_s, start: (event. * 1_000_000).to_i.to_s, className: event.type.to_s, data: event.data, } if event.type == :tag item[:content] = event.data item.delete(:data) end dataset << item end # Add fiber events @events.each do |event| item = { group: "fiber_#{event.fiber_id}", content: event.type.to_s, start: (event. * 1_000_000).to_i.to_s, className: event.type.to_s, data: event.data, } # Add end time if the event has duration item[:end] = (event. * 1_000_000).to_i.to_s if event.respond_to?(:end_timestamp) dataset << item end { groups: groups, dataset: dataset, } end |
#record(fiber_id, type, data = nil) ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/ori/tracer.rb', line 43 def record(fiber_id, type, data = nil) return unless fiber_id @start_time ||= current_time @fiber_ids << fiber_id @events << Event.new( fiber_id, type, (current_time - @start_time).round(8), data, @fiber_scopes[fiber_id], ) end |
#record_scope(scope_id, type, data = nil) ⇒ Object
58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/ori/tracer.rb', line 58 def record_scope(scope_id, type, data = nil) return unless scope_id @start_time ||= current_time @scope_events << ScopeEvent.new( scope_id, type, (current_time - @start_time).round(8), data, ) end |
#register_fiber(fiber_id, scope_id) ⇒ Object
39 40 41 |
# File 'lib/ori/tracer.rb', line 39 def register_fiber(fiber_id, scope_id) @fiber_scopes[fiber_id] = scope_id end |
#register_scope(scope_id, parent_scope_id = nil, creating_fiber_id = nil, name: nil) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/ori/tracer.rb', line 22 def register_scope(scope_id, parent_scope_id = nil, creating_fiber_id = nil, name: nil) if parent_scope_id @scope_hierarchy[parent_scope_id] ||= [] @scope_hierarchy[parent_scope_id] << scope_id end # Store scope names and use them as group IDs @scope_names ||= {} if name @scope_names[scope_id] = "Scope #{name}" end # Track which fiber created this scope (if any) @scope_creators ||= {} @scope_creators[scope_id] = creating_fiber_id if creating_fiber_id end |
#visualize ⇒ Object
71 72 73 74 75 76 77 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 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 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 |
# File 'lib/ori/tracer.rb', line 71 def visualize return "No events recorded." if @events.empty? name_width = 42 min_spacing = 1 duration = [@events.last., 0.00000001].max output = [] output << "Fiber Execution Timeline (#{duration.round(3)}s)" # First pass: calculate all positions with minimum spacing positions_by_fiber = {} max_position = T.let(0, T.untyped) @fiber_ids.sort.each do |fiber_id| fiber_events = @events.select { |e| e.fiber_id == fiber_id } next if fiber_events.empty? # Calculate raw positions based on timestamps positions = [] fiber_events.each_with_index do |evt, idx| raw_pos = (evt. / duration * TIMELINE_WIDTH).floor # Use larger scale initially if idx > 0 # Ensure minimum spacing from previous event prev_pos = T.unsafe(positions.last) || -1 positions << [raw_pos, prev_pos + min_spacing].max else positions << raw_pos end end positions_by_fiber[fiber_id] = positions max_position = [max_position, T.unsafe(positions.last) || 0].max end # Calculate final timeline width based on max position timeline_width = max_position + 1 # Add some padding separator = "=" * (timeline_width + name_width + 3) output << separator # Second pass: render the timeline @fiber_ids.sort.each do |fiber_id| fiber_events = @events.select { |e| e.fiber_id == fiber_id } next if fiber_events.empty? fiber_name = @fiber_names[fiber_id] || "Fiber #{fiber_id}" line = "#{fiber_name.ljust(name_width)} |" timeline = " " * timeline_width positions = positions_by_fiber[fiber_id] # Render events using calculated positions fiber_events.each_with_index do |evt, idx| pos = positions[idx] next_pos = positions[idx + 1] # Choose character based on event type char = case evt.type when :opened, :created then "█" when :resuming then "▶" when :waiting_io then "~" when :sleeping then "." when :yielded then "╎" when :closed, :completed then "▒" when :cancelling then "⏹" when :error, :cancelled then "✗" when :awaiting then "↻" else " " end timeline[pos] = char # Fill the space until the next event if there is one next unless next_pos length = next_pos - pos - 1 next if length <= 0 fill_char = case evt.type when :resuming then "═" when :waiting_io then "~" when :sleeping then "." when :yielded then "-" else " " end timeline[pos + 1, length] = fill_char * length end line << timeline << "|" output << line end output << separator output << "Legend: (█ Start) (▒ Finish) (═ Running) (~ IO-Wait) (. Sleeping) (╎ Yield) (✗ Error)" output.join("\n") end |
#write_timeline_data(output_path) ⇒ Object
314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'lib/ori/tracer.rb', line 314 def write_timeline_data(output_path) data = generate_timeline_data # Create JavaScript file content js_content = " export const groups = \#{data[:groups].to_json};\n\n export const dataset = \#{data[:dataset].to_json};\n JAVASCRIPT\n\n # Write to file\n File.write(File.join(output_path, \"index.html\"), File.read(File.join(__dir__, \"out\", \"index.html\")))\n File.write(File.join(output_path, \"script.js\"), js_content)\nend\n" |