Sridevi Ramesh | 0d88ab3 | 2017-09-21 11:07:28 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | CPU_SYS_DIR=/sys/devices/system/cpu/ |
| 4 | NR_CPUS=0 |
| 5 | CPU= |
| 6 | SHOW_LAYOUT=0 |
| 7 | |
| 8 | |
| 9 | function usage() |
| 10 | { |
| 11 | echo "usage:" |
| 12 | echo " $0 [cpu] [-L]" |
| 13 | echo |
| 14 | echo "option:" |
| 15 | echo " [cpu]" |
| 16 | echo " Logical cpu id." |
| 17 | echo " -L" |
| 18 | echo " Show processor layout." |
| 19 | echo " -h" |
| 20 | echo " Show this help." |
| 21 | exit 1 |
| 22 | } |
| 23 | |
| 24 | function parse_args() |
| 25 | { |
| 26 | while getopts "hL" OPTION |
| 27 | do |
| 28 | case $OPTION in |
| 29 | L) |
| 30 | SHOW_LAYOUT=1 |
| 31 | ;; |
| 32 | *) |
| 33 | usage |
| 34 | exit 1 |
| 35 | ;; |
| 36 | esac |
| 37 | done |
| 38 | |
| 39 | CPU=${!OPTIND} |
| 40 | } |
| 41 | |
| 42 | parse_args $* |
| 43 | |
| 44 | [ "$1" == "-h" ] && usage |
| 45 | |
| 46 | declare -a CPUS |
| 47 | declare -a CPU_FILE |
| 48 | declare -a CPU_PIR |
| 49 | declare -a CORE_ID |
| 50 | declare -a CHIP_ID |
| 51 | |
| 52 | declare -a CHIP_EQ |
| 53 | declare -a CHIP_EX |
| 54 | declare -a CHIP_CORES |
| 55 | |
| 56 | declare -A CORE_MATRIX |
| 57 | |
| 58 | for cpu_file in $(find $CPU_SYS_DIR -name "cpu[0-9]*") |
| 59 | do |
| 60 | cpu=$(basename $cpu_file | tr -dc '0-9') |
| 61 | [ -n "$cpu" ] && NR_CPUS=$(expr $NR_CPUS + 1) |
| 62 | |
| 63 | CPU_VALID[$cpu]=0 |
| 64 | [ -n "$CPU" ] && [ $cpu != $CPU ] && continue |
| 65 | [ ! -e $cpu_file/pir ] && continue |
| 66 | |
| 67 | CPU_VALID[$cpu]=1 |
| 68 | |
| 69 | CPU_FILE[$cpu]=$cpu_file |
| 70 | pir=$(cat $cpu_file/pir) |
| 71 | CPU_PIR[$cpu]=$pir |
| 72 | CORE_ID[$cpu]=$(perl -e '{ printf("%d", (0x'$pir' >> 2) & 0x3f); }') |
| 73 | CHIP_ID[$cpu]=$(perl -e '{ printf("%x", (0x'$pir' >> 8) & 0x7f); }') |
| 74 | done |
| 75 | |
| 76 | i=0 |
| 77 | chip_id=-1 |
| 78 | core_id=-1 |
| 79 | prev_eq_id=-1 |
| 80 | prev_ex_id=-1 |
| 81 | num_threads=0 |
| 82 | CPU_LIST=" " |
| 83 | while [ $i -lt $NR_CPUS ] |
| 84 | do |
| 85 | [ ${CPU_VALID[$i]} -eq 0 ] && i=$(expr $i + 1) && continue |
| 86 | |
| 87 | [ "$chip_id" != "${CHIP_ID[$i]}" ] && core_id=-1 && prev_eq_id=-1 && prev_ex_id=-1 |
| 88 | chip_id=${CHIP_ID[$i]} |
| 89 | if [ "$core_id" != ${CORE_ID[$i]} ] |
| 90 | then |
| 91 | if [ $num_threads -ne 0 ]; then |
| 92 | echo "THREADS: $num_threads CPUs: $CPU_LIST" |
| 93 | CPU_LIST=" " |
| 94 | fi |
| 95 | CPU_LIST="$CPU_LIST$i " |
| 96 | echo -n "CHIP ID: ${CHIP_ID[$i]} " |
| 97 | echo -n "CORE ID: ${CORE_ID[$i]} " |
| 98 | CHIP_CORES[$chip_id]="${CHIP_CORES[$chip_id]},${CORE_ID[$i]}" |
| 99 | CORE_MATRIX[$chip_id,${CORE_ID[$i]}]=1 |
| 100 | eq_id=$(perl -e '{ printf("%d", (('${CORE_ID[$i]}' & 0x1c) >> 2)); }') |
| 101 | if [ $eq_id != $prev_eq_id ] |
| 102 | then |
| 103 | CHIP_EQ[$chip_id]="${CHIP_EQ[$chip_id]},$eq_id" |
| 104 | prev_eq_id=$eq_id |
| 105 | fi |
| 106 | ex_id=$(perl -e '{ printf("%d", (('${CORE_ID[$i]}') >> 1)); }') |
| 107 | if [ $ex_id != $prev_ex_id ] |
| 108 | then |
| 109 | CHIP_EX[$chip_id]="${CHIP_EX[$chip_id]},$ex_id" |
| 110 | prev_ex_id=$ex_id |
| 111 | fi |
| 112 | num_threads=1 |
| 113 | else |
| 114 | CPU_LIST="$CPU_LIST$i " |
| 115 | num_threads=$(expr $num_threads + 1) |
| 116 | fi |
| 117 | core_id=${CORE_ID[$i]} |
| 118 | |
| 119 | i=$(expr $i + 1) |
| 120 | done |
| 121 | echo "THREADS: $num_threads CPUs: $CPU_LIST" |
| 122 | |
| 123 | echo |
| 124 | echo "-----------------------------" |
| 125 | for chip_id in ${!CHIP_CORES[@]} |
| 126 | do |
| 127 | echo "p[$chip_id]" |
| 128 | CHIP_CORES[$chip_id]="$(echo ${CHIP_CORES[$chip_id]} | cut -c 2-)" |
| 129 | CHIP_EQ[$chip_id]="$(echo ${CHIP_EQ[$chip_id]} | cut -c 2-)" |
| 130 | CHIP_EX[$chip_id]="$(echo ${CHIP_EX[$chip_id]} | cut -c 2-)" |
| 131 | echo " eq[${CHIP_EQ[$chip_id]}]" |
| 132 | echo " ex[${CHIP_EX[$chip_id]}]" |
| 133 | echo " c[${CHIP_CORES[$chip_id]}]" |
| 134 | done |
| 135 | echo "-----------------------------" |
| 136 | |
| 137 | |
| 138 | [ $SHOW_LAYOUT -eq 0 ] && exit |
| 139 | |
| 140 | # Print chip layout |
| 141 | |
| 142 | function print_header() |
| 143 | { |
| 144 | local _row_=$1 |
| 145 | |
| 146 | for q in 0 2 4 |
| 147 | do |
| 148 | quad=$(perl -e '{ printf("%02d", ('$q' + '$_row_')); }') |
| 149 | echo -n " +---EQ$quad----+ " |
| 150 | done |
| 151 | echo |
| 152 | } |
| 153 | |
| 154 | function print_core_info() |
| 155 | { |
| 156 | local _row_=$1 |
| 157 | local _cpos_=$2 # Core position |
| 158 | |
| 159 | for q in 0 2 4 |
| 160 | do |
| 161 | quad=$(perl -e '{ printf("%02d", ('$q' + '$_row_')); }') |
| 162 | core_id=$(perl -e '{ printf("%d", ('$_cpos_' + ('$quad' * 4))); }') |
| 163 | core_id_str=$(perl -e '{ printf("%-2d", ('$_cpos_' + ('$quad' * 4))); }') |
| 164 | ex=$(perl -e '{ printf("%-2d", ('$core_id' >> 1)); }') |
| 165 | if [ -n "${CORE_MATRIX[$chip_id,$core_id]}" ] |
| 166 | then |
| 167 | echo -n " |EX-$ex C$core_id_str|" |
| 168 | else |
| 169 | echo -n " | |" |
| 170 | fi |
| 171 | done |
| 172 | echo |
| 173 | if [ $_cpos_ -eq 3 ] |
| 174 | then |
| 175 | echo " +-----------+ +-----------+ +-----------+" |
| 176 | else |
| 177 | echo " + - - - - - + + - - - - - + + - - - - - +" |
| 178 | fi |
| 179 | } |
| 180 | |
| 181 | echo |
| 182 | echo "----------Processor Layout-------------------" |
| 183 | for chip_id in ${!CHIP_CORES[@]} |
| 184 | do |
| 185 | echo "p[$chip_id]" |
| 186 | |
| 187 | for row in 0 1 |
| 188 | do |
| 189 | print_header $row |
| 190 | for core_pos in 0 1 2 3 |
| 191 | do |
| 192 | print_core_info $row $core_pos |
| 193 | done |
| 194 | echo |
| 195 | |
| 196 | done |
| 197 | echo |
| 198 | |
| 199 | done |