Skip to content

Commit

Permalink
Profiling fixes for code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
ddeclerck committed Apr 9, 2024
1 parent f16d177 commit 8f9868e
Show file tree
Hide file tree
Showing 9 changed files with 70 additions and 59 deletions.
10 changes: 5 additions & 5 deletions cobc/codegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -4374,24 +4374,24 @@ output_funcall (cb_tree x)
output ("cob_prof_enter_procedure (prof_info, %d);", proc_idx);
output_newline ();
output_prefix ();
output ("cob_prof_fallthrough_label = 0");
output ("fallthrough_label = 0");
break;
case COB_PROF_USE_PARAGRAPH_ENTRY: {
int paragraph_idx = CB_INTEGER(p->argv[1])->val;
int entry_idx = CB_INTEGER(p->argv[2])->val;
output ("if (!cob_prof_fallthrough_label)");
output ("if (!fallthrough_label)");
output_block_open ();
output_line ("cob_prof_use_paragraph_entry (prof_info, %d, %d);",
paragraph_idx, entry_idx);
output_block_close ();
output_line ("else");
output_block_open ();
output_line ("cob_prof_fallthrough_label = 0;");
output_line ("fallthrough_label = 0;");
output_block_close ();
break;
}
case COB_PROF_STAYIN_PARAGRAPH:
output ("cob_prof_fallthrough_label = 1");
output ("fallthrough_label = 1");
break;
}
return;
Expand Down Expand Up @@ -13782,7 +13782,7 @@ output_cob_prof_data ( struct cb_program * program )
}
output_local ("};\n");

output_local ("static int cob_prof_fallthrough_label = 0;\n");
output_local ("static int fallthrough_label = 0;\n");
output_local ("static struct cob_prof_module *prof_info;\n");

output_local ("\n/* End of cob_prof data */\n");
Expand Down
2 changes: 2 additions & 0 deletions libcob/ChangeLog
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
$f (executable filename), $d (date in yyyymmdd) and
$t (time in hhmmss)
* common.c (cob_set_main_argv0): extracted from cob_init
* fileio.c (cob_path_to_absolute): extracted from insert
and cob_set_main_argv0

2024-01-25 David Declerck <david.declerck@ocamlpro.com>

Expand Down
20 changes: 2 additions & 18 deletions libcob/call.c
Original file line number Diff line number Diff line change
Expand Up @@ -661,25 +661,9 @@ insert (const char *name, void *func, lt_dlhandle handle,
p->func = func;
p->handle = handle;
p->module = module;
if (path) {
#if defined(HAVE_CANONICALIZE_FILE_NAME)
/* Malloced path or NULL */
p->path = canonicalize_file_name (path);
#elif defined(HAVE_REALPATH)
char *s;

s = cob_malloc ((size_t)COB_NORMAL_BUFF);
if (realpath (path, s) != NULL) {
p->path = cob_strdup (s);
}
cob_free (s);
#elif defined (_WIN32)
/* Malloced path or NULL */
p->path = _fullpath (NULL, path, 1);
#endif
if (!p->path) {
p->path = cob_strdup (path);
}
if (path) {
p->path = cob_path_to_absolute (path);
}
p->no_phys_cancel = nocanc;
val = hash ((const unsigned char *)name);
Expand Down
3 changes: 3 additions & 0 deletions libcob/coblocal.h
Original file line number Diff line number Diff line change
Expand Up @@ -583,6 +583,9 @@ COB_HIDDEN int cob_cmps (const unsigned char *, const unsigned char *,

COB_HIDDEN FILE * cob_open_logfile (const char *filename);

/* Whether we are in testsuite mode */
COB_HIDDEN int is_test;

#undef COB_HIDDEN

#endif /* COB_LOCAL_H */
27 changes: 10 additions & 17 deletions libcob/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -333,6 +333,8 @@ static const unsigned char *sort_collate = NULL;
static const char *cob_source_file = NULL;
static unsigned int cob_source_line = 0;

int is_test = 0;

#ifdef HAVE_DESIGNATED_INITS
const char *cob_statement_name[STMT_MAX_ENTRY] = {
[STMT_UNKNOWN] = "UNKNOWN"
Expand Down Expand Up @@ -7864,7 +7866,11 @@ cob_expand_env_string (const char *strval)
const char *s = NULL;
switch ( strval[k+1] ){
case '$': /* Replace $$ with process-id */
j += sprintf (&env[j], "%d", cob_sys_getpid());
if (is_test) {
j += sprintf (&env[j], "%d", 123456);
} else {
j += sprintf (&env[j], "%d", cob_sys_getpid());
}
k++;
break;
case 'f': /* $f is the executable filename */
Expand Down Expand Up @@ -10204,22 +10210,7 @@ void cob_set_main_argv0 (const int argc, char **argv)
#endif

if (argc && argv && argv[0]) {
#if defined (HAVE_CANONICALIZE_FILE_NAME)
/* Returns malloced path or NULL */
cobglobptr->cob_main_argv0 = canonicalize_file_name (argv[0]);
#elif defined (HAVE_REALPATH)
s = cob_malloc ((size_t)COB_LARGE_BUFF);
if (realpath (argv[0], s) != NULL) {
cobglobptr->cob_main_argv0 = cob_strdup (s);
}
cob_free (s);
#elif defined (_WIN32)
/* Returns malloced path or NULL */
cobglobptr->cob_main_argv0 = _fullpath (NULL, argv[0], 1);
#endif
if (!cobglobptr->cob_main_argv0) {
cobglobptr->cob_main_argv0 = cob_strdup (argv[0]);
}
cobglobptr->cob_main_argv0 = cob_path_to_absolute (argv[0]);
} else {
cobglobptr->cob_main_argv0 = cob_strdup (_("unknown"));
}
Expand Down Expand Up @@ -10289,6 +10280,8 @@ cob_init (const int argc, char **argv)

cob_initialized = 1;

is_test = !!getenv ("COB_IS_RUNNING_IN_TESTMODE");

#ifdef HAVE_SETLOCALE
/* Prime the locale from user settings */
s = setlocale (LC_ALL, "");
Expand Down
2 changes: 2 additions & 0 deletions libcob/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -2680,6 +2680,8 @@ COB_EXPIMP void cob_file_sort_giving_extfh (cob_file *, const size_t, ...);
COB_EXPIMP void cob_file_release (cob_file *);
COB_EXPIMP void cob_file_return (cob_file *);

COB_EXPIMP char * cob_path_to_absolute (const char *path);

/***************************/
/* Functions in reportio.c */
/***************************/
Expand Down
28 changes: 28 additions & 0 deletions libcob/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -10754,3 +10754,31 @@ EXTFH3 (unsigned char *opcode, FCD3 *fcd)
}
return sts;
}


char *
cob_path_to_absolute (const char *path)
{
char *abs_path = NULL;
if (path) {
#if defined(HAVE_CANONICALIZE_FILE_NAME)
/* Returns malloced path or NULL */
abs_path = canonicalize_file_name (path);
#elif defined(HAVE_REALPATH)
char *s;

s = cob_malloc ((size_t)COB_NORMAL_BUFF);
if (realpath (path, s) != NULL) {
abs_path = cob_strdup (s);
}
cob_free (s);
#elif defined (_WIN32)
/* Returns malloced path or NULL */
abs_path = _fullpath (NULL, path, 1);
#endif
if (!abs_path) {
abs_path = cob_strdup (path);
}
}
return abs_path;
}
7 changes: 1 addition & 6 deletions libcob/profiling.c
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,6 @@ static int current_idx = -1;

/* Whether profiling is active or not. */
static int is_active = 0;
/* Whether we are in testsuite mode */
static int is_test = 0;

/* Which clock to use for clock_gettime (if available) */
#ifdef HAVE_CLOCK_GETTIME
Expand Down Expand Up @@ -155,9 +153,6 @@ prof_init_static ()
if (!init_done && cobsetptr) {
prof_setup_clock ();
is_active = cobsetptr->cob_prof_enable;
if (is_active) {
is_test = !!getenv ("COB_IS_RUNNING_IN_TESTMODE");
}
init_done = 1;
}
}
Expand Down Expand Up @@ -460,7 +455,7 @@ cob_prof_print_line (
case 'i':
if (info){
if (is_test){
fprintf (file, "%d", 42);
fprintf (file, "%d", 123456);
} else {
fprintf (file, "%d", cob_sys_getpid());
}
Expand Down
30 changes: 17 additions & 13 deletions tests/testsuite.src/run_misc.at
Original file line number Diff line number Diff line change
Expand Up @@ -14675,26 +14675,30 @@ prog.cob:11: warning: GO TO SECTION '2ND'

AT_CHECK([COB_PROF_ENABLE=0 COB_PROF_FILE='prof-$b.csv' $COBCRUN_DIRECT ./prog], [0], [], [])

AT_CHECK([COB_PROF_ENABLE=1 COB_PROF_FILE='prof-$b.csv' COB_PROF_FORMAT=%i,%m,%s,%p,%e,%f,%l,%w,%k,%t,%h,%n,%x $COBCRUN_DIRECT ./prog], [0], [],
[File prof-prog.csv generated
# Specific test for $f, which is an absolute file name
AT_CHECK([COB_PROF_ENABLE=1 COB_PROF_FILE='$f-prof.csv' $COBCRUN_DIRECT ./prog 2>/dev/null], [0], [], [])
AT_CHECK([ls prog-prof.csv], [0], [ignore], [])

AT_CHECK([COB_PROF_ENABLE=1 COB_PROF_FILE='prof-$$-$b.csv' COB_PROF_FORMAT=%i,%m,%s,%p,%e,%f,%l,%w,%k,%t,%h,%n,%x $COBCRUN_DIRECT ./prog], [0], [],
[File prof-123456-prog.csv generated
])

# note: The time here is actually the number of times the procedure has
# been run, to avoid any indeterminism in the running time of the
# procedure.

AT_CHECK([cat prof-prog.csv], [0],
AT_CHECK([cat prof-123456-prog.csv], [0],
[pid,program-id,section,paragraph,entry,file,line,location,kind,time-ns,time,ncalls,%x
42,prog,,,,prog.cob,4,prog.cob:4,PROGRAM,13000000,0.013 s,1,%x
42,prog,1ST,,,prog.cob,5,prog.cob:5,SECTION,12000000,0.012 s,1,%x
42,prog,1ST,PARA-0001,,prog.cob,6,prog.cob:6,PARAGRAPH,11000000,0.011 s,1,%x
42,prog,1ST,PARA-0002,,prog.cob,8,prog.cob:8,PARAGRAPH,0,0.000 s,0,%x
42,prog,1ST,PARA-0003,,prog.cob,10,prog.cob:10,PARAGRAPH,1000000,0.001 s,1,%x
42,prog,1ST,PARA-0004,,prog.cob,12,prog.cob:12,PARAGRAPH,0,0.000 s,0,%x
42,prog,2ND,,,prog.cob,14,prog.cob:14,SECTION,6000000,0.006 s,1,%x
42,prog,2ND,PARA-0005,,prog.cob,15,prog.cob:15,PARAGRAPH,3000000,0.003 s,1,%x
42,prog,2ND,PARA-0006,,prog.cob,17,prog.cob:17,PARAGRAPH,2000000,0.002 s,2,%x
42,prog,2ND,PARA-0007,,prog.cob,19,prog.cob:19,PARAGRAPH,1000000,0.001 s,1,%x
123456,prog,,,,prog.cob,4,prog.cob:4,PROGRAM,13000000,0.013 s,1,%x
123456,prog,1ST,,,prog.cob,5,prog.cob:5,SECTION,12000000,0.012 s,1,%x
123456,prog,1ST,PARA-0001,,prog.cob,6,prog.cob:6,PARAGRAPH,11000000,0.011 s,1,%x
123456,prog,1ST,PARA-0002,,prog.cob,8,prog.cob:8,PARAGRAPH,0,0.000 s,0,%x
123456,prog,1ST,PARA-0003,,prog.cob,10,prog.cob:10,PARAGRAPH,1000000,0.001 s,1,%x
123456,prog,1ST,PARA-0004,,prog.cob,12,prog.cob:12,PARAGRAPH,0,0.000 s,0,%x
123456,prog,2ND,,,prog.cob,14,prog.cob:14,SECTION,6000000,0.006 s,1,%x
123456,prog,2ND,PARA-0005,,prog.cob,15,prog.cob:15,PARAGRAPH,3000000,0.003 s,1,%x
123456,prog,2ND,PARA-0006,,prog.cob,17,prog.cob:17,PARAGRAPH,2000000,0.002 s,2,%x
123456,prog,2ND,PARA-0007,,prog.cob,19,prog.cob:19,PARAGRAPH,1000000,0.001 s,1,%x
])

AT_CLEANUP
Expand Down

0 comments on commit 8f9868e

Please sign in to comment.