Skip to content

Commit

Permalink
Add tests and documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
emilienlemaire committed Sep 7, 2023
1 parent bbad458 commit e5dace0
Show file tree
Hide file tree
Showing 14 changed files with 364 additions and 164 deletions.
6 changes: 3 additions & 3 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
2023-09-04 Emilien Lemaire <[email protected]>

* parser.y: save the names of the procedures in global variables.
* parser.y: generate `cob_perf_enter` and `cob_perf_exit` calls when
* parser.y: generate `cob_prof_enter` and `cob_prof_exit` calls when
needed
* flag.def: add `-fperf` to enable profiling
* flag.def: add `-fprof` to enable profiling
* cobc.h: add a `cb_any_list` struct, a linked list with `void*` type data
and global variables of this type, used for profiling
* typeck.c (emit_stop_run): add a call to `cob_perf_end` before the call
* typeck.c (emit_stop_run): add a call to `cob_prof_end` before the call
to `cob_stop_run`
* codegen.c: handle profiling code generation

Expand Down
44 changes: 12 additions & 32 deletions cobc/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -1793,7 +1793,7 @@ output_standard_includes (struct cb_program *prog)
}
}
output_line ("#include <libcob.h>");
if (cb_flag_perf) output_line ("#include <libcob/cobperf.h>");
if (cb_flag_prof) output_line ("#include <libcob/cobprof.h>");
output_newline ();
}

Expand Down Expand Up @@ -7875,11 +7875,11 @@ output_goto_1 (cb_tree x)
output_move (cb_space, cb_debug_contents);
}

if (cb_flag_perf) {
if (cb_flag_prof) {
if (!!lb->section) {
output_line("cob_perf_goto(\"%s\", \"%s\");", lb->section->name, lb->name);
output_line("cob_prof_goto(\"%s\", \"%s\");", lb->section->name, lb->name);
} else {
output_line("cob_perf_goto(NULL, \"%s\");", lb->name);
output_line("cob_prof_goto(NULL, \"%s\");", lb->name);
}
}
output_line ("goto %s%d;", CB_PREFIX_LABEL, lb->id);
Expand Down Expand Up @@ -12191,9 +12191,8 @@ output_internal_function (struct cb_program *prog, cb_tree parameter_list)

/* Entry dispatch */
output_line ("/* Entry dispatch */");
if (cb_flag_perf) {
output_line("cob_perf_init (total_times, paragraphs, paragraphs_count,\n"
" sections, sections_count,\n"
if (cb_flag_prof) {
output_line("cob_prof_init (total_times, sections, sections_count,\n"
" para_per_sections, max_paragraphs_count);");
}
if (cb_flag_stack_extended) {
Expand Down Expand Up @@ -12284,8 +12283,8 @@ output_internal_function (struct cb_program *prog, cb_tree parameter_list)
}
} else {
output_line ("/* Program exit */");
if (cb_flag_perf) {
output_line("cob_perf_end ();");
if (cb_flag_prof) {
output_line("cob_prof_end ();");
}
output_newline ();
if (needs_exit_prog) {
Expand Down Expand Up @@ -13648,7 +13647,7 @@ max_length_section() {
}

static void
output_cob_perf_data ()
output_cob_prof_data ()
{
size_t paragraphs_count = 0;
size_t sections_count = 0;
Expand All @@ -13668,30 +13667,11 @@ output_cob_perf_data ()
}
}

output_local ("/* cob_perf data */\n\n");
output_local ("/* cob_prof data */\n\n");
output_local ("static size_t called_paragraphs[255];\n");
output_local ("static long start_times[255];\n");
output_local ("static long total_times[%lu][255] = {0};\n", paragraphs_count);

output_local ("static const char *paragraphs[] = {\n");
{
struct cb_any_list *sl_p = sections_l;
struct cb_any_list *sn_p = sections_name;
while (!!sl_p && !!sn_p) {
struct cb_any_list *pl_p = (struct cb_any_list*)sl_p->data;
output_local(" \"%s\"", (char*)sn_p->data);
while (!!pl_p) {
output_local (",\n \"%s\"", (char*)pl_p->data);
pl_p = pl_p->next;
}
sl_p = sl_p->next;
sn_p = sn_p->next;
if (!!sl_p) output_local (",\n");
}
output_local("};\n");
}
output_local ("static size_t paragraphs_count = %lu;\n", paragraphs_count);

output_local ("static const char *sections[] = {\n");
{
struct cb_any_list *sn_p = sections_name;
Expand Down Expand Up @@ -13728,7 +13708,7 @@ output_cob_perf_data ()
output_local ("\n};\n");
}
output_local ("static size_t max_paragraphs_count = %lu;\n\n", max);
output_local ("/* End of cob_perf data */\n");
output_local ("/* End of cob_prof data */\n");
}

void
Expand Down Expand Up @@ -14007,7 +13987,7 @@ codegen_internal (struct cb_program *prog, const int subsequent_call)
output_local_base_cache ();
output_local_field_cache (prog);

if (cb_flag_perf) output_cob_perf_data ();
if (cb_flag_prof) output_cob_prof_data ();
/* Report data fields */
if (prog->report_storage) {
comment_gen = 0;
Expand Down
4 changes: 2 additions & 2 deletions cobc/flag.def
Original file line number Diff line number Diff line change
Expand Up @@ -255,5 +255,5 @@ CB_FLAG_ON (cb_diagnostics_show_caret, 1, "diagnostics-show-caret",
CB_FLAG_ON (cb_diagnostics_show_line_numbers, 1, "diagnostics-show-line-numbers",
_(" -fno-diagnostics-show-line-numbers\tsuppress display of line numbers in diagnostics"))

CB_FLAG (cb_flag_perf, 1, "perf",
_(" -fperf enable profiling of the COBOL program"))
CB_FLAG (cb_flag_prof, 1, "prof",
_(" -fprof enable profiling of the COBOL program"))
52 changes: 26 additions & 26 deletions cobc/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -10949,22 +10949,22 @@ procedure_division:
if (current_paragraph->exit_label) {
emit_statement (current_paragraph->exit_label);
}
if (cb_flag_perf) {
if (cb_flag_prof) {
curr_para_name = cb_build_string (current_paragraph->name, strlen (current_paragraph->name));
curr_sec_name = cb_build_string (current_section->name, strlen (current_section->name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_perf_exit_paragraph", curr_sec_name, curr_para_name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_prof_exit_paragraph", curr_sec_name, curr_para_name));
}
emit_statement (cb_build_perform_exit (current_paragraph));
}
if (current_section) {
if (current_section->exit_label) {
emit_statement (current_section->exit_label);
}
if (cb_flag_perf) {
if (cb_flag_prof) {
sections_l = cb_any_list_add (sections_l, (void*)paragraphs_l, 0);
paragraphs_l = NULL;
curr_sec_name = cb_build_string (current_section->name, strlen (current_section->name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_perf_exit_section", curr_sec_name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_prof_exit_section", curr_sec_name));
}
emit_statement (cb_build_perform_exit (current_section));
}
Expand Down Expand Up @@ -10992,7 +10992,7 @@ procedure_division:
emit_statement (CB_TREE (current_section));
label = cb_build_reference ("MAIN PARAGRAPH");
current_paragraph = CB_LABEL (cb_build_label (label, NULL));
if (cb_flag_perf) {
if (cb_flag_prof) {
sections_name = cb_any_list_add (sections_name, (void*)"MAIN SECTION", 1);
paragraphs_l = cb_any_list_add (paragraphs_l, (void*)"MAIN PARAGRAPH", 1);
}
Expand Down Expand Up @@ -11283,12 +11283,12 @@ _procedure_declaratives:
if (current_paragraph->exit_label) {
emit_statement (current_paragraph->exit_label);
}
if (cb_flag_perf) {
if (cb_flag_prof) {
sections_l = cb_any_list_add (sections_l, (void*)paragraphs_l, 0);
paragraphs_l = NULL;
curr_para_name = cb_build_string (current_paragraph->name, strlen (current_paragraph->name));
curr_sec_name = cb_build_string (current_section->name, strlen (current_section->name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_perf_exit_paragraph", curr_sec_name, curr_para_name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_prof_exit_paragraph", curr_sec_name, curr_para_name));
}
emit_statement (cb_build_perform_exit (current_paragraph));
current_paragraph = NULL;
Expand All @@ -11298,9 +11298,9 @@ _procedure_declaratives:
emit_statement (current_section->exit_label);
}
current_section->flag_fatal_check = 1;
if (cb_flag_perf) {
if (cb_flag_prof) {
curr_sec_name = cb_build_string (current_section->name, strlen (current_section->name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_perf_exit_section", curr_sec_name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_prof_exit_section", curr_sec_name));
}
emit_statement (cb_build_perform_exit (current_section));
current_section = NULL;
Expand Down Expand Up @@ -11381,22 +11381,22 @@ section_header:
if (current_paragraph->exit_label) {
emit_statement (current_paragraph->exit_label);
}
if (cb_flag_perf) {
if (cb_flag_prof) {
curr_para_name = cb_build_string (current_paragraph->name, strlen (current_paragraph->name));
curr_sec_name = cb_build_string (current_section->name, strlen (current_section->name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_perf_exit_paragraph", curr_sec_name, curr_para_name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_prof_exit_paragraph", curr_sec_name, curr_para_name));
}
emit_statement (cb_build_perform_exit (current_paragraph));
}
if (current_section) {
if (current_section->exit_label) {
emit_statement (current_section->exit_label);
}
if (cb_flag_perf) {
if (cb_flag_prof) {
sections_l = cb_any_list_add (sections_l, (void*)paragraphs_l, 0);
paragraphs_l = NULL;
curr_sec_name = cb_build_string (current_section->name, strlen (current_section->name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_perf_exit_section", curr_sec_name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_prof_exit_section", curr_sec_name));
}
emit_statement (cb_build_perform_exit (current_section));
}
Expand All @@ -11411,7 +11411,7 @@ section_header:

/* Begin a new section */
current_section = CB_LABEL (cb_build_label ($1, NULL));
if (cb_flag_perf)
if (cb_flag_prof)
sections_name = cb_any_list_add (sections_name, (void*)current_section->name, 1);
current_section->flag_section = 1;
/* Careful here, one negation */
Expand All @@ -11425,9 +11425,9 @@ section_header:
{
cb_tree curr_sec_name;
emit_statement (CB_TREE (current_section));
if (cb_flag_perf) {
if (cb_flag_prof) {
curr_sec_name = cb_build_string (current_section->name, strlen(current_section->name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_perf_enter_section", curr_sec_name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_prof_enter_section", curr_sec_name));
}
}
;
Expand All @@ -11454,10 +11454,10 @@ paragraph_header:
if (current_paragraph->exit_label) {
emit_statement (current_paragraph->exit_label);
}
if (cb_flag_perf) {
if (cb_flag_prof) {
curr_para_name = cb_build_string (current_paragraph->name, strlen (current_paragraph->name));
curr_sec_name = cb_build_string (current_section->name, strlen (current_section->name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_perf_exit_paragraph", curr_sec_name, curr_para_name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_prof_exit_paragraph", curr_sec_name, curr_para_name));
}
emit_statement (cb_build_perform_exit (current_paragraph));
if (current_program->flag_debugging && !in_debugging) {
Expand All @@ -11478,10 +11478,10 @@ paragraph_header:
current_section->flag_skip_label = !!skip_statements;
current_section->xref.skip = 1;
emit_statement (CB_TREE (current_section));
if (cb_flag_perf) {
if (cb_flag_prof) {
sections_name = cb_any_list_add (sections_name, (void*)"MAIN SECTION", 1);
curr_sec_name = cb_build_string (current_section->name, strlen(current_section->name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_perf_enter_section", curr_sec_name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_prof_enter_section", curr_sec_name));
}
}
current_paragraph = CB_LABEL (cb_build_label ($1, current_section));
Expand All @@ -11490,11 +11490,11 @@ paragraph_header:
current_paragraph->flag_real_label = !in_debugging;
current_paragraph->segment = current_section->segment;
emit_statement (CB_TREE (current_paragraph));
if (cb_flag_perf) {
if (cb_flag_prof) {
paragraphs_l = cb_any_list_add (paragraphs_l, (void*)current_paragraph->name, 1);
curr_para_name = cb_build_string (current_paragraph->name, strlen(current_paragraph->name));
curr_sec_name = cb_build_string (current_section->name, strlen(current_section->name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_perf_enter_paragraph", curr_sec_name, curr_para_name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_prof_enter_paragraph", curr_sec_name, curr_para_name));
}
}
;
Expand Down Expand Up @@ -11601,9 +11601,9 @@ statements:
current_section->flag_declaratives = !!in_declaratives;
current_section->xref.skip = 1;
emit_statement (CB_TREE (current_section));
if (cb_flag_perf) {
if (cb_flag_prof) {
curr_sec_name = cb_build_string (current_section->name, strlen(current_section->name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_perf_enter_section", curr_sec_name));
emit_statement (CB_BUILD_FUNCALL_1 ("cob_prof_enter_section", curr_sec_name));
}
}
if (!current_paragraph) {
Expand All @@ -11618,11 +11618,11 @@ statements:
current_paragraph->flag_dummy_paragraph = 1;
current_paragraph->xref.skip = 1;
emit_statement (CB_TREE (current_paragraph));
if (cb_flag_perf) {
if (cb_flag_prof) {
paragraphs_l = cb_any_list_add (paragraphs_l, (void*)"MAIN PARAGRAPH", 1);
curr_para_name = cb_build_string (current_paragraph->name, strlen(current_paragraph->name));
curr_sec_name = cb_build_string (current_section->name, strlen(current_section->name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_perf_enter_paragraph", curr_sec_name, curr_para_name));
emit_statement (CB_BUILD_FUNCALL_2 ("cob_prof_enter_paragraph", curr_sec_name, curr_para_name));
}
}
if (check_headers_present (COBC_HD_PROCEDURE_DIVISION, 0, 0, 0) == 1) {
Expand Down
2 changes: 1 addition & 1 deletion cobc/typeck.c
Original file line number Diff line number Diff line change
Expand Up @@ -14059,7 +14059,7 @@ cb_emit_start (cb_tree file, cb_tree op, cb_tree key, cb_tree keylen)
void
cb_emit_stop_run (cb_tree x)
{
if (cb_flag_perf) cb_emit (CB_BUILD_FUNCALL_0 ("cob_perf_end"));
if (cb_flag_prof) cb_emit (CB_BUILD_FUNCALL_0 ("cob_prof_end"));
cb_emit (CB_BUILD_FUNCALL_1 ("cob_stop_run", cb_build_cast_int (x)));
}

Expand Down
4 changes: 4 additions & 0 deletions doc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
2023-09-07 Emilien Lemaire <[email protected]>

* gnucobol.texi: document the profiling feature


2023-07-10 Simon Sobisch <[email protected]>

Expand Down
Loading

0 comments on commit e5dace0

Please sign in to comment.