Skip to content

Commit

Permalink
Better profiling interface and profiling results
Browse files Browse the repository at this point in the history
  • Loading branch information
emilienlemaire committed Sep 15, 2023
1 parent 9b436b8 commit 5cd301f
Show file tree
Hide file tree
Showing 13 changed files with 614 additions and 601 deletions.
4 changes: 0 additions & 4 deletions ChangeLog
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
2023-09-04 Emilien Lemaire <emilien.lemaire@ocamlpro.com>

* configure.ac: check for libxlsxwriter

2023-07-28 Simon Sobisch <simonsobisch@gnu.org>

* configure.ac: check for mousemask and mmask_t
Expand Down
6 changes: 3 additions & 3 deletions cobc/ChangeLog
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
2023-09-04 Emilien Lemaire <emilien.lemaire@ocamlpro.com>

* parser.y: save the names of the procedures in global variables.
* parser.y: add variables for profiling, and a `cb_test_list_add` function
* parser.y: generate `cob_prof_enter` and `cob_prof_exit` calls when
needed
* 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
* cobc.h: add an extern `cb_text_list` struct, to save the name of all
created procedures and global variables of this type
* 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
11 changes: 1 addition & 10 deletions cobc/cobc.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,13 +279,6 @@ struct cb_text_list {
const char *text;
};

/* Generic list structure */
struct cb_any_list {
struct cb_any_list *next;
struct cb_any_list *last;
void *data;
};

/* Structure for extended filenames */
struct local_filename {
struct local_filename *next; /* next pointer */
Expand Down Expand Up @@ -623,9 +616,7 @@ extern int yyparse (void);
#endif

/* Data used for profiling */
extern struct cb_any_list *paragraphs_l; /* The list of paragraphs in the currently parsed section */
extern struct cb_any_list *sections_l; /* The list of list of paragraphs (one by section) */
extern struct cb_any_list *sections_name; /* The list of sections names (should be same length as sections_l) */
extern struct cb_text_list *procedures_list;

/* typeck.c */
extern size_t suppress_warn; /* no warnings for internal generated stuff */
Expand Down
160 changes: 74 additions & 86 deletions cobc/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
along with GnuCOBOL. If not, see <https://www.gnu.org/licenses/>.
*/

#include "libcob/common.h"
#include "tarstamp.h"
#include "config.h"

Expand Down Expand Up @@ -1793,7 +1794,6 @@ output_standard_includes (struct cb_program *prog)
}
}
output_line ("#include <libcob.h>");
if (cb_flag_prof) output_line ("#include <libcob/cobprof.h>");
output_newline ();
}

Expand Down Expand Up @@ -4295,13 +4295,30 @@ output_funcall_item (cb_tree x, const int i, unsigned int func_nolitcast)
output_param (x, i);
}

static int
get_text_list_idx (struct cb_text_list *list, const char *text)
{
struct cb_text_list *l = list;
int i = 0;

while (!!l) {
if (!strcmp (text, l->text)) {
return i;
}
l = l->next;
i++;
}

return -1;
}

static void
output_funcall (cb_tree x)
{
struct cb_funcall *p;
cb_tree l;
int i;
int proc_idx;
const int nolitcast_origin = nolitcast;
const int screenptr_origin = screenptr;

Expand All @@ -4311,6 +4328,14 @@ output_funcall (cb_tree x)
return;
}

if (!strcmp("cob_prof_enter_paragraph", p->name) || !strcmp("cob_prof_exit_paragraph", p->name)
|| !strcmp("cob_prof_enter_section", p->name) || !strcmp("cob_prof_exit_section", p->name)) {
proc_idx = get_text_list_idx(procedures_list, CB_STRING(p->argv[0])->data);
output ("%s (%d)", p->name, proc_idx);
return;
}


screenptr = p->screenptr;
output ("%s (", p->name);
for (i = 0; i < p->argc; i++) {
Expand Down Expand Up @@ -7847,6 +7872,8 @@ static void
output_goto_1 (cb_tree x)
{
struct cb_label *lb = CB_LABEL (x);
char procedure_name[COB_NORMAL_BUFF];
int idx;

if (current_prog->flag_segments && last_segment != lb->segment) {
/* Zap independent labels */
Expand Down Expand Up @@ -7875,13 +7902,22 @@ output_goto_1 (cb_tree x)
output_move (cb_space, cb_debug_contents);
}

if (cb_flag_prof) {
if (!!lb->section) {
output_line("cob_prof_goto(\"%s\", \"%s\");", lb->section->name, lb->name);
} else {
output_line("cob_prof_goto(NULL, \"%s\");", lb->name);
}
}
if (cb_flag_prof) {
/* If no section, then lb = section or exit label */
if (!!lb->section) {
sprintf (procedure_name, "%s|%s|%s:%d", lb->section->name, lb->name,
lb->common.source_file, lb->common.source_line);
idx = get_text_list_idx (procedures_list, procedure_name);
} else {
sprintf (procedure_name, "%s||%s:%d", lb->name, lb->common.source_file,
lb->common.source_line);
/* If idx == -1 then GO TO exit, no need to generate a call */
idx = get_text_list_idx(procedures_list, procedure_name);
}
if (idx != -1) {
output_line("cob_prof_goto (%d);", idx);
}
}
output_line ("goto %s%d;", CB_PREFIX_LABEL, lb->id);
}

Expand Down Expand Up @@ -12192,8 +12228,13 @@ output_internal_function (struct cb_program *prog, cb_tree parameter_list)
/* Entry dispatch */
output_line ("/* Entry dispatch */");
if (cb_flag_prof) {
output_line("cob_prof_init (total_times, sections, sections_count,\n"
" para_per_sections, max_paragraphs_count);");
struct cb_text_list *l = procedures_list;
int i = 0;
while (!!l) {
i++;
l = l->next;
}
output_line("cob_prof_init (total_times, called_count, procedures_list, %d);", i);
}
if (cb_flag_stack_extended) {
/* entry marker = first frameptr is the one with
Expand Down Expand Up @@ -13627,87 +13668,34 @@ output_header (const char *locbuff, const struct cb_program *cp)
}
}

static size_t
max_length_section() {
struct cb_any_list *sl_p = sections_l;
size_t max = 0;

while (!!sl_p) {
struct cb_any_list *pl_p = (struct cb_any_list*)sl_p->data;
size_t len = 0;
while (!!pl_p) {
len++;
pl_p = pl_p->next;
}
max = len > max ? len : max;
sl_p = sl_p->next;
}

return max;
}

static void
output_cob_prof_data ()
{
size_t paragraphs_count = 0;
size_t sections_count = 0;
size_t max = max_length_section();

{
struct cb_any_list *sl_p = sections_l;
while (!!sl_p) {
struct cb_any_list *pl_p = sl_p->data;
paragraphs_count++;
sections_count++;
while (!!pl_p) {
paragraphs_count++;
pl_p = pl_p->next;
}
sl_p = sl_p->next;
}
}
int procedure_count = 0;
struct cb_text_list *l = procedures_list;

while (!!l) {
procedure_count++;
l = l->next;
}

l = procedures_list;
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 *sections[] = {\n");
{
struct cb_any_list *sn_p = sections_name;
while (!!sn_p) {
output_local (" \"%s\"", (char*)sn_p->data);
sn_p = sn_p->next;
if (!!sn_p) output_local (",\n");
}
output_local ("\n};\n");
}
output_local ("static size_t sections_count = %lu;\n", sections_count);

output_local ("static const char *para_per_sections[][255] = {\n");
{
struct cb_any_list *sl_p = sections_l;
while (!!sl_p) {
struct cb_any_list *pl_p = (struct cb_any_list*)sl_p->data;
size_t len = 0;
output_local (" {\n");
while (!!pl_p) {
output_local (" \"%s\"", (char*)pl_p->data);
len++;
pl_p = pl_p->next;
if (!!pl_p) output_local (",\n");
}
while (len < max) {
output_local (",\n NULL");
len++;
}
output_local ("\n }");
sl_p = sl_p->next;
if (!!sl_p) output_local (",\n");
}
output_local ("\n};\n");
}
output_local ("static size_t max_paragraphs_count = %lu;\n\n", max);

output_local ("#include <libcob/cobprof.h>\n\n");

output_local ("const char *procedures_list[%d] = {\n", procedure_count + 1);
while (!!l) {
output_local (" \"%s\",\n", l->text);
l = l->next;
}
output_local (" \"\"");
output_local ("};\n");

output_local ("unsigned called_count[%d] = {0};", procedure_count);

output_local ("struct cob_monotonic_time total_times[%d];", procedure_count);

output_local ("/* End of cob_prof data */\n");
}

Expand Down
Loading

0 comments on commit 5cd301f

Please sign in to comment.