tools/testing/selftests/resctrl/resctrlfs.c

Source file repositories/reference/linux-study-clean/tools/testing/selftests/resctrl/resctrlfs.c

File Facts

System
Linux kernel
Corpus path
tools/testing/selftests/resctrl/resctrlfs.c
Extension
.c
Size
22030 bytes
Lines
1002
Domain
Support Tooling And Documentation
Bucket
tools
Inferred role
Support Tooling And Documentation: implementation source
Status
source implementation candidate

Why This File Exists

Repository support layer: documentation, build tooling, samples, user-space helper tools, generated initramfs support, licenses, and validation utilities.

Dependency Surface

Detected Declarations

Annotated Snippet

switch (c) {
		case 'f':
			count++;
			fallthrough;
		case '7': case 'b': case 'd': case 'e':
			count++;
			fallthrough;
		case '3': case '5': case '6': case '9': case 'a': case 'c':
			count++;
			fallthrough;
		case '1': case '2': case '4': case '8':
			count++;
			break;
		}
	}
	fclose(fp);

	return count;
}

static bool cpus_offline_empty(void)
{
	char offline_cpus_str[64];
	FILE *fp;

	fp = fopen("/sys/devices/system/cpu/offline", "r");
	if (!fp) {
		ksft_perror("Could not open /sys/devices/system/cpu/offline");
		return 0;
	}

	if (fscanf(fp, "%63s", offline_cpus_str) < 0) {
		if (!errno) {
			fclose(fp);
			return 1;
		}
		ksft_perror("Could not read /sys/devices/system/cpu/offline");
	}

	fclose(fp);

	return 0;
}

/*
 * Detect SNC by comparing #CPUs in node0 with #CPUs sharing LLC with CPU0.
 * If any CPUs are offline declare the detection as unreliable.
 */
int snc_nodes_per_l3_cache(void)
{
	int node_cpus, cache_cpus;
	static int snc_mode;

	if (!snc_mode) {
		snc_mode = 1;
		if (!cpus_offline_empty()) {
			ksft_print_msg("Runtime SNC detection unreliable due to offline CPUs.\n");
			ksft_print_msg("Setting SNC mode to disabled.\n");
			snc_unreliable = 1;
			return snc_mode;
		}
		node_cpus = count_sys_bitmap_bits("/sys/devices/system/node/node0/cpumap");
		cache_cpus = count_sys_bitmap_bits("/sys/devices/system/cpu/cpu0/cache/index3/shared_cpu_map");

		if (!node_cpus || !cache_cpus) {
			ksft_print_msg("Could not determine Sub-NUMA Cluster mode.\n");
			snc_unreliable = 1;
			return snc_mode;
		}
		snc_mode = cache_cpus / node_cpus;

		/*
		 * On some platforms (e.g. Hygon),
		 * cache_cpus < node_cpus, the calculated snc_mode is 0.
		 *
		 * Set snc_mode = 1 to indicate that SNC mode is not
		 * supported on the platform.
		 */
		if (!snc_mode)
			snc_mode = 1;

		if (snc_mode > 1)
			ksft_print_msg("SNC-%d mode discovered.\n", snc_mode);
	}

	return snc_mode;
}

/*
 * get_cache_size - Get cache size for a specified CPU

Annotation

Implementation Notes