Giter VIP home page Giter VIP logo

cpuusage's Introduction

Cpuusage

Linux Mac
Linux macOS

Cpuusage is an instrumentation CPU profiler for Linux and macOS applications. It provides logging of standard POSIX function calls for any application, and logging of internal function calls for programs compiled with -finstrument-functions. The logging output format is a HTML-file generated using Chromium Catapult.

Example Usage

Tracing all POSIX function calls from a regular (non-instrumented) application:

$ cpuusage -v -o cutrace1.html -a ./ex001 
cpuusage: https://github.com/d99kris/cpuusage
cpuusage: initializing
cpuusage: starting program ./ex001
cpuusage: processing output trace
cpuusage: completed processing 96 samples
$ xdg-open cutrace1.html 

Resulting visualization: culog1 screenshot

Supported Platforms

Cpuusage is primarily developed and tested on Linux, but basic functionality should work in macOS as well. Current version has been tested on:

  • macOS Big Sur 11.0
  • Ubuntu 20.04 LTS

Limitation: On macOS this tool relies on code injection using DYLD_INSERT_LIBRARIES, which generally does not work with third-party applications in a standard system. Using it on (your own) applications built from source should work fine though.

Installation

Pre-requisites (Ubuntu):

sudo apt install git cmake build-essential python-lxml

Pre-requisites (Python):

pip3 install -U six

Download the source code:

git clone https://github.com/d99kris/cpuusage && cd cpuusage

Generate Makefile and build:

mkdir -p build && cd build && cmake .. && make -s

Optionally install in system:

sudo make install

Usage

General syntax:

cpuusage -a [OPTIONS] PROG [ARGS..]
cpuusage -c [OPTIONS] PROG [ARGS..]
cpuusage -f <FUNCTIONS> [OPTIONS] PROG [ARGS..]
cpuusage -i <INCLUDES> [OPTIONS] PROG [ARGS..]
cpuusage -p [OPTIONS] PROG [ARGS..]
cpuusage -r JSONFILE [OPTIONS]
cpuusage --help|-h
cpuusage --version|-v

Options:

-a     trace all standard POSIX function calls

-c     trace internal function calls (requires PROG to be compiled with
       -finstrument-functions)

-d     debug mode, running program through debugger

-e     separate processes for child process trace

-f <FUNCTIONS>
       trace specified POSIX functions

-i <INCLUDES>
       trace functions in specified POSIX headers

-p     trace child process durations

--help, -h
       display this help and exit

--version, -v
       output version information and exit

-j <PATH>
       write iterim JSON trace log to specified path

-m <MINTIME>
       only log samples with minimum specified duration (in usec)
       currently only supported when tracing main-thread only.

-n <SAMPLES>
       only log up to specified number of samples

-o <PATH>
       write  HTML  report  to  specified  path,  rather  than  default
       ./cutrace-PID.html

-r <JSONFILE>
       read JSON file and convert to HTML report

-s     start tracing with SIGUSR1 and stop with SIGUSR2, instead of
       trace from process start

-u     trace only main-thread

-v     verbose mode

-y     auto-open resulting html file

PROG   program to run and profile

[ARGS] optional arguments to the program

More Examples

Tracing specified POSIX function calls from a regular (non-instrumented) application:

$ cpuusage -v -o cutrace2.html -f fopen,fwrite,fread,fclose ./ex001 
cpuusage: https://github.com/d99kris/cpuusage
cpuusage: initializing
cpuusage: starting program ./ex001
cpuusage: processing output trace
cpuusage: completed processing 36 samples
$ xdg-open cutrace2.html 

Resulting visualization: culog2 screenshot

Tracing internal function calls in an instrumented application:

$ cpuusage -v -o cutrace3.html -c ./ex002
cpuusage: https://github.com/d99kris/cpuusage
cpuusage: initializing
cpuusage: starting program ./ex002
cpuusage: processing output trace
cpuusage: completed processing 8 samples
$ xdg-open cutrace3.html 

Resulting visualization: culog3 screenshot

Tracing internal function calls and all external POSIX function calls in an instrumented application:

$ cpuusage -v -o cutrace4.html -c -a ./ex002
cpuusage: https://github.com/d99kris/cpuusage
cpuusage: initializing
cpuusage: starting program ./ex002
cpuusage: processing output trace
cpuusage: completed processing 104 samples
$ xdg-open cutrace4.html 

Resulting visualization: culog4 screenshot

Tracing process durations:

$ cpuusage -v -o cutrace5.html -p ./ex005.sh 
cpuusage: https://github.com/d99kris/cpuusage
cpuusage: initializing
cpuusage: starting program ./ex005.sh
cpuusage: processing output trace
cpuusage: completed processing 5 samples

Resulting visualization: culog5 screenshot

Alternatives

There are many CPU profilers available for Linux and macOS. Most of them are sample-based, and here is a list of some of them:

  • Gperftools
  • Gprof
  • Instruments - Time Profiler
  • Valgrind - Callgrind

Technical Details

Refer to ext/README.md for details on the external components used by cpuusage.

License

Cpuusage is distributed under the BSD 3-Clause license. See LICENSE file.

Keywords

linux, macos, cpu usage, instrumented profiler, alternative to callgrind.

cpuusage's People

Contributors

d99kris avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.