Skip to content

Commit

Permalink
core-helper: add file read/discard helpers to read all data from a file
Browse files Browse the repository at this point in the history
Add helpers to read and discard all the data in a file, useful for exercising
the reading of large /proc and /sys files.

Signed-off-by: Colin Ian King <[email protected]>
  • Loading branch information
ColinIanKing committed Oct 13, 2024
1 parent d0135ea commit 29a24a7
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 0 deletions.
39 changes: 39 additions & 0 deletions core-helper.c
Original file line number Diff line number Diff line change
Expand Up @@ -2073,6 +2073,26 @@ ssize_t stress_system_write(
return ret;
}

/*
* stress_system_discard()
* read and discard contents of a given file
*/
ssize_t stress_system_discard(const char *path)
{
int fd;
ssize_t ret;

if (UNLIKELY(!path))
return -EINVAL;
fd = open(path, O_RDONLY);
if (UNLIKELY(fd < 0))
return -errno;
ret = stress_read_discard(fd);
(void)close(fd);

return ret;
}

/*
* stress_system_read()
* read a buffer from a /sys or /proc entry
Expand Down Expand Up @@ -3295,6 +3315,25 @@ int stress_get_unused_uid(uid_t *uid)
}
#endif

/*
* stress_read_discard(cont int fd)
* read and discard contents of file fd
*/
ssize_t stress_read_discard(const int fd)
{
ssize_t rbytes = 0, ret;

do {
char buffer[4096];

ret = read(fd, buffer, sizeof(buffer));
if (ret > 0)
rbytes += ret;
} while (ret > 0);

return rbytes;
}

/*
* stress_read_buffer()
* In addition to read() this function makes sure all bytes have been
Expand Down
2 changes: 2 additions & 0 deletions core-helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ extern WARN_UNUSED int stress_cache_alloc(const char *name) NONNULL(1);
extern void stress_cache_free(void);
extern ssize_t stress_system_write(const char *path, const char *buf,
const size_t buf_len);
extern ssize_t stress_system_discard(const char *path);
extern WARN_UNUSED ssize_t stress_system_read(const char *path, char *buf,
const size_t buf_len);
extern WARN_UNUSED bool stress_is_prime64(const uint64_t n);
Expand Down Expand Up @@ -142,6 +143,7 @@ extern WARN_UNUSED int stress_dirent_list_prune(struct dirent **dlist, const int
extern WARN_UNUSED bool stress_warn_once_hash(const char *filename, const int line) NONNULL(1);
extern WARN_UNUSED uint16_t stress_ipv4_checksum(uint16_t *ptr, const size_t sz);
extern WARN_UNUSED int stress_get_unused_uid(uid_t *uid);
extern ssize_t stress_read_discard(const int fd);
extern WARN_UNUSED ssize_t stress_read_buffer(const int fd, void* buffer,
const ssize_t size, const bool ignore_sig_eintr);
extern WARN_UNUSED ssize_t stress_write_buffer(const int fd, const void* buffer,
Expand Down

0 comments on commit 29a24a7

Please sign in to comment.