Skip to main content

License Build Status PyPI version Pyversions

malloc_tracer

About

This is a debugging tool for tracing malloc that occurs inside a function or class.

import numpy as np
import malloc_tracer


def func(x, y, z):
    dataset1 = np.empty((100, ), dtype=np.float64)
    print('x', x)
    dataset1 = np.empty((1000, ), dtype=np.float64)

    l = [i for i in range(100000)]

    if x == 0:
        dataset4a = np.empty((100000, ), dtype=np.float64)
        return 0
    elif x == 1:
        dataset4b = np.empty((100000, ), dtype=np.float64)
        return 1

    dataset3 = np.empty((3000, ), dtype=np.float64)
    return 2


tracer = malloc_tracer.Tracer(func)

This is equivalent to the following code.

import numpy as np
from tracemalloc import start, take_snapshot, stop


SNAPSHOT = None


def func(x, y, z):
    try:
        start()
        dataset1 = np.empty((100,), dtype=np.float64)
        print('x', x)
        dataset1 = np.empty((1000,), dtype=np.float64)

        l = [i for i in range(100000)]

        if (x == 0):
            dataset4a = np.empty((100000,), dtype=np.float64)
            return 0
        elif (x == 1):
            dataset4b = np.empty((100000,), dtype=np.float64)
            return 1

        dataset3 = np.empty((3000,), dtype=np.float64)
        return 2
    finally:
        global SNAPSHOT
        SNAPSHOT = take_snapshot()
        stop()

Feature

Compatibility

malloc_tracer works with Python 3.4 or higher.

Dependencies

Installation

pip install malloc-tracer

Usage

Trace a function.

import numpy as np
import malloc_tracer


def func(x, y, z):
    dataset1 = np.empty((100, ), dtype=np.float64)
    print('x', x)
    dataset1 = np.empty((1000, ), dtype=np.float64)

    l = [i for i in range(100000)]

    if x == 0:
        dataset4a = np.empty((100000, ), dtype=np.float64)
        return 0
    elif x == 1:
        dataset4b = np.empty((100000, ), dtype=np.float64)
        return 1

    dataset3 = np.empty((3000, ), dtype=np.float64)
    return 2
tracer = malloc_tracer.Tracer(func)
tracer.trace(
    target_args=dict(x=1, y=2, z=3)
)
usage1

usage1

Trace a method.

import numpy as np
import malloc_tracer


class Klass(object):

    CONSTANT = 'CONSTANT'

    def __init__(self, value):
        self._value = value

    def method(self, x):
        dataset1 = np.empty((100, ), dtype=np.float64)
        print('x', x)
        dataset1 = np.empty((1000, ), dtype=np.float64)

        l = [i for i in range(100000)]

        if x == 0:
            dataset4a = np.empty((100000, ), dtype=np.float64)
            return 0
        elif x == 1:
            dataset4b = np.empty((100000, ), dtype=np.float64)
            return 1

        dataset3 = np.empty((3000, ), dtype=np.float64)
        return 2

    @staticmethod
    def smethod():
        dataset = np.empty((100, ), dtype=np.float64)
        l = [i for i in range(100000)]
        print('Hello')
        return dataset

    @classmethod
    def cmethod(cls, var):
        return cls.CONSTANT + var
instance = Klass(1)
tracer = malloc_tracer.Tracer(instance.method)
tracer.trace(
    target_args=dict(x=1)
)
usage2a

usage2a

Trace a static method.

tracer = malloc_tracer.Tracer(Klass.smethod)
tracer.trace(
    target_args=dict()
)
usage2b

usage2b

Trace a class method.

tracer = malloc_tracer.Tracer(Klass.cmethod)
tracer.trace(
    target_args=dict(var='Hello world.')
)
usage2c

usage2c

Displays related traces for each file.

import numpy as np
import malloc_tracer


global_var1 = None
global_var2 = None


def func2():
    global global_var1
    global global_var2
    global_var1 = np.empty((1000, ), dtype=np.float64)
    global_var2 = np.empty((10000, ), dtype=np.float64)


def func(x, y, z):
    dataset1 = np.empty((100, ), dtype=np.float64)
    print('x', x)
    dataset1 = np.empty((1000, ), dtype=np.float64)

    l = [i for i in range(100000)]

    func2()

    if x == 0:
        dataset4a = np.empty((100000, ), dtype=np.float64)
        return 0
    elif x == 1:
        dataset4b = np.empty((100000, ), dtype=np.float64)
        return 1

    dataset3 = np.empty((3000, ), dtype=np.float64)
    return 2
tracer = malloc_tracer.Tracer(func)
tracer.trace(
    target_args=dict(x=1, y=2, z=3),
    related_traces_output_mode=malloc_tracer.RelatedTracesOutputMode.FOR_EACH_FILE
)
usage3a

usage3a

Displays related traces in descending order.

tracer = malloc_tracer.Tracer(func)
tracer.trace(
    target_args=dict(x=1, y=2, z=3),
    related_traces_output_mode=malloc_tracer.RelatedTracesOutputMode.IN_DESCENDING_ORDER
)
usage3b

usage3b

Convenience function.

malloc_tracer.trace(
    func,
    target_args=dict(x=1, y=2, z=3),
    related_traces_output_mode=malloc_tracer.RelatedTracesOutputMode.IN_DESCENDING_ORDER
)

License

This software is released under the MIT License, see LICENSE.

Metadata

Release files for malloc-tracer 1.7.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for malloc-tracer 1.7.0
File Size Uploaded Provenance Actions
malloc_tracer-1.7.0.tar.gz 202.8 kB Without provenance Details for malloc_tracer-1.7.0.tar.gz

Release files / malloc_tracer-1.7.0.tar.gz

Download URL malloc_tracer-1.7.0.tar.gz
Size 202.8 kB
Tags Source
SHA-256 checksum
How to use checksums
94348e9513e6e4d0594e51e8524972ebad5f21029d85dae862271e37fa65b623
BLAKE2b-256 checksum
How to use checksums
593faeaadbdafb56eeea40551744ba6a311c7d9caf8ce616430df79a8c6886b6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/3.3.0 pkginfo/1.6.1 requests/2.25.1 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.55.1 CPython/3.8.3

Security for malloc-tracer 1.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page