linux-rt-users.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: John Kacur <jkacur@redhat.com>
To: rt-users <linux-rt-users@vger.kernel.org>
Cc: Clark Williams <williams@redhat.com>, John Kacur <jkacur@redhat.com>
Subject: [PATCH 4/4] rt-tests: cyclictest: Fix -t without a user specified [NUM]
Date: Wed,  4 Mar 2020 16:55:48 -0500	[thread overview]
Message-ID: <20200304215548.13079-4-jkacur@redhat.com> (raw)
In-Reply-To: <20200304215548.13079-1-jkacur@redhat.com>

Fix -t without a specified [NUM] to run the same number of threads as
available processors.

Currently it runs the same number of threads as cpus on a system.
However, if cyclictest is contrained to run on a smaller set of cpus
either because of a container or a user specified affinity or a
combination of the two, then the actual number of available cpus is
smaller. -t should reflect that.

Signed-off-by: John Kacur <jkacur@redhat.com>
---
 src/cyclictest/cyclictest.c | 36 ++++++++++++++++++++++++++++++++++--
 1 file changed, 34 insertions(+), 2 deletions(-)

diff --git a/src/cyclictest/cyclictest.c b/src/cyclictest/cyclictest.c
index f8df4c6954ad..c5f1fd46567a 100644
--- a/src/cyclictest/cyclictest.c
+++ b/src/cyclictest/cyclictest.c
@@ -1040,6 +1040,35 @@ static unsigned int is_cpumask_zero(const struct bitmask *mask)
 	return (rt_numa_bitmask_count(mask) == 0);
 }
 
+/* Get available cpus according to getaffinity or according to the
+ * intersection of getaffinity and the user specified affinity
+ * in the case of AFFINITY_SPECIFIED, the function has to be called
+ * after the call to parse_cpumask made in process_options()
+ */
+static int get_available_cpus(void)
+{
+	int num_cpus = 0;
+	int res;
+	pthread_t thread;
+	cpu_set_t cpuset;
+
+
+	if (affinity_mask != NULL) {
+		num_cpus = rt_numa_bitmask_count(affinity_mask);
+	} else {
+		CPU_ZERO(&cpuset);
+		thread = pthread_self();
+		res = pthread_getaffinity_np(thread, sizeof(cpu_set_t), &cpuset);
+		if (res != 0) {
+			fatal("pthread_getaffinity_np failed: %s\n", strerror(res));
+		}
+		num_cpus = CPU_COUNT(&cpuset);
+	}
+
+	return num_cpus;
+
+}
+
 /* cpu_for_thread AFFINITY_SPECIFIED */
 static int cpu_for_thread_sp(int thread_num, int max_cpus)
 {
@@ -1358,7 +1387,7 @@ static void process_options (int argc, char *argv[], int max_cpus)
 			if (numa)
 				fatal("numa and smp options are mutually exclusive\n");
 			smp = 1;
-			num_threads = max_cpus;
+			num_threads = -1; /* update after parsing */
 			setaffinity = AFFINITY_USEALL;
 			break;
 		case 't':
@@ -1372,7 +1401,7 @@ static void process_options (int argc, char *argv[], int max_cpus)
 			else if (optind<argc && atoi(argv[optind]))
 				num_threads = atoi(argv[optind]);
 			else
-				num_threads = max_cpus;
+				num_threads = -1; /* update after parsing */
 			break;
 		case OPT_TRIGGER:
 			trigger = atoi(optarg);
@@ -1485,6 +1514,9 @@ static void process_options (int argc, char *argv[], int max_cpus)
 	if (priority < 0 || priority > 99)
 		error = 1;
 
+	if (num_threads == -1)
+		num_threads = get_available_cpus();
+
 	if (priospread && priority == 0) {
 		fprintf(stderr, "defaulting realtime priority to %d\n",
 			num_threads+1);
-- 
2.20.1


      parent reply	other threads:[~2020-03-04 21:56 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-03-04 21:55 [PATCH 1/4] rt-tests: cyclictest: Remove HAVE_PARSE_CPUSTRING_ALL John Kacur
2020-03-04 21:55 ` [PATCH 2/4] rt-tests: cyclictest: Remove support for compiling without NUMA John Kacur
2020-03-04 21:55 ` [PATCH 3/4] rt-tests: cyclictest: Make the affinity mask apply to the main thread too John Kacur
2020-03-04 21:55 ` John Kacur [this message]

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=20200304215548.13079-4-jkacur@redhat.com \
    --to=jkacur@redhat.com \
    --cc=linux-rt-users@vger.kernel.org \
    --cc=williams@redhat.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 a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).