Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,34 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LIBPERF_INTERNAL_CPUMAP_H
#define __LIBPERF_INTERNAL_CPUMAP_H
#include <linux/refcount.h>
#include <perf/cpumap.h>
#include <internal/rc_check.h>
/**
* A sized, reference counted, sorted array of integers representing CPU
* numbers. This is commonly used to capture which CPUs a PMU is associated
* with. The indices into the cpumap are frequently used as they avoid having
* gaps if CPU numbers were used. For events associated with a pid, rather than
* a CPU, a single dummy map with an entry of -1 is used.
*/
DECLARE_RC_STRUCT(perf_cpu_map) {
refcount_t refcnt;
/** Length of the map array. */
unsigned int nr;
/** The CPU values. */
struct perf_cpu map[];
};
struct perf_cpu_map *perf_cpu_map__alloc(unsigned int nr_cpus);
int perf_cpu_map__idx(const struct perf_cpu_map *cpus, struct perf_cpu cpu);
bool perf_cpu_map__is_subset(const struct perf_cpu_map *a, const struct perf_cpu_map *b);
void perf_cpu_map__set_nr(struct perf_cpu_map *map, unsigned int nr_cpus);
static inline refcount_t *perf_cpu_map__refcnt(struct perf_cpu_map *map)
{
return &RC_CHK_ACCESS(map)->refcnt;
}
#endif /* __LIBPERF_INTERNAL_CPUMAP_H */
@@ -0,0 +1,140 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LIBPERF_INTERNAL_EVLIST_H
#define __LIBPERF_INTERNAL_EVLIST_H
#include <linux/list.h>
#include <api/fd/array.h>
#include <internal/cpumap.h>
#include <internal/evsel.h>
#define PERF_EVLIST__HLIST_BITS 8
#define PERF_EVLIST__HLIST_SIZE (1 << PERF_EVLIST__HLIST_BITS)
struct perf_cpu_map;
struct perf_thread_map;
struct perf_mmap_param;
struct perf_evlist {
struct list_head entries;
int nr_entries;
bool has_user_cpus;
bool needs_map_propagation;
/**
* The cpus passed from the command line or all online CPUs by
* default.
*/
struct perf_cpu_map *user_requested_cpus;
/** The union of all evsel cpu maps. */
struct perf_cpu_map *all_cpus;
struct perf_thread_map *threads;
int nr_mmaps;
size_t mmap_len;
struct fdarray pollfd;
struct hlist_head heads[PERF_EVLIST__HLIST_SIZE];
struct perf_mmap *mmap;
struct perf_mmap *mmap_ovw;
struct perf_mmap *mmap_first;
struct perf_mmap *mmap_ovw_first;
};
typedef void
(*perf_evlist_mmap__cb_idx_t)(struct perf_evlist*, struct perf_evsel*,
struct perf_mmap_param*, int);
typedef struct perf_mmap*
(*perf_evlist_mmap__cb_get_t)(struct perf_evlist*, bool, int);
typedef int
(*perf_evlist_mmap__cb_mmap_t)(struct perf_mmap*, struct perf_mmap_param*, int, struct perf_cpu);
struct perf_evlist_mmap_ops {
perf_evlist_mmap__cb_idx_t idx;
perf_evlist_mmap__cb_get_t get;
perf_evlist_mmap__cb_mmap_t mmap;
};
int perf_evlist__alloc_pollfd(struct perf_evlist *evlist);
int perf_evlist__add_pollfd(struct perf_evlist *evlist, int fd,
void *ptr, short revent, enum fdarray_flags flags);
int perf_evlist__mmap_ops(struct perf_evlist *evlist,
struct perf_evlist_mmap_ops *ops,
struct perf_mmap_param *mp);
void perf_evlist__init(struct perf_evlist *evlist);
void perf_evlist__exit(struct perf_evlist *evlist);
/**
* __perf_evlist__for_each_entry - iterate thru all the evsels
* @list: list_head instance to iterate
* @evsel: struct perf_evsel iterator
*/
#define __perf_evlist__for_each_entry(list, evsel) \
list_for_each_entry(evsel, list, node)
/**
* evlist__for_each_entry - iterate thru all the evsels
* @evlist: perf_evlist instance to iterate
* @evsel: struct perf_evsel iterator
*/
#define perf_evlist__for_each_entry(evlist, evsel) \
__perf_evlist__for_each_entry(&(evlist)->entries, evsel)
/**
* __perf_evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
* @list: list_head instance to iterate
* @evsel: struct evsel iterator
*/
#define __perf_evlist__for_each_entry_reverse(list, evsel) \
list_for_each_entry_reverse(evsel, list, node)
/**
* perf_evlist__for_each_entry_reverse - iterate thru all the evsels in reverse order
* @evlist: evlist instance to iterate
* @evsel: struct evsel iterator
*/
#define perf_evlist__for_each_entry_reverse(evlist, evsel) \
__perf_evlist__for_each_entry_reverse(&(evlist)->entries, evsel)
/**
* __perf_evlist__for_each_entry_safe - safely iterate thru all the evsels
* @list: list_head instance to iterate
* @tmp: struct evsel temp iterator
* @evsel: struct evsel iterator
*/
#define __perf_evlist__for_each_entry_safe(list, tmp, evsel) \
list_for_each_entry_safe(evsel, tmp, list, node)
/**
* perf_evlist__for_each_entry_safe - safely iterate thru all the evsels
* @evlist: evlist instance to iterate
* @evsel: struct evsel iterator
* @tmp: struct evsel temp iterator
*/
#define perf_evlist__for_each_entry_safe(evlist, tmp, evsel) \
__perf_evlist__for_each_entry_safe(&(evlist)->entries, tmp, evsel)
static inline struct perf_evsel *perf_evlist__first(struct perf_evlist *evlist)
{
return list_entry(evlist->entries.next, struct perf_evsel, node);
}
static inline struct perf_evsel *perf_evlist__last(struct perf_evlist *evlist)
{
return list_entry(evlist->entries.prev, struct perf_evsel, node);
}
u64 perf_evlist__read_format(struct perf_evlist *evlist);
void perf_evlist__id_add(struct perf_evlist *evlist,
struct perf_evsel *evsel,
int cpu_map_idx, int thread, u64 id);
int perf_evlist__id_add_fd(struct perf_evlist *evlist,
struct perf_evsel *evsel,
int cpu_map_idx, int thread, int fd);
void perf_evlist__reset_id_hash(struct perf_evlist *evlist);
void __perf_evlist__set_leader(struct list_head *list, struct perf_evsel *leader);
void perf_evlist__go_system_wide(struct perf_evlist *evlist, struct perf_evsel *evsel);
#endif /* __LIBPERF_INTERNAL_EVLIST_H */
@@ -0,0 +1,153 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LIBPERF_INTERNAL_EVSEL_H
#define __LIBPERF_INTERNAL_EVSEL_H
#include <linux/types.h>
#include <linux/perf_event.h>
#include <stdbool.h>
#include <sys/types.h>
#include <internal/cpumap.h>
struct perf_thread_map;
struct xyarray;
/**
* The per-thread accumulated period storage node.
*/
struct perf_sample_id_period {
struct list_head node;
struct hlist_node hnode;
/* Holds total ID period value for PERF_SAMPLE_READ processing. */
u64 period;
/* The TID that the values belongs to */
u32 tid;
};
/**
* perf_evsel_for_each_per_thread_period_safe - safely iterate thru all the
* per_stream_periods
* @evlist:perf_evsel instance to iterate
* @item: struct perf_sample_id_period iterator
* @tmp: struct perf_sample_id_period temp iterator
*/
#define perf_evsel_for_each_per_thread_period_safe(evsel, tmp, item) \
list_for_each_entry_safe(item, tmp, &(evsel)->per_stream_periods, node)
#define PERF_SAMPLE_ID__HLIST_BITS 4
#define PERF_SAMPLE_ID__HLIST_SIZE (1 << PERF_SAMPLE_ID__HLIST_BITS)
/*
* Per fd, to map back from PERF_SAMPLE_ID to evsel, only used when there are
* more than one entry in the evlist.
*/
struct perf_sample_id {
struct hlist_node node;
u64 id;
struct perf_evsel *evsel;
/*
* 'idx' will be used for AUX area sampling. A sample will have AUX area
* data that will be queued for decoding, where there are separate
* queues for each CPU (per-cpu tracing) or task (per-thread tracing).
* The sample ID can be used to lookup 'idx' which is effectively the
* queue number.
*/
int idx;
struct perf_cpu cpu;
pid_t tid;
/* Guest machine pid and VCPU, valid only if machine_pid is non-zero */
pid_t machine_pid;
struct perf_cpu vcpu;
/*
* Per-thread, and global event counts are mutually exclusive:
* Whilst it is possible to combine events into a group with differing
* values of PERF_SAMPLE_READ, it is not valid to have inconsistent
* values for `inherit`. Therefore it is not possible to have a
* situation where a per-thread event is sampled as a global event;
* all !inherit groups are global, and all groups where the sampling
* event is inherit + PERF_SAMPLE_READ will be per-thread. Any event
* that is part of such a group that is inherit but not PERF_SAMPLE_READ
* will be read as per-thread. If such an event can also trigger a
* sample (such as with sample_period > 0) then it will not cause
* `read_format` to be included in its PERF_RECORD_SAMPLE, and
* therefore will not expose the per-thread group members as global.
*/
union {
/*
* Holds total ID period value for PERF_SAMPLE_READ processing
* (when period is not per-thread).
*/
u64 period;
/*
* Holds total ID period value for PERF_SAMPLE_READ processing
* (when period is per-thread).
*/
struct hlist_head periods[PERF_SAMPLE_ID__HLIST_SIZE];
};
};
struct perf_evsel {
struct list_head node;
struct perf_event_attr attr;
/** The commonly used cpu map of CPUs the event should be opened upon, etc. */
struct perf_cpu_map *cpus;
/**
* The cpu map read from the PMU. For core PMUs this is the list of all
* CPUs the event can be opened upon. For other PMUs this is the default
* cpu map for opening the event on, for example, the first CPU on a
* socket for an uncore event.
*/
struct perf_cpu_map *pmu_cpus;
struct perf_thread_map *threads;
struct xyarray *fd;
struct xyarray *mmap;
struct xyarray *sample_id;
u64 *id;
u32 ids;
struct perf_evsel *leader;
/* For events where the read_format value is per-thread rather than
* global, stores the per-thread cumulative period */
struct list_head per_stream_periods;
/* parse modifier helper */
int nr_members;
/*
* system_wide is for events that need to be on every CPU, irrespective
* of user requested CPUs or threads. Tha main example of this is the
* dummy event. Map propagation will set cpus for this event to all CPUs
* as software PMU events like dummy, have a CPU map that is empty.
*/
bool system_wide;
/*
* Some events, for example uncore events, require a CPU.
* i.e. it cannot be the 'any CPU' value of -1.
*/
bool requires_cpu;
/** Is the PMU for the event a core one? Effects the handling of own_cpus. */
bool is_pmu_core;
/** Does the evsel on read on the first CPU index such as tool time events? */
bool reads_only_on_cpu_idx0;
int idx;
};
void perf_evsel__init(struct perf_evsel *evsel, struct perf_event_attr *attr,
int idx);
void perf_evsel__exit(struct perf_evsel *evsel);
int perf_evsel__alloc_fd(struct perf_evsel *evsel, int ncpus, int nthreads);
void perf_evsel__close_fd(struct perf_evsel *evsel);
void perf_evsel__free_fd(struct perf_evsel *evsel);
int perf_evsel__read_size(struct perf_evsel *evsel);
int perf_evsel__apply_filter(struct perf_evsel *evsel, const char *filter);
int perf_evsel__alloc_id(struct perf_evsel *evsel, int ncpus, int nthreads);
void perf_evsel__free_id(struct perf_evsel *evsel);
bool perf_evsel__attr_has_per_thread_sample_period(struct perf_evsel *evsel);
u64 *perf_sample_id__get_period_storage(struct perf_sample_id *sid, u32 tid,
bool per_thread);
#endif /* __LIBPERF_INTERNAL_EVSEL_H */
@@ -0,0 +1,14 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LIBPERF_INTERNAL_LIB_H
#define __LIBPERF_INTERNAL_LIB_H
#include <sys/types.h>
extern unsigned int page_size;
ssize_t readn(int fd, void *buf, size_t n);
ssize_t writen(int fd, const void *buf, size_t n);
ssize_t preadn(int fd, void *buf, size_t n, off_t offs);
#endif /* __LIBPERF_INTERNAL_CPUMAP_H */
@@ -0,0 +1,60 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LIBPERF_INTERNAL_MMAP_H
#define __LIBPERF_INTERNAL_MMAP_H
#include <linux/compiler.h>
#include <linux/refcount.h>
#include <linux/types.h>
#include <stdbool.h>
#include <internal/cpumap.h>
/* perf sample has 16 bits size limit */
#define PERF_SAMPLE_MAX_SIZE (1 << 16)
struct perf_mmap;
struct perf_counts_values;
typedef void (*libperf_unmap_cb_t)(struct perf_mmap *map);
/**
* struct perf_mmap - perf's ring buffer mmap details
*
* @refcnt - e.g. code using PERF_EVENT_IOC_SET_OUTPUT to share this
*/
struct perf_mmap {
void *base;
int mask;
int fd;
struct perf_cpu cpu;
refcount_t refcnt;
u64 prev;
u64 start;
u64 end;
bool overwrite;
u64 flush;
libperf_unmap_cb_t unmap_cb;
void *event_copy;
size_t event_copy_sz;
struct perf_mmap *next;
};
struct perf_mmap_param {
int prot;
int mask;
};
size_t perf_mmap__mmap_len(struct perf_mmap *map);
void perf_mmap__init(struct perf_mmap *map, struct perf_mmap *prev,
bool overwrite, libperf_unmap_cb_t unmap_cb);
int perf_mmap__mmap(struct perf_mmap *map, struct perf_mmap_param *mp,
int fd, struct perf_cpu cpu);
void perf_mmap__munmap(struct perf_mmap *map);
void perf_mmap__get(struct perf_mmap *map);
void perf_mmap__put(struct perf_mmap *map);
u64 perf_mmap__read_head(struct perf_mmap *map);
int perf_mmap__read_self(struct perf_mmap *map, struct perf_counts_values *count);
#endif /* __LIBPERF_INTERNAL_MMAP_H */
@@ -0,0 +1,113 @@
/* SPDX-License-Identifier: (LGPL-2.1 OR BSD-2-Clause) */
#ifndef __LIBPERF_INTERNAL_RC_CHECK_H
#define __LIBPERF_INTERNAL_RC_CHECK_H
#include <stdlib.h>
#include <linux/zalloc.h>
/*
* Enable reference count checking implicitly with leak checking, which is
* integrated into address sanitizer.
*/
#if defined(__SANITIZE_ADDRESS__) || defined(LEAK_SANITIZER) || defined(ADDRESS_SANITIZER)
#define REFCNT_CHECKING 1
#elif defined(__has_feature)
#if __has_feature(address_sanitizer) || __has_feature(leak_sanitizer)
#define REFCNT_CHECKING 1
#endif
#endif
/*
* Shared reference count checking macros.
*
* Reference count checking is an approach to sanitizing the use of reference
* counted structs. It leverages address and leak sanitizers to make sure gets
* are paired with a put. Reference count checking adds a malloc-ed layer of
* indirection on a get, and frees it on a put. A missed put will be reported as
* a memory leak. A double put will be reported as a double free. Accessing
* after a put will cause a use-after-free and/or a segfault.
*/
#ifndef REFCNT_CHECKING
/* Replaces "struct foo" so that the pointer may be interposed. */
#define DECLARE_RC_STRUCT(struct_name) \
struct struct_name
/* Declare a reference counted struct variable. */
#define RC_STRUCT(struct_name) struct struct_name
/*
* Interpose the indirection. Result will hold the indirection and object is the
* reference counted struct.
*/
#define ADD_RC_CHK(result, object) (result = object, object)
/* Strip the indirection layer. */
#define RC_CHK_ACCESS(object) object
/* Frees the object and the indirection layer. */
#define RC_CHK_FREE(object) free(object)
/* A get operation adding the indirection layer. */
#define RC_CHK_GET(result, object) ADD_RC_CHK(result, object)
/* A put operation removing the indirection layer. */
#define RC_CHK_PUT(object) {}
/* Pointer equality when the indirection may or may not be there. */
#define RC_CHK_EQUAL(object1, object2) (object1 == object2)
#else
/* Replaces "struct foo" so that the pointer may be interposed. */
#define DECLARE_RC_STRUCT(struct_name) \
struct original_##struct_name; \
struct struct_name { \
struct original_##struct_name *orig; \
}; \
struct original_##struct_name
/* Declare a reference counted struct variable. */
#define RC_STRUCT(struct_name) struct original_##struct_name
/*
* Interpose the indirection. Result will hold the indirection and object is the
* reference counted struct.
*/
#define ADD_RC_CHK(result, object) \
( \
object ? (result = malloc(sizeof(*result)), \
result ? (result->orig = object, result) \
: (result = NULL, NULL)) \
: (result = NULL, NULL) \
)
/* Strip the indirection layer. */
#define RC_CHK_ACCESS(object) object->orig
/* Frees the object and the indirection layer. */
#define RC_CHK_FREE(object) \
do { \
zfree(&object->orig); \
free(object); \
} while(0)
/* A get operation adding the indirection layer. */
#define RC_CHK_GET(result, object) ADD_RC_CHK(result, (object ? object->orig : NULL))
/* A put operation removing the indirection layer. */
#define RC_CHK_PUT(object) \
do { \
if (object) { \
object->orig = NULL; \
free(object); \
} \
} while(0)
/* Pointer equality when the indirection may or may not be there. */
#define RC_CHK_EQUAL(object1, object2) (object1 == object2 || \
(object1 && object2 && object1->orig == object2->orig))
#endif
#endif /* __LIBPERF_INTERNAL_RC_CHECK_H */
@@ -0,0 +1,67 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LIBPERF_INTERNAL_TESTS_H
#define __LIBPERF_INTERNAL_TESTS_H
#include <stdio.h>
#include <unistd.h>
extern int tests_failed;
extern int tests_verbose;
static inline int get_verbose(char **argv, int argc)
{
int c;
int verbose = 0;
while ((c = getopt(argc, argv, "v")) != -1) {
switch (c)
{
case 'v':
verbose = 1;
break;
default:
break;
}
}
optind = 1;
return verbose;
}
#define __T_START \
do { \
tests_verbose = get_verbose(argv, argc); \
fprintf(stdout, "- running %s...", __FILE__); \
fflush(NULL); \
tests_failed = 0; \
} while (0)
#define __T_END \
do { \
if (tests_failed) \
fprintf(stdout, " FAILED (%d)\n", tests_failed); \
else \
fprintf(stdout, "OK\n"); \
} while (0)
#define __T(text, cond) \
do { \
if (!(cond)) { \
fprintf(stderr, "FAILED %s:%d %s\n", __FILE__, __LINE__, text); \
tests_failed++; \
return -1; \
} \
} while (0)
#define __T_VERBOSE(...) \
do { \
if (tests_verbose) { \
if (tests_verbose == 1) { \
fputc('\n', stderr); \
tests_verbose++; \
} \
fprintf(stderr, ##__VA_ARGS__); \
} \
} while (0)
#endif /* __LIBPERF_INTERNAL_TESTS_H */
@@ -0,0 +1,23 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LIBPERF_INTERNAL_THREADMAP_H
#define __LIBPERF_INTERNAL_THREADMAP_H
#include <linux/refcount.h>
#include <sys/types.h>
#include <unistd.h>
struct thread_map_data {
pid_t pid;
char *comm;
};
struct perf_thread_map {
refcount_t refcnt;
int nr;
int err_thread;
struct thread_map_data map[];
};
struct perf_thread_map *perf_thread_map__realloc(struct perf_thread_map *map, int nr);
#endif /* __LIBPERF_INTERNAL_THREADMAP_H */
@@ -0,0 +1,43 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef __LIBPERF_INTERNAL_XYARRAY_H
#define __LIBPERF_INTERNAL_XYARRAY_H
#include <linux/compiler.h>
#include <sys/types.h>
struct xyarray {
size_t row_size;
size_t entry_size;
size_t entries;
size_t max_x;
size_t max_y;
char contents[] __aligned(8);
};
struct xyarray *xyarray__new(int xlen, int ylen, size_t entry_size);
void xyarray__delete(struct xyarray *xy);
void xyarray__reset(struct xyarray *xy);
static inline void *__xyarray__entry(struct xyarray *xy, int x, int y)
{
return &xy->contents[x * xy->row_size + y * xy->entry_size];
}
static inline void *xyarray__entry(struct xyarray *xy, size_t x, size_t y)
{
if (x >= xy->max_x || y >= xy->max_y)
return NULL;
return __xyarray__entry(xy, x, y);
}
static inline int xyarray__max_y(struct xyarray *xy)
{
return xy->max_y;
}
static inline int xyarray__max_x(struct xyarray *xy)
{
return xy->max_x;
}
#endif /* __LIBPERF_INTERNAL_XYARRAY_H */