Skip to content

Instantly share code, notes, and snippets.

@inducer
Last active February 12, 2021 07:09
Show Gist options
  • Save inducer/05a516f6ca5e9a236058b47643f637df to your computer and use it in GitHub Desktop.
Save inducer/05a516f6ca5e9a236058b47643f637df to your computer and use it in GitHub Desktop.
Instrumenting POCL memory allocation

Tracing POCL Memory Allocation

To use this, build pocl with the patch below and then run your OpenCL program with

POCL_DEBUG=memory

Since you'll be using a patched pocl, maybe you will need ocl-icd to see that. To get that to happen, you can use this shell script. Save as, maybe, with-my-pocl:

#! /bin/bash
OCL_ICD_VENDORS=$HOME/shared/pack/pocl-master-install LD_LIBRARY_PATH=$HOME/shared/pack/pocl-master-install/lib:$LD_LIBRARY_PATH exec "$@"

Then run:

POCL_DEBUG=memory with-my-pocl ./my-program 2>out

The resulting file out can then be read by the parsing script.

import re
import datetime
POCL_LEADER_RE = re.compile(r"^\[([-0-9: ]+)(\.[0-9]+)\] POCL:(.*)$")
def main():
with open("out", "r") as inf:
lines = inf.readlines()
pocl_logs = []
i = 0
while i < len(lines):
ln = lines[i]
if m := POCL_LEADER_RE.match(ln):
i += 1
dt = (
datetime.datetime.strptime(m.group(1), "%Y-%m-%d %H:%M:%S").timestamp()
+ float(m.group(2)))
level, what, msg = lines[i].strip().split("|")
pocl_logs.append((dt, m.group(3).strip(), level.strip(" *"), what.strip(), msg.strip()))
i += 1
if i % 100_000 in [0, 1]:
print(i)
mem_timestamps = []
allocated_sizes = []
allocated = 0
alloc_timestamps = []
addresses = []
for tstamp, where, level, what, msg in pocl_logs:
if msg.startswith("mem_alloc"):
addr, size = msg.split(", ")
size = int(size.strip().removeprefix("size").strip())
addr = int(addr.strip().removeprefix("mem_alloc").strip(), 16)
allocated += size
mem_timestamps.append(tstamp)
allocated_sizes.append(allocated)
alloc_timestamps.append(tstamp)
addresses.append(addr)
elif msg.startswith("mem_free"):
mem_timestamps.append(tstamp)
_, size = msg.split(", ")
size = int(size.strip().removeprefix("size").strip())
allocated -= size
allocated_sizes.append(allocated)
else:
pass
import matplotlib.pyplot as plt
if 1:
plt.plot(mem_timestamps, allocated_sizes)
else:
plt.plot(alloc_timestamps, addresses, "x")
plt.show()
if __name__ == "__main__":
main()
diff --git a/lib/CL/devices/common.c b/lib/CL/devices/common.c
index a62eca66..bb7ca332 100644
--- a/lib/CL/devices/common.c
+++ b/lib/CL/devices/common.c
@@ -1280,6 +1280,8 @@ pocl_aligned_malloc_global_mem(cl_device_id device, size_t align, size_t size)
if (!retval)
goto ERROR;
+ POCL_MSG_PRINT_MEMORY (" mem_alloc %p, size %d\n", retval, size);
+
mem->currently_allocated += size;
if (mem->max_ever_allocated < mem->currently_allocated)
mem->max_ever_allocated = mem->currently_allocated;
@@ -1301,6 +1303,8 @@ pocl_free_global_mem(cl_device_id device, void* ptr, size_t size)
mem->currently_allocated -= size;
POCL_UNLOCK (mem->pocl_lock);
+ POCL_MSG_PRINT_MEMORY (" mem_free %p, size %d\n", ptr, size);
+
POCL_MEM_FREE(ptr);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment