diff --git a/mace/core/BUILD b/mace/core/BUILD index 97cf8dd19bf378a4d16e230a90ac5ff0239f9f54..81b731d7a5510635a568317b1f473453121da269 100644 --- a/mace/core/BUILD +++ b/mace/core/BUILD @@ -97,6 +97,7 @@ cc_library( deps = [ ":opencl_headers", "//mace/codegen:generated_opencl_dev", + "//mace/utils:logging", "//mace/utils:utils_hdrs", ], ) @@ -108,5 +109,6 @@ cc_library( deps = [ ":opencl_headers", "//mace/codegen:generated_opencl_prod", + "//mace/utils:logging", ], ) diff --git a/mace/core/net.cc b/mace/core/net.cc index 515d0e98686acaf4c1d795aeee62a7ac9801572f..9afb008ff03b4e4980f198d1f8bd32ebb0ccb42f 100644 --- a/mace/core/net.cc +++ b/mace/core/net.cc @@ -5,6 +5,7 @@ #include "mace/core/net.h" #include "mace/core/workspace.h" #include "mace/utils/utils.h" +#include "mace/utils/memory_logging.h" namespace mace { @@ -36,6 +37,7 @@ SimpleNet::SimpleNet(const std::shared_ptr op_registry, } bool SimpleNet::Run(RunMetadata *run_metadata) { + MACE_MEMORY_LOGGING_GUARD(); VLOG(1) << "Running net " << name_; for (auto iter = operators_.begin(); iter != operators_.end(); ++iter) { bool future_wait = (device_type_ == DeviceType::OPENCL && diff --git a/mace/core/runtime/opencl/opencl_development.cc b/mace/core/runtime/opencl/opencl_development.cc index b0ebcd245b97baff041afa8a75f0810c0a034dda..266eceba678f4156aef681b88fb914c725114ace 100644 --- a/mace/core/runtime/opencl/opencl_development.cc +++ b/mace/core/runtime/opencl/opencl_development.cc @@ -6,6 +6,7 @@ #include "mace/core/runtime/opencl/cl2_header.h" #include "mace/utils/utils.h" +#include "mace/utils/logging.h" namespace mace { diff --git a/mace/core/runtime/opencl/opencl_production.cc b/mace/core/runtime/opencl/opencl_production.cc index 11a793e7a7765dc1dc3cbd835877f3082a4924cd..265fcbefe3a35e0798e5d0e16f63baac1ba05377 100644 --- a/mace/core/runtime/opencl/opencl_production.cc +++ b/mace/core/runtime/opencl/opencl_production.cc @@ -4,6 +4,7 @@ #include #include "mace/core/runtime/opencl/cl2_header.h" +#include "mace/utils/logging.h" namespace mace { diff --git a/mace/utils/BUILD b/mace/utils/BUILD index fbd6e037d4f0802dbab2da55b1097a894e6ce7a4..1788399023cf1b8dc5881f537d85a263047d61b2 100644 --- a/mace/utils/BUILD +++ b/mace/utils/BUILD @@ -16,6 +16,7 @@ cc_library( ], hdrs = [ "logging.h", + "memory_logging.h", ], linkopts = if_android([ "-llog", diff --git a/mace/utils/memory_logging.h b/mace/utils/memory_logging.h new file mode 100644 index 0000000000000000000000000000000000000000..e9d540bccc9ab14607dc39ca55d0ed4ca160d50f --- /dev/null +++ b/mace/utils/memory_logging.h @@ -0,0 +1,98 @@ +// +// Copyright (c) 2017 XiaoMi All rights reserved. +// + +#ifndef MACE_UTILS_MEMORY_LOGGING_H_ +#define MACE_UTILS_MEMORY_LOGGING_H_ + +#include +#include "mace/utils/logging.h" + +namespace mace { + +class MallinfoChangeLogger { + public: + MallinfoChangeLogger(const std::string &name) : name_(name) { + prev_ = mallinfo(); + } + ~MallinfoChangeLogger() { + struct mallinfo curr = mallinfo(); + LogMallinfoChange(name_, curr, prev_); + } + + private: + const std::string name_; + struct mallinfo prev_; + + struct mallinfo LogMallinfoChange(const std::string &name, + const struct mallinfo curr, + const struct mallinfo prev) { + if (prev.arena != curr.arena) { + LOG(INFO) << "[" << name << "] " + << "Non-mmapped space allocated (bytes): " << curr.arena + << ", diff: " << ((int64_t)curr.arena - (int64_t)prev.arena); + } + if (prev.ordblks != curr.ordblks) { + LOG(INFO) << "[" << name << "] " + << "Number of free chunks: " << curr.ordblks << ", diff: " + << ((int64_t)curr.ordblks - (int64_t)prev.ordblks); + } + if (prev.smblks != curr.smblks) { + LOG(INFO) << "[" << name << "] " + << "Number of free fastbin blocks: " << curr.smblks + << ", diff: " << ((int64_t)curr.smblks - (int64_t)prev.smblks); + } + if (prev.hblks != curr.hblks) { + LOG(INFO) << "[" << name << "] " + << "Number of mmapped regions: " << curr.hblks + << ", diff: " << ((int64_t)curr.hblks - (int64_t)prev.hblks); + } + if (prev.hblkhd != curr.hblkhd) { + LOG(INFO) << "[" << name << "] " + << "Space allocated in mmapped regions (bytes): " << curr.hblkhd + << ", diff: " << ((int64_t)curr.hblkhd - (int64_t)prev.hblkhd); + } + if (prev.usmblks != curr.usmblks) { + LOG(INFO) << "[" << name << "] " + << "Maximum total allocated space (bytes): " << curr.usmblks + << ", diff: " + << ((int64_t)curr.usmblks - (int64_t)prev.usmblks); + } + if (prev.fsmblks != curr.fsmblks) { + LOG(INFO) << "[" << name << "] " + << "Space in freed fastbin blocks (bytes): " << curr.fsmblks + << ", diff: " + << ((int64_t)curr.fsmblks - (int64_t)prev.fsmblks); + } + if (prev.uordblks != curr.uordblks) { + LOG(INFO) << "[" << name << "] " + << "Total allocated space (bytes): " << curr.uordblks + << ", diff: " + << ((int64_t)curr.uordblks - (int64_t)prev.uordblks); + } + if (prev.fordblks != curr.fordblks) { + LOG(INFO) << "[" << name << "] " + << "Total free space (bytes): " << curr.fordblks << ", diff: " + << ((int64_t)curr.fordblks - (int64_t)prev.fordblks); + } + if (prev.keepcost != curr.keepcost) { + LOG(INFO) << "[" << name << "] " + << "Top-most, releasable space (bytes): " << curr.keepcost + << ", diff: " + << ((int64_t)curr.keepcost - (int64_t)prev.keepcost); + } + return curr; + } +}; + +#ifdef MACE_ENABLE_MEMORY_LOGGING +#define MACE_MEMORY_LOGGING_GUARD() \ + MallinfoChangeLogger mem_logger_##__line__(std::string(__FILE__) + ":" + \ + std::string(__func__)); +#else +#define MACE_MEMORY_LOGGING_GUARD() +#endif + +} // namespace mace + +#endif // MACE_UTILS_MEMORY_LOGGING_H_ diff --git a/tools/bazel-adb-run.sh b/tools/bazel-adb-run.sh index 0a4188411b7e2fe23d5cbeecb0f37bff6f543332..9c9033b1aebc7e4198432eab8bfd2b7c14954f5a 100755 --- a/tools/bazel-adb-run.sh +++ b/tools/bazel-adb-run.sh @@ -29,6 +29,7 @@ python mace/python/tools/encrypt_opencl_codegen.py \ --cl_kernel_dir=./mace/kernels/opencl/cl/ --output_path=${CODEGEN_DIR}/opencl/opencl_encrypt_program.cc echo "Step 2: Generate version source" +mkdir -p ${CODEGEN_DIR}/version bash mace/tools/git/gen_version_source.sh ${CODEGEN_DIR}/version/version.cc echo "Step 3: Build target"