gitoria

tracker

All repositories: gitoria

ReadmeCodePull requestsReleasesTicketsSettings
Commitd714ae42d714ae42Log/status: AP56 closed out, FF1mred714ae42/plugins/proc/tests/memory.py

6.3 KB

  1. #!/usr/bin/env python3
  2. """Proc ABI ownership regression. Run after native/build.sh; Python stdlib only.
  3. The counting heap sees plugin allocations, independent of the host GC. consume()
  4. matches runtime/pluginval.hl: copy/consume children, then invoke owner cleanup.
  5. RSS is measured after warmup, without malloc_trim. Every batch must leave exactly
  6. the same live allocations and fds and no child (running or zombie).
  7. """
  8. import argparse
  9. import ctypes as C
  10. import json
  11. import os
  12. from pathlib import Path
  13. import select
  14. import time
  15. class String(C.Structure):
  16. _fields_ = [('ptr', C.c_void_p), ('len', C.c_size_t)]
  17. class Data(C.Union):
  18. _fields_ = [('number', C.c_double), ('string', String), ('object', C.c_void_p)]
  19. class Value(C.Structure):
  20. _fields_ = [('type', C.c_uint8), ('data', Data), ('deinit', C.c_void_p)]
  21. class Field(C.Structure):
  22. _fields_ = [('key', String), ('value', Value)]
  23. class Object(C.Structure):
  24. _fields_ = [('fields', C.POINTER(Field)), ('count', C.c_size_t), ('deinit', C.c_void_p)]
  25. class Iterator(C.Structure):
  26. _fields_ = [('context', C.c_void_p), ('next', C.c_void_p), ('deinit', C.c_void_p),
  27. ('try_next', C.c_void_p), ('wake_fd', C.c_int),
  28. ('head_stamp', C.c_void_p), ('close', C.c_void_p)]
  29. Alloc = C.CFUNCTYPE(C.c_size_t, C.c_size_t, C.c_size_t)
  30. Free = C.CFUNCTYPE(C.c_uint64, C.c_size_t)
  31. Usable = C.CFUNCTYPE(C.c_size_t, C.c_size_t)
  32. class Heap(C.Structure):
  33. _fields_ = [('alloc', Alloc), ('free', Free), ('usable', Usable)]
  34. libc = C.CDLL(None)
  35. libc.malloc.argtypes = [C.c_size_t]
  36. libc.malloc.restype = C.c_void_p
  37. libc.free.argtypes = [C.c_void_p]
  38. libc.malloc_usable_size.argtypes = [C.c_void_p]
  39. libc.malloc_usable_size.restype = C.c_size_t
  40. live = {}
  41. @Alloc
  42. def alloc(alignment, size):
  43. assert alignment <= 16
  44. ptr = libc.malloc(size)
  45. live[ptr] = size
  46. return ptr
  47. @Free
  48. def free(ptr):
  49. live.pop(ptr)
  50. libc.free(ptr)
  51. return 0
  52. @Usable
  53. def usable(ptr):
  54. return libc.malloc_usable_size(ptr)
  55. heap = Heap(alloc, free, usable)
  56. def text(raw):
  57. return Value(3, Data(string=String(C.cast(C.c_char_p(raw), C.c_void_p), len(raw))))
  58. def consume(value):
  59. if value.type == 3 or value.type == 7:
  60. result = C.string_at(value.data.string.ptr, value.data.string.len)
  61. elif value.type == 2:
  62. result = value.data.number
  63. elif value.type == 5:
  64. # Transfer the iterator to the caller, as the runtime wrapper does.
  65. result = C.cast(value.data.object, C.POINTER(Iterator))
  66. elif value.type == 4:
  67. obj = C.cast(value.data.object, C.POINTER(Object))
  68. result = {}
  69. for i in range(obj.contents.count):
  70. field = obj.contents.fields[i]
  71. key = C.string_at(field.key.ptr, field.key.len).decode()
  72. result[key] = consume(field.value)
  73. if obj.contents.deinit:
  74. C.CFUNCTYPE(None, C.POINTER(Object))(obj.contents.deinit)(obj)
  75. else:
  76. result = None
  77. if value.deinit:
  78. C.CFUNCTYPE(None, C.POINTER(Value))(value.deinit)(C.byref(value))
  79. return result
  80. def rss():
  81. return int(Path('/proc/self/status').read_text().split('VmRSS:')[1].split()[0])
  82. def state():
  83. return len(live), sum(live.values()), len(os.listdir('/proc/self/fd'))
  84. def one(plugin, command, asynchronous, expected):
  85. arg = text(command)
  86. value = (plugin.hl_proc_spawn if asynchronous else plugin.hl_proc_run)(1, C.byref(arg))
  87. result = consume(value)
  88. if not asynchronous:
  89. assert result['exit'] == expected
  90. assert len(result['out']) == (65536 if command.startswith(b'head') else 5 if command.startswith(b'printf') else 0)
  91. assert result['err'] == (b'bad' if expected == 7 else b'')
  92. return
  93. iterator = result['events']
  94. it = iterator.contents
  95. poll = select.poll()
  96. poll.register(it.wake_fd, select.POLLIN)
  97. next_event = C.CFUNCTYPE(Value, C.c_void_p)(it.try_next)
  98. deadline = time.monotonic() + 10
  99. while True:
  100. assert time.monotonic() < deadline, 'child completion timeout'
  101. assert poll.poll(1000), 'child event timeout'
  102. event = next_event(it.context)
  103. data = consume(event)
  104. if data and 'exit' in data:
  105. assert data['exit'] == expected
  106. break
  107. C.CFUNCTYPE(None, C.c_void_p)(it.deinit)(it.context)
  108. # Iterator storage belongs to the plugin, like runtime/plugwrap.hl's wrapper.
  109. def main():
  110. parser = argparse.ArgumentParser(description=__doc__)
  111. parser.add_argument('plugin', nargs='?', default=str(Path(__file__).resolve().parents[1] / 'libproc.so'))
  112. parser.add_argument('--calls', type=int, default=5000)
  113. parser.add_argument('--allow-leaks', action='store_true', help='collect baseline evidence without assertions')
  114. args = parser.parse_args()
  115. plugin = C.CDLL(str(Path(args.plugin).resolve()))
  116. plugin.hl_plugin_set_heap(C.byref(heap))
  117. for name in ['hl_proc_run', 'hl_proc_spawn']:
  118. fn = getattr(plugin, name)
  119. fn.argtypes = [C.c_uint32, C.POINTER(Value)]
  120. fn.restype = Value
  121. for asynchronous in [False, True]:
  122. for command, expected in [(b'true', 0), (b'printf hello', 0),
  123. (b'head -c 65536 /dev/zero', 0),
  124. (b"sh -c 'printf bad >&2; exit 7'", 7)]:
  125. for _ in range(100):
  126. one(plugin, command, asynchronous, expected)
  127. # The worker posts exit just before dropping its reference. Allow that
  128. # final release to finish before sampling, rather than racing its thread.
  129. time.sleep(.05)
  130. before, start_rss = state(), rss()
  131. for _ in range(args.calls):
  132. one(plugin, command, asynchronous, expected)
  133. time.sleep(.05)
  134. after, end_rss = state(), rss()
  135. children = Path(f'/proc/self/task/{os.getpid()}/children').read_text().strip()
  136. row = dict(form='spawn' if asynchronous else 'run', command=command.decode(),
  137. calls=args.calls, blocks=after[0]-before[0], bytes=after[1]-before[1],
  138. fd_before=before[2], fd_after=after[2], children=children,
  139. rss_growth_kib=end_rss-start_rss)
  140. print(json.dumps(row), flush=True)
  141. if not args.allow_leaks:
  142. assert after == before, row
  143. assert not children, row
  144. assert end_rss-start_rss <= 1024, row
  145. if __name__ == '__main__':
  146. main()

Branches

Latest commits

  • d714ae42Log/status: AP56 closed out, FF1mre
  • a429d088Mission FF1mre
  • 03bbfba3Log: AP56c causemre
  • 24845fe3Log: AP56b result, AP56cmre
  • 6a6d71eaLog: gitoria membershipmre
  • 8b842dc8Log + CONDUCTOR: gitoria PR switch-onmre
  • 6bb7c20estatus: window 2026-11-09mre
  • 5db84974Log: Apollo window GO, AP56b started, BS1 copy removedmre
  • e84586a7Mission AP56bmre
  • 15917f03Record PR workflow rule (gitoria#50)mre
  • 942ac995status: paused workers statemre
  • 44b934e5log: respawn cleanupmre
  • 03a80097Log: Anton GC A/Bmre
  • c7cdef62Log: Apollo STOP, workers pausedmre
  • d0c083fbLog: usage stopmre
  • c6d464e5Log: Apollo GO backlog, AP56 and BS1 startedmre
  • 1eb6f1a1Missions AP56, BS1mre
  • 4871f8adLog: AP52 live on day52, browser gate greenmre
  • d9bf2988Merge ap52: day52, tracker#58 server argument, #51 modal close rule, #56 keyed season rowsmre
  • c4a60ef2Log: RT52 retest passed, AP52 startedmre