From: Cyril Hrubis <chrubis@suse.cz>
To: Shirisha G <shirisha@linux.ibm.com>
Cc: ltp@lists.linux.it
Subject: Re: [LTP] [PATCH v2] Migrating the libhugetlbfs/testcases/slbpacaflush.c test
Date: Mon, 24 Jun 2024 13:57:13 +0200 [thread overview]
Message-ID: <ZnlfGVAO9-53sQ8z@yuki> (raw)
In-Reply-To: <20240416091107.783352-1-shirisha@linux.ibm.com>
Hi!
> +#define _GNU_SOURCE
> +#define SYSFS_CPU_ONLINE_FMT "/sys/devices/system/cpu/cpu%d/online"
> +#define MNTPOINT "hugetlbfs/"
> +#define SINGLE_CPU (CPU_SETSIZE/8)
> +#include "hugetlb.h"
> +
> +
> +#include <stdio.h>
> +#include <sched.h>
> +
> +
> +long hpage_size;
> +int fd;
> +void *p;
> +volatile unsigned long *q;
> +int online_cpus[2], err;
> +cpu_set_t cpu0, cpu1;
All these should be static.
> +void check_online_cpus(int online_cpus[], int nr_cpus_needed)
> +{
> + char cpu_state, path_buf[64];
> + int cpu_idx, fd, ret, i;
> +
> + cpu_idx = 0;
> +
> + if (get_nprocs() < nr_cpus_needed)
> + tst_brk(TCONF, "minimum %d online cpus are required", nr_cpus_needed);
> +
> + for (i = 0; i < get_nprocs_conf() && cpu_idx < nr_cpus_needed; i++) {
> + errno = 0;
> + sprintf(path_buf, SYSFS_CPU_ONLINE_FMT, i);
> + fd = open(path_buf, O_RDONLY);
> + if (fd < 0) {
> + /* If 'online' is absent, the cpu cannot be offlined */
> + if (errno == ENOENT) {
> + online_cpus[cpu_idx] = i;
> + cpu_idx++;
> + continue;
> + } else {
> + tst_res(TFAIL | TERRNO, "Unable to open %s", path_buf);
> + }
> + }
> +
> + ret = read(fd, &cpu_state, 1);
> + if (ret < 1)
> + tst_res(TFAIL | TERRNO, "Unable to read %s", path_buf);
> +
> + if (cpu_state == '1') {
> + online_cpus[cpu_idx] = i;
> + cpu_idx++;
> + }
> +
> + if (fd >= 0)
> + SAFE_CLOSE(fd);
> + }
> +
> + if (cpu_idx < nr_cpus_needed)
> + tst_brk(TCONF, "minimum %d online cpus were not found", nr_cpus_needed);
> +}
There seems to be an easier method, recently we needed to find online
CPU for the startvation tests and all you need to do is to get the
current thread affinity and then look for non-zero bits in that, see
commit:
commit 1800e635783b69cacdce9f654ecd730a8f30915b
Author: Edward Liaw via ltp <ltp@lists.linux.it>
Date: Wed Jun 19 16:28:07 2024 +0000
And I suppose that it would make sense to put this function in the test
library so taht we do not have to repeat it over in tests, but we would
have to make it return the actual number of CPUs found and do the
tst_brk(TCONF, ...) in the tests instead, so we would add:
unsigned int tst_get_online_cpus(int online_cpus[], unsigned int online_cpus_cnt);
And the test would do:
if (tst_get_online_cpus(online_cpus, 2) != 2)
tst_brK(TCONF, "Require at least 2 online CPUs.");
> +static void run_test(void)
> +{
> + check_online_cpus(online_cpus, 2);
> + CPU_ZERO(&cpu0);
> + CPU_SET(online_cpus[0], &cpu0);
> + CPU_ZERO(&cpu1);
> + CPU_SET(online_cpus[1], &cpu1);
> +
> + err = sched_setaffinity(getpid(), SINGLE_CPU, &cpu0);
> + if (err != 0)
> + tst_res(TFAIL | TERRNO, "sched_setaffinity(cpu%d)", online_cpus[0]);
> +
> + err = sched_setaffinity(getpid(), SINGLE_CPU, &cpu1);
> +
> + if (err != 0)
> + tst_res(TFAIL | TERRNO, "sched_setaffinity(cpu%d)", online_cpus[1]);
> + p = SAFE_MMAP(NULL, hpage_size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);
> +
> + err = sched_setaffinity(getpid(), SINGLE_CPU, &cpu0);
> + if (err != 0)
> + tst_res(TFAIL, "sched_setaffinity(cpu%d)", online_cpus[0]);
> + q = (volatile unsigned long *)(p + getpagesize());
> + *q = 0xdeadbeef;
I suppose that the test crashes here, when the entries are not
propagated, right?
> + tst_res(TPASS, "Test Passed inconclusive");
We usually print something as tst_res(TPASS, "Nothing bad happend, probably");
in the case that we haven't managed to crash the system.
Also we have to unmap the p here so that the test works properly with -i
parameter.
> +}
> +
> +static void setup(void)
> +{
> + hpage_size = tst_get_hugepage_size();
> + fd = tst_creat_unlinked(MNTPOINT, 0);
> +}
> +
> +void cleanup(void)
> +{
> + if (fd > 0)
> + SAFE_CLOSE(fd);
> +}
> +
> +static struct tst_test test = {
> + .needs_root = 1,
> + .mntpoint = MNTPOINT,
> + .needs_hugetlbfs = 1,
> + .needs_tmpdir = 1,
> + .setup = setup,
> + .cleanup = cleanup,
> + .test_all = run_test,
> + .hugepages = {1, TST_NEEDS},
> +};
> --
> 2.39.3
>
>
> --
> Mailing list info: https://lists.linux.it/listinfo/ltp
--
Cyril Hrubis
chrubis@suse.cz
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2024-06-24 11:57 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-04-16 9:11 [LTP] [PATCH v2] Migrating the libhugetlbfs/testcases/slbpacaflush.c test Shirisha G
2024-06-24 11:57 ` Cyril Hrubis [this message]
2025-08-22 5:51 ` Shirisha ganta
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=ZnlfGVAO9-53sQ8z@yuki \
--to=chrubis@suse.cz \
--cc=ltp@lists.linux.it \
--cc=shirisha@linux.ibm.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.