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