tools/perf/tests/api-io.c

Source file repositories/reference/linux-study-clean/tools/perf/tests/api-io.c

File Facts

System
Linux kernel
Corpus path
tools/perf/tests/api-io.c
Extension
.c
Size
6307 bytes
Lines
344
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

// SPDX-License-Identifier: GPL-2.0-only
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <limits.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "debug.h"
#include "tests.h"
#include <api/io.h>
#include <linux/kernel.h>
#include <linux/zalloc.h>

#define TEMPL "/tmp/perf-test-XXXXXX"

#define EXPECT_EQUAL(val, expected)                             \
do {								\
	if (val != expected) {					\
		pr_debug("%s:%d: %d != %d\n",			\
			__FILE__, __LINE__, val, expected);	\
		ret = -1;					\
	}							\
} while (0)

#define EXPECT_EQUAL64(val, expected)                           \
do {								\
	if (val != expected) {					\
		pr_debug("%s:%d: %lld != %lld\n",		\
			__FILE__, __LINE__, val, expected);	\
		ret = -1;					\
	}							\
} while (0)

static int make_test_file(char path[PATH_MAX], const char *contents)
{
	ssize_t contents_len = strlen(contents);
	int fd;

	strcpy(path, TEMPL);
	fd = mkstemp(path);
	if (fd < 0) {
		pr_debug("mkstemp failed");
		return -1;
	}
	if (write(fd, contents, contents_len) < contents_len) {
		pr_debug("short write");
		close(fd);
		unlink(path);
		return -1;
	}
	close(fd);
	return 0;
}

static int setup_test(char path[PATH_MAX], const char *contents,
		      size_t buf_size, struct io *io)
{
	if (make_test_file(path, contents))
		return -1;

	io->fd = open(path, O_RDONLY);
	if (io->fd < 0) {
		pr_debug("Failed to open '%s'\n", path);
		unlink(path);
		return -1;
	}
	io->buf = malloc(buf_size);
	if (io->buf == NULL) {
		pr_debug("Failed to allocate memory");
		close(io->fd);
		unlink(path);
		return -1;
	}
	io__init(io, io->fd, io->buf, buf_size);
	return 0;
}

static void cleanup_test(char path[PATH_MAX], struct io *io)
{
	zfree(&io->buf);
	close(io->fd);
	unlink(path);
}

static int do_test_get_char(const char *test_string, size_t buf_size)
{
	char path[PATH_MAX];

Annotation

Implementation Notes