Skip to content

Commit

Permalink
Add capability of benchmarking arbitrary binary
Browse files Browse the repository at this point in the history
Differential Revision: D64247087

Pull Request resolved: #1107
  • Loading branch information
kimishpatel authored and jainapurva committed Oct 24, 2024
1 parent 127255b commit ab40ce8
Showing 1 changed file with 11 additions and 3 deletions.
14 changes: 11 additions & 3 deletions torchao/experimental/benchmark_infra/ios/output_redirect.mm
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,36 @@
std::string file_name =
std::string(std::getenv("HOME")) + "/tmp/BENCH_LOG";
redirect_out_ = fopen(file_name.c_str(), "w");
stdout_dupfd_ = dup(1);
stdout_dupfd_ = dup(STDOUT_FILENO);
stderr_dupfd_ = dup(STDERR_FILENO);
/* replace stdout with our output fd */
dup2(fileno(redirect_out_), 1);
dup2(fileno(redirect_out_), STDOUT_FILENO);
dup2(fileno(redirect_out_), STDERR_FILENO);
fflush(stdout);
fflush(stderr);
setvbuf(stdout, nil, _IONBF, 0);
setvbuf(stderr, nil, _IONBF, 0);
setvbuf(redirect_out_, nil, _IONBF, 0);
}
}

~STDIORedirector() {
if (@available(iOS 17, *)) {
fflush(stdout);
fflush(stderr);
/* restore stdout */
dup2(stdout_dupfd_, 1);
dup2(stdout_dupfd_, STDOUT_FILENO);
dup2(stderr_dupfd_, STDERR_FILENO);
close(stdout_dupfd_);
close(stderr_dupfd_);
fclose(redirect_out_);
}
}

private:
FILE* redirect_out_;
int stdout_dupfd_;
int stderr_dupfd_;
};

static STDIORedirector stdio_redirector_;

0 comments on commit ab40ce8

Please sign in to comment.