Permalink
Browse files

add statistics option

  • Loading branch information...
Constellation committed Feb 21, 2012
1 parent 43e49df commit 7c0b7cb03ef04c5cc505857e24c8380f8ed3cc51
Showing with 77 additions and 17 deletions.
  1. +10 −7 iv/lv5/main.cc
  2. +1 −1 iv/lv5/railgun/compiler_statement.h
  3. +0 −7 iv/lv5/railgun/op.h
  4. +45 −0 iv/lv5/railgun/statistics.h
  5. +13 −1 iv/lv5/railgun/vm.h
  6. +8 −1 iv/lv5/railgun/vm_fwd.h
View
@@ -31,7 +31,7 @@ iv::lv5::railgun::Code* Compile(iv::lv5::railgun::Context* ctx,
}
int Execute(const iv::core::StringPiece& data,
- const std::string& filename) {
+ const std::string& filename, bool statistics) {
iv::lv5::Error e;
iv::lv5::railgun::Context ctx;
iv::core::FileSource src(data, filename);
@@ -53,13 +53,13 @@ int Execute(const iv::core::StringPiece& data,
const iv::lv5::JSString* const str = res.ToString(&ctx, &e);
if (!e) {
std::fprintf(stderr, "%s\n", str->GetUTF8().c_str());
- return EXIT_FAILURE;
- } else {
- return EXIT_FAILURE;
}
- } else {
- return EXIT_SUCCESS;
+ return EXIT_FAILURE;
}
+ if (statistics) {
+ ctx.vm()->DumpStatistics();
+ }
+ return EXIT_SUCCESS;
}
int DisAssemble(const iv::core::StringPiece& data,
@@ -162,6 +162,9 @@ int main(int argc, char **argv) {
cmd.Add("dis",
"dis",
0, "print bytecode");
+ cmd.Add("statistics",
+ "statistics",
+ 0, "print statistics");
cmd.Add("copyright",
"copyright",
0, "print the copyright");
@@ -220,7 +223,7 @@ int main(int argc, char **argv) {
} else if (cmd.Exist("interp")) {
return Interpret(src, filename);
} else {
- return Execute(src, filename);
+ return Execute(src, filename, cmd.Exist("statistics"));
}
} else {
// Interactive Shell Mode
@@ -575,7 +575,7 @@ class Compiler::TryTarget : private core::Noncopyable<> {
assert(has_finally_);
const Level::Stack& stack = compiler_->level_stack();
const Level& entry = stack.back();
- assert(entry.type() == FINALLY);
+ assert(entry.type() == Level::FINALLY);
assert(entry.holes() == &vec_);
const Jump::Targets* vec = entry.holes();
for (Jump::Targets::const_iterator it = vec->begin(),
View
@@ -156,13 +156,6 @@ V(INSTANTIATE_VARIABLE_BINDING, 3)\
V(INITIALIZE_HEAP_IMMUTABLE, 3)\
V(LOAD_ARGUMENTS, 2)
-struct ERR {
- enum Type {
- NOT_REFERENCE = 0,
- NUM_OF_ERR
- };
-};
-
struct OP {
#define IV_LV5_RAILGUN_DEFINE_ENUM(V, N) V,
enum Type {
@@ -0,0 +1,45 @@
+#ifndef IV_LV5_RAILGUN_STATISTICS_H_
+#define IV_LV5_RAILGUN_STATISTICS_H_
+#include <vector>
+#include <utility>
+#include <algorithm>
+#include <iv/detail/functional.h>
+#include <iv/detail/cstdint.h>
+#include <iv/lv5/railgun/op.h>
+namespace iv {
+namespace lv5 {
+namespace railgun {
+
+class Statistics {
+ public:
+ typedef std::vector<std::pair<uint32_t, OP::Type> > Histogram;
+
+ Statistics()
+ : histogram_() {
+ histogram_.reserve(OP::NUM_OF_OP);
+ for (int i = 0; i < OP::NUM_OF_OP; ++i) {
+ histogram_.push_back(std::make_pair(0, static_cast<OP::Type>(i)));
+ }
+ }
+
+ void Increment(OP::Type opcode) {
+ ++(histogram_[opcode].first);
+ }
+
+ void Dump() const {
+ Histogram hist = histogram_;
+ std::sort(hist.begin(), hist.end(), std::greater<Histogram::value_type>());
+ for (Histogram::const_iterator it = hist.begin(),
+ last = hist.end(); it != last; ++it) {
+ std::string str(OP::String(it->second));
+ str.resize(31, ' ');
+ std::cout << str << " : " << it->first << std::endl;
+ }
+ }
+
+ private:
+ Histogram histogram_;
+};
+
+} } } // namespace iv::lv5::railgun
+#endif // IV_LV5_RAILGUN_STATISTICS_H_
View
@@ -121,9 +121,16 @@ JSVal VM::Execute(Frame* start, Error* e) {
#ifdef IV_LV5_RAILGUN_USE_DIRECT_THREADED_CODE
// direct threading mode
+#ifdef DEBUG
+#define DEFINE_OPCODE(op)\
+ case OP::op:\
+ op:\
+ statistics_.Increment(OP::op);
+#else
#define DEFINE_OPCODE(op)\
case OP::op:\
op:
+#endif // ifdef DEBUG
#define DISPATCH_ERROR() break
@@ -149,9 +156,14 @@ JSVal VM::Execute(Frame* start, Error* e) {
#else
// not direct threading mode
-
+#ifdef DEBUG
+#define DEFINE_OPCODE(op)\
+ case OP::op:\
+ statistics_.Increment(OP::op);
+#else
#define DEFINE_OPCODE(op)\
case OP::op:
+#endif // ifdef DEBUG
#define DISPATCH_ERROR() break
View
@@ -7,6 +7,7 @@
#include <iv/lv5/railgun/operation.h>
#include <iv/lv5/railgun/stack.h>
#include <iv/lv5/railgun/direct_threading.h>
+#include <iv/lv5/railgun/statistics.h>
namespace iv {
namespace lv5 {
namespace railgun {
@@ -29,7 +30,8 @@ class VM {
: ctx_(ctx),
operation_(ctx),
stack_(),
- direct_threading_dispatch_table_(NULL) {
+ direct_threading_dispatch_table_(NULL),
+ statistics_() {
}
template<OP::Type op>
@@ -49,6 +51,10 @@ class VM {
return &stack_;
}
+ void DumpStatistics() const {
+ statistics_.Dump();
+ }
+
#if defined(IV_LV5_RAILGUN_USE_DIRECT_THREADED_CODE)
private:
// dispatch table get pass
@@ -99,6 +105,7 @@ class VM {
Operation operation_;
Stack stack_;
const DirectThreadingDispatchTable* direct_threading_dispatch_table_;
+ Statistics statistics_;
};
} } } // namespace iv::lv5::railgun

0 comments on commit 7c0b7cb

Please sign in to comment.