blob: a1fa03cb898c3e509948e5c16aab7b0553ec5146 [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{
Sridevi Ramesh0e4d8872019-01-10 09:06:58 -060012 echo "This script runs on HOST OS and gives core-id"
13 echo "corresponding to the chip-id."
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -050014 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
25}
26
27function parse_args()
28{
29 while getopts "hL" OPTION
30 do
31 case $OPTION in
32 L)
33 SHOW_LAYOUT=1
34 ;;
35 *)
36 usage
37 exit 1
38 ;;
39 esac
40 done
41
42 CPU=${!OPTIND}
43}
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
63 cpu=$(basename $cpu_file | tr -dc '0-9')
64 [ -n "$cpu" ] && NR_CPUS=$(expr $NR_CPUS + 1)
65
66 CPU_VALID[$cpu]=0
67 [ -n "$CPU" ] && [ $cpu != $CPU ] && continue
68 [ ! -e $cpu_file/pir ] && continue
69
70 CPU_VALID[$cpu]=1
71
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); }')
77done
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
88 [ ${CPU_VALID[$i]} -eq 0 ] && i=$(expr $i + 1) && continue
89
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=" "
97 fi
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]}
121
122 i=$(expr $i + 1)
123done
124echo "THREADS: $num_threads CPUs: $CPU_LIST"
125
126echo
127echo "-----------------------------"
128for chip_id in ${!CHIP_CORES[@]}
129do
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]}]"
137done
138echo "-----------------------------"
139
140
141[ $SHOW_LAYOUT -eq 0 ] && exit
142
143# Print chip layout
144
145function print_header()
146{
147 local _row_=$1
148
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
155}
156
157function print_core_info()
158{
159 local _row_=$1
160 local _cpos_=$2 # Core position
161
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]}" ]
169 then
170 echo -n " |EX-$ex C$core_id_str|"
171 else
172 echo -n " | |"
173 fi
174 done
175 echo
176 if [ $_cpos_ -eq 3 ]
177 then
178 echo " +-----------+ +-----------+ +-----------+"
179 else
180 echo " + - - - - - + + - - - - - + + - - - - - +"
181 fi
182}
183
184echo
185echo "----------Processor Layout-------------------"
186for chip_id in ${!CHIP_CORES[@]}
187do
188 echo "p[$chip_id]"
189
190 for row in 0 1
191 do
192 print_header $row
193 for core_pos in 0 1 2 3
194 do
195 print_core_info $row $core_pos
196 done
197 echo
198
199 done
200 echo
201
202done