浏览代码

Merge pull request #11411 from ghalliday/issue20080

HPCC-20080 Rearrange some sort code and add debugging helper function

Reviewed-By: Richard Chapman <rchapman@hpccsystems.com>
Richard Chapman 7 年之前
父节点
当前提交
5fcb9f4b6b
共有 3 个文件被更改,包括 25 次插入13 次删除
  1. 14 12
      common/thorhelper/thorsort.cpp
  2. 10 1
      roxie/roxiemem/roxiemem.cpp
  3. 1 0
      roxie/roxiemem/roxiemem.hpp

+ 14 - 12
common/thorhelper/thorsort.cpp

@@ -18,6 +18,7 @@
 #include "platform.h"
 #include "thorsort.hpp"
 #include "jset.hpp"
+#include "jlog.hpp"
 #include "errorlist.h"
 #include <exception>
 
@@ -187,7 +188,7 @@ inline void clonePartition(void * * result, size_t n, void * * src)
 {
     void * * tgt = result;
     while (n--)
-       *tgt++ = *src++;
+        *tgt++ = *src++;
 }
 
 inline void * * mergePartitionsRev(const ICompare & compare, void * * result, size_t n1, void * * ret1, size_t n2, void * * ret2, size_t n)
@@ -197,27 +198,27 @@ inline void * * mergePartitionsRev(const ICompare & compare, void * * result, si
     ret2 += (n2-1);
     while (n--)
     {
-       if (compare.docompare(*ret1, *ret2) >= 0)
+       if (compare.docompare(*ret1, *ret2) <= 0)
        {
-           *tgt-- = *ret1--;
-           if (--n1 == 0)
+           *tgt-- = *ret2--;
+           if (--n2 == 0)
            {
+               //There must be at least one row in the left partition - copy any that remain
                while (n--)
                {
-                   *tgt-- = *ret2--;
+                   *tgt-- = *ret1--;
                }
                return result;
            }
        }
        else
        {
-           *tgt-- = *ret2--;
-           if (--n2 == 0)
+           *tgt-- = *ret1--;
+           if (--n1 == 0)
            {
-               //There must be at least one row in the left partition - copy any that remain
                while (n--)
                {
-                   *tgt-- = *ret1--;
+                   *tgt-- = *ret2--;
                }
                return result;
            }
@@ -473,7 +474,7 @@ class TbbParallelMergeSorter
                 clonePartition(result + delta, n, src1 + delta);
             }
             else
-                mergePartitionsRev(compare, result, n2, src2, n1, src1, n);
+                mergePartitionsRev(compare, result, n1, src1, n2, src2, n);
             return NULL;
         }
     };
@@ -549,7 +550,8 @@ class TbbParallelMergeSorter
                     {
                         if (c <= 0)
                         {
-                            //value in left is smallest.  Find the position of the value <= the left value
+                            //value in left is smallest.  Find the position of the first value >= the left value
+                            //do not include it since there may be more left matches to merge first
                             posLeft[part] = leftPos;
                             size_t matchRight = findFirstGE(src1[leftPos], prevRight, rightPos, src2);
                             posRight[part] = matchRight;
@@ -586,7 +588,7 @@ class TbbParallelMergeSorter
                 size_t start = posLeft[i] + posRight[i];
                 size_t end = posLeft[i+1] + posRight[i+1];
                 size_t num = end - start;
-                size_t numFwd = num/2;
+                size_t numFwd = (num+1)/2;
 #ifdef TRACE_PARTITION
                 DBGLOG("  ([%d..%d],[%d..%d] %d,%d = %d)",
                         (unsigned)posLeft[i], (unsigned)posLeft[i+1], (unsigned)posRight[i], (unsigned)posRight[i+1],

+ 10 - 1
roxie/roxiemem/roxiemem.cpp

@@ -1078,6 +1078,15 @@ static inline bool isValidRoxiePtr(const void *_ptr)
     return ptr >= heapBase && ptr < heapEnd;
 }
 
+size_t getRelativeRoxiePtr(const void *_ptr)
+{
+    const char *ptr = (const char *) _ptr;
+    if (ptr >= heapBase && ptr < heapEnd)
+        return ptr - heapBase;
+    return -1;
+}
+
+
 inline static HeapletBase *findBase(const void *ptr)
 {
     return (HeapletBase *) ((memsize_t) ptr & HEAP_ALIGNMENT_MASK);
@@ -1968,7 +1977,7 @@ private:
 #endif
         if (unlikely((header->allocatorId & ~ACTIVITY_MASK) != ACTIVITY_MAGIC))
         {
-            DBGLOG("%s: Invalid pointer %p id(%x) cnt(%x)", reason, ptr, header->allocatorId, header->count.load());
+            DBGLOG("%s: Invalid pointer %p(%zu) id(%x) cnt(%x)", reason, ptr, getRelativeRoxiePtr(ptr), header->allocatorId, header->count.load());
             PrintStackReport();
             PrintMemoryReport();
             HEAPERROR("Invalid pointer");

+ 1 - 0
roxie/roxiemem/roxiemem.hpp

@@ -541,6 +541,7 @@ extern roxiemem_decl void setMemTraceSizeLimit(memsize_t value);
 extern roxiemem_decl StringBuffer &memstats(StringBuffer &stats);
 extern roxiemem_decl void memstats(unsigned &totalpg, unsigned &freepg, unsigned &maxblk);
 extern roxiemem_decl IPerfMonHook *createRoxieMemStatsPerfMonHook(IPerfMonHook *chain=NULL); // for passing to jdebug startPerformanceMonitor
+extern roxiemem_decl size_t getRelativeRoxiePtr(const void *_ptr); // Useful for debugging - to provide a value that is consistent from run to run
 
 } // namespace roxiemem
 #endif