forked from heechul/memguard
-
Notifications
You must be signed in to change notification settings - Fork 1
/
memguard.c
1553 lines (1298 loc) · 44.5 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 <[email protected]>
*
* 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_RCFS 0
#define USE_BWLOCK 0
#define DEBUG(x)
#define DEBUG_RECLAIM(x)
#define DEBUG_USER(x)
#define DEBUG_BWLOCK(x)
#define DEBUG_PROFILE(x) x
#define DEBUG_RCFS(x)
/**************************************************************************
* Included Files
**************************************************************************/
#include <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(4, 13, 0)
# include <linux/sched/types.h>
#endif
#if LINUX_VERSION_CODE > KERNEL_VERSION(3, 8, 0)
# include <linux/sched/rt.h>
#endif
#include <linux/sched.h>
/**************************************************************************
* Public Definitions
**************************************************************************/
#define MAX_NCPUS 64
#define CACHE_LINE_SIZE 64
#define BUF_SIZE 256
#define PREDICTOR 1 /* 0 - used, 1 - ewma(a=1/2), 2 - ewma(a=1/4) */
#if LINUX_VERSION_CODE > KERNEL_VERSION(4, 10, 0) // somewhere between 4.4-4.10
# define TM_NS(x) (x)
#else
# define TM_NS(x) (x).tv64
#endif
/**************************************************************************
* Public Types
**************************************************************************/
struct memstat{
u64 used_read_budget; /* used read budget */
u64 used_write_budget; /* used write budget */
u64 assigned_read_budget; /* assigned read budget */
u64 assigned_write_budget; /* assigned write 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 read_budget; /* assigned read budget */
int write_budget; /* assigned write budged */
int read_limit; /* read limit mode (exclusive to weight)*/
int write_limit; /* write limit mode (exclusive to weight) */
/* for control logic */
int cur_read_budget; /* currently available read budget */
int cur_write_budget; /* currently available write budget */
struct task_struct * throttled_task;
ktime_t throttled_time; /* absolute time when throttled */
u64 old_read_val; /* hold previous read counter value */
u64 old_write_val; /* hold previous write counter value */
int prev_read_throttle_error; /* check whether there was throttle error in
the previous period for the read counter */
int prev_write_throttle_error; /* check whether there was throttle error in
the previous period for the write counter */
int exclusive_mode; /* 1 - if in exclusive mode */
ktime_t exclusive_time; /* time when exclusive mode begins */
struct irq_work read_pending; /* delayed work for NMIs */
struct perf_event *read_event; /* PMC: LLC misses */
struct irq_work write_pending; /* delayed work for NMIs */
struct perf_event *write_event; /* PMC: LLC writebacks */
#if USE_RCFS
struct perf_event *cycle_event; /* PMC: cycles */
struct perf_event *instr_event; /* PMC: retired instructions */
#endif
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 read_used[3]; /* EWMA memory load */
int write_used[3]; /* EWMA memory load */
long period_cnt; /* active periods count */
};
/* global info */
struct memguard_info {
int master;
ktime_t period_in_ktime;
ktime_t cur_period_start;
int start_tick;
long period_cnt;
cpumask_var_t throttle_mask;
cpumask_var_t active_mask;
atomic_t wsum;
#if USE_BWLOCK
int bwlocked_cores;
#endif
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_bwlock = 1;
static int g_use_exclusive = 0;
static int g_read_budget_mb[MAX_NCPUS];
static int g_write_budget_mb[MAX_NCPUS];
static struct dentry *memguard_dir;
/* 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,
};
static const u32 prio_to_wmult[40] = {
/* -20 */ 48388, 59856, 76040, 92818, 118348,
/* -15 */ 147320, 184698, 229616, 287308, 360437,
/* -10 */ 449829, 563644, 704093, 875809, 1099582,
/* -5 */ 1376151, 1717300, 2157191, 2708050, 3363326,
/* 0 */ 4194304, 5237765, 6557202, 8165337, 10153587,
/* 5 */ 12820798, 15790321, 19976592, 24970740, 31350126,
/* 10 */ 39045157, 49367440, 61356676, 76695844, 95443717,
/* 15 */ 119304647, 148102320, 186737708, 238609294, 286331153,
};
#if USE_BWLOCK
/* should be defined in the scheduler code */
static int sysctl_maxperf_bw_mb = 100000;
static int sysctl_throttle_bw_mb = 100;
#endif
/**************************************************************************
* External Function Prototypes
**************************************************************************/
#if USE_BWLOCK
extern int nr_bwlocked_cores(void);
#endif
extern void register_get_cpi(int (*func_ptr)(void));
/**************************************************************************
* Local Function Prototypes
**************************************************************************/
static void period_timer_callback_slave(void *info);
enum hrtimer_restart period_timer_callback_master(struct hrtimer *timer);
static void memguard_read_process_overflow(struct irq_work *entry);
static void memguard_write_process_overflow(struct irq_work *entry);
static int throttle_thread(void *arg);
static void memguard_on_each_cpu_mask(const struct cpumask *mask,
smp_call_func_t func,
void *info, bool wait);
/**************************************************************************
* Module parameters
**************************************************************************/
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_bwlock, int, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP);
MODULE_PARM_DESC(g_use_bwlock, "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 main code
**************************************************************************/
/* similar to on_each_cpu_mask(), but this must be called with IRQ disabled */
static void memguard_on_each_cpu_mask(const struct cpumask *mask,
smp_call_func_t func,
void *info, bool wait)
{
int cpu = smp_processor_id();
smp_call_function_many(mask, func, info, wait);
if (cpumask_test_cpu(cpu, mask)) {
func(info);
}
}
/** 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 = g_period_us*1024*1024;
int mb = div64_u64(events*CACHE_LINE_SIZE*1000000 + (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_read_event_used(struct core_info *cinfo)
{
return perf_event_count(cinfo->read_event) - cinfo->old_read_val;
}
/** return used event in the current period */
static inline u64 memguard_write_event_used(struct core_info *cinfo)
{
return perf_event_count(cinfo->write_event) - cinfo->old_write_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->read_budget, cinfo->cur_read_budget, cinfo->period_cnt);
}
#if USE_RCFS
/* return cpi value of the calling core */
static int get_cpi(void)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
u64 old_val, new_val;
u64 delta_cycles, delta_instrs;
int wcpi;
BUG_ON(!cinfo->cycle_event);
BUG_ON(!cinfo->instr_event);
old_val = local64_read(&cinfo->cycle_event->count);
cinfo->cycle_event->pmu->stop(cinfo->cycle_event, PERF_EF_UPDATE);
new_val = local64_read(&cinfo->cycle_event->count);
delta_cycles = new_val - old_val;
old_val = local64_read(&cinfo->instr_event->count);
cinfo->instr_event->pmu->stop(cinfo->instr_event, PERF_EF_UPDATE);
new_val = local64_read(&cinfo->instr_event->count);
delta_instrs = new_val - old_val;
wcpi = (int)div64_u64(delta_cycles * 1024, delta_instrs + 1);
cinfo->cycle_event->pmu->start(cinfo->cycle_event, 0);
cinfo->cycle_event->pmu->start(cinfo->instr_event, 0);
DEBUG_RCFS(trace_printk("d_cycle: %lld d_instr: %lld wcpi: %d\n",
delta_cycles, delta_instrs, wcpi));
return wcpi;
}
#endif
#if USE_BWLOCK
static int mg_nr_bwlocked_cores(void)
{
struct memguard_info *global = &memguard_info;
int i;
int nr = nr_bwlocked_cores(); /* check running tasks */
for_each_cpu(i, global->throttle_mask) {
struct task_struct *t = (struct task_struct *)
per_cpu_ptr(core_info, i)->throttled_task;
if (t && t->bwlock_val > 0)
nr += t->bwlock_val;
}
return nr;
}
#endif
/**
* update per-core usage statistics
*/
void update_statistics(struct core_info *cinfo)
{
/* counter must be stopped by now. */
s64 read_new, write_new;
int read_used, write_used;
read_new = perf_event_count(cinfo->read_event);
read_used = (int)(read_new - cinfo->old_read_val);
cinfo->old_read_val = read_new;
cinfo->overall.used_read_budget += read_used;
cinfo->overall.assigned_read_budget += cinfo->read_budget;
write_new = perf_event_count(cinfo->write_event);
write_used = (int)(write_new - cinfo->old_write_val);
cinfo->old_write_val = write_new;
cinfo->overall.used_write_budget += write_used;
cinfo->overall.assigned_write_budget += cinfo->write_budget;
/* EWMA filtered per-core usage statistics */
cinfo->read_used[0] = read_used;
cinfo->read_used[1] = (cinfo->read_used[1] * (2-1) + read_used) >> 1;
/* used[1]_k = 1/2 used[1]_k-1 + 1/2 used */
cinfo->read_used[2] = (cinfo->read_used[2] * (4-1) + read_used) >> 2;
/* used[2]_k = 3/4 used[2]_k-1 + 1/4 used */
/* EWMA filtered per-core usage statistics */
cinfo->write_used[0] = write_used;
cinfo->write_used[1] = (cinfo->write_used[1] * (2-1) + write_used) >> 1;
/* used[1]_k = 1/2 used[1]_k-1 + 1/2 used */
cinfo->write_used[2] = (cinfo->write_used[2] * (4-1) + write_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 +=
(TM_NS(ktime_get()) - TM_NS(cinfo->throttled_time));
cinfo->overall.throttled++;
}
/* throttling error condition:
I was too aggressive in giving up "unsed" budget */
if (cinfo->prev_read_throttle_error && read_used < cinfo->read_budget) {
int diff = cinfo->read_budget - read_used;
int idx;
cinfo->overall.throttled_error ++; // += diff;
BUG_ON(cinfo->read_budget == 0);
idx = (int)(diff * 10 / cinfo->read_budget);
cinfo->overall.throttled_error_dist[idx]++;
trace_printk("ERR: throttled_error: %d < %d\n", read_used, cinfo->read_budget);
/* compensation for error to catch-up*/
cinfo->read_used[PREDICTOR] = cinfo->read_budget + diff;
}
cinfo->prev_read_throttle_error = 0;
if (cinfo->prev_write_throttle_error && write_used < cinfo->write_budget) {
int diff = cinfo->write_budget - write_used;
int idx;
cinfo->overall.throttled_error ++; // += diff;
BUG_ON(cinfo->write_budget == 0);
idx = (int)(diff * 10 / cinfo->write_budget);
cinfo->overall.throttled_error_dist[idx]++;
trace_printk("ERR: throttled_error: %d < %d\n", read_used, cinfo->read_budget);
/* compensation for error to catch-up*/
cinfo->write_used[PREDICTOR] = cinfo->write_budget + diff;
}
cinfo->prev_write_throttle_error = 0;
/* I was the lucky guy who used the DRAM exclusively */
if (cinfo->exclusive_mode) {
u64 exclusive_ns, exclusive_bw;
/* used time */
exclusive_ns = (TM_NS(ktime_get()) -
TM_NS(cinfo->exclusive_time));
/* used bw */
exclusive_bw = (cinfo->read_used[0] - cinfo->read_budget);
cinfo->overall.exclusive_ns += exclusive_ns;
cinfo->overall.exclusive_bw += exclusive_bw;
cinfo->exclusive_mode = 0;
cinfo->overall.exclusive++;
}
DEBUG_PROFILE(trace_printk("%lld %d %p CPU%d org: %d cur: %d period: %ld\n",
read_new, read_used, cinfo->throttled_task,
smp_processor_id(),
cinfo->read_budget,
cinfo->cur_read_budget,
cinfo->period_cnt));
}
/**
* 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->read_pending);
}
static void event_write_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->write_pending);
}
/**
* 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;
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;
if (period == global->period_cnt) {
ktime_t new_expire = ktime_add(start, global->period_in_ktime);
long new_period = ++global->period_cnt;
/* 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);
}
}
/**
* memory overflow handler.
* must not be executed in NMI context. but in hard irq context
*/
static void memguard_read_process_overflow(struct irq_work *entry)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
struct memguard_info *global = &memguard_info;
ktime_t start;
s64 read_budget_used;//, write_budget_used;
start = ktime_get();
BUG_ON(in_nmi() || !in_irq());
read_budget_used = memguard_read_event_used(cinfo);
/* erroneous overflow, that could have happend before period timer
stop the pmu */
if (read_budget_used < cinfo->cur_read_budget) {
trace_printk("ERR: overflow in timer. used %lld < cur_budget %d. ignore\n",
read_budget_used, cinfo->cur_read_budget);
return;
}
/* no more overflow interrupt */
local64_set(&cinfo->read_event->hw.period_left, 0xfffffff);
//local64_set(&cinfo->write_event->hw.period_left, 0xfffffff);
/* check if we donated too much */
if (read_budget_used < cinfo->read_budget) {
trace_printk("ERR: throttling error\n");
cinfo->prev_read_throttle_error = 1;
}
if (!cpumask_test_cpu(smp_processor_id(), global->active_mask)) {
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);
return;
}
/* we are going to be throttled */
cpumask_set_cpu(smp_processor_id(), global->throttle_mask);
smp_mb(); // w -> r ordering of the local cpu.
if (cpumask_equal(global->throttle_mask, global->active_mask)) {
/* all other cores are alreay throttled */
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_many(global->throttle_mask, __unthrottle_core, NULL, 0);
cinfo->exclusive_mode = 1;
cinfo->exclusive_time = ktime_get();
cinfo->throttled_task = NULL;
DEBUG_RECLAIM(trace_printk("exclusive mode begin\n"));
return;
} 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;
}
}
if (cinfo->prev_read_throttle_error)
return;
/*
* fail to reclaim. now throttle this core
*/
DEBUG_RECLAIM(trace_printk("fail to reclaim after %lld nsec.\n",
TM_NS(ktime_get()) - TM_NS(start)));
/* wake-up throttle task */
cinfo->throttled_task = current;
cinfo->throttled_time = start;
WARN_ON_ONCE(!strncmp(current->comm, "swapper", 7));
wake_up_interruptible(&cinfo->throttle_evt);
}
static void memguard_write_process_overflow(struct irq_work *entry)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
struct memguard_info *global = &memguard_info;
ktime_t start;
s64 write_budget_used;
start = ktime_get();
BUG_ON(in_nmi() || !in_irq());
write_budget_used = memguard_write_event_used(cinfo);
if (write_budget_used < cinfo->cur_write_budget)
{
trace_printk("ERR: overflow in write timer. used %lld < cur_budget %d. ignore\n",
write_budget_used, cinfo->cur_write_budget);
return;
}
//local64_set(&cinfo->read_event->hw.period_left, 0xfffffff);
local64_set(&cinfo->write_event->hw.period_left, 0xfffffff);
if (write_budget_used < cinfo->write_budget) {
trace_printk("ERR: throttling error\n");
cinfo->prev_write_throttle_error = 1;
}
if (!cpumask_test_cpu(smp_processor_id(), global->active_mask)) {
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);
return;
}
cpumask_set_cpu(smp_processor_id(), global->throttle_mask);
smp_mb(); // w -> r ordering of the local cpu.
if (cpumask_equal(global->throttle_mask, global->active_mask)) {
if (g_use_exclusive == 1) {
cinfo->exclusive_mode = 1;
cinfo->exclusive_time = ktime_get();
DEBUG_RECLAIM(trace_printk("exclusive mode begin\n"));
return;
} else if (g_use_exclusive == 2) {
smp_call_function_many(global->throttle_mask, __unthrottle_core, NULL, 0);
cinfo->exclusive_mode = 1;
cinfo->exclusive_time = ktime_get();
cinfo->throttled_task = NULL;
DEBUG_RECLAIM(trace_printk("exclusive mode begin\n"));
return;
} 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;
}
}
if (cinfo->prev_write_throttle_error)
return;
trace_printk("WHY\n");
DEBUG_RECLAIM(trace_printk("WHY fail to reclaim after %lld nsec.\n",
TM_NS(ktime_get()) - TM_NS(start)));
cinfo->throttled_task = current;
cinfo->throttled_time = start;
WARN_ON_ONCE(!strncmp(current->comm, "swapper", 7));
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->read_event->pmu->stop(cinfo->read_event, PERF_EF_UPDATE);
cinfo->write_event->pmu->stop(cinfo->write_event, PERF_EF_UPDATE);
/* I'm actively participating */
cpumask_clear_cpu(cpu, global->throttle_mask);
smp_mb();
cpumask_set_cpu(cpu, global->active_mask);
/* unthrottle tasks (if any) */
if (cinfo->throttled_task)
target = (struct task_struct *)cinfo->throttled_task;
else
target = current;
cinfo->throttled_task = NULL;
#if USE_BWLOCK
/* bwlock check */
if (g_use_bwlock) {
if (global->bwlocked_cores > 0) {
if (target->bwlock_val > 0)
cinfo->read_limit = convert_mb_to_events(sysctl_maxperf_bw_mb);
else
cinfo->read_limit = convert_mb_to_events(sysctl_throttle_bw_mb);
} else {
cinfo->read_limit = convert_mb_to_events(sysctl_maxperf_bw_mb);
}
}
DEBUG_BWLOCK(trace_printk("%s|bwlock_val %d|g->bwlocked_cores %d\n",
(current)?current->comm:"null",
current->bwlock_val, global->bwlocked_cores));
#endif
DEBUG(trace_printk("%p|New period %ld. global->budget=%d\n",
cinfo->throttled_task,
cinfo->period_cnt, cinfo->read_budget));
/* update statistics. */
update_statistics(cinfo);
/* new budget assignment from user */
if (cinfo->read_limit > 0) {
/* limit mode */
cinfo->read_budget = cinfo->read_limit;
}
/* budget can't be zero? */
cinfo->read_budget = max(cinfo->read_budget, 1);
/* new budget assignment from user */
if (cinfo->write_limit > 0) {
/* limit mode */
cinfo->write_budget = cinfo->write_limit;
}
/* budget can't be zero? */
cinfo->write_budget = max(cinfo->write_budget, 1);
if (cinfo->read_event->hw.sample_period != cinfo->read_budget) {
/* new budget is assigned */
trace_printk("MSG: new budget %d is assigned\n",
cinfo->read_budget);
cinfo->read_event->hw.sample_period = cinfo->read_budget;
}
if (cinfo->write_event->hw.sample_period != cinfo->write_budget) {
/* new budget is assigned */
trace_printk("MSG: new write budget %d is assigned\n",
cinfo->write_budget);
cinfo->write_event->hw.sample_period = cinfo->write_budget;
}
/* per-task donation policy */
cinfo->cur_read_budget = cinfo->read_budget;
cinfo->cur_write_budget = cinfo->write_budget;
/* setup an interrupt */
cinfo->cur_read_budget = max(1, cinfo->cur_read_budget);
local64_set(&cinfo->read_event->hw.period_left, cinfo->cur_read_budget);
cinfo->cur_write_budget = max(1, cinfo->cur_write_budget);
local64_set(&cinfo->write_event->hw.period_left, cinfo->cur_write_budget);
/* enable performance counter */
cinfo->read_event->pmu->start(cinfo->read_event, PERF_EF_RELOAD);
cinfo->write_event->pmu->start(cinfo->write_event, PERF_EF_RELOAD);
}
/**
* 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;
now = timer->base->get_time();
global->cur_period_start = now;
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;
global->period_cnt += orun;
new_period = global->period_cnt;
if (orun > 1)
trace_printk("ERR: timer overrun %d at period %ld\n",
orun, new_period);
#if USE_BWLOCK
global->bwlocked_cores = mg_nr_bwlocked_cores();
#endif
memguard_on_each_cpu_mask(global->active_mask,
period_timer_callback_slave, (void *)new_period, 0);
DEBUG(trace_printk("master end\n"));
return HRTIMER_RESTART;
}
#if USE_RCFS
static struct perf_event *init_counting_counter(int cpu, int id)
{
struct perf_event *event = NULL;
struct perf_event_attr sched_perf_hw_attr = {
/* use generalized hardware abstraction */
.type = PERF_TYPE_HARDWARE,
.config = id,
.size = sizeof(struct perf_event_attr),
.pinned = 1,
.disabled = 1,
.exclude_kernel = 1, /* TODO: 1 mean, no kernel mode counting */
};
/* Try to register using hardware perf events */
event = perf_event_create_kernel_counter(
&sched_perf_hw_attr,
cpu, NULL,
NULL
#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;
}
/* This is needed since 4.1? */
perf_event_enable(event);
/* success path */
pr_info("cpu%d enabled counter type %d.\n", cpu, (int)id);
return event;
}
#endif /* RCFS */
static struct perf_event *init_counter(int cpu, int budget, int write)
{
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,
.type = PERF_TYPE_RAW,
.config = 0x17,
.size = sizeof(struct perf_event_attr),
.pinned = 1,
.disabled = 1,
.exclude_kernel = 1, /* TODO: 1 mean, no kernel mode counting */
};
if(write == 1)
{
sched_perf_hw_attr.config = 0x18;
}
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, "armv7")) {
sched_perf_hw_attr.type = PERF_TYPE_RAW;
sched_perf_hw_attr.config = 0x17; /* Level 2 data cache refill */
} 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;
if(write == 0)
{
/* 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
);
}
else if(write == 1)
{
/* Try to register using hardware perf events */
event = perf_event_create_kernel_counter(
&sched_perf_hw_attr,
cpu, NULL,
event_write_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;
}
/* This is needed since 4.1? */
perf_event_enable(event);
/* success path */
pr_info("cpu%d enabled counter.\n", cpu);
return event;
}
static void __disable_counter(void *info)
{
struct core_info *cinfo = this_cpu_ptr(core_info);
BUG_ON(!cinfo->read_event);
BUG_ON(!cinfo->write_event);
/* stop the counter */
cinfo->read_event->pmu->stop(cinfo->read_event, PERF_EF_UPDATE);
cinfo->read_event->pmu->del(cinfo->read_event, 0);
cinfo->write_event->pmu->stop(cinfo->write_event, PERF_EF_UPDATE);
cinfo->write_event->pmu->del(cinfo->write_event, 0);
#if USE_RCFS
cinfo->cycle_event->pmu->stop(cinfo->cycle_event, PERF_EF_UPDATE);
cinfo->cycle_event->pmu->del(cinfo->cycle_event, 0);
cinfo->instr_event->pmu->stop(cinfo->instr_event, PERF_EF_UPDATE);
cinfo->instr_event->pmu->del(cinfo->instr_event, 0);
#endif
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)