Compare commits

..

No commits in common. "0360a2da3550715ebd9958b48c1d578319ff3777" and "5e84e270bc3930038b9924f99d6ab47c235ca813" have entirely different histories.

9 changed files with 54 additions and 188 deletions

View File

@ -5,7 +5,7 @@
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/genhavr", "program": "${workspaceFolder}/main",
"args": [ "args": [
"--cluster", "--cluster",
"10" "10"
@ -16,20 +16,10 @@
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"cwd": "${workspaceFolder}", "cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/genhavr", "program": "${workspaceFolder}/main",
"args": [ "args": [
"10" "10"
] ]
},
{
"name": "Debug processor",
"type": "cppdbg",
"request": "launch",
"cwd": "${workspaceFolder}",
"program": "${workspaceFolder}/prochavr",
"args": [
"${workspaceFolder}/pairs.json"
]
} }
] ]
} }

View File

@ -62,7 +62,6 @@ PROCSRC="./$JSON_BUILD_DIR/*.o \
PROCOUT=prochavr PROCOUT=prochavr
if [[ $ENABLE_PROFILING == true ]]; then if [[ $ENABLE_PROFILING == true ]]; then
JSONFLAGS+="-DENABLE_PROFILING"
PROCSRC+="./$PROF_BUILD_DIR/*.o" PROCSRC+="./$PROF_BUILD_DIR/*.o"
PROCFLAGS="-DENABLE_PROFILING" PROCFLAGS="-DENABLE_PROFILING"

View File

@ -1,22 +0,0 @@
#ifndef PROFILER_IDS_H
#define PROFILER_IDS_H
enum profiler_ids {
PROFILER_ID_CLI_PARSE,
PROFILER_ID_JSON_PARSE,
PROFILER_ID_READ_JSON_FILE,
PROFILER_ID_PARSER_SETUP,
PROFILER_ID_LEX_GET_TOKEN,
PROFILER_ID_PARSE_TOKEN,
PROFILER_ID_PARSER_TEAR_DOWN,
PROFILER_ID_LOAD_JSON_PAIRS,
PROFILER_ID_READ_BINARY,
PROFILER_ID_HAVERSINE_SUM,
PROFILER_ID_HAVERSINE_DISTANCE,
PROFILER_ID_HAVERSINE_AVG,
PROFILER_ID_TEAR_DOWN,
COUNT_PROFILER_IDS,
};
#endif // !PROFILER_IDS_H

View File

@ -8,12 +8,12 @@
#endif // !MAX_PROFILE_SAMPLES #endif // !MAX_PROFILE_SAMPLES
#ifdef ENABLE_PROFILING #ifdef ENABLE_PROFILING
#define PROFILE_START(COUNT) profile_start(COUNT) #define PROFILE_START profile_start()
#define PROFILE_END profile_end() #define PROFILE_END profile_end()
#define SAMPLE_START(ID, TITLE) sample_start(ID, TITLE) #define SAMPLE_START(ID, TITLE) profiler_sample_t ID = sample_start(TITLE)
#define SAMPLE_END(ID) sample_end(ID) #define SAMPLE_END(ID) sample_end(&ID)
#else #else
#define PROFILE_START(COUNT) #define PROFILE_START
#define PROFILE_END #define PROFILE_END
#define SAMPLE_START(ID, TITLE) #define SAMPLE_START(ID, TITLE)
#define SAMPLE_END(ID) #define SAMPLE_END(ID)
@ -23,16 +23,11 @@
extern "C" { extern "C" {
#endif #endif
typedef struct sample profiler_sample_t; typedef struct {
struct sample {
const char *title; const char *title;
u64 start; u64 start;
u64 duration; u64 end;
u64 children_duration; } profiler_sample_t;
u64 hit_count;
profiler_sample_t *parent;
};
u64 get_os_frequency(); u64 get_os_frequency();
@ -45,11 +40,11 @@ u64 read_cpu_timer(void);
// CPU frequency in hz/sec // CPU frequency in hz/sec
u64 get_cpu_freq(u64 milliseconds); u64 get_cpu_freq(u64 milliseconds);
void profile_start(u64 count); void profile_start();
void profile_end(); void profile_end();
void sample_start(u64 id, const char *title); profiler_sample_t sample_start(const char *title);
void sample_end(u64 id); void sample_end(profiler_sample_t *sample);
#ifdef __cplusplus #ifdef __cplusplus
} }

View File

@ -29,10 +29,6 @@ dstr_t *dstr_with_capacity(u64 capacity) {
} }
dstr_t *dstr_from_string(const char *str) { dstr_t *dstr_from_string(const char *str) {
if (!str) {
return NULL;
}
u64 length = strlen(str); u64 length = strlen(str);
u64 capacity = length * CAPACITY_SCALAR; u64 capacity = length * CAPACITY_SCALAR;
@ -50,7 +46,7 @@ dstr_t *dstr_from_string(const char *str) {
} }
void dstr_update(dstr_t **dst, const char *src) { void dstr_update(dstr_t **dst, const char *src) {
if (!dst || !(*dst)) { if (!(*dst)) {
return; return;
} }
@ -82,7 +78,7 @@ void dstr_update(dstr_t **dst, const char *src) {
} }
void dstr_free(dstr_t **str) { void dstr_free(dstr_t **str) {
if (!str || !(*str)) { if (!(*str)) {
return; return;
} }
@ -91,7 +87,7 @@ void dstr_free(dstr_t **str) {
} }
void dstr_concat(dstr_t **dst, const char *src) { void dstr_concat(dstr_t **dst, const char *src) {
if (!dst || !(*dst)) { if (!(*dst)) {
return; return;
} }
@ -113,7 +109,7 @@ void dstr_concat(dstr_t **dst, const char *src) {
} }
void dstr_append(dstr_t **dst, char c) { void dstr_append(dstr_t **dst, char c) {
if (!dst || !(*dst)) { if (!(*dst)) {
return; return;
} }
@ -129,10 +125,6 @@ void dstr_append(dstr_t **dst, char c) {
} }
void dstr_resize(dstr_t **str) { void dstr_resize(dstr_t **str) {
if (!str || !(*str)) {
return;
}
u64 capacity = (*str)->size; u64 capacity = (*str)->size;
dstr_t *tmp = (dstr_t *)realloc(*str, sizeof(dstr_t) + capacity + 1); dstr_t *tmp = (dstr_t *)realloc(*str, sizeof(dstr_t) + capacity + 1);

View File

@ -131,16 +131,8 @@ void free_json(jentity_t **entity) {
dstr_free(&(entt_ptr->pair.key)); dstr_free(&(entt_ptr->pair.key));
} }
if (!value) {
return;
}
switch (value->type) { switch (value->type) {
case JVAL_COLLECTION: case JVAL_COLLECTION:
if (!(value->collection)) {
break;
}
if (value->collection->begin) { if (value->collection->begin) {
free_json(&(value->collection->begin)); free_json(&(value->collection->begin));
} }

View File

@ -1,7 +1,5 @@
#include "json/parser.h" #include "json/parser.h"
#include "aliases.h" #include "aliases.h"
#include "profiler/ids.h"
#include "profiler/timer.h"
#include "json/dstring.h" #include "json/dstring.h"
#include "json/json_entities.h" #include "json/json_entities.h"
#include "json/lexer.h" #include "json/lexer.h"
@ -23,8 +21,6 @@ INTERNAL jentity_t *add_value(parser_t *parser);
INTERNAL void add_collection(parser_t *parser); INTERNAL void add_collection(parser_t *parser);
jentity_t *load_json(const char *filepath) { jentity_t *load_json(const char *filepath) {
SAMPLE_START(PROFILER_ID_READ_JSON_FILE, "READ JSON FILE");
FILE *fp = fopen(filepath, "r"); FILE *fp = fopen(filepath, "r");
if (!fp) { if (!fp) {
@ -44,10 +40,6 @@ jentity_t *load_json(const char *filepath) {
fclose(fp); fclose(fp);
SAMPLE_END(PROFILER_ID_READ_JSON_FILE);
SAMPLE_START(PROFILER_ID_PARSER_SETUP, "JSON PARSER SETUP");
lexer_t *lexer = NULL; lexer_t *lexer = NULL;
parser_t *parser = NULL; parser_t *parser = NULL;
@ -63,23 +55,15 @@ jentity_t *load_json(const char *filepath) {
return NULL; return NULL;
} }
SAMPLE_END(PROFILER_ID_PARSER_SETUP);
SAMPLE_START(PROFILER_ID_LEX_GET_TOKEN, "GET NEXT TOKEN");
lex_result_t result = get_next_token(lexer, json); lex_result_t result = get_next_token(lexer, json);
SAMPLE_END(PROFILER_ID_LEX_GET_TOKEN);
if (result.error.errno) { if (result.error.errno) {
printf("%s\n", result.error.msg); printf("%s\n", result.error.msg);
} else { } else {
while (result.token.type != TK_NO_TOKEN) { while (result.token.type != TK_NO_TOKEN) {
SAMPLE_START(PROFILER_ID_PARSE_TOKEN, "PARSE TOKEN");
parse_token(parser, result.token); parse_token(parser, result.token);
SAMPLE_END(PROFILER_ID_PARSE_TOKEN);
SAMPLE_START(PROFILER_ID_LEX_GET_TOKEN, "GET NEXT TOKEN");
result = get_next_token(lexer, NULL); result = get_next_token(lexer, NULL);
SAMPLE_END(PROFILER_ID_LEX_GET_TOKEN);
if (result.error.errno) { if (result.error.errno) {
printf("%s\n", result.error.msg); printf("%s\n", result.error.msg);
@ -90,14 +74,10 @@ jentity_t *load_json(const char *filepath) {
jentity_t *root = parser->root; jentity_t *root = parser->root;
SAMPLE_START(PROFILER_ID_PARSER_TEAR_DOWN, "PARSER TEAR DOWN");
parser_free(&parser); parser_free(&parser);
lexer_free(&lexer); lexer_free(&lexer);
free(json); free(json);
SAMPLE_END(PROFILER_ID_PARSER_TEAR_DOWN);
return root; return root;
} }

View File

@ -1,7 +1,6 @@
#include "haversine.h" #include "haversine.h"
#include "point_types.h" #include "point_types.h"
#include "processor/proc_argparser.h" #include "processor/proc_argparser.h"
#include "profiler/ids.h"
#include "profiler/timer.h" #include "profiler/timer.h"
#include "json/dstring.h" #include "json/dstring.h"
#include "json/json_entities.h" #include "json/json_entities.h"
@ -15,21 +14,21 @@
#include <string.h> #include <string.h>
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
PROFILE_START(COUNT_PROFILER_IDS); PROFILE_START;
SAMPLE_START(PROFILER_ID_CLI_PARSE, "CLI PARSING"); SAMPLE_START(cli_parse, "CLI PARSING");
ProcessorArgs args = parse_args(argc, argv); ProcessorArgs args = parse_args(argc, argv);
SAMPLE_END(PROFILER_ID_CLI_PARSE); SAMPLE_END(cli_parse);
SAMPLE_START(PROFILER_ID_JSON_PARSE, "JSON PARSING"); SAMPLE_START(json_parse, "JSON PARSING");
jentity_t *root = load_json(args.filepath); jentity_t *root = load_json(args.filepath);
assert(root->type == JENTITY_SINGLE && root->value.type == JVAL_COLLECTION); assert(root->type == JENTITY_SINGLE && root->value.type == JVAL_COLLECTION);
SAMPLE_END(PROFILER_ID_JSON_PARSE); SAMPLE_END(json_parse);
SAMPLE_START(PROFILER_ID_LOAD_JSON_PAIRS, "LOAD JSON PAIRS"); SAMPLE_START(load_pairs_json, "LOAD JSON PAIRS");
jentity_t *pairs = root->value.collection->begin; jentity_t *pairs = root->value.collection->begin;
@ -62,9 +61,9 @@ int main(int argc, char *argv[]) {
point_pairs[index++] = p; point_pairs[index++] = p;
} }
SAMPLE_END(PROFILER_ID_LOAD_JSON_PAIRS); SAMPLE_END(load_pairs_json);
SAMPLE_START(PROFILER_ID_READ_BINARY, "BINARY READ"); SAMPLE_START(binary_file_read, "BINARY READ");
const char *filename = "count_and_distances"; const char *filename = "count_and_distances";
@ -76,17 +75,15 @@ int main(int argc, char *argv[]) {
fseek(fp, sizeof(u64), SEEK_SET); fseek(fp, sizeof(u64), SEEK_SET);
} }
SAMPLE_END(PROFILER_ID_READ_BINARY); SAMPLE_END(binary_file_read);
SAMPLE_START(PROFILER_ID_HAVERSINE_SUM, "HAVERSINE SUM"); SAMPLE_START(haversine_sum, "HAVERSINE SUM");
f64 sum = 0.0; f64 sum = 0.0;
f64 distance = 0.0; f64 distance = 0.0;
f64 saved_distance = 0.0; f64 saved_distance = 0.0;
for (u64 i = 0; i < pair_count; ++i) { for (u64 i = 0; i < pair_count; ++i) {
SAMPLE_START(PROFILER_ID_HAVERSINE_DISTANCE, "HAVERSINE DISTANCE");
distance = haversine_of_degrees(point_pairs[i], EARTH_RADIUS_KM); distance = haversine_of_degrees(point_pairs[i], EARTH_RADIUS_KM);
SAMPLE_END(PROFILER_ID_HAVERSINE_DISTANCE);
if (fp) { if (fp) {
fread(&saved_distance, sizeof(f64), 1, fp); fread(&saved_distance, sizeof(f64), 1, fp);
@ -100,13 +97,13 @@ int main(int argc, char *argv[]) {
sum += distance; sum += distance;
} }
SAMPLE_END(PROFILER_ID_HAVERSINE_SUM); SAMPLE_END(haversine_sum);
SAMPLE_START(PROFILER_ID_HAVERSINE_AVG, "HAVERSINE AVERAGE"); SAMPLE_START(haversine_average, "HAVERSINE AVERAGE");
printf("\nAVERAGE DISTANCE: %f\n", sum / pair_count); printf("\nAVERAGE DISTANCE: %f\n", sum / pair_count);
SAMPLE_END(PROFILER_ID_HAVERSINE_AVG); SAMPLE_END(haversine_average);
SAMPLE_START(PROFILER_ID_TEAR_DOWN, "TEAR DOWN"); SAMPLE_START(tear_down, "TEAR DOWN");
if (fp) { if (fp) {
fclose(fp); fclose(fp);
@ -114,7 +111,7 @@ int main(int argc, char *argv[]) {
free(point_pairs); free(point_pairs);
SAMPLE_END(PROFILER_ID_TEAR_DOWN); SAMPLE_END(tear_down);
PROFILE_END; PROFILE_END;

View File

@ -13,7 +13,6 @@ typedef struct {
u64 start; u64 start;
u64 end; u64 end;
u64 max_title_length; u64 max_title_length;
profiler_sample_t *active;
} profiler_t; } profiler_t;
INTERNAL profiler_t profiler = {0}; INTERNAL profiler_t profiler = {0};
@ -59,11 +58,10 @@ u64 get_cpu_freq(u64 milliseconds) {
return cpu_freq; return cpu_freq;
} }
void profile_start(u64 count) { void profile_start() {
profiler.cpu_freq = get_cpu_freq(500); profiler.cpu_freq = get_cpu_freq(500);
profiler.start = read_cpu_timer(); profiler.start = read_cpu_timer();
profiler.max_title_length = 0; profiler.max_title_length = 0;
profiler.size = count;
} }
void profile_end() { void profile_end() {
@ -73,16 +71,11 @@ void profile_end() {
profiler.end = read_cpu_timer(); profiler.end = read_cpu_timer();
u64 total = 0; u64 total = profiler.end - profiler.start;
if (profiler.end >= profiler.start) {
total = profiler.end - profiler.start;
}
u16 time_precision = 16; u16 time_precision = 16;
u16 time_char_count = 20; u16 time_char_count = 20;
u16 duration_char_count = 22; u16 duration_char_count = 22;
u16 hits_char_count = 10;
u16 percentage_precision = 8; u16 percentage_precision = 8;
u16 percentage_char_count = 12; u16 percentage_char_count = 12;
@ -101,85 +94,35 @@ void profile_end() {
for (u64 i = 0; i < profiler.size; ++i) { for (u64 i = 0; i < profiler.size; ++i) {
sample = &(profiler.samples[i]); sample = &(profiler.samples[i]);
if (sample->hit_count == 0) { u64 duration = sample->end - sample->start;
continue;
}
u64 duration = 0; printf("%*s: %*lld (%*.*f %%)\n", (i32)profiler.max_title_length,
sample->title, duration_char_count, (unsigned long long)duration,
if (sample->duration >= sample->children_duration) { percentage_char_count, percentage_precision,
duration = sample->duration - sample->children_duration; (f64)duration / total * 100.0);
}
printf("%*s (hits: %*lld): %*lld (%*.*f %%", (i32)profiler.max_title_length,
sample->title, hits_char_count,
(unsigned long long)sample->hit_count, duration_char_count,
(unsigned long long)duration, percentage_char_count,
percentage_precision, (f64)duration / total * 100.0);
if (sample->children_duration > 0) {
printf(", w/ children: %*.*f %%", percentage_char_count,
percentage_precision, (f64)sample->duration / total * 100.0);
}
printf(")\n");
} }
} }
void sample_start(u64 id, const char *title) { profiler_sample_t sample_start(const char *title) {
if (id >= MAX_PROFILE_SAMPLES) { return (profiler_sample_t){
.title = title,
.start = read_cpu_timer(),
.end = 0,
};
}
void sample_end(profiler_sample_t *sample) {
if (profiler.size == MAX_PROFILE_SAMPLES) {
return; return;
} }
profiler_sample_t *sample = &(profiler.samples[id]);
if (!(sample->title) || strcmp(title, sample->title) != 0) {
sample->title = title;
sample->start = 0;
sample->duration = 0;
sample->children_duration = 0;
sample->hit_count = 0;
sample->parent = NULL;
u64 length = strlen(sample->title); u64 length = strlen(sample->title);
if (length > profiler.max_title_length) { if (length > profiler.max_title_length) {
profiler.max_title_length = length; profiler.max_title_length = length;
} }
}
sample->end = read_cpu_timer();
sample->start = read_cpu_timer();
++(sample->hit_count); profiler.samples[(profiler.size)++] = *sample;
if (sample != profiler.active) {
// This handles recursive functions by changing the parent only when a
// function isn't calling itself
sample->parent = profiler.active;
}
profiler.active = sample;
}
void sample_end(u64 id) {
if (id >= MAX_PROFILE_SAMPLES) {
return;
}
profiler_sample_t *sample = &(profiler.samples[id]);
u64 duration = read_cpu_timer() - sample->start;
if (!(sample->parent) || sample != sample->parent) {
// This handles recursive functions by adding to the duration only when a
// function isn't calling itself
sample->duration += duration;
}
if (sample->parent && sample != sample->parent) {
// This handles recursive functions by adding to the children_duration only
// when a function isn't calling itself
sample->parent->children_duration += duration;
}
profiler.active = sample->parent;
} }