BASH算术脚本

时间:2018-02-27 19:39:36

标签: bash

我的输入文件结构如下:

input.txt   
 system.switch_cpus.commit.op_class_0::SimdFloatMultAcc            0      0.00%     59.63% # Class of committed instruction
    936 system.switch_cpus.commit.op_class_0::SimdFloatSqrt            0      0.00%     59.63% # Class of committed instruction
    937 system.switch_cpus.commit.op_class_0::MemRead      3451622     34.52%     94.15% # Class of committed instruction
    938 system.switch_cpus.commit.op_class_0::MemWrite       585428      5.85%    100.00% # Class of committed instruction
    939 system.switch_cpus.commit.op_class_0::FloatMemRead            0      0.00%    100.00% # Class of committed instruction
    940 system.switch_cpus.commit.op_class_0::FloatMemWrite            0      0.00%    100.00% # Class of committed instruction
    941 system.switch_cpus.commit.op_class_0::IprAccess            0      0.00%    100.00% # Class of committed instruction
    942 system.switch_cpus.commit.op_class_0::InstPrefetch            0      0.00%    100.00% # Class of committed instruction
    943 system.switch_cpus.commit.op_class_0::total     10000000                       # Class of committed instruction
    944 system.switch_cpus.commit.bw_lim_events      10000000                       # number cycles where commit BW limit reached
    945 system.switch_cpus.rob.rob_reads             80558432                       # The number of ROB reads
    946 system.switch_cpus.rob.rob_writes            43430539                       # The number of ROB writes
    947 system.switch_cpus.timesIdled                   37218                       # Number of times that the entire CPU went into an idle     state and unscheduled itself
    948 system.switch_cpus.idleCycles                 2755508                       # Total number of cycles that the CPU has spent unsched    uled due to idling
    949 system.switch_cpus.committedInsts            10000000                       # Number of Instructions Simulated
    950 system.switch_cpus.committedOps              10000000                       # Number of Ops (including micro ops) Simulated
    951 system.switch_cpus.cpi                       8.369191                       # CPI: Cycles Per Instruction
    952 system.switch_cpus.cpi_total                 8.369191                       # CPI: Total CPI of All Threads
    953 system.switch_cpus.ipc                       0.119486                       # IPC: Instructions Per Cycle
    954 system.switch_cpus.ipc_total                 0.119486                       # IPC: Total IPC of All Threads
    955 system.switch_cpus.int_regfile_reads         21773538                       # number of integer regfile reads
    956 system.switch_cpus.int_regfile_writes         9447282                       # number of integer regfile writes

我试图找到一个未在输入文件中直接报告的值,但需要一个简单的算术运算。

以下是我正在使用的代码:

Code:
  1 list=(IPC CycleCount CommitCount LDQ_Stall STQ_Stall IQ_Stall ROB_STALL BTBMiss BrCount BrMisPred LDCount STCount ICacheMissCount L    d_miss_Count St_miss_Count)
  2 IPC="system.switch_cpus.ipc"
  3 CycleCount="system.switch_cpus.numCycles"
  4 CommitCount="system.switch_cpus.commit.committedInsts"
  5 LDQ_Stall="system.switch_cpus.rename.LQFullEvents"
  6 STQ_Stall="system.switch_cpus.rename.SQFullEvents"
  7 IQ_Stall="system.switch_cpus.rename.IQFullEvents"
  8 ROB_STALL="system.switch_cpus.rename.ROBFullEvents"
  9 BTBLookups="system.switch_cpus.branchPred.BTBLookups"
 10 BTBHits="system.switch_cpus.branchPred.BTBHits"
 11 #BTBMiss="$BTBLookups-$BTBHits"
 12 BrCount="system.switch_cpus.commit.branches"
 13 BrMisPred="system.switch_cpus.commit.branchMispredicts"
 14 LDCount="system.switch_cpus.commit.loads"
 15 #STCount="system.switch_cpus.commit.refs"-"$LDCount"
 16 IcacheMissCount="system.cpu.icache.overall_misses::total"
 17 Ld_miss_Count="system.cpu.dcache.ReadReq_misses::total"
 18 St_miss_Count="system.cpu.dcache.WriteReq_misses::total"
 19 
 20 
 21 
 22 
 23 
 24 for i in ${list[@]}:
 25   do 
 26   #  echo "$i"
 27     echo ${!i}
 28     if [ "${i}" == "BTBMiss"]; then
 29       lookup=$(awk -v a="${BTBLookups}" '{if ($1==a) {print $2})
 30       hits=$(awk -v a="${BTBHits}" '{if ($1==a) {print $2}}) 
 31       echo "$i = $((lookup-hits))"
 32  
 33     elif [$i=="STCount"];then
 34                                                                                                                                    
 35       refcount=`awk '($1=="system.switch_cpus.commit.refs") {print $2}'`
 36       ldcount=`awk '($1=="system.switch_cpus.commit.loads") {print$2}'`
 37       echo "$i = $((refcount-ldcount))"
 38       
 39     else
 40     awk -v a="${!i}" '{if ($1==a) {print $2}}' $1
 41   fi
 42   done

对于每个列表元素,如果元素匹配“BTBMiss”,它应该找到代码中列出的值并报告该数字。否则,它将执行从input.txt文件中查找每个元素值的常规操作。 运行代码,我的代码中的“if”语句出现了一些奇怪的问题:

$--> ./parser.sh  input.txt 
system.switch_cpus.ipc
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [IPC==STCount]: command not found
0.119486
system.switch_cpus.numCycles
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [CycleCount==STCount]: command not found
83691906
system.switch_cpus.commit.committedInsts
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [CommitCount==STCount]: command not found
10000000
system.switch_cpus.rename.LQFullEvents
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [LDQ_Stall==STCount]: command not found
59696
system.switch_cpus.rename.SQFullEvents
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [STQ_Stall==STCount]: command not found
1546
system.switch_cpus.rename.IQFullEvents
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [IQ_Stall==STCount]: command not found
109463
system.switch_cpus.rename.ROBFullEvents
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [ROB_STALL==STCount]: command not found
9574

./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [BTBMiss==STCount]: command not found


system.switch_cpus.commit.branches
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [BrCount==STCount]: command not found
2478601
system.switch_cpus.commit.branchMispredicts
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [BrMisPred==STCount]: command not found
442168
system.switch_cpus.commit.loads
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [LDCount==STCount]: command not found
3451622

./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [STCount==STCount]: command not found



./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [ICacheMissCount==STCount]: command not found


system.cpu.dcache.ReadReq_misses::total
./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [Ld_miss_Count==STCount]: command not found
2233581

./parser.sh: line 28: [: missing `]'
./parser.sh: line 33: [St_miss_Count:==STCount]: command not found

if if ... elif ... else语句我做错了什么?

1 个答案:

答案 0 :(得分:0)

这似乎有效:

 1 list=(IPC CycleCount CommitCount LDQ_Stall STQ_Stall IQ_Stall ROB_STALL BTBMiss BrCount BrMisPred LDCount STCount IcacheMissCount Ld_miss_Count St_miss_Count)
  2 IPC="system.switch_cpus.ipc"
  3 CycleCount="system.switch_cpus.numCycles"
  4 CommitCount="system.switch_cpus.commit.committedInsts"
  5 LDQ_Stall="system.switch_cpus.rename.LQFullEvents"
  6 STQ_Stall="system.switch_cpus.rename.SQFullEvents"
  7 IQ_Stall="system.switch_cpus.rename.IQFullEvents"
  8 ROB_STALL="system.switch_cpus.rename.ROBFullEvents"
  9 BTBLookups="system.switch_cpus.branchPred.BTBLookups"
 10 BTBHits="system.switch_cpus.branchPred.BTBHits"
 11 #BTBMiss="$BTBLookups-$BTBHits"
 12 BrCount="system.switch_cpus.commit.branches"
 13 BrMisPred="system.switch_cpus.commit.branchMispredicts"
 14 LDCount="system.switch_cpus.commit.loads"
 15 #STCount="system.switch_cpus.commit.refs"-"$LDCount"
 16 IcacheMissCount="system.cpu.icache.overall_misses::total"
 17 Ld_miss_Count="system.cpu.dcache.ReadReq_misses::total"
 18 St_miss_Count="system.cpu.dcache.WriteReq_misses::total"
 19 
 20 
 21 
 22 
 23 
 24 for i in "${list[@]}"                                                                                                                                                                                                                                                        
 25   do
 26   #  echo "$i"
 27    # echo ${!i}
 28     if [ "${i}" == "BTBMiss" ]; then
 29       lookup=$(awk -v a="${BTBLookups}" '{if ($1==a) print $2}' $1)
 30       hits=$(awk -v a="${BTBHits}" '{if ($1==a) print $2}' $1)
 31       echo "$i = $((lookup-hits))"
 32 
 33     elif [ $i == "STCount" ];then
 34 
 35       refcount=`awk '($1=="system.switch_cpus.commit.refs") {print $2}' $1`
 36       ldcount=`awk '($1=="system.switch_cpus.commit.loads") {print$2}' $1`
 37       echo "$i = $((refcount-ldcount))"
 38 
 39     else
 40     value=$(awk -v a="${!i}" '{if ($1==a) print $2}' $1)
 41     echo "$i = $value"
 42   fi
 43   done

输出:

./parser.sh  out/stats.txt 
IPC = 0.119486
CycleCount = 83691906
CommitCount = 10000000
LDQ_Stall = 59696
STQ_Stall = 1546
IQ_Stall = 109463
ROB_STALL = 9574
BTBMiss = 3562
BrCount = 2478601
BrMisPred = 442168
LDCount = 3451622
STCount = 585428
IcacheMissCount = 36218
Ld_miss_Count = 2233581
St_miss_Count = 207436