diff --git a/hphp/hhbbc/hhbbc.h b/hphp/hhbbc/hhbbc.h index b3f7e3369661b..931c1971090da 100644 --- a/hphp/hhbbc/hhbbc.h +++ b/hphp/hhbbc/hhbbc.h @@ -256,12 +256,18 @@ struct Options { */ bool HardPrivatePropInference = true; - /** + /* * If true, we'll assume that dynamic function calls (like '$f()') do not * have effects on unknown locals (i.e. are not extract / compact /...). * See, e.g. __SystemLib\\extract vs extract. */ bool DisallowDynamicVarEnvFuncs = true; + + /* + * The filepath where to save the stats file. If the path is empty, then we + * save the stats file to a temporary file. + */ + std::string stats_file; }; extern Options options; diff --git a/hphp/hhbbc/main.cpp b/hphp/hhbbc/main.cpp index d5312d58513c2..b6aad0aa74806 100644 --- a/hphp/hhbbc/main.cpp +++ b/hphp/hhbbc/main.cpp @@ -39,6 +39,7 @@ #include "hphp/runtime/vm/repo-global-data.h" #include "hphp/hhbbc/misc.h" +#include "hphp/hhbbc/stats.h" #include "hphp/hhbbc/parallel.h" namespace HPHP { namespace HHBBC { @@ -75,6 +76,9 @@ void parse_options(int argc, char** argv) { ("input", po::value(&input_repo)->default_value("hhvm.hhbc"), "input hhbc repo path") + ("stats-file", + po::value(&options.stats_file)->default_value(""), + "stats file path") ("no-optimizations", po::bool_switch(&options.NoOptimizations), "turn off all optimizations") diff --git a/hphp/hhbbc/stats.cpp b/hphp/hhbbc/stats.cpp index 88dd012d6ca7f..6ffd254b66e91 100644 --- a/hphp/hhbbc/stats.cpp +++ b/hphp/hhbbc/stats.cpp @@ -520,17 +520,32 @@ void print_stats(const Index& index, const php::Program& program) { std::cout << str; } - char fileBuf[] = "/tmp/hhbbcXXXXXX"; - int fd = mkstemp(fileBuf); - if (fd == -1) { - std::cerr << "couldn't open temporary file for stats: " + auto stats_file = options.stats_file; + + auto const file = [&] () -> std::FILE* { + if (!stats_file.empty()) { + return fopen(stats_file.c_str(), "w"); + } + + char fileBuf[] = "/tmp/hhbbcXXXXXX"; + auto const fd = mkstemp(fileBuf); + stats_file = fileBuf; + if (fd == -1) return nullptr; + + if (auto const fp = fdopen(fd, "w")) return fp; + close(fd); + return nullptr; + }(); + + if (file == nullptr) { + std::cerr << "couldn't open file for stats: " << folly::errnoStr(errno) << '\n'; return; } - SCOPE_EXIT { close(fd); }; - auto file = fdopen(fd, "w"); - std::cout << "stats saved to " << fileBuf << '\n'; - std::fprintf(file, "%s", str.c_str()); + + SCOPE_EXIT { fclose(file); }; + std::cout << "stats saved to " << stats_file << '\n'; + std::fwrite(str.c_str(), sizeof(char), str.size(), file); std::fflush(file); }