Any bash text processing gurus?

Notice: Page may contain affiliate links for which we may earn a small commission through services like Amazon Affiliates or Skimlinks.

Patrick

Administrator
Staff member
Dec 21, 2010
12,512
5,800
113
Working on my monero Docker container and ran into a roadblock.

Using nproc was easy to get cores and use them as a base for threads to use.

Now I need to get L3 cache and then take half the MB value and round.

The math is easy. Being able to store a value for L3cacheMB is beyond me. I can see the number in lscpu, but parsing is another matter.

Any ideas on how to do this?
 

Patrick

Administrator
Staff member
Dec 21, 2010
12,512
5,800
113
Ok this is ugly but seems to be working:
Code:
$ lscpu | grep L3 | tr -s " " | awk -F':' '{print $2}' | cut -c 2- | rev | cut -c 2- | rev
46080
Thank you again @xhypno402 !
 

TuxDude

Well-Known Member
Sep 17, 2011
616
338
63
If you're going to use awk anyways, which is a full on language of itself that can do math and such, why also involve so many other tools and pipes? @xhypno402 's solution can be simplified down to just:

lscpu | awk '/^L3/ {print $NF}'

And you can do further math or whatever within that awk statement too if required.
 

Patrick

Administrator
Staff member
Dec 21, 2010
12,512
5,800
113
If you're going to use awk anyways, which is a full on language of itself that can do math and such, why also involve so many other tools and pipes?
You should not underestimate how bad I am at text parsing!

Sitting delayed on the plane at our gate at SFO but the above has it working on the 4 socket system so that is a good start.

Thanks for the awk tip will see if I can edit en route.
 

TuxDude

Well-Known Member
Sep 17, 2011
616
338
63
So you want L3 cache in MB as a final output? I just tested my line on a VM that thinks it has 2x dual-core chips (4 cores total), where the output is simply:

20480K
 
  • Like
Reactions: Patrick

Patrick

Administrator
Staff member
Dec 21, 2010
12,512
5,800
113
We pushed from the gate but I got it dividing by 1024 for MB, multiplying by number of sockets, dividing by 2 and rounding down.

The program runs in 2MB L3 cache so the object was to get that number for the entire system to plug into the miner's thread parameter.

Had to put away my laptop but will update what I have soon (even incorporates your suggestion @TuxDude ). It is working properly.

Next step is creating the docker swarm and running the container as a service.

Something to do after Whistler's slopes close.
 

TuxDude

Well-Known Member
Sep 17, 2011
616
338
63
Sounds like this should do what you want then.

lscpu | awk '/^L3/ {l3=sprintf("%u", $NF)/1024} /^Socket/ {sockets=sprintf("%u", $NF)} END {print l3*sockets/2}'

Grabs both the L3 cache amount and number of sockets from lscpu output and stores them into variables, then does the bit of math you described at the end and prints out the result.

Given this lscpu output:
$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Thread(s) per core: 1
Core(s) per socket: 2
Socket(s): 2
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 45
Model name: Intel(R) Xeon(R) CPU E5-2660 0 @ 2.20GHz
Stepping: 2
CPU MHz: 2199.421
BogoMIPS: 4400.00
Hypervisor vendor: VMware
Virtualization type: full
L1d cache: 32K
L1i cache: 32K
L2 cache: 256K
L3 cache: 20480K
NUMA node0 CPU(s): 0-3
it outputs just the number 20:

$ lscpu | awk '/^L3/ {l3=sprintf("%u", $NF)/1024} /^Socket/ {sockets=sprintf("%u", $NF)} END {print l3*sockets/2}'
20
 
  • Like
Reactions: Patrick and tjk

Patrick

Administrator
Staff member
Dec 21, 2010
12,512
5,800
113
nice, how are conditions ?
have done some backcountry-hiking near german/austrian border today with bluebird and 80cm fresh from the weekend :)
Rain at the bottom. Will see tomorrow how it is at higher elevations.

Thanks @TuxDude
 

gigatexal

I'm here to learn
Nov 25, 2012
2,913
607
113
Portland, Oregon
alexandarnarayan.com
Awk is well awkward -- i find this a bit easier to follow, but it probably won't work in really bare environment without python:

check it out:

repl.it

Code:
import os
import pprint #optional

lscpu = {}

for line in list(os.popen('lscpu')):
  elem = line.splitlines()
  for e in elem:
    tmp = e.split(':')
    key = tmp[0]
    val = tmp[1].strip()
    lscpu.update({key:val})

pprint.pprint(lscpu) #can comment out, for debug purposes
print('Cache as an integer: ',int(lscpu['L2 cache'][0:-1])) #input any key here
granted one could make this a lot better by allowing it to take input from the command-line and make it executable with a #/usr/python etc
 
Last edited:
  • Like
Reactions: Patrick