/* First iteration to fill caches, second one to read them. */ for (c = 0; c < 2; c++) {
memset(buf, 0, TEST_FILE_SIZE);
size = dso__data_read_offset(dso, &machine, 10,
buf, TEST_FILE_SIZE);
dir = opendir(path);
TEST_ASSERT_VAL("failed to open fd directory", dir);
while ((dent = readdir(dir)) != NULL) { if (!strcmp(dent->d_name, ".") ||
!strcmp(dent->d_name, "..")) continue;
nr++;
}
closedir(dir); return nr - 1;
}
staticint dsos__create(int cnt, int size, struct dsos *dsos)
{ int i;
dsos__init(dsos);
for (i = 0; i < cnt; i++) { struct dso *dso; char *file = test_file(size);
TEST_ASSERT_VAL("failed to get dso file", file);
dso = dso__new(file);
TEST_ASSERT_VAL("failed to get dso", dso);
TEST_ASSERT_VAL("failed to add dso", !dsos__add(dsos, dso));
dso__put(dso);
}
staticint test__dso_data_cache(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{ struct machine machine; long nr_end, nr = open_files_cnt(); int dso_cnt, limit, i, fd;
/* Rest the internal dso open counter limit. */
reset_fd_limit();
memset(&machine, 0, sizeof(machine));
/* set as system limit */
limit = nr * 4;
TEST_ASSERT_VAL("failed to set file limit", !set_fd_limit(limit));
/* and this is now our dso open FDs limit */
dso_cnt = limit / 2;
TEST_ASSERT_VAL("failed to create dsos\n",
!dsos__create(dso_cnt, TEST_FILE_SIZE, &machine.dsos));
for (i = 0; i < (dso_cnt - 1); i++) { struct dso *dso = machine.dsos.dsos[i];
/* * Open dsos via dso__data_fd(), it opens the data * file and keep it open (unless open file limit).
*/
fd = dso__data_fd(dso, &machine);
TEST_ASSERT_VAL("failed to get fd", fd > 0);
if (i % 2) { #define BUFSIZE 10
u8 buf[BUFSIZE];
ssize_t n;
n = dso__data_read_offset(dso, &machine, 0, buf, BUFSIZE);
TEST_ASSERT_VAL("failed to read dso", n == BUFSIZE);
}
}
/* verify the first one is already open */
TEST_ASSERT_VAL("dsos[0] is not open", dso__data(machine.dsos.dsos[0])->fd != -1);
/* open +1 dso to reach the allowed limit */
fd = dso__data_fd(machine.dsos.dsos[i], &machine);
TEST_ASSERT_VAL("failed to get fd", fd > 0);
/* should force the first one to be closed */
TEST_ASSERT_VAL("failed to close dsos[0]", dso__data(machine.dsos.dsos[0])->fd == -1);
/* Make sure we did not leak any file descriptor. */
nr_end = open_files_cnt();
pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
TEST_ASSERT_VAL("failed leaking files", nr == nr_end); return 0;
}
staticlong new_limit(int count)
{ int fd = open("/dev/null", O_RDONLY); long ret = fd; if (count > 0)
ret = new_limit(--count);
close(fd); return ret;
}
staticint test__dso_data_reopen(struct test_suite *test __maybe_unused, int subtest __maybe_unused)
{ struct machine machine; long nr_end, nr = open_files_cnt(), lim = new_limit(3); int fd, fd_extra;
/* Rest the internal dso open counter limit. */
reset_fd_limit();
memset(&machine, 0, sizeof(machine));
/* * Test scenario: * - create 3 dso objects * - set process file descriptor limit to current * files count + 3 * - test that the first dso gets closed when we * reach the files count limit
*/
/* Make sure we are able to open 3 fds anyway */
TEST_ASSERT_VAL("failed to set file limit",
!set_fd_limit((lim)));
TEST_ASSERT_VAL("failed to create dsos\n",
!dsos__create(3, TEST_FILE_SIZE, &machine.dsos));
/* open dso_0 */
fd = dso__data_fd(dso_0, &machine);
TEST_ASSERT_VAL("failed to get fd", fd > 0);
/* open dso_1 */
fd = dso__data_fd(dso_1, &machine);
TEST_ASSERT_VAL("failed to get fd", fd > 0);
/* * open extra file descriptor and we just * reached the files count limit
*/
fd_extra = open("/dev/null", O_RDONLY);
TEST_ASSERT_VAL("failed to open extra fd", fd_extra > 0);
/* open dso_2 */
fd = dso__data_fd(dso_2, &machine);
TEST_ASSERT_VAL("failed to get fd", fd > 0);
/* * dso_0 should get closed, because we reached * the file descriptor limit
*/
TEST_ASSERT_VAL("failed to close dso_0", dso__data(dso_0)->fd == -1);
/* open dso_0 */
fd = dso__data_fd(dso_0, &machine);
TEST_ASSERT_VAL("failed to get fd", fd > 0);
/* * dso_1 should get closed, because we reached * the file descriptor limit
*/
TEST_ASSERT_VAL("failed to close dso_1", dso__data(dso_1)->fd == -1);
/* Make sure we did not leak any file descriptor. */
nr_end = open_files_cnt();
pr_debug("nr start %ld, nr stop %ld\n", nr, nr_end);
TEST_ASSERT_VAL("failed leaking files", nr == nr_end); return 0;
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.