xref: /mOS-networking-stack/core/src/cpu.c (revision 8a941c7e)
176404edcSAsim Jamshed #ifndef _GNU_SOURCE
276404edcSAsim Jamshed #define _GNU_SOURCE
376404edcSAsim Jamshed #endif
476404edcSAsim Jamshed 
576404edcSAsim Jamshed #include <stdio.h>
676404edcSAsim Jamshed #include <unistd.h>
776404edcSAsim Jamshed #include <errno.h>
876404edcSAsim Jamshed #include <sched.h>
976404edcSAsim Jamshed #include <sys/stat.h>
1076404edcSAsim Jamshed #include <sys/syscall.h>
1176404edcSAsim Jamshed #include <assert.h>
1276404edcSAsim Jamshed #ifndef DISABLE_NUMA
1376404edcSAsim Jamshed #include <numa.h>
1476404edcSAsim Jamshed #endif
1576404edcSAsim Jamshed 
1676404edcSAsim Jamshed #define MAX_FILE_NAME 1024
1776404edcSAsim Jamshed 
1876404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
1976404edcSAsim Jamshed int
GetNumCPUs()2076404edcSAsim Jamshed GetNumCPUs()
2176404edcSAsim Jamshed {
2276404edcSAsim Jamshed 	return sysconf(_SC_NPROCESSORS_ONLN);
2376404edcSAsim Jamshed }
2476404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
2576404edcSAsim Jamshed pid_t
Gettid()2676404edcSAsim Jamshed Gettid()
2776404edcSAsim Jamshed {
2876404edcSAsim Jamshed 	return syscall(__NR_gettid);
2976404edcSAsim Jamshed }
3076404edcSAsim Jamshed /*----------------------------------------------------------------------------*/
3176404edcSAsim Jamshed int
mtcp_core_affinitize(int cpu)3276404edcSAsim Jamshed mtcp_core_affinitize(int cpu)
3376404edcSAsim Jamshed {
3476404edcSAsim Jamshed #ifndef DISABLE_NUMA
3576404edcSAsim Jamshed 	struct bitmask *bmask;
3676404edcSAsim Jamshed #endif /* DISABLE_NUMA */
3776404edcSAsim Jamshed 	cpu_set_t cpus;
3876404edcSAsim Jamshed 	FILE *fp;
3976404edcSAsim Jamshed 	char sysfname[MAX_FILE_NAME];
4076404edcSAsim Jamshed 	int phy_id;
4176404edcSAsim Jamshed 	size_t n;
4276404edcSAsim Jamshed 	int ret;
4376404edcSAsim Jamshed 
4476404edcSAsim Jamshed 	n = GetNumCPUs();
4576404edcSAsim Jamshed 
4676404edcSAsim Jamshed 	if (cpu < 0 || cpu >= (int) n) {
4776404edcSAsim Jamshed 		errno = -EINVAL;
4876404edcSAsim Jamshed 		return -1;
4976404edcSAsim Jamshed 	}
5076404edcSAsim Jamshed 
5176404edcSAsim Jamshed 	CPU_ZERO(&cpus);
5276404edcSAsim Jamshed 	CPU_SET((unsigned)cpu, &cpus);
5376404edcSAsim Jamshed 
5476404edcSAsim Jamshed 	ret = sched_setaffinity(Gettid(), sizeof(cpus), &cpus);
5576404edcSAsim Jamshed 
5676404edcSAsim Jamshed #ifndef DISABLE_NUMA
5776404edcSAsim Jamshed 	if (numa_max_node() == 0)
5876404edcSAsim Jamshed 		return ret;
5976404edcSAsim Jamshed 
60a5e1a556SAsim Jamshed 	bmask = numa_bitmask_alloc(numa_max_node() + 1);
6176404edcSAsim Jamshed 	assert(bmask);
6276404edcSAsim Jamshed #endif /* DISABLE_NUMA */
6376404edcSAsim Jamshed 
6476404edcSAsim Jamshed 	/* read physical id of the core from sys information */
6576404edcSAsim Jamshed 	snprintf(sysfname, MAX_FILE_NAME - 1,
6676404edcSAsim Jamshed 			"/sys/devices/system/cpu/cpu%d/topology/physical_package_id", cpu);
6776404edcSAsim Jamshed 	fp = fopen(sysfname, "r");
6876404edcSAsim Jamshed 	if (!fp) {
6976404edcSAsim Jamshed 		perror(sysfname);
7076404edcSAsim Jamshed 		errno = EFAULT;
7176404edcSAsim Jamshed 		return -1;
7276404edcSAsim Jamshed 	}
7376404edcSAsim Jamshed 
7476404edcSAsim Jamshed 	ret = fscanf(fp, "%d", &phy_id);
7576404edcSAsim Jamshed 	if (ret != 1) {
76*8a941c7eSAsim Jamshed 		fclose(fp);
7776404edcSAsim Jamshed 		perror("Fail to read core id");
7876404edcSAsim Jamshed 		errno = EFAULT;
7976404edcSAsim Jamshed 		return -1;
8076404edcSAsim Jamshed 	}
8176404edcSAsim Jamshed 
8276404edcSAsim Jamshed 
8376404edcSAsim Jamshed #ifndef DISABLE_NUMA
8476404edcSAsim Jamshed 	numa_bitmask_setbit(bmask, phy_id);
8576404edcSAsim Jamshed 	numa_set_membind(bmask);
8676404edcSAsim Jamshed 	numa_bitmask_free(bmask);
8776404edcSAsim Jamshed #endif /* DISABLE_NUMA */
8876404edcSAsim Jamshed 
8976404edcSAsim Jamshed 	fclose(fp);
9076404edcSAsim Jamshed 
9176404edcSAsim Jamshed 	return ret;
9276404edcSAsim Jamshed }
93