Skip to content

Commit

Permalink
Merge pull request #20 from bertwesarg/chrome-args
Browse files Browse the repository at this point in the history
Fix timestamps in timeline (aka Chrome trace) conversion
  • Loading branch information
wangvsa authored Jan 23, 2023
2 parents ade8ace + 124dd07 commit 3498d87
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 76 deletions.
2 changes: 2 additions & 0 deletions include/recorder-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@
#define _RECORDER_UTILS_H_

#include <fcntl.h>
#include <pthread.h>

void utils_init();
void utils_finalize();
void* recorder_malloc(size_t size);
void recorder_free(void* ptr, size_t size);
pthread_t recorder_gettid(void);
long get_file_size(const char *filename); // return the size of a file
int accept_filename(const char *filename); // if include the file in trace
double recorder_wtime(void); // return the timestamp
Expand Down
2 changes: 1 addition & 1 deletion include/recorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@
\
Record *record = recorder_malloc(sizeof(Record)); \
record->func_id = get_function_id_by_name(#func); \
record->tid = pthread_self(); \
record->tid = recorder_gettid(); \
logger_record_enter(record); \
record->tstart = recorder_wtime(); \
ret res = RECORDER_REAL_CALL(func) real_args ; \
Expand Down
2 changes: 1 addition & 1 deletion lib/recorder-cuda-profiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ Record* create_recorder_record(CUpti_ActivityKernel5 *kernel) {
Record *record = recorder_malloc(sizeof(Record));
record->func_id = RECORDER_USER_FUNCTION;
record->level = 0;
record->tid = pthread_self();
record->tid = recorder_gettid();
record->tstart = (kernel->start - startTimestamp)/10e9;
record->tstart = (kernel->end - startTimestamp)/10e9;
record->arg_count = 2;
Expand Down
4 changes: 2 additions & 2 deletions lib/recorder-function-profiler.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ func_hash_t* func_table;


void* compose_func_hash_key(void* func, int *key_len) {
pthread_t tid = pthread_self();
pthread_t tid = recorder_gettid();
*key_len = sizeof(pthread_t) + sizeof(void*);
void* key = recorder_malloc(*key_len);
memcpy(key, &tid, sizeof(pthread_t));
Expand Down Expand Up @@ -89,7 +89,7 @@ void __cyg_profile_func_exit (void *func, void *caller)
Record *record = recorder_malloc(sizeof(Record));
record->func_id = RECORDER_USER_FUNCTION;
record->level = 0;
record->tid = pthread_self();
record->tid = recorder_gettid();
record->tstart = entry->tstart_head->tstart;
record->tend = recorder_wtime();
record->arg_count = 2;
Expand Down
12 changes: 11 additions & 1 deletion lib/recorder-utils.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
#define _GNU_SOURCE
#include <unistd.h>
#include <sys/time.h> // for gettimeofday()
#include <stdarg.h> // for va_list, va_start and va_end
#include <sys/syscall.h> // for SYS_gettid
#include <assert.h>
#include <errno.h>
#include <math.h>
#include "recorder.h"
#include "recorder-utils.h"


// Log pointer addresses in the trace file?
static bool log_pointer = false;
static size_t memory_usage = 0;
Expand Down Expand Up @@ -169,6 +170,15 @@ inline int accept_filename(const char *filename) {
return 1;
}

inline pthread_t recorder_gettid(void)
{
#ifdef SYS_gettid
return syscall(SYS_gettid);
#else
return pthread_self();
#endif
}

inline long get_file_size(const char *filename) {
struct stat sb;
int res = stat(filename, &sb); // careful here, make sure stat() is not intercepted
Expand Down
68 changes: 43 additions & 25 deletions tools/reader.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,36 @@
#include <assert.h>
#include "./reader.h"

static int mpi_start_idx = -1;
static int hdf5_start_idx = -1;
void check_version(RecorderReader* reader) {
char version_file[1024];
snprintf(version_file, sizeof(version_file), "%s/VERSION", reader->logs_dir);

static double prev_tstart = 0;

void read_metadata(char* path, RecorderMetadata *metadata) {
FILE* fp = fopen(path, "rb");
FILE* fp = fopen(version_file, "r");
assert(fp != NULL);
fread(metadata, sizeof(RecorderMetadata), 1, fp);
int major, minor, patch;
fscanf(fp, "%d.%d.%d", &major, &minor, &patch);
if(major != VERSION_MAJOR || minor != VERSION_MINOR) {
fprintf(stderr, "incompatible version: file=%d.%d.%d != reader=%d.%d.%d\n",
major, minor, patch, VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH);
exit(1);
}
fclose(fp);
}

void read_func_list(char* path, RecorderReader *reader) {
FILE* fp = fopen(path, "rb");
void read_metadata(RecorderReader* reader) {
char metadata_file[1024];
snprintf(metadata_file, sizeof(metadata_file), "%s/recorder.mt", reader->logs_dir);

FILE* fp = fopen(metadata_file, "rb");
assert(fp != NULL);
fread(&reader->metadata, sizeof(reader->metadata), 1, fp);

long pos = ftell(fp);
fseek(fp, 0, SEEK_END);
long fsize = ftell(fp) - sizeof(RecorderMetadata);
long fsize = ftell(fp) - pos;
char buf[fsize];

fseek(fp, sizeof(RecorderMetadata), SEEK_SET); // skip RecorderMetadata object
fseek(fp, pos, SEEK_SET);
fread(buf, 1, fsize, fp);

int start_pos = 0, end_pos = 0;
Expand All @@ -35,14 +45,14 @@ void read_func_list(char* path, RecorderReader *reader) {
memset(reader->func_list[func_id], 0, sizeof(reader->func_list[func_id]));
memcpy(reader->func_list[func_id], buf+start_pos, end_pos-start_pos);
start_pos = end_pos+1;
if((mpi_start_idx==-1) &&
if((reader->mpi_start_idx==-1) &&
(NULL!=strstr(reader->func_list[func_id], "MPI")))
mpi_start_idx = func_id;
reader->mpi_start_idx = func_id;

if((hdf5_start_idx==-1) &&
if((reader->hdf5_start_idx==-1) &&
(NULL!=strstr(reader->func_list[func_id], "H5")))

hdf5_start_idx = func_id;
reader->hdf5_start_idx = func_id;

func_id++;
}
Expand All @@ -52,15 +62,23 @@ void read_func_list(char* path, RecorderReader *reader) {
}

void recorder_init_reader(const char* logs_dir, RecorderReader *reader) {
char metadata_file[1024];
assert(logs_dir);
assert(reader);

memset(reader, 0, sizeof(*reader));
strcpy(reader->logs_dir, logs_dir);
reader->mpi_start_idx = -1;
reader->hdf5_start_idx = -1;
reader->prev_tstart = 0.0;

check_version(reader);

sprintf(metadata_file, "%s/recorder.mt", logs_dir);
read_metadata(metadata_file, &reader->metadata);
read_func_list(metadata_file, reader);
read_metadata(reader);
}

void recorder_free_reader(RecorderReader *reader) {
assert(reader);
memset(reader, 0, sizeof(*reader));
}

const char* recorder_get_func_name(RecorderReader* reader, Record* record) {
Expand All @@ -70,9 +88,9 @@ const char* recorder_get_func_name(RecorderReader* reader, Record* record) {
}

int recorder_get_func_type(RecorderReader* reader, Record* record) {
if(record->func_id < mpi_start_idx)
if(record->func_id < reader->mpi_start_idx)
return RECORDER_POSIX;
if(record->func_id < hdf5_start_idx) {
if(record->func_id < reader->hdf5_start_idx) {
const char* func_name = recorder_get_func_name(reader, record);
if(strncmp(func_name, "MPI_File", 8) == 0)
return RECORDER_MPIIO;
Expand Down Expand Up @@ -239,9 +257,9 @@ void rule_application(RecorderReader* reader, RuleHash* rules, int rule_id, Call
// Fill in timestamps
uint32_t ts[2];
fread(ts, sizeof(uint32_t), 2, ts_file);
record->tstart = ts[0] * reader->metadata.time_resolution + prev_tstart;
record->tend = ts[1] * reader->metadata.time_resolution + prev_tstart;
prev_tstart = record->tstart;
record->tstart = ts[0] * reader->metadata.time_resolution + reader->prev_tstart;
record->tend = ts[1] * reader->metadata.time_resolution + reader->prev_tstart;
reader->prev_tstart = record->tstart;

user_op(record, user_arg);

Expand All @@ -263,7 +281,7 @@ void recorder_decode_records_core(RecorderReader *reader, CST *cst, CFG *cfg,

assert(cst->rank == cfg->rank);

prev_tstart = 0;
reader->prev_tstart = 0.0;

char ts_filename[1096] = {0};
sprintf(ts_filename, "%s/%d.ts", reader->logs_dir, cst->rank);
Expand Down
10 changes: 10 additions & 0 deletions tools/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
#include <stdbool.h>
#include "recorder-logger.h"

// keep in sync with VERSION_STR in lib/recorder-logger.c
// equal (major, minor) is needed for compatibility
#define VERSION_MAJOR 2
#define VERSION_MINOR 3
#define VERSION_PATCH 3

#define POSIX_SEMANTICS 0
#define COMMIT_SEMANTICS 1
Expand All @@ -19,6 +24,11 @@ typedef struct RecorderReader_t {

char func_list[256][64];
char logs_dir[1024];

int mpi_start_idx;
int hdf5_start_idx;

double prev_tstart;
} RecorderReader;

typedef struct Interval_t {
Expand Down
Loading

0 comments on commit 3498d87

Please sign in to comment.