From 81b0718a8fca1ea3fa619ab75bb4cc9275026a4f Mon Sep 17 00:00:00 2001 From: Timothee Cour Date: Mon, 24 Feb 2020 01:24:47 -0800 Subject: make CI tests faster + more precise --- tests/gc/gcleak2.nim | 77 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 72 insertions(+), 5 deletions(-) diff --git a/tests/gc/gcleak2.nim b/tests/gc/gcleak2.nim index fe1718aef..eb71fe55f 100644 --- a/tests/gc/gcleak2.nim +++ b/tests/gc/gcleak2.nim @@ -2,6 +2,11 @@ discard """ outputsub: "no leak: " """ +## this test makes sure getOccupiedMem() is constant after a few iterations +## for --gc:bohem, --gc:markAndSweep, --gc:arc, --gc:orc. +## and for other gc, it consists of cycles with constant min/max after a few cycles. +## This ensures we have no leaks. + when defined(GC_setMaxPause): GC_setMaxPause 2_000 @@ -14,13 +19,75 @@ proc makeObj(): TTestObj = result.x = "Hello" result.s = @[1,2,3] +const collectAlways = defined(gcMarkAndSweep) or defined(boehmgc) +const isDeterministic = collectAlways or defined(gcArc) or defined(gcOrc) + ## when isDeterministic, we expect memory to reach a fixed point + ## after `numIterStable` iterations + +var memMax = 0 # peak + +when isDeterministic: + const numIterStable = 3 + # stabilize after this many iterations +else: + const numCycleStable = when defined(useRealtimeGC): 6 else: 4 + # stabilize after this many cycles; empirically determined + # after running all combinations of gc's with / without -d:release + var memPrevious = 0 + var memMin = int.high # right after a peak + var numCollections = 0 + +let numIter = when isDeterministic: 1_000 else: 1_000_000 + ## full collection is expensive, and the memory doesn't change after + ## each iteration so there's no point in a large `numIter` (this was taking + ## 350s for `nim c -r -d:release --gc:boehm` + `nim c -r --gc:boehm`, 50% + ## of running time of `testament/testament all`. + proc inProc() = - for i in 1 .. 1_000_000: - when defined(gcMarkAndSweep) or defined(boehmgc): - GC_fullcollect() + for i in 1 .. numIter: + when collectAlways: GC_fullcollect() var obj: TTestObj obj = makeObj() - if getOccupiedMem() > 300_000: quit("still a leak!") + let mem = getOccupiedMem() + when isDeterministic: + if i <= numIterStable: + memMax = mem + doAssert memMax <= 50_000 # adjust as needed + else: + # memory shouldn't increase after 1st few iterations + # on linux 386 it somehow takes 3 iters to converge + doAssert mem <= memMax + else: + if mem < memPrevious: + # a collection happened, it peaked at memPrevious + # echo (mem, memMin, memMax, numCollections, numIter, i) # for debugging + numCollections.inc + if numCollections <= numCycleStable: + # this is the 1st few collections, we update the min/max + doAssert memPrevious < 300_000 # adjust as needed + if memMin < mem: # `<` intentional; the valley may increase + memMin = mem + if memMax < memPrevious: + memMax = memPrevious + else: + # after a collection, we always go back to same level + doAssert mem <= memMin, $(mem, memMin) + + if numCollections >= numCycleStable: + # after a few cycles, the max stabilizes + doAssert mem <= memMax, $(mem, memMax) + + memPrevious = mem inProc() -echo "no leak: ", getOccupiedMem() +let mem = getOccupiedMem() +var msg = "no leak: " +when isDeterministic: + msg.add $(mem, memMax) + echo msg +else: + msg.add $(mem, memMin, memMax, numCollections, numIter) + echo msg + # make sure some collections did happen, otherwise the previous tests + # are meaningless + doAssert numCollections > 1000 # 3999 on local OSX; leaving some slack -- cgit 1.4.1-2-gfad0 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
# Copyright (c) 2009, 2010 hut <hut@lavabit.com>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.

"""The BrowserView manages a set of BrowserColumns."""
from . import Widget
from .browsercolumn import BrowserColumn
from .pager import Pager
from ..displayable import DisplayableContainer

class BrowserView(Widget, DisplayableContainer):
	ratios = None
	preview = True
	preview_available = True
	stretch_ratios = None
	need_clear = False

	def __init__(self, win, ratios, preview = True):
		DisplayableContainer.__init__(self, win)
		self.ratios = ratios
		self.preview = preview

		# normalize ratios:
		ratio_sum = float(sum(ratios))
		self.ratios = tuple(map(lambda x: x / ratio_sum, ratios))

		if len(self.ratios) >= 2:
			self.stretch_ratios = self.ratios[:-2] + \
					((self.ratios[-2] + self.ratios[-1] * 0.9),
					(self.ratios[-1] * 0.1))
		
		offset = 1 - len(ratios)
		if preview: offset += 1

		for level in range(len(ratios)):
			fl = BrowserColumn(self.win, level + offset)
			self.add_child(fl)

		try:
			self.main_column = self.container[preview and -2 or -1]
		except IndexError:
			self.main_column = None
		else:
			self.main_column.display_infostring = True
			self.main_column.main_column = True

		self.pager = Pager(self.win, embedded=True)
		self.pager.visible = False
		self.add_child(self.pager)

	def draw(self):
		if str(self.env.keybuffer) in ("`", "'"):
			self._draw_bookmarks()
		else:
			if self.need_clear:
				self.win.erase()
				self.need_redraw = True
				self.need_clear = False
			DisplayableContainer.draw(self)

	def finalize(self):
		if self.pager.visible:
			try:
				self.fm.ui.win.move(self.main_column.y, self.main_column.x)
			except:
				pass
		else:
			x = self.main_column.x
			y = self.main_column.y + self.main_column.target.pointer\
					- self.main_column.scroll_begin
			try:
				self.fm.ui.win.move(y, x)
			except:
				pass
	
	def _draw_bookmarks(self):
		self.need_clear = True

		sorted_bookmarks = sorted(item for item in self.fm.bookmarks \
				if '/.' not in item[1].path)

		def generator():
			return zip(range(self.hei), sorted_bookmarks)

		try:
			maxlen = max(len(item[1].path) for i, item in generator())
		except ValueError:
			return
		maxlen = min(maxlen + 5, self.wid)

		for line, items in generator():
			key, mark = items
			string = " " + key + ": " + mark.path
			self.addnstr(line, 0, string.ljust(maxlen), self.wid)
	
	def resize(self, y, x, hei, wid):
		"""Resize all the columns according to the given ratio"""
		DisplayableContainer.resize(self, y, x, hei, wid)
		left = 0

		cut_off_last = self.preview and not self.preview_available \
				and self.stretch_ratios

		if cut_off_last:
			generator = zip(self.stretch_ratios, range(len(self.ratios)))
		else:
			generator = zip(self.ratios, range(len(self.ratios)))

		last_i = len(self.ratios) - 1

		for ratio, i in generator:
			wid = int(ratio * self.wid)

			if i == last_i:
				wid = int(self.wid - left + 1)

			if i == last_i - 1:
				self.pager.resize(0, left, hei, max(1, self.wid - left))

			try:
				self.container[i].resize(0, left, hei, max(1, wid-1))
			except KeyError:
				pass

			left += wid
	
	def click(self, event):
		n = event.ctrl() and 1 or 3
		if event.pressed(4):
			self.main_column.scroll(relative = -n)
		elif event.pressed(2) or event.key_invalid():
			self.main_column.scroll(relative = n)
		else:
			DisplayableContainer.click(self, event)
	
	def open_pager(self):
		self.pager.visible = True
		self.pager.focused = True
		self.pager.open()
		try:
			self.container[-2].visible = False
			self.container[-3].visible = False
		except IndexError:
			pass
	
	def close_pager(self):
		self.pager.visible = False
		self.pager.focused = False
		self.pager.close()
		try:
			self.container[-2].visible = True
			self.container[-3].visible = True
		except IndexError:
			pass
	
	def poke(self):
		DisplayableContainer.poke(self)
		if self.settings.collapse_preview and self.preview:
			has_preview = self.container[-2].has_preview()
			if self.preview_available != has_preview:
				self.preview_available = has_preview
				self.resize(self.y, self.x, self.hei, self.wid)