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