tools/perf/util/data.c

Source file repositories/reference/linux-study-clean/tools/perf/util/data.c

File Facts

System
Linux kernel
Corpus path
tools/perf/util/data.c
Extension
.c
Size
11335 bytes
Lines
581
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

if (file->fptr) {
			fclose(file->fptr);
			file->fptr = NULL;
		}
	} else {
		close(file->fd);
		file->fd = -1;
	}
	zfree(&file->path);
}

static void close_dir(struct perf_data_file *files, int nr)
{
	while (--nr >= 0)
		perf_data_file__close(&files[nr]);

	free(files);
}

void perf_data__close_dir(struct perf_data *data)
{
	close_dir(data->dir.files, data->dir.nr);
	data->dir.files = NULL;
	data->dir.nr = 0;
}

int perf_data__create_dir(struct perf_data *data, int nr)
{
	enum rlimit_action set_rlimit = NO_CHANGE;
	struct perf_data_file *files = NULL;
	int i, ret;

	if (WARN_ON(!data->is_dir))
		return -EINVAL;

	files = calloc(nr, sizeof(*files));
	if (!files)
		return -ENOMEM;

	for (i = 0; i < nr; i++) {
		struct perf_data_file *file = &files[i];

		ret = asprintf(&file->path, "%s/data.%d", data->path, i);
		if (ret < 0) {
			ret = -ENOMEM;
			goto out_err;
		}

retry_open:
		ret = open(file->path, O_RDWR|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);
		if (ret < 0) {
			/*
			 * If using parallel threads to collect data,
			 * perf record needs at least 6 fds per CPU.
			 * When we run out of them try to increase the limits.
			 */
			if (errno == EMFILE && rlimit__increase_nofile(&set_rlimit))
				goto retry_open;

			ret = -errno;
			goto out_err;
		}
		set_rlimit = NO_CHANGE;

		file->fd = ret;
	}

	data->dir.version = PERF_DIR_VERSION;
	data->dir.files   = files;
	data->dir.nr      = nr;
	return 0;

out_err:
	close_dir(files, i);
	return ret;
}

int perf_data__open_dir(struct perf_data *data)
{
	struct perf_data_file *files = NULL;
	struct dirent *dent;
	int ret = -1;
	DIR *dir;
	int nr = 0;

	/*
	 * Directory containing a single regular perf data file which is already
	 * open, means there is nothing more to do here.
	 */
	if (perf_data__is_single_file(data))

Annotation

Implementation Notes