Skip to content

Commit

Permalink
Log JIT code using sequence numbers and map file
Browse files Browse the repository at this point in the history
Trying to add a lot of information into the bytecode filename did not
really work out. So now we just use the sequence number for the bytecode.
This will conflict in case you have multiple running processes, but
this is debugging output so there is no reason to care.
  • Loading branch information
bdw committed Oct 14, 2015
1 parent fe87f7b commit 87729c2
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
2 changes: 2 additions & 0 deletions src/core/instance.h
Expand Up @@ -195,6 +195,8 @@ struct MVMInstance {

/* Directory name for JIT bytecode dumps */
char *jit_bytecode_dir;
/* File for map of frame information for bytecode dumps */
FILE *jit_bytecode_map;
/* sequence number for JIT compiled frames */
AO_t jit_seq_nr;

Expand Down
47 changes: 20 additions & 27 deletions src/jit/log.c
Expand Up @@ -10,35 +10,28 @@ void MVM_jit_log(MVMThreadContext *tc, const char * fmt, ...) {
va_end(args);
}

static char * jitcode_name(MVMThreadContext *tc, MVMJitCode *code) {
MVMuint64 cuuid_len;
MVMuint64 name_len;

char *cuuid = MVM_string_ascii_encode(tc, code->sf->body.cuuid,
&cuuid_len);
char *name = MVM_string_ascii_encode(tc, code->sf->body.name,
&name_len);
MVMint32 dirname_len = strlen(tc->instance->jit_bytecode_dir);
char seq_nr[20];
MVMint32 seq_nr_len = sprintf(seq_nr, "%d", code->seq_nr);
char *filename = MVM_malloc(dirname_len + seq_nr_len + cuuid_len + name_len + 14);
sprintf(filename, "%s/jit-%s-%s.%s.bin", tc->instance->jit_bytecode_dir,
seq_nr, cuuid, name);
MVM_free(cuuid);
MVM_free(name);
return filename;
}

void MVM_jit_log_bytecode(MVMThreadContext *tc, MVMJitCode *code) {
char * filename = jitcode_name(tc, code);
FILE * f = fopen(filename, "w");
if (f) {
fwrite(code->func_ptr, sizeof(char), code->size, f);
fclose(f);
MVM_jit_log(tc, "Dump bytecode in %s\n", filename);

/* Filename format: moar-jit-%d.bin. number can consume at most 10
* bytes, moar-jit-.bin is 13 bytes, one byte for the zero at the
* end, one byte for the directory separator is 25 bytes, plus the
* length of the bytecode directory itself */
char * filename = MVM_malloc(strlen(tc->instance->jit_bytecode_dir) + 25);
FILE * out;
sprintf(filename, "%s/moar-jit-%04d.bin", tc->instance->jit_bytecode_dir, code->seq_nr);
out = fopen(filename, "w");
if (out) {
fwrite(code->func_ptr, sizeof(char), code->size, out);
fclose(out);
if (tc->instance->jit_bytecode_map) {
char *frame_name = MVM_string_utf8_encode_C_string(tc, code->sf->body.name);
char *frame_cuuid = MVM_string_utf8_encode_C_string(tc, code->sf->body.cuuid);
/* I'd like to add linenumber and filename information, but it's really a lot of work at this point */
fprintf(tc->instance->jit_bytecode_map, "%s\t%s\t%s\n", filename, frame_name, frame_cuuid);
MVM_free(frame_name);
MVM_free(frame_cuuid);
}
} else {
MVM_jit_log(tc, "Could not dump bytecode in %s\n", filename);
MVM_jit_log(tc, "ERROR: could dump bytecode in %s\n", filename);
}
MVM_free(filename);
}
19 changes: 13 additions & 6 deletions src/moar.c
Expand Up @@ -159,17 +159,22 @@ MVMInstance * MVM_vm_create_instance(void) {
if (jit_log && strlen(jit_log))
instance->jit_log_fh = fopen(jit_log, "w");
jit_bytecode_dir = getenv("MVM_JIT_BYTECODE_DIR");
if (jit_bytecode_dir && strlen(jit_bytecode_dir))
if (jit_bytecode_dir && strlen(jit_bytecode_dir)) {
char *bytecode_map_name = MVM_malloc(strlen(jit_bytecode_dir) + strlen("/jit-map.txt") + 1);
sprintf(bytecode_map_name, "%s/jit-map.txt", jit_bytecode_dir);
instance->jit_bytecode_map = fopen(bytecode_map_name, "w");
instance->jit_bytecode_dir = jit_bytecode_dir;
MVM_free(bytecode_map_name);
}
instance->jit_seq_nr = 0;

/* Various kinds of debugging that can be enabled. */
dynvar_log = getenv("MVM_DYNVAR_LOG");
if (dynvar_log && strlen(dynvar_log)) {
instance->dynvar_log_fh = fopen(dynvar_log, "w");
fprintf(instance->dynvar_log_fh, "+ x 0 0 0 0 0 %llu\n", uv_hrtime());
fflush(instance->dynvar_log_fh);
instance->dynvar_log_lasttime = uv_hrtime();
fprintf(instance->dynvar_log_fh, "+ x 0 0 0 0 0 %llu\n", uv_hrtime());
fflush(instance->dynvar_log_fh);
instance->dynvar_log_lasttime = uv_hrtime();
}
else
instance->dynvar_log_fh = NULL;
Expand Down Expand Up @@ -268,9 +273,11 @@ void MVM_vm_exit(MVMInstance *instance) {
fclose(instance->spesh_log_fh);
if (instance->jit_log_fh)
fclose(instance->jit_log_fh);
if (isntance->jit_bytecode_map)
fcose(instance->jit_bytecode_map);
if (instance->dynvar_log_fh) {
fprintf(instance->dynvar_log_fh, "- x 0 0 0 0 %lld %llu %llu\n", instance->dynvar_log_lasttime, uv_hrtime(), uv_hrtime());
fclose(instance->dynvar_log_fh);
fprintf(instance->dynvar_log_fh, "- x 0 0 0 0 %lld %llu %llu\n", instance->dynvar_log_lasttime, uv_hrtime(), uv_hrtime());
fclose(instance->dynvar_log_fh);
}

/* And, we're done. */
Expand Down

0 comments on commit 87729c2

Please sign in to comment.