Commit 14521976 authored by Shane Snyder's avatar Shane Snyder

refactor interface between stdio/posix for fileno

parent 76e23680
......@@ -46,6 +46,7 @@ DARSHAN_FORWARD_DECL(creat64, int, (const char* path, mode_t mode));
DARSHAN_FORWARD_DECL(dup, int, (int oldfd));
DARSHAN_FORWARD_DECL(dup2, int, (int oldfd, int newfd));
DARSHAN_FORWARD_DECL(dup3, int, (int oldfd, int newfd, int flags));
DARSHAN_FORWARD_DECL(fileno, int, (FILE *stream));
DARSHAN_FORWARD_DECL(mkstemp, int, (char *template));
DARSHAN_FORWARD_DECL(mkostemp, int, (char *template, int flags));
DARSHAN_FORWARD_DECL(mkstemps, int, (char *template, int suffixlen));
......@@ -169,9 +170,8 @@ extern void dxt_posix_write(darshan_record_id rec_id, int64_t offset,
extern void dxt_posix_read(darshan_record_id rec_id, int64_t offset,
int64_t length, double start_time, double end_time);
/* function for registering a newly opened file descriptor with the POSIX module */
int darshan_posix_add_open_fd(int fd, char *rec_name, int counter,
double tm1, double tm2);
/* extern function def for querying record name from a STDIO stream */
extern char *darshan_stdio_lookup_record_name(FILE *stream);
static struct posix_runtime *posix_runtime = NULL;
static pthread_mutex_t posix_runtime_mutex = PTHREAD_RECURSIVE_MUTEX_INITIALIZER_NP;
......@@ -581,6 +581,38 @@ int DARSHAN_DECL(dup3)(int oldfd, int newfd, int flags)
return(ret);
}
int DARSHAN_DECL(fileno)(FILE *stream)
{
int ret;
double tm1, tm2;
darshan_record_id rec_id;
struct posix_file_record_ref *rec_ref;
MAP_OR_FAIL(fileno);
tm1 = darshan_core_wtime();
ret = __real_fileno(stream);
tm2 = darshan_core_wtime();
if(ret >= 0)
{
char *rec_name = darshan_stdio_lookup_record_name(stream);
if(rec_name)
{
POSIX_PRE_RECORD();
rec_id = darshan_core_gen_record_id(rec_name);
rec_ref = darshan_lookup_record_ref(posix_runtime->rec_id_hash,
&rec_id, sizeof(darshan_record_id));
if(!rec_ref)
rec_ref = posix_track_new_file_record(rec_id, rec_name);
POSIX_RECORD_REFOPEN(ret, rec_ref, tm1, tm2, POSIX_FILENOS);
POSIX_POST_RECORD();
}
}
return(ret);
}
int DARSHAN_DECL(mkstemp)(char* template)
{
int ret;
......@@ -1516,26 +1548,6 @@ static void posix_aio_tracker_add(int fd, void *aiocbp)
return;
}
int darshan_posix_add_open_fd(int fd, char *rec_name, int counter,
double tm1, double tm2)
{
darshan_record_id rec_id;
struct posix_file_record_ref *rec_ref;
int ret = 0;
POSIX_PRE_RECORD();
rec_id = darshan_core_gen_record_id(rec_name);
rec_ref = darshan_lookup_record_ref(posix_runtime->rec_id_hash, &rec_id,
sizeof(darshan_record_id));
if(!rec_ref)
rec_ref = posix_track_new_file_record(rec_id, rec_name);
POSIX_RECORD_REFOPEN(fd, rec_ref, tm1, tm2, counter);
POSIX_POST_RECORD();
return(ret);
}
static void posix_finalize_file_records(void *rec_ref_p)
{
struct posix_file_record_ref *rec_ref =
......
......@@ -118,7 +118,6 @@ DARSHAN_FORWARD_DECL(fseeko64, int, (FILE *stream, off64_t offset, int whence));
DARSHAN_FORWARD_DECL(fsetpos, int, (FILE *stream, const fpos_t *pos));
DARSHAN_FORWARD_DECL(fsetpos64, int, (FILE *stream, const fpos64_t *pos));
DARSHAN_FORWARD_DECL(rewind, void, (FILE *stream));
DARSHAN_FORWARD_DECL(fileno, int, (FILE *stream));
/* structure to track stdio stats at runtime */
struct stdio_file_record_ref
......@@ -163,11 +162,12 @@ static struct stdio_file_record_ref *stdio_track_new_file_record(
darshan_record_id rec_id, const char *path);
static void stdio_cleanup_runtime();
/* external prototype from POSIX module used to register new POSIX file
* records corresponding to file descriptors returned by STDIO fileno()
*/
extern int darshan_posix_add_open_fd(int fd, char *rec_name, int counter,
double tm1, double tm2);
/* we need access to fileno (defined in POSIX module) for instrumenting fopen calls */
#ifdef DARSHAN_PRELOAD
extern int (*__real_fileno)(FILE *stream);
#else
extern int __real_fileno(FILE *stream);
#endif
#define STDIO_LOCK() pthread_mutex_lock(&stdio_runtime_mutex)
#define STDIO_UNLOCK() pthread_mutex_unlock(&stdio_runtime_mutex)
......@@ -957,35 +957,6 @@ int DARSHAN_DECL(fsetpos64)(FILE *stream, const fpos64_t *pos)
return(ret);
}
int DARSHAN_DECL(fileno)(FILE *stream)
{
int ret;
struct stdio_file_record_ref *rec_ref;
double tm1, tm2;
MAP_OR_FAIL(fileno);
tm1 = darshan_core_wtime();
ret = __real_fileno(stream);
tm2 = darshan_core_wtime();
if(ret >= 0)
{
STDIO_PRE_RECORD();
rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash, &stream, sizeof(stream));
if(rec_ref)
{
char *rec_name = darshan_core_lookup_record_name(
rec_ref->file_rec->base_rec.id);
/* register this new FD with the POSIX module so we can track it */
darshan_posix_add_open_fd(ret, rec_name, POSIX_FILENOS, tm1, tm2);
}
STDIO_POST_RECORD();
}
return(ret);
}
/**********************************************************
* Internal functions for manipulating STDIO module state *
**********************************************************/
......@@ -1442,6 +1413,23 @@ static void stdio_shared_record_variance(MPI_Comm mod_comm,
return;
}
char *darshan_stdio_lookup_record_name(FILE *stream)
{
struct stdio_file_record_ref *rec_ref;
char *rec_name = NULL;
STDIO_LOCK();
if(stdio_runtime)
{
rec_ref = darshan_lookup_record_ref(stdio_runtime->stream_hash,
&stream, sizeof(stream));
if(rec_ref)
rec_name = darshan_core_lookup_record_name(rec_ref->file_rec->base_rec.id);
}
STDIO_UNLOCK();
return(rec_name);
}
/*
* Local variables:
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment