Merge "sysconf(): implement cache queries." into main am: a71a15fe34
am: 3996bbe901
am: a9ca959498
Original change: https://android-review.googlesource.com/c/platform/bionic/+/2721536 Change-Id: I9c26aaff7285dcb1bde407ffc6638f949378045d Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
commit
3e268603ec
2 changed files with 136 additions and 17 deletions
|
@ -41,6 +41,107 @@
|
||||||
#include "platform/bionic/page.h"
|
#include "platform/bionic/page.h"
|
||||||
#include "private/bionic_tls.h"
|
#include "private/bionic_tls.h"
|
||||||
|
|
||||||
|
struct sysconf_cache {
|
||||||
|
long size, assoc, linesize;
|
||||||
|
|
||||||
|
static sysconf_cache from_size_and_geometry(int size_id, int geometry_id) {
|
||||||
|
sysconf_cache result;
|
||||||
|
result.size = getauxval(size_id);
|
||||||
|
unsigned long geometry = getauxval(geometry_id);
|
||||||
|
result.assoc = geometry >> 16;
|
||||||
|
result.linesize = geometry & 0xffff;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
struct sysconf_caches {
|
||||||
|
sysconf_cache l1_i, l1_d, l2, l3, l4;
|
||||||
|
};
|
||||||
|
|
||||||
|
#if defined(__riscv)
|
||||||
|
|
||||||
|
static sysconf_caches* __sysconf_caches() {
|
||||||
|
static sysconf_caches cached = []{
|
||||||
|
sysconf_caches info = {};
|
||||||
|
// riscv64 kernels conveniently hand us all this information.
|
||||||
|
info.l1_i = sysconf_cache::from_size_and_geometry(AT_L1I_CACHESIZE, AT_L1I_CACHEGEOMETRY);
|
||||||
|
info.l1_d = sysconf_cache::from_size_and_geometry(AT_L1D_CACHESIZE, AT_L1D_CACHEGEOMETRY);
|
||||||
|
info.l2 = sysconf_cache::from_size_and_geometry(AT_L2_CACHESIZE, AT_L2_CACHEGEOMETRY);
|
||||||
|
info.l3 = sysconf_cache::from_size_and_geometry(AT_L3_CACHESIZE, AT_L3_CACHEGEOMETRY);
|
||||||
|
return info;
|
||||||
|
}();
|
||||||
|
return &cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
#elif defined(__aarch64__)
|
||||||
|
|
||||||
|
static sysconf_caches* __sysconf_caches() {
|
||||||
|
static sysconf_caches cached = []{
|
||||||
|
sysconf_caches info = {};
|
||||||
|
// arm64 is especially limited. We can infer the L1 line sizes, but that's it.
|
||||||
|
uint64_t ctr_el0;
|
||||||
|
__asm__ __volatile__("mrs %0, ctr_el0" : "=r"(ctr_el0));
|
||||||
|
info.l1_i.linesize = 4 << (ctr_el0 & 0xf);
|
||||||
|
info.l1_d.linesize = 4 << ((ctr_el0 >> 16) & 0xf);
|
||||||
|
return info;
|
||||||
|
}();
|
||||||
|
return &cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else
|
||||||
|
|
||||||
|
long __sysconf_fread_long(const char* path) {
|
||||||
|
long result = 0;
|
||||||
|
FILE* fp = fopen(path, "re");
|
||||||
|
if (fp != nullptr) {
|
||||||
|
fscanf(fp, "%ld", &result);
|
||||||
|
fclose(fp);
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
static sysconf_caches* __sysconf_caches() {
|
||||||
|
static sysconf_caches cached = []{
|
||||||
|
sysconf_caches info = {};
|
||||||
|
char path[64];
|
||||||
|
for (int i = 0; i < 4; i++) {
|
||||||
|
sysconf_cache c;
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cache/index%d/size", i);
|
||||||
|
c.size = __sysconf_fread_long(path) * 1024;
|
||||||
|
if (c.size == 0) break;
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cache/index%d/ways_of_associativity", i);
|
||||||
|
c.assoc = __sysconf_fread_long(path);
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cache/index%d/coherency_line_size", i);
|
||||||
|
c.linesize = __sysconf_fread_long(path);
|
||||||
|
|
||||||
|
snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cache/index%d/level", i);
|
||||||
|
int level = __sysconf_fread_long(path);
|
||||||
|
if (level == 1) {
|
||||||
|
snprintf(path, sizeof(path), "/sys/devices/system/cpu/cpu0/cache/index%d/type", i);
|
||||||
|
FILE* fp = fopen(path, "re");
|
||||||
|
char type = fgetc(fp);
|
||||||
|
fclose(fp);
|
||||||
|
if (type == 'D') {
|
||||||
|
info.l1_d = c;
|
||||||
|
} else if (type == 'I') {
|
||||||
|
info.l1_i = c;
|
||||||
|
}
|
||||||
|
} else if (level == 2) {
|
||||||
|
info.l2 = c;
|
||||||
|
} else if (level == 3) {
|
||||||
|
info.l3 = c;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return info;
|
||||||
|
}();
|
||||||
|
return &cached;
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
static long __sysconf_rlimit(int resource) {
|
static long __sysconf_rlimit(int resource) {
|
||||||
rlimit rl;
|
rlimit rl;
|
||||||
getrlimit(resource, &rl);
|
getrlimit(resource, &rl);
|
||||||
|
@ -218,23 +319,21 @@ long sysconf(int name) {
|
||||||
case _SC_XOPEN_STREAMS: return -1; // Obsolescent in POSIX.1-2008.
|
case _SC_XOPEN_STREAMS: return -1; // Obsolescent in POSIX.1-2008.
|
||||||
case _SC_XOPEN_UUCP: return -1;
|
case _SC_XOPEN_UUCP: return -1;
|
||||||
|
|
||||||
// We do not have actual implementations for cache queries.
|
case _SC_LEVEL1_ICACHE_SIZE: return __sysconf_caches()->l1_i.size;
|
||||||
// It's valid to return 0 as the result is unknown.
|
case _SC_LEVEL1_ICACHE_ASSOC: return __sysconf_caches()->l1_i.assoc;
|
||||||
case _SC_LEVEL1_ICACHE_SIZE: return 0;
|
case _SC_LEVEL1_ICACHE_LINESIZE: return __sysconf_caches()->l1_i.linesize;
|
||||||
case _SC_LEVEL1_ICACHE_ASSOC: return 0;
|
case _SC_LEVEL1_DCACHE_SIZE: return __sysconf_caches()->l1_d.size;
|
||||||
case _SC_LEVEL1_ICACHE_LINESIZE: return 0;
|
case _SC_LEVEL1_DCACHE_ASSOC: return __sysconf_caches()->l1_d.assoc;
|
||||||
case _SC_LEVEL1_DCACHE_SIZE: return 0;
|
case _SC_LEVEL1_DCACHE_LINESIZE: return __sysconf_caches()->l1_d.linesize;
|
||||||
case _SC_LEVEL1_DCACHE_ASSOC: return 0;
|
case _SC_LEVEL2_CACHE_SIZE: return __sysconf_caches()->l2.size;
|
||||||
case _SC_LEVEL1_DCACHE_LINESIZE: return 0;
|
case _SC_LEVEL2_CACHE_ASSOC: return __sysconf_caches()->l2.assoc;
|
||||||
case _SC_LEVEL2_CACHE_SIZE: return 0;
|
case _SC_LEVEL2_CACHE_LINESIZE: return __sysconf_caches()->l2.linesize;
|
||||||
case _SC_LEVEL2_CACHE_ASSOC: return 0;
|
case _SC_LEVEL3_CACHE_SIZE: return __sysconf_caches()->l3.size;
|
||||||
case _SC_LEVEL2_CACHE_LINESIZE: return 0;
|
case _SC_LEVEL3_CACHE_ASSOC: return __sysconf_caches()->l3.assoc;
|
||||||
case _SC_LEVEL3_CACHE_SIZE: return 0;
|
case _SC_LEVEL3_CACHE_LINESIZE: return __sysconf_caches()->l3.linesize;
|
||||||
case _SC_LEVEL3_CACHE_ASSOC: return 0;
|
case _SC_LEVEL4_CACHE_SIZE: return __sysconf_caches()->l4.size;
|
||||||
case _SC_LEVEL3_CACHE_LINESIZE: return 0;
|
case _SC_LEVEL4_CACHE_ASSOC: return __sysconf_caches()->l4.assoc;
|
||||||
case _SC_LEVEL4_CACHE_SIZE: return 0;
|
case _SC_LEVEL4_CACHE_LINESIZE: return __sysconf_caches()->l4.linesize;
|
||||||
case _SC_LEVEL4_CACHE_ASSOC: return 0;
|
|
||||||
case _SC_LEVEL4_CACHE_LINESIZE: return 0;
|
|
||||||
|
|
||||||
default:
|
default:
|
||||||
errno = EINVAL;
|
errno = EINVAL;
|
||||||
|
|
|
@ -1166,6 +1166,26 @@ TEST(UNISTD_TEST, sysconf_unknown) {
|
||||||
VERIFY_SYSCONF_UNKNOWN(666);
|
VERIFY_SYSCONF_UNKNOWN(666);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void show_cache(const char* name, long size, long assoc, long line_size) {
|
||||||
|
printf("%s cache size: %ld bytes, line size %ld bytes, ", name, size, line_size);
|
||||||
|
if (assoc == 0) {
|
||||||
|
printf("fully");
|
||||||
|
} else {
|
||||||
|
printf("%ld-way", assoc);
|
||||||
|
}
|
||||||
|
printf(" associative\n");
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST(UNISTD_TEST, sysconf_cache) {
|
||||||
|
// It's not obvious we can _test_ any of these, but we can at least
|
||||||
|
// show the output for humans to inspect.
|
||||||
|
show_cache("L1D", sysconf(_SC_LEVEL1_DCACHE_SIZE), sysconf(_SC_LEVEL1_DCACHE_ASSOC), sysconf(_SC_LEVEL1_DCACHE_LINESIZE));
|
||||||
|
show_cache("L1I", sysconf(_SC_LEVEL1_ICACHE_SIZE), sysconf(_SC_LEVEL1_ICACHE_ASSOC), sysconf(_SC_LEVEL1_ICACHE_LINESIZE));
|
||||||
|
show_cache("L2", sysconf(_SC_LEVEL2_CACHE_SIZE), sysconf(_SC_LEVEL2_CACHE_ASSOC), sysconf(_SC_LEVEL2_CACHE_LINESIZE));
|
||||||
|
show_cache("L3", sysconf(_SC_LEVEL3_CACHE_SIZE), sysconf(_SC_LEVEL3_CACHE_ASSOC), sysconf(_SC_LEVEL3_CACHE_LINESIZE));
|
||||||
|
show_cache("L4", sysconf(_SC_LEVEL4_CACHE_SIZE), sysconf(_SC_LEVEL4_CACHE_ASSOC), sysconf(_SC_LEVEL4_CACHE_LINESIZE));
|
||||||
|
}
|
||||||
|
|
||||||
TEST(UNISTD_TEST, dup2_same) {
|
TEST(UNISTD_TEST, dup2_same) {
|
||||||
// POSIX says of dup2:
|
// POSIX says of dup2:
|
||||||
// If fildes2 is already a valid open file descriptor ...
|
// If fildes2 is already a valid open file descriptor ...
|
||||||
|
|
Loading…
Reference in a new issue