Use sample_start and sample_end pairs instead of the PROFILER_SAMPLE
macro
This commit is contained in:
parent
0073114723
commit
c053d20a8f
@ -3,17 +3,16 @@
|
|||||||
|
|
||||||
#include "aliases.h"
|
#include "aliases.h"
|
||||||
|
|
||||||
// TODO (Abdelrahman): Not fully satisfied with this solution
|
|
||||||
#define PROFILER_SAMPLE(ID, TITLE, CODE) \
|
|
||||||
u64 ID##_start = read_cpu_timer(); \
|
|
||||||
CODE; \
|
|
||||||
u64 ID##_end = read_cpu_timer(); \
|
|
||||||
add_sample(TITLE, ID##_end - ID##_start);
|
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
extern "C" {
|
extern "C" {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
const char *title;
|
||||||
|
u64 start;
|
||||||
|
u64 end;
|
||||||
|
} profiler_sample_t;
|
||||||
|
|
||||||
u64 get_os_frequency();
|
u64 get_os_frequency();
|
||||||
|
|
||||||
// Time in nanoseconds
|
// Time in nanoseconds
|
||||||
@ -28,7 +27,8 @@ u64 get_cpu_freq(u64 milliseconds);
|
|||||||
void profile_start();
|
void profile_start();
|
||||||
void profile_end();
|
void profile_end();
|
||||||
|
|
||||||
void add_sample(const char *title, u64 duration);
|
profiler_sample_t sample_start(const char *title);
|
||||||
|
void sample_end(profiler_sample_t *sample);
|
||||||
|
|
||||||
#ifdef __cplusplus
|
#ifdef __cplusplus
|
||||||
}
|
}
|
||||||
|
@ -16,18 +16,18 @@
|
|||||||
int main(int argc, char *argv[]) {
|
int main(int argc, char *argv[]) {
|
||||||
profile_start();
|
profile_start();
|
||||||
|
|
||||||
// clang-format off
|
profiler_sample_t cli_parse = sample_start("CLI PARSING");
|
||||||
PROFILER_SAMPLE(cli_parse, "CLI PARSING",
|
|
||||||
ProcessorArgs args = parse_args(argc, argv);
|
ProcessorArgs args = parse_args(argc, argv);
|
||||||
);
|
sample_end(&cli_parse);
|
||||||
|
|
||||||
|
profiler_sample_t json_parse = sample_start("JSON PARSING");
|
||||||
|
|
||||||
PROFILER_SAMPLE(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(&json_parse);
|
||||||
|
|
||||||
PROFILER_SAMPLE(load_pairs_json, "LOAD JSON PAIRS",
|
profiler_sample_t load_pairs_json = sample_start("LOAD JSON PAIRS");
|
||||||
jentity_t *pairs = root->value.collection->begin;
|
jentity_t *pairs = root->value.collection->begin;
|
||||||
|
|
||||||
assert(pairs->type == JENTITY_PAIR &&
|
assert(pairs->type == JENTITY_PAIR &&
|
||||||
@ -58,9 +58,11 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
point_pairs[index++] = p;
|
point_pairs[index++] = p;
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
PROFILER_SAMPLE(binary_file_read, "BINARY READ",
|
sample_end(&load_pairs_json);
|
||||||
|
|
||||||
|
profiler_sample_t binary_file_read = sample_start("BINARY READ");
|
||||||
|
|
||||||
const char *filename = "count_and_distances";
|
const char *filename = "count_and_distances";
|
||||||
|
|
||||||
FILE *fp = fopen(filename, "r");
|
FILE *fp = fopen(filename, "r");
|
||||||
@ -70,9 +72,11 @@ int main(int argc, char *argv[]) {
|
|||||||
// Skip the count
|
// Skip the count
|
||||||
fseek(fp, sizeof(u64), SEEK_SET);
|
fseek(fp, sizeof(u64), SEEK_SET);
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
PROFILER_SAMPLE(haversine_sum, "HAVERSINE SUM",
|
sample_end(&binary_file_read);
|
||||||
|
|
||||||
|
profiler_sample_t haversine_sum = sample_start("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;
|
||||||
@ -90,22 +94,24 @@ int main(int argc, char *argv[]) {
|
|||||||
|
|
||||||
sum += distance;
|
sum += distance;
|
||||||
}
|
}
|
||||||
);
|
|
||||||
|
|
||||||
PROFILER_SAMPLE(haversine_average, "HAVERSINE AVERAGE",
|
sample_end(&haversine_sum);
|
||||||
|
|
||||||
|
profiler_sample_t haversine_average = sample_start("HAVERSINE AVERAGE");
|
||||||
printf("\nAVERAGE DISTANCE: %f\n", sum / pair_count);
|
printf("\nAVERAGE DISTANCE: %f\n", sum / pair_count);
|
||||||
);
|
sample_end(&haversine_average);
|
||||||
|
|
||||||
|
profiler_sample_t tear_down = sample_start("TEAR DOWN");
|
||||||
|
|
||||||
PROFILER_SAMPLE(tear_down, "TEAR DOWN",
|
|
||||||
if (fp) {
|
if (fp) {
|
||||||
fclose(fp);
|
fclose(fp);
|
||||||
}
|
}
|
||||||
|
|
||||||
free(point_pairs);
|
free(point_pairs);
|
||||||
);
|
|
||||||
|
sample_end(&tear_down);
|
||||||
|
|
||||||
profile_end();
|
profile_end();
|
||||||
// clang-format on
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
@ -8,11 +8,6 @@
|
|||||||
|
|
||||||
#define MAX_PROFILE_SAMPLES 1024
|
#define MAX_PROFILE_SAMPLES 1024
|
||||||
|
|
||||||
typedef struct {
|
|
||||||
const char *title;
|
|
||||||
u64 duration;
|
|
||||||
} profiler_sample_t;
|
|
||||||
|
|
||||||
typedef struct {
|
typedef struct {
|
||||||
profiler_sample_t samples[MAX_PROFILE_SAMPLES];
|
profiler_sample_t samples[MAX_PROFILE_SAMPLES];
|
||||||
u64 cpu_freq;
|
u64 cpu_freq;
|
||||||
@ -101,26 +96,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]);
|
||||||
|
|
||||||
|
u64 duration = sample->end - sample->start;
|
||||||
|
|
||||||
printf("%*s: %*lld (%*.*f %%)\n", (i32)profiler.max_title_length,
|
printf("%*s: %*lld (%*.*f %%)\n", (i32)profiler.max_title_length,
|
||||||
sample->title, duration_char_count,
|
sample->title, duration_char_count, (unsigned long long)duration,
|
||||||
(unsigned long long)sample->duration, percentage_char_count,
|
percentage_char_count, percentage_precision,
|
||||||
percentage_precision, (f64)sample->duration / total * 100.0);
|
(f64)duration / total * 100.0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
void add_sample(const char *title, u64 duration) {
|
profiler_sample_t sample_start(const char *title) {
|
||||||
|
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) {
|
if (profiler.size == MAX_PROFILE_SAMPLES) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
u64 length = strlen(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;
|
||||||
}
|
}
|
||||||
|
|
||||||
profiler.samples[(profiler.size)++] = (profiler_sample_t){
|
sample->end = read_cpu_timer();
|
||||||
.title = title,
|
|
||||||
.duration = duration,
|
profiler.samples[(profiler.size)++] = *sample;
|
||||||
};
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user