Skip to content

Commit bd4e0d4

Browse files
authored
Improve Crystal::Tracing (#15297)
- Avoid spaces in fiber names (easier to columnize) - Support more types (Bool, Char, Time::Span)
1 parent 47b948f commit bd4e0d4

File tree

6 files changed

+43
-22
lines changed

6 files changed

+43
-22
lines changed

src/crystal/event_loop/epoll.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class Crystal::EventLoop::Epoll < Crystal::EventLoop::Polling
5555
{% end %}
5656

5757
private def system_run(blocking : Bool, & : Fiber ->) : Nil
58-
Crystal.trace :evloop, "run", blocking: blocking ? 1 : 0
58+
Crystal.trace :evloop, "run", blocking: blocking
5959

6060
# wait for events (indefinitely when blocking)
6161
buffer = uninitialized LibC::EpollEvent[128]

src/crystal/event_loop/kqueue.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ class Crystal::EventLoop::Kqueue < Crystal::EventLoop::Polling
7373
private def system_run(blocking : Bool, & : Fiber ->) : Nil
7474
buffer = uninitialized LibC::Kevent[128]
7575

76-
Crystal.trace :evloop, "run", blocking: blocking ? 1 : 0
76+
Crystal.trace :evloop, "run", blocking: blocking
7777
timeout = blocking ? nil : Time::Span.zero
7878
kevents = @kqueue.wait(buffer.to_slice, timeout)
7979

src/crystal/scheduler.cr

+3-3
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Crystal::Scheduler
6666
end
6767

6868
def self.sleep(time : Time::Span) : Nil
69-
Crystal.trace :sched, "sleep", for: time.total_nanoseconds.to_i64!
69+
Crystal.trace :sched, "sleep", for: time
7070
Thread.current.scheduler.sleep(time)
7171
end
7272

@@ -225,7 +225,7 @@ class Crystal::Scheduler
225225
pending = Atomic(Int32).new(count - 1)
226226
@@workers = Array(Thread).new(count) do |i|
227227
if i == 0
228-
worker_loop = Fiber.new(name: "Worker Loop") { Thread.current.scheduler.run_loop }
228+
worker_loop = Fiber.new(name: "worker-loop") { Thread.current.scheduler.run_loop }
229229
worker_loop.set_current_thread
230230
Thread.current.scheduler.enqueue worker_loop
231231
Thread.current
@@ -272,7 +272,7 @@ class Crystal::Scheduler
272272

273273
# Background loop to cleanup unused fiber stacks.
274274
def spawn_stack_pool_collector
275-
fiber = Fiber.new(name: "Stack pool collector", &->@stack_pool.collect_loop)
275+
fiber = Fiber.new(name: "stack-pool-collector", &->@stack_pool.collect_loop)
276276
{% if flag?(:preview_mt) %} fiber.set_current_thread {% end %}
277277
enqueue(fiber)
278278
end

src/crystal/system/unix/signal.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ module Crystal::System::Signal
8282
end
8383

8484
private def self.start_loop
85-
spawn(name: "Signal Loop") do
85+
spawn(name: "signal-loop") do
8686
loop do
8787
value = reader.read_bytes(Int32)
8888
rescue IO::Error

src/crystal/system/win32/process.cr

+1-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,7 @@ struct Crystal::System::Process
203203
def self.start_interrupt_loop : Nil
204204
return unless @@setup_interrupt_handler.test_and_set
205205

206-
spawn(name: "Interrupt signal loop") do
206+
spawn(name: "interrupt-signal-loop") do
207207
while true
208208
@@interrupt_count.wait { sleep 50.milliseconds }
209209

src/crystal/tracing.cr

+36-15
Original file line numberDiff line numberDiff line change
@@ -51,37 +51,58 @@ module Crystal
5151
@size = 0
5252
end
5353

54-
def write(bytes : Bytes) : Nil
54+
def write(value : Bytes) : Nil
5555
pos = @size
5656
remaining = N - pos
5757
return if remaining == 0
5858

59-
n = bytes.size.clamp(..remaining)
60-
bytes.to_unsafe.copy_to(@buf.to_unsafe + pos, n)
59+
n = value.size.clamp(..remaining)
60+
value.to_unsafe.copy_to(@buf.to_unsafe + pos, n)
6161
@size = pos + n
6262
end
6363

64-
def write(string : String) : Nil
65-
write string.to_slice
64+
def write(value : String) : Nil
65+
write value.to_slice
6666
end
6767

68-
def write(fiber : Fiber) : Nil
69-
write fiber.as(Void*)
70-
write ":"
71-
write fiber.name || "?"
68+
def write(value : Char) : Nil
69+
chars = uninitialized UInt8[4]
70+
i = 0
71+
value.each_byte do |byte|
72+
chars[i] = byte
73+
i += 1
74+
end
75+
write chars.to_slice[0, i]
76+
end
77+
78+
def write(value : Fiber) : Nil
79+
write value.as(Void*)
80+
write ':'
81+
write value.name || '?'
7282
end
7383

74-
def write(ptr : Pointer) : Nil
84+
def write(value : Pointer) : Nil
7585
write "0x"
76-
System.to_int_slice(ptr.address, 16, true, 2) { |bytes| write(bytes) }
86+
System.to_int_slice(value.address, 16, true, 2) { |bytes| write(bytes) }
87+
end
88+
89+
def write(value : Int::Signed) : Nil
90+
System.to_int_slice(value, 10, true, 2) { |bytes| write(bytes) }
91+
end
92+
93+
def write(value : Int::Unsigned) : Nil
94+
System.to_int_slice(value, 10, false, 2) { |bytes| write(bytes) }
95+
end
96+
97+
def write(value : Time::Span) : Nil
98+
write(value.seconds * Time::NANOSECONDS_PER_SECOND + value.nanoseconds)
7799
end
78100

79-
def write(int : Int::Signed) : Nil
80-
System.to_int_slice(int, 10, true, 2) { |bytes| write(bytes) }
101+
def write(value : Bool) : Nil
102+
write value ? '1' : '0'
81103
end
82104

83-
def write(uint : Int::Unsigned) : Nil
84-
System.to_int_slice(uint, 10, false, 2) { |bytes| write(bytes) }
105+
def write(value : Nil) : Nil
85106
end
86107

87108
def to_slice : Bytes

0 commit comments

Comments
 (0)