cmemprof

package module
v0.0.0-...-4811d11 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 23, 2022 License: Apache-2.0, BSD-3-Clause Imports: 12 Imported by: 0

README

C memory allocation profiler

This repository contains an experimental C allocation profiler for Go.

WARNING: This is very much a work in progress. It still needs plenty of work to be production-ready. Use at your own peril!

It's been lightly tested on Linux (Ubuntu 20.04) and macos (Big Sur).

Building

On macos, you don't need to do anything special.

On Linux, you'll need a linker that supports the --wrap argument (such as the GNU linker, or mold) which is required to provide replacements for the allocation functions since dlsym uses calloc.

For Go versions prior to Go 1.15, you will also need to explicitly allow this argument for use by CGo by setting the following environment variable:

export CGO_LDFLAGS_ALLOW="-Wl,--wrap=.*"

Usage

Import this package and use the cmemprof.Profiler interface to start and stop profiling:

package main

import (
        "os"

        "github.com/nsrip-dd/cmemprof"
)

func main() {
        var profiler cmemprof.Profile
        profiler.Start(512*1024)
        defer func() {
		f, _ := os.Create("cmem.pprof")
		defer f.Close()
		p, err := profiler.Stop()
		if err != nil {
			return
		}
		p.Write(f)
	}

        // your code here
}

You will also need to import a cgo symbolizer such as github.com/ianlancetaylor/cgosymbolizer somewhere in your program to see the C portion of call stacks in the profiles.

Limitations

  • It's not optimized so there is noticeable overhead when the profiler is enabled.
  • This library has a few workarounds to avoid crashes due to unusual interactions between the Go runtime, cgo-generated code, and intercepting malloc. These workarounds depend on unstable implementation details. As such, this code may break your program for future Go releases. Use with caution.

Documentation

Overview

Package cmemprof profiles C memory allocations (malloc, calloc, realloc, etc.)

Importing this package in a program will replace malloc, calloc, and realloc with wrappers which will sample allocations and record them to a profile.

To use this package:

f, _ := os.Create("cmem.pprof")
var profiler cmemprof.Profile
profiler.Start(500)
// ... do allocations
profile, err := profiler.Stop()

Building this package on Linux requires a non-standard linker flag to wrap the alloaction functions. For Go versions < 1.15, cgo won't allow the flag so it has to be explicitly allowed by setting the following environment variable when building a program that uses this package:

export CGO_LDFLAGS_ALLOW="-Wl,--wrap=.*"

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Profile

type Profile struct {

	// SamplingRate is the value, in bytes, such that an average of one
	// sample will be recorded for every SamplingRate bytes allocated.  An
	// allocation of N bytes will be recorded with probability min(1, N /
	// SamplingRate).
	SamplingRate int
	// contains filtered or unexported fields
}

Profile provides access to a C memory allocation profiler based on instrumenting malloc, calloc, and realloc.

func (*Profile) Start

func (c *Profile) Start(rate int)

Start begins profiling C memory allocations.

func (*Profile) Stop

func (c *Profile) Stop() (*profile.Profile, error)

Stop cancels memory profiling and waits for the profile to be written to the io.Writer passed to Start. Returns any error from writing the profile.

Directories

Path Synopsis
internal
cgosymbolizer
Package cgosymbolizer provides a cgo symbolizer based on libbacktrace.
Package cgosymbolizer provides a cgo symbolizer based on libbacktrace.
testapp Module

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL