我要评分
获取效率
正确性
完整性
易理解

libkperf APIs

  • Collection types:
    1
    2
    3
    4
    5
    enum PmuTaskType {
        COUNTING = 0,   // Counting
        SAMPLING = 1,   // PMU sampling
        SPE_SAMPLING = 2,   // SPE sampling
    };
    
  • Metric types:
    1
    2
    3
    4
    5
    6
    7
    8
    enum PmuMetricType {
        TMA_LEVEL1,         // Topdown level1
        TMA_FRONTEND,       // Topdown frontend
        TMA_BADSPEC,        // Topdown bad speculation
        TMA_CORE_BOUND,     // Topdown core bound
        TMA_MEM_BOUND,      // Topdown memory bound
        TMA_RESOURCE_BOUND, // Topdown resource bound
    };
    
  • Metric aggregation types:
    1
    2
    3
    4
    5
    6
    7
    enum AggregateType {
        PER_SYSTEM,         // By system
        PER_CORE,           // By core
        PER_NUMA,           // By NUMA
        PER_SOCKET,         // By socket
        PER_THREAD,         // By thread
    };
    
  • SPE data selector:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    enum SpeFilter {
        SPE_FILTER_NONE = 0,
        TS_ENABLE = 1UL << 0,       // Uses the value of the general timer to enable timestamps.
        PA_ENABLE = 1UL << 1,       // Collects the physical and virtual addresses of the load and store.
        PCT_ENABLE = 1UL << 2,      // Collects physical timestamps instead of virtual timestamps.
        JITTER = 1UL << 16,         // Uses jitter to avoid resonance during sampling.
        BRANCH_FILTER = 1UL << 32,  // Collects branches.
        LOAD_FILTER = 1UL << 33,    // Collects load instructions.
        STORE_FILTER = 1UL << 34,   // Collects store instructions.
        SPE_DATA_ALL = TS_ENABLE | PA_ENABLE | PCT_ENABLE | JITTER | BRANCH_FILTER | LOAD_FILTER | STORE_FILTER
    };
    
  • SPE event selector:
    1
    2
    3
    4
    5
    6
    7
    enum SpeEventFilter {
        SPE_EVENT_NONE = 0,
        SPE_EVENT_RETIRED = 0x2,        // Instruction retired
        SPE_EVENT_L1DMISS = 0x8,        // L1D refill
        SPE_EVENT_TLB_WALK = 0x20,      // TLB refill
        SPE_EVENT_MISPREDICTED = 0x80,  // Mispredict
    };
    
  • Task attributes:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    struct PmuAttr {
        char** evtList;                 // Event list.
        unsigned numEvt;                // Event list length.
        int* pidList;                   // PID list.
        unsigned numPid;                // PID list length.
        int* cpuList;                   // CPU ID list.
        unsigned numCpu;                // CPU ID list length.
        union {
            unsigned period;              // Sampling period.
            unsigned freq;                // Sampling frequency.
        };
        unsigned useFreq : 1;             // Indicates whether to use the sampling frequency. If the value is 1, the previous union indicates the sampling frequency. If the value is 0, the previous union indicates the sampling period.
        // SPE related fields.
        enum SpeFilter dataFilter;       // SPE data selector.
        enum SpeEventFilter evFilter;   // SPE event selector.
        unsigned long minLatency;       // Collects only samples with this delay or higher delay.
    };
    
  • Topdown level 1 data:
    1
    2
    3
    4
    5
    6
    7
    struct TmaLevel1 {
        float ipc;
        float retired;
        float backendBd;
        float frontendBd;
        float badSpeculation;
    };
    
  • Topdown frontend data:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    struct TmaFtEndBd {
        struct TmaLevel1* level1;
        float fetchLatencyBound;
        float itlbMiss;
        float l1Tlb;
        float l2Tlb;
        float icacheMiss;
        float l1Miss;
        float l2Miss;
        float bpMispFlush;
        float oooFlush;
        float spFlush;
        float fetchBandwidthBound;
    };
    
  • Topdown core bound data:
    1
    2
    3
    4
    5
    6
    7
    struct TmaCoreBd {
        struct TmaLevel1* level1;
        float coreBound;
        float divider;
        float fsuStall;
        float exePortsUtil;
    };
    
  • Topdown bad speculation data:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    struct TmaBadSpec {
        struct TmaLevel1* level1;
        float branchMispredict;
        float indirectBranch;
        float pushBranch;
        float popBranch;
        float otherBranch;
        float machineClear;
        float nukeFlush;
        float otherFlush;
    };
    
  • Topdown memory bound data:
    1
    2
    3
    4
    5
    6
    7
    8
    struct TmaMemBd {
        struct TmaLevel1* level1;
        float memoryBound;
        float l1Bound;
        float l2Bound;
        float memBound;
        float storeBound;
    };
    
  • Topdown resource bound data:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    struct TmaResourceBd {
        struct TmaLevel1* level1;
        float resourceBound;
        float syncStall;
        float robStall;
        float ptagStall;
        float saveopqStall;
        float pcBufferStall;
        float ccphysicalStall;
        float intphysicalStall;
        float vfpphysicalStall;
    };
    
  • Metric data:
    1
    2
    3
    4
    5
    6
    7
    8
    union PmuMetricData {
        struct TmaLevel1* level1;
        struct TmaFtEndBd* tmaFtEndBd;
        struct TmaCoreBd* tmaCorebd;
        struct TmaBadSpec* tmaBadSpec;
        struct TmaMemBd* tmaMemBd;
        struct TmaResourceBd* tmaResourceBd;
    };
    
  • Metric data:
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    struct PmuMetric {
        int id;                         // The ID meaning varies according to AggregateType:
                                        // For PER_SYSTEM, the ID is equal to -1.
                                        // For PER_CORE, the ID indicates the CPU ID.
                                        // For PER_NUMA, the ID indicates the NUMA node ID.
                                        // For PER_SOCKET, the ID indicates the socket ID.
                                        // For PER_THREAD, the ID indicates the PID.
        int metricType;                 // Aggregation type.
        union PmuMetricData data;       // Metric data.
    };
    struct PmuDataExt {
        unsigned long pa;               // Physical address.
        unsigned long va;               // Virtual address.
        unsigned long event;            // Event ID.
    };
    struct SampleRawData {
        char *data;                     // Raw data corresponding to the tracepointer event.
    };
    struct SampleRawField {
        char* fieldName;                 // Field name.
        char* fieldStr;                  // Field description.
        unsigned offset;                 // Offset.
        unsigned size;                   // Number of bytes.
        unsigned isSigned;               // Whether there is a sign.
    };
    struct PmuData {
        struct Stack* stack;            // Call stack.
        const char *evt;                // Event name.
        int64_t ts;                     // Timestamp.
        pid_t pid;                      // Process ID.
        int tid;                        // Thread ID.
        unsigned cpu;                   // CPU ID.
        struct CpuTopology *cpuTopo;    // CPU topology.
        const char *comm;               // Process command.
        uint64_t period;                // Collection duration.
        uint64_t count;                 // Count of the current data. This parameter is valid only for the COUNTING type.
        struct PmuDataExt *ext;         // Extended data. This parameter is valid only for the SPE type.
        struct SampleRawData *rawData;  // Raw data corresponding to the tracepointer event.
    };
    
  • Initialize the collection task.

    If the initialization is successful, a descriptor greater than or equal to 0 is returned. If the initialization fails, -1 is returned.

    1
    int PmuOpen(enum PmuTaskType collectType, struct PmuAttr *attr);
    
  • Start the collection.

    If the value of <milliseconds> is -1, the collection does not stop until the process ends or the collection is stopped by PmuStop.

    The return value is an error code:

    1
    int PmuCollect(int pd, int milliseconds);
    
  • Stop the collection process.
    1
    void PmuStop(int pd);
    
  • Read the data collected last time. The data is stored in <pmuData>, which is an array structure.

    The return value is the length of <pmuData>.

    If the data read fails, -1 is returned.

    1
    int PmuRead(int pd, struct PmuData** pmuData);
    
  • Stop the collection task corresponding to <pd>.
    1
    void PmuClose(int pd);
    
  • Obtain the topdown event list for invoking PmuOpen.
    1
    const char** PmuTopDownEvents(enum PmuMetricType metric, unsigned *numEvt);
    
  • Calculate the metric data based on the metric type and aggregation type.

    The metric data is stored in <metric>, which is an array structure.

    The return value is the length of <metric>.

    If the metric data fails to be calculated, -1 is returned.

    1
    2
    int PmuComputeMetric(
        int pd, enum PmuMetricType metricType, enum AggregateType aggregateType, struct PmuMetric** metric);
    
  • Release struct PmuMetric*.
    1
    void PmuMetricFree(struct PmuMetric* metricData);
    
  • Release struct PmuData*.
    1
    void PmuDataFree(struct PmuData* pmuData);
    
  • Obtain the specified tracepointer field.
    1
    int PmuGetField(struct SampleRawData *rawData, const char *fieldName, void *value, uint32_t vSize);
    
  • Obtain the description of the specified tracepointer field.
    1
    struct SampleRawField *PmuGetFieldExp(struct SampleRawData *rawData, const char *fieldName);