Skip to content

Commit

Permalink
move prof filename default to common.c
Browse files Browse the repository at this point in the history
  • Loading branch information
lefessan committed Jan 4, 2024
1 parent 08900fb commit d374915
Show file tree
Hide file tree
Showing 3 changed files with 109 additions and 95 deletions.
181 changes: 99 additions & 82 deletions libcob/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ static struct config_tbl gc_conf[] = {
{"COB_CORE_ON_ERROR", "core_on_error", "0", coeopts, GRP_MISC, ENV_UINT | ENV_ENUMVAL, SETPOS (cob_core_on_error)},
{"COB_CORE_FILENAME", "core_filename", "./core.libcob", NULL, GRP_MISC, ENV_STR, SETPOS (cob_core_filename)},
{"COB_DUMP_FILE", "dump_file", NULL, NULL, GRP_MISC, ENV_FILE, SETPOS (cob_dump_filename)},
{"COB_PROF_FILE", "prof_file", NULL, NULL, GRP_MISC, ENV_FILE, SETPOS (cob_prof_filename)},
{"COB_PROF_FILE", "prof_file", "cob-prof-$b-$$-$d-$t.csv", NULL, GRP_MISC, ENV_FILE, SETPOS (cob_prof_filename)},
{"COB_PROF_ENABLE", "prof_enable", "0", NULL, GRP_MISC, ENV_BOOL, SETPOS (cob_prof_enable)},
{"COB_PROF_MAX_DEPTH", "prof_max_depth", "8192", NULL, GRP_MISC, ENV_UINT, SETPOS (cob_prof_max_depth)},
{"COB_PROF_FORMAT", "prof_format", "%m,%s,%p,%e,%w,%k,%t,%h,%n", NULL, GRP_MISC, ENV_STR, SETPOS (cob_prof_format)},
Expand Down Expand Up @@ -7734,7 +7734,9 @@ var_print (const char *msg, const char *val, const char *default_val,

}

/* Returns a pointer to the last string following a /, \ or : */
/* Returns an allocated string containing a sub-string of argument
* started after the last a /, \ or :, and before the first following
* dot. */
static char *
get_basename (const char *s)
{
Expand Down Expand Up @@ -7849,11 +7851,19 @@ cob_expand_env_string (const char *strval)
k++;
break;
case 'f': /* $f is the executable filename */
s = cobglobptr->cob_main_argv0;
if (!cobglobptr->cob_main_argv0){
env[j++] = strval[k];
} else {
s = cobglobptr->cob_main_argv0;
}
break;
case 'b': /* $b is the executable basename */
need_free = get_basename (cobglobptr->cob_main_argv0);
s = need_free;
if (!cobglobptr->cob_main_argv0){
env[j++] = strval[k];
} else {
need_free = get_basename (cobglobptr->cob_main_argv0);
s = need_free;
}
break;
case 'd': /* $d date as yyyymmdd */
time = cob_get_current_datetime (DTR_DATE);
Expand Down Expand Up @@ -10106,15 +10116,95 @@ cob_call_with_exception_check (const char *name, const int argc, void **argv)
return 0;
}

void
cob_init (const int argc, char **argv)

static
void cob_set_main_argv0 (const int argc, char **argv)
{
char *s;
#if defined (HAVE_READLINK) || defined (HAVE_GETEXECNAME)
const char *path;
#endif
int i;

#ifdef _WIN32
s = cob_malloc ((size_t)COB_LARGE_BUFF);
i = GetModuleFileNameA (NULL, s, COB_LARGE_MAX);
if (i > 0 && i < COB_LARGE_BUFF) {
cobglobptr->cob_main_argv0 = cob_strdup (s);
cob_free (s);
return;
}
cob_free (s);
#elif defined (HAVE_READLINK)
path = NULL;
if (!access ("/proc/self/exe", R_OK)) {
path = "/proc/self/exe";
} else if (!access ("/proc/curproc/file", R_OK)) {
path = "/proc/curproc/file";
} else if (!access ("/proc/self/path/a.out", R_OK)) {
path = "/proc/self/path/a.out";
}
if (path) {
s = cob_malloc ((size_t)COB_LARGE_BUFF);
i = (int)readlink (path, s, (size_t)COB_LARGE_MAX);
if (i > 0 && i < COB_LARGE_BUFF) {
s[i] = 0;
cobglobptr->cob_main_argv0 = cob_strdup (s);
cob_free (s);
return;
}
cob_free (s);
}
#endif

#ifdef HAVE_GETEXECNAME
path = getexecname ();
if (path) {
#ifdef HAVE_REALPATH
s = cob_malloc ((size_t)COB_LARGE_BUFF);
if (realpath (path, s) != NULL) {
cobglobptr->cob_main_argv0 = cob_strdup (s);
} else {
cobglobptr->cob_main_argv0 = cob_strdup (path);
}
cob_free (s);
#else
cobglobptr->cob_main_argv0 = cob_strdup (path);
#endif
return;
}
#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]);
}
} else {
cobglobptr->cob_main_argv0 = cob_strdup (_("unknown"));
}
/* The above must be last in this function as we do early return */
/* from certain ifdef's */
}

void
cob_init (const int argc, char **argv)
{
char *s;
int i;

/* Ensure initialization is only done once. Within generated modules and
libcob this is already ensured, but an external caller may call this
function again */
Expand Down Expand Up @@ -10164,6 +10254,8 @@ cob_init (const int argc, char **argv)
/* Get global structure */
cobglobptr = cob_malloc (sizeof (cob_global));

cob_set_main_argv0 (argc, argv);

/* Get settings structure */
cobsetptr = cob_malloc (sizeof (cob_settings));

Expand Down Expand Up @@ -10289,81 +10381,6 @@ cob_init (const int argc, char **argv)
}
#endif
}

/* This must be last in this function as we do early return */
/* from certain ifdef's */

#ifdef _WIN32
s = cob_malloc ((size_t)COB_LARGE_BUFF);
i = GetModuleFileNameA (NULL, s, COB_LARGE_MAX);
if (i > 0 && i < COB_LARGE_BUFF) {
cobglobptr->cob_main_argv0 = cob_strdup (s);
cob_free (s);
return;
}
cob_free (s);
#elif defined (HAVE_READLINK)
path = NULL;
if (!access ("/proc/self/exe", R_OK)) {
path = "/proc/self/exe";
} else if (!access ("/proc/curproc/file", R_OK)) {
path = "/proc/curproc/file";
} else if (!access ("/proc/self/path/a.out", R_OK)) {
path = "/proc/self/path/a.out";
}
if (path) {
s = cob_malloc ((size_t)COB_LARGE_BUFF);
i = (int)readlink (path, s, (size_t)COB_LARGE_MAX);
if (i > 0 && i < COB_LARGE_BUFF) {
s[i] = 0;
cobglobptr->cob_main_argv0 = cob_strdup (s);
cob_free (s);
return;
}
cob_free (s);
}
#endif

#ifdef HAVE_GETEXECNAME
path = getexecname ();
if (path) {
#ifdef HAVE_REALPATH
s = cob_malloc ((size_t)COB_LARGE_BUFF);
if (realpath (path, s) != NULL) {
cobglobptr->cob_main_argv0 = cob_strdup (s);
} else {
cobglobptr->cob_main_argv0 = cob_strdup (path);
}
cob_free (s);
#else
cobglobptr->cob_main_argv0 = cob_strdup (path);
#endif
return;
}
#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]);
}
} else {
cobglobptr->cob_main_argv0 = cob_strdup (_("unknown"));
}
/* The above must be last in this function as we do early return */
/* from certain ifdef's */
}

/*
Expand Down
13 changes: 5 additions & 8 deletions libcob/profiling.c
Original file line number Diff line number Diff line change
Expand Up @@ -211,7 +211,7 @@ cob_prof_enter_procedure (struct cob_prof_module *info, int proc_idx)
{
cob_ns_time t;

if (!is_active || info == NULL) return;
if (!is_active) return;

t = get_ns_time ();

Expand All @@ -235,7 +235,7 @@ cob_prof_exit_procedure (struct cob_prof_module *info, int proc_idx)
/* Exit all the sections/paragraphs */
cob_ns_time t;

if (!is_active || info == NULL) return;
if (!is_active) return;

t = get_ns_time ();

Expand Down Expand Up @@ -263,7 +263,7 @@ cob_prof_enter_paragraph (struct cob_prof_module *info, int proc_idx)
void
cob_prof_enter_section (struct cob_prof_module *info, int proc_idx)
{
if (!is_active || info == NULL) return;
if (!is_active) return;
/* We do not measure time on section enter/exit, we use the cumulative time
of all paragraphs of the section */
info->called_count[proc_idx] ++;
Expand All @@ -284,7 +284,7 @@ cob_prof_exit_program (struct cob_prof_module *info){
void
cob_prof_use_paragraph_entry (struct cob_prof_module *info,
int paragraph_idx, int entry_idx){
if (!is_active || info == NULL) return;
if (!is_active) return;
info->called_count[entry_idx] ++;
cob_prof_enter_paragraph (info, paragraph_idx);
}
Expand All @@ -307,7 +307,7 @@ cob_prof_goto (struct cob_prof_module *info)
int curr_proc;
struct cob_prof_module *curr_info;

if (!is_active || info == NULL) return;
if (!is_active) return;

curr_proc = called_procedures[current_idx];
curr_info = called_runtimes[current_idx];
Expand Down Expand Up @@ -509,9 +509,6 @@ cob_prof_end ()
}

prof_filename = cobsetptr->cob_prof_filename;
if (!prof_filename){
prof_filename = cob_expand_env_string("cob-prof-$b-$$-$d-$t.csv");
}

file = fopen (prof_filename, !cobsetptr->cob_unix_lf ? "w" : "wb");

Expand Down
10 changes: 5 additions & 5 deletions tests/testsuite.src/used_binaries.at
Original file line number Diff line number Diff line change
Expand Up @@ -1088,23 +1088,23 @@ AT_DATA([prog.cob], [
PARA-0007.
STOP RUN.
])
AT_CAPTURE_FILE([prof.csv])
AT_CAPTURE_FILE([prof-prog.csv])

AT_CHECK([$COMPILE -fprof -x prog.cob], [0], [],
[prog.cob: in section '1ST':
prog.cob: in paragraph 'PARA-0003':
prog.cob:11: warning: GO TO SECTION '2ND'
])

AT_CHECK([COB_PROF_ENABLE=1 COB_PROF_FILE=prof.csv ./prog], [0], [],
[File prof.csv generated
AT_CHECK([COB_PROF_ENABLE=1 COB_PROF_FILE='prof-$b.csv' ./prog], [0], [],
[File prof-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.csv], [0],

AT_CHECK([cat prof-prog.csv], [0],
[program-id,section,paragraph,entry,location,kind,time-ns,time,ncalls
prog,,,,prog.cob:0,PROGRAM,13000000,0.013 s,1
prog,1ST,,,prog.cob:5,SECTION,12000000,0.012 s,1
Expand Down

0 comments on commit d374915

Please sign in to comment.