Skip to content

Commit

Permalink
changed stdio calls to syscalls to make it non-interceptable.
Browse files Browse the repository at this point in the history
  • Loading branch information
hariharan-devarajan committed Aug 23, 2023
1 parent d925697 commit 24e958c
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 29 deletions.
10 changes: 5 additions & 5 deletions src/dlio_profiler/dlio_logger.h
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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<dlio_profiler::ChromeWriter>(nullptr);
writer = std::make_shared<dlio_profiler::ChromeWriter>(-1);
writer->initialize(log_file.data(), this->throw_error);
this->is_init=true;
library_start = get_current_time();
Expand Down
49 changes: 47 additions & 2 deletions src/dlio_profiler/writer/base_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,62 @@
#ifndef DLIO_PROFILER_BASE_WRITER_H
#define DLIO_PROFILER_BASE_WRITER_H

#include <unordered_map>
#include <any>
#include <csignal>
#include <dlio_profiler/core/common.h>

#include <fcntl.h>
#include <stdarg.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/syscall.h>
#include <syscall.h>
#include <unordered_map>
namespace dlio_profiler {
class BaseWriter {
private:
std::unordered_map<char *, std::any> metadata;
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;

Expand Down
37 changes: 19 additions & 18 deletions src/dlio_profiler/writer/chrome_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,45 +17,46 @@
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);
}
}
}

void
dlio_profiler::ChromeWriter::log(std::string &event_name, std::string &category, TimeResolution &start_time, TimeResolution &duration,
std::unordered_map<std::string, std::any> &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());
}
Expand Down
6 changes: 3 additions & 3 deletions src/dlio_profiler/writer/chrome_writer.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<std::string, std::any> &metadata, int process_id);
bool is_first_write;
Expand All @@ -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
}
Expand Down
2 changes: 1 addition & 1 deletion test/test.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down

0 comments on commit 24e958c

Please sign in to comment.