Skip to content

Commit

Permalink
Add detailed block allocator stats (#612)
Browse files Browse the repository at this point in the history
  • Loading branch information
marcauberer authored Jul 23, 2024
1 parent 2fd6a22 commit 3fac6dc
Show file tree
Hide file tree
Showing 9 changed files with 49 additions and 29 deletions.
8 changes: 8 additions & 0 deletions Options.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -128,3 +128,11 @@ if (SPICE_TSAN)
else ()
message(STATUS "Spice: Thread sanitizer disabled.")
endif ()

option(SPICE_OVERLOAD_NEW_DELETE "" OFF)
if (SPICE_OVERLOAD_NEW_DELETE)
message(STATUS "Spice: New and delete operators are overloaded")
add_compile_definitions(SPICE_NEW_DELETE_OVERLOADED)
else ()
message(STATUS "Spice: New and delete operators are not overloaded")
endif ()
1 change: 0 additions & 1 deletion src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ set(SOURCES
ast/ASTBuilder.h
ast/Attributes.h
# AST visualizer
visualizer/ASTVisualizer.cpp
visualizer/ASTVisualizer.h
# Import collector
importcollector/ImportCollector.cpp
Expand Down
3 changes: 3 additions & 0 deletions src/SourceFile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ void SourceFile::runBackEnd() { // NOLINT(misc-no-recursion)
std::cout << " or " << std::to_string(totalLineCount) << " lines in total.\n";
std::cout << "Total number of blocks allocated via BlockAllocator: " << CommonUtil::formatBytes(allocatedBytes);
std::cout << " in " << std::to_string(allocationCount) << " allocations.\n";
#ifndef NDEBUG
resourceManager.astNodeAlloc.printAllocatedClassStatistic();
#endif
std::cout << "Total number of types: " << std::to_string(totalTypeCount) << "\n";
std::cout << "Total compile time: " << std::to_string(totalDuration) << " ms\n";
}
Expand Down
15 changes: 15 additions & 0 deletions src/util/BlockAllocator.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,19 +46,34 @@ template <typename Base> class BlockAllocator {
T *ptr = new (destAddr) T(std::forward<Args>(args)...);
allocatedObjects.push_back(ptr);

#ifndef NDEBUG
allocatedClassStatistic[typeid(T).name()]++;
#endif

// Update offset to be ready to store the next object
offsetInBlock += objSize;
return ptr;
}

[[nodiscard]] size_t getTotalAllocatedSize() const { return memoryBlocks.size() * blockSize; }
[[nodiscard]] size_t getAllocationCount() const { return allocatedObjects.size(); }
#ifndef NDEBUG
void printAllocatedClassStatistic() const {
std::vector<std::pair<const char *, size_t>> elements(allocatedClassStatistic.begin(), allocatedClassStatistic.end());
std::sort(elements.begin(), elements.end(), [](const auto &left, const auto &right) { return left.second > right.second; });
for (const auto &[mangledName, count] : elements)
std::cout << CommonUtil::demangleTypeName(mangledName) << ": " << count << std::endl;
}
#endif

private:
// Private members
const MemoryManager &memoryManager;
std::vector<byte *> memoryBlocks;
std::vector<Base *> allocatedObjects;
#ifndef NDEBUG
std::unordered_map<const char *, size_t> allocatedClassStatistic;
#endif
size_t blockSize;
size_t offsetInBlock = 0;

Expand Down
17 changes: 17 additions & 0 deletions src/util/CommonUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,23 @@ std::string CommonUtil::formatBytes(size_t bytes) {
return {buffer};
}

/**
* Demangle CXX type name
*
* @param mangledName Mangled CXX type name
* @return Demangled name
*/
std::string CommonUtil::demangleTypeName(const char *mangledName) {
int status;
char *demangled = abi::__cxa_demangle(mangledName, nullptr, nullptr, &status);
if (status == 0) {
std::string result(demangled);
free(demangled);
return result;
}
return mangledName;
}

/**
* Check if the given string is a valid mangled name
*
Expand Down
1 change: 1 addition & 0 deletions src/util/CommonUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class CommonUtil {
static std::vector<std::string> split(const std::string &input);
static size_t getSystemPageSize();
static std::string formatBytes(size_t bytes);
static std::string demangleTypeName(const char *mangledName);
static bool isValidMangledName(const std::string &mangledName);
static std::string getCircularImportMessage(std::stack<const SourceFile *> &sourceFiles);
static std::string getVersionInfo();
Expand Down
6 changes: 3 additions & 3 deletions src/util/Memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class DefaultMemoryManager : public MemoryManager {
} // namespace spice::compiler

// Overload new and delete operators for debugging purposes
#ifdef false
void *operator new(std::size_t n) noexcept(false) { return malloc(n); }
#ifdef SPICE_NEW_DELETE_OVERLOADED
void *operator new(size_t n) noexcept(false) { return malloc(n); }
void operator delete(void *ptr) noexcept { free(ptr); }
#endif
#endif
22 changes: 0 additions & 22 deletions src/visualizer/ASTVisualizer.cpp

This file was deleted.

5 changes: 2 additions & 3 deletions src/visualizer/ASTVisualizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <CompilerPass.h>
#include <ast/ASTNodes.h>
#include <ast/AbstractASTVisitor.h>
#include <util/CommonUtil.h>

namespace spice::compiler {

Expand Down Expand Up @@ -117,7 +118,7 @@ class ASTVisualizer : private CompilerPass, public AbstractASTVisitor {
std::stringstream result;

// Prepare strings
std::string typeName(demangleTypeName(typeid(T).name()));
std::string typeName(CommonUtil::demangleTypeName(typeid(T).name()));
std::string codeLoc = node->codeLoc.toString();
std::string nodeName = typeName.substr(typeName.rfind("::") + 2);
std::string nodeId = codeLoc + "_" + nodeName;
Expand All @@ -141,8 +142,6 @@ class ASTVisualizer : private CompilerPass, public AbstractASTVisitor {

return result.str();
}

static std::string demangleTypeName(const char *mangledName);
};

} // namespace spice::compiler

0 comments on commit 3fac6dc

Please sign in to comment.