blob: 33710eaff15f3829f8bf3be877ed72ec3bebf738 [file] [log] [blame]
Sridevi Ramesh0d88ab32017-09-21 11:07:28 -05001#!/bin/bash
2
3CPU_SYS_DIR=/sys/devices/system/cpu/
4NR_CPUS=0
5CPU=
6SHOW_LAYOUT=0
7
8
9function 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
24function 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
42parse_args $*
43
44[ "$1" == "-h" ] && usage
45
46declare -a CPUS
47declare -a CPU_FILE
48declare -a CPU_PIR
49declare -a CORE_ID
50declare -a CHIP_ID
51
52declare -a CHIP_EQ
53declare -a CHIP_EX
54declare -a CHIP_CORES
55
56declare -A CORE_MATRIX
57
58for cpu_file in $(find $CPU_SYS_DIR -name "cpu[0-9]*")
59do
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); }')
74done
75
76i=0
77chip_id=-1
78core_id=-1
79prev_eq_id=-1
80prev_ex_id=-1
81num_threads=0
82CPU_LIST=" "
83while [ $i -lt $NR_CPUS ]
84do
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)
120done
121echo "THREADS: $num_threads CPUs: $CPU_LIST"
122
123echo
124echo "-----------------------------"
125for chip_id in ${!CHIP_CORES[@]}
126do
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]}]"
134done
135echo "-----------------------------"
136
137
138[ $SHOW_LAYOUT -eq 0 ] && exit
139
140# Print chip layout
141
142function 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
154function 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
181echo
182echo "----------Processor Layout-------------------"
183for chip_id in ${!CHIP_CORES[@]}
184do
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
199done