From 24e958cb0109c5d569f145961e1fc3d3a7920845 Mon Sep 17 00:00:00 2001 From: Hariharan Devarajan Date: Tue, 22 Aug 2023 22:29:29 -0700 Subject: [PATCH] changed stdio calls to syscalls to make it non-interceptable. --- src/dlio_profiler/dlio_logger.h | 10 ++--- src/dlio_profiler/writer/base_writer.h | 49 +++++++++++++++++++++- src/dlio_profiler/writer/chrome_writer.cpp | 37 ++++++++-------- src/dlio_profiler/writer/chrome_writer.h | 6 +-- test/test.py | 2 +- 5 files changed, 75 insertions(+), 29 deletions(-) diff --git a/src/dlio_profiler/dlio_logger.h b/src/dlio_profiler/dlio_logger.h index 66be61a7..78330a17 100644 --- a/src/dlio_profiler/dlio_logger.h +++ b/src/dlio_profiler/dlio_logger.h @@ -30,19 +30,19 @@ class DLIOLogger { throw_error = true; } this->is_init=true; - FILE *fp = NULL; + int fd = -1; std::string log_file; if (log_file.empty()) { char *dlio_profiler_log_dir = getenv("DLIO_PROFILER_LOG_DIR"); if (dlio_profiler_log_dir == nullptr) { - fp = stderr; + fd = fileno(stderr); log_file = "STDERR"; } else { if (strcmp(dlio_profiler_log_dir, "STDERR") == 0) { - fp = stderr; + fd = fileno(stderr); log_file = "STDERR"; } else if (strcmp(dlio_profiler_log_dir, "STDOUT") == 0) { - fp = stdout; + fd = fileno(stdout); log_file = "STDOUT"; } else { int pid = getpid(); @@ -57,7 +57,7 @@ class DLIOLogger { } inline void update_log_file(std::string log_file, int process_id = -1) { this->process_id = process_id; - writer = std::make_shared(nullptr); + writer = std::make_shared(-1); writer->initialize(log_file.data(), this->throw_error); this->is_init=true; library_start = get_current_time(); diff --git a/src/dlio_profiler/writer/base_writer.h b/src/dlio_profiler/writer/base_writer.h index df5fb847..61e904b7 100644 --- a/src/dlio_profiler/writer/base_writer.h +++ b/src/dlio_profiler/writer/base_writer.h @@ -5,10 +5,16 @@ #ifndef DLIO_PROFILER_BASE_WRITER_H #define DLIO_PROFILER_BASE_WRITER_H -#include #include +#include #include - +#include +#include +#include +#include +#include +#include +#include namespace dlio_profiler { class BaseWriter { private: @@ -16,6 +22,45 @@ namespace dlio_profiler { protected: bool throw_error; std::string filename; + int dlp_open(const char *pathname, int flags, ...) { + mode_t mode; + va_list args; + long result; + + va_start(args, flags); + if (flags & O_CREAT) { + mode = va_arg(args, mode_t); + } + else { + mode = 0; + } + va_end(args); +#if defined(SYS_open) + result = syscall(SYS_open, pathname, flags, mode); +#else + result = syscall(SYS_openat, AT_FDCWD, pathname, flags, mode); +#endif + + if (result >= 0) + return (int) result; + return -1; + } + + ssize_t dlp_write(int fd, const void *buf, size_t count) { + return syscall(SYS_write, fd, buf, count); + } + + ssize_t dlp_read(int fd, void *buf, size_t count) { + return syscall(SYS_read, fd, buf, count); + } + + int dlp_close(int fd) { + return syscall(SYS_close, fd); + } + + int dlp_fsync(int fd) { + return syscall(SYS_fsync, fd); + } public: virtual void initialize(char *filename, bool throw_error) = 0; diff --git a/src/dlio_profiler/writer/chrome_writer.cpp b/src/dlio_profiler/writer/chrome_writer.cpp index 7516dbde..a8cf7576 100644 --- a/src/dlio_profiler/writer/chrome_writer.cpp +++ b/src/dlio_profiler/writer/chrome_writer.cpp @@ -17,10 +17,12 @@ void dlio_profiler::ChromeWriter::initialize(char *filename, bool throw_error) { this->throw_error = throw_error; this->filename = filename; - if (this->fp == nullptr) { - fp = fopen(filename, "a+"); - if (fp == nullptr) { - ERROR(fp == nullptr,"unable to create log file %s", filename); + if (fd == -1) { + fd = dlp_open(filename, O_WRONLY | O_APPEND | O_CREAT, S_IRUSR | S_IWUSR); + if (fd == -1) { + ERROR(fd == -1,"unable to create log file %s", filename); + } else { + DLIO_PROFILER_LOGINFO("created log file %s with fd %d", filename, fd); } } } @@ -28,34 +30,33 @@ void dlio_profiler::ChromeWriter::initialize(char *filename, bool throw_error) { void dlio_profiler::ChromeWriter::log(std::string &event_name, std::string &category, TimeResolution &start_time, TimeResolution &duration, std::unordered_map &metadata, int process_id) { - if (fp != nullptr) { + if (fd != -1) { std::string json = convert_json(event_name, category, start_time, duration, metadata, process_id); - auto written_elements = fwrite(json.c_str(), json.size(), sizeof(char), fp); - fflush(fp); - if (written_elements != 1) { - ERROR(written_elements != 1, "unable to write to log file %s", filename.c_str()); + auto written_elements = dlp_write(fd, json.c_str(), json.size()); + if (written_elements != json.size()) { + ERROR(written_elements != json.size(), "unable to log write %s fd %d for a+ written only %d of %d with error %s", filename.c_str(), fd, written_elements, json.size(), strerror(errno)); } } is_first_write = false; } void dlio_profiler::ChromeWriter::finalize() { - if (fp != nullptr) { + if (fd != -1) { DLIO_PROFILER_LOGINFO("Profiler finalizing writer %s\n", filename .c_str()); - int status = fclose(fp); + int status = dlp_close(fd); if (status != 0) { ERROR(status != 0, "unable to close log file %d for a+", filename.c_str()); } - fp = fopen(this->filename.c_str(), "r+"); - if (fp == nullptr) { - ERROR(fp == nullptr,"unable to open log file %s with r+", this->filename.c_str()); + fd = dlp_open(this->filename.c_str(), O_WRONLY); + if (fd != -1) { + ERROR(fd != -1,"unable to open log file %s with r+", this->filename.c_str()); } std::string data = "[\n"; - auto written_elements = fwrite(data.c_str(), data.size(), sizeof(char), fp); - if (written_elements != 1) { - ERROR(written_elements != 1, "unable to finalize log write %s for r+", filename.c_str()); + auto written_elements = dlp_write(fd, data.c_str(), data.size()); + if (written_elements != data.size()) { + ERROR(written_elements != data.size(), "unable to finalize log write %s for r+ written only %d of %d", filename.c_str(), data.size(), written_elements); } - status = fclose(fp); + status = dlp_close(fd); if (status != 0) { ERROR(status != 0, "unable to close log file %d for r+", filename.c_str()); } diff --git a/src/dlio_profiler/writer/chrome_writer.h b/src/dlio_profiler/writer/chrome_writer.h index 18e5570e..ebb60349 100644 --- a/src/dlio_profiler/writer/chrome_writer.h +++ b/src/dlio_profiler/writer/chrome_writer.h @@ -15,7 +15,7 @@ namespace dlio_profiler { class ChromeWriter: public BaseWriter { private: hwloc_topology_t topology; - FILE* fp; + int fd; std::string convert_json(std::string &event_name, std::string &category, TimeResolution start_time, TimeResolution duration, std::unordered_map &metadata, int process_id); bool is_first_write; @@ -38,9 +38,9 @@ namespace dlio_profiler { return std::string(hostname); } public: - ChromeWriter(FILE* fp=nullptr):BaseWriter(), is_first_write(true), mtx_map(){ + ChromeWriter(int fd=-1):BaseWriter(), is_first_write(true), mtx_map(){ process_id = getpid(); - this->fp = fp; + this->fd = fd; hwloc_topology_init(&topology); // initialization hwloc_topology_load(topology); // actual detection } diff --git a/test/test.py b/test/test.py index c463c765..82c4ec33 100644 --- a/test/test.py +++ b/test/test.py @@ -40,8 +40,8 @@ def write_read_jpeg(index): img.save(out_path_spec, format='JPEG', bits=8) with open(out_path_spec, "rb") as f: image = im.open(f) + out_records = np.asarray(image) #image = im.open(out_path_spec) - out_records = np.asarray(image) import threading