finalize.cc 2.66 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <cinttypes>

#include "wasm.hh"


const int iterations = 100000;

12 13
int live_count = 0;

14 15 16 17 18
void finalize(void* data) {
  intptr_t i = reinterpret_cast<intptr_t>(data);
  if (i % (iterations / 10) == 0) {
    std::cout << "Finalizing #" << i << "..." << std::endl;
  }
19
  --live_count;
20 21
}

22
void run_in_store(wasm::Store* store) {
23 24 25 26 27 28 29 30 31 32 33
  // Load binary.
  std::cout << "Loading binary..." << std::endl;
  std::ifstream file("finalize.wasm");
  file.seekg(0, std::ios_base::end);
  auto file_size = file.tellg();
  file.seekg(0);
  auto binary = wasm::vec<byte_t>::make_uninitialized(file_size);
  file.read(binary.get(), file_size);
  file.close();
  if (file.fail()) {
    std::cout << "> Error loading module!" << std::endl;
34
    exit(1);
35 36 37 38 39 40 41
  }

  // Compile.
  std::cout << "Compiling module..." << std::endl;
  auto module = wasm::Module::make(store, binary);
  if (!module) {
    std::cout << "> Error compiling module!" << std::endl;
42
    exit(1);
43 44 45 46 47 48 49 50 51
  }

  // Instantiate.
  std::cout << "Instantiating modules..." << std::endl;
  for (int i = 0; i <= iterations; ++i) {
    if (i % (iterations / 10) == 0) std::cout << i << std::endl;
    auto instance = wasm::Instance::make(store, module.get(), nullptr);
    if (!instance) {
      std::cout << "> Error instantiating module " << i << "!" << std::endl;
52
      exit(1);
53 54
    }
    instance->set_host_info(reinterpret_cast<void*>(i), &finalize);
55
    ++live_count;
56 57 58 59 60 61 62
  }

  // Shut down.
  std::cout << "Shutting down..." << std::endl;
}


63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
void run() {
  // Initialize.
  std::cout << "Initializing..." << std::endl;
  auto engine = wasm::Engine::make();

  std::cout << "Live count " << live_count << std::endl;
  std::cout << "Creating store 1..." << std::endl;
  auto store1 = wasm::Store::make(engine.get());

  std::cout << "Running in store 1..." << std::endl;
  run_in_store(store1.get());
  std::cout << "Live count " << live_count << std::endl;

  {
    std::cout << "Creating store 2..." << std::endl;
    auto store2 = wasm::Store::make(engine.get());

    std::cout << "Running in store 2..." << std::endl;
    run_in_store(store2.get());
    std::cout << "Live count " << live_count << std::endl;

    std::cout << "Deleting store 2..." << std::endl;
    std::cout << "Live count " << live_count << std::endl;
  }

  std::cout << "Running in store 1..." << std::endl;
  run_in_store(store1.get());
  std::cout << "Live count " << live_count << std::endl;

  std::cout << "Deleting store 1..." << std::endl;
}


96 97
int main(int argc, const char* argv[]) {
  run();
98 99
  std::cout << "Live count " << live_count << std::endl;
  assert(live_count == 0);
100 101 102 103
  std::cout << "Done." << std::endl;
  return 0;
}