Commit a7d756fa authored by Kevin Modzelewski's avatar Kevin Modzelewski

Merge pull request #469 from toshok/stats-work

Stats
parents f95bd65f d30281e3
......@@ -361,6 +361,8 @@ static void handle_sigint(int signum) {
// TODO: this should set a flag saying a KeyboardInterrupt is pending.
// For now, just call abort(), so that we get a traceback at least.
fprintf(stderr, "SIGINT!\n");
joinRuntime();
Stats::dump();
abort();
}
......
......@@ -23,6 +23,7 @@ namespace pyston {
#if !DISABLE_STATS
std::vector<long>* Stats::counts;
std::unordered_map<int, std::string>* Stats::names;
bool Stats::enabled;
StatCounter::StatCounter(const std::string& name) : id(Stats::getStatId(name)) {
}
......@@ -50,7 +51,10 @@ int Stats::getStatId(const std::string& name) {
return rtn;
}
void Stats::dump() {
void Stats::dump(bool includeZeros) {
if (!Stats::enabled)
return;
printf("Stats:\n");
std::vector<std::pair<std::string, int>> pairs;
......@@ -61,7 +65,8 @@ void Stats::dump() {
std::sort(pairs.begin(), pairs.end());
for (int i = 0; i < pairs.size(); i++) {
printf("%s: %ld\n", pairs[i].first.c_str(), (*counts)[pairs[i].second]);
if (includeZeros || (*counts)[pairs[i].second] > 0)
printf("%s: %ld\n", pairs[i].first.c_str(), (*counts)[pairs[i].second]);
}
}
......
......@@ -32,13 +32,16 @@ struct Stats {
private:
static std::vector<long>* counts;
static std::unordered_map<int, std::string>* names;
static bool enabled;
public:
static int getStatId(const std::string& name);
static void setEnabled(bool enabled) { Stats::enabled = enabled; }
static void log(int id, int count = 1) { (*counts)[id] += count; }
static void dump();
static void clear() { std::fill(counts->begin(), counts->end(), 0); }
static void dump(bool includeZeros = true);
static void endOfInit();
};
......@@ -64,6 +67,7 @@ public:
#else
struct Stats {
static void setEnabled(bool enabled) {}
static void dump() { printf("(Stats disabled)\n"); }
static void log(int id, int count = 1) {}
static int getStatId(const std::string& name) { return 0; }
......
......@@ -95,7 +95,6 @@ static int main(int argc, char** argv) {
int code;
bool force_repl = false;
bool stats = false;
bool unbuffered = false;
const char* command = NULL;
......@@ -123,7 +122,7 @@ static int main(int argc, char** argv) {
} else if (code == 'j') {
DUMPJIT = true;
} else if (code == 's') {
stats = true;
Stats::setEnabled(true);
} else if (code == 'S') {
Py_NoSiteFlag = 1;
} else if (code == 'u') {
......@@ -241,8 +240,7 @@ static int main(int argc, char** argv) {
} catch (ExcInfo e) {
int retcode = 1;
(void)handle_toplevel_exn(e, &retcode);
if (stats)
Stats::dump();
Stats::dump();
return retcode;
}
}
......@@ -269,8 +267,7 @@ static int main(int argc, char** argv) {
int retcode = 1;
(void)handle_toplevel_exn(e, &retcode);
if (!force_repl) {
if (stats)
Stats::dump();
Stats::dump();
return retcode;
}
}
......@@ -320,8 +317,7 @@ static int main(int argc, char** argv) {
} catch (ExcInfo e) {
int retcode = 0xdeadbeef; // should never be seen
if (handle_toplevel_exn(e, &retcode)) {
if (stats)
Stats::dump();
Stats::dump();
return retcode;
}
}
......@@ -340,8 +336,7 @@ static int main(int argc, char** argv) {
int rtncode = joinRuntime();
_t.split("finishing up");
if (stats)
Stats::dump();
Stats::dump();
return rtncode;
}
......
......@@ -48,10 +48,29 @@ static Box* setOption(Box* option, Box* value) {
return None;
}
static Box* clearStats() {
Stats::clear();
return None;
}
static Box* dumpStats(Box* includeZeros) {
if (includeZeros->cls != bool_cls)
raiseExcHelper(TypeError, "includeZeros must be a 'bool' object but received a '%s'",
getTypeName(includeZeros));
Stats::dump(((BoxedBool*)includeZeros)->n != 0);
return None;
}
void setupPyston() {
pyston_module = createModule("__pyston__", "__builtin__");
pyston_module->giveAttr("setOption",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)setOption, UNKNOWN, 2), "setOption"));
pyston_module->giveAttr("clearStats",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)clearStats, NONE, 0), "clearStats"));
pyston_module->giveAttr("dumpStats",
new BoxedBuiltinFunctionOrMethod(boxRTFunction((void*)dumpStats, NONE, 1, 1, false, false),
"dumpStats", { False }));
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment