[Darshan-commits] [Git][darshan/darshan][master] 2 commits: Beter error handling

Shane Snyder xgitlab at cels.anl.gov
Tue May 11 22:12:16 CDT 2021



Shane Snyder pushed to branch master at darshan / darshan


Commits:
44581600 by Rob Latham at 2021-05-11T22:12:12-05:00
Beter error handling

Now tells user why a system calls failed

- - - - -
eac90fc7 by Shane Snyder at 2021-05-11T22:12:12-05:00
Merge branch 'dev-better-error-handling' into 'master'

better error reporting

See merge request darshan/darshan!92
- - - - -


3 changed files:

- darshan-util/darshan-logutils.c
- darshan-util/pydarshan/darshan/backend/cffi_backend.py
- + darshan-util/pydarshan/tests/test_error.py


Changes:

=====================================
darshan-util/darshan-logutils.c
=====================================
@@ -151,7 +151,8 @@ darshan_fd darshan_log_open(const char *name)
     tmp_fd->state->fildes = open(name, O_RDONLY);
     if(tmp_fd->state->fildes < 0)
     {
-        fprintf(stderr, "Error: failed to open darshan log file %s.\n", name);
+        fprintf(stderr, "Error: %s failed to open darshan log file %s: %s.\n", __func__,
+                name, strerror(errno));
         free(tmp_fd->state);
         free(tmp_fd);
         return(NULL);
@@ -162,7 +163,8 @@ darshan_fd darshan_log_open(const char *name)
     ret = darshan_log_get_header(tmp_fd);
     if(ret < 0)
     {
-        fprintf(stderr, "Error: failed to read darshan log file header.\n");
+        fprintf(stderr, "Error: %s failed to read darshan log file header: %s.\n",
+                __func__, strerror(errno));
         close(tmp_fd->state->fildes);
         free(tmp_fd->state);
         free(tmp_fd);
@@ -213,7 +215,8 @@ darshan_fd darshan_log_create(const char *name, enum darshan_comp_type comp_type
     tmp_fd->state->fildes = creat(name, 0400);
     if(tmp_fd->state->fildes < 0)
     {
-        fprintf(stderr, "Error: failed to open darshan log file %s.\n", name);
+        fprintf(stderr, "Error: %s failed to open darshan log file %s: %s.\n",
+                __func__, name, strerror(errno));
         free(tmp_fd->state);
         free(tmp_fd);
         return(NULL);
@@ -260,11 +263,17 @@ darshan_fd darshan_log_create(const char *name, enum darshan_comp_type comp_type
  */
 int darshan_log_get_job(darshan_fd fd, struct darshan_job *job)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     char job_buf[DARSHAN_JOB_RECORD_SIZE] = {0};
     int job_buf_sz = DARSHAN_JOB_RECORD_SIZE;
     int ret;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
     assert(state);
     assert(fd->job_map.len > 0 && fd->job_map.off > 0);
 
@@ -306,11 +315,17 @@ int darshan_log_get_job(darshan_fd fd, struct darshan_job *job)
  */
 int darshan_log_put_job(darshan_fd fd, struct darshan_job *job)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     struct darshan_job job_copy;
     int len;
     int ret;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
     assert(state);
 
     memset(&job_copy, 0, sizeof(*job));
@@ -347,10 +362,16 @@ int darshan_log_put_job(darshan_fd fd, struct darshan_job *job)
  */
 int darshan_log_get_exe(darshan_fd fd, char *buf)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     char *newline;
     int ret;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
     assert(state);
 
     /* if the exe/mount data has not been saved yet, read in the job info */
@@ -385,11 +406,17 @@ int darshan_log_get_exe(darshan_fd fd, char *buf)
  */
 int darshan_log_put_exe(darshan_fd fd, char *buf)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     int len = strlen(buf);
     int ret;
 
-    assert(fd->state);
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
+    assert(state);
 
     ret = darshan_log_dzwrite(fd, DARSHAN_JOB_REGION_ID, buf, len);
     if(ret != len)
@@ -413,11 +440,17 @@ int darshan_log_put_exe(darshan_fd fd, char *buf)
 int darshan_log_get_mounts(darshan_fd fd, struct darshan_mnt_info **mnt_data_array,
     int* count)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     char *pos;
     int array_index = 0;
     int ret;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
     assert(state);
 
     /* if the exe/mount data has not been saved yet, read in the job info */
@@ -482,13 +515,19 @@ int darshan_log_get_mounts(darshan_fd fd, struct darshan_mnt_info **mnt_data_arr
 int darshan_log_put_mounts(darshan_fd fd, struct darshan_mnt_info *mnt_data_array,
     int count)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     int i;
     char mnt_dat[DARSHAN_EXE_LEN] = {0};
     int ret;
     int left = DARSHAN_EXE_LEN;
     int pos = 0;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
     assert(state);
 
     /* write each mount entry to file */
@@ -528,7 +567,7 @@ int darshan_log_put_mounts(darshan_fd fd, struct darshan_mnt_info *mnt_data_arra
  */
 int darshan_log_get_namehash(darshan_fd fd, struct darshan_name_record_ref **hash)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     char *name_rec_buf;
     int name_rec_buf_sz;
     int read;
@@ -536,6 +575,12 @@ int darshan_log_get_namehash(darshan_fd fd, struct darshan_name_record_ref **has
     int buf_len = 0;
     int buf_processed;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
     assert(state);
 
     /* just return if there is no name record mapping data */
@@ -600,7 +645,7 @@ int darshan_log_get_filtered_namehash(darshan_fd fd,
         darshan_record_id *whitelist, int whitelist_count
         )
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     char *name_rec_buf;
     int name_rec_buf_sz;
     int read;
@@ -608,6 +653,12 @@ int darshan_log_get_filtered_namehash(darshan_fd fd,
     int buf_len = 0;
     int buf_processed;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
     assert(state);
 
     /* just return if there is no name record mapping data */
@@ -675,12 +726,18 @@ int darshan_log_get_filtered_namehash(darshan_fd fd,
  */
 int darshan_log_put_namehash(darshan_fd fd, struct darshan_name_record_ref *hash)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     struct darshan_name_record_ref *ref, *tmp;
     struct darshan_name_record *name_rec;
     int name_rec_len;
     int wrote;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
     assert(state);
 
     /* allocate memory for largest possible hash record */
@@ -720,9 +777,15 @@ int darshan_log_put_namehash(darshan_fd fd, struct darshan_name_record_ref *hash
 int darshan_log_get_mod(darshan_fd fd, darshan_module_id mod_id,
     void *mod_buf, int mod_buf_sz)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     int ret;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
     assert(state);
 
     if(mod_id < 0 || mod_id >= DARSHAN_MAX_MODS)
@@ -773,9 +836,15 @@ int darshan_log_get_mod(darshan_fd fd, darshan_module_id mod_id,
 int darshan_log_put_mod(darshan_fd fd, darshan_module_id mod_id,
     void *mod_buf, int mod_buf_sz, int ver)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     int ret;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return(-1);
+    }
+    state = fd->state;
     assert(state);
 
     if(mod_id < 0 || mod_id >= DARSHAN_MAX_MODS)
@@ -809,9 +878,15 @@ int darshan_log_put_mod(darshan_fd fd, darshan_module_id mod_id,
  */
 void darshan_log_close(darshan_fd fd)
 {
-    struct darshan_fd_int_state *state = fd->state;
+    struct darshan_fd_int_state *state;
     int ret;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        return;
+    }
+    state = fd->state;
     assert(state);
 
     /* if the file was created for writing */
@@ -2068,6 +2143,13 @@ void darshan_log_get_modules (darshan_fd fd,
     int i;
     int j;
 
+    if(!fd)
+    {
+        fprintf(stderr, "Error: invalid Darshan log file handle.\n");
+        *count = 0;
+        return;
+    }
+
     *mods = malloc(sizeof(**mods) * DARSHAN_MAX_MODS);
     assert(*mods);
 
@@ -2148,7 +2230,7 @@ void darshan_log_get_filtered_name_records(darshan_fd fd,
     if(ret < 0)
     {
         darshan_log_close(fd);
-        //return(-1);
+        return;
     }
 
     int num = HASH_CNT(hlink, name_hash);


=====================================
darshan-util/pydarshan/darshan/backend/cffi_backend.py
=====================================
@@ -321,6 +321,8 @@ def log_get_generic_record(log, mod_name, dtype='numpy'):
 
     """
     modules = log_get_modules(log)
+    if mod_name not in modules:
+        return None
     mod_type = _structdefs[mod_name]
 
     rec = {}
@@ -447,6 +449,8 @@ def _log_get_lustre_record(log, dtype='numpy'):
         log: handle returned by darshan.open
     """
     modules = log_get_modules(log)
+    if 'LUSTRE' not in modules:
+        return None
 
     rec = {}
     buf = ffi.new("void **")
@@ -528,6 +532,8 @@ def log_get_dxt_record(log, mod_name, reads=True, writes=True, dtype='dict'):
     """
 
     modules = log_get_modules(log)
+    if mod_name not in modules:
+        return None
     mod_type = _structdefs[mod_name]
     #name_records = log_get_name_records(log)
 


=====================================
darshan-util/pydarshan/tests/test_error.py
=====================================
@@ -0,0 +1,20 @@
+#!/usr/bin/env python
+# -*- coding: utf-8 -*-
+
+"""Tests for `pydarshan` package."""
+
+import pytest
+
+import darshan.backend.cffi_backend as backend
+
+def test_cannotopen():
+    """Ensure we do not dump core on error."""
+
+    # assert fake log file name comes back with a NULL file handle
+    log = backend.log_open("fake/tooth-fairy.darshan")
+    c_file_handle_repr = log['handle'].__repr__()
+    assert 'NULL' in c_file_handle_repr
+
+    # assert no record is returned using above NULL file handle
+    rec = backend.log_get_record(log, "MPI-IO")
+    assert rec == None



View it on GitLab: https://xgitlab.cels.anl.gov/darshan/darshan/-/compare/1a6fdd9fed9bc6584110e13e86183cd5598a575f...eac90fc7d34c86a3683b7f3a3a0b5ee2ed86fa89

-- 
View it on GitLab: https://xgitlab.cels.anl.gov/darshan/darshan/-/compare/1a6fdd9fed9bc6584110e13e86183cd5598a575f...eac90fc7d34c86a3683b7f3a3a0b5ee2ed86fa89
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/20210511/1de6ab00/attachment-0001.html>


More information about the Darshan-commits mailing list