Skip to content

Commit c4a8526

Browse files
Yang Jihongnamhyung
authored andcommitted
perf data: Increase RLIMIT_NOFILE limit when open too many files in perf_data__create_dir()
If using parallel threads to collect data, perf record needs at least 6 fds per CPU. (one for sys_perf_event_open, four for pipe msg and ack of the pipe, see record__thread_data_open_pipes(), and one for open perf.data.XXX) For an environment with more than 100 cores, if perf record uses both `-a` and `--threads` options, it is easy to exceed the upper limit of the file descriptor number, when we run out of them try to increase the limits. Before: $ ulimit -n 1024 $ lscpu | grep 'On-line CPU(s)' On-line CPU(s) list: 0-159 $ perf record --threads -a sleep 1 Failed to create data directory: Too many open files After: $ ulimit -n 1024 $ lscpu | grep 'On-line CPU(s)' On-line CPU(s) list: 0-159 $ perf record --threads -a sleep 1 [ perf record: Woken up 1 times to write data ] [ perf record: Captured and wrote 0.394 MB perf.data (1576 samples) ] Signed-off-by: Yang Jihong <yangjihong1@huawei.com> Acked-by: Namhyung Kim <namhyung@kernel.org> Link: https://lore.kernel.org/r/20231013075945.698874-1-yangjihong1@huawei.com Signed-off-by: Namhyung Kim <namhyung@kernel.org>
1 parent 3f8b6e5 commit c4a8526

1 file changed

Lines changed: 12 additions & 0 deletions

File tree

tools/perf/util/data.c

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "util.h" // rm_rf_perf_data()
1818
#include "debug.h"
1919
#include "header.h"
20+
#include "evsel.h"
2021
#include <internal/lib.h>
2122

2223
static void close_dir(struct perf_data_file *files, int nr)
@@ -35,6 +36,7 @@ void perf_data__close_dir(struct perf_data *data)
3536

3637
int perf_data__create_dir(struct perf_data *data, int nr)
3738
{
39+
enum rlimit_action set_rlimit = NO_CHANGE;
3840
struct perf_data_file *files = NULL;
3941
int i, ret;
4042

@@ -54,11 +56,21 @@ int perf_data__create_dir(struct perf_data *data, int nr)
5456
goto out_err;
5557
}
5658

59+
retry_open:
5760
ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
5861
if (ret < 0) {
62+
/*
63+
* If using parallel threads to collect data,
64+
* perf record needs at least 6 fds per CPU.
65+
* When we run out of them try to increase the limits.
66+
*/
67+
if (errno == EMFILE && evsel__increase_rlimit(&set_rlimit))
68+
goto retry_open;
69+
5970
ret = -errno;
6071
goto out_err;
6172
}
73+
set_rlimit = NO_CHANGE;
6274

6375
file->fd = ret;
6476
}

0 commit comments

Comments
 (0)