Skip to content

Commit

Permalink
Add option to specify the path of the stats file
Browse files Browse the repository at this point in the history
Summary: Fix a FILE* leak along the way.

Reviewed By: @jdelong

Differential Revision: D2194625
  • Loading branch information
alexmalyshev authored and hhvm-bot committed Jul 6, 2015
1 parent ab476c6 commit 4952751
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
8 changes: 7 additions & 1 deletion hphp/hhbbc/hhbbc.h
Expand Up @@ -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;

Expand Down
4 changes: 4 additions & 0 deletions hphp/hhbbc/main.cpp
Expand Up @@ -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 {
Expand Down Expand Up @@ -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")
Expand Down
31 changes: 23 additions & 8 deletions hphp/hhbbc/stats.cpp
Expand Up @@ -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);
}

Expand Down

0 comments on commit 4952751

Please sign in to comment.