From: Brendan Hansen Date: Fri, 25 Sep 2020 03:51:55 +0000 (-0500) Subject: added basics of timing the different stages of compilation X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=3ecf2a46911fa69bd25cd56ed1ba6acf926656d9;p=onyx.git added basics of timing the different stages of compilation --- diff --git a/Makefile b/Makefile index 302e4efa..b073230e 100644 --- a/Makefile +++ b/Makefile @@ -1,4 +1,5 @@ RELEASE=1 +TIME=0 OBJ_FILES=\ build/onyxlex.o \ @@ -25,6 +26,10 @@ else endif endif +ifeq ($(TIME), 1) + TIMEFLAG=-DREPORT_TIMES=1 +endif + INCLUDES=-I./include LIBS= TARGET=./onyx @@ -40,10 +45,10 @@ else endif build/%.o: src/%.c include/bh.h - $(CC) $(FLAGS) -c $< -o $@ $(INCLUDES) + $(CC) $(TIMEFLAG) $(FLAGS) -c $< -o $@ $(INCLUDES) $(TARGET): $(OBJ_FILES) - $(CC) $(FLAGS) $(OBJ_FILES) -o $@ $(LIBS) + $(CC) $(TIMEFLAG) $(FLAGS) $(OBJ_FILES) -o $@ $(LIBS) install: $(TARGET) core/* cp $(TARGET) /usr/bin/ diff --git a/include/bh.h b/include/bh.h index ffb5379f..c179531b 100644 --- a/include/bh.h +++ b/include/bh.h @@ -9,6 +9,7 @@ #include #include #include +#include #include #include @@ -769,6 +770,12 @@ BH_ALLOCATOR_PROC(bh_managed_heap_allocator_proc); +//------------------------------------------------------------------------------ +// TIME / DURATION +//------------------------------------------------------------------------------ +u64 bh_time_curr(); +u64 bh_time_duration(u64 old); + @@ -2263,6 +2270,31 @@ void bh_imap_clear(bh_imap* imap) { #endif // ifndef BH_NO_IMAP + + + + + + +u64 bh_time_curr() { + struct timespec spec; + clock_gettime(CLOCK_MONOTONIC, &spec); + + time_t sec = spec.tv_sec; + u64 ms = spec.tv_nsec / 1000000; + if (ms > 999) { + sec++; + ms = 0; + } + + return sec * 1000 + ms; +} + +u64 bh_time_duration(u64 old) { + u64 curr = bh_time_curr(); + return curr - old; +} + #endif // ifdef BH_DEFINE #endif // ifndef BH_H diff --git a/misc/onyx.vim b/misc/onyx.vim index 2afb167e..e06ab001 100644 --- a/misc/onyx.vim +++ b/misc/onyx.vim @@ -37,7 +37,7 @@ syn keyword onyxCommentStart contained TODO NOTE BUG HACK syn region onyxComment start="//" end="$" keepend contains=onyxCommentStart syn region onyxComment start="/\*" end="\*/" contains=onyxCommentStart -syn match onyxDefinitionGroup "\<[a-zA-Z_][a-zA-Z0-9_]*\> *::" contains=onyxDefinition +syn match onyxDefinitionGroup "\<[a-zA-Z_][a-zA-Z0-9_]*\> *:" contains=onyxDefinition syn match onyxDefinition "\<[a-zA-Z_][a-zA-Z0-9_]*\>" contained syn match onyxDirective "\#[a-zA-Z_]\+" diff --git a/onyx b/onyx index 70b0a692..2579a91e 100755 Binary files a/onyx and b/onyx differ diff --git a/src/onyx.c b/src/onyx.c index 5036c03c..a5a6d789 100644 --- a/src/onyx.c +++ b/src/onyx.c @@ -20,6 +20,38 @@ #endif + + +#ifdef REPORT_TIMES +// CLEANUP: Move this to another file +typedef struct TimerStackElem { + char* label; + u64 time; +} TimerStackElem; + +static bh_arr(TimerStackElem) global_timer_stack; + +void timer_stack_init() { + global_timer_stack = NULL; + bh_arr_new(global_heap_allocator, global_timer_stack, 4); +} + +void timer_stack_push(char* label) { + TimerStackElem elem; + elem.label = label; + elem.time = bh_time_curr(); + bh_arr_push(global_timer_stack, elem); +} + +void timer_stack_pop() { + TimerStackElem elem = bh_arr_pop(global_timer_stack); + u64 duration = bh_time_duration(elem.time); + bh_printf("[Time] %s took %lms.\n", elem.label, duration); +} +#endif + + + static const char* docstring = "Onyx compiler version " VERSION "\n" "\n" "The compiler for the Onyx programming language.\n" @@ -329,6 +361,10 @@ static void merge_parse_results(CompilerState* compiler_state, ParseResults* res static CompilerProgress process_source_file(CompilerState* compiler_state, char* filename) { if (bh_table_has(bh_file_contents, compiler_state->loaded_files, filename)) return ONYX_COMPILER_PROGRESS_SUCCESS; +#ifdef REPORT_TIMES + timer_stack_push(bh_aprintf(global_heap_allocator, "Parsing '%s'", filename)); +#endif + bh_file file; bh_file_error err = bh_file_open(&file, filename); @@ -360,6 +396,10 @@ static CompilerProgress process_source_file(CompilerState* compiler_state, char* ParseResults results = parse_source_file(compiler_state, &fc); merge_parse_results(compiler_state, &results); +#ifdef REPORT_TIMES + timer_stack_pop(); +#endif + if (onyx_has_errors()) { return ONYX_COMPILER_PROGRESS_FAILED_PARSE; } else { @@ -370,6 +410,10 @@ static CompilerProgress process_source_file(CompilerState* compiler_state, char* static i32 onyx_compile(CompilerState* compiler_state) { +#ifdef REPORT_TIMES + timer_stack_push("Parsing"); +#endif + // NOTE: While the queue is not empty, process the next file while (!bh_arr_is_empty(compiler_state->queued_files)) { CompilerProgress result = process_source_file(compiler_state, (char *) compiler_state->queued_files[0]); @@ -380,6 +424,13 @@ static i32 onyx_compile(CompilerState* compiler_state) { bh_arr_fastdelete(compiler_state->queued_files, 0); } +#ifdef REPORT_TIMES + timer_stack_pop(); + + timer_stack_push("Checking semantics"); + timer_stack_push("Initializing builtins"); +#endif + initialize_builtins(compiler_state->ast_alloc, &compiler_state->prog_info); if (onyx_has_errors()) { return ONYX_COMPILER_PROGRESS_FAILED_SEMPASS; @@ -400,6 +451,10 @@ static i32 onyx_compile(CompilerState* compiler_state) { sizeof(Entity), sort_entities); +#ifdef REPORT_TIMES + timer_stack_pop(); +#endif + // NOTE: Check types and semantic rules if (compiler_state->options->verbose_output) bh_printf("[Checking semantics]\n"); @@ -407,6 +462,10 @@ static i32 onyx_compile(CompilerState* compiler_state) { onyx_sempass_init(compiler_state->sp_alloc, compiler_state->ast_alloc); onyx_sempass(&compiler_state->prog_info); +#ifdef REPORT_TIMES + timer_stack_pop(); +#endif + if (onyx_has_errors()) { return ONYX_COMPILER_PROGRESS_FAILED_SEMPASS; } @@ -423,6 +482,11 @@ static i32 onyx_compile(CompilerState* compiler_state) { if (compiler_state->options->verbose_output) bh_printf("[Generating WASM]\n"); +#ifdef REPORT_TIMES + timer_stack_push("Code generation"); + timer_stack_push("Generating WASM"); +#endif + compiler_state->wasm_mod = onyx_wasm_module_create(compiler_state->options->allocator); onyx_wasm_module_compile(&compiler_state->wasm_mod, &compiler_state->prog_info); @@ -430,6 +494,10 @@ static i32 onyx_compile(CompilerState* compiler_state) { return ONYX_COMPILER_PROGRESS_FAILED_BINARY_GEN; } +#ifdef REPORT_TIMES + timer_stack_pop(); + timer_stack_push("Write to file"); +#endif // NOTE: Output to file bh_file output_file; @@ -442,6 +510,11 @@ static i32 onyx_compile(CompilerState* compiler_state) { onyx_wasm_module_write_to_file(&compiler_state->wasm_mod, output_file); +#ifdef REPORT_TIMES + timer_stack_pop(); + timer_stack_pop(); +#endif + return ONYX_COMPILER_PROGRESS_SUCCESS; }