Nervos CKB Docs
  • Basics
  • Reference
  • Labs
  • Integrate
  • Essays

›Essays

Basics

  • Basics Introduction
  • Concepts

    • Nervos Blockchain
    • Cell Model
    • Consensus
    • Economics
    • CKB-VM

    Guides

    • Run a CKB Dev Blockchain
    • Run a CKB Mainnet Node
    • Run a CKB Testnet Node
    • Neuron Wallet Guide
    • Run a CKB Mainnet Node and Testnet Node with Docker
    • Get CKB Binary
    • Get CKB Binary on Windows (experimental)
  • Tools
  • Glossary

Reference

  • Introduction
  • Cell
  • Script
  • Transaction
  • JSON-RPC

Labs

  • Introduction
  • Write a SUDT script by Capsule
  • Introduction to Lumos via NervosDAO
  • Dynamic loading in Capsule

Integrate

  • Nervos CKB Mainnet - Integration Guide
  • Q&A | For Wallets/Exchanges/Mining Pools
  • Nervos CKB SDK

Essays

  • Introduction
  • Developer Materials Guide
  • A Tour of RFCs
  • Transaction validation lifecycle
  • Tips for debugging CKB script
  • Tips for profiling CKB script
  • The General Workflow for Constructing a Transaction
  • Script dependencies
  • Introduction to CKB Studio
  • Technical Bits on Polyjuice
  • CKB FAQs
  • Tips for CKB development
  • Integrity Check for CKB Release
  • Mint SUDT via Contract

Tips for profiling CKB script

Before starting, make sure you have understood the concept of cycles.

In the development phase of dapps, it is a wide range of needs to estimate how many cycles our dapp will consume.

ckb-vm-pprof is a tool for visualization and analysis of profiling data. It can run a script off-chain and then collect the runtime data. According to your preference, It can generate both text and graphical reports.

Get ckb-vm-pprof

$ git clone https://github.com/nervosnetwork/ckb-vm-pprof
$ cd ckb-vm-pprof
$ cargo build --release

The executable file is located at ./target/release/ckb-vm-pprof, you can copy it to any $PATH you like.

Basic usage

Suppose the program to be tested is:

int fib(int n) {
    if (n == 0 || n == 1) {
        return n;
    } else {
        return fib(n-1) + fib(n-2);
    }
}

int main() {
    if (fib(10) != 55) {
        return 1;
    }
    return 0;
}

The Fibonacci function is used in the example because it is not only simple enough, but also a recursive function.

Build with -g option

-g option, described in the gcc documentation as:

-g
Produce debugging information in the operating system’s native format (stabs, COFF, XCOFF, or DWARF). GDB can work with this debugging information.

On most systems that use stabs format, -g enables use of extra debugging information that only GDB can use; this extra information makes debugging work better in GDB but probably makes other debuggers crash or refuse to read the program. If you want to control for certain whether to generate the extra information, use -gstabs+, -gstabs, -gxcoff+, -gxcoff, or -gvms (see below).

It is necessary to bring it when compiling, so the compilation command is:

$ riscv64-unknown-elf-gcc -g -o fib fib.c

Install visualization package

If you only need a text report, you can ignore this step.

$ cargo install inferno

Inferno is a port of parts of the flamegraph toolkit to Rust, with the aim of improving the performance of the original flamegraph tools.

Get reports

We just need to use ckb-vm-pprof to run the binary fib:

$ ckb-vm-pprof --bin fib > flamegraph.txt

$ cat flamegraph.txt
??:?? 938
??:??; /src/ckb-vm-pprof/res/fib.c:main 24
??:??; /src/ckb-vm-pprof/res/fib.c:main; /src/ckb-vm-pprof/res/fib.c:fib 7311

The basic format of the text report is Function0; Function1; ... FunctionN Cycles, that is, the call stack and the final cycles. ??:?? means that some codes that does not belong to any user defined function(usually generated by the compiler).

Generate graphics for easy reading:

$ cat flamegraph.txt | inferno-flamegraph > fib.svg

fib.svg

Please note that the function with too small proportion will not be displayed on the flamegraph by default.

← Tips for debugging CKB scriptThe General Workflow for Constructing a Transaction →
  • Get ckb-vm-pprof
  • Basic usage
    • Build with -g option
    • Install visualization package
    • Get reports
Foundation
About Us
Developer
GitHubWhitepaperRFCs
TwitterBlogTelegramRedditYouTubeForum
Copyright © 2020 Nervos Foundation. All Rights Reserved.
Note we've completely rebuilt Nervos Doc site! For the old doc site, please see docs-old.