tracker
All repositories: gitoria
6.3 KB
#!/usr/bin/env python3"""Proc ABI ownership regression. Run after native/build.sh; Python stdlib only.The counting heap sees plugin allocations, independent of the host GC. consume()matches runtime/pluginval.hl: copy/consume children, then invoke owner cleanup.RSS is measured after warmup, without malloc_trim. Every batch must leave exactlythe same live allocations and fds and no child (running or zombie)."""import argparseimport ctypes as Cimport jsonimport osfrom pathlib import Pathimport selectimport timeclass String(C.Structure):_fields_ = [('ptr', C.c_void_p), ('len', C.c_size_t)]class Data(C.Union):_fields_ = [('number', C.c_double), ('string', String), ('object', C.c_void_p)]class Value(C.Structure):_fields_ = [('type', C.c_uint8), ('data', Data), ('deinit', C.c_void_p)]class Field(C.Structure):_fields_ = [('key', String), ('value', Value)]class Object(C.Structure):_fields_ = [('fields', C.POINTER(Field)), ('count', C.c_size_t), ('deinit', C.c_void_p)]class Iterator(C.Structure):_fields_ = [('context', C.c_void_p), ('next', C.c_void_p), ('deinit', C.c_void_p),('try_next', C.c_void_p), ('wake_fd', C.c_int),('head_stamp', C.c_void_p), ('close', C.c_void_p)]Alloc = C.CFUNCTYPE(C.c_size_t, C.c_size_t, C.c_size_t)Free = C.CFUNCTYPE(C.c_uint64, C.c_size_t)Usable = C.CFUNCTYPE(C.c_size_t, C.c_size_t)class Heap(C.Structure):_fields_ = [('alloc', Alloc), ('free', Free), ('usable', Usable)]libc = C.CDLL(None)libc.malloc.argtypes = [C.c_size_t]libc.malloc.restype = C.c_void_plibc.free.argtypes = [C.c_void_p]libc.malloc_usable_size.argtypes = [C.c_void_p]libc.malloc_usable_size.restype = C.c_size_tlive = {}@Allocdef alloc(alignment, size):assert alignment <= 16ptr = libc.malloc(size)live[ptr] = sizereturn ptr@Freedef free(ptr):live.pop(ptr)libc.free(ptr)return 0@Usabledef usable(ptr):return libc.malloc_usable_size(ptr)heap = Heap(alloc, free, usable)def text(raw):return Value(3, Data(string=String(C.cast(C.c_char_p(raw), C.c_void_p), len(raw))))def consume(value):if value.type == 3 or value.type == 7:result = C.string_at(value.data.string.ptr, value.data.string.len)elif value.type == 2:result = value.data.numberelif value.type == 5:# Transfer the iterator to the caller, as the runtime wrapper does.result = C.cast(value.data.object, C.POINTER(Iterator))elif value.type == 4:obj = C.cast(value.data.object, C.POINTER(Object))result = {}for i in range(obj.contents.count):field = obj.contents.fields[i]key = C.string_at(field.key.ptr, field.key.len).decode()result[key] = consume(field.value)if obj.contents.deinit:C.CFUNCTYPE(None, C.POINTER(Object))(obj.contents.deinit)(obj)else:result = Noneif value.deinit:C.CFUNCTYPE(None, C.POINTER(Value))(value.deinit)(C.byref(value))return resultdef rss():return int(Path('/proc/self/status').read_text().split('VmRSS:')[1].split()[0])def state():return len(live), sum(live.values()), len(os.listdir('/proc/self/fd'))def one(plugin, command, asynchronous, expected):arg = text(command)value = (plugin.hl_proc_spawn if asynchronous else plugin.hl_proc_run)(1, C.byref(arg))result = consume(value)if not asynchronous:assert result['exit'] == expectedassert len(result['out']) == (65536 if command.startswith(b'head') else 5 if command.startswith(b'printf') else 0)assert result['err'] == (b'bad' if expected == 7 else b'')returniterator = result['events']it = iterator.contentspoll = select.poll()poll.register(it.wake_fd, select.POLLIN)next_event = C.CFUNCTYPE(Value, C.c_void_p)(it.try_next)deadline = time.monotonic() + 10while True:assert time.monotonic() < deadline, 'child completion timeout'assert poll.poll(1000), 'child event timeout'event = next_event(it.context)data = consume(event)if data and 'exit' in data:assert data['exit'] == expectedbreakC.CFUNCTYPE(None, C.c_void_p)(it.deinit)(it.context)# Iterator storage belongs to the plugin, like runtime/plugwrap.hl's wrapper.def main():parser = argparse.ArgumentParser(description=__doc__)parser.add_argument('plugin', nargs='?', default=str(Path(__file__).resolve().parents[1] / 'libproc.so'))parser.add_argument('--calls', type=int, default=5000)parser.add_argument('--allow-leaks', action='store_true', help='collect baseline evidence without assertions')args = parser.parse_args()plugin = C.CDLL(str(Path(args.plugin).resolve()))plugin.hl_plugin_set_heap(C.byref(heap))for name in ['hl_proc_run', 'hl_proc_spawn']:fn = getattr(plugin, name)fn.argtypes = [C.c_uint32, C.POINTER(Value)]fn.restype = Valuefor asynchronous in [False, True]:for command, expected in [(b'true', 0), (b'printf hello', 0),(b'head -c 65536 /dev/zero', 0),(b"sh -c 'printf bad >&2; exit 7'", 7)]:for _ in range(100):one(plugin, command, asynchronous, expected)# The worker posts exit just before dropping its reference. Allow that# final release to finish before sampling, rather than racing its thread.time.sleep(.05)before, start_rss = state(), rss()for _ in range(args.calls):one(plugin, command, asynchronous, expected)time.sleep(.05)after, end_rss = state(), rss()children = Path(f'/proc/self/task/{os.getpid()}/children').read_text().strip()row = dict(form='spawn' if asynchronous else 'run', command=command.decode(),calls=args.calls, blocks=after[0]-before[0], bytes=after[1]-before[1],fd_before=before[2], fd_after=after[2], children=children,rss_growth_kib=end_rss-start_rss)print(json.dumps(row), flush=True)if not args.allow_leaks:assert after == before, rowassert not children, rowassert end_rss-start_rss <= 1024, rowif __name__ == '__main__':main()
Branches
Latest commits
- 642b4fa8|||PR records: DP75 day62 live, AP62 briefTracker
- bfc4ea4dMerge branch 'ap62' into mainTracker
- 2c95ac46|||PR AP62: re-vendor Hybriel set day62 (source 70341ad2, bin a54f5f08)Tracker
- b9c7c3c8Merge branch 'records-dp74' into mainTracker
- 85cfb4ae|||PR records: AP61 day61, DP74 liveTracker
- b1496147Merge branch 'ap61' into mainTracker
- 2047955bMerge branch 'u12-gateports' into mainTracker
- c8da40f3|||PR AP61: re-vendor Hybriel set day61 (source a05af47c, bin a11042e5)Tracker
- 4e018678|||PR deploy.sh: gates use Tracker's ports 8716-8719 and LC_ALL=C; README: 2 backups keptTracker
- 0b86324aMerge branch 'records-dp73' into mainTracker
- 9ce5f59b|||PR records: DP73 live 613ae0e; deploy.sh keeps 2 backupsTracker
- 613ae0ecMerge branch 'bs3' into mainTracker
- 1383264b|||PR BS3: keep shared synchronous sorting fastTracker
- 63e5ba1a|||PR BS3: guard boot imports and share resumable sort buffersTracker
- 310613f1Merge branch 'records-dp72' into mainTracker
- 6f7b61ba|||PR records: deploy-swap fix live, Kunibert findingsTracker
- c41587d2Merge branch 'bs2' into mainTracker
- 0dd538a6|||PR BS2: finish the boot index in slices so a booting instance keeps answeringTracker
- 019d5cedMerge branch 'records-1110' into mainTracker
- f88ae8b9|||PR records: day56 toggle live, reviewTracker