Linux-kselftest Archive on lore.kernel.org
 help / color / Atom feed
From: Andrew Jones <drjones@redhat.com>
To: Ben Gardon <bgardon@google.com>
Cc: linux-kernel@vger.kernel.org, kvm@vger.kernel.org,
	linux-kselftest@vger.kernel.org,
	Paolo Bonzini <pbonzini@redhat.com>,
	Cannon Matthews <cannonmatthews@google.com>,
	Peter Xu <peterx@redhat.com>, Peter Shier <pshier@google.com>,
	Oliver Upton <oupton@google.com>
Subject: Re: [PATCH v4 04/10] KVM: selftests: Add memory size parameter to the demand paging test
Date: Fri, 24 Jan 2020 11:01:47 +0100
Message-ID: <20200124100147.p5tlpsgu6phauygv@kamzik.brq.redhat.com> (raw)
In-Reply-To: <20200123180436.99487-5-bgardon@google.com>

> diff --git a/tools/testing/selftests/kvm/lib/test_util.c b/tools/testing/selftests/kvm/lib/test_util.c
> new file mode 100644
> index 0000000000000..706e0f963a44b
> --- /dev/null
> +++ b/tools/testing/selftests/kvm/lib/test_util.c
> @@ -0,0 +1,61 @@
> +// SPDX-License-Identifier: GPL-2.0-only
> +/*
> + * tools/testing/selftests/kvm/lib/test_util.c
> + *
> + * Copyright (C) 2020, Google LLC.
> + */
> +
> +#include "test_util.h"
> +
> +#include <ctype.h>
> +
> +/*
> + * Parses "[0-9]+[kmgt]?".
> + */
> +size_t parse_size(const char *size)
> +{
> +	size_t len = strlen(size);
> +	size_t i;
> +	size_t scale_shift = 0;
> +	size_t base;
> +
> +	TEST_ASSERT(len > 0, "Need at least 1 digit in '%s'", size);
> +
> +	/* Find the first letter in the string, indicating scale. */
> +	for (i = 0; i < len; i++) {
> +		if (!isdigit(size[i])) {
> +			TEST_ASSERT(i > 0, "Need at least 1 digit in '%s'",
> +				    size);
> +			TEST_ASSERT(i == len - 1,
> +				    "Expected letter at the end in '%s'.",
> +				    size);
> +			switch (tolower(size[i])) {
> +			case 't':
> +				scale_shift = 40;
> +				break;
> +			case 'g':
> +				scale_shift = 30;
> +				break;
> +			case 'm':
> +				scale_shift = 20;
> +				break;
> +			case 'k':
> +				scale_shift = 10;
> +				break;
> +			default:
> +				TEST_ASSERT(false, "Unknown size letter %c",
> +					    size[i]);
> +			}
> +		}
> +	}
> +
> +	TEST_ASSERT(scale_shift < 8 * sizeof(size_t),
> +		    "Overflow parsing scale!");
> +
> +	base = atoi(size);

I'd use strtoull(size, NULL, 0), allowing the user to input full 0x...
sizes too. And, if the strtoull is done before the scale parsing, then
you could supply a non-null endptr and avoid the need for the for loop.

Thanks,
drew


  reply index

Thread overview: 35+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-01-23 18:04 [PATCH v4 00/10] Create a userfaultfd " Ben Gardon
2020-01-23 18:04 ` [PATCH v4 01/10] KVM: selftests: Create a " Ben Gardon
2020-01-24  9:49   ` Andrew Jones
2020-01-27  9:18   ` Thomas Huth
2020-01-23 18:04 ` [PATCH v4 02/10] KVM: selftests: Add demand paging content to the " Ben Gardon
2020-01-23 18:04 ` [PATCH v4 03/10] KVM: selftests: Add configurable demand paging delay Ben Gardon
2020-01-23 18:04 ` [PATCH v4 04/10] KVM: selftests: Add memory size parameter to the demand paging test Ben Gardon
2020-01-24 10:01   ` Andrew Jones [this message]
2020-01-23 18:04 ` [PATCH v4 05/10] KVM: selftests: Pass args to vCPU in global vCPU args struct Ben Gardon
2020-01-23 18:04 ` [PATCH v4 06/10] KVM: selftests: Add support for vcpu_args_set to aarch64 and s390x Ben Gardon
2020-01-24  9:03   ` Paolo Bonzini
2020-01-24  9:35     ` Andrew Jones
2020-01-24  9:44       ` Paolo Bonzini
2020-01-24 10:45       ` Andrew Jones
2020-01-24 18:33     ` Christian Borntraeger
2020-01-25  9:34   ` Paolo Bonzini
2020-01-27  8:38     ` Andrew Jones
2020-01-27  9:01   ` Thomas Huth
2020-01-23 18:04 ` [PATCH v4 07/10] KVM: selftests: Support multiple vCPUs in demand paging test Ben Gardon
2020-01-24 10:16   ` Andrew Jones
2020-01-24 10:49   ` Andrew Jones
2020-01-25  9:39     ` Paolo Bonzini
2020-01-27  8:39       ` Andrew Jones
2020-01-23 18:04 ` [PATCH v4 08/10] KVM: selftests: Time guest demand paging Ben Gardon
2020-01-24 10:21   ` Andrew Jones
2020-01-23 18:04 ` [PATCH v4 09/10] KVM: selftests: Stop memslot creation in KVM internal memslot region Ben Gardon
2020-01-24  8:58   ` Paolo Bonzini
2020-01-24 18:41     ` Ben Gardon
2020-01-25  9:37       ` Paolo Bonzini
2020-01-27 17:28         ` Ben Gardon
2020-01-23 18:04 ` [PATCH v4 10/10] KVM: selftests: Move memslot 0 above KVM internal memslots Ben Gardon
2020-01-24  9:01   ` Paolo Bonzini
2020-01-24 18:53     ` Ben Gardon
2020-01-27  9:42   ` Thomas Huth
2020-01-24  9:03 ` [PATCH v4 00/10] Create a userfaultfd demand paging test Paolo Bonzini

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=20200124100147.p5tlpsgu6phauygv@kamzik.brq.redhat.com \
    --to=drjones@redhat.com \
    --cc=bgardon@google.com \
    --cc=cannonmatthews@google.com \
    --cc=kvm@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=oupton@google.com \
    --cc=pbonzini@redhat.com \
    --cc=peterx@redhat.com \
    --cc=pshier@google.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

Linux-kselftest Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/linux-kselftest/0 linux-kselftest/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 linux-kselftest linux-kselftest/ https://lore.kernel.org/linux-kselftest \
		linux-kselftest@vger.kernel.org
	public-inbox-index linux-kselftest

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.linux-kselftest


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git