github.com/grafana/pyroscope@v1.18.0/docs/sources/view-and-analyze-profile-data/self-vs-total.md (about)

     1  ---
     2  title: Understand 'self' vs. 'total' metrics in profiling with Pyroscope
     3  menuTitle: Understand 'self' vs. 'total' metrics
     4  description: Learn the differences between 'self' and 'total' metrics in profiling and their specific applications in CPU and Memory profiling with Pyroscope.
     5  weight: 300
     6  aliases:
     7    - ../ingest-and-analyze-profile-data/self-vs-total/
     8  keywords:
     9    - pyroscope
    10    - cpu profiling
    11    - memory profiling
    12    - self vs total
    13    - performance analysis
    14  ---
    15  
    16  # Understand 'self' vs. 'total' metrics in profiling with Pyroscope
    17  
    18  Profiling in Pyroscope provides many different ways of analyzing your profiling data. One of the key pieces of this analysis are the metrics 'self' and 'total', whose understanding is key for accurate interpretation of profiling data in both CPU and memory contexts. These metrics can be seen both within the table and the flame graph view of the UI.
    19  
    20  ![Self vs total annotated](https://grafana.com/media/docs/pyroscope/self_vs_total_annotated.png)
    21  
    22  ## Self
    23  
    24  'Self' refers to the resource usage (CPU time, memory allocation, etc.) directly attributed to a specific function or a code segment, excluding the resources used by its sub-functions or calls
    25  
    26  This metric helps isolate the direct impact of a specific code block, making it crucial for pinpointing primary resource consumers
    27  
    28  ![Self vs total diagram](https://grafana.com/media/docs/pyroscope/self_vs_total_explainer_diagram.png)
    29  
    30  
    31  ## Total
    32  
    33  'Total' encompasses the combined resource usage of a function along with all the functions it calls
    34  
    35  It provides a holistic view of a function's overall resource consumption, essential for understanding cumulative impacts
    36  
    37  ## 'Self' and 'Total' in CPU profiling
    38  
    39  In CPU profiling, 'self' indicates the CPU time consumed directly by the function, crucial for identifying functions with high CPU demand.
    40  
    41  The 'total' CPU time includes time spent in the function itself plus time in all called functions, highlighting comprehensive CPU usage
    42  
    43  The example below demonstrates a simplified pseudocode representation of a CPU-intensive process.
    44  This illustrates how 'self' and 'total' time would be calculated for different functions in a typical application. The following diagram provides a visual representation of these concepts.
    45  
    46  ![Self vs total diagram](https://grafana.com/media/docs/pyroscope/self_vs_total_explainer_diagram.png)
    47  
    48  ```python
    49  def handle_request():
    50      # Root function representing the total request handling process
    51      parse_json()
    52      process_data()
    53  
    54  def parse_json():
    55      # Function for parsing JSON data
    56      validate()
    57  
    58  def validate():
    59      # Function for validating the parsed JSON schema
    60      # Schema validation logic
    61  
    62  def process_data():
    63      # Function for processing data
    64      apply()
    65  
    66  def apply():
    67      # Function for applying transformations to data
    68      # Transformation logic
    69  
    70  # Simulate a request handling
    71  handle_request()
    72  ```
    73  
    74  ## 'Self' and 'Total' in memory profiling
    75  
    76  - **Self in Memory**: For memory profiling, 'self' measures the memory allocated by the function itself, vital for detecting direct memory allocation issues.
    77  - **Total in Memory**: 'Total' memory includes allocations by the function and it's called functions, essential for assessing overall memory footprint.
    78  
    79  The same example from the CPU profiling section can be used to illustrate the concepts of 'self' and 'total' in memory profiling, just with memory units instead of CPU.
    80  
    81  ## Conclusion
    82  
    83  Grasping the distinction between 'self' and 'total' metrics is fundamental for effective performance analysis in Pyroscope. Whether in CPU or memory profiling (or any other type), these metrics provide value insights for optimizing applications and enhancing their efficiency and reliability.