]>
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() { | |
60b1f378 | 23 | list_directory "/proc/irq" |
de72bd91 MT |
24 | } |
25 | ||
26 | interrupt_use_smp_affinity() { | |
27 | local processors=$(system_get_processors) | |
28 | ||
29 | # There is no point in this feature when there is only one processor | |
30 | [ ${processors} -eq 1 ] && return ${EXIT_FALSE} | |
31 | ||
32 | return ${EXIT_TRUE} | |
33 | } | |
34 | ||
35 | __interrupts_for() { | |
36 | local path=${1} | |
37 | ||
38 | local f | |
39 | for f in ${path}; do | |
40 | [ -d "${f}" ] || continue | |
41 | ||
42 | local interrupt=$(dirname ${f}) | |
43 | basename ${interrupt} | |
44 | done | |
45 | } | |
46 | ||
47 | interrupts_for_device() { | |
48 | assert [ $# -eq 1 ] | |
49 | ||
50 | local device=${1} | |
51 | ||
52 | __interrupts_for "/proc/irq/*/${device}" | |
53 | } | |
54 | ||
55 | interrupts_for_device_queue() { | |
56 | assert [ $# -eq 2 ] | |
57 | ||
58 | local device="${1}" | |
59 | local queue="${2}" | |
60 | ||
61 | __interrupts_for "/proc/irq/*/${device}-[rt]x${queue}" | |
62 | } | |
63 | ||
64 | interrupt_get_smp_affinity() { | |
65 | assert [ $# -eq 1 ] | |
66 | ||
67 | local interrupt=${1} | |
68 | ||
69 | local path="/proc/irq/${interrupt}/smp_affinity" | |
70 | assert [ -r "${path}" ] | |
71 | ||
72 | # Convert bitmap to list of processors | |
4115b00b | 73 | __bitmap_to_processor_ids $(<${path}) |
de72bd91 MT |
74 | } |
75 | ||
4115b00b | 76 | __bitmap_to_processor_ids() { |
de72bd91 MT |
77 | local bitmap=${1} |
78 | ||
79 | # This function shifts the bit map to the right | |
80 | # and if the least significant bit equals one | |
81 | # the index of that bit is returned. | |
82 | ||
83 | local id=0 | |
84 | while [ $(( 0x${bitmap} )) -gt 0 ]; do | |
85 | if [ $(( 0x${bitmap} & 0x1 )) -eq 1 ]; then | |
86 | print "${id}" | |
87 | fi | |
88 | ||
89 | bitmap=$(( 0x${bitmap} >> 1 )) | |
90 | ((id++)) | |
91 | done | |
92 | } | |
93 | ||
94 | __processor_id_to_bitmap() { | |
2212045f | 95 | hex $(( 1 << "$@" )) |
de72bd91 MT |
96 | } |
97 | ||
98 | interrupt_set_smp_affinity() { | |
99 | assert [ $# -eq 2 ] | |
100 | ||
101 | local interrupt=${1} | |
102 | local processor=${2} | |
103 | ||
104 | # Processor ID must be greater or equal than zero | |
105 | # and not larger than the highest processor index | |
106 | local num_processors=$(system_get_processors) | |
107 | if [ ${processor} -ge ${num_processors} ]; then | |
108 | error "Invalid processor ID ${processor}" | |
109 | return ${EXIT_ERROR} | |
110 | fi | |
111 | ||
112 | local path="/proc/irq/${interrupt}/smp_affinity" | |
113 | assert [ -w "${path}" ] | |
114 | ||
115 | log DEBUG "Setting SMP affinity for interrupt ${interrupt} to processor ${processor}" | |
116 | ||
117 | # Write processor ID as hex value | |
118 | __processor_id_to_bitmap ${processor} > ${path} | |
119 | } | |
120 | ||
121 | interrupt_choose_least_busy_processor() { | |
122 | local processors=$(system_get_processors) | |
123 | local -a interrupts | |
124 | ||
125 | # Create an array with the number of interrupts | |
126 | # already handled by each processor | |
127 | ||
128 | local i | |
129 | for i in $(range ${processors}); do | |
130 | interrupts[${i}]=0 | |
131 | done | |
132 | ||
133 | local processor interrupt | |
134 | for interrupt in $(interrupts_list); do | |
135 | for processor in $(interrupt_get_smp_affinity ${interrupt}); do | |
136 | interrupts[${processor}]=$(( ${interrupts[${processor}]} + 1 )) | |
137 | done | |
138 | done | |
139 | ||
140 | # Walk through that map and find the first processor with the | |
141 | # smallest number of interrupts handled so far | |
142 | ||
143 | local least_busy_index=0 | |
144 | for i in $(range ${processors}); do | |
145 | if [ ${interrupts[${least_busy_index}]} -gt ${interrupts[${i}]} ]; then | |
146 | least_busy_index=${i} | |
147 | fi | |
148 | done | |
149 | ||
150 | print "${least_busy_index}" | |
151 | } |