Commit 44581600 authored by Rob Latham's avatar Rob Latham Committed by Shane Snyder
Browse files

Beter error handling

Now tells user why a system calls failed
parent 1a6fdd9f
...@@ -151,7 +151,8 @@ darshan_fd darshan_log_open(const char *name) ...@@ -151,7 +151,8 @@ darshan_fd darshan_log_open(const char *name)
tmp_fd->state->fildes = open(name, O_RDONLY); tmp_fd->state->fildes = open(name, O_RDONLY);
if(tmp_fd->state->fildes < 0) 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->state);
free(tmp_fd); free(tmp_fd);
return(NULL); return(NULL);
...@@ -162,7 +163,8 @@ darshan_fd darshan_log_open(const char *name) ...@@ -162,7 +163,8 @@ darshan_fd darshan_log_open(const char *name)
ret = darshan_log_get_header(tmp_fd); ret = darshan_log_get_header(tmp_fd);
if(ret < 0) 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); close(tmp_fd->state->fildes);
free(tmp_fd->state); free(tmp_fd->state);
free(tmp_fd); free(tmp_fd);
...@@ -213,7 +215,8 @@ darshan_fd darshan_log_create(const char *name, enum darshan_comp_type comp_type ...@@ -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); tmp_fd->state->fildes = creat(name, 0400);
if(tmp_fd->state->fildes < 0) 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->state);
free(tmp_fd); free(tmp_fd);
return(NULL); return(NULL);
...@@ -260,11 +263,17 @@ darshan_fd darshan_log_create(const char *name, enum darshan_comp_type comp_type ...@@ -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) 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}; char job_buf[DARSHAN_JOB_RECORD_SIZE] = {0};
int job_buf_sz = DARSHAN_JOB_RECORD_SIZE; int job_buf_sz = DARSHAN_JOB_RECORD_SIZE;
int ret; int ret;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return(-1);
}
state = fd->state;
assert(state); assert(state);
assert(fd->job_map.len > 0 && fd->job_map.off > 0); 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) ...@@ -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) 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; struct darshan_job job_copy;
int len; int len;
int ret; int ret;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return(-1);
}
state = fd->state;
assert(state); assert(state);
memset(&job_copy, 0, sizeof(*job)); memset(&job_copy, 0, sizeof(*job));
...@@ -347,10 +362,16 @@ int darshan_log_put_job(darshan_fd fd, struct darshan_job *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) 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; char *newline;
int ret; int ret;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return(-1);
}
state = fd->state;
assert(state); assert(state);
/* if the exe/mount data has not been saved yet, read in the job info */ /* 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) ...@@ -385,11 +406,17 @@ int darshan_log_get_exe(darshan_fd fd, char *buf)
*/ */
int darshan_log_put_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 len = strlen(buf);
int ret; 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); ret = darshan_log_dzwrite(fd, DARSHAN_JOB_REGION_ID, buf, len);
if(ret != len) if(ret != len)
...@@ -413,11 +440,17 @@ int darshan_log_put_exe(darshan_fd fd, char *buf) ...@@ -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 darshan_log_get_mounts(darshan_fd fd, struct darshan_mnt_info **mnt_data_array,
int* count) int* count)
{ {
struct darshan_fd_int_state *state = fd->state; struct darshan_fd_int_state *state;
char *pos; char *pos;
int array_index = 0; int array_index = 0;
int ret; int ret;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return(-1);
}
state = fd->state;
assert(state); assert(state);
/* if the exe/mount data has not been saved yet, read in the job info */ /* 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 ...@@ -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 darshan_log_put_mounts(darshan_fd fd, struct darshan_mnt_info *mnt_data_array,
int count) int count)
{ {
struct darshan_fd_int_state *state = fd->state; struct darshan_fd_int_state *state;
int i; int i;
char mnt_dat[DARSHAN_EXE_LEN] = {0}; char mnt_dat[DARSHAN_EXE_LEN] = {0};
int ret; int ret;
int left = DARSHAN_EXE_LEN; int left = DARSHAN_EXE_LEN;
int pos = 0; int pos = 0;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return(-1);
}
state = fd->state;
assert(state); assert(state);
/* write each mount entry to file */ /* 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 ...@@ -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) 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; char *name_rec_buf;
int name_rec_buf_sz; int name_rec_buf_sz;
int read; int read;
...@@ -536,6 +575,12 @@ int darshan_log_get_namehash(darshan_fd fd, struct darshan_name_record_ref **has ...@@ -536,6 +575,12 @@ int darshan_log_get_namehash(darshan_fd fd, struct darshan_name_record_ref **has
int buf_len = 0; int buf_len = 0;
int buf_processed; int buf_processed;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return(-1);
}
state = fd->state;
assert(state); assert(state);
/* just return if there is no name record mapping data */ /* just return if there is no name record mapping data */
...@@ -600,7 +645,7 @@ int darshan_log_get_filtered_namehash(darshan_fd fd, ...@@ -600,7 +645,7 @@ int darshan_log_get_filtered_namehash(darshan_fd fd,
darshan_record_id *whitelist, int whitelist_count 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; char *name_rec_buf;
int name_rec_buf_sz; int name_rec_buf_sz;
int read; int read;
...@@ -608,6 +653,12 @@ int darshan_log_get_filtered_namehash(darshan_fd fd, ...@@ -608,6 +653,12 @@ int darshan_log_get_filtered_namehash(darshan_fd fd,
int buf_len = 0; int buf_len = 0;
int buf_processed; int buf_processed;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return(-1);
}
state = fd->state;
assert(state); assert(state);
/* just return if there is no name record mapping data */ /* just return if there is no name record mapping data */
...@@ -675,12 +726,18 @@ int darshan_log_get_filtered_namehash(darshan_fd fd, ...@@ -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) 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_ref *ref, *tmp;
struct darshan_name_record *name_rec; struct darshan_name_record *name_rec;
int name_rec_len; int name_rec_len;
int wrote; int wrote;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return(-1);
}
state = fd->state;
assert(state); assert(state);
/* allocate memory for largest possible hash record */ /* 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 ...@@ -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, int darshan_log_get_mod(darshan_fd fd, darshan_module_id mod_id,
void *mod_buf, int mod_buf_sz) void *mod_buf, int mod_buf_sz)
{ {
struct darshan_fd_int_state *state = fd->state; struct darshan_fd_int_state *state;
int ret; int ret;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return(-1);
}
state = fd->state;
assert(state); assert(state);
if(mod_id < 0 || mod_id >= DARSHAN_MAX_MODS) 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, ...@@ -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, int darshan_log_put_mod(darshan_fd fd, darshan_module_id mod_id,
void *mod_buf, int mod_buf_sz, int ver) 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; int ret;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return(-1);
}
state = fd->state;
assert(state); assert(state);
if(mod_id < 0 || mod_id >= DARSHAN_MAX_MODS) 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, ...@@ -809,9 +878,15 @@ int darshan_log_put_mod(darshan_fd fd, darshan_module_id mod_id,
*/ */
void darshan_log_close(darshan_fd fd) void darshan_log_close(darshan_fd fd)
{ {
struct darshan_fd_int_state *state = fd->state; struct darshan_fd_int_state *state;
int ret; int ret;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
return;
}
state = fd->state;
assert(state); assert(state);
/* if the file was created for writing */ /* if the file was created for writing */
...@@ -2068,6 +2143,13 @@ void darshan_log_get_modules (darshan_fd fd, ...@@ -2068,6 +2143,13 @@ void darshan_log_get_modules (darshan_fd fd,
int i; int i;
int j; int j;
if(!fd)
{
fprintf(stderr, "Error: invalid Darshan log file handle.\n");
*count = 0;
return;
}
*mods = malloc(sizeof(**mods) * DARSHAN_MAX_MODS); *mods = malloc(sizeof(**mods) * DARSHAN_MAX_MODS);
assert(*mods); assert(*mods);
...@@ -2148,7 +2230,7 @@ void darshan_log_get_filtered_name_records(darshan_fd fd, ...@@ -2148,7 +2230,7 @@ void darshan_log_get_filtered_name_records(darshan_fd fd,
if(ret < 0) if(ret < 0)
{ {
darshan_log_close(fd); darshan_log_close(fd);
//return(-1); return;
} }
int num = HASH_CNT(hlink, name_hash); int num = HASH_CNT(hlink, name_hash);
......
...@@ -321,6 +321,8 @@ def log_get_generic_record(log, mod_name, dtype='numpy'): ...@@ -321,6 +321,8 @@ def log_get_generic_record(log, mod_name, dtype='numpy'):
""" """
modules = log_get_modules(log) modules = log_get_modules(log)
if mod_name not in modules:
return None
mod_type = _structdefs[mod_name] mod_type = _structdefs[mod_name]
rec = {} rec = {}
...@@ -447,6 +449,8 @@ def _log_get_lustre_record(log, dtype='numpy'): ...@@ -447,6 +449,8 @@ def _log_get_lustre_record(log, dtype='numpy'):
log: handle returned by darshan.open log: handle returned by darshan.open
""" """
modules = log_get_modules(log) modules = log_get_modules(log)
if 'LUSTRE' not in modules:
return None
rec = {} rec = {}
buf = ffi.new("void **") buf = ffi.new("void **")
...@@ -528,6 +532,8 @@ def log_get_dxt_record(log, mod_name, reads=True, writes=True, dtype='dict'): ...@@ -528,6 +532,8 @@ def log_get_dxt_record(log, mod_name, reads=True, writes=True, dtype='dict'):
""" """
modules = log_get_modules(log) modules = log_get_modules(log)
if mod_name not in modules:
return None
mod_type = _structdefs[mod_name] mod_type = _structdefs[mod_name]
#name_records = log_get_name_records(log) #name_records = log_get_name_records(log)
......
#!/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
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