Skip to content

Commit

Permalink
[runcore] Tidied code in the profiling runcore; no functional changes.
Browse files Browse the repository at this point in the history
git-svn-id: https://svn.parrot.org/parrot/trunk@41198 d31e2699-5ff4-0310-a27c-f18f2fbe73fe
  • Loading branch information
chromatic committed Sep 11, 2009
1 parent fa910cf commit 1a35011
Showing 1 changed file with 66 additions and 55 deletions.
121 changes: 66 additions & 55 deletions src/runcore/profiling.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,13 +77,14 @@ Parrot_runcore_profiling_init(PARROT_INTERP)

Parrot_profiling_runcore_t *coredata =
mem_allocate_typed(Parrot_profiling_runcore_t);
coredata->name = CONST_STRING(interp, "profiling");
coredata->id = PARROT_PROFILING_CORE;
coredata->opinit = PARROT_CORE_OPLIB_INIT;
coredata->runops = (Parrot_runcore_runops_fn_t) init_profiling_core;
coredata->destroy = NULL;
coredata->prepare_run = NULL;
coredata->flags = 0;

coredata->name = CONST_STRING(interp, "profiling");
coredata->id = PARROT_PROFILING_CORE;
coredata->opinit = PARROT_CORE_OPLIB_INIT;
coredata->runops = (Parrot_runcore_runops_fn_t) init_profiling_core;
coredata->destroy = NULL;
coredata->prepare_run = NULL;
coredata->flags = 0;

PARROT_RUNCORE_FUNC_TABLE_SET(coredata);

Expand Down Expand Up @@ -114,7 +115,6 @@ init_profiling_core(PARROT_INTERP, ARGIN(Parrot_profiling_runcore_t *runcore), A
profile_output_var = Parrot_getenv("PARROT_PROFILING_OUTPUT", &free_env_var);

if (profile_output_var) {

STRING *lc_filename;
runcore->profile_filename = Parrot_str_new(interp, profile_output_var, 0);
profile_filename = Parrot_str_to_cstring(interp, runcore->profile_filename);
Expand All @@ -128,9 +128,8 @@ init_profiling_core(PARROT_INTERP, ARGIN(Parrot_profiling_runcore_t *runcore), A
runcore->profile_fd = stdout;
runcore->profile_filename = lc_filename;
}
else {
else
runcore->profile_fd = fopen(profile_filename, "w");
}

if (free_env_var)
mem_sys_free(profile_output_var);
Expand All @@ -152,7 +151,8 @@ init_profiling_core(PARROT_INTERP, ARGIN(Parrot_profiling_runcore_t *runcore), A
runcore->runloop_count = 0;
runcore->level = 0;
runcore->time_size = 32;
runcore->time = mem_allocate_n_typed(runcore->time_size, UHUGEINTVAL);
runcore->time = mem_allocate_n_typed(runcore->time_size,
UHUGEINTVAL);
Profiling_first_loop_SET(runcore);

if (!runcore->profile_fd) {
Expand All @@ -166,6 +166,7 @@ init_profiling_core(PARROT_INTERP, ARGIN(Parrot_profiling_runcore_t *runcore), A
return runops_profiling_core(interp, runcore, pc);
}


/*
=item C<static opcode_t * runops_profiling_core(PARROT_INTERP,
Expand All @@ -186,11 +187,11 @@ ARGIN(opcode_t *pc))
{
ASSERT_ARGS(runops_profiling_core)

Parrot_Context_info preop_info, postop_info;
PMC *preop_sub, *argv;
opcode_t *preop_pc;
UHUGEINTVAL op_time;
STRING *unknown_file = CONST_STRING(interp, "<unknown file>");
UHUGEINTVAL op_time;
Parrot_Context_info preop_info, postop_info;

runcore->runcore_start = Parrot_hires_get_time();

Expand All @@ -200,31 +201,34 @@ ARGIN(opcode_t *pc))
if (runcore->level >= runcore->time_size) {
runcore->time_size *= 2;
runcore->time =
mem_realloc_n_typed(runcore->time, runcore->time_size+1, UHUGEINTVAL);
mem_realloc_n_typed(runcore->time, runcore->time_size + 1,
UHUGEINTVAL);
}

/* store the time between DO_OP and the start of this runcore in this
* op's running total */
runcore->time[runcore->level] = runcore->runcore_start - runcore->op_start;
runcore->time[runcore->level] =
runcore->runcore_start - runcore->op_start;
}

Parrot_Context_get_info(interp, CURRENT_CONTEXT(interp), &postop_info);

argv = VTABLE_get_pmc_keyed_int(interp, interp->iglobals, IGLOBALS_ARGV_LIST);

if (argv && !Profiling_have_printed_cli_TEST(runcore)) {

/* silly way to avoid line length codingstds nit */
PMC *iglobals = interp->iglobals;
PMC *executable = VTABLE_get_pmc_keyed_int(interp, iglobals, IGLOBALS_EXECUTABLE);
PMC *executable = VTABLE_get_pmc_keyed_int(interp, iglobals,
IGLOBALS_EXECUTABLE);
STRING *command_line = Parrot_str_join(interp, CONST_STRING(interp, " "), argv);

char *exec_cstr, *command_line_cstr;

exec_cstr = Parrot_str_to_cstring(interp, VTABLE_get_string(interp, executable));
exec_cstr = Parrot_str_to_cstring(interp,
VTABLE_get_string(interp, executable));
command_line_cstr = Parrot_str_to_cstring(interp, command_line);

/* The CLI line won't reflect any options passed to the parrot binary. */
/* CLI line won't reflect any options passed to the parrot binary. */
fprintf(runcore->profile_fd, "CLI:%s %s\n", exec_cstr, command_line_cstr);

Parrot_str_free_cstring(exec_cstr);
Expand All @@ -233,46 +237,46 @@ ARGIN(opcode_t *pc))
Profiling_have_printed_cli_SET(runcore);
}


if (Profiling_first_loop_TEST(runcore)) {

fprintf(runcore->profile_fd, "VERSION:1\n");
/* silly hack to make all separate runloops appear to come from a single source */
/* NOTE: yes, {x{ foo:bar }x} is ugly an inefficient. Escaping would

/* silly hack to make all separate runloops appear to come from a
* single source
* NOTE: yes, {x{ foo:bar }x} is ugly an inefficient. Escaping would
* be more effort but the priority right now is to get the runcore
* working correctly. Once all the bugs are ironed out we'll switch to
* a nice efficient compressed binary format. */
fprintf(runcore->profile_fd,
"CS:{x{ns:main}x}{x{file:no_file}x}{x{sub:0x1}x}{x{ctx:0x1}x}\n");
fprintf(runcore->profile_fd,
"OP:{x{line:%d}x}{x{time:0}x}{x{op:noop}x}\n", (int) runcore->runloop_count);
"CS:{x{ns:main}x}{x{file:no_file}x}{x{sub:0x1}x}{x{ctx:0x1}x}\n"
"OP:{x{line:%d}x}{x{time:0}x}{x{op:noop}x}\n",
(int) runcore->runloop_count);

runcore->runloop_count++;
Profiling_first_loop_CLEAR(runcore);
}

while (pc) {

STRING *postop_file_name;
Parrot_Context *preop_ctx;

if (pc < code_start || pc >= code_end) {
if (pc < code_start || pc >= code_end)
Parrot_ex_throw_from_c_args(interp, NULL, 1,
"attempt to access code outside of current code segment");
}

/* avoid an extra call to Parrot_Context_get_info */
mem_sys_memcopy(&preop_info, &postop_info, sizeof (Parrot_Context_info));

Parrot_Context_get_info(interp, CURRENT_CONTEXT(interp), &postop_info);

CONTEXT(interp)->current_pc = pc;
preop_sub = CONTEXT(interp)->current_sub;
preop_pc = pc;
preop_ctx = CONTEXT(interp);
preop_sub = CONTEXT(interp)->current_sub;
preop_pc = pc;
preop_ctx = CONTEXT(interp);

runcore->level++;
Profiling_exit_check_CLEAR(runcore);
runcore->op_start = Parrot_hires_get_time();

runcore->op_start = Parrot_hires_get_time();
DO_OP(pc, interp);
runcore->op_finish = Parrot_hires_get_time();

Expand All @@ -281,19 +285,21 @@ ARGIN(opcode_t *pc))
op_time += runcore->time[runcore->level];
runcore->time[runcore->level] = 0;
}
else {
else
op_time = runcore->op_finish - runcore->op_start;
}

runcore->level--;
postop_file_name = postop_info.file;

if (!postop_file_name) postop_file_name = unknown_file;
if (!postop_file_name)
postop_file_name = unknown_file;

/* if current context changed since the last time a CS line was printed... */
/* if current context changed since the last printing of a CS line... */
/* Occasionally the ctx stays the same while the sub changes, possible
* with a call to a subclass' method. */
if ((runcore->prev_ctx != preop_ctx) || runcore->prev_sub != preop_ctx->current_sub) {

if ((runcore->prev_ctx != preop_ctx)
|| runcore->prev_sub != preop_ctx->current_sub) {

if (preop_ctx->current_sub) {
STRING *sub_name;
Expand All @@ -303,13 +309,13 @@ ARGIN(opcode_t *pc))
sub_cstr = Parrot_str_to_cstring(interp, sub_name);
filename_cstr = Parrot_str_to_cstring(interp, postop_file_name);
ns_cstr = Parrot_str_to_cstring(interp,
VTABLE_get_string(interp, preop_ctx->current_namespace));
VTABLE_get_string(interp,
preop_ctx->current_namespace));

fprintf(runcore->profile_fd,
"CS:{x{ns:%s;%s}x}{x{file:%s}x}{x{sub:0x%p}x}{x{ctx:0x%p}x}\n",
ns_cstr, sub_cstr, filename_cstr,
preop_ctx->current_sub,
preop_ctx);
"CS:{x{ns:%s;%s}x}{x{file:%s}x}{x{sub:0x%p}x}{x{ctx:0x%p}x}\n",
ns_cstr, sub_cstr, filename_cstr,
preop_ctx->current_sub, preop_ctx);

Parrot_str_free_cstring(sub_cstr);
Parrot_str_free_cstring(filename_cstr);
Expand All @@ -324,20 +330,23 @@ ARGIN(opcode_t *pc))
* but it gives me obviously incorrect results while postop_info.line
* works. It might be an imcc bug or it might just be me
* misunderstanding something. */
fprintf(runcore->profile_fd, "OP:{x{line:%d}x}{x{time:%li}x}{x{op:%s}x}\n",
postop_info.line, (unsigned long)op_time,
(interp->op_info_table)[*preop_pc].name);

} /* while (pc) */
fprintf(runcore->profile_fd,
"OP:{x{line:%d}x}{x{time:%li}x}{x{op:%s}x}\n",
postop_info.line, (unsigned long)op_time,
(interp->op_info_table)[*preop_pc].name);
}

/* make it easy to tell separate runloops apart */
if (runcore->level == 0) {
fprintf(runcore->profile_fd, "END_OF_RUNLOOP\n");
/* silly hack to make all separate runloops appear to come from a single source */
fprintf(runcore->profile_fd,
"CS:{x{ns:main}x}{x{file:no_file}x}{x{sub:0x1}x}{x{ctx:0x1}x}\n");

/* silly hack to make all separate runloops appear to come from a
* single source */
fprintf(runcore->profile_fd,
"OP:{x{line:%d}x}{x{time:0}x}{x{op:noop}x}\n", (int) runcore->runloop_count);
"CS:{x{ns:main}x}{x{file:no_file}x}{x{sub:0x1}x}{x{ctx:0x1}x}\n"
"OP:{x{line:%d}x}{x{time:0}x}{x{op:noop}x}\n",
(int) runcore->runloop_count);

runcore->runloop_count++;
}

Expand Down Expand Up @@ -365,9 +374,11 @@ destroy_profiling_core(PARROT_INTERP, ARGIN(Parrot_profiling_runcore_t *runcore)
ASSERT_ARGS(destroy_profiling_core)

char *filename_cstr = Parrot_str_to_cstring(interp, runcore->profile_filename);
fprintf(stderr, "\nPROFILING RUNCORE: wrote profile to %s\n", filename_cstr);
fprintf(stderr, "Use tools/dev/pprof2cg.pl to generate Callgrind-compatible "
"output from this file.\n");

fprintf(stderr, "\nPROFILING RUNCORE: wrote profile to %s\n"
"Use tools/dev/pprof2cg.pl to generate Callgrind-compatible "
"output from this file.\n", filename_cstr);

Parrot_str_free_cstring(filename_cstr);

fclose(runcore->profile_fd);
Expand Down

0 comments on commit 1a35011

Please sign in to comment.