From 1627a6ff18cc32a4fc9ce5f1debde9822434172f Mon Sep 17 00:00:00 2001
From: Patrick Daly <pdaly@codeaurora.org>
Date: Tue, 10 Feb 2015 19:26:31 -0800
Subject: [PATCH] ldrpv2: Fix next rtb entry algorithm

Consider the case of a nentries==8 and 3 cpus.
Numbers in parenthesis are the equivalent location in the circular buffer.
CPU:   Index0:  Index1: Index2: Index3:
0      0        3       6       9(1)
1      1        4       7       10(2)
2      2        5       8(0)

The current design is only appropriate for the case where
nentries % nrcpus == 0.

Fix this issue by incrementing the index by (nentries % nrcpus)
each time circular buffer wraps around.

CPU:   Index0:  Index1: Index2:
0      0        3       6+2==8(0)
1      1        4       7+2==9(1)
2      2        5       8+2==10(2)

Change-Id: I439fb540fc2c437c6b642d18aa9683603c270f36
---
 linux-ramdump-parser-v2/parsers/rtb.py | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/linux-ramdump-parser-v2/parsers/rtb.py b/linux-ramdump-parser-v2/parsers/rtb.py
index 9a0c99d..143d77c 100755
--- a/linux-ramdump-parser-v2/parsers/rtb.py
+++ b/linux-ramdump-parser-v2/parsers/rtb.py
@@ -136,6 +136,13 @@ class RTB(RamParser):
         rtbout.write('[{0}] : {1} interrupt {2:x} handled from addr {3:x} {4} {5}\n'.format(
             timestamp, logtype, data, caller, func, line).encode('ascii', 'ignore'))
 
+    def next_rtb_entry(self, index, step_size, mask):
+        unused_buffer_size = (mask + 1) % step_size
+        #check for wraparound
+        if ((index + step_size + unused_buffer_size) & mask) < (index & mask):
+            return (index + step_size + unused_buffer_size) & mask
+        return (index + step_size) & mask
+
     def parse(self):
         rtb = self.ramdump.addr_lookup('msm_rtb')
         if rtb is None:
@@ -175,7 +182,7 @@ class RTB(RamParser):
             next_ptr = 0
             next_entry = 0
             while True:
-                next_entry = (last + step_size) & mask
+                next_entry = self.next_rtb_entry(last, step_size, mask)
                 last_ptr = rtb_read_ptr + last * rtb_entry_size + idx_offset
                 next_ptr = rtb_read_ptr + next_entry * \
                     rtb_entry_size + idx_offset
@@ -210,7 +217,7 @@ class RTB(RamParser):
                         getattr(RTB, func)(self, rtb_out, ptr, name_str)
                 if next_entry == last:
                     stop = 1
-                next_entry = (next_entry + step_size) & mask
+                next_entry = self.next_rtb_entry(next_entry, step_size, mask)
                 if (stop == 1):
                     break
             print_out_str('Wrote RTB to msm_rtb{0}.txt'.format(i))
-- 
GitLab