]>
Commit | Line | Data |
---|---|---|
de72bd91 MT |
1 | #!/bin/bash |
2 | ############################################################################### | |
3 | # # | |
4 | # IPFire.org - A linux based firewall # | |
5 | # Copyright (C) 2016 IPFire Network Development Team # | |
6 | # # | |
7 | # This program is free software: you can redistribute it and/or modify # | |
8 | # it under the terms of the GNU General Public License as published by # | |
9 | # the Free Software Foundation, either version 3 of the License, or # | |
10 | # (at your option) any later version. # | |
11 | # # | |
12 | # This program is distributed in the hope that it will be useful, # | |
13 | # but WITHOUT ANY WARRANTY; without even the implied warranty of # | |
14 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # | |
15 | # GNU General Public License for more details. # | |
16 | # # | |
17 | # You should have received a copy of the GNU General Public License # | |
18 | # along with this program. If not, see <http://www.gnu.org/licenses/>. # | |
19 | # # | |
20 | ############################################################################### | |
21 | ||
22 | interrupts_list() { | |
5c53e53e MT |
23 | local interrupt |
24 | for interrupt in list_directory "/proc/irq"; do | |
25 | if interrupt_exists "${interrupt}"; then | |
26 | print "${interrupt}" | |
27 | fi | |
28 | done | |
29 | } | |
30 | ||
31 | interrupt_exists() { | |
32 | local interrupt="${1}" | |
33 | ||
34 | [ -d "/proc/irq/${interrupt}" ] | |
de72bd91 MT |
35 | } |
36 | ||
37 | interrupt_use_smp_affinity() { | |
38 | local processors=$(system_get_processors) | |
39 | ||
40 | # There is no point in this feature when there is only one processor | |
41 | [ ${processors} -eq 1 ] && return ${EXIT_FALSE} | |
42 | ||
43 | return ${EXIT_TRUE} | |
44 | } | |
45 | ||
46 | __interrupts_for() { | |
47 | local path=${1} | |
48 | ||
49 | local f | |
50 | for f in ${path}; do | |
51 | [ -d "${f}" ] || continue | |
52 | ||
53 | local interrupt=$(dirname ${f}) | |
54 | basename ${interrupt} | |
55 | done | |
56 | } | |
57 | ||
58 | interrupts_for_device() { | |
59 | assert [ $# -eq 1 ] | |
60 | ||
61 | local device=${1} | |
62 | ||
63 | __interrupts_for "/proc/irq/*/${device}" | |
64 | } | |
65 | ||
66 | interrupts_for_device_queue() { | |
67 | assert [ $# -eq 2 ] | |
68 | ||
69 | local device="${1}" | |
70 | local queue="${2}" | |
71 | ||
72 | __interrupts_for "/proc/irq/*/${device}-[rt]x${queue}" | |
73 | } | |
74 | ||
75 | interrupt_get_smp_affinity() { | |
76 | assert [ $# -eq 1 ] | |
77 | ||
78 | local interrupt=${1} | |
79 | ||
80 | local path="/proc/irq/${interrupt}/smp_affinity" | |
81 | assert [ -r "${path}" ] | |
82 | ||
83 | # Convert bitmap to list of processors | |
4115b00b | 84 | __bitmap_to_processor_ids $(<${path}) |
de72bd91 MT |
85 | } |
86 | ||
4115b00b | 87 | __bitmap_to_processor_ids() { |
de72bd91 MT |
88 | local bitmap=${1} |
89 | ||
90 | # This function shifts the bit map to the right | |
91 | # and if the least significant bit equals one | |
92 | # the index of that bit is returned. | |
93 | ||
94 | local id=0 | |
95 | while [ $(( 0x${bitmap} )) -gt 0 ]; do | |
96 | if [ $(( 0x${bitmap} & 0x1 )) -eq 1 ]; then | |
97 | print "${id}" | |
98 | fi | |
99 | ||
100 | bitmap=$(( 0x${bitmap} >> 1 )) | |
101 | ((id++)) | |
102 | done | |
103 | } | |
104 | ||
105 | __processor_id_to_bitmap() { | |
9e6e8d86 | 106 | hex $(( 1 << $@ )) |
de72bd91 MT |
107 | } |
108 | ||
109 | interrupt_set_smp_affinity() { | |
110 | assert [ $# -eq 2 ] | |
111 | ||
112 | local interrupt=${1} | |
113 | local processor=${2} | |
114 | ||
115 | # Processor ID must be greater or equal than zero | |
116 | # and not larger than the highest processor index | |
117 | local num_processors=$(system_get_processors) | |
118 | if [ ${processor} -ge ${num_processors} ]; then | |
119 | error "Invalid processor ID ${processor}" | |
120 | return ${EXIT_ERROR} | |
121 | fi | |
122 | ||
123 | local path="/proc/irq/${interrupt}/smp_affinity" | |
124 | assert [ -w "${path}" ] | |
125 | ||
126 | log DEBUG "Setting SMP affinity for interrupt ${interrupt} to processor ${processor}" | |
127 | ||
128 | # Write processor ID as hex value | |
129 | __processor_id_to_bitmap ${processor} > ${path} | |
130 | } | |
131 | ||
132 | interrupt_choose_least_busy_processor() { | |
133 | local processors=$(system_get_processors) | |
134 | local -a interrupts | |
135 | ||
136 | # Create an array with the number of interrupts | |
137 | # already handled by each processor | |
138 | ||
139 | local i | |
140 | for i in $(range ${processors}); do | |
141 | interrupts[${i}]=0 | |
142 | done | |
143 | ||
144 | local processor interrupt | |
145 | for interrupt in $(interrupts_list); do | |
146 | for processor in $(interrupt_get_smp_affinity ${interrupt}); do | |
147 | interrupts[${processor}]=$(( ${interrupts[${processor}]} + 1 )) | |
148 | done | |
149 | done | |
150 | ||
151 | # Walk through that map and find the first processor with the | |
152 | # smallest number of interrupts handled so far | |
153 | ||
154 | local least_busy_index=0 | |
155 | for i in $(range ${processors}); do | |
156 | if [ ${interrupts[${least_busy_index}]} -gt ${interrupts[${i}]} ]; then | |
157 | least_busy_index=${i} | |
158 | fi | |
159 | done | |
160 | ||
161 | print "${least_busy_index}" | |
162 | } |