github.com/containers/podman/v2@v2.2.2-0.20210501105131-c1e07d070c4c/dependencies/analyses/README.md (about)

     1  # A set of scripts and instructions that help to analyze and debloat go-lang dependencies
     2  
     3  Note that all scripts mentioned below follow the [KISS principle](https://en.wikipedia.org/wiki/KISS_principle) on purpose.
     4  The scripts are meant to be used in combination to aid in understanding the package's dependencies and how they contribute to the size of the compiled binary.
     5  
     6  ## Size of packages
     7  
     8  To analyze the size of all go packages used during the build process, pass the `-work -a` build flags to `go build`.
     9  The `-a` flag forces go to rebuild all packages even if they are already up-to-date (e.g., in the build cache), while the `-work` flag instructs go to print the temporary work directory used for compiling the packages.
    10  The path to the temporary work directory of `go-build` must be passed to `go-archive-analysis.sh` by setting it as an environment variable.
    11  The analysis script will then read and parse the build data and print a sorted table of the package size in bytes followed by the package name.
    12  
    13  Running such an analysis on libpod may look as follows:
    14  
    15  ```
    16  # 1) Build the Podman binary with `-work -a`.
    17  [libpod]$ BUILDFLAGS="-work -a" make podman
    18  [...]
    19  WORK=/tmp/go-build794287815
    20  
    21  # 2) Set the work directory as an environment variable and call the analysis script
    22  [libpod]$ WORK=/tmp/go-build794287815 ./dependencies/analyses/go-archive-analysis.sh | head -n10
    23  17M github.com/containers/podman/cmd/podman/cliconfig
    24  13M github.com/containers/podman/vendor/github.com/DataDog/zstd
    25  10M github.com/containers/podman/vendor/k8s.io/api/core/v1
    26  3.7M net/http
    27  3.7M github.com/containers/podman/libpod
    28  3.2M runtime
    29  2.7M github.com/containers/podman/vendor/github.com/gogo/protobuf/proto
    30  2.5M github.com/containers/podman/vendor/k8s.io/apimachinery/pkg/apis/meta/v1
    31  2.3M github.com/containers/podman/vendor/github.com/vishvananda/netlink
    32  2.1M github.com/containers/podman/cmd/podman/varlink
    33  ```
    34  
    35  The output of the `go-archive-analysis.sh` script is a sorted table with the size in bytes followed by the package.
    36  The size denotes the size of the compiled package (i.e., the `.a` file).
    37  
    38  
    39  ## Size of symbols in binary
    40  
    41  Once the binary is compiled, we can run another set of analyses on it.
    42  The `nm-symbols-analysis.sh` is a wrapper around `go tool nm` and prints a table with the size in bytes followed by the symbol's name.
    43  To avoid information overload, the scripts prints only symbols from the text/code segment.
    44  
    45  Running such an analysis on libpod may look as follows:
    46  
    47  ```
    48  # 1) Compile the binary
    49  [libpod]$ make podman
    50  [...]
    51  
    52  # 2) Run the script with the binary as an argument
    53  [libpod]$ ./dependencies/analyses/nm-symbols-analysis.sh ./bin/podman | grep "containers/libpod/libpod" | head -n10
    54  299             github.com/containers/podman/libpod.(*BoltState).AddContainer
    55  658             github.com/containers/podman/libpod.(*BoltState).AddContainerToPod
    56  2120            github.com/containers/podman/libpod.(*BoltState).AddPod
    57  3773            github.com/containers/podman/libpod.(*BoltState).AddPod.func1
    58  965             github.com/containers/podman/libpod.(*BoltState).AddVolume
    59  1651            github.com/containers/podman/libpod.(*BoltState).AddVolume.func1
    60  558             github.com/containers/podman/libpod.(*BoltState).AllContainers
    61  282             github.com/containers/podman/libpod.(*BoltState).AllContainers.func1
    62  1121            github.com/containers/podman/libpod.(*BoltState).AllContainers.func1.1
    63  558             github.com/containers/podman/libpod.(*BoltState).AllPods
    64  ```
    65  
    66  Running the script can help identify sources of bloat and reveal potential candidates (e.g., entire packages, types, or function) for refactoring.
    67  
    68  
    69  ## Dependency Tree
    70  
    71  Use the `dependency-tree.sh` script to figure out which package includes which packages.
    72  The output of the script has the format `package: dependency_1, dependency_2, ...`.
    73  Each line is followed by a blank line to make it easier to read.
    74  The script generates two files:
    75  
    76   - `direct-tree.txt` - listing direct dependencies
    77   - `transitive-tree.txt` - listing direct and transitive dependencies
    78  
    79  Running such a dependency-tree analysis may look as follows:
    80  
    81  
    82  ```
    83  [libpod]$ ./dependencies/analyses/dependency-tree.sh github.com/containers/podman
    84  [libpod]$ grep "^github.com/containers/podman/pkg/registries" direct-tree.txt
    85  github.com/containers/podman/pkg/registries: github.com/containers/podman/vendor/github.com/containers/image/pkg/sysregistriesv2, github.com/containers/podman/vendor/github.com/containers/image/types, github.com/containers/podman/pkg/rootless, github.com/containers/podman/vendor/github.com/docker/distribution/reference, github.com/containers/podman/vendor/github.com/pkg/errors, os, path/filepath, strings
    86  ```
    87  
    88  As shown above, the script's output can then be used to query for specific packages (e.g, with `grep`).