forked from heechul/memguard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
memguard.c
1595 lines (1330 loc) · 42.8 KB
/
memguard.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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Memory bandwidth controller for multi-core systems
*
* Copyright (C) 2013 Heechul Yun <heechul@illinois.edu>
*
* This file is distributed under the University of Illinois Open Source
* License. See LICENSE.TXT for details.
*
*/
/**************************************************************************
* Conditional Compilation Options
**************************************************************************/
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
#define USE_DEBUG 1
/**************************************************************************
* Included Files
**************************************************************************/
#include <linux/version.h>
#include <generated/uapi/linux/version.h>
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/hrtimer.h>
#include <linux/ktime.h>
#include <linux/smp.h> /* IPI calls */
#include <linux/irq_work.h>
#include <linux/hardirq.h>
#include <linux/perf_event.h>
#include <linux/delay.h>
#include <linux/debugfs.h>
#include <linux/seq_file.h>
#include <asm/atomic.h>
#include <linux/slab.h>
#include <linux/vmalloc.h>
#include <linux/uaccess.h>
#include <linux/notifier.h>
#include <linux/kthread.h>
#include <linux/printk.h>
#include <linux/interrupt.h>
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 8, 0)
# include <linux/sched/rt.h>
#endif
#include <linux/cpu.h>
#include <asm/idle.h>
/**************************************************************************
* Public Definitions
**************************************************************************/
#define MAX_NCPUS 32
#define CACHE_LINE_SIZE 64
#if USE_DEBUG
# define DEBUG(x) x
# define DEBUG_RECLAIM(x) x
# define DEBUG_USER(x) x
#else
# define DEBUG(x)
# define DEBUG_RECLAIM(x)
# define DEBUG_USER(x)
#endif
/**************************************************************************
* Public Types
**************************************************************************/
struct memstat{
u64 used_budget; /* used budget*/
u64 assigned_budget;
u64 throttled_time_ns;
int throttled; /* throttled period count */
u64 throttled_error; /* throttled & error */
int throttled_error_dist[10]; /* pct distribution */
int exclusive; /* exclusive period count */
u64 exclusive_ns; /* exclusive mode real-time duration */
u64 exclusive_bw; /* exclusive mode used bandwidth */
};
/* percpu info */
struct core_info {
/* user configurations */
int budget; /* assigned budget */
int limit; /* limit mode (exclusive to weight)*/
int weight; /* weight mode (exclusive to limit)*/
int wsum; /* local copy of global->wsum */
/* for control logic */
int cur_budget; /* currently available budget */
volatile struct task_struct * throttled_task;
ktime_t throttled_time; /* absolute time when throttled */
u64 old_val; /* hold previous counter value */
int prev_throttle_error; /* check whether there was throttle error in
the previous period */
u64 exclusive_vtime; /* exclusive mode vtime for scheduling */
int exclusive_mode; /* 1 - if in exclusive mode */
ktime_t exclusive_time; /* time when exclusive mode begins */
struct irq_work pending; /* delayed work for NMIs */
struct perf_event *event;/* performance counter i/f */
struct task_struct *throttle_thread; /* forced throttle idle thread */
wait_queue_head_t throttle_evt; /* throttle wait queue */
/* statistics */
struct memstat overall; /* stat for overall periods. reset by user */
int used[3]; /* EWMA memory load */
long period_cnt; /* active periods count */
};
/* global info */
struct memguard_info {
int master;
ktime_t period_in_ktime;
int start_tick;
int budget; /* reclaimed budget */
long period_cnt;
spinlock_t lock;
int max_budget; /* \sum(cinfo->budget) */
cpumask_var_t throttle_mask;
cpumask_var_t active_mask;
atomic_t wsum;
long cpuhog_cnt;
struct hrtimer hr_timer;
};
/**************************************************************************
* Global Variables
**************************************************************************/
static struct memguard_info memguard_info;
static struct core_info __percpu *core_info;
static char *g_hw_type = "";
static int g_period_us = 1000;
static int g_use_reclaim = 0; /* minimum remaining time to reclaim */
static int g_use_exclusive = 0;
static int g_use_task_priority = 0;
static int g_budget_pct[MAX_NCPUS];
static int g_budget_cnt = 4;
static int g_budget_min_value = 1000;
static int g_budget_max_bw = 2100; /* MB/s. best=6000 MB/s, worst=2100 MB/s */
static struct dentry *memguard_dir;
static int g_test = 0;
/* copied from kernel/sched/sched.h */
static const int prio_to_weight[40] = {
/* -20 */ 88761, 71755, 56483, 46273, 36291,
/* -15 */ 29154, 23254, 18705, 14949, 11916,
/* -10 */ 9548, 7620, 6100, 4904, 3906,
/* -5 */ 3121, 2501, 1991, 1586, 1277,
/* 0 */ 1024, 820, 655, 526, 423,
/* 5 */ 335, 272, 215, 172, 137,
/* 10 */ 110, 87, 70, 56, 45,
/* 15 */ 36, 29, 23, 18, 15,
};
/**************************************************************************
* External Function Prototypes
**************************************************************************/
extern unsigned long nr_running_cpu(int cpu);
extern int idle_cpu(int cpu);
/**************************************************************************
* Local Function Prototypes
**************************************************************************/
static int self_test(void);
static void __reset_stats(void *info);
static void period_timer_callback_slave(void *info);
enum hrtimer_restart period_timer_callback_master(struct hrtimer *timer);
static void memguard_process_overflow(struct irq_work *entry);
static int throttle_thread(void *arg);
/**************************************************************************
* Module parameters
**************************************************************************/
module_param(g_test, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(g_test, "number of test iterations");
module_param(g_hw_type, charp, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(g_hw_type, "hardware type");
module_param(g_use_reclaim, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(g_use_reclaim, "enable/disable reclaim");
module_param(g_period_us, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(g_period_us, "throttling period in usec");
module_param_array(g_budget_pct, int, &g_budget_cnt, 0000);
MODULE_PARM_DESC(g_budget_pct, "array of budget per cpu");
module_param(g_budget_max_bw, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(g_budget_max_bw, "maximum memory bandwidth (MB/s)");
/**************************************************************************
* Module main code
**************************************************************************/
static int __cpuinit memguard_cpu_callback(struct notifier_block *nfb,
unsigned long action, void *hcpu)
{
unsigned int cpu = (unsigned long)hcpu;
switch (action) {
case CPU_ONLINE:
case CPU_ONLINE_FROZEN:
trace_printk("CPU%d is online\n", cpu);
break;
case CPU_DEAD:
case CPU_DEAD_FROZEN:
trace_printk("CPU%d is offline\n", cpu);
break;
}
return NOTIFY_OK;
}
static struct notifier_block __cpuinitdata memguard_cpu_notifier =
{
.notifier_call = memguard_cpu_callback,
};
/** convert MB/s to #of events (i.e., LLC miss counts) per 1ms */
static inline u64 convert_mb_to_events(int mb)
{
return div64_u64((u64)mb*1024*1024,
CACHE_LINE_SIZE* (1000000/g_period_us));
}
static inline int convert_events_to_mb(u64 events)
{
int divisor = 1024*1024;
int mb = div64_u64(events*CACHE_LINE_SIZE*1000 + (divisor-1), divisor);
return mb;
}
static inline void print_current_context(void)
{
trace_printk("in_interrupt(%ld)(hard(%ld),softirq(%d)"
",in_nmi(%d)),irqs_disabled(%d)\n",
in_interrupt(), in_irq(), (int)in_softirq(),
(int)in_nmi(), (int)irqs_disabled());
}
/** read current counter value. */
static inline u64 perf_event_count(struct perf_event *event)
{
return local64_read(&event->count) +
atomic64_read(&event->child_count);
}
/** return used event in the current period */
static inline u64 memguard_event_used(struct core_info *cinfo)
{
return perf_event_count(cinfo->event) - cinfo->old_val;
}
static void print_core_info(int cpu, struct core_info *cinfo)
{
pr_info("CPU%d: budget: %d, cur_budget: %d, period: %ld\n",
cpu, cinfo->budget, cinfo->cur_budget, cinfo->period_cnt);
}
/**
* update per-core usage statistics
*/
void update_statistics(struct core_info *cinfo)
{
/* counter must be stopped by now. */
s64 new;
int used;
u64 exclusive_vtime = 0;
new = perf_event_count(cinfo->event);
used = (int)(new - cinfo->old_val);
cinfo->old_val = new;
cinfo->overall.used_budget += used;
cinfo->overall.assigned_budget += cinfo->budget;
/* EWMA filtered per-core usage statistics */
cinfo->used[0] = used;
cinfo->used[1] = (cinfo->used[1] * (2-1) + used) >> 1;
/* used[1]_k = 1/2 used[1]_k-1 + 1/2 used */
cinfo->used[2] = (cinfo->used[2] * (4-1) + used) >> 2;
/* used[2]_k = 3/4 used[2]_k-1 + 1/4 used */
/* core is currently throttled. */
if (cinfo->throttled_task) {
cinfo->overall.throttled_time_ns +=
(ktime_get().tv64 - cinfo->throttled_time.tv64);
cinfo->overall.throttled++;
}
/* throttling error condition:
I was too aggressive in giving up "unsed" budget */
if (cinfo->prev_throttle_error && used < cinfo->budget) {
int diff = cinfo->budget - used;
int idx;
cinfo->overall.throttled_error ++; // += diff;
BUG_ON(cinfo->budget == 0);
idx = (int)(diff * 10 / cinfo->budget);
cinfo->overall.throttled_error_dist[idx]++;
trace_printk("ERR: throttled_error: %d < %d\n", used, cinfo->budget);
/* compensation for error to catch-up*/
cinfo->used[1] = cinfo->budget + diff;
}
cinfo->prev_throttle_error = 0;
/* I was the lucky guy who used the DRAM exclusively */
if (cinfo->exclusive_mode) {
struct memguard_info *global = &memguard_info;
int wsum = 0;
int i;
u64 exclusive_ns, exclusive_bw;
/* used time */
exclusive_ns = (ktime_get().tv64 - cinfo->exclusive_time.tv64);
/* used bw */
exclusive_bw = (cinfo->used[0] - cinfo->budget);
if (g_use_exclusive == 3)
exclusive_vtime = exclusive_ns;
else if (g_use_exclusive == 4)
exclusive_vtime = exclusive_bw;
else
exclusive_vtime = 0;
if (cinfo->weight > 0) {
/* weighted vtime (used by scheduler on throttle) */
for_each_cpu(i, global->active_mask)
wsum += per_cpu_ptr(core_info, i)->weight;
cinfo->exclusive_vtime +=
div64_u64((u64)exclusive_vtime * wsum,
cinfo->weight);
} else
cinfo->exclusive_vtime += exclusive_vtime;
cinfo->overall.exclusive_ns += exclusive_ns;
cinfo->overall.exclusive_bw += exclusive_bw;
cinfo->exclusive_mode = 0;
cinfo->overall.exclusive++;
}
DEBUG(trace_printk("%lld %d %p CPU%d org: %d cur: %d excl(%d): %lld\n",
new, used, cinfo->throttled_task,
smp_processor_id(),
cinfo->budget,
cinfo->cur_budget,
g_use_exclusive, exclusive_vtime));
}
/**
* budget is used up. PMU generate an interrupt
* this run in hardirq, nmi context with irq disabled
*/
static void event_overflow_callback(struct perf_event *event,
#if LINUX_VERSION_CODE < KERNEL_VERSION(3, 2, 0)
int nmi,
#endif
struct perf_sample_data *data,
struct pt_regs *regs)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
BUG_ON(!cinfo);
irq_work_queue(&cinfo->pending);
}
/* must be in hardirq context */
static int donate_budget(long cur_period, int budget)
{
struct memguard_info *global = &memguard_info;
spin_lock(&global->lock);
if (global->period_cnt == cur_period) {
global->budget += budget;
}
spin_unlock(&global->lock);
return global->budget;
}
/* must be in hardirq context */
static int reclaim_budget(long cur_period, int budget)
{
struct memguard_info *global = &memguard_info;
int reclaimed = 0;
spin_lock(&global->lock);
if (global->period_cnt == cur_period) {
reclaimed = min(budget, global->budget);
global->budget -= reclaimed;
}
spin_unlock(&global->lock);
return reclaimed;
}
/**
* reclaim local budget from global budget pool
*/
static int request_budget(struct core_info *cinfo)
{
int amount = 0;
int budget_used = memguard_event_used(cinfo);
BUG_ON(!cinfo);
if (budget_used < cinfo->budget && current->policy == SCHED_NORMAL) {
/* didn't used up my original budget */
amount = cinfo->budget - budget_used;
} else {
/* I'm requesting more than I originall assigned */
amount = g_budget_min_value;
}
if (amount > 0) {
/* successfully reclaim my budget */
amount = reclaim_budget(cinfo->period_cnt, amount);
}
return amount;
}
/**
* called by process_overflow
*/
static void __unthrottle_core(void *info)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
if (cinfo->throttled_task) {
cinfo->exclusive_mode = 1;
cinfo->exclusive_time = ktime_get();
cinfo->throttled_task = NULL;
smp_wmb();
DEBUG_RECLAIM(trace_printk("exclusive mode begin\n"));
}
}
static void __newperiod(void *info)
{
long period = (long)info;
ktime_t start = ktime_get();
struct memguard_info *global = &memguard_info;
spin_lock(&global->lock);
if (period == global->period_cnt) {
ktime_t new_expire = ktime_add(start, global->period_in_ktime);
long new_period = ++global->period_cnt;
global->budget = 0;
spin_unlock(&global->lock);
/* arrived before timer interrupt is called */
hrtimer_start_range_ns(&global->hr_timer, new_expire,
0, HRTIMER_MODE_ABS_PINNED);
DEBUG(trace_printk("begin new period\n"));
on_each_cpu(period_timer_callback_slave, (void *)new_period, 0);
} else
spin_unlock(&global->lock);
}
/**
* memory overflow handler.
* must not be executed in NMI context. but in hard irq context
*/
static void memguard_process_overflow(struct irq_work *entry)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
struct memguard_info *global = &memguard_info;
int amount = 0;
ktime_t start = ktime_get();
s64 budget_used;
BUG_ON(in_nmi() || !in_irq());
WARN_ON_ONCE(cinfo->budget > global->max_budget);
spin_lock(&global->lock);
if (!cpumask_test_cpu(smp_processor_id(), global->active_mask)) {
spin_unlock(&global->lock);
trace_printk("ERR: not active\n");
return;
} else if (global->period_cnt != cinfo->period_cnt) {
trace_printk("ERR: global(%ld) != local(%ld) period mismatch\n",
global->period_cnt, cinfo->period_cnt);
spin_unlock(&global->lock);
return;
}
spin_unlock(&global->lock);
budget_used = memguard_event_used(cinfo);
/* erroneous overflow, that could have happend before period timer
stop the pmu */
if (budget_used < cinfo->cur_budget) {
trace_printk("ERR: overflow in timer. used %lld < cur_budget %d. ignore\n",
budget_used, cinfo->cur_budget);
return;
}
/* try to reclaim budget from the global pool */
amount = request_budget(cinfo);
if (amount > 0) {
cinfo->cur_budget += amount;
local64_set(&cinfo->event->hw.period_left, amount);
DEBUG_RECLAIM(trace_printk("successfully reclaimed %d\n", amount));
return;
}
/* no more overflow interrupt */
local64_set(&cinfo->event->hw.period_left, 0xfffffff);
/* check if we donated too much */
if (budget_used < cinfo->budget) {
trace_printk("ERR: throttling error\n");
cinfo->prev_throttle_error = 1;
}
/* we are going to be throttled */
spin_lock(&global->lock);
cpumask_set_cpu(smp_processor_id(), global->throttle_mask);
if (cpumask_equal(global->throttle_mask, global->active_mask)) {
/* all other cores are alreay throttled */
spin_unlock(&global->lock);
if (g_use_exclusive == 1) {
/* algorithm 1: last one get all remaining time */
cinfo->exclusive_mode = 1;
cinfo->exclusive_time = ktime_get();
DEBUG_RECLAIM(trace_printk("exclusive mode begin\n"));
return;
} else if (g_use_exclusive == 2) {
/* algorithm 2: wakeup all (i.e., non regulation) */
smp_call_function(__unthrottle_core, NULL, 0);
cinfo->exclusive_mode = 1;
cinfo->exclusive_time = ktime_get();
DEBUG_RECLAIM(trace_printk("exclusive mode begin\n"));
return;
} else if (g_use_exclusive == 3 || g_use_exclusive == 4) {
/* algorithm 3: CFS based on exclusive_vtime_ns */
int target_cpu = smp_processor_id(); /* wake up cpu */
u64 min_vtime = 0;
int i;
for_each_cpu(i, global->active_mask) {
u64 cur_vtime = per_cpu_ptr(core_info, i)
->exclusive_vtime;
if (min_vtime == 0 || cur_vtime < min_vtime) {
min_vtime = cur_vtime;
target_cpu = i;
}
}
if (target_cpu == smp_processor_id()) {
cinfo->exclusive_mode = 1;
cinfo->exclusive_time = ktime_get();
DEBUG_RECLAIM(trace_printk("exclusive%d mode begin"
"vtime: %lld\n",
cinfo->exclusive_mode,
cinfo->exclusive_vtime));
return;
}
smp_call_function_single(
target_cpu, __unthrottle_core, NULL, 0);
/* i'll be throttled */
} else if (g_use_exclusive == 5) {
smp_call_function_single(global->master, __newperiod,
(void *)cinfo->period_cnt, 0);
return;
} else if (g_use_exclusive > 5) {
trace_printk("ERR: Unsupported exclusive mode %d\n",
g_use_exclusive);
return;
} else if (g_use_exclusive != 0 &&
cpumask_weight(global->active_mask) == 1) {
trace_printk("ERR: don't throttle one active core\n");
return;
}
} else
spin_unlock(&global->lock);
if (cinfo->prev_throttle_error)
return;
/*
* fail to reclaim. now throttle this core
*/
DEBUG_RECLAIM(trace_printk("fail to reclaim after %lld nsec.\n",
ktime_get().tv64 - start.tv64));
/* wake-up throttle task */
cinfo->throttled_task = current;
cinfo->throttled_time = start;
WARN_ON_ONCE(!strncmp(current->comm, "swapper", 7));
smp_mb();
wake_up_interruptible(&cinfo->throttle_evt);
}
/**
* per-core period processing
*
* called by scheduler tick to replenish budget and unthrottle if needed
* run in interrupt context (irq disabled)
*/
/*
* period_timer algorithm:
* excess = 0;
* if predict < budget:
* excess = budget - predict;
* global += excess
* set interrupt at (budget - excess)
*/
static void period_timer_callback_slave(void *info)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
struct memguard_info *global = &memguard_info;
struct task_struct *target;
long new_period = (long)info;
int cpu = smp_processor_id();
/* must be irq disabled. hard irq */
BUG_ON(!irqs_disabled());
WARN_ON_ONCE(!in_irq());
if (new_period <= cinfo->period_cnt) {
trace_printk("ERR: new_period(%ld) <= cinfo->period_cnt(%ld)\n",
new_period, cinfo->period_cnt);
return;
}
/* assign local period */
cinfo->period_cnt = new_period;
/* stop counter */
cinfo->event->pmu->stop(cinfo->event, PERF_EF_UPDATE);
if (cinfo->exclusive_vtime == 0 &&
(g_use_exclusive == 3 || g_use_exclusive == 4))
{
/* set the minimum vtime */
u64 min_vtime = 0;
int i;
for_each_cpu(i, global->active_mask) {
u64 cur_vtime =
per_cpu_ptr(core_info, i)->exclusive_vtime;
if (min_vtime == 0 || cur_vtime < min_vtime)
min_vtime = cur_vtime;
}
cinfo->exclusive_vtime = min_vtime;
}
/* I'm actively participating */
spin_lock(&global->lock);
cpumask_clear_cpu(cpu, global->throttle_mask);
cpumask_set_cpu(cpu, global->active_mask);
spin_unlock(&global->lock);
DEBUG(trace_printk("%p|New period %ld. global->budget=%d\n",
cinfo->throttled_task,
cinfo->period_cnt, global->budget));
/* update statistics. */
update_statistics(cinfo);
/* task priority to weight conversion */
if (g_use_task_priority) {
int prio = current->static_prio - MAX_RT_PRIO;
if (prio < 0)
prio = 0;
cinfo->weight = prio_to_weight[prio];
DEBUG(trace_printk("Task WGT: %d prio:%d\n", cinfo->weight, prio));
}
/* new budget assignment from user */
spin_lock(&global->lock);
if (cinfo->weight > 0) {
/* weight mode */
int wsum = 0; int i;
smp_mb();
for_each_cpu(i, global->active_mask)
wsum += per_cpu_ptr(core_info, i)->weight;
cinfo->budget =
div64_u64((u64)global->max_budget*cinfo->weight, wsum);
DEBUG(trace_printk("WGT: budget:%d/%d weight:%d/%d\n",
cinfo->budget, global->max_budget,
cinfo->weight, wsum));
} else if (cinfo->limit > 0) {
/* limit mode */
cinfo->budget = cinfo->limit;
} else {
pr_err("both limit and weight = 0");
}
if (cinfo->budget > global->max_budget)
trace_printk("ERR: c->budget(%d) > g->max_budget(%d)\n",
cinfo->budget, global->max_budget);
spin_unlock(&global->lock);
/* budget can't be zero? */
cinfo->budget = max(cinfo->budget, 1);
if (cinfo->event->hw.sample_period != cinfo->budget) {
/* new budget is assigned */
trace_printk("MSG: new budget %d is assigned\n",
cinfo->budget);
cinfo->event->hw.sample_period = cinfo->budget;
}
/* unthrottle tasks (if any) */
if (cinfo->throttled_task)
target = (struct task_struct *)cinfo->throttled_task;
else
target = current;
cinfo->throttled_task = NULL;
/* per-task donation policy */
if (!g_use_reclaim || rt_task(target)) {
cinfo->cur_budget = cinfo->budget;
DEBUG(trace_printk("HRT or !g_use_reclaim: don't donate\n"));
} else if (target->policy == SCHED_BATCH ||
target->policy == SCHED_IDLE) {
/* Non rt task: donate all */
donate_budget(cinfo->period_cnt, cinfo->budget);
cinfo->cur_budget = 0;
DEBUG(trace_printk("NonRT: donate all %d\n", cinfo->budget));
} else if (target->policy == SCHED_NORMAL) {
BUG_ON(rt_task(target));
if (cinfo->used[1] < cinfo->budget) {
/* donate 'expected surplus' ahead of time. */
int surplus = max(cinfo->budget - cinfo->used[1], 0);
WARN_ON_ONCE(surplus > global->max_budget);
donate_budget(cinfo->period_cnt, surplus);
cinfo->cur_budget = cinfo->budget - surplus;
DEBUG(trace_printk("SRT: surplus: %d, budget: %d\n", surplus,
cinfo->budget));
} else {
cinfo->cur_budget = cinfo->budget;
DEBUG(trace_printk("SRT: don't donate\n"));
}
}
/* setup an interrupt */
cinfo->cur_budget = max(1, cinfo->cur_budget);
local64_set(&cinfo->event->hw.period_left, cinfo->cur_budget);
/* enable performance counter */
cinfo->event->pmu->start(cinfo->event, PERF_EF_RELOAD);
}
static void __init_per_core(void *info)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
memset(cinfo, 0, sizeof(struct core_info));
smp_rmb();
/* initialize per_event structure */
cinfo->event = (struct perf_event *)info;
/* initialize budget */
cinfo->budget = cinfo->limit = cinfo->event->hw.sample_period;
/* create idle threads */
cinfo->throttled_task = NULL;
init_waitqueue_head(&cinfo->throttle_evt);
/* initialize statistics */
__reset_stats(cinfo);
print_core_info(smp_processor_id(), cinfo);
smp_wmb();
/* initialize nmi irq_work_queue */
init_irq_work(&cinfo->pending, memguard_process_overflow);
}
/**
* called while cpu_base->lock is held by hrtimer_interrupt()
*/
enum hrtimer_restart period_timer_callback_master(struct hrtimer *timer)
{
struct memguard_info *global = &memguard_info;
ktime_t now;
int orun;
long new_period;
cpumask_var_t active_mask;
now = timer->base->get_time();
DEBUG(trace_printk("master begin\n"));
BUG_ON(smp_processor_id() != global->master);
orun = hrtimer_forward(timer, now, global->period_in_ktime);
if (orun == 0)
return HRTIMER_RESTART;
spin_lock(&global->lock);
global->period_cnt += orun;
global->budget = 0;
new_period = global->period_cnt;
cpumask_copy(active_mask, global->active_mask);
spin_unlock(&global->lock);
DEBUG(trace_printk("spinlock end\n"));
if (orun > 1)
trace_printk("ERR: timer overrun %d at period %ld\n",
orun, new_period);
on_each_cpu_mask(active_mask,
period_timer_callback_slave, (void *)new_period, 0);
DEBUG(trace_printk("master end\n"));
return HRTIMER_RESTART;
}
static struct perf_event *init_counter(int cpu, int budget)
{
struct perf_event *event = NULL;
struct perf_event_attr sched_perf_hw_attr = {
/* use generalized hardware abstraction */
.type = PERF_TYPE_HARDWARE,
.config = PERF_COUNT_HW_CACHE_MISSES,
.size = sizeof(struct perf_event_attr),
.pinned = 1,
.disabled = 1,
.exclude_kernel = 1, /* TODO: 1 mean, no kernel mode counting */
.pinned = 1,
};
if (!strcmp(g_hw_type, "core2")) {
sched_perf_hw_attr.type = PERF_TYPE_RAW;
sched_perf_hw_attr.config = 0x7024; /* 7024 - incl. prefetch
5024 - only prefetch
4024 - excl. prefetch */
} else if (!strcmp(g_hw_type, "snb")) {
sched_perf_hw_attr.type = PERF_TYPE_RAW;
sched_perf_hw_attr.config = 0x08b0; /* 08b0 - incl. prefetch */
} else if (!strcmp(g_hw_type, "soft")) {
sched_perf_hw_attr.type = PERF_TYPE_SOFTWARE;
sched_perf_hw_attr.config = PERF_COUNT_SW_CPU_CLOCK;
}
/* select based on requested event type */
sched_perf_hw_attr.sample_period = budget;
/* Try to register using hardware perf events */
event = perf_event_create_kernel_counter(
&sched_perf_hw_attr,
cpu, NULL,
event_overflow_callback
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 2, 0)
,NULL
#endif
);
if (!event)
return NULL;
if (IS_ERR(event)) {
/* vary the KERN level based on the returned errno */
if (PTR_ERR(event) == -EOPNOTSUPP)
pr_info("cpu%d. not supported\n", cpu);
else if (PTR_ERR(event) == -ENOENT)
pr_info("cpu%d. not h/w event\n", cpu);
else
pr_err("cpu%d. unable to create perf event: %ld\n",
cpu, PTR_ERR(event));
return NULL;
}
/* success path */
pr_info("cpu%d enabled counter.\n", cpu);
smp_wmb();
return event;
}
static void __kill_throttlethread(void *info)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
cinfo->throttled_task = NULL;
}
static void __disable_counter(void *info)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
BUG_ON(!cinfo->event);
/* stop the counter */
cinfo->event->pmu->stop(cinfo->event, PERF_EF_UPDATE);
cinfo->event->pmu->del(cinfo->event, 0);
pr_info("LLC bandwidth throttling disabled\n");
}
static void disable_counters(void)
{
on_each_cpu(__disable_counter, NULL, 0);
}
static void __start_counter(void* info)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
cinfo->event->pmu->add(cinfo->event, PERF_EF_START);
}
static void start_counters(void)
{
on_each_cpu(__start_counter, NULL, 0);
}
/**************************************************************************
* Local Functions
**************************************************************************/
static ssize_t memguard_control_write(struct file *filp,
const char __user *ubuf,
size_t cnt, loff_t *ppos)
{
char buf[256];
char *p = buf;
struct memguard_info *global = &memguard_info;
if (copy_from_user(&buf, ubuf, (cnt > 256) ? 256: cnt) != 0)
return 0;
if (!strncmp(p, "maxbw ", 6)) {
sscanf(p+6, "%d", &g_budget_max_bw);
global->max_budget =
convert_mb_to_events(g_budget_max_bw);
WARN_ON(global->max_budget == 0);
}
else if (!strncmp(p, "taskprio ", 9))
sscanf(p+9, "%d", &g_use_task_priority);
else if (!strncmp(p, "reclaim ", 8))
sscanf(p+8, "%d", &g_use_reclaim);
else if (!strncmp(p, "exclusive ", 10))
sscanf(p+10, "%d", &g_use_exclusive);
else
pr_info("ERROR: %s\n", p);
smp_mb();
return cnt;
}
static int memguard_control_show(struct seq_file *m, void *v)
{
char buf[64];
struct memguard_info *global = &memguard_info;
seq_printf(m, "maxbw: %d (MB/s)\n", g_budget_max_bw);
seq_printf(m, "reclaim: %d\n", g_use_reclaim);
seq_printf(m, "exclusive: %d\n", g_use_exclusive);
seq_printf(m, "taskprio: %d\n", g_use_task_priority);
seq_printf(m, "active: %*pbl\n", cpumask_pr_args(global->active_mask));
seq_printf(m, "throttle: %*pbl\n", cpumask_pr_args(global->throttle_mask));
return 0;
}
static int memguard_control_open(struct inode *inode, struct file *filp)
{
return single_open(filp, memguard_control_show, NULL);
}
static const struct file_operations memguard_control_fops = {
.open = memguard_control_open,
.write = memguard_control_write,
.read = seq_read,
.llseek = seq_lseek,
.release = single_release,
};
static void __update_budget(void *info)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
cinfo->limit = (unsigned long)info;
cinfo->weight = 0;
smp_mb();
DEBUG_USER(trace_printk("MSG: New budget of Core%d is %d\n",
smp_processor_id(), cinfo->budget));
}
static void __update_weight(void *info)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
cinfo->weight = (unsigned long)info;
cinfo->limit = 0;
smp_mb();