Skip to content

Commit

Permalink
Allow optional sorting of profiler output via HL_PROFILER_SORT env var (
Browse files Browse the repository at this point in the history
Fixes #7638)
  • Loading branch information
steven-johnson committed Jun 15, 2023
1 parent 2149734 commit 9b55ca2
Showing 1 changed file with 38 additions and 2 deletions.
40 changes: 38 additions & 2 deletions src/runtime/profiler_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,23 @@ WEAK void halide_profiler_memory_free(void *user_context,
WEAK void halide_profiler_report_unlocked(void *user_context, halide_profiler_state *s) {
StringStreamPrinter<1024> sstr(user_context);

int64_t (*compare_fs_fn)(halide_profiler_func_stats * a, halide_profiler_func_stats * b) = nullptr;

const char *sort_str = getenv("HL_PROFILER_SORT");
if (sort_str) {
if (!strcmp(sort_str, "time")) {
// Sort by descending time
compare_fs_fn = [](halide_profiler_func_stats *a, halide_profiler_func_stats *b) -> int64_t {
return (int64_t)b->time - (int64_t)a->time;
};
} else if (!strcmp(sort_str, "name")) {
// Sort by ascending name
compare_fs_fn = [](halide_profiler_func_stats *a, halide_profiler_func_stats *b) -> int64_t {
return strcmp(a->name, b->name);
};
}
}

for (halide_profiler_pipeline_stats *p = s->pipelines; p;
p = (halide_profiler_pipeline_stats *)(p->next)) {
float t = p->time / 1000000.0f;
Expand Down Expand Up @@ -366,9 +383,9 @@ WEAK void halide_profiler_report_unlocked(void *user_context, halide_profiler_st
}

if (print_f_states) {
int f_stats_count = 0;
halide_profiler_func_stats **f_stats = (halide_profiler_func_stats **)__builtin_alloca(p->num_funcs * sizeof(halide_profiler_func_stats *));
for (int i = 0; i < p->num_funcs; i++) {
size_t cursor = 0;
sstr.clear();
halide_profiler_func_stats *fs = p->funcs + i;

// The first func is always a catch-all overhead
Expand All @@ -377,6 +394,25 @@ WEAK void halide_profiler_report_unlocked(void *user_context, halide_profiler_st
continue;
}

f_stats[f_stats_count++] = fs;
}

if (compare_fs_fn) {
for (int i = 1; i < f_stats_count; i++) {
for (int j = i; j > 0 && compare_fs_fn(f_stats[j - 1], f_stats[j]) > 0; j--) {
auto *a = f_stats[j - 1];
auto *b = f_stats[j];
f_stats[j - 1] = b;
f_stats[j] = a;
}
}
}

for (int i = 0; i < f_stats_count; i++) {
size_t cursor = 0;
sstr.clear();
halide_profiler_func_stats *fs = f_stats[i];

sstr << " " << fs->name << ": ";
cursor += 25;
while (sstr.size() < cursor) {
Expand Down

0 comments on commit 9b55ca2

Please sign in to comment.