Skip to content

Commit

Permalink
Merge pull request #124 from hariharan-devarajan/hariharan/stdio_cove…
Browse files Browse the repository at this point in the history
…rage

Hariharan/stdio coverage
  • Loading branch information
ChristopherHogan committed Feb 11, 2021
2 parents c9afa31 + 20b09a1 commit 3a455d4
Show file tree
Hide file tree
Showing 5 changed files with 176 additions and 78 deletions.
3 changes: 1 addition & 2 deletions adapter/include/hermes/adapter/stdio.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ HERMES_FORWARD_DECL(fwrite, size_t,
HERMES_FORWARD_DECL(fputc, int, (int c, FILE *stream));
HERMES_FORWARD_DECL(putc, int, (int c, FILE *stream));
HERMES_FORWARD_DECL(fgetpos, int, (FILE * stream, fpos_t *pos));
HERMES_FORWARD_DECL(fgetpos64, int, (FILE * stream, fpos64_t *pos));
HERMES_FORWARD_DECL(putw, int, (int w, FILE *stream));
HERMES_FORWARD_DECL(fputs, int, (const char *s, FILE *stream));
HERMES_FORWARD_DECL(fprintf, int, (FILE * stream, const char *format, ...));
Expand All @@ -71,8 +72,6 @@ HERMES_FORWARD_DECL(fread, size_t,
HERMES_FORWARD_DECL(fgetc, int, (FILE * stream));
HERMES_FORWARD_DECL(getc, int, (FILE * stream));
HERMES_FORWARD_DECL(getw, int, (FILE * stream));
HERMES_FORWARD_DECL(_IO_getc, int, (FILE * stream));
HERMES_FORWARD_DECL(_IO_putc, int, (int, FILE *stream));
HERMES_FORWARD_DECL(fgets, char *, (char *s, int size, FILE *stream));
HERMES_FORWARD_DECL(fseek, int, (FILE * stream, long offset, int whence));
HERMES_FORWARD_DECL(fseeko, int, (FILE * stream, off_t offset, int whence));
Expand Down
24 changes: 9 additions & 15 deletions adapter/src/hermes/adapter/interceptor.cc
Original file line number Diff line number Diff line change
Expand Up @@ -61,24 +61,18 @@ bool IsTracked(const std::string& path) {
return false;
}
}
if (INTERCEPTOR_LIST->user_path_exclusions.empty()) {
for (const auto& pth : kPathInclusions) {
if (path.find(pth) == 0) {
return true;
}
}
for (const auto& pth : kPathExclusions) {
if (path.find(pth) == 0) {
return false;
}

for (const auto& pth : kPathInclusions) {
if (path.find(pth) == 0) {
return true;
}
} else {
for (const auto& pth : INTERCEPTOR_LIST->user_path_exclusions) {
if (path.find(pth) == 0) {
return false;
}
}
for (const auto& pth : kPathExclusions) {
if (path.find(pth) == 0) {
return false;
}
}

return true;
}
bool IsTracked(FILE* fh) {
Expand Down
7 changes: 1 addition & 6 deletions adapter/src/hermes/adapter/interceptor.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,6 @@ const char* kPathInclusions[] = {"/var/opt/cray/dws/mounts/"};
* or included for interceptions.
*/
struct InterceptorList {
/**
* Allow users to override the path exclusions
*/
std::vector<std::string> user_path_exclusions;
/**
* Allow adapter to exclude hermes specific files.
*/
Expand All @@ -77,8 +73,7 @@ struct InterceptorList {
* Default constructor
*/
InterceptorList()
: user_path_exclusions(),
hermes_paths_exclusion(),
: hermes_paths_exclusion(),
hermes_flush_exclusion() {}
};

Expand Down
70 changes: 25 additions & 45 deletions adapter/src/hermes/adapter/stdio/stdio.cc
Original file line number Diff line number Diff line change
Expand Up @@ -648,6 +648,31 @@ int HERMES_DECL(fgetpos)(FILE *fp, fpos_t *pos) {
return ret;
}

int HERMES_DECL(fgetpos64)(FILE *fp, fpos64_t *pos) {
int ret;
if (hermes::adapter::IsTracked(fp) && pos) {
auto mdm = hermes::adapter::Singleton<MetadataManager>::GetInstance();
auto existing = mdm->Find(fp);
if (existing.second) {
LOG(INFO) << "Intercept fgetpos64." << std::endl;
// TODO(chogan): @portability In the GNU C Library, fpos_t is an opaque
// data structure that contains internal data to represent file offset and
// conversion state information. In other systems, it might have a
// different internal representation. This will need to change to support
// other compilers.
pos->__pos = existing.first.st_ptr;
ret = 0;
} else {
MAP_OR_FAIL(fgetpos64);
ret = real_fgetpos64_(fp, pos);
}
} else {
MAP_OR_FAIL(fgetpos64);
ret = real_fgetpos64_(fp, pos);
}
return ret;
}

int HERMES_DECL(putc)(int c, FILE *fp) {
int ret;
if (hermes::adapter::IsTracked(fp)) {
Expand Down Expand Up @@ -778,51 +803,6 @@ int HERMES_DECL(getc)(FILE *stream) {
return (ret);
}

/* NOTE: stdio.h typically implements getc() as a macro pointing to _IO_getc */
int HERMES_DECL(_IO_getc)(FILE *stream) {
int ret = -1;
if (hermes::adapter::IsTracked(stream)) {
auto mdm = hermes::adapter::Singleton<MetadataManager>::GetInstance();
auto existing = mdm->Find(stream);
if (existing.second) {
LOG(INFO) << "Intercept _IO_getc." << std::endl;
unsigned char value;
auto ret_size =
read_internal(existing, &value, sizeof(unsigned char), stream);
if (ret_size == sizeof(unsigned char)) {
ret = value;
}
} else {
MAP_OR_FAIL(_IO_getc);
ret = real__IO_getc_(stream);
}
} else {
MAP_OR_FAIL(_IO_getc);
ret = real__IO_getc_(stream);
}
return (ret);
}

/* NOTE: stdio.h typically implements putc() as a macro pointing to _IO_putc */
int HERMES_DECL(_IO_putc)(int c, FILE *stream) {
int ret;
if (hermes::adapter::IsTracked(stream)) {
auto mdm = hermes::adapter::Singleton<MetadataManager>::GetInstance();
auto existing = mdm->Find(stream);
if (existing.second) {
LOG(INFO) << "Intercept _IO_putc char:" << (char *)&c << "." << std::endl;
ret = write_internal(existing, &c, 1, stream);
} else {
MAP_OR_FAIL(_IO_putc);
ret = real__IO_putc_(c, stream);
}
} else {
MAP_OR_FAIL(_IO_putc);
ret = real__IO_putc_(c, stream);
}
return (ret);
}

int HERMES_DECL(getw)(FILE *stream) {
int ret = -1;
if (hermes::adapter::IsTracked(stream)) {
Expand Down
150 changes: 140 additions & 10 deletions adapter/test/stdio/stdio_adapter_func_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,29 @@ TEST_CASE("getc", "[process=" + std::to_string(info.comm_size) +
posttest(false);
}

TEST_CASE("getw", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=batched_getw]"
"[repetition=" +
std::to_string(info.num_iterations) + "][file=1]") {
pretest();
SECTION("iterate and get all characters") {
FILE* fh = fopen(info.existing_file.c_str(), "r");
REQUIRE(fh != nullptr);
size_t total_chars = 0;
int c = '0';
do {
c = getw(fh);
total_chars++;
if (total_chars >= info.num_iterations) break;
} while (c != EOF);
REQUIRE(total_chars == info.num_iterations);
int status = fclose(fh);
REQUIRE(status == 0);
}
posttest(false);
}

TEST_CASE("fgets", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=single_fgets]"
Expand Down Expand Up @@ -360,10 +383,10 @@ TEST_CASE("putc", "[process=" + std::to_string(info.comm_size) +
posttest(false);
}
TEST_CASE("putw", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=batched_putw]"
"[repetition=" +
std::to_string(info.num_iterations) + "][file=1]") {
"]"
"[operation=batched_putw]"
"[repetition=" +
std::to_string(info.num_iterations) + "][file=1]") {
pretest();
SECTION("iterate and get all characters") {
FILE* fh = fopen(info.new_file.c_str(), "w+");
Expand Down Expand Up @@ -464,6 +487,40 @@ TEST_CASE("fseeko", "[process=" + std::to_string(info.comm_size) +
posttest(false);
}

TEST_CASE("fseeko64", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=single_fseeko64]"
"[repetition=1][file=1]") {
pretest();
SECTION("test all seek modes") {
FILE* fh = fopen(info.existing_file.c_str(), "r");
REQUIRE(fh != nullptr);
int status = fseeko64(fh, 0, SEEK_SET);
REQUIRE(status == 0);
size_t offset = ftell(fh);
REQUIRE(offset == 0);

status = fseeko64(fh, 0, SEEK_CUR);
REQUIRE(status == 0);
offset = ftell(fh);
REQUIRE(offset == 0);

status = fseeko64(fh, 0, SEEK_END);
REQUIRE(status == 0);
offset = ftell(fh);
REQUIRE(offset == info.total_size);

status = fseeko64(fh, 0, SEEK_CUR);
REQUIRE(status == 0);
offset = ftell(fh);
REQUIRE(offset == info.total_size);

status = fclose(fh);
REQUIRE(status == 0);
}
posttest(false);
}

TEST_CASE("rewind", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=single_rewind]"
Expand Down Expand Up @@ -523,6 +580,35 @@ TEST_CASE("fsetpos", "[process=" + std::to_string(info.comm_size) +
posttest(false);
}

TEST_CASE("fsetpos64", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=single_fsetpos64]"
"[repetition=1][file=1]") {
pretest();
SECTION("test all seek modes") {
FILE* fh = fopen(info.existing_file.c_str(), "r");
REQUIRE(fh != nullptr);
fpos64_t position;
fgetpos64(fh, &position);

position.__pos = 0;
int status = fsetpos64(fh, &position);
REQUIRE(status == 0);
size_t offset = ftell(fh);
REQUIRE(offset == 0);

position.__pos = info.total_size;
status = fsetpos64(fh, &position);
REQUIRE(status == 0);
offset = ftell(fh);
REQUIRE(offset == info.total_size);

status = fclose(fh);
REQUIRE(status == 0);
}
posttest(false);
}

TEST_CASE("fgetpos", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=single_fgetpos]"
Expand All @@ -549,10 +635,36 @@ TEST_CASE("fgetpos", "[process=" + std::to_string(info.comm_size) +
posttest(false);
}

TEST_CASE("fgetpos64", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=single_fgetpos64]"
"[repetition=1][file=1]") {
pretest();
SECTION("test all seek modes") {
FILE* fh = fopen(info.existing_file.c_str(), "r");
REQUIRE(fh != nullptr);
fpos64_t position;

int status = fseek(fh, 0, SEEK_SET);
REQUIRE(status == 0);
status = fgetpos64(fh, &position);
REQUIRE(position.__pos == 0);

status = fseek(fh, 0, SEEK_END);
REQUIRE(status == 0);
status = fgetpos64(fh, &position);
REQUIRE(position.__pos == (long int)info.total_size);

status = fclose(fh);
REQUIRE(status == 0);
}
posttest(false);
}

TEST_CASE("Open64", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=single_open]"
"[repetition=1][file=1]") {
"]"
"[operation=single_open]"
"[repetition=1][file=1]") {
pretest();
SECTION("open non-existant file") {
FILE* fh = fopen64(info.new_file.c_str(), "r");
Expand Down Expand Up @@ -602,9 +714,9 @@ TEST_CASE("Open64", "[process=" + std::to_string(info.comm_size) +
}

TEST_CASE("Freopen64", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=single_freopen]"
"[repetition=1][file=1]") {
"]"
"[operation=single_freopen]"
"[repetition=1][file=1]") {
pretest();
SECTION("change different modes") {
FILE* fhr = fopen(info.existing_file.c_str(), "r");
Expand Down Expand Up @@ -639,3 +751,21 @@ TEST_CASE("Freopen64", "[process=" + std::to_string(info.comm_size) +
}
posttest(false);
}

TEST_CASE("MultiOpen", "[process=" + std::to_string(info.comm_size) +
"]"
"[operation=multi_open]"
"[repetition=1][file=1]") {
pretest();
SECTION("Open same file twice and then close both fps") {
FILE* fh1 = fopen(info.existing_file.c_str(), "r");
REQUIRE(fh1 != nullptr);
FILE* fh2 = fopen(info.existing_file.c_str(), "r");
REQUIRE(fh2 != nullptr);
int status = fclose(fh1);
REQUIRE(status == 0);
status = fclose(fh2);
REQUIRE(status == 0);
}
posttest(false);
}

0 comments on commit 3a455d4

Please sign in to comment.