[Darshan-commits] [Git][darshan/darshan][fileno-wrapping] 3 commits: implement proper handling of dup family of calls

Shane Snyder xgitlab at cels.anl.gov
Fri Jan 11 15:42:25 CST 2019


Shane Snyder pushed to branch fileno-wrapping at darshan / darshan


Commits:
76e23680 by Shane Snyder at 2019-01-11T17:19:25Z
implement proper handling of dup family of calls

- - - - -
14521976 by Shane Snyder at 2019-01-11T17:35:37Z
refactor interface between stdio/posix for fileno

- - - - -
db253374 by Shane Snyder at 2019-01-11T20:21:23Z
refactor code and properly handle fdopen

- - - - -


6 changed files:

- darshan-posix-log-format.h
- darshan-runtime/lib/darshan-posix.c
- darshan-runtime/lib/darshan-stdio.c
- darshan-runtime/share/ld-opts/darshan-posix-ld-opts
- darshan-runtime/share/ld-opts/darshan-stdio-ld-opts
- darshan-stdio-log-format.h


Changes:

=====================================
darshan-posix-log-format.h
=====================================
@@ -12,6 +12,10 @@
 #define POSIX_COUNTERS \
     /* count of posix opens */\
     X(POSIX_OPENS) \
+    /* count of number of filenos */\
+    X(POSIX_FILENOS) \
+    /* count of number of dups */\
+    X(POSIX_DUPS) \
     /* count of posix reads */\
     X(POSIX_READS) \
     /* count of posix writes */\
@@ -26,8 +30,6 @@
     X(POSIX_FSYNCS) \
     /* count of posix fdatasyncs */\
     X(POSIX_FDSYNCS) \
-    /* count of number of filenos */\
-    X(POSIX_FILENOS) \
     /* mode of file */\
     X(POSIX_MODE) \
     /* total bytes read */\


=====================================
darshan-runtime/lib/darshan-posix.c
=====================================
@@ -43,6 +43,10 @@ DARSHAN_FORWARD_DECL(open64, int, (const char *path, int flags, ...));
 DARSHAN_FORWARD_DECL(__open_2, int, (const char *path, int oflag));
 DARSHAN_FORWARD_DECL(creat, int, (const char* path, mode_t mode));
 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));
@@ -166,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;
@@ -195,40 +198,50 @@ static int enable_dxt_io_trace = 0;
     POSIX_UNLOCK(); \
 } while(0)
 
-#define POSIX_RECORD_OPEN(__ret, __path, __mode, __tm1, __tm2, __linked_counter) do { \
-    darshan_record_id rec_id; \
-    struct posix_file_record_ref *rec_ref; \
-    char *newpath; \
+#define POSIX_RECORD_OPEN(__ret, __path, __mode, __tm1, __tm2) do { \
+    darshan_record_id __rec_id; \
+    struct posix_file_record_ref *__rec_ref; \
+    char *__newpath; \
     if(__ret < 0) break; \
-    newpath = darshan_clean_file_path(__path); \
-    if(!newpath) newpath = (char *)__path; \
-    if(darshan_core_excluded_path(newpath)) { \
-        if(newpath != __path) free(newpath); \
+    __newpath = darshan_clean_file_path(__path); \
+    if(!__newpath) __newpath = (char *)__path; \
+    if(darshan_core_excluded_path(__newpath)) { \
+        if(__newpath != __path) free(__newpath); \
         break; \
     } \
-    rec_id = darshan_core_gen_record_id(newpath); \
-    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, newpath); \
-    if(!rec_ref) { \
-        if(newpath != __path) free(newpath); \
+    __rec_id = darshan_core_gen_record_id(__newpath); \
+    __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, __newpath); \
+    if(!__rec_ref) { \
+        if(__newpath != __path) free(__newpath); \
         break; \
     } \
-    if(__mode) \
-        rec_ref->file_rec->counters[POSIX_MODE] = __mode; \
-    rec_ref->offset = 0; \
-    rec_ref->last_byte_written = 0; \
-    rec_ref->last_byte_read = 0; \
-    rec_ref->file_rec->counters[POSIX_OPENS] += 1; \
-    if(__linked_counter >= 0) rec_ref->file_rec->counters[__linked_counter] += 1; \
-    if(rec_ref->file_rec->fcounters[POSIX_F_OPEN_START_TIMESTAMP] == 0 || \
-     rec_ref->file_rec->fcounters[POSIX_F_OPEN_START_TIMESTAMP] > __tm1) \
-        rec_ref->file_rec->fcounters[POSIX_F_OPEN_START_TIMESTAMP] = __tm1; \
-    rec_ref->file_rec->fcounters[POSIX_F_OPEN_END_TIMESTAMP] = __tm2; \
-    DARSHAN_TIMER_INC_NO_OVERLAP(rec_ref->file_rec->fcounters[POSIX_F_META_TIME], \
-        __tm1, __tm2, rec_ref->last_meta_end); \
-    darshan_add_record_ref(&(posix_runtime->fd_hash), &__ret, sizeof(int), rec_ref); \
-    darshan_instrument_fs_data(rec_ref->fs_type, newpath, __ret); \
-    if(newpath != __path) free(newpath); \
+    _POSIX_RECORD_OPEN(__ret, __rec_ref, __mode, __tm1, __tm2, 1, -1); \
+    darshan_instrument_fs_data(__rec_ref->fs_type, __newpath, __ret); \
+    if(__newpath != __path) free(__newpath); \
+} while(0)
+
+#define POSIX_RECORD_REFOPEN(__ret, __rec_ref, __tm1, __tm2, __ref_counter) do { \
+    if(__ret < 0 || !__rec_ref) break; \
+    _POSIX_RECORD_OPEN(__ret, __rec_ref, 0, __tm1, __tm2, 0, __ref_counter); \
+} while(0)
+
+#define _POSIX_RECORD_OPEN(__ret, __rec_ref, __mode, __tm1, __tm2, __reset_flag, __ref_counter) do { \
+    if(__mode) __rec_ref->file_rec->counters[POSIX_MODE] = __mode; \
+    if(__reset_flag) { \
+        __rec_ref->offset = 0; \
+        __rec_ref->last_byte_written = 0; \
+        __rec_ref->last_byte_read = 0; \
+    } \
+    __rec_ref->file_rec->counters[POSIX_OPENS] += 1; \
+    if(__ref_counter >= 0) __rec_ref->file_rec->counters[__ref_counter] += 1; \
+    if(__rec_ref->file_rec->fcounters[POSIX_F_OPEN_START_TIMESTAMP] == 0 || \
+     __rec_ref->file_rec->fcounters[POSIX_F_OPEN_START_TIMESTAMP] > __tm1) \
+        __rec_ref->file_rec->fcounters[POSIX_F_OPEN_START_TIMESTAMP] = __tm1; \
+    __rec_ref->file_rec->fcounters[POSIX_F_OPEN_END_TIMESTAMP] = __tm2; \
+    DARSHAN_TIMER_INC_NO_OVERLAP(__rec_ref->file_rec->fcounters[POSIX_F_META_TIME], \
+        __tm1, __tm2, __rec_ref->last_meta_end); \
+    darshan_add_record_ref(&(posix_runtime->fd_hash), &__ret, sizeof(int), __rec_ref); \
 } while(0)
 
 #define POSIX_RECORD_READ(__ret, __fd, __pread_flag, __pread_offset, __aligned, __tm1, __tm2) do { \
@@ -403,7 +416,7 @@ int DARSHAN_DECL(open)(const char *path, int flags, ...)
     }
 
     POSIX_PRE_RECORD();
-    POSIX_RECORD_OPEN(ret, path, mode, tm1, tm2, -1);
+    POSIX_RECORD_OPEN(ret, path, mode, tm1, tm2);
     POSIX_POST_RECORD();
 
     return(ret);
@@ -421,7 +434,7 @@ int DARSHAN_DECL(__open_2)(const char *path, int oflag)
     tm2 = darshan_core_wtime();
 
     POSIX_PRE_RECORD();
-    POSIX_RECORD_OPEN(ret, path, 0, tm1, tm2, -1);
+    POSIX_RECORD_OPEN(ret, path, 0, tm1, tm2);
     POSIX_POST_RECORD();
 
     return(ret);
@@ -454,7 +467,7 @@ int DARSHAN_DECL(open64)(const char *path, int flags, ...)
     }
 
     POSIX_PRE_RECORD();
-    POSIX_RECORD_OPEN(ret, path, mode, tm1, tm2, -1);
+    POSIX_RECORD_OPEN(ret, path, mode, tm1, tm2);
     POSIX_POST_RECORD();
 
     return(ret);
@@ -472,7 +485,7 @@ int DARSHAN_DECL(creat)(const char* path, mode_t mode)
     tm2 = darshan_core_wtime();
 
     POSIX_PRE_RECORD();
-    POSIX_RECORD_OPEN(ret, path, mode, tm1, tm2, -1);
+    POSIX_RECORD_OPEN(ret, path, mode, tm1, tm2);
     POSIX_POST_RECORD();
 
     return(ret);
@@ -490,12 +503,117 @@ int DARSHAN_DECL(creat64)(const char* path, mode_t mode)
     tm2 = darshan_core_wtime();
 
     POSIX_PRE_RECORD();
-    POSIX_RECORD_OPEN(ret, path, mode, tm1, tm2, -1);
+    POSIX_RECORD_OPEN(ret, path, mode, tm1, tm2);
     POSIX_POST_RECORD();
 
     return(ret);
 }
 
+int DARSHAN_DECL(dup)(int oldfd)
+{
+    int ret;
+    struct posix_file_record_ref *rec_ref;
+    double tm1, tm2;
+
+    MAP_OR_FAIL(dup);
+
+    tm1 = darshan_core_wtime();
+    ret = __real_dup(oldfd);
+    tm2 = darshan_core_wtime();
+
+    if(ret >= 0)
+    {
+        POSIX_PRE_RECORD();
+        rec_ref = darshan_lookup_record_ref(posix_runtime->fd_hash,
+            &oldfd, sizeof(oldfd));
+        POSIX_RECORD_REFOPEN(ret, rec_ref, tm1, tm2, POSIX_DUPS);
+        POSIX_POST_RECORD();
+    }
+
+    return(ret);
+}
+
+int DARSHAN_DECL(dup2)(int oldfd, int newfd)
+{
+    int ret;
+    struct posix_file_record_ref *rec_ref;
+    double tm1, tm2;
+
+    MAP_OR_FAIL(dup2);
+
+    tm1 = darshan_core_wtime();
+    ret = __real_dup2(oldfd, newfd);
+    tm2 = darshan_core_wtime();
+
+    if(ret >=0)
+    {
+        POSIX_PRE_RECORD();
+        rec_ref = darshan_lookup_record_ref(posix_runtime->fd_hash,
+            &oldfd, sizeof(oldfd));
+        POSIX_RECORD_REFOPEN(ret, rec_ref, tm1, tm2, POSIX_DUPS);
+        POSIX_POST_RECORD();
+    }
+
+    return(ret);
+}
+
+int DARSHAN_DECL(dup3)(int oldfd, int newfd, int flags)
+{
+    int ret;
+    struct posix_file_record_ref *rec_ref;
+    double tm1, tm2;
+
+    MAP_OR_FAIL(dup3);
+
+    tm1 = darshan_core_wtime();
+    ret = __real_dup3(oldfd, newfd, flags);
+    tm2 = darshan_core_wtime();
+
+    if(ret >=0)
+    {
+        POSIX_PRE_RECORD();
+        rec_ref = darshan_lookup_record_ref(posix_runtime->fd_hash,
+            &oldfd, sizeof(oldfd));
+        POSIX_RECORD_REFOPEN(ret, rec_ref, tm1, tm2, POSIX_DUPS);
+        POSIX_POST_RECORD();
+    }
+
+    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)
+        {
+            rec_id = darshan_core_gen_record_id(rec_name);
+
+            POSIX_PRE_RECORD();
+            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;
@@ -508,7 +626,7 @@ int DARSHAN_DECL(mkstemp)(char* template)
     tm2 = darshan_core_wtime();
 
     POSIX_PRE_RECORD();
-    POSIX_RECORD_OPEN(ret, template, 0, tm1, tm2, -1);
+    POSIX_RECORD_OPEN(ret, template, 0, tm1, tm2);
     POSIX_POST_RECORD();
 
     return(ret);
@@ -526,7 +644,7 @@ int DARSHAN_DECL(mkostemp)(char* template, int flags)
     tm2 = darshan_core_wtime();
 
     POSIX_PRE_RECORD();
-    POSIX_RECORD_OPEN(ret, template, 0, tm1, tm2, -1);
+    POSIX_RECORD_OPEN(ret, template, 0, tm1, tm2);
     POSIX_POST_RECORD();
 
     return(ret);
@@ -544,7 +662,7 @@ int DARSHAN_DECL(mkstemps)(char* template, int suffixlen)
     tm2 = darshan_core_wtime();
 
     POSIX_PRE_RECORD();
-    POSIX_RECORD_OPEN(ret, template, 0, tm1, tm2, -1);
+    POSIX_RECORD_OPEN(ret, template, 0, tm1, tm2);
     POSIX_POST_RECORD();
 
     return(ret);
@@ -562,7 +680,7 @@ int DARSHAN_DECL(mkostemps)(char* template, int suffixlen, int flags)
     tm2 = darshan_core_wtime();
 
     POSIX_PRE_RECORD();
-    POSIX_RECORD_OPEN(ret, template, 0, tm1, tm2, -1);
+    POSIX_RECORD_OPEN(ret, template, 0, tm1, tm2);
     POSIX_POST_RECORD();
 
     return(ret);
@@ -1431,18 +1549,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)
-{
-    int ret = 0;
-
-    POSIX_PRE_RECORD();
-    POSIX_RECORD_OPEN(fd, rec_name, 0, 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 =
@@ -1777,6 +1883,24 @@ static void posix_cleanup_runtime()
     return;
 }
 
+char *darshan_posix_lookup_record_name(int fd)
+{
+    struct posix_file_record_ref *rec_ref;
+    char *rec_name = NULL;
+
+    POSIX_LOCK();
+    if(posix_runtime)
+    {
+        rec_ref = darshan_lookup_record_ref(posix_runtime->fd_hash,
+            &fd, sizeof(fd));
+        if(rec_ref)
+            rec_name = darshan_core_lookup_record_name(rec_ref->file_rec->base_rec.id);
+    }
+    POSIX_UNLOCK();
+
+    return(rec_name);
+}
+
 /* posix module shutdown benchmark routine */
 void darshan_posix_shutdown_bench_setup(int test_case)
 {
@@ -1805,14 +1929,14 @@ void darshan_posix_shutdown_bench_setup(int test_case)
         case 1: /* single file-per-process */
             snprintf(filepath, 256, "fpp-0_rank-%d", my_rank);
             
-            POSIX_RECORD_OPEN(fd_array[0], filepath, 777, 0, 1, -1);
+            POSIX_RECORD_OPEN(fd_array[0], filepath, 777, 0, 1);
             POSIX_RECORD_WRITE(size_array[0], fd_array[0], 0, 0, 1, 1, 2);
 
             break;
         case 2: /* single shared file */
             snprintf(filepath, 256, "shared-0");
 
-            POSIX_RECORD_OPEN(fd_array[0], filepath, 777, 0, 1, -1);
+            POSIX_RECORD_OPEN(fd_array[0], filepath, 777, 0, 1);
             POSIX_RECORD_WRITE(size_array[0], fd_array[0], 0, 0, 1, 1, 2);
 
             break;
@@ -1821,7 +1945,7 @@ void darshan_posix_shutdown_bench_setup(int test_case)
             {
                 snprintf(filepath, 256, "fpp-%d_rank-%d", i , my_rank);
 
-                POSIX_RECORD_OPEN(fd_array[i], filepath, 777, 0, 1, -1);
+                POSIX_RECORD_OPEN(fd_array[i], filepath, 777, 0, 1);
                 POSIX_RECORD_WRITE(size_array[i % DARSHAN_COMMON_VAL_MAX_RUNTIME_COUNT],
                     fd_array[i], 0, 0, 1, 1, 2);
             }
@@ -1832,7 +1956,7 @@ void darshan_posix_shutdown_bench_setup(int test_case)
             {
                 snprintf(filepath, 256, "shared-%d", i);
 
-                POSIX_RECORD_OPEN(fd_array[i], filepath, 777, 0, 1, -1);
+                POSIX_RECORD_OPEN(fd_array[i], filepath, 777, 0, 1);
                 POSIX_RECORD_WRITE(size_array[i % DARSHAN_COMMON_VAL_MAX_RUNTIME_COUNT],
                     fd_array[i], 0, 0, 1, 1, 2);
             }


=====================================
darshan-runtime/lib/darshan-stdio.c
=====================================
@@ -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,15 @@ 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);
+/* extern function def for querying record name from a POSIX fd */
+extern char *darshan_posix_lookup_record_name(int fd);
+
+/* 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)
@@ -187,36 +190,46 @@ extern int darshan_posix_add_open_fd(int fd, char *rec_name, int counter,
 } while(0)
 
 #define STDIO_RECORD_OPEN(__ret, __path, __tm1, __tm2) do { \
-    darshan_record_id rec_id; \
-    struct stdio_file_record_ref* rec_ref; \
-    char *newpath; \
+    darshan_record_id __rec_id; \
+    struct stdio_file_record_ref *__rec_ref; \
+    char *__newpath; \
     int __fd; \
     MAP_OR_FAIL(fileno); \
-    if(__ret == NULL) break; \
-    newpath = darshan_clean_file_path(__path); \
-    if(!newpath) newpath = (char*)__path; \
-    if(darshan_core_excluded_path(newpath)) { \
-        if(newpath != (char*)__path) free(newpath); \
+    if(!__ret || !__path) break; \
+    __newpath = darshan_clean_file_path(__path); \
+    if(!__newpath) __newpath = (char*)__path; \
+    if(darshan_core_excluded_path(__newpath)) { \
+        if(__newpath != (char*)__path) free(__newpath); \
         break; \
     } \
-    rec_id = darshan_core_gen_record_id(newpath); \
-    rec_ref = darshan_lookup_record_ref(stdio_runtime->rec_id_hash, &rec_id, sizeof(rec_id)); \
-    if(!rec_ref) rec_ref = stdio_track_new_file_record(rec_id, newpath); \
-    if(!rec_ref) { \
-        if(newpath != (char*)__path) free(newpath); \
+    __rec_id = darshan_core_gen_record_id(__newpath); \
+    __rec_ref = darshan_lookup_record_ref(stdio_runtime->rec_id_hash, &__rec_id, sizeof(darshan_record_id)); \
+    if(!__rec_ref) __rec_ref = stdio_track_new_file_record(__rec_id, __newpath); \
+    if(!__rec_ref) { \
+        if(__newpath != (char*)__path) free(__newpath); \
         break; \
     } \
-    rec_ref->offset = 0; \
-    rec_ref->file_rec->counters[STDIO_OPENS] += 1; \
-    if(rec_ref->file_rec->fcounters[STDIO_F_OPEN_START_TIMESTAMP] == 0 || \
-     rec_ref->file_rec->fcounters[STDIO_F_OPEN_START_TIMESTAMP] > __tm1) \
-        rec_ref->file_rec->fcounters[STDIO_F_OPEN_START_TIMESTAMP] = __tm1; \
-    rec_ref->file_rec->fcounters[STDIO_F_OPEN_END_TIMESTAMP] = __tm2; \
-    DARSHAN_TIMER_INC_NO_OVERLAP(rec_ref->file_rec->fcounters[STDIO_F_META_TIME], __tm1, __tm2, rec_ref->last_meta_end); \
-    darshan_add_record_ref(&(stdio_runtime->stream_hash), &(__ret), sizeof(__ret), rec_ref); \
+    _STDIO_RECORD_OPEN(__ret, __rec_ref, __tm1, __tm2, 1, -1); \
     __fd = __real_fileno(__ret); \
-    darshan_instrument_fs_data(rec_ref->fs_type, newpath, __fd); \
-    if(newpath != (char*)__path) free(newpath); \
+    darshan_instrument_fs_data(__rec_ref->fs_type, __newpath, __fd); \
+    if(__newpath != (char*)__path) free(__newpath); \
+} while(0)
+
+#define STDIO_RECORD_REFOPEN(__ret, __rec_ref, __tm1, __tm2, __ref_counter) do { \
+    if(!ret || !rec_ref) break; \
+    _STDIO_RECORD_OPEN(__ret, __rec_ref, __tm1, __tm2, 0, __ref_counter); \
+} while(0)
+
+#define _STDIO_RECORD_OPEN(__ret, __rec_ref, __tm1, __tm2, __reset_flag, __ref_counter) do { \
+    if(__reset_flag) __rec_ref->offset = 0; \
+    __rec_ref->file_rec->counters[STDIO_OPENS] += 1; \
+    if(__ref_counter >= 0) __rec_ref->file_rec->counters[__ref_counter] += 1; \
+    if(__rec_ref->file_rec->fcounters[STDIO_F_OPEN_START_TIMESTAMP] == 0 || \
+     __rec_ref->file_rec->fcounters[STDIO_F_OPEN_START_TIMESTAMP] > __tm1) \
+        __rec_ref->file_rec->fcounters[STDIO_F_OPEN_START_TIMESTAMP] = __tm1; \
+    __rec_ref->file_rec->fcounters[STDIO_F_OPEN_END_TIMESTAMP] = __tm2; \
+    DARSHAN_TIMER_INC_NO_OVERLAP(__rec_ref->file_rec->fcounters[STDIO_F_META_TIME], __tm1, __tm2, __rec_ref->last_meta_end); \
+    darshan_add_record_ref(&(stdio_runtime->stream_hash), &(__ret), sizeof(__ret), __rec_ref); \
 } while(0)
 
 
@@ -299,6 +312,8 @@ FILE* DARSHAN_DECL(fdopen)(int fd, const char *mode)
 {
     FILE* ret;
     double tm1, tm2;
+    darshan_record_id rec_id;
+    struct stdio_file_record_ref *rec_ref;
 
     MAP_OR_FAIL(fdopen);
 
@@ -306,9 +321,23 @@ FILE* DARSHAN_DECL(fdopen)(int fd, const char *mode)
     ret = __real_fdopen(fd, mode);
     tm2 = darshan_core_wtime();
 
-    STDIO_PRE_RECORD();
-    STDIO_RECORD_OPEN(ret, "UNKNOWN-FDOPEN", tm1, tm2);
-    STDIO_POST_RECORD();
+    if(ret)
+    {
+        char *rec_name = darshan_posix_lookup_record_name(fd);
+        if(rec_name)
+        {
+            rec_id = darshan_core_gen_record_id(rec_name);
+
+            STDIO_PRE_RECORD();
+            rec_ref = darshan_lookup_record_ref(stdio_runtime->rec_id_hash,
+                &rec_id, sizeof(darshan_record_id));
+            if(!rec_ref)
+                rec_ref = stdio_track_new_file_record(rec_id, rec_name);
+            STDIO_RECORD_REFOPEN(ret, rec_ref, tm1, tm2, STDIO_FDOPENS);
+            STDIO_POST_RECORD();
+        }
+    }
+
 
     return(ret);
 }
@@ -957,35 +986,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 +1442,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:


=====================================
darshan-runtime/share/ld-opts/darshan-posix-ld-opts
=====================================
@@ -3,6 +3,9 @@
 --wrap=__open_2
 --wrap=creat
 --wrap=creat64
+--wrap=dup
+--wrap=dup2
+--wrap=dup3
 --wrap=mkstemp
 --wrap=mkostemp
 --wrap=mkstemps
@@ -36,3 +39,4 @@
 --wrap=aio_return64
 --wrap=lio_listio
 --wrap=lio_listio64
+--wrap=fileno


=====================================
darshan-runtime/share/ld-opts/darshan-stdio-ld-opts
=====================================
@@ -29,4 +29,3 @@
 --wrap=rewind
 --wrap=__isoc99_fscanf
 --wrap=printf
---wrap=fileno


=====================================
darshan-stdio-log-format.h
=====================================
@@ -13,6 +13,8 @@
 #define STDIO_COUNTERS \
     /* count of fopens */\
     X(STDIO_OPENS) \
+    /* count of fdopens */\
+    X(STDIO_FDOPENS) \
     /* number of reads */ \
     X(STDIO_READS) \
     /* number of writes */ \



View it on GitLab: https://xgitlab.cels.anl.gov/darshan/darshan/compare/cfc92e59581583ed06fda42603e61d379e72793a...db253374a43045d640015d76693a0d83523d67fb

-- 
View it on GitLab: https://xgitlab.cels.anl.gov/darshan/darshan/compare/cfc92e59581583ed06fda42603e61d379e72793a...db253374a43045d640015d76693a0d83523d67fb
You're receiving this email because of your account on xgitlab.cels.anl.gov.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.mcs.anl.gov/pipermail/darshan-commits/attachments/20190111/f532fe36/attachment-0001.html>


More information about the Darshan-commits mailing list