基本上,我想要的是从lscpu中提取L3缓存的字节大小。棘手的部分是lscpu使用的单元在不同版本之间不一致,我需要处理的是所有版本(包括-字节选项可用之前的版本)。从我所看到的来看,lscpu将使用K,KiB,M或MiB,所以这就是我想要解析的。
以下是lscpu的输出:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 16
On-line CPU(s) list: 0-15
Thread(s) per core: 1
Core(s) per socket: 1
Socket(s): 16
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 60
Model name: Intel Core Processor (Haswell, no TSX, IBRS)
Stepping: 1
CPU MHz: 2299.998
BogoMIPS: 4599.99
Virtualization: VT-x
Hypervisor vendor: KVM
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 4096K
L3 cache: 16384K
NUMA node0 CPU(s): 0-15
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush mmx fxsr sse sse2 ss syscall nx pdpe1gb rdtscp lm constant_tsc rep_good nopl xtopology eagerfpu pni pclmulqdq vmx ssse3 fma cx16 pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand hypervisor lahf_lm abm invpcid_single ssbd ibrs ibpb tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt arat md_clear spec_ctrl到目前为止,这就是我所拥有的,但似乎无法完成。
$ lscpu | awk '/L3 cache:/{print $3$4;next};/(M|MiB)$/{printf "%u\n", $3*(1024*1024);next};/(K|KiB)$/{printf "%u\n", $3*1024;next}'
0
32768
32768
4194304
16384K有什么想法吗?如何调整我的awk命令使其工作?
编辑:我的预期输出将是:
16777216发布于 2021-07-27 21:13:06
事实上,赛勒斯的答案是最优的,但如果你设定在awk上,请尝试如下:
awk -F: 'BEGIN{def=1024}/^L3/{if($2~/M/){def=def*def}; printf "%u\n", $2*def}'发布于 2021-07-27 19:53:53
获取以字节为单位的第三级缓存(L3):
getconf LEVEL3_CACHE_SIZE发布于 2021-07-28 04:23:51
$ cat tst.awk
BEGIN {
mult["K"] = 1000
mult["KiB"] = 1024
mult["M"] = mult["K"]^2
mult["MiB"] = mult["KiB"]^2
}
sub(/^L3 cache:/,"") {
smbl = $NF
sub(/[^[:alpha:]]+/,"",smbl)
print $0 * mult[smbl]
}$ awk -f tst.awk file
16384000https://stackoverflow.com/questions/68550194
复制相似问题