aboutsummaryrefslogtreecommitdiff
path: root/core/prof/spall/spall.odin
blob: 16b809359dc5d6a6f8616619dcbf074979ebb44f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
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
58
59
60
61
62
63
64
65
66
67
68
69
70
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
169
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
package spall

import "core:os"
import "core:time"
import "base:intrinsics"

// File Format

MANUAL_MAGIC :: u64le(0x0BADF00D)

Manual_Stream_Header :: struct #packed {
	magic:           u64le,
	version:         u64le,
	timestamp_scale: f64le,
	reserved:        u64le,
}

Manual_Buffer_Header :: struct #packed {
	size:     u32le,
	tid:      u32le,
	pid:      u32le,
	first_ts: u64le,
}

Manual_Event_Type :: enum u8 {
	Invalid      = 0,

	Begin        = 3,
	End          = 4,
	Instant      = 5,

	Pad_Skip     = 7,

	Name_Process = 8,
	Name_Thread  = 9,
}

Begin_Event :: struct #packed {
	type:     Manual_Event_Type,
	ts:       u64le,
	name_len: u8,
	args_len: u8,
}
BEGIN_EVENT_MAX :: size_of(Begin_Event) + 255 + 255

End_Event :: struct #packed {
	type: Manual_Event_Type,
	ts:   u64le,
}

Pad_Skip :: struct #packed {
	type: Manual_Event_Type,
	size: u32le,
}

Name_Event :: struct #packed {
	type: Manual_Event_Type,
	name_len: u8,
}
NAME_EVENT_MAX :: size_of(Name_Event) + 255

// User Interface

Context :: struct {
	precise_time:    bool,
	timestamp_scale: f64,
	fd:              os.Handle,
}

Buffer :: struct {
	data: []u8,
	head: int,
	tid:  u32,
	pid:  u32,
	first_ts: u64,
}

BUFFER_DEFAULT_SIZE :: 0x10_0000


context_create_with_scale :: proc(filename: string, precise_time: bool, timestamp_scale: f64) -> (ctx: Context, ok: bool) #optional_ok {
	fd, err := os.open(filename, os.O_WRONLY | os.O_APPEND | os.O_CREATE | os.O_TRUNC, 0o600)
	if err != nil {
		return
	}

	ctx.fd = fd
	ctx.precise_time = precise_time
	ctx.timestamp_scale = timestamp_scale

	temp := [size_of(Manual_Stream_Header)]u8{}
	_build_stream_header(temp[:], ctx.timestamp_scale)
	os.write(ctx.fd, temp[:])
	ok = true
	return
}

context_create_with_sleep :: proc(filename: string, sleep := 2 * time.Second) -> (ctx: Context, ok: bool) #optional_ok {
	freq, freq_ok := time.tsc_frequency(sleep)
	timestamp_scale: f64 = ((1 / f64(freq)) * 1_000_000_000) if freq_ok else 1
	return context_create_with_scale(filename, freq_ok, timestamp_scale)
}

context_create :: proc{context_create_with_scale, context_create_with_sleep}

context_destroy :: proc(ctx: ^Context) {
	if ctx == nil {
		return
	}

	os.close(ctx.fd)
	ctx^ = Context{}
}

buffer_create :: proc(data: []byte, tid: u32 = 0, pid: u32 = 0) -> (buffer: Buffer, ok: bool) #optional_ok {
	assert(len(data) >= 1024)
	buffer.data     = data
	buffer.tid      = tid
	buffer.pid      = pid
	buffer.first_ts = 0
	buffer.head = size_of(Manual_Buffer_Header)
	ok = true
	return
}

@(no_instrumentation)
buffer_flush :: proc "contextless" (ctx: ^Context, buffer: ^Buffer) #no_bounds_check /* bounds check would segfault instrumentation */ {
	if len(buffer.data) == 0 {
		return
	}

	buffer_size := buffer.head - size_of(Manual_Buffer_Header)
	hdr := (^Manual_Buffer_Header)(raw_data(buffer.data))
	hdr.size = u32le(buffer_size)
	hdr.pid  = u32le(buffer.pid)
	hdr.tid  = u32le(buffer.tid)
	hdr.first_ts = u64le(buffer.first_ts)

	start := _trace_now(ctx)
	write(ctx.fd, buffer.data[:buffer.head])
	buffer.head = size_of(Manual_Buffer_Header)
	end := _trace_now(ctx)

	buffer.head += _build_begin(buffer.data[buffer.head:], "Spall Trace Buffer Flush", "", start)
	buffer.head += _build_end(buffer.data[buffer.head:], end)
	buffer.first_ts = end
}

buffer_destroy :: proc(ctx: ^Context, buffer: ^Buffer) {
	buffer_flush(ctx, buffer)

	buffer^ = Buffer{}
}



@(deferred_in=_scoped_buffer_end)
SCOPED_EVENT :: proc(ctx: ^Context, buffer: ^Buffer, name: string, args: string = "", location := #caller_location) -> bool {
	_buffer_begin(ctx, buffer, name, args, location)
	return true
}

@(private)
_scoped_buffer_end :: proc(ctx: ^Context, buffer: ^Buffer, _, _: string, _ := #caller_location) {
	_buffer_end(ctx, buffer)
}

@(no_instrumentation)
_trace_now :: proc "contextless" (ctx: ^Context) -> u64 {
	if !ctx.precise_time {
		return u64(tick_now())
	}

	return u64(intrinsics.read_cycle_counter())
}

@(no_instrumentation)
_build_stream_header :: proc "contextless" (buffer: []u8, timestamp_scale: f64) -> (header_size: int, ok: bool) #optional_ok {
	header_size = size_of(Manual_Stream_Header)
	if header_size > len(buffer) {
		return 0, false
	}

	hdr := (^Manual_Stream_Header)(raw_data(buffer))
	hdr.magic = MANUAL_MAGIC
	hdr.version = 3
	hdr.timestamp_scale = f64le(timestamp_scale)
	hdr.reserved = 0
	ok = true
	return
}

@(no_instrumentation)
_build_begin :: #force_inline proc "contextless" (buffer: []u8, name: string, args: string, ts: u64) -> (event_size: int, ok: bool) #optional_ok #no_bounds_check /* bounds check would segfault instrumentation */ {
	ev := (^Begin_Event)(raw_data(buffer))
	name_len := min(len(name), 255)
	args_len := min(len(args), 255)

	event_size = size_of(Begin_Event) + name_len + args_len
	if event_size > len(buffer) {
		return 0, false
	}

	ev.type = .Begin
	ev.ts   = u64le(ts)
	ev.name_len = u8(name_len)
	ev.args_len = u8(args_len)
	intrinsics.mem_copy_non_overlapping(raw_data(buffer[size_of(Begin_Event):]), raw_data(name), name_len)
	intrinsics.mem_copy_non_overlapping(raw_data(buffer[size_of(Begin_Event)+name_len:]), raw_data(args), args_len)
	ok = true

	return
}

@(no_instrumentation)
_build_end :: proc "contextless" (buffer: []u8, ts: u64) -> (event_size: int, ok: bool) #optional_ok {
	ev := (^End_Event)(raw_data(buffer))
	event_size = size_of(End_Event)
	if event_size > len(buffer) {
		return 0, false
	}

	ev.type = .End
	ev.ts   = u64le(ts)
	ok = true

	return
}

@(no_instrumentation)
_build_name_event :: #force_inline proc "contextless" (buffer: []u8, name: string, type: Manual_Event_Type) -> (event_size: int, ok: bool) #optional_ok #no_bounds_check /* bounds check would segfault instrumentation */ {
	ev := (^Name_Event)(raw_data(buffer))
	name_len := min(len(name), 255)

	event_size = size_of(Name_Event) + name_len
	if event_size > len(buffer) {
		return 0, false
	}
	if type != .Name_Process && type != .Name_Thread {
		return 0, false
	}

	ev.type = type
	ev.name_len = u8(name_len)
	intrinsics.mem_copy_non_overlapping(raw_data(buffer[size_of(Name_Event):]), raw_data(name), name_len)
	ok = true

	return
}

@(no_instrumentation)
_buffer_begin :: proc "contextless" (ctx: ^Context, buffer: ^Buffer, name: string, args: string = "", location := #caller_location) #no_bounds_check /* bounds check would segfault instrumentation */ {
	if buffer.head + BEGIN_EVENT_MAX > len(buffer.data) {
		buffer_flush(ctx, buffer)
	}
	name := location.procedure if name == "" else name
	buffer.head += _build_begin(buffer.data[buffer.head:], name, args, _trace_now(ctx))
}

@(no_instrumentation)
_buffer_end :: proc "contextless" (ctx: ^Context, buffer: ^Buffer) #no_bounds_check /* bounds check would segfault instrumentation */ {
	ts := _trace_now(ctx)

	if buffer.head + size_of(End_Event) > len(buffer.data) {
		buffer_flush(ctx, buffer)
	}

	buffer.head += _build_end(buffer.data[buffer.head:], ts)
}

@(no_instrumentation)
_buffer_name_thread :: proc "contextless" (ctx: ^Context, buffer: ^Buffer, name: string, location := #caller_location) #no_bounds_check /* bounds check would segfault instrumentation */ {
	if buffer.head + NAME_EVENT_MAX > len(buffer.data) {
		buffer_flush(ctx, buffer)
	}
	buffer.head += _build_name_event(buffer.data[buffer.head:], name, .Name_Thread)
}

@(no_instrumentation)
_buffer_name_process :: proc "contextless" (ctx: ^Context, buffer: ^Buffer, name: string, location := #caller_location) #no_bounds_check /* bounds check would segfault instrumentation */ {
	if buffer.head + NAME_EVENT_MAX > len(buffer.data) {
		buffer_flush(ctx, buffer)
	}
	buffer.head += _build_name_event(buffer.data[buffer.head:], name, .Name_Process)
}

@(no_instrumentation)
write :: proc "contextless" (fd: os.Handle, buf: []byte) -> (n: int, err: os.Error) {
	return _write(fd, buf)
}

@(no_instrumentation)
tick_now :: proc "contextless" () -> (ns: i64) {
	return _tick_now()
}