mirror of
https://github.com/odin-lang/Odin.git
synced 2025-12-28 17:04:34 +00:00
new_os: core/prof/spall
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
package spall
|
||||
|
||||
import "core:os"
|
||||
import "core:time"
|
||||
import "base:intrinsics"
|
||||
import "base:intrinsics"
|
||||
|
||||
import os "core:os/os2"
|
||||
import "core:time"
|
||||
|
||||
// File Format
|
||||
|
||||
@@ -64,7 +65,9 @@ NAME_EVENT_MAX :: size_of(Name_Event) + 255
|
||||
Context :: struct {
|
||||
precise_time: bool,
|
||||
timestamp_scale: f64,
|
||||
fd: os.Handle,
|
||||
|
||||
file: ^os.File,
|
||||
fd: uintptr,
|
||||
}
|
||||
|
||||
Buffer :: struct {
|
||||
@@ -79,18 +82,20 @@ 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)
|
||||
file, err := os.open(filename, {.Write, .Append, .Create, .Trunc}, {.Read_User, .Write_User})
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
ctx.fd = fd
|
||||
ctx.file = file
|
||||
ctx.fd = os.fd(file)
|
||||
|
||||
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[:])
|
||||
write(ctx.fd, temp[:])
|
||||
ok = true
|
||||
return
|
||||
}
|
||||
@@ -109,7 +114,7 @@ context_destroy :: proc(ctx: ^Context) {
|
||||
return
|
||||
}
|
||||
|
||||
os.close(ctx.fd)
|
||||
os.close(ctx.file)
|
||||
ctx^ = Context{}
|
||||
}
|
||||
|
||||
@@ -288,12 +293,12 @@ _buffer_name_process :: proc "contextless" (ctx: ^Context, buffer: ^Buffer, name
|
||||
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, private="package")
|
||||
write :: proc "contextless" (fd: uintptr, buf: []byte) {
|
||||
_write(fd, buf)
|
||||
}
|
||||
|
||||
@(no_instrumentation)
|
||||
@(no_instrumentation, private="package")
|
||||
tick_now :: proc "contextless" () -> (ns: i64) {
|
||||
return _tick_now()
|
||||
}
|
||||
|
||||
@@ -1,19 +1,17 @@
|
||||
#+private
|
||||
package spall
|
||||
|
||||
// Only for types and constants.
|
||||
import "core:os"
|
||||
|
||||
// Package is `#+no-instrumentation`, safe to use.
|
||||
import "core:sys/linux"
|
||||
|
||||
MAX_RW :: 0x7fffffff
|
||||
|
||||
@(no_instrumentation)
|
||||
_write :: proc "contextless" (fd: os.Handle, data: []byte) -> (n: int, err: os.Error) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
||||
_write :: proc "contextless" (fd: uintptr, data: []byte) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
||||
n: int
|
||||
for n < len(data) {
|
||||
chunk := data[:min(len(data), MAX_RW)]
|
||||
n += linux.write(linux.Fd(fd), chunk) or_return
|
||||
n += linux.write(linux.Fd(fd), chunk) or_break
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
@@ -2,29 +2,23 @@
|
||||
#+build darwin, freebsd, openbsd, netbsd
|
||||
package spall
|
||||
|
||||
// Only for types.
|
||||
import "core:os"
|
||||
|
||||
import "core:sys/posix"
|
||||
|
||||
MAX_RW :: 0x7fffffff
|
||||
|
||||
@(no_instrumentation)
|
||||
_write :: proc "contextless" (fd: os.Handle, data: []byte) -> (n: int, err: os.Error) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
||||
if len(data) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
_write :: proc "contextless" (fd: uintptr, data: []byte) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
||||
n: int
|
||||
for n < len(data) {
|
||||
chunk := data[:min(len(data), MAX_RW)]
|
||||
written := posix.write(posix.FD(fd), raw_data(chunk), len(chunk))
|
||||
if written < 0 {
|
||||
return n, os.get_last_error()
|
||||
return
|
||||
}
|
||||
n += written
|
||||
}
|
||||
|
||||
return n, nil
|
||||
return
|
||||
}
|
||||
|
||||
// NOTE(tetra): "RAW" means: Not adjusted by NTP.
|
||||
|
||||
@@ -1,20 +1,13 @@
|
||||
#+private
|
||||
package spall
|
||||
|
||||
// Only for types.
|
||||
import "core:os"
|
||||
|
||||
// Package is `#+no-instrumentation`, safe to use.
|
||||
import win32 "core:sys/windows"
|
||||
|
||||
MAX_RW :: 1<<30
|
||||
|
||||
@(no_instrumentation)
|
||||
_write :: proc "contextless" (fd: os.Handle, data: []byte) -> (int, os.Error) #no_bounds_check /* bounds check would segfault instrumentation */ {
|
||||
if len(data) == 0 {
|
||||
return 0, nil
|
||||
}
|
||||
|
||||
_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))
|
||||
@@ -25,11 +18,12 @@ _write :: proc "contextless" (fd: os.Handle, data: []byte) -> (int, os.Error) #n
|
||||
|
||||
e := win32.WriteFile(win32.HANDLE(fd), &data[total_write], to_write, &single_write_length, nil)
|
||||
if single_write_length <= 0 || !e {
|
||||
return int(total_write), os.get_last_error()
|
||||
return
|
||||
}
|
||||
total_write += i64(single_write_length)
|
||||
}
|
||||
return int(total_write), nil
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
@(no_instrumentation)
|
||||
|
||||
Reference in New Issue
Block a user