forked from Irqbalance/irqbalance
-
Notifications
You must be signed in to change notification settings - Fork 0
/
irqlist.c
207 lines (176 loc) · 6.3 KB
/
irqlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* Copyright (C) 2006, Intel Corporation
* Copyright (C) 2012, Neil Horman <[email protected]>
*
* This file is part of irqbalance
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*/
/*
* This file has the basic functions to manipulate interrupt metadata
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <math.h>
#include "types.h"
#include "irqbalance.h"
struct load_balance_info {
unsigned long long int total_load;
unsigned long long avg_load;
unsigned long long min_load;
unsigned long long adjustment_load;
int load_sources;
unsigned long long int deviations;
long double std_deviation;
unsigned int num_over;
unsigned int num_under;
unsigned int num_powersave;
struct topo_obj *powersave;
};
static void gather_load_stats(struct topo_obj *obj, void *data)
{
struct load_balance_info *info = data;
if (info->load_sources == 0 || obj->load < info->min_load)
info->min_load = obj->load;
info->total_load += obj->load;
info->load_sources += 1;
}
static void compute_deviations(struct topo_obj *obj, void *data)
{
struct load_balance_info *info = data;
unsigned long long int deviation;
deviation = (obj->load > info->avg_load) ?
obj->load - info->avg_load :
info->avg_load - obj->load;
info->deviations += (deviation * deviation);
}
static void move_candidate_irqs(struct irq_info *info, void *data)
{
struct load_balance_info *lb_info = data;
unsigned long delta_load = 0;
/* Don't rebalance irqs that don't want it */
if (info->level == BALANCE_NONE)
return;
/* Don't move cpus that only have one irq, regardless of load */
if (g_list_length(info->assigned_obj->interrupts) <= 1)
return;
/* IRQs with a load of 1 have most likely not had any interrupts and
* aren't worth migrating
*/
if (info->load <= 1)
return;
if (migrate_ratio > 0) {
delta_load = (lb_info->adjustment_load - lb_info->min_load) / migrate_ratio;
}
/* If we can migrate an irq without swapping the imbalance do it. */
if ((lb_info->min_load + info->load) < delta_load + (lb_info->adjustment_load - info->load)) {
lb_info->adjustment_load -= info->load;
lb_info->min_load += info->load;
if (lb_info->min_load > lb_info->adjustment_load) {
lb_info->min_load = lb_info->adjustment_load;
}
} else
return;
log(TO_CONSOLE, LOG_INFO, "Selecting irq %d for rebalancing\n", info->irq);
migrate_irq(&info->assigned_obj->interrupts, &rebalance_irq_list, info);
info->assigned_obj = NULL;
}
static void migrate_overloaded_irqs(struct topo_obj *obj, void *data)
{
struct load_balance_info *info = data;
if (obj->powersave_mode)
info->num_powersave++;
if ((obj->load + info->std_deviation) <= info->avg_load) {
info->num_under++;
if (power_thresh != ULONG_MAX && !info->powersave)
if (!obj->powersave_mode)
info->powersave = obj;
} else if ((obj->load - info->std_deviation) >=info->avg_load) {
info->num_over++;
}
if ((obj->load > info->min_load) &&
(g_list_length(obj->interrupts) > 1)) {
/* order the list from greatest to least workload */
sort_irq_list(&obj->interrupts);
/*
* Each irq carries a weighted average amount of load
* we think it's responsible for. This object's load is larger
* than the object with the minimum load. Select irqs for
* migration if we could move them to the minimum object
* without reversing the imbalance or until we only have one
* left.
*/
info->adjustment_load = obj->load;
for_each_irq(obj->interrupts, move_candidate_irqs, info);
}
}
static void force_irq_migration(struct irq_info *info, void *data __attribute__((unused)))
{
migrate_irq(&info->assigned_obj->interrupts, &rebalance_irq_list, info);
info->assigned_obj = NULL;
}
static void clear_powersave_mode(struct topo_obj *obj, void *data __attribute__((unused)))
{
obj->powersave_mode = 0;
}
static void find_overloaded_objs(GList *name, struct load_balance_info *info) {
memset(info, 0, sizeof(struct load_balance_info));
for_each_object(name, gather_load_stats, info);
info->load_sources = (info->load_sources == 0) ? 1 : (info->load_sources);
info->avg_load = info->total_load / info->load_sources;
for_each_object(name, compute_deviations, info);
/* Don't divide by zero if there is a single load source */
if (info->load_sources == 1)
info->std_deviation = 0;
else {
info->std_deviation = (long double)(info->deviations / (info->load_sources - 1));
info->std_deviation = sqrt(info->std_deviation);
}
for_each_object(name, migrate_overloaded_irqs, info);
}
void update_migration_status(void)
{
struct load_balance_info info;
find_overloaded_objs(cpus, &info);
if (power_thresh != ULONG_MAX && cycle_count > 5) {
if (!info.num_over && (info.num_under >= power_thresh) && info.powersave) {
log(TO_ALL, LOG_INFO, "cpu %d entering powersave mode\n", info.powersave->number);
info.powersave->powersave_mode = 1;
if (g_list_length(info.powersave->interrupts) > 0)
for_each_irq(info.powersave->interrupts, force_irq_migration, NULL);
} else if ((info.num_over) && (info.num_powersave)) {
log(TO_ALL, LOG_INFO, "Load average increasing, re-enabling all cpus for irq balancing\n");
for_each_object(cpus, clear_powersave_mode, NULL);
}
}
find_overloaded_objs(cache_domains, &info);
find_overloaded_objs(packages, &info);
find_overloaded_objs(numa_nodes, &info);
}
static void dump_workload(struct irq_info *info, void *unused __attribute__((unused)))
{
log(TO_CONSOLE, LOG_INFO, "Interrupt %i node_num %d (class %s) has workload %lu \n",
info->irq, irq_numa_node(info)->number, classes[info->class], (unsigned long)info->load);
}
void dump_workloads(void)
{
for_each_irq(NULL, dump_workload, NULL);
}