Re: [PATCH v5 8/8] selftests/resctrl: Adjust effective L3 cache size when SNC enabled

From: Maciej Wieczór-Retman
Date: Thu Aug 31 2023 - 02:51:41 EST


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