diff --git a/examples/app/vsgviewer/vsgviewer.cpp b/examples/app/vsgviewer/vsgviewer.cpp index 8eda191b..9de19d66 100644 --- a/examples/app/vsgviewer/vsgviewer.cpp +++ b/examples/app/vsgviewer/vsgviewer.cpp @@ -72,7 +72,6 @@ int main(int argc, char** argv) windowTraits->apiDumpLayer = arguments.read({"--api", "-a"}); windowTraits->synchronizationLayer = arguments.read("--sync"); bool reportAverageFrameRate = arguments.read("--fps"); - if (int mt = 0; arguments.read({"--memory-tracking", "--mt"}, mt)) vsg::Allocator::instance()->setMemoryTracking(mt); if (arguments.read("--double-buffer")) windowTraits->swapchainPreferences.imageCount = 2; if (arguments.read("--triple-buffer")) windowTraits->swapchainPreferences.imageCount = 3; // default if (arguments.read("--IMMEDIATE")) { windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; } diff --git a/examples/commands/vsgocclusionquery/vsgocclusionquery.cpp b/examples/commands/vsgocclusionquery/vsgocclusionquery.cpp index 99c96bec..6ef6b1b5 100644 --- a/examples/commands/vsgocclusionquery/vsgocclusionquery.cpp +++ b/examples/commands/vsgocclusionquery/vsgocclusionquery.cpp @@ -29,7 +29,6 @@ int main(int argc, char** argv) windowTraits->windowTitle = "vsgocclusionquery"; windowTraits->debugLayer = arguments.read({"--debug", "-d"}); windowTraits->apiDumpLayer = arguments.read({"--api", "-a"}); - if (int mt = 0; arguments.read({"--memory-tracking", "--mt"}, mt)) vsg::Allocator::instance()->setMemoryTracking(mt); if (arguments.read("--double-buffer")) windowTraits->swapchainPreferences.imageCount = 2; if (arguments.read("--triple-buffer")) windowTraits->swapchainPreferences.imageCount = 3; // default if (arguments.read("--IMMEDIATE")) windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; diff --git a/examples/commands/vsgtimestamps/vsgtimestamps.cpp b/examples/commands/vsgtimestamps/vsgtimestamps.cpp index 86139971..86bcddb1 100644 --- a/examples/commands/vsgtimestamps/vsgtimestamps.cpp +++ b/examples/commands/vsgtimestamps/vsgtimestamps.cpp @@ -29,7 +29,6 @@ int main(int argc, char** argv) windowTraits->windowTitle = "vsgtimestamps"; windowTraits->debugLayer = arguments.read({"--debug", "-d"}); windowTraits->apiDumpLayer = arguments.read({"--api", "-a"}); - if (int mt = 0; arguments.read({"--memory-tracking", "--mt"}, mt)) vsg::Allocator::instance()->setMemoryTracking(mt); if (arguments.read("--double-buffer")) windowTraits->swapchainPreferences.imageCount = 2; if (arguments.read("--triple-buffer")) windowTraits->swapchainPreferences.imageCount = 3; // default if (arguments.read("--IMMEDIATE")) windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; diff --git a/examples/core/vsgallocator/vsgallocator.cpp b/examples/core/vsgallocator/vsgallocator.cpp index b8707ff1..5e348e3c 100644 --- a/examples/core/vsgallocator/vsgallocator.cpp +++ b/examples/core/vsgallocator/vsgallocator.cpp @@ -9,52 +9,44 @@ #include #include -class CustomAllocator : public vsg::Allocator +class StdAllocator : public vsg::Allocator { public: - CustomAllocator(std::unique_ptr in_nestedAllocator = {}) : + StdAllocator(std::unique_ptr in_nestedAllocator = {}) : vsg::Allocator(std::move(in_nestedAllocator)) { - if (memoryTracking & vsg::MEMORY_TRACKING_REPORT_ACTIONS) - { - std::cout << "CustomAllocator()" << this << std::endl; - } } - ~CustomAllocator() + ~StdAllocator() { - if (memoryTracking & vsg::MEMORY_TRACKING_REPORT_ACTIONS) - { - std::cout << "~CustomAllocator() " << this << std::endl; - } } void report(std::ostream& out) const override { - std::cout << "CustomAllocator::report() " << allocatorMemoryBlocks.size() << std::endl; - vsg::Allocator::report(out); + out << "StdAllocator::report() " << std::endl; } - void* allocate(std::size_t size, vsg::AllocatorAffinity allocatorAffinity = vsg::ALLOCATOR_AFFINITY_OBJECTS) override + void* allocate(std::size_t size, vsg::AllocatorAffinity) override { - void* ptr = Allocator::allocate(size, allocatorAffinity); - if (memoryTracking & vsg::MEMORY_TRACKING_REPORT_ACTIONS) - { - std::cout << "CustomAllocator::allocate(" << size << ", " << allocatorAffinity << ") ptr = " << ptr << std::endl; - } - return ptr; + return operator new (size); //, std::align_val_t{default_alignment}); } bool deallocate(void* ptr, std::size_t size) override { - if (memoryTracking & vsg::MEMORY_TRACKING_REPORT_ACTIONS) - { - std::cout << "CustomAllocator::deallocate(" << ptr << ")" << std::endl; - } - return Allocator::deallocate(ptr, size); + if (nestedAllocator && nestedAllocator->deallocate(ptr, size)) return true; + + operator delete (ptr);//, std::align_val_t{default_alignment}); + return true; } + + size_t deleteEmptyMemoryBlocks() override { return 0; } + size_t totalAvailableSize() const override { return 0; } + size_t totalReservedSize() const override { return 0; } + size_t totalMemorySize() const override { return 0; } + void setBlockSize(vsg::AllocatorAffinity, size_t) {} }; + struct SceneStatistics : public vsg::Inherit { std::map objectCounts; @@ -77,8 +69,7 @@ int main(int argc, char** argv) vsg::CommandLine arguments(&argc, argv); // Allocaotor related command line settings - if (arguments.read("--custom")) vsg::Allocator::instance().reset(new CustomAllocator(std::move(vsg::Allocator::instance()))); - if (int mt; arguments.read({"--memory-tracking", "--mt"}, mt)) vsg::Allocator::instance()->setMemoryTracking(mt); + if (arguments.read("--std")) vsg::Allocator::instance().reset(new StdAllocator(std::move(vsg::Allocator::instance()))); if (int type; arguments.read("--allocator", type)) vsg::Allocator::instance()->allocatorType = vsg::AllocatorType(type); if (size_t objectsBlockSize; arguments.read("--objects", objectsBlockSize)) vsg::Allocator::instance()->setBlockSize(vsg::ALLOCATOR_AFFINITY_OBJECTS, objectsBlockSize); if (size_t nodesBlockSize; arguments.read("--nodes", nodesBlockSize)) vsg::Allocator::instance()->setBlockSize(vsg::ALLOCATOR_AFFINITY_NODES, nodesBlockSize); diff --git a/examples/nodes/vsgannotation/vsgannotation.cpp b/examples/nodes/vsgannotation/vsgannotation.cpp index 5e2d34cd..33836e46 100644 --- a/examples/nodes/vsgannotation/vsgannotation.cpp +++ b/examples/nodes/vsgannotation/vsgannotation.cpp @@ -108,7 +108,6 @@ int main(int argc, char** argv) windowTraits->apiDumpLayer = arguments.read({"--api", "-a"}); windowTraits->synchronizationLayer = arguments.read("--sync"); bool reportAverageFrameRate = arguments.read("--fps"); - if (int mt = 0; arguments.read({"--memory-tracking", "--mt"}, mt)) vsg::Allocator::instance()->setMemoryTracking(mt); if (arguments.read("--double-buffer")) windowTraits->swapchainPreferences.imageCount = 2; if (arguments.read("--triple-buffer")) windowTraits->swapchainPreferences.imageCount = 3; // default if (arguments.read("--IMMEDIATE")) { windowTraits->swapchainPreferences.presentMode = VK_PRESENT_MODE_IMMEDIATE_KHR; } diff --git a/examples/nodes/vsggroups/vsggroups.cpp b/examples/nodes/vsggroups/vsggroups.cpp index c3a34a11..cd8f3ff2 100644 --- a/examples/nodes/vsggroups/vsggroups.cpp +++ b/examples/nodes/vsggroups/vsggroups.cpp @@ -180,15 +180,83 @@ std::shared_ptr createSharedPtrQuadTree(unsigned in return t; } + +// consider tcmalloc? https://goog-perftools.sourceforge.net/doc/tcmalloc.html +// consider Alloc https://www.codeproject.com/Articles/1084801/Replace-malloc-free-with-a-Fast-Fixed-Block-Memory +class StdAllocator : public vsg::Allocator +{ +public: + StdAllocator(std::unique_ptr in_nestedAllocator = {}) : + vsg::Allocator(std::move(in_nestedAllocator)) + { + } + + ~StdAllocator() + { + } + + void report(std::ostream& out) const override + { + out << "StdAllocator::report() " << std::endl; + } + + void* allocate(std::size_t size, vsg::AllocatorAffinity) override + { + return operator new (size); //, std::align_val_t{default_alignment}); + } + + bool deallocate(void* ptr, std::size_t size) override + { + if (nestedAllocator && nestedAllocator->deallocate(ptr, size)) return true; + + operator delete (ptr);//, std::align_val_t{default_alignment}); + return true; + } + + size_t deleteEmptyMemoryBlocks() override { return 0; } + size_t totalAvailableSize() const override { return 0; } + size_t totalReservedSize() const override { return 0; } + size_t totalMemorySize() const override { return 0; } + void setBlockSize(vsg::AllocatorAffinity, size_t) {} +}; + +const size_t KB = 1024; +const size_t MB = 1024 * KB; +const size_t GB = 1024 * MB; + +struct Units +{ + Units(size_t v) : value(v) {} + + size_t value; +}; + +std::ostream& operator<<(std::ostream& out, const Units& size) +{ + if (size.value>GB) out << static_cast(size.value)/static_cast(GB) << " gigabytes"; + else if (size.value>MB) out << static_cast(size.value)/static_cast(MB) << " megabytes"; + else if (size.value>KB) out << static_cast(size.value)/static_cast(KB) <<" kilobytes"; + else out << size.value<<" bytes"; + return out; +} int main(int argc, char** argv) { vsg::CommandLine arguments(&argc, argv); + if (arguments.read("--std")) vsg::Allocator::instance().reset(new StdAllocator(std::move(vsg::Allocator::instance()))); + auto numLevels = arguments.value(11u, {"-l", "--levels"}); auto numTraversals = arguments.value(10u, {"-t", "--traversals"}); auto type = arguments.value(std::string("vsg::Group"), "--type"); auto quiet = arguments.read("-q"); auto inputFilename = arguments.value("", "-i"); auto outputFilename = arguments.value("", "-o"); + + size_t unit = arguments.value(MB, "--unit"); + if (int allocatorType; arguments.read("--allocator", allocatorType)) vsg::Allocator::instance()->allocatorType = vsg::AllocatorType(allocatorType); + if (size_t objectsBlockSize; arguments.read("--objects", objectsBlockSize)) vsg::Allocator::instance()->setBlockSize(vsg::ALLOCATOR_AFFINITY_OBJECTS, objectsBlockSize * unit); + if (size_t nodesBlockSize; arguments.read("--nodes", nodesBlockSize)) vsg::Allocator::instance()->setBlockSize(vsg::ALLOCATOR_AFFINITY_NODES, nodesBlockSize * unit); + if (size_t dataBlockSize; arguments.read("--data", dataBlockSize)) vsg::Allocator::instance()->setBlockSize(vsg::ALLOCATOR_AFFINITY_DATA, dataBlockSize * unit); + vsg::ref_ptr vsg_recordTraversal(arguments.read("-d") ? new vsg::RecordTraversal : nullptr); vsg::ref_ptr vsg_ConstVisitor(arguments.read("-c") ? new VsgConstVisitor : nullptr); if (arguments.errors()) return arguments.writeErrorMessages(std::cerr);