aboutsummaryrefslogtreecommitdiff
path: root/core/prof/spall/spall_windows.odin
blob: 2d059dc4d5acce1822b6ab866c1f8519899d36e2 (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
#+private
package spall

// Package is `#+no-instrumentation`, safe to use.
import win32 "core:sys/windows"

MAX_RW :: 1<<30

@(no_instrumentation)
_write :: proc "contextless" (fd: uintptr, data: []byte) #no_bounds_check /* bounds check would segfault instrumentation */ {
	single_write_length: win32.DWORD
	total_write: i64
	length := i64(len(data))

	for total_write < length {
		remaining := length - total_write
		to_write := win32.DWORD(min(i32(remaining), MAX_RW))

		e := win32.WriteFile(win32.HANDLE(fd), &data[total_write], to_write, &single_write_length, nil)
		if single_write_length <= 0 || !e {
			return
		}
		total_write += i64(single_write_length)
	}

	return
}

@(no_instrumentation)
_tick_now :: proc "contextless" () -> (ns: i64) {
	@(no_instrumentation)
	mul_div_u64 :: #force_inline proc "contextless" (val, num, den: i64) -> i64 {
		q := val / den
		r := val % den
		return q * num + r * num / den
	}

	@thread_local qpc_frequency: win32.LARGE_INTEGER

	if qpc_frequency == 0 {
		win32.QueryPerformanceFrequency(&qpc_frequency)
	}
	now: win32.LARGE_INTEGER
	win32.QueryPerformanceCounter(&now)

	return mul_div_u64(i64(now), 1e9, i64(qpc_frequency))
}