From d783753cb5050e3a16363cd8a2bfb4b361c130c0 Mon Sep 17 00:00:00 2001 From: madmaxoft Date: Wed, 27 Aug 2014 09:46:59 +0200 Subject: AnvilStats: initial per-height blocktype implementation (WIP). --- Tools/AnvilStats/Globals.h | 11 ++++++ Tools/AnvilStats/Statistics.cpp | 87 ++++++++++++++++++++++++++++++++++++++--- Tools/AnvilStats/Statistics.h | 3 ++ 3 files changed, 96 insertions(+), 5 deletions(-) diff --git a/Tools/AnvilStats/Globals.h b/Tools/AnvilStats/Globals.h index df1430cc4..21d54739a 100644 --- a/Tools/AnvilStats/Globals.h +++ b/Tools/AnvilStats/Globals.h @@ -241,6 +241,17 @@ public: +/** Clamp value to the specified range. */ +template +T Clamp(T a_Value, T a_Min, T a_Max) +{ + return (a_Value < a_Min) ? a_Min : ((a_Value > a_Max) ? a_Max : a_Value); +} + + + + + // Common headers (part 2, with macros): #include "../../src/ChunkDef.h" #include "../../src/BlockID.h" diff --git a/Tools/AnvilStats/Statistics.cpp b/Tools/AnvilStats/Statistics.cpp index f7519fd37..11b9f09c5 100644 --- a/Tools/AnvilStats/Statistics.cpp +++ b/Tools/AnvilStats/Statistics.cpp @@ -26,9 +26,11 @@ cStatistics::cStats::cStats(void) : m_MinChunkZ(0x7fffffff), m_MaxChunkZ(0x80000000) { - memset(m_BiomeCounts, 0, sizeof(m_BiomeCounts)); - memset(m_BlockCounts, 0, sizeof(m_BlockCounts)); - memset(m_SpawnerEntity, 0, sizeof(m_SpawnerEntity)); + memset(m_BiomeCounts, 0, sizeof(m_BiomeCounts)); + memset(m_BlockCounts, 0, sizeof(m_BlockCounts)); + memset(m_PerHeightBlockCounts, 0, sizeof(m_PerHeightBlockCounts)); + memset(m_PerHeightSpawners, 0, sizeof(m_PerHeightSpawners)); + memset(m_SpawnerEntity, 0, sizeof(m_SpawnerEntity)); } @@ -46,6 +48,11 @@ void cStatistics::cStats::Add(const cStatistics::cStats & a_Stats) for (int j = 0; j <= 255; j++) { m_BlockCounts[i][j] += a_Stats.m_BlockCounts[i][j]; + m_PerHeightBlockCounts[i][j] += a_Stats.m_PerHeightBlockCounts[i][j]; + } + for (int j = 0; j < ARRAYCOUNT(m_PerHeightSpawners[0]); j++) + { + m_PerHeightSpawners[i][j] += a_Stats.m_PerHeightSpawners[i][j]; } } for (int i = 0; i < ARRAYCOUNT(m_SpawnerEntity); i++) @@ -149,6 +156,7 @@ bool cStatistics::OnSection for (int y = 0; y < 16; y++) { + int Height = (int)a_Y * 16 + y; for (int z = 0; z < 16; z++) { for (int x = 0; x < 16; x++) @@ -156,6 +164,7 @@ bool cStatistics::OnSection unsigned char Biome = m_BiomeData[x + 16 * z]; // Cannot use cChunkDef, different datatype unsigned char BlockType = cChunkDef::GetBlock(a_BlockTypes, x, y, z); m_Stats.m_BlockCounts[Biome][BlockType] += 1; + m_Stats.m_PerHeightBlockCounts[Height][BlockType] += 1; } } } @@ -259,16 +268,27 @@ bool cStatistics::OnTileTick( void cStatistics::OnSpawner(cParsedNBT & a_NBT, int a_TileEntityTag) { + // Get the spawned entity type: int EntityIDTag = a_NBT.FindChildByName(a_TileEntityTag, "EntityId"); if ((EntityIDTag < 0) || (a_NBT.GetType(EntityIDTag) != TAG_String)) { return; } eEntityType Ent = GetEntityType(a_NBT.GetString(EntityIDTag)); - if (Ent < ARRAYCOUNT(m_Stats.m_SpawnerEntity)) + if (Ent >= ARRAYCOUNT(m_Stats.m_SpawnerEntity)) + { + return; + } + m_Stats.m_SpawnerEntity[Ent] += 1; + + // Get the spawner pos: + int PosYTag = a_NBT.FindChildByName(a_TileEntityTag, "y"); + if ((PosYTag < 0) || (a_NBT.GetType(PosYTag) != TAG_Int)) { - m_Stats.m_SpawnerEntity[Ent] += 1; + return; } + int BlockY = Clamp(a_NBT.GetInt(PosYTag), 0, 255); + m_Stats.m_PerHeightSpawners[BlockY][Ent] += 1; } @@ -316,6 +336,8 @@ cStatisticsFactory::~cStatisticsFactory() SaveBiomes(); LOG(" BlockTypes.xls"); SaveBlockTypes(); + LOG(" PerHeightBlockTypes.xls"); + SavePerHeightBlockTypes(); LOG(" BiomeBlockTypes.xls"); SaveBiomeBlockTypes(); LOG(" Spawners.xls"); @@ -395,6 +417,61 @@ void cStatisticsFactory::SaveBlockTypes(void) +void cStatisticsFactory::SavePerHeightBlockTypes(void) +{ + // Export as two tables: biomes 0-127 and 128-255, because OpenOffice doesn't support more than 256 columns + + cFile f; + if (!f.Open("PerHeightBlockTypes.xls", cFile::fmWrite)) + { + LOG("Cannot write to file PerHeightBlockTypes.xls. Statistics not written."); + return; + } + + // Write header: + f.Printf("Blocks 0 - 127:\nHeight\t"); + for (int i = 0; i < 128; i++) + { + f.Printf("\t%s(%d)", GetBlockTypeString(i), i); + } + f.Printf("\n"); + + // Write first half: + for (int y = 0; y < 256; y++) + { + f.Printf("%d", y); + for (int BlockType = 0; BlockType < 128; BlockType++) + { + f.Printf("\t%d", m_CombinedStats.m_PerHeightBlockCounts[y][BlockType]); + } // for BlockType + f.Printf("\n"); + } // for y - height (0 - 127) + f.Printf("\n"); + + // Write second header: + f.Printf("Blocks 128 - 255:\nHeight\t"); + for (int i = 128; i < 256; i++) + { + f.Printf("\t%s(%d)", GetBlockTypeString(i), i); + } + f.Printf("\n"); + + // Write second half: + for (int y = 0; y < 256; y++) + { + f.Printf("%d", y); + for (int BlockType = 128; BlockType < 256; BlockType++) + { + f.Printf("\t%d", m_CombinedStats.m_PerHeightBlockCounts[y][BlockType]); + } // for BlockType + f.Printf("\n"); + } // for y - height (0 - 127) +} + + + + + void cStatisticsFactory::SaveBiomeBlockTypes(void) { // Export as two tables: biomes 0-127 and 128-255, because OpenOffice doesn't support more than 256 columns diff --git a/Tools/AnvilStats/Statistics.h b/Tools/AnvilStats/Statistics.h index 53e353f22..f6165edd9 100644 --- a/Tools/AnvilStats/Statistics.h +++ b/Tools/AnvilStats/Statistics.h @@ -31,6 +31,8 @@ public: UInt64 m_NumEntities; UInt64 m_NumTileEntities; UInt64 m_NumTileTicks; + UInt64 m_PerHeightBlockCounts[256][256]; // First dimension is the height, second dimension is BlockType + UInt64 m_PerHeightSpawners[256][entMax + 1]; // First dimension is the height, second dimension is spawned entity type int m_MinChunkX, m_MaxChunkX; // X coords range int m_MinChunkZ, m_MaxChunkZ; // Z coords range @@ -128,6 +130,7 @@ protected: void JoinResults(void); void SaveBiomes(void); void SaveBlockTypes(void); + void SavePerHeightBlockTypes(void); void SaveBiomeBlockTypes(void); void SaveStatistics(void); void SaveSpawners(void); -- cgit v1.2.3 From ccaa51f9132a1427a82e24a5d6eaf5d1992f7df9 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 27 Aug 2014 11:52:54 +0300 Subject: AnvilStats: Fixed Win64 compilation. --- Tools/AnvilStats/Utils.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tools/AnvilStats/Utils.cpp b/Tools/AnvilStats/Utils.cpp index baa87bd69..d7543cb4c 100644 --- a/Tools/AnvilStats/Utils.cpp +++ b/Tools/AnvilStats/Utils.cpp @@ -272,7 +272,7 @@ extern const char * GetEntityTypeString(eEntityType a_EntityType) int GetNumCores(void) { // Get number of cores by querying the system process affinity mask (Windows-specific) - DWORD Affinity, ProcAffinity; + DWORD_PTR Affinity, ProcAffinity; GetProcessAffinityMask(GetCurrentProcess(), &ProcAffinity, &Affinity); int NumCores = 0; while (Affinity > 0) -- cgit v1.2.3 From b361e994ef4aac5e2f5672a291d4abf1a3714bc6 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 27 Aug 2014 11:53:46 +0300 Subject: AnvilStats: Added cmake directive for larger executable stack. This fixes runtime "stack overflow" errors caused by large stack-allocated arrays used for decompression. --- Tools/AnvilStats/CMakeLists.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/Tools/AnvilStats/CMakeLists.txt b/Tools/AnvilStats/CMakeLists.txt index 8cbcf6be4..557a4c17a 100644 --- a/Tools/AnvilStats/CMakeLists.txt +++ b/Tools/AnvilStats/CMakeLists.txt @@ -118,5 +118,27 @@ add_executable(AnvilStats ${SHARED_OSS_HDR} ) + target_link_libraries(AnvilStats zlib) + + + + +# Under MSVC we need to enlarge the default stack size for the executable: +if (MSVC) + get_target_property(TEMP AnvilStats LINK_FLAGS) + if (TEMP STREQUAL "TEMP-NOTFOUND") + SET(TEMP "") # set to empty string + message("LINKER_FLAGS not found") + else () + SET(TEMP "${TEMP} ") # a space to cleanly separate from existing content + message("LINKER_FLAGS: ${LINKER_FLAGS}") + endif () + # append our values + SET(TEMP "${TEMP}/STACK:16777216") + set_target_properties(AnvilStats PROPERTIES LINK_FLAGS ${TEMP}) +endif () + + + -- cgit v1.2.3 From 751d345c59bbaf265393c5306f181ee3654a35a6 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 27 Aug 2014 11:54:22 +0300 Subject: AnvilStats: Ignoring output XLS files. --- Tools/AnvilStats/.gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/Tools/AnvilStats/.gitignore b/Tools/AnvilStats/.gitignore index afd34cdda..f093bbe7d 100644 --- a/Tools/AnvilStats/.gitignore +++ b/Tools/AnvilStats/.gitignore @@ -11,3 +11,4 @@ Profiling *.png world/ *.html +*.xls -- cgit v1.2.3 From af47b5ece2bfe274e305f0602dff8e7a74ae0360 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 27 Aug 2014 12:24:56 +0300 Subject: AnvilStats: Added per-height spawner stats. --- Tools/AnvilStats/Statistics.cpp | 48 +++++++++++++++++++++++++++++++++++++---- Tools/AnvilStats/Statistics.h | 3 +++ 2 files changed, 47 insertions(+), 4 deletions(-) diff --git a/Tools/AnvilStats/Statistics.cpp b/Tools/AnvilStats/Statistics.cpp index 11b9f09c5..c8a98b488 100644 --- a/Tools/AnvilStats/Statistics.cpp +++ b/Tools/AnvilStats/Statistics.cpp @@ -342,6 +342,8 @@ cStatisticsFactory::~cStatisticsFactory() SaveBiomeBlockTypes(); LOG(" Spawners.xls"); SaveSpawners(); + LOG(" PerHeightSpawners.xls"); + SavePerHeightSpawners(); } @@ -429,7 +431,7 @@ void cStatisticsFactory::SavePerHeightBlockTypes(void) } // Write header: - f.Printf("Blocks 0 - 127:\nHeight\t"); + f.Printf("Blocks 0 - 127:\nHeight"); for (int i = 0; i < 128; i++) { f.Printf("\t%s(%d)", GetBlockTypeString(i), i); @@ -442,14 +444,14 @@ void cStatisticsFactory::SavePerHeightBlockTypes(void) f.Printf("%d", y); for (int BlockType = 0; BlockType < 128; BlockType++) { - f.Printf("\t%d", m_CombinedStats.m_PerHeightBlockCounts[y][BlockType]); + f.Printf("\t%llu", m_CombinedStats.m_PerHeightBlockCounts[y][BlockType]); } // for BlockType f.Printf("\n"); } // for y - height (0 - 127) f.Printf("\n"); // Write second header: - f.Printf("Blocks 128 - 255:\nHeight\t"); + f.Printf("Blocks 128 - 255:\nHeight"); for (int i = 128; i < 256; i++) { f.Printf("\t%s(%d)", GetBlockTypeString(i), i); @@ -462,7 +464,7 @@ void cStatisticsFactory::SavePerHeightBlockTypes(void) f.Printf("%d", y); for (int BlockType = 128; BlockType < 256; BlockType++) { - f.Printf("\t%d", m_CombinedStats.m_PerHeightBlockCounts[y][BlockType]); + f.Printf("\t%llu", m_CombinedStats.m_PerHeightBlockCounts[y][BlockType]); } // for BlockType f.Printf("\n"); } // for y - height (0 - 127) @@ -598,3 +600,41 @@ void cStatisticsFactory::SaveSpawners(void) + +void cStatisticsFactory::SavePerHeightSpawners(void) +{ + cFile f; + if (!f.Open("PerHeightSpawners.xls", cFile::fmWrite)) + { + LOG("Cannot write to file PerHeightSpawners.xls. Statistics not written."); + return; + } + + // Write header: + f.Printf("Height\tTotal"); + for (int i = 0; i < entMax; i++) + { + f.Printf("\t%s", GetEntityTypeString((eEntityType)i)); + } + f.Printf("\n"); + + // Write individual lines: + for (int y = 0; y < 256; y++) + { + UInt64 Total = 0; + for (int i = 0; i < entMax; i++) + { + Total += m_CombinedStats.m_PerHeightSpawners[y][i]; + } + f.Printf("%d\t%llu", y, Total); + for (int i = 0; i < entMax; i++) + { + f.Printf("\t%llu", m_CombinedStats.m_PerHeightSpawners[y][i]); + } + f.Printf("\n"); + } +} + + + + diff --git a/Tools/AnvilStats/Statistics.h b/Tools/AnvilStats/Statistics.h index f6165edd9..1b012e283 100644 --- a/Tools/AnvilStats/Statistics.h +++ b/Tools/AnvilStats/Statistics.h @@ -76,6 +76,8 @@ protected: virtual bool OnEmptySection(unsigned char a_Y) override; + virtual bool OnSectionsFinished(void) override { return false; } // continue processing + virtual bool OnEntity( const AString & a_EntityType, double a_PosX, double a_PosY, double a_PosZ, @@ -134,6 +136,7 @@ protected: void SaveBiomeBlockTypes(void); void SaveStatistics(void); void SaveSpawners(void); + void SavePerHeightSpawners(void); } ; -- cgit v1.2.3 From cde195c1569f22027306adf12e0e3e520d69b9f8 Mon Sep 17 00:00:00 2001 From: Mattes D Date: Wed, 27 Aug 2014 12:29:57 +0300 Subject: AnvilStats: Fixed thread start race condition. The whole program would sometimes fail to process anything because the threads were waited-for before they were started. --- Tools/AnvilStats/Processor.cpp | 24 ++++++++++++++++-------- Tools/AnvilStats/Processor.h | 10 ++++++++-- 2 files changed, 24 insertions(+), 10 deletions(-) diff --git a/Tools/AnvilStats/Processor.cpp b/Tools/AnvilStats/Processor.cpp index a16f78c18..6c4bb0ad5 100644 --- a/Tools/AnvilStats/Processor.cpp +++ b/Tools/AnvilStats/Processor.cpp @@ -28,6 +28,7 @@ cProcessor::cThread::cThread(cCallback & a_Callback, cProcessor & a_ParentProces m_Callback(a_Callback), m_ParentProcessor(a_ParentProcessor) { + LOG("Created a new thread: %p", this); super::Start(); } @@ -35,11 +36,20 @@ cProcessor::cThread::cThread(cCallback & a_Callback, cProcessor & a_ParentProces +void cProcessor::cThread::WaitForStart(void) +{ + m_HasStarted.Wait(); +} + + + + + void cProcessor::cThread::Execute(void) { - LOG("Started a new thread: %d", cIsThread::GetCurrentID()); + LOG("Started a new thread: %p, ID %d", this, cIsThread::GetCurrentID()); - m_ParentProcessor.m_ThreadsHaveStarted.Set(); + m_HasStarted.Set(); for (;;) { @@ -52,7 +62,7 @@ void cProcessor::cThread::Execute(void) ProcessFile(FileName); } // for-ever - LOG("Thread %d terminated", cIsThread::GetCurrentID()); + LOG("Thread %p (ID %d) terminated", this, cIsThread::GetCurrentID()); } @@ -522,20 +532,18 @@ void cProcessor::ProcessWorld(const AString & a_WorldFolder, cCallbackFactory & #endif // _DEBUG //*/ + // Start all the threads: for (int i = 0; i < NumThreads; i++) { cCallback * Callback = a_CallbackFactory.GetNewCallback(); m_Threads.push_back(new cThread(*Callback, *this)); } - // Wait for the first thread to start processing: - m_ThreadsHaveStarted.Wait(); - - // Wait for all threads to finish - // simply by calling each thread's destructor sequentially + // Wait for all threads to finish: LOG("Waiting for threads to finish"); for (cThreads::iterator itr = m_Threads.begin(), end = m_Threads.end(); itr != end; ++itr) { + (*itr)->WaitForStart(); delete *itr; } // for itr - m_Threads[] LOG("Processor finished"); diff --git a/Tools/AnvilStats/Processor.h b/Tools/AnvilStats/Processor.h index 72fea3081..db50ec619 100644 --- a/Tools/AnvilStats/Processor.h +++ b/Tools/AnvilStats/Processor.h @@ -30,6 +30,7 @@ class cProcessor cCallback & m_Callback; cProcessor & m_ParentProcessor; + cEvent m_HasStarted; // cIsThread override: virtual void Execute(void) override; @@ -48,6 +49,9 @@ class cProcessor public: cThread(cCallback & a_Callback, cProcessor & a_ParentProcessor); + + /** Waits until the thread starts processing the callback code. */ + void WaitForStart(void); } ; typedef std::vector cThreads; @@ -65,10 +69,12 @@ protected: AStringList m_FileQueue; cThreads m_Threads; - cEvent m_ThreadsHaveStarted; // This is signalled by each thread to notify the parent thread that it can start waiting for those threads - + + + /** Populates m_FileQueue with Anvil files from the specified folder. */ void PopulateFileQueue(const AString & a_WorldFolder); + /** Returns one filename from m_FileQueue, and removes the name from the queue. */ AString GetOneFileName(void); } ; -- cgit v1.2.3