From 8ee174659f55590430590148825cb43ac0ef2c2a Mon Sep 17 00:00:00 2001 From: Ben Longbons Date: Sat, 4 Oct 2014 21:54:47 -0700 Subject: Add pretty-printers for time --- src/net/timer.py | 117 ++++++++++++++++++++++++++++++++++++++++++++++++++ tools/debug-debug.gdb | 1 + 2 files changed, 118 insertions(+) create mode 100644 src/net/timer.py diff --git a/src/net/timer.py b/src/net/timer.py new file mode 100644 index 0000000..2ccb3bb --- /dev/null +++ b/src/net/timer.py @@ -0,0 +1,117 @@ +class duration(object): + __slots__ = ('_whole', '_frac', '_units') + name = 'std::chrono::duration' + enabled = True + + def __init__(self, value): + from decimal import Decimal + + rep = int(value['__r']) + ratio = value.type.template_argument(1) + num = int(ratio.template_argument(0)) + den = int(ratio.template_argument(1)) + # this will fail on duration + value = Decimal(rep) * num / den + whole = int(value) + self._whole = whole + self._frac = value - whole + units = { + (1, 1000*1000*1000): ('nanoseconds', '_ns'), + (1, 1000*1000): ('microseconds', '_us'), + (1, 1000): ('milliseconds', '_ms'), + (1, 1): ('seconds', '_s'), + (60, 1): ('minutes', '_min'), + (60*60, 1): ('hours', '_h'), + (24*60*60, 1): ('duration>', '_d'), + # days don't exist (probably because of leap seconds) + } + self._units = units.get((num, den)) or ('duration>' % (num, den), '_?') + + def to_string(self): + whole = self._whole + frac = self._frac + cu, su = self._units + if not whole and not frac: + return '0%s' % su + s = whole + min = s // 60 + s %= 60 + h = min // 60 + min %= 60 + d = h // 24 + h %= 24 + msx = frac * 1000 + ms = int(msx) + usx = (msx - ms) * 1000 + us = int(usx) + nsx = (usx - us) * 1000 + ns = int(nsx) + bits = [ + '%d_d' % d if d else None, + '%d_h' % h if h else None, + '%d_min' % min if min else None, + '%d_s' % s if s else None, + '%d_ms' % ms if ms else None, + '%d_us' % us if us else None, + '%d_ns' % ns if ns else None, + ] + body = ' + '.join(b for b in bits if b is not None) + if not body.endswith(su): + body = '%s(%s)' % (cu, body) + elif '+' in body: + body = '(%s)' % body + return body + + tests = [ + ('std::chrono::nanoseconds(0)', '0_ns'), + ('std::chrono::microseconds(0)', '0_us'), + ('std::chrono::milliseconds(0)', '0_ms'), + ('std::chrono::seconds(0)', '0_s'), + ('std::chrono::minutes(0)', '0_min'), + ('std::chrono::hours(0)', '0_h'), + ('std::chrono::duration>(0)', '0_d'), + + ('std::chrono::nanoseconds(1)', '1_ns'), + ('std::chrono::microseconds(1)', '1_us'), + ('std::chrono::milliseconds(1)', '1_ms'), + ('std::chrono::seconds(1)', '1_s'), + ('std::chrono::minutes(1)', '1_min'), + ('std::chrono::hours(1)', '1_h'), + ('std::chrono::duration>(1)', '1_d'), + + ('std::chrono::nanoseconds(1)', '1_ns'), + ('std::chrono::microseconds(1)', '1_us'), + ('std::chrono::milliseconds(1)', '1_ms'), + ('std::chrono::seconds(1)', '1_s'), + ('std::chrono::minutes(1)', '1_min'), + ('std::chrono::hours(1)', '1_h'), + ('std::chrono::duration>(1)', '1_d'), + + ('std::chrono::nanoseconds(3)', '3_ns'), + ('std::chrono::microseconds(3)', '3_us'), + ('std::chrono::milliseconds(3)', '3_ms'), + ('std::chrono::seconds(3)', '3_s'), + ('std::chrono::minutes(3)', '3_min'), + ('std::chrono::hours(3)', '3_h'), + ('std::chrono::duration>(3)', '3_d'), + + ('std::chrono::nanoseconds(1000)', 'nanoseconds(1_us)'), + ('std::chrono::microseconds(1000)', 'microseconds(1_ms)'), + ('std::chrono::milliseconds(1000)', 'milliseconds(1_s)'), + ('std::chrono::seconds(60)', 'seconds(1_min)'), + ('std::chrono::minutes(60)', 'minutes(1_h)'), + ('std::chrono::hours(24)', 'hours(1_d)'), + + ('std::chrono::nanoseconds(1001)', '(1_us + 1_ns)'), + ('std::chrono::microseconds(1001)', '(1_ms + 1_us)'), + ('std::chrono::milliseconds(1001)', '(1_s + 1_ms)'), + ('std::chrono::seconds(61)', '(1_min + 1_s)'), + ('std::chrono::minutes(61)', '(1_h + 1_min)'), + ('std::chrono::hours(25)', '(1_d + 1_h)'), + + ('std::chrono::nanoseconds(1001*1000)', 'nanoseconds(1_ms + 1_us)'), + ('std::chrono::microseconds(1001*1000)', 'microseconds(1_s + 1_ms)'), + ('std::chrono::milliseconds(61*1000)', 'milliseconds(1_min + 1_s)'), + ('std::chrono::seconds(61*60)', 'seconds(1_h + 1_min)'), + ('std::chrono::minutes(25*60)', 'minutes(1_d + 1_h)'), + ] diff --git a/tools/debug-debug.gdb b/tools/debug-debug.gdb index 72a45e1..e1fa19e 100644 --- a/tools/debug-debug.gdb +++ b/tools/debug-debug.gdb @@ -39,6 +39,7 @@ end set print static-members off set print elements 9999 set print frame-arguments none +set python print-stack full set logging on rbreak do_breakpoint -- cgit v1.2.3-60-g2f50