[<prev] [next>] [<thread-prev] [thread-next>] [day] [month] [year] [list]
Message-ID: <zulbpqdmui664qflvo3fcw3wnuailsiwkjtfu4xr37kutdoagy@54z7s6hud2vf>
Date: Thu, 31 Aug 2023 08:51:15 +0200
From: Maciej Wieczór-Retman
<maciej.wieczor-retman@...el.com>
To: "Luck, Tony" <tony.luck@...el.com>
CC: "Yu, Fenghua" <fenghua.yu@...el.com>,
"Chatre, Reinette" <reinette.chatre@...el.com>,
Peter Newman <peternewman@...gle.com>,
"Jonathan Corbet" <corbet@....net>,
Shuah Khan <skhan@...uxfoundation.org>,
"x86@...nel.org" <x86@...nel.org>,
Shaopeng Tan <tan.shaopeng@...itsu.com>,
James Morse <james.morse@....com>,
Jamie Iles <quic_jiles@...cinc.com>,
"Babu Moger" <babu.moger@....com>,
Randy Dunlap <rdunlap@...radead.org>,
"linux-kernel@...r.kernel.org" <linux-kernel@...r.kernel.org>,
"linux-doc@...r.kernel.org" <linux-doc@...r.kernel.org>,
"patches@...ts.linux.dev" <patches@...ts.linux.dev>
Subject: Re: [PATCH v5 8/8] selftests/resctrl: Adjust effective L3 cache size
when SNC enabled
On 2023-08-30 at 15:43:05 +0000, Luck, Tony wrote:
>> >+static int count_sys_bitmap_bits(char *name)
>> >+{
>> >+ FILE *fp = fopen(name, "r");
>> >+ int count = 0, c;
>> >+
>> >+ if (!fp)
>> >+ return 0;
>> >+
>> >+ while ((c = fgetc(fp)) != EOF) {
>> >+ if (!isxdigit(c))
>> >+ continue;
>> >+ switch (c) {
>> >+ case 'f':
>> >+ count++;
>> >+ case '7': case 'b': case 'd': case 'e':
>> >+ count++;
>> >+ case '3': case '5': case '6': case '9': case 'a': case 'c':
>> >+ count++;
>> >+ case '1': case '2': case '4': case '8':
>> >+ count++;
>> >+ }
>> >+ }
>> >+ fclose(fp);
>> >+
>> >+ return count;
>> >+}
>> >+
>>
>> The resctrl selftest has a function for counting bits, could it be used
>> here instead of the switch statement like this for example?
>>
>> count = count_bits(c);
>>
>> Or is there some reason this wouldn't be a good fit here?
>
>Thanks for looking at my patch.
>
>That count_bits() function is doing so with input from an "unsigned long"
>argument. My function is parsing the string result from a sysfs file which
>might look like this:
>
>$ cat shared_cpu_map
>0000,00000fff,ffffff00,0000000f,ffffffff
>
>To use count_bits() I'd have to use something like strtol() on each of the
>comma separated fields first to convert from ascii strings to binary
>values to feed into count_bits().
I missed they are being read as characters and not bytes, sorry.
Out of curiosity, what about using fscanf instead of fgetc? With the
format being %x and reading one byte at the time. Then instead of
isxdigit just checking if the read number was bigger than 0xF.
I also remembered there is a gcc (and I think clang has it as well)
builtin function that returns the number of set bits in a number.
So it would look like this:
while ((fscanf(fp, "%x", c)) != EOF ) {
if (c > 0xF)
continue;
count = __builtin_popcount(c);
}
Are there some problems with an approach like that?
--
Kind regards
Maciej Wieczór-Retman
Powered by blists - more mailing lists