- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
这是我的代码,perf_event_open 不能打开超过 7 个 fds
#include <errno.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/ioctl.h>
#include <linux/perf_event.h>
#include <asm/unistd.h>
static long
perf_event_open(struct perf_event_attr *hw_event, pid_t pid,
int cpu, int group_fd, unsigned long flags)
{
int ret;
ret = syscall(__NR_perf_event_open, hw_event, pid, cpu,
group_fd, flags);
return ret;
}
int
main(int argc, char **argv)
{
struct perf_event_attr pe;
long long count;
int fd,fd1,fd2,fd3,fd4,fd5,fd6,fd7,fd8;
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_INSTRUCTIONS;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd = perf_event_open(&pe, 0, -1, -1, 0);
if (fd == -1) {
fprintf(stderr, "Error opening leader %llx, errno:%d, reason:%s\n", pe.config, errno, strerror(errno));
exit(EXIT_FAILURE);
}
ioctl(fd, PERF_EVENT_IOC_RESET, 0);
ioctl(fd, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring instruction count for this printf\n");
ioctl(fd, PERF_EVENT_IOC_DISABLE, 0);
read(fd, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
// ----------------------------------------------------------------------
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_CPU_CYCLES;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd1 = perf_event_open(&pe, 0, -1, -1, 0);
if (fd1 == -1) {
fprintf(stderr, "Error opening leader %llx, errno:%d, reason:%s\n", pe.config, errno, strerror(errno));
exit(EXIT_FAILURE);
}
ioctl(fd1, PERF_EVENT_IOC_RESET, 0);
ioctl(fd1, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring cpu cycles count for this printf\n");
ioctl(fd1, PERF_EVENT_IOC_DISABLE, 0);
read(fd1, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_CACHE_REFERENCES;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd2 = perf_event_open(&pe, 0, -1, -1, 0);
if (fd2 == -1) {
fprintf(stderr, "Error opening leader %llx, errno:%d, reason:%s\n", pe.config, errno, strerror(errno));
exit(EXIT_FAILURE);
}
ioctl(fd2, PERF_EVENT_IOC_RESET, 0);
ioctl(fd2, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring cache references count for this printf\n");
ioctl(fd2, PERF_EVENT_IOC_DISABLE, 0);
read(fd2, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_CACHE_MISSES;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd3 = perf_event_open(&pe, 0, -1, -1, 0);
if (fd3 == -1) {
fprintf(stderr, "Error opening leader %llx, errno:%d, reason:%s\n", pe.config, errno, strerror(errno));
exit(EXIT_FAILURE);
}
ioctl(fd3, PERF_EVENT_IOC_RESET, 0);
ioctl(fd3, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring cache misses count for this printf\n");
ioctl(fd3, PERF_EVENT_IOC_DISABLE, 0);
read(fd3, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_BRANCH_INSTRUCTIONS;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd4 = perf_event_open(&pe, 0, -1, -1, 0);
if (fd4 == -1) {
fprintf(stderr, "Error opening leader %llx, errno:%d, reason:%s\n", pe.config, errno, strerror(errno));
exit(EXIT_FAILURE);
}
ioctl(fd4, PERF_EVENT_IOC_RESET, 0);
ioctl(fd4, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring branch instructions count for this printf\n");
ioctl(fd4, PERF_EVENT_IOC_DISABLE, 0);
read(fd4, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_BRANCH_MISSES;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd5 = perf_event_open(&pe, 0, -1, -1, 0);
if (fd5 == -1) {
fprintf(stderr, "Error opening leader %llx, errno:%d, reason:%s\n", pe.config, errno, strerror(errno));
exit(EXIT_FAILURE);
}
ioctl(fd5, PERF_EVENT_IOC_RESET, 0);
ioctl(fd5, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring branch misses count for this printf\n");
ioctl(fd5, PERF_EVENT_IOC_DISABLE, 0);
read(fd5, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_BUS_CYCLES;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd6 = perf_event_open(&pe, 0, -1, -1, 0);
if (fd6 == -1) {
fprintf(stderr, "Error opening leader %llx, errno:%d, reason:%s\n", pe.config, errno, strerror(errno));
exit(EXIT_FAILURE);
}
ioctl(fd6, PERF_EVENT_IOC_RESET, 0);
ioctl(fd6, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring bus cycles count for this printf\n");
ioctl(fd6, PERF_EVENT_IOC_DISABLE, 0);
read(fd6, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_STALLED_CYCLES_FRONTEND;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd7 = perf_event_open(&pe, 0, -1, -1, 0);
if (fd7 == -1) {
fprintf(stderr, "Error opening leader %llx, errno:%d, reason:%s\n", pe.config, errno, strerror(errno));
exit(EXIT_FAILURE);
}
ioctl(fd7, PERF_EVENT_IOC_RESET, 0);
ioctl(fd7, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring stalled cycles frontend count for this printf\n");
ioctl(fd7, PERF_EVENT_IOC_DISABLE, 0);
read(fd7, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
// ----------------------------------------------------------------------
// ----------------------------------------------------------------------
memset(&pe, 0, sizeof(struct perf_event_attr));
pe.type = PERF_TYPE_HARDWARE;
pe.size = sizeof(struct perf_event_attr);
pe.config = PERF_COUNT_HW_STALLED_CYCLES_BACKEND;
pe.disabled = 1;
pe.exclude_kernel = 1;
pe.exclude_hv = 1;
fd8 = perf_event_open(&pe, 0, -1, -1, 0);
if (fd8 == -1) {
fprintf(stderr, "Error opening leader %llx, errno:%d, reason:%s\n", pe.config, errno, strerror(errno));
exit(EXIT_FAILURE);
}
ioctl(fd8, PERF_EVENT_IOC_RESET, 0);
ioctl(fd8, PERF_EVENT_IOC_ENABLE, 0);
printf("Measuring stalled cycles backend count for this printf\n");
ioctl(fd8, PERF_EVENT_IOC_DISABLE, 0);
read(fd8, &count, sizeof(long long));
printf("Used %lld instructions\n", count);
// ----------------------------------------------------------------------
close(fd);
close(fd1);
close(fd2);
close(fd3);
close(fd4);
close(fd5);
close(fd6);
close(fd7);
close(fd8);
}
这里是输出,只有前 7 个 perf 事件被打开,其余的将失败并返回 errno 2。
Measuring instruction count for this printf
Used 3857 instructions
Measuring cpu cycles count for this printf
Used 1546 instructions
Measuring cache references count for this printf
Used 17 instructions
Measuring cache misses count for this printf
Used 0 instructions
Measuring branch instructions count for this printf
Used 194 instructions
Measuring branch misses count for this printf
Used 12 instructions
Measuring bus cycles count for this printf
Used 34 instructions
Error opening leader 7, errno:2, reason:No such file or directory
我在 linux 手册页中找不到关于此限制的任何说明,请告诉我发生了什么,非常感谢。我的操作系统版本是 centos 6.5 。
最佳答案
这与打开的 perf 事件的数量无关,因为您只需删除前 7 个 perf_event_open
调用即可轻松找到。
没有这样的文件或目录 (ENOENT
) 与太多打开的文件 (EMFILE
). perf_event_open
的联机帮助页解释了返回码:
ENOENT
Returned if the type setting is not valid. This error is also returned for some unsupported generic events.
该事件在您的系统(使用给定的内核)上根本不可用。 This answer讨论了 PERF_COUNT_HW_STALLED_CYCLES_*END
事件在现代 CPU 上的可用性。
关于c - perf_event_open 不能打开超过 7 个 fd,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47829826/
我正在尝试访问 PMU 硬件性能计数器详细信息,主要关注 CPU 周期。下面是它的 C 代码。 #include #include #include #inclu
有谁知道如何设置 perf_event_attr 结构,通过 perf_event_open() 触发 PMU 监控多个(类型)事件? 像 perf record -e cycles,faults l
我想计算(或多或少)一段代码的确切指令数量。此外,我希望在通过特定数量的指令后收到信号。 为此,我使用了由 perf_event_open . 我正在使用联机帮助页建议的第二种方式来实现溢出信号: S
我正在尝试分析具有相当复杂结构的现有应用程序。现在我正在使用 perf_event_open和需要的 ioctl呼吁启用我感兴趣的事件。 manpage保持不变 PERF_COUNT_HW_INSTR
我正在试验 PERF_EVENTS,这是 Linux 内核提供的性能事件接口(interface)。我通过 perf_event_open 系统调用成功获取了性能参数(cpu 周期,...)。 lon
我运行以下调用 perf_event_open 系统调用的程序:Linux sama-desktop 3.18.0-20-rpi2 #21-Ubuntu SMP PREEMPT Sun Apr 5 0
我正在尝试使用 Linux 上的 perf_event 来监视进程的执行并对其退出指令的数量进行采样。我想随着时间的推移以固定的、恒定的采样周期对过程进行采样。 一切似乎都很好,我已经成功编写了代码,
我正在使用 perf_event_open 获取样本。我试着让每个人都说到点子上。但是 perf_event_open 不够快。我尝试使用以下命令更改采样率: echo 10000000 > /pro
在环形缓冲区中,我们可以只为 PERF_RECORD_SAMPLE 检索调用链,还是也可以为其他记录类型检索调用链? perf_event_open 的手册页仅明确说明调用链可用于 PERF_RECO
这是我的代码,perf_event_open 不能打开超过 7 个 fds #include #include #include #include #include #include #i
我正在为我编写的软件设置分析,但我无法使用 perf_event_open 获得上下文切换计数。 为了测试问题,我也尝试使用 perf_event_open man_page 上提供的示例代码。使用
我的目标是编写一些代码以在某个时间间隔记录所有 CPU 的当前调用堆栈。本质上,我想做与 perf record 相同的事情,但我自己使用 perf_event_open。 根据联机帮助页,我似乎需要
我正在尝试使用perf_event_open syscall编写一段代码。但是我不了解在内存映射中返回的结构中id和stream_id字段之间的真正区别。 这种结构是这样的(来自perf_event_
我是一名优秀的程序员,十分优秀!