blob: 16a4e5a030fdc285550bc39e00f63e7c2df8bb29 [file] [log] [blame]
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -05001#!/bin/bash
Sridevi Ramesh0e4d8872019-01-10 09:06:58 -06002# Refer to help text in the usage function for details.
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -05003
4CPU_SYS_DIR=/sys/devices/system/cpu/
5NR_CPUS=0
6CPU=
7SHOW_LAYOUT=0
8
9
10function usage()
11{
Patrick Williams90dfee32022-12-08 06:52:46 -060012 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 Ramesh0d88ab32017-09-21 11:07:28 -050025}
26
27function parse_args()
28{
Patrick Williams90dfee32022-12-08 06:52:46 -060029 while getopts "hL" OPTION
30 do
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -050031 case $OPTION in
Patrick Williams90dfee32022-12-08 06:52:46 -060032 L)
33 SHOW_LAYOUT=1
34 ;;
35 *)
36 usage
37 exit 1
38 ;;
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -050039 esac
Patrick Williams90dfee32022-12-08 06:52:46 -060040 done
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -050041
Patrick Williams90dfee32022-12-08 06:52:46 -060042 CPU=${!OPTIND}
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -050043}
44
45parse_args $*
46
47[ "$1" == "-h" ] && usage
48
49declare -a CPUS
50declare -a CPU_FILE
51declare -a CPU_PIR
52declare -a CORE_ID
53declare -a CHIP_ID
54
55declare -a CHIP_EQ
56declare -a CHIP_EX
57declare -a CHIP_CORES
58
59declare -A CORE_MATRIX
60
61for cpu_file in $(find $CPU_SYS_DIR -name "cpu[0-9]*")
62do
Patrick Williams90dfee32022-12-08 06:52:46 -060063 cpu=$(basename $cpu_file | tr -dc '0-9')
64 [ -n "$cpu" ] && NR_CPUS=$(expr $NR_CPUS + 1)
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -050065
Patrick Williams90dfee32022-12-08 06:52:46 -060066 CPU_VALID[$cpu]=0
67 [ -n "$CPU" ] && [ $cpu != $CPU ] && continue
68 [ ! -e $cpu_file/pir ] && continue
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -050069
Patrick Williams90dfee32022-12-08 06:52:46 -060070 CPU_VALID[$cpu]=1
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -050071
Patrick Williams90dfee32022-12-08 06:52:46 -060072 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 Ramesh0d88ab32017-09-21 11:07:28 -050077done
78
79i=0
80chip_id=-1
81core_id=-1
82prev_eq_id=-1
83prev_ex_id=-1
84num_threads=0
85CPU_LIST=" "
86while [ $i -lt $NR_CPUS ]
87do
Patrick Williams90dfee32022-12-08 06:52:46 -060088 [ ${CPU_VALID[$i]} -eq 0 ] && i=$(expr $i + 1) && continue
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -050089
Patrick Williams90dfee32022-12-08 06:52:46 -060090 [ "$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 Ramesh0d88ab32017-09-21 11:07:28 -050097 fi
Patrick Williams90dfee32022-12-08 06:52:46 -060098 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 Ramesh0d88ab32017-09-21 11:07:28 -0500121
Patrick Williams90dfee32022-12-08 06:52:46 -0600122 i=$(expr $i + 1)
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -0500123done
124echo "THREADS: $num_threads CPUs: $CPU_LIST"
125
126echo
127echo "-----------------------------"
128for chip_id in ${!CHIP_CORES[@]}
129do
Patrick Williams90dfee32022-12-08 06:52:46 -0600130 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 Ramesh0d88ab32017-09-21 11:07:28 -0500137done
138echo "-----------------------------"
139
140
141[ $SHOW_LAYOUT -eq 0 ] && exit
142
143# Print chip layout
144
145function print_header()
146{
Patrick Williams90dfee32022-12-08 06:52:46 -0600147 local _row_=$1
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -0500148
Patrick Williams90dfee32022-12-08 06:52:46 -0600149 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 Ramesh0d88ab32017-09-21 11:07:28 -0500155}
156
157function print_core_info()
158{
Patrick Williams90dfee32022-12-08 06:52:46 -0600159 local _row_=$1
160 local _cpos_=$2 # Core position
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -0500161
Patrick Williams90dfee32022-12-08 06:52:46 -0600162 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 Ramesh0d88ab32017-09-21 11:07:28 -0500169 then
Patrick Williams90dfee32022-12-08 06:52:46 -0600170 echo -n " |EX-$ex C$core_id_str|"
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -0500171 else
Patrick Williams90dfee32022-12-08 06:52:46 -0600172 echo -n " | |"
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -0500173 fi
Patrick Williams90dfee32022-12-08 06:52:46 -0600174 done
175 echo
176 if [ $_cpos_ -eq 3 ]
177 then
178 echo " +-----------+ +-----------+ +-----------+"
179 else
180 echo " + - - - - - + + - - - - - + + - - - - - +"
181 fi
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -0500182}
183
184echo
185echo "----------Processor Layout-------------------"
186for chip_id in ${!CHIP_CORES[@]}
187do
Patrick Williams90dfee32022-12-08 06:52:46 -0600188 echo "p[$chip_id]"
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -0500189
Patrick Williams90dfee32022-12-08 06:52:46 -0600190 for row in 0 1
191 do
192 print_header $row
193 for core_pos in 0 1 2 3
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -0500194 do
Patrick Williams90dfee32022-12-08 06:52:46 -0600195 print_core_info $row $core_pos
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -0500196 done
197 echo
198
Patrick Williams90dfee32022-12-08 06:52:46 -0600199 done
200 echo
201
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -0500202done