Files
thor/main.odin
T

119 lines
2.8 KiB
Odin

package main
import "base:runtime"
import "core:log"
import "core:mem"
import "core:os"
import "core:prof/spall"
import "core:sync"
import "core:time"
import "treesitter"
SPALL :: #config(SPALL, false)
when SPALL {
spall_ctx: spall.Context
@(thread_local)
spall_buffer: spall.Buffer
init_spall_for_thread :: proc() {
backing := make([]u8, spall.BUFFER_DEFAULT_SIZE, context.temp_allocator)
spall_buffer = spall.buffer_create(backing, u32(sync.current_thread_id()))
}
cleanup_spall_for_thread :: proc() {
spall.buffer_destroy(&spall_ctx, &spall_buffer)
}
}
main :: proc() {
when SPALL {
ctx, ok := spall.context_create_with_scale("thor.spall", false, 1.0)
if !ok {
log.fatal("Failed to create spall trace file")
os.exit(1)
}
spall_ctx = ctx
defer spall.context_destroy(&spall_ctx)
spall_backing := make([]u8, spall.BUFFER_DEFAULT_SIZE)
defer delete(spall_backing)
spall_buffer = spall.buffer_create(spall_backing, u32(sync.current_thread_id()))
defer spall.buffer_destroy(&spall_ctx, &spall_buffer)
}
logger_opts: log.Options =
(log.Default_Console_Logger_Opts - log.Full_Timestamp_Opts - {.Short_File_Path})
level := log.Level.Info
for arg in os.args {
switch (arg) {
case "-verbose":
level = .Debug;
break
case "-quiet":
level = .Warning;
break
}
}
console_logger := log.create_console_logger(level, logger_opts)
context.logger = console_logger
defer log.destroy_console_logger(console_logger)
treesitter.init_persistent()
when SPALL {
treesitter.set_thread_callbacks(init_spall_for_thread, cleanup_spall_for_thread)
}
for {
defer free_all(context.temp_allocator)
defer log.debugf(
"max_temp_allocator_size=%M",
(cast(^runtime.Default_Temp_Allocator)context.temp_allocator.data)^.arena.total_used,
)
tick := time.tick_now()
site: Site
init_site(&site, os.args)
defer destroy_site(&site)
// TODO: Make it so this isn't necessary
context.allocator = site_allocator(&site)
defer log.debugf(
"current_site_allocator_size=%M",
site.arena.block_size * (len(site.arena.used_blocks) + len(site.arena.unused_blocks)) -
site.arena.bytes_left,
)
build_vfs(&site)
treesitter.grammar_dir = site.grammars
treesitter.query_dir = site.queries
site_load_content(&site)
render_site(&site)
log.infof("Built site in %M", time.tick_since(tick))
(.Watch in site.features) or_break
time.sleep(5 * time.Second)
}
}
when SPALL {
@(instrumentation_enter)
spall_enter :: proc "contextless" (
proc_address, call_site_return_address: rawptr,
loc: runtime.Source_Code_Location,
) {
spall._buffer_begin(&spall_ctx, &spall_buffer, "", "", loc)
}
@(instrumentation_exit)
spall_exit :: proc "contextless" (
proc_address, call_site_return_address: rawptr,
loc: runtime.Source_Code_Location,
) {
spall._buffer_end(&spall_ctx, &spall_buffer)
}
}