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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
|
Makefile | 4 +-
arch/arm/Kconfig | 8 +
arch/arm/include/asm/timex.h | 2 +
arch/arm/include/asm/unistd.h | 3 +
arch/arm/kernel/calls.S | 14 +
arch/arm/kernel/smp.c | 4 +
arch/arm/mach-realview/include/mach/timex.h | 27 +
arch/x86/Kconfig | 8 +
arch/x86/include/asm/entry_arch.h | 1 +
arch/x86/include/asm/feather_trace.h | 17 +
arch/x86/include/asm/feather_trace_32.h | 79 +++
arch/x86/include/asm/feather_trace_64.h | 67 ++
arch/x86/include/asm/hw_irq.h | 3 +
arch/x86/include/asm/irq_vectors.h | 5 +
arch/x86/include/asm/processor.h | 4 +
arch/x86/include/asm/unistd_32.h | 6 +-
arch/x86/include/asm/unistd_64.h | 4 +
arch/x86/kernel/Makefile | 2 +
arch/x86/kernel/cpu/intel_cacheinfo.c | 17 +
arch/x86/kernel/entry_64.S | 2 +
arch/x86/kernel/ft_event.c | 118 ++++
arch/x86/kernel/irqinit.c | 3 +
arch/x86/kernel/smp.c | 27 +
arch/x86/kernel/syscall_table_32.S | 14 +
fs/exec.c | 13 +-
fs/inode.c | 2 +
include/linux/completion.h | 1 +
include/linux/fs.h | 21 +-
include/linux/hrtimer.h | 32 +
include/linux/sched.h | 19 +-
include/linux/smp.h | 5 +
include/linux/tick.h | 5 +
include/litmus/bheap.h | 77 +++
include/litmus/budget.h | 8 +
include/litmus/debug_trace.h | 37 ++
include/litmus/edf_common.h | 27 +
include/litmus/fdso.h | 70 +++
include/litmus/feather_buffer.h | 94 +++
include/litmus/feather_trace.h | 65 ++
include/litmus/ftdev.h | 52 ++
include/litmus/jobs.h | 9 +
include/litmus/litmus.h | 241 +++++++
include/litmus/litmus_proc.h | 19 +
include/litmus/preempt.h | 164 +++++
include/litmus/rt_domain.h | 182 ++++++
include/litmus/rt_param.h | 196 ++++++
include/litmus/sched_plugin.h | 159 +++++
include/litmus/sched_trace.h | 183 ++++++
include/litmus/trace.h | 113 ++++
include/litmus/unistd_32.h | 23 +
include/litmus/unistd_64.h | 37 ++
kernel/exit.c | 4 +
kernel/fork.c | 7 +
kernel/hrtimer.c | 95 +++
kernel/printk.c | 14 +-
kernel/sched.c | 118 ++++-
kernel/sched_fair.c | 2 +-
kernel/sched_rt.c | 2 +-
kernel/time/tick-sched.c | 47 ++
litmus/Kconfig | 194 ++++++
litmus/Makefile | 27 +
litmus/bheap.c | 314 ++++++++++
litmus/budget.c | 111 ++++
litmus/ctrldev.c | 150 +++++
litmus/edf_common.c | 102 +++
litmus/fdso.c | 281 +++++++++
litmus/fmlp.c | 268 ++++++++
litmus/ft_event.c | 43 ++
litmus/ftdev.c | 440 +++++++++++++
litmus/jobs.c | 43 ++
litmus/litmus.c | 547 ++++++++++++++++
litmus/litmus_proc.c | 259 ++++++++
litmus/preempt.c | 131 ++++
litmus/rt_domain.c | 355 +++++++++++
litmus/sched_cedf.c | 873 ++++++++++++++++++++++++++
litmus/sched_gsn_edf.c | 828 +++++++++++++++++++++++++
litmus/sched_litmus.c | 320 ++++++++++
litmus/sched_pfair.c | 894 +++++++++++++++++++++++++++
litmus/sched_plugin.c | 253 ++++++++
litmus/sched_psn_edf.c | 483 +++++++++++++++
litmus/sched_task_trace.c | 226 +++++++
litmus/sched_trace.c | 252 ++++++++
litmus/srp.c | 318 ++++++++++
litmus/sync.c | 104 +++
litmus/trace.c | 122 ++++
85 files changed, 10485 insertions(+), 35 deletions(-)
diff --git a/Makefile b/Makefile
index 860c26a..8e53f47 100644
--- a/Makefile
+++ b/Makefile
@@ -1,7 +1,7 @@
VERSION = 2
PATCHLEVEL = 6
SUBLEVEL = 36
-EXTRAVERSION =
+EXTRAVERSION =-litmus2010
NAME = Flesh-Eating Bats with Fangs
# *DOCUMENTATION*
@@ -659,7 +659,7 @@ export mod_strip_cmd
ifeq ($(KBUILD_EXTMOD),)
-core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/
+core-y += kernel/ mm/ fs/ ipc/ security/ crypto/ block/ litmus/
vmlinux-dirs := $(patsubst %/,%,$(filter %/, $(init-y) $(init-m) \
$(core-y) $(core-m) $(drivers-y) $(drivers-m) \
diff --git a/arch/arm/Kconfig b/arch/arm/Kconfig
index 9c26ba7..babad6d 100644
--- a/arch/arm/Kconfig
+++ b/arch/arm/Kconfig
@@ -1808,3 +1808,11 @@ source "security/Kconfig"
source "crypto/Kconfig"
source "lib/Kconfig"
+
+config ARCH_HAS_SEND_PULL_TIMERS
+ def_bool n
+
+config ARCH_HAS_FEATHER_TRACE
+ def_bool n
+
+source "litmus/Kconfig"
diff --git a/arch/arm/include/asm/timex.h b/arch/arm/include/asm/timex.h
index 3be8de3..8a102a3 100644
--- a/arch/arm/include/asm/timex.h
+++ b/arch/arm/include/asm/timex.h
@@ -16,9 +16,11 @@
typedef unsigned long cycles_t;
+#ifndef get_cycles
static inline cycles_t get_cycles (void)
{
return 0;
}
+#endif
#endif
diff --git a/arch/arm/include/asm/unistd.h b/arch/arm/include/asm/unistd.h
index c891eb7..625b304 100644
--- a/arch/arm/include/asm/unistd.h
+++ b/arch/arm/include/asm/unistd.h
@@ -397,6 +397,9 @@
#define __NR_fanotify_mark (__NR_SYSCALL_BASE+368)
#define __NR_prlimit64 (__NR_SYSCALL_BASE+369)
+#define __NR_LITMUS (__NR_SYSCALL_BASE+370)
+#include <litmus/unistd_32.h>
+
/*
* The following SWIs are ARM private.
*/
diff --git a/arch/arm/kernel/calls.S b/arch/arm/kernel/calls.S
index 5c26ecc..584a683 100644
--- a/arch/arm/kernel/calls.S
+++ b/arch/arm/kernel/calls.S
@@ -379,6 +379,20 @@
CALL(sys_fanotify_init)
CALL(sys_fanotify_mark)
CALL(sys_prlimit64)
+/* 370 */ CALL(sys_set_rt_task_param)
+ CALL(sys_get_rt_task_param)
+ CALL(sys_complete_job)
+ CALL(sys_od_open)
+ CALL(sys_od_close)
+/* 375 */ CALL(sys_fmlp_down)
+ CALL(sys_fmlp_up)
+ CALL(sys_srp_down)
+ CALL(sys_srp_up)
+ CALL(sys_query_job_no)
+/* 380 */ CALL(sys_wait_for_job_release)
+ CALL(sys_wait_for_ts_release)
+ CALL(sys_release_ts)
+ CALL(sys_null_call)
#ifndef syscalls_counted
.equ syscalls_padding, ((NR_syscalls + 3) & ~3) - NR_syscalls
#define syscalls_counted
diff --git a/arch/arm/kernel/smp.c b/arch/arm/kernel/smp.c
index 40dc74f..b72fbf3 100644
--- a/arch/arm/kernel/smp.c
+++ b/arch/arm/kernel/smp.c
@@ -38,6 +38,8 @@
#include <asm/localtimer.h>
#include <asm/smp_plat.h>
+#include <litmus/preempt.h>
+
/*
* as from 2.5, kernels no longer have an init_tasks structure
* so we need some other way of telling a new secondary core
@@ -533,6 +535,8 @@ asmlinkage void __exception do_IPI(struct pt_regs *regs)
* nothing more to do - eveything is
* done on the interrupt return path
*/
+ /* LITMUS^RT: take action based on scheduler state */
+ sched_state_ipi();
break;
case IPI_CALL_FUNC:
diff --git a/arch/arm/mach-realview/include/mach/timex.h b/arch/arm/mach-realview/include/mach/timex.h
index 4eeb069..e8bcc40 100644
--- a/arch/arm/mach-realview/include/mach/timex.h
+++ b/arch/arm/mach-realview/include/mach/timex.h
@@ -21,3 +21,30 @@
*/
#define CLOCK_TICK_RATE (50000000 / 16)
+
+#if defined(CONFIG_MACH_REALVIEW_PB11MP) || defined(CONFIG_MACH_REALVIEW_PB1176)
+
+static inline unsigned long realview_get_arm11_cp15_ccnt(void)
+{
+ unsigned long cycles;
+ /* Read CP15 CCNT register. */
+ asm volatile ("mrc p15, 0, %0, c15, c12, 1" : "=r" (cycles));
+ return cycles;
+}
+
+#define get_cycles realview_get_arm11_cp15_ccnt
+
+#elif defined(CONFIG_MACH_REALVIEW_PBA8)
+
+
+static inline unsigned long realview_get_a8_cp15_ccnt(void)
+{
+ unsigned long cycles;
+ /* Read CP15 CCNT register. */
+ asm volatile ("mrc p15, 0, %0, c9, c13, 0" : "=r" (cycles));
+ return cycles;
+}
+
+#define get_cycles realview_get_a8_cp15_ccnt
+
+#endif
diff --git a/arch/x86/Kconfig b/arch/x86/Kconfig
index cea0cd9..5181ed3 100644
--- a/arch/x86/Kconfig
+++ b/arch/x86/Kconfig
@@ -2142,3 +2142,11 @@ source "crypto/Kconfig"
source "arch/x86/kvm/Kconfig"
source "lib/Kconfig"
+
+config ARCH_HAS_FEATHER_TRACE
+ def_bool y
+
+config ARCH_HAS_SEND_PULL_TIMERS
+ def_bool y
+
+source "litmus/Kconfig"
diff --git a/arch/x86/include/asm/entry_arch.h b/arch/x86/include/asm/entry_arch.h
index 8e8ec66..5d07dea 100644
--- a/arch/x86/include/asm/entry_arch.h
+++ b/arch/x86/include/asm/entry_arch.h
@@ -13,6 +13,7 @@
BUILD_INTERRUPT(reschedule_interrupt,RESCHEDULE_VECTOR)
BUILD_INTERRUPT(call_function_interrupt,CALL_FUNCTION_VECTOR)
BUILD_INTERRUPT(call_function_single_interrupt,CALL_FUNCTION_SINGLE_VECTOR)
+BUILD_INTERRUPT(pull_timers_interrupt,PULL_TIMERS_VECTOR)
BUILD_INTERRUPT(irq_move_cleanup_interrupt,IRQ_MOVE_CLEANUP_VECTOR)
BUILD_INTERRUPT(reboot_interrupt,REBOOT_VECTOR)
diff --git a/arch/x86/include/asm/feather_trace.h b/arch/x86/include/asm/feather_trace.h
new file mode 100644
index 0000000..4fd3163
--- /dev/null
+++ b/arch/x86/include/asm/feather_trace.h
@@ -0,0 +1,17 @@
+#ifndef _ARCH_FEATHER_TRACE_H
+#define _ARCH_FEATHER_TRACE_H
+
+#include <asm/msr.h>
+
+static inline unsigned long long ft_timestamp(void)
+{
+ return __native_read_tsc();
+}
+
+#ifdef CONFIG_X86_32
+#include "feather_trace_32.h"
+#else
+#include "feather_trace_64.h"
+#endif
+
+#endif
diff --git a/arch/x86/include/asm/feather_trace_32.h b/arch/x86/include/asm/feather_trace_32.h
new file mode 100644
index 0000000..70202f9
--- /dev/null
+++ b/arch/x86/include/asm/feather_trace_32.h
@@ -0,0 +1,79 @@
+/* Do not directly include this file. Include feather_trace.h instead */
+
+#define feather_callback __attribute__((regparm(0)))
+
+/*
+ * make the compiler reload any register that is not saved in
+ * a cdecl function call
+ */
+#define CLOBBER_LIST "memory", "cc", "eax", "ecx", "edx"
+
+#define ft_event(id, callback) \
+ __asm__ __volatile__( \
+ "1: jmp 2f \n\t" \
+ " call " #callback " \n\t" \
+ ".section __event_table, \"aw\" \n\t" \
+ ".long " #id ", 0, 1b, 2f \n\t" \
+ ".previous \n\t" \
+ "2: \n\t" \
+ : : : CLOBBER_LIST)
+
+#define ft_event0(id, callback) \
+ __asm__ __volatile__( \
+ "1: jmp 2f \n\t" \
+ " subl $4, %%esp \n\t" \
+ " movl $" #id ", (%%esp) \n\t" \
+ " call " #callback " \n\t" \
+ " addl $4, %%esp \n\t" \
+ ".section __event_table, \"aw\" \n\t" \
+ ".long " #id ", 0, 1b, 2f \n\t" \
+ ".previous \n\t" \
+ "2: \n\t" \
+ : : : CLOBBER_LIST)
+
+#define ft_event1(id, callback, param) \
+ __asm__ __volatile__( \
+ "1: jmp 2f \n\t" \
+ " subl $8, %%esp \n\t" \
+ " movl %0, 4(%%esp) \n\t" \
+ " movl $" #id ", (%%esp) \n\t" \
+ " call " #callback " \n\t" \
+ " addl $8, %%esp \n\t" \
+ ".section __event_table, \"aw\" \n\t" \
+ ".long " #id ", 0, 1b, 2f \n\t" \
+ ".previous \n\t" \
+ "2: \n\t" \
+ : : "r" (param) : CLOBBER_LIST)
+
+#define ft_event2(id, callback, param, param2) \
+ __asm__ __volatile__( \
+ "1: jmp 2f \n\t" \
+ " subl $12, %%esp \n\t" \
+ " movl %1, 8(%%esp) \n\t" \
+ " movl %0, 4(%%esp) \n\t" \
+ " movl $" #id ", (%%esp) \n\t" \
+ " call " #callback " \n\t" \
+ " addl $12, %%esp \n\t" \
+ ".section __event_table, \"aw\" \n\t" \
+ ".long " #id ", 0, 1b, 2f \n\t" \
+ ".previous \n\t" \
+ "2: \n\t" \
+ : : "r" (param), "r" (param2) : CLOBBER_LIST)
+
+
+#define ft_event3(id, callback, p, p2, p3) \
+ __asm__ __volatile__( \
+ "1: jmp 2f \n\t" \
+ " subl $16, %%esp \n\t" \
+ " movl %2, 12(%%esp) \n\t" \
+ " movl %1, 8(%%esp) \n\t" \
+ " movl %0, 4(%%esp) \n\t" \
+ " movl $" #id ", (%%esp) \n\t" \
+ " call " #callback " \n\t" \
+ " addl $16, %%esp \n\t" \
+ ".section __event_table, \"aw\" \n\t" \
+ ".long " #id ", 0, 1b, 2f \n\t" \
+ ".previous \n\t" \
+ "2: \n\t" \
+ : : "r" (p), "r" (p2), "r" (p3) : CLOBBER_LIST)
+
diff --git a/arch/x86/include/asm/feather_trace_64.h b/arch/x86/include/asm/feather_trace_64.h
new file mode 100644
index 0000000..54ac2ae
--- /dev/null
+++ b/arch/x86/include/asm/feather_trace_64.h
@@ -0,0 +1,67 @@
+/* Do not directly include this file. Include feather_trace.h instead */
+
+/* regparm is the default on x86_64 */
+#define feather_callback
+
+# define _EVENT_TABLE(id,from,to) \
+ ".section __event_table, \"aw\"\n\t" \
+ ".balign 8\n\t" \
+ ".quad " #id ", 0, " #from ", " #to " \n\t" \
+ ".previous \n\t"
+
+/*
+ * x86_64 callee only owns rbp, rbx, r12 -> r15
+ * the called can freely modify the others
+ */
+#define CLOBBER_LIST "memory", "cc", "rdi", "rsi", "rdx", "rcx", \
+ "r8", "r9", "r10", "r11", "rax"
+
+#define ft_event(id, callback) \
+ __asm__ __volatile__( \
+ "1: jmp 2f \n\t" \
+ " call " #callback " \n\t" \
+ _EVENT_TABLE(id,1b,2f) \
+ "2: \n\t" \
+ : : : CLOBBER_LIST)
+
+#define ft_event0(id, callback) \
+ __asm__ __volatile__( \
+ "1: jmp 2f \n\t" \
+ " movq $" #id ", %%rdi \n\t" \
+ " call " #callback " \n\t" \
+ _EVENT_TABLE(id,1b,2f) \
+ "2: \n\t" \
+ : : : CLOBBER_LIST)
+
+#define ft_event1(id, callback, param) \
+ __asm__ __volatile__( \
+ "1: jmp 2f \n\t" \
+ " movq %0, %%rsi \n\t" \
+ " movq $" #id ", %%rdi \n\t" \
+ " call " #callback " \n\t" \
+ _EVENT_TABLE(id,1b,2f) \
+ "2: \n\t" \
+ : : "r" (param) : CLOBBER_LIST)
+
+#define ft_event2(id, callback, param, param2) \
+ __asm__ __volatile__( \
+ "1: jmp 2f \n\t" \
+ " movq %1, %%rdx \n\t" \
+ " movq %0, %%rsi \n\t" \
+ " movq $" #id ", %%rdi \n\t" \
+ " call " #callback " \n\t" \
+ _EVENT_TABLE(id,1b,2f) \
+ "2: \n\t" \
+ : : "r" (param), "r" (param2) : CLOBBER_LIST)
+
+#define ft_event3(id, callback, p, p2, p3) \
+ __asm__ __volatile__( \
+ "1: jmp 2f \n\t" \
+ " movq %2, %%rcx \n\t" \
+ " movq %1, %%rdx \n\t" \
+ " movq %0, %%rsi \n\t" \
+ " movq $" #id ", %%rdi \n\t" \
+ " call " #callback " \n\t" \
+ _EVENT_TABLE(id,1b,2f) \
+ "2: \n\t" \
+ : : "r" (p), "r" (p2), "r" (p3) : CLOBBER_LIST)
diff --git a/arch/x86/include/asm/hw_irq.h b/arch/x86/include/asm/hw_irq.h
index 46c0fe0..c174115 100644
--- a/arch/x86/include/asm/hw_irq.h
+++ b/arch/x86/include/asm/hw_irq.h
@@ -53,6 +53,8 @@ extern void threshold_interrupt(void);
extern void call_function_interrupt(void);
extern void call_function_single_interrupt(void);
+extern void pull_timers_interrupt(void);
+
/* IOAPIC */
#define IO_APIC_IRQ(x) (((x) >= NR_IRQS_LEGACY) || ((1<<(x)) & io_apic_irqs))
extern unsigned long io_apic_irqs;
@@ -122,6 +124,7 @@ extern asmlinkage void smp_irq_move_cleanup_interrupt(void);
extern void smp_reschedule_interrupt(struct pt_regs *);
extern void smp_call_function_interrupt(struct pt_regs *);
extern void smp_call_function_single_interrupt(struct pt_regs *);
+extern void smp_pull_timers_interrupt(struct pt_regs *);
#ifdef CONFIG_X86_32
extern void smp_invalidate_interrupt(struct pt_regs *);
#else
diff --git a/arch/x86/include/asm/irq_vectors.h b/arch/x86/include/asm/irq_vectors.h
index e2ca300..6143ebe 100644
--- a/arch/x86/include/asm/irq_vectors.h
+++ b/arch/x86/include/asm/irq_vectors.h
@@ -109,6 +109,11 @@
#define LOCAL_TIMER_VECTOR 0xef
/*
+ * LITMUS^RT pull timers IRQ vector
+ */
+#define PULL_TIMERS_VECTOR 0xee
+
+/*
* Generic system vector for platform specific use
*/
#define X86_PLATFORM_IPI_VECTOR 0xed
diff --git a/arch/x86/include/asm/processor.h b/arch/x86/include/asm/processor.h
index 325b7bd..ebaa04a 100644
--- a/arch/x86/include/asm/processor.h
+++ b/arch/x86/include/asm/processor.h
@@ -169,6 +169,10 @@ extern void print_cpu_info(struct cpuinfo_x86 *);
extern void init_scattered_cpuid_features(struct cpuinfo_x86 *c);
extern unsigned int init_intel_cacheinfo(struct cpuinfo_x86 *c);
extern unsigned short num_cache_leaves;
+#ifdef CONFIG_SYSFS
+extern int get_shared_cpu_map(cpumask_var_t mask,
+ unsigned int cpu, int index);
+#endif
extern void detect_extended_topology(struct cpuinfo_x86 *c);
extern void detect_ht(struct cpuinfo_x86 *c);
diff --git a/arch/x86/include/asm/unistd_32.h b/arch/x86/include/asm/unistd_32.h
index b766a5e..b7ba19a 100644
--- a/arch/x86/include/asm/unistd_32.h
+++ b/arch/x86/include/asm/unistd_32.h
@@ -347,9 +347,13 @@
#define __NR_fanotify_mark 339
#define __NR_prlimit64 340
+#define __NR_LITMUS 341
+
+#include "litmus/unistd_32.h"
+
#ifdef __KERNEL__
-#define NR_syscalls 341
+#define NR_syscalls 341 + NR_litmus_syscalls
#define __ARCH_WANT_IPC_PARSE_VERSION
#define __ARCH_WANT_OLD_READDIR
diff --git a/arch/x86/include/asm/unistd_64.h b/arch/x86/include/asm/unistd_64.h
index 363e9b8..332bf3c 100644
--- a/arch/x86/include/asm/unistd_64.h
+++ b/arch/x86/include/asm/unistd_64.h
@@ -670,6 +670,10 @@ __SYSCALL(__NR_fanotify_mark, sys_fanotify_mark)
#define __NR_prlimit64 302
__SYSCALL(__NR_prlimit64, sys_prlimit64)
+#define __NR_LITMUS 303
+
+#include "litmus/unistd_64.h"
+
#ifndef __NO_STUBS
#define __ARCH_WANT_OLD_READDIR
#define __ARCH_WANT_OLD_STAT
diff --git a/arch/x86/kernel/Makefile b/arch/x86/kernel/Makefile
index fedf32a..6890dbb 100644
--- a/arch/x86/kernel/Makefile
+++ b/arch/x86/kernel/Makefile
@@ -118,6 +118,8 @@ obj-$(CONFIG_X86_CHECK_BIOS_CORRUPTION) += check.o
obj-$(CONFIG_SWIOTLB) += pci-swiotlb.o
+obj-$(CONFIG_FEATHER_TRACE) += ft_event.o
+
###
# 64 bit specific files
ifeq ($(CONFIG_X86_64),y)
diff --git a/arch/x86/kernel/cpu/intel_cacheinfo.c b/arch/x86/kernel/cpu/intel_cacheinfo.c
index 898c2f4..3fec7d9 100644
--- a/arch/x86/kernel/cpu/intel_cacheinfo.c
+++ b/arch/x86/kernel/cpu/intel_cacheinfo.c
@@ -758,6 +758,23 @@ unsigned int __cpuinit init_intel_cacheinfo(struct cpuinfo_x86 *c)
static DEFINE_PER_CPU(struct _cpuid4_info *, ici_cpuid4_info);
#define CPUID4_INFO_IDX(x, y) (&((per_cpu(ici_cpuid4_info, x))[y]))
+/* returns CPUs that share the index cache with cpu */
+int get_shared_cpu_map(cpumask_var_t mask, unsigned int cpu, int index)
+{
+ int ret = 0;
+ struct _cpuid4_info *this_leaf;
+
+ if (index >= num_cache_leaves) {
+ index = num_cache_leaves - 1;
+ ret = index;
+ }
+
+ this_leaf = CPUID4_INFO_IDX(cpu,index);
+ cpumask_copy(mask, to_cpumask(this_leaf->shared_cpu_map));
+
+ return ret;
+}
+
#ifdef CONFIG_SMP
static void __cpuinit cache_shared_cpu_map_setup(unsigned int cpu, int index)
{
diff --git a/arch/x86/kernel/entry_64.S b/arch/x86/kernel/entry_64.S
index 17be5ec..115e895 100644
--- a/arch/x86/kernel/entry_64.S
+++ b/arch/x86/kernel/entry_64.S
@@ -1016,6 +1016,8 @@ apicinterrupt CALL_FUNCTION_VECTOR \
call_function_interrupt smp_call_function_interrupt
apicinterrupt RESCHEDULE_VECTOR \
reschedule_interrupt smp_reschedule_interrupt
+apicinterrupt PULL_TIMERS_VECTOR \
+ pull_timers_interrupt smp_pull_timers_interrupt
#endif
apicinterrupt ERROR_APIC_VECTOR \
diff --git a/arch/x86/kernel/ft_event.c b/arch/x86/kernel/ft_event.c
new file mode 100644
index 0000000..37cc332
--- /dev/null
+++ b/arch/x86/kernel/ft_event.c
@@ -0,0 +1,118 @@
+#include <linux/types.h>
+
+#include <litmus/feather_trace.h>
+
+/* the feather trace management functions assume
+ * exclusive access to the event table
+ */
+
+#ifndef CONFIG_DEBUG_RODATA
+
+#define BYTE_JUMP 0xeb
+#define BYTE_JUMP_LEN 0x02
+
+/* for each event, there is an entry in the event table */
+struct trace_event {
+ long id;
+ long count;
+ long start_addr;
+ long end_addr;
+};
+
+extern struct trace_event __start___event_table[];
+extern struct trace_event __stop___event_table[];
+
+/* Workaround: if no events are defined, then the event_table section does not
+ * exist and the above references cause linker errors. This could probably be
+ * fixed by adjusting the linker script, but it is easier to maintain for us if
+ * we simply create a dummy symbol in the event table section.
+ */
+int __event_table_dummy[0] __attribute__ ((section("__event_table")));
+
+int ft_enable_event(unsigned long id)
+{
+ struct trace_event* te = __start___event_table;
+ int count = 0;
+ char* delta;
+ unsigned char* instr;
+
+ while (te < __stop___event_table) {
+ if (te->id == id && ++te->count == 1) {
+ instr = (unsigned char*) te->start_addr;
+ /* make sure we don't clobber something wrong */
+ if (*instr == BYTE_JUMP) {
+ delta = (((unsigned char*) te->start_addr) + 1);
+ *delta = 0;
+ }
+ }
+ if (te->id == id)
+ count++;
+ te++;
+ }
+
+ printk(KERN_DEBUG "ft_enable_event: enabled %d events\n", count);
+ return count;
+}
+
+int ft_disable_event(unsigned long id)
+{
+ struct trace_event* te = __start___event_table;
+ int count = 0;
+ char* delta;
+ unsigned char* instr;
+
+ while (te < __stop___event_table) {
+ if (te->id == id && --te->count == 0) {
+ instr = (unsigned char*) te->start_addr;
+ if (*instr == BYTE_JUMP) {
+ delta = (((unsigned char*) te->start_addr) + 1);
+ *delta = te->end_addr - te->start_addr -
+ BYTE_JUMP_LEN;
+ }
+ }
+ if (te->id == id)
+ count++;
+ te++;
+ }
+
+ printk(KERN_DEBUG "ft_disable_event: disabled %d events\n", count);
+ return count;
+}
+
+int ft_disable_all_events(void)
+{
+ struct trace_event* te = __start___event_table;
+ int count = 0;
+ char* delta;
+ unsigned char* instr;
+
+ while (te < __stop___event_table) {
+ if (te->count) {
+ instr = (unsigned char*) te->start_addr;
+ if (*instr == BYTE_JUMP) {
+ delta = (((unsigned char*) te->start_addr)
+ + 1);
+ *delta = te->end_addr - te->start_addr -
+ BYTE_JUMP_LEN;
+ te->count = 0;
+ count++;
+ }
+ }
+ te++;
+ }
+ return count;
+}
+
+int ft_is_event_enabled(unsigned long id)
+{
+ struct trace_event* te = __start___event_table;
+
+ while (te < __stop___event_table) {
+ if (te->id == id)
+ return te->count;
+ te++;
+ }
+ return 0;
+}
+
+#endif
diff --git a/arch/x86/kernel/irqinit.c b/arch/x86/kernel/irqinit.c
index 990ae7c..9772b1a 100644
--- a/arch/x86/kernel/irqinit.c
+++ b/arch/x86/kernel/irqinit.c
@@ -189,6 +189,9 @@ static void __init smp_intr_init(void)
alloc_intr_gate(CALL_FUNCTION_SINGLE_VECTOR,
call_function_single_interrupt);
+ /* IPI for hrtimer pulling on remote cpus */
+ alloc_intr_gate(PULL_TIMERS_VECTOR, pull_timers_interrupt);
+
/* Low priority IPI to cleanup after moving an irq */
set_intr_gate(IRQ_MOVE_CLEANUP_VECTOR, irq_move_cleanup_interrupt);
set_bit(IRQ_MOVE_CLEANUP_VECTOR, used_vectors);
diff --git a/arch/x86/kernel/smp.c b/arch/x86/kernel/smp.c
index d801210..74cca60 100644
--- a/arch/x86/kernel/smp.c
+++ b/arch/x86/kernel/smp.c
@@ -23,6 +23,10 @@
#include <linux/cpu.h>
#include <linux/gfp.h>
+#include <litmus/preempt.h>
+#include <litmus/debug_trace.h>
+#include <litmus/trace.h>
+
#include <asm/mtrr.h>
#include <asm/tlbflush.h>
#include <asm/mmu_context.h>
@@ -118,6 +122,7 @@ static void native_smp_send_reschedule(int cpu)
WARN_ON(1);
return;
}
+ TS_SEND_RESCHED_START(cpu);
apic->send_IPI_mask(cpumask_of(cpu), RESCHEDULE_VECTOR);
}
@@ -147,6 +152,16 @@ void native_send_call_func_ipi(const struct cpumask *mask)
free_cpumask_var(allbutself);
}
+/* trigger timers on remote cpu */
+void smp_send_pull_timers(int cpu)
+{
+ if (unlikely(cpu_is_offline(cpu))) {
+ WARN_ON(1);
+ return;
+ }
+ apic->send_IPI_mask(cpumask_of(cpu), PULL_TIMERS_VECTOR);
+}
+
/*
* this function calls the 'stop' function on all other CPUs in the system.
*/
@@ -198,7 +213,10 @@ static void native_smp_send_stop(void)
void smp_reschedule_interrupt(struct pt_regs *regs)
{
ack_APIC_irq();
+ /* LITMUS^RT: this IPI might need to trigger the sched state machine. */
+ sched_state_ipi();
inc_irq_stat(irq_resched_count);
+ TS_SEND_RESCHED_END;
/*
* KVM uses this interrupt to force a cpu out of guest mode
*/
@@ -222,6 +240,15 @@ void smp_call_function_single_interrupt(struct pt_regs *regs)
irq_exit();
}
+extern void hrtimer_pull(void);
+
+void smp_pull_timers_interrupt(struct pt_regs *regs)
+{
+ ack_APIC_irq();
+ TRACE("pull timer interrupt\n");
+ hrtimer_pull();
+}
+
struct smp_ops smp_ops = {
.smp_prepare_boot_cpu = native_smp_prepare_boot_cpu,
.smp_prepare_cpus = native_smp_prepare_cpus,
diff --git a/arch/x86/kernel/syscall_table_32.S b/arch/x86/kernel/syscall_table_32.S
index b35786d..d78c5ed 100644
--- a/arch/x86/kernel/syscall_table_32.S
+++ b/arch/x86/kernel/syscall_table_32.S
@@ -340,3 +340,17 @@ ENTRY(sys_call_table)
.long sys_fanotify_init
.long sys_fanotify_mark
.long sys_prlimit64 /* 340 */
+ .long sys_set_rt_task_param /* LITMUS^RT 341 */
+ .long sys_get_rt_task_param
+ .long sys_complete_job
+ .long sys_od_open
+ .long sys_od_close
+ .long sys_fmlp_down
+ .long sys_fmlp_up
+ .long sys_srp_down
+ .long sys_srp_up
+ .long sys_query_job_no
+ .long sys_wait_for_job_release
+ .long sys_wait_for_ts_release
+ .long sys_release_ts
+ .long sys_null_call
diff --git a/fs/exec.c b/fs/exec.c
index 6d2b6f9..56536ad 100644
--- a/fs/exec.c
+++ b/fs/exec.c
@@ -19,7 +19,7 @@
* current->executable is only used by the procfs. This allows a dispatch
* table to check for several different types of binary formats. We keep
* trying until we recognize the file or we run out of supported binary
- * formats.
+ * formats.
*/
#include <linux/slab.h>
@@ -55,6 +55,8 @@
#include <linux/fs_struct.h>
#include <linux/pipe_fs_i.h>
+#include <litmus/litmus.h>
+
#include <asm/uaccess.h>
#include <asm/mmu_context.h>
#include <asm/tlb.h>
@@ -78,7 +80,7 @@ int __register_binfmt(struct linux_binfmt * fmt, int insert)
insert ? list_add(&fmt->lh, &formats) :
list_add_tail(&fmt->lh, &formats);
write_unlock(&binfmt_lock);
- return 0;
+ return 0;
}
EXPORT_SYMBOL(__register_binfmt);
@@ -1064,7 +1066,7 @@ void setup_new_exec(struct linux_binprm * bprm)
group */
current->self_exec_id++;
-
+
flush_signal_handlers(current, 0);
flush_old_files(current->files);
}
@@ -1154,8 +1156,8 @@ int check_unsafe_exec(struct linux_binprm *bprm)
return res;
}
-/*
- * Fill the binprm structure from the inode.
+/*
+ * Fill the binprm structure from the inode.
* Check permissions, then read the first 128 (BINPRM_BUF_SIZE) bytes
*
* This may be called multiple times for binary chains (scripts for example).
@@ -1367,6 +1369,7 @@ int do_execve(const char * filename,
goto out_unmark;
sched_exec();
+ litmus_exec();
bprm->file = file;
bprm->filename = filename;
diff --git a/fs/inode.c b/fs/inode.c
index 8646433..d4fe9c0 100644
--- a/fs/inode.c
+++ b/fs/inode.c
@@ -266,6 +266,8 @@ void inode_init_once(struct inode *inode)
#ifdef CONFIG_FSNOTIFY
INIT_HLIST_HEAD(&inode->i_fsnotify_marks);
#endif
+ INIT_LIST_HEAD(&inode->i_obj_list);
+ mutex_init(&inode->i_obj_mutex);
}
EXPORT_SYMBOL(inode_init_once);
diff --git a/include/linux/completion.h b/include/linux/completion.h
index 51e3145..c63950e 100644
--- a/include/linux/completion.h
+++ b/include/linux/completion.h
@@ -90,6 +90,7 @@ extern bool completion_done(struct completion *x);
extern void complete(struct completion *);
extern void complete_all(struct completion *);
+extern void complete_n(struct completion *, int n);
/**
* INIT_COMPLETION: - reinitialize a completion structure
diff --git a/include/linux/fs.h b/include/linux/fs.h
index 63d069b..29a6724 100644
--- a/include/linux/fs.h
+++ b/include/linux/fs.h
@@ -16,8 +16,8 @@
* nr_file rlimit, so it's safe to set up a ridiculously high absolute
* upper limit on files-per-process.
*
- * Some programs (notably those using select()) may have to be
- * recompiled to take full advantage of the new limits..
+ * Some programs (notably those using select()) may have to be
+ * recompiled to take full advantage of the new limits..
*/
/* Fixed constants first: */
@@ -172,7 +172,7 @@ struct inodes_stat_t {
#define SEL_EX 4
/* public flags for file_system_type */
-#define FS_REQUIRES_DEV 1
+#define FS_REQUIRES_DEV 1
#define FS_BINARY_MOUNTDATA 2
#define FS_HAS_SUBTYPE 4
#define FS_REVAL_DOT 16384 /* Check the paths ".", ".." for staleness */
@@ -470,7 +470,7 @@ struct iattr {
*/
#include <linux/quota.h>
-/**
+/**
* enum positive_aop_returns - aop return codes with specific semantics
*
* @AOP_WRITEPAGE_ACTIVATE: Informs the caller that page writeback has
@@ -480,7 +480,7 @@ struct iattr {
* be a candidate for writeback again in the near
* future. Other callers must be careful to unlock
* the page if they get this return. Returned by
- * writepage();
+ * writepage();
*
* @AOP_TRUNCATED_PAGE: The AOP method that was handed a locked page has
* unlocked it and the page might have been truncated.
@@ -721,6 +721,7 @@ static inline int mapping_writably_mapped(struct address_space *mapping)
struct posix_acl;
#define ACL_NOT_CACHED ((void *)(-1))
+struct inode_obj_id_table;
struct inode {
struct hlist_node i_hash;
@@ -784,6 +785,8 @@ struct inode {
struct posix_acl *i_acl;
struct posix_acl *i_default_acl;
#endif
+ struct list_head i_obj_list;
+ struct mutex i_obj_mutex;
void *i_private; /* fs or device private pointer */
};
@@ -997,10 +1000,10 @@ static inline int file_check_writeable(struct file *filp)
#define MAX_NON_LFS ((1UL<<31) - 1)
-/* Page cache limit. The filesystems should put that into their s_maxbytes
- limits, otherwise bad things can happen in VM. */
+/* Page cache limit. The filesystems should put that into their s_maxbytes
+ limits, otherwise bad things can happen in VM. */
#if BITS_PER_LONG==32
-#define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
+#define MAX_LFS_FILESIZE (((u64)PAGE_CACHE_SIZE << (BITS_PER_LONG-1))-1)
#elif BITS_PER_LONG==64
#define MAX_LFS_FILESIZE 0x7fffffffffffffffUL
#endif
@@ -2145,7 +2148,7 @@ extern int may_open(struct path *, int, int);
extern int kernel_read(struct file *, loff_t, char *, unsigned long);
extern struct file * open_exec(const char *);
-
+
/* fs/dcache.c -- generic fs support functions */
extern int is_subdir(struct dentry *, struct dentry *);
extern int path_is_under(struct path *, struct path *);
diff --git a/include/linux/hrtimer.h b/include/linux/hrtimer.h
index fd0c1b8..76da541 100644
--- a/include/linux/hrtimer.h
+++ b/include/linux/hrtimer.h
@@ -167,6 +167,7 @@ struct hrtimer_clock_base {
* @nr_retries: Total number of hrtimer interrupt retries
* @nr_hangs: Total number of hrtimer interrupt hangs
* @max_hang_time: Maximum time spent in hrtimer_interrupt
+ * @to_pull: LITMUS^RT list of timers to be pulled on this cpu
*/
struct hrtimer_cpu_base {
raw_spinlock_t lock;
@@ -180,8 +181,32 @@ struct hrtimer_cpu_base {
unsigned long nr_hangs;
ktime_t max_hang_time;
#endif
+ struct list_head to_pull;
};
+#ifdef CONFIG_ARCH_HAS_SEND_PULL_TIMERS
+
+#define HRTIMER_START_ON_INACTIVE 0
+#define HRTIMER_START_ON_QUEUED 1
+
+/*
+ * struct hrtimer_start_on_info - save timer info on remote cpu
+ * @list: list of hrtimer_start_on_info on remote cpu (to_pull)
+ * @timer: timer to be triggered on remote cpu
+ * @time: time event
+ * @mode: timer mode
+ * @state: activity flag
+ */
+struct hrtimer_start_on_info {
+ struct list_head list;
+ struct hrtimer *timer;
+ ktime_t time;
+ enum hrtimer_mode mode;
+ atomic_t state;
+};
+
+#endif
+
static inline void hrtimer_set_expires(struct hrtimer *timer, ktime_t time)
{
timer->_expires = time;
@@ -348,6 +373,13 @@ __hrtimer_start_range_ns(struct hrtimer *timer, ktime_t tim,
unsigned long delta_ns,
const enum hrtimer_mode mode, int wakeup);
+#ifdef CONFIG_ARCH_HAS_SEND_PULL_TIMERS
+extern void hrtimer_start_on_info_init(struct hrtimer_start_on_info *info);
+extern int hrtimer_start_on(int cpu, struct hrtimer_start_on_info *info,
+ struct hrtimer *timer, ktime_t time,
+ const enum hrtimer_mode mode);
+#endif
+
extern int hrtimer_cancel(struct hrtimer *timer);
extern int hrtimer_try_to_cancel(struct hrtimer *timer);
diff --git a/include/linux/sched.h b/include/linux/sched.h
index 1e2a6db..c9ac4fc 100644
--- a/include/linux/sched.h
+++ b/include/linux/sched.h
@@ -38,6 +38,7 @@
#define SCHED_BATCH 3
/* SCHED_ISO: reserved but not implemented yet */
#define SCHED_IDLE 5
+#define SCHED_LITMUS 6
/* Can be ORed in to make sure the process is reverted back to SCHED_NORMAL on fork */
#define SCHED_RESET_ON_FORK 0x40000000
@@ -94,6 +95,9 @@ struct sched_param {
#include <asm/processor.h>
+#include <litmus/rt_param.h>
+#include <litmus/preempt.h>
+
struct exec_domain;
struct futex_pi_state;
struct robust_list_head;
@@ -1159,6 +1163,7 @@ struct sched_rt_entity {
};
struct rcu_node;
+struct od_table_entry;
struct task_struct {
volatile long state; /* -1 unrunnable, 0 runnable, >0 stopped */
@@ -1243,9 +1248,9 @@ struct task_struct {
unsigned long stack_canary;
#endif
- /*
+ /*
* pointers to (original) parent process, youngest child, younger sibling,
- * older sibling, respectively. (p->father can be replaced with
+ * older sibling, respectively. (p->father can be replaced with
* p->real_parent->pid)
*/
struct task_struct *real_parent; /* real parent process */
@@ -1453,6 +1458,13 @@ struct task_struct {
int make_it_fail;
#endif
struct prop_local_single dirties;
+
+ /* LITMUS RT parameters and state */
+ struct rt_param rt_param;
+
+ /* references to PI semaphores, etc. */
+ struct od_table_entry *od_table;
+
#ifdef CONFIG_LATENCYTOP
int latency_record_count;
struct latency_record latency_record[LT_SAVECOUNT];
@@ -2014,7 +2026,7 @@ static inline int dequeue_signal_lock(struct task_struct *tsk, sigset_t *mask, s
spin_unlock_irqrestore(&tsk->sighand->siglock, flags);
return ret;
-}
+}
extern void block_all_signals(int (*notifier)(void *priv), void *priv,
sigset_t *mask);
@@ -2290,6 +2302,7 @@ static inline int test_tsk_thread_flag(struct task_struct *tsk, int flag)
static inline void set_tsk_need_resched(struct task_struct *tsk)
{
set_tsk_thread_flag(tsk,TIF_NEED_RESCHED);
+ sched_state_will_schedule(tsk);
}
static inline void clear_tsk_need_resched(struct task_struct *tsk)
diff --git a/include/linux/smp.h b/include/linux/smp.h
index cfa2d20..f86d407 100644
--- a/include/linux/smp.h
+++ b/include/linux/smp.h
@@ -80,6 +80,11 @@ int smp_call_function_any(const struct cpumask *mask,
void (*func)(void *info), void *info, int wait);
/*
+ * sends a 'pull timer' event to a remote CPU
+ */
+extern void smp_send_pull_timers(int cpu);
+
+/*
* Generic and arch helpers
*/
#ifdef CONFIG_USE_GENERIC_SMP_HELPERS
diff --git a/include/linux/tick.h b/include/linux/tick.h
index b232ccc..1e29bd5 100644
--- a/include/linux/tick.h
+++ b/include/linux/tick.h
@@ -74,6 +74,11 @@ extern int tick_is_oneshot_available(void);
extern struct tick_device *tick_get_device(int cpu);
# ifdef CONFIG_HIGH_RES_TIMERS
+/* LITMUS^RT tick alignment */
+#define LINUX_DEFAULT_TICKS 0
+#define LITMUS_ALIGNED_TICKS 1
+#define LITMUS_STAGGERED_TICKS 2
+
extern int tick_init_highres(void);
extern int tick_program_event(ktime_t expires, int force);
extern void tick_setup_sched_timer(void);
diff --git a/include/litmus/bheap.h b/include/litmus/bheap.h
new file mode 100644
index 0000000..cf4864a
--- /dev/null
+++ b/include/litmus/bheap.h
@@ -0,0 +1,77 @@
+/* bheaps.h -- Binomial Heaps
+ *
+ * (c) 2008, 2009 Bjoern Brandenburg
+ */
+
+#ifndef BHEAP_H
+#define BHEAP_H
+
+#define NOT_IN_HEAP UINT_MAX
+
+struct bheap_node {
+ struct bheap_node* parent;
+ struct bheap_node* next;
+ struct bheap_node* child;
+
+ unsigned int degree;
+ void* value;
+ struct bheap_node** ref;
+};
+
+struct bheap {
+ struct bheap_node* head;
+ /* We cache the minimum of the heap.
+ * This speeds up repeated peek operations.
+ */
+ struct bheap_node* min;
+};
+
+typedef int (*bheap_prio_t)(struct bheap_node* a, struct bheap_node* b);
+
+void bheap_init(struct bheap* heap);
+void bheap_node_init(struct bheap_node** ref_to_bheap_node_ptr, void* value);
+
+static inline int bheap_node_in_heap(struct bheap_node* h)
+{
+ return h->degree != NOT_IN_HEAP;
+}
+
+static inline int bheap_empty(struct bheap* heap)
+{
+ return heap->head == NULL && heap->min == NULL;
+}
+
+/* insert (and reinitialize) a node into the heap */
+void bheap_insert(bheap_prio_t higher_prio,
+ struct bheap* heap,
+ struct bheap_node* node);
+
+/* merge addition into target */
+void bheap_union(bheap_prio_t higher_prio,
+ struct bheap* target,
+ struct bheap* addition);
+
+struct bheap_node* bheap_peek(bheap_prio_t higher_prio,
+ struct bheap* heap);
+
+struct bheap_node* bheap_take(bheap_prio_t higher_prio,
+ struct bheap* heap);
+
+void bheap_uncache_min(bheap_prio_t higher_prio, struct bheap* heap);
+int bheap_decrease(bheap_prio_t higher_prio, struct bheap_node* node);
+
+void bheap_delete(bheap_prio_t higher_prio,
+ struct bheap* heap,
+ struct bheap_node* node);
+
+/* allocate from memcache */
+struct bheap_node* bheap_node_alloc(int gfp_flags);
+void bheap_node_free(struct bheap_node* hn);
+
+/* allocate a heap node for value and insert into the heap */
+int bheap_add(bheap_prio_t higher_prio, struct bheap* heap,
+ void* value, int gfp_flags);
+
+void* bheap_take_del(bheap_prio_t higher_prio,
+ struct bheap* heap);
+#endif
diff --git a/include/litmus/budget.h b/include/litmus/budget.h
new file mode 100644
index 0000000..732530e
--- /dev/null
+++ b/include/litmus/budget.h
@@ -0,0 +1,8 @@
+#ifndef _LITMUS_BUDGET_H_
+#define _LITMUS_BUDGET_H_
+
+/* Update the per-processor enforcement timer (arm/reproram/cancel) for
+ * the next task. */
+void update_enforcement_timer(struct task_struct* t);
+
+#endif
diff --git a/include/litmus/debug_trace.h b/include/litmus/debug_trace.h
new file mode 100644
index 0000000..48d086d
--- /dev/null
+++ b/include/litmus/debug_trace.h
@@ -0,0 +1,37 @@
+#ifndef LITMUS_DEBUG_TRACE_H
+#define LITMUS_DEBUG_TRACE_H
+
+#ifdef CONFIG_SCHED_DEBUG_TRACE
+void sched_trace_log_message(const char* fmt, ...);
+void dump_trace_buffer(int max);
+#else
+
+#define sched_trace_log_message(fmt, ...)
+
+#endif
+
+extern atomic_t __log_seq_no;
+
+#ifdef CONFIG_SCHED_DEBUG_TRACE_CALLER
+#define TRACE_PREFIX "%d P%d [%s@%s:%d]: "
+#define TRACE_ARGS atomic_add_return(1, &__log_seq_no), \
+ raw_smp_processor_id(), \
+ __FUNCTION__, __FILE__, __LINE__
+#else
+#define TRACE_PREFIX "%d P%d: "
+#define TRACE_ARGS atomic_add_return(1, &__log_seq_no), \
+ raw_smp_processor_id()
+#endif
+
+#define TRACE(fmt, args...) \
+ sched_trace_log_message(TRACE_PREFIX fmt, \
+ TRACE_ARGS, ## args)
+
+#define TRACE_TASK(t, fmt, args...) \
+ TRACE("(%s/%d:%d) " fmt, (t)->comm, (t)->pid, \
+ (t)->rt_param.job_params.job_no, ##args)
+
+#define TRACE_CUR(fmt, args...) \
+ TRACE_TASK(current, fmt, ## args)
+
+#endif
diff --git a/include/litmus/edf_common.h b/include/litmus/edf_common.h
new file mode 100644
index 0000000..80d4321
--- /dev/null
+++ b/include/litmus/edf_common.h
@@ -0,0 +1,27 @@
+/*
+ * EDF common data structures and utility functions shared by all EDF
+ * based scheduler plugins
+ */
+
+/* CLEANUP: Add comments and make it less messy.
+ *
+ */
+
+#ifndef __UNC_EDF_COMMON_H__
+#define __UNC_EDF_COMMON_H__
+
+#include <litmus/rt_domain.h>
+
+void edf_domain_init(rt_domain_t* rt, check_resched_needed_t resched,
+ release_jobs_t release);
+
+int edf_higher_prio(struct task_struct* first,
+ struct task_struct* second);
+
+int edf_ready_order(struct bheap_node* a, struct bheap_node* b);
+
+int edf_preemption_needed(rt_domain_t* rt, struct task_struct *t);
+
+int edf_set_hp_task(struct pi_semaphore *sem);
+int edf_set_hp_cpu_task(struct pi_semaphore *sem, int cpu);
+#endif
diff --git a/include/litmus/fdso.h b/include/litmus/fdso.h
new file mode 100644
index 0000000..61f1b5b
--- /dev/null
+++ b/include/litmus/fdso.h
@@ -0,0 +1,70 @@
+/* fdso.h - file descriptor attached shared objects
+ *
+ * (c) 2007 B. Brandenburg, LITMUS^RT project
+ */
+
+#ifndef _LINUX_FDSO_H_
+#define _LINUX_FDSO_H_
+
+#include <linux/list.h>
+#include <asm/atomic.h>
+
+#include <linux/fs.h>
+#include <linux/slab.h>
+
+#define MAX_OBJECT_DESCRIPTORS 32
+
+typedef enum {
+ MIN_OBJ_TYPE = 0,
+
+ FMLP_SEM = 0,
+ SRP_SEM = 1,
+
+ MAX_OBJ_TYPE = 1
+} obj_type_t;
+
+struct inode_obj_id {
+ struct list_head list;
+ atomic_t count;
+ struct inode* inode;
+
+ obj_type_t type;
+ void* obj;
+ unsigned int id;
+};
+
+
+struct od_table_entry {
+ unsigned int used;
+
+ struct inode_obj_id* obj;
+ void* extra;
+};
+
+struct fdso_ops {
+ void* (*create) (void);
+ void (*destroy)(void*);
+ int (*open) (struct od_table_entry*, void* __user);
+ int (*close) (struct od_table_entry*);
+};
+
+/* translate a userspace supplied od into the raw table entry
+ * returns NULL if od is invalid
+ */
+struct od_table_entry* __od_lookup(int od);
+
+/* translate a userspace supplied od into the associated object
+ * returns NULL if od is invalid
+ */
+static inline void* od_lookup(int od, obj_type_t type)
+{
+ struct od_table_entry* e = __od_lookup(od);
+ return e && e->obj->type == type ? e->obj->obj : NULL;
+}
+
+#define lookup_fmlp_sem(od)((struct pi_semaphore*) od_lookup(od, FMLP_SEM))
+#define lookup_srp_sem(od) ((struct srp_semaphore*) od_lookup(od, SRP_SEM))
+#define lookup_ics(od) ((struct ics*) od_lookup(od, ICS_ID))
+
+
+#endif
diff --git a/include/litmus/feather_buffer.h b/include/litmus/feather_buffer.h
new file mode 100644
index 0000000..6c18277
--- /dev/null
+++ b/include/litmus/feather_buffer.h
@@ -0,0 +1,94 @@
+#ifndef _FEATHER_BUFFER_H_
+#define _FEATHER_BUFFER_H_
+
+/* requires UINT_MAX and memcpy */
+
+#define SLOT_FREE 0
+#define SLOT_BUSY 1
+#define SLOT_READY 2
+
+struct ft_buffer {
+ unsigned int slot_count;
+ unsigned int slot_size;
+
+ int free_count;
+ unsigned int write_idx;
+ unsigned int read_idx;
+
+ char* slots;
+ void* buffer_mem;
+ unsigned int failed_writes;
+};
+
+static inline int init_ft_buffer(struct ft_buffer* buf,
+ unsigned int slot_count,
+ unsigned int slot_size,
+ char* slots,
+ void* buffer_mem)
+{
+ int i = 0;
+ if (!slot_count || UINT_MAX % slot_count != slot_count - 1) {
+ /* The slot count must divide UNIT_MAX + 1 so that when it
+ * wraps around the index correctly points to 0.
+ */
+ return 0;
+ } else {
+ buf->slot_count = slot_count;
+ buf->slot_size = slot_size;
+ buf->slots = slots;
+ buf->buffer_mem = buffer_mem;
+ buf->free_count = slot_count;
+ buf->write_idx = 0;
+ buf->read_idx = 0;
+ buf->failed_writes = 0;
+ for (i = 0; i < slot_count; i++)
+ buf->slots[i] = SLOT_FREE;
+ return 1;
+ }
+}
+
+static inline int ft_buffer_start_write(struct ft_buffer* buf, void **ptr)
+{
+ int free = fetch_and_dec(&buf->free_count);
+ unsigned int idx;
+ if (free <= 0) {
+ fetch_and_inc(&buf->free_count);
+ *ptr = 0;
+ fetch_and_inc(&buf->failed_writes);
+ return 0;
+ } else {
+ idx = fetch_and_inc((int*) &buf->write_idx) % buf->slot_count;
+ buf->slots[idx] = SLOT_BUSY;
+ *ptr = ((char*) buf->buffer_mem) + idx * buf->slot_size;
+ return 1;
+ }
+}
+
+static inline void ft_buffer_finish_write(struct ft_buffer* buf, void *ptr)
+{
+ unsigned int idx = ((char*) ptr - (char*) buf->buffer_mem) / buf->slot_size;
+ buf->slots[idx] = SLOT_READY;
+}
+
+
+/* exclusive reader access is assumed */
+static inline int ft_buffer_read(struct ft_buffer* buf, void* dest)
+{
+ unsigned int idx;
+ if (buf->free_count == buf->slot_count)
+ /* nothing available */
+ return 0;
+ idx = buf->read_idx % buf->slot_count;
+ if (buf->slots[idx] == SLOT_READY) {
+ memcpy(dest, ((char*) buf->buffer_mem) + idx * buf->slot_size,
+ buf->slot_size);
+ buf->slots[idx] = SLOT_FREE;
+ buf->read_idx++;
+ fetch_and_inc(&buf->free_count);
+ return 1;
+ } else
+ return 0;
+}
+
+
+#endif
diff --git a/include/litmus/feather_trace.h b/include/litmus/feather_trace.h
new file mode 100644
index 0000000..028dfb2
--- /dev/null
+++ b/include/litmus/feather_trace.h
@@ -0,0 +1,65 @@
+#ifndef _FEATHER_TRACE_H_
+#define _FEATHER_TRACE_H_
+
+#include <asm/atomic.h>
+
+int ft_enable_event(unsigned long id);
+int ft_disable_event(unsigned long id);
+int ft_is_event_enabled(unsigned long id);
+int ft_disable_all_events(void);
+
+/* atomic_* funcitons are inline anyway */
+static inline int fetch_and_inc(int *val)
+{
+ return atomic_add_return(1, (atomic_t*) val) - 1;
+}
+
+static inline int fetch_and_dec(int *val)
+{
+ return atomic_sub_return(1, (atomic_t*) val) + 1;
+}
+
+/* Don't use rewriting implementation if kernel text pages are read-only.
+ * Ftrace gets around this by using the identity mapping, but that's more
+ * effort that is warrented right now for Feather-Trace.
+ * Eventually, it may make sense to replace Feather-Trace with ftrace.
+ */
+#if defined(CONFIG_ARCH_HAS_FEATHER_TRACE) && !defined(CONFIG_DEBUG_RODATA)
+
+#include <asm/feather_trace.h>
+
+#else /* !__ARCH_HAS_FEATHER_TRACE */
+
+/* provide default implementation */
+
+#include <asm/timex.h> /* for get_cycles() */
+
+static inline unsigned long long ft_timestamp(void)
+{
+ return get_cycles();
+}
+
+#define feather_callback
+
+#define MAX_EVENTS 1024
+
+extern int ft_events[MAX_EVENTS];
+
+#define ft_event(id, callback) \
+ if (ft_events[id]) callback();
+
+#define ft_event0(id, callback) \
+ if (ft_events[id]) callback(id);
+
+#define ft_event1(id, callback, param) \
+ if (ft_events[id]) callback(id, param);
+
+#define ft_event2(id, callback, param, param2) \
+ if (ft_events[id]) callback(id, param, param2);
+
+#define ft_event3(id, callback, p, p2, p3) \
+ if (ft_events[id]) callback(id, p, p2, p3);
+
+#endif /* __ARCH_HAS_FEATHER_TRACE */
+
+#endif
diff --git a/include/litmus/ftdev.h b/include/litmus/ftdev.h
new file mode 100644
index 0000000..348387e
--- /dev/null
+++ b/include/litmus/ftdev.h
@@ -0,0 +1,52 @@
+#ifndef _LITMUS_FTDEV_H_
+#define _LITMUS_FTDEV_H_
+
+#include <litmus/feather_trace.h>
+#include <litmus/feather_buffer.h>
+#include <linux/mutex.h>
+#include <linux/cdev.h>
+
+#define FTDEV_ENABLE_CMD 0
+#define FTDEV_DISABLE_CMD 1
+
+struct ftdev;
+
+/* return 0 if buffer can be opened, otherwise -$REASON */
+typedef int (*ftdev_can_open_t)(struct ftdev* dev, unsigned int buf_no);
+/* return 0 on success, otherwise -$REASON */
+typedef int (*ftdev_alloc_t)(struct ftdev* dev, unsigned int buf_no);
+typedef void (*ftdev_free_t)(struct ftdev* dev, unsigned int buf_no);
+
+
+struct ftdev_event;
+
+struct ftdev_minor {
+ struct ft_buffer* buf;
+ unsigned int readers;
+ struct mutex lock;
+ /* FIXME: filter for authorized events */
+ struct ftdev_event* events;
+ struct device* device;
+};
+
+struct ftdev {
+ dev_t major;
+ struct cdev cdev;
+ struct class* class;
+ const char* name;
+ struct ftdev_minor* minor;
+ unsigned int minor_cnt;
+ ftdev_alloc_t alloc;
+ ftdev_free_t free;
+ ftdev_can_open_t can_open;
+};
+
+struct ft_buffer* alloc_ft_buffer(unsigned int count, size_t size);
+void free_ft_buffer(struct ft_buffer* buf);
+
+int ftdev_init( struct ftdev* ftdev, struct module* owner,
+ const int minor_cnt, const char* name);
+void ftdev_exit(struct ftdev* ftdev);
+int register_ftdev(struct ftdev* ftdev);
+
+#endif
diff --git a/include/litmus/jobs.h b/include/litmus/jobs.h
new file mode 100644
index 0000000..9bd361e
--- /dev/null
+++ b/include/litmus/jobs.h
@@ -0,0 +1,9 @@
+#ifndef __LITMUS_JOBS_H__
+#define __LITMUS_JOBS_H__
+
+void prepare_for_next_period(struct task_struct *t);
+void release_at(struct task_struct *t, lt_t start);
+long complete_job(void);
+
+#endif
+
diff --git a/include/litmus/litmus.h b/include/litmus/litmus.h
new file mode 100644
index 0000000..2464837
--- /dev/null
+++ b/include/litmus/litmus.h
@@ -0,0 +1,241 @@
+/*
+ * Constant definitions related to
+ * scheduling policy.
+ */
+
+#ifndef _LINUX_LITMUS_H_
+#define _LINUX_LITMUS_H_
+
+#include <litmus/debug_trace.h>
+
+#ifdef CONFIG_RELEASE_MASTER
+extern atomic_t release_master_cpu;
+#endif
+
+/* in_list - is a given list_head queued on some list?
+ */
+static inline int in_list(struct list_head* list)
+{
+ return !( /* case 1: deleted */
+ (list->next == LIST_POISON1 &&
+ list->prev == LIST_POISON2)
+ ||
+ /* case 2: initialized */
+ (list->next == list &&
+ list->prev == list)
+ );
+}
+
+#define NO_CPU 0xffffffff
+
+void litmus_fork(struct task_struct *tsk);
+void litmus_exec(void);
+/* clean up real-time state of a task */
+void exit_litmus(struct task_struct *dead_tsk);
+
+long litmus_admit_task(struct task_struct *tsk);
+void litmus_exit_task(struct task_struct *tsk);
+
+#define is_realtime(t) ((t)->policy == SCHED_LITMUS)
+#define rt_transition_pending(t) \
+ ((t)->rt_param.transition_pending)
+
+#define tsk_rt(t) (&(t)->rt_param)
+
+/* Realtime utility macros */
+#define get_rt_flags(t) (tsk_rt(t)->flags)
+#define set_rt_flags(t,f) (tsk_rt(t)->flags=(f))
+#define get_exec_cost(t) (tsk_rt(t)->task_params.exec_cost)
+#define get_exec_time(t) (tsk_rt(t)->job_params.exec_time)
+#define get_rt_period(t) (tsk_rt(t)->task_params.period)
+#define get_rt_phase(t) (tsk_rt(t)->task_params.phase)
+#define get_partition(t) (tsk_rt(t)->task_params.cpu)
+#define get_deadline(t) (tsk_rt(t)->job_params.deadline)
+#define get_release(t) (tsk_rt(t)->job_params.release)
+#define get_class(t) (tsk_rt(t)->task_params.cls)
+
+inline static int budget_exhausted(struct task_struct* t)
+{
+ return get_exec_time(t) >= get_exec_cost(t);
+}
+
+inline static lt_t budget_remaining(struct task_struct* t)
+{
+ if (!budget_exhausted(t))
+ return get_exec_cost(t) - get_exec_time(t);
+ else
+ /* avoid overflow */
+ return 0;
+}
+
+#define budget_enforced(t) (tsk_rt(t)->task_params.budget_policy != NO_ENFORCEMENT)
+
+#define budget_precisely_enforced(t) (tsk_rt(t)->task_params.budget_policy \
+ == PRECISE_ENFORCEMENT)
+
+#define is_hrt(t) \
+ (tsk_rt(t)->task_params.class == RT_CLASS_HARD)
+#define is_srt(t) \
+ (tsk_rt(t)->task_params.class == RT_CLASS_SOFT)
+#define is_be(t) \
+ (tsk_rt(t)->task_params.class == RT_CLASS_BEST_EFFORT)
+
+/* Our notion of time within LITMUS: kernel monotonic time. */
+static inline lt_t litmus_clock(void)
+{
+ return ktime_to_ns(ktime_get());
+}
+
+/* A macro to convert from nanoseconds to ktime_t. */
+#define ns_to_ktime(t) ktime_add_ns(ktime_set(0, 0), t)
+
+#define get_domain(t) (tsk_rt(t)->domain)
+
+/* Honor the flag in the preempt_count variable that is set
+ * when scheduling is in progress.
+ */
+#define is_running(t) \
+ ((t)->state == TASK_RUNNING || \
+ task_thread_info(t)->preempt_count & PREEMPT_ACTIVE)
+
+#define is_blocked(t) \
+ (!is_running(t))
+#define is_released(t, now) \
+ (lt_before_eq(get_release(t), now))
+#define is_tardy(t, now) \
+ (lt_before_eq(tsk_rt(t)->job_params.deadline, now))
+
+/* real-time comparison macros */
+#define earlier_deadline(a, b) (lt_before(\
+ (a)->rt_param.job_params.deadline,\
+ (b)->rt_param.job_params.deadline))
+#define earlier_release(a, b) (lt_before(\
+ (a)->rt_param.job_params.release,\
+ (b)->rt_param.job_params.release))
+
+void preempt_if_preemptable(struct task_struct* t, int on_cpu);
+
+#ifdef CONFIG_SRP
+void srp_ceiling_block(void);
+#else
+#define srp_ceiling_block() /* nothing */
+#endif
+
+#define bheap2task(hn) ((struct task_struct*) hn->value)
+
+#ifdef CONFIG_NP_SECTION
+
+static inline int is_kernel_np(struct task_struct *t)
+{
+ return tsk_rt(t)->kernel_np;
+}
+
+static inline int is_user_np(struct task_struct *t)
+{
+ return tsk_rt(t)->ctrl_page ? tsk_rt(t)->ctrl_page->np_flag : 0;
+}
+
+static inline void request_exit_np(struct task_struct *t)
+{
+ if (is_user_np(t)) {
+ /* Set the flag that tells user space to call
+ * into the kernel at the end of a critical section. */
+ if (likely(tsk_rt(t)->ctrl_page)) {
+ TRACE_TASK(t, "setting delayed_preemption flag\n");
+ tsk_rt(t)->ctrl_page->delayed_preemption = 1;
+ }
+ }
+}
+
+static inline void clear_exit_np(struct task_struct *t)
+{
+ if (likely(tsk_rt(t)->ctrl_page))
+ tsk_rt(t)->ctrl_page->delayed_preemption = 0;
+}
+
+static inline void make_np(struct task_struct *t)
+{
+ tsk_rt(t)->kernel_np++;
+}
+
+/* Caller should check if preemption is necessary when
+ * the function return 0.
+ */
+static inline int take_np(struct task_struct *t)
+{
+ return --tsk_rt(t)->kernel_np;
+}
+
+#else
+
+static inline int is_kernel_np(struct task_struct* t)
+{
+ return 0;
+}
+
+static inline int is_user_np(struct task_struct* t)
+{
+ return 0;
+}
+
+static inline void request_exit_np(struct task_struct *t)
+{
+ /* request_exit_np() shouldn't be called if !CONFIG_NP_SECTION */
+ BUG();
+}
+
+static inline void clear_exit_np(struct task_struct* t)
+{
+}
+
+#endif
+
+static inline int is_np(struct task_struct *t)
+{
+#ifdef CONFIG_SCHED_DEBUG_TRACE
+ int kernel, user;
+ kernel = is_kernel_np(t);
+ user = is_user_np(t);
+ if (kernel || user)
+ TRACE_TASK(t, " is non-preemptive: kernel=%d user=%d\n",
+
+ kernel, user);
+ return kernel || user;
+#else
+ return unlikely(is_kernel_np(t) || is_user_np(t));
+#endif
+}
+
+static inline int is_present(struct task_struct* t)
+{
+ return t && tsk_rt(t)->present;
+}
+
+
+/* make the unit explicit */
+typedef unsigned long quanta_t;
+
+enum round {
+ FLOOR,
+ CEIL
+};
+
+
+/* Tick period is used to convert ns-specified execution
+ * costs and periods into tick-based equivalents.
+ */
+extern ktime_t tick_period;
+
+static inline quanta_t time2quanta(lt_t time, enum round round)
+{
+ s64 quantum_length = ktime_to_ns(tick_period);
+
+ if (do_div(time, quantum_length) && round == CEIL)
+ time++;
+ return (quanta_t) time;
+}
+
+/* By how much is cpu staggered behind CPU 0? */
+u64 cpu_stagger_offset(int cpu);
+
+#endif
diff --git a/include/litmus/litmus_proc.h b/include/litmus/litmus_proc.h
new file mode 100644
index 0000000..fbc0082
--- /dev/null
+++ b/include/litmus/litmus_proc.h
@@ -0,0 +1,19 @@
+#include <litmus/sched_plugin.h>
+#include <linux/proc_fs.h>
+
+int __init init_litmus_proc(void);
+void exit_litmus_proc(void);
+
+/*
+ * On success, returns 0 and sets the pointer to the location of the new
+ * proc dir entry, otherwise returns an error code and sets pde to NULL.
+ */
+long make_plugin_proc_dir(struct sched_plugin* plugin,
+ struct proc_dir_entry** pde);
+
+/*
+ * Plugins should deallocate all child proc directory entries before
+ * calling this, to avoid memory leaks.
+ */
+void remove_plugin_proc_dir(struct sched_plugin* plugin);
+
diff --git a/include/litmus/preempt.h b/include/litmus/preempt.h
new file mode 100644
index 0000000..260c6fe
--- /dev/null
+++ b/include/litmus/preempt.h
@@ -0,0 +1,164 @@
+#ifndef LITMUS_PREEMPT_H
+#define LITMUS_PREEMPT_H
+
+#include <linux/types.h>
+#include <linux/cache.h>
+#include <linux/percpu.h>
+#include <asm/atomic.h>
+
+#include <litmus/debug_trace.h>
+
+extern DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, resched_state);
+
+#ifdef CONFIG_DEBUG_KERNEL
+const char* sched_state_name(int s);
+#define TRACE_STATE(fmt, args...) TRACE("SCHED_STATE " fmt, args)
+#else
+#define TRACE_STATE(fmt, args...) /* ignore */
+#endif
+
+#define VERIFY_SCHED_STATE(x) \
+ do { int __s = get_sched_state(); \
+ if ((__s & (x)) == 0) \
+ TRACE_STATE("INVALID s=0x%x (%s) not " \
+ "in 0x%x (%s) [%s]\n", \
+ __s, sched_state_name(__s), \
+ (x), #x, __FUNCTION__); \
+ } while (0);
+
+#define TRACE_SCHED_STATE_CHANGE(x, y, cpu) \
+ TRACE_STATE("[P%d] 0x%x (%s) -> 0x%x (%s)\n", \
+ cpu, (x), sched_state_name(x), \
+ (y), sched_state_name(y))
+
+
+typedef enum scheduling_state {
+ TASK_SCHEDULED = (1 << 0), /* The currently scheduled task is the one that
+ * should be scheduled, and the processor does not
+ * plan to invoke schedule(). */
+ SHOULD_SCHEDULE = (1 << 1), /* A remote processor has determined that the
+ * processor should reschedule, but this has not
+ * been communicated yet (IPI still pending). */
+ WILL_SCHEDULE = (1 << 2), /* The processor has noticed that it has to
+ * reschedule and will do so shortly. */
+ TASK_PICKED = (1 << 3), /* The processor is currently executing schedule(),
+ * has selected a new task to schedule, but has not
+ * yet performed the actual context switch. */
+ PICKED_WRONG_TASK = (1 << 4), /* The processor has not yet performed the context
+ * switch, but a remote processor has already
+ * determined that a higher-priority task became
+ * eligible after the task was picked. */
+} sched_state_t;
+
+static inline sched_state_t get_sched_state_on(int cpu)
+{
+ return atomic_read(&per_cpu(resched_state, cpu));
+}
+
+static inline sched_state_t get_sched_state(void)
+{
+ return atomic_read(&__get_cpu_var(resched_state));
+}
+
+static inline int is_in_sched_state(int possible_states)
+{
+ return get_sched_state() & possible_states;
+}
+
+static inline int cpu_is_in_sched_state(int cpu, int possible_states)
+{
+ return get_sched_state_on(cpu) & possible_states;
+}
+
+static inline void set_sched_state(sched_state_t s)
+{
+ TRACE_SCHED_STATE_CHANGE(get_sched_state(), s, smp_processor_id());
+ atomic_set(&__get_cpu_var(resched_state), s);
+}
+
+static inline int sched_state_transition(sched_state_t from, sched_state_t to)
+{
+ sched_state_t old_state;
+
+ old_state = atomic_cmpxchg(&__get_cpu_var(resched_state), from, to);
+ if (old_state == from) {
+ TRACE_SCHED_STATE_CHANGE(from, to, smp_processor_id());
+ return 1;
+ } else
+ return 0;
+}
+
+static inline int sched_state_transition_on(int cpu,
+ sched_state_t from,
+ sched_state_t to)
+{
+ sched_state_t old_state;
+
+ old_state = atomic_cmpxchg(&per_cpu(resched_state, cpu), from, to);
+ if (old_state == from) {
+ TRACE_SCHED_STATE_CHANGE(from, to, cpu);
+ return 1;
+ } else
+ return 0;
+}
+
+/* Plugins must call this function after they have decided which job to
+ * schedule next. IMPORTANT: this function must be called while still holding
+ * the lock that is used to serialize scheduling decisions.
+ *
+ * (Ideally, we would like to use runqueue locks for this purpose, but that
+ * would lead to deadlocks with the migration code.)
+ */
+static inline void sched_state_task_picked(void)
+{
+ VERIFY_SCHED_STATE(WILL_SCHEDULE);
+
+ /* WILL_SCHEDULE has only a local tansition => simple store is ok */
+ set_sched_state(TASK_PICKED);
+}
+
+static inline void sched_state_entered_schedule(void)
+{
+ /* Update state for the case that we entered schedule() not due to
+ * set_tsk_need_resched() */
+ set_sched_state(WILL_SCHEDULE);
+}
+
+/* Called by schedule() to check if the scheduling decision is still valid
+ * after a context switch. Returns 1 if the CPU needs to reschdule. */
+static inline int sched_state_validate_switch(void)
+{
+ int left_state_ok = 0;
+
+ VERIFY_SCHED_STATE(PICKED_WRONG_TASK | TASK_PICKED);
+
+ if (is_in_sched_state(TASK_PICKED)) {
+ /* Might be good; let's try to transition out of this
+ * state. This must be done atomically since remote processors
+ * may try to change the state, too. */
+ left_state_ok = sched_state_transition(TASK_PICKED, TASK_SCHEDULED);
+ }
+
+ if (!left_state_ok) {
+ /* We raced with a higher-priority task arrival => not
+ * valid. The CPU needs to reschedule. */
+ set_sched_state(WILL_SCHEDULE);
+ return 1;
+ } else
+ return 0;
+}
+
+/* State transition events. See litmus/preempt.c for details. */
+void sched_state_will_schedule(struct task_struct* tsk);
+void sched_state_ipi(void);
+/* Cause a CPU (remote or local) to reschedule. */
+void litmus_reschedule(int cpu);
+void litmus_reschedule_local(void);
+
+#ifdef CONFIG_DEBUG_KERNEL
+void sched_state_plugin_check(void);
+#else
+#define sched_state_plugin_check() /* no check */
+#endif
+
+#endif
diff --git a/include/litmus/rt_domain.h b/include/litmus/rt_domain.h
new file mode 100644
index 0000000..ac24929
--- /dev/null
+++ b/include/litmus/rt_domain.h
@@ -0,0 +1,182 @@
+/* CLEANUP: Add comments and make it less messy.
+ *
+ */
+
+#ifndef __UNC_RT_DOMAIN_H__
+#define __UNC_RT_DOMAIN_H__
+
+#include <litmus/bheap.h>
+
+#define RELEASE_QUEUE_SLOTS 127 /* prime */
+
+struct _rt_domain;
+
+typedef int (*check_resched_needed_t)(struct _rt_domain *rt);
+typedef void (*release_jobs_t)(struct _rt_domain *rt, struct bheap* tasks);
+
+struct release_queue {
+ /* each slot maintains a list of release heaps sorted
+ * by release time */
+ struct list_head slot[RELEASE_QUEUE_SLOTS];
+};
+
+typedef struct _rt_domain {
+ /* runnable rt tasks are in here */
+ raw_spinlock_t ready_lock;
+ struct bheap ready_queue;
+
+ /* real-time tasks waiting for release are in here */
+ raw_spinlock_t release_lock;
+ struct release_queue release_queue;
+
+#ifdef CONFIG_RELEASE_MASTER
+ int release_master;
+#endif
+
+ /* for moving tasks to the release queue */
+ raw_spinlock_t tobe_lock;
+ struct list_head tobe_released;
+
+ /* how do we check if we need to kick another CPU? */
+ check_resched_needed_t check_resched;
+
+ /* how do we release jobs? */
+ release_jobs_t release_jobs;
+
+ /* how are tasks ordered in the ready queue? */
+ bheap_prio_t order;
+} rt_domain_t;
+
+struct release_heap {
+ /* list_head for per-time-slot list */
+ struct list_head list;
+ lt_t release_time;
+ /* all tasks to be released at release_time */
+ struct bheap heap;
+ /* used to trigger the release */
+ struct hrtimer timer;
+
+#ifdef CONFIG_RELEASE_MASTER
+ /* used to delegate releases */
+ struct hrtimer_start_on_info info;
+#endif
+ /* required for the timer callback */
+ rt_domain_t* dom;
+};
+
+
+static inline struct task_struct* __next_ready(rt_domain_t* rt)
+{
+ struct bheap_node *hn = bheap_peek(rt->order, &rt->ready_queue);
+ if (hn)
+ return bheap2task(hn);
+ else
+ return NULL;
+}
+
+void rt_domain_init(rt_domain_t *rt, bheap_prio_t order,
+ check_resched_needed_t check,
+ release_jobs_t relase);
+
+void __add_ready(rt_domain_t* rt, struct task_struct *new);
+void __merge_ready(rt_domain_t* rt, struct bheap *tasks);
+void __add_release(rt_domain_t* rt, struct task_struct *task);
+
+static inline struct task_struct* __take_ready(rt_domain_t* rt)
+{
+ struct bheap_node* hn = bheap_take(rt->order, &rt->ready_queue);
+ if (hn)
+ return bheap2task(hn);
+ else
+ return NULL;
+}
+
+static inline struct task_struct* __peek_ready(rt_domain_t* rt)
+{
+ struct bheap_node* hn = bheap_peek(rt->order, &rt->ready_queue);
+ if (hn)
+ return bheap2task(hn);
+ else
+ return NULL;
+}
+
+static inline int is_queued(struct task_struct *t)
+{
+ BUG_ON(!tsk_rt(t)->heap_node);
+ return bheap_node_in_heap(tsk_rt(t)->heap_node);
+}
+
+static inline void remove(rt_domain_t* rt, struct task_struct *t)
+{
+ bheap_delete(rt->order, &rt->ready_queue, tsk_rt(t)->heap_node);
+}
+
+static inline void add_ready(rt_domain_t* rt, struct task_struct *new)
+{
+ unsigned long flags;
+ /* first we need the write lock for rt_ready_queue */
+ raw_spin_lock_irqsave(&rt->ready_lock, flags);
+ __add_ready(rt, new);
+ raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
+}
+
+static inline void merge_ready(rt_domain_t* rt, struct bheap* tasks)
+{
+ unsigned long flags;
+ raw_spin_lock_irqsave(&rt->ready_lock, flags);
+ __merge_ready(rt, tasks);
+ raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
+}
+
+static inline struct task_struct* take_ready(rt_domain_t* rt)
+{
+ unsigned long flags;
+ struct task_struct* ret;
+ /* first we need the write lock for rt_ready_queue */
+ raw_spin_lock_irqsave(&rt->ready_lock, flags);
+ ret = __take_ready(rt);
+ raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
+ return ret;
+}
+
+
+static inline void add_release(rt_domain_t* rt, struct task_struct *task)
+{
+ unsigned long flags;
+ raw_spin_lock_irqsave(&rt->tobe_lock, flags);
+ __add_release(rt, task);
+ raw_spin_unlock_irqrestore(&rt->tobe_lock, flags);
+}
+
+#ifdef CONFIG_RELEASE_MASTER
+void __add_release_on(rt_domain_t* rt, struct task_struct *task,
+ int target_cpu);
+
+static inline void add_release_on(rt_domain_t* rt,
+ struct task_struct *task,
+ int target_cpu)
+{
+ unsigned long flags;
+ raw_spin_lock_irqsave(&rt->tobe_lock, flags);
+ __add_release_on(rt, task, target_cpu);
+ raw_spin_unlock_irqrestore(&rt->tobe_lock, flags);
+}
+#endif
+
+static inline int __jobs_pending(rt_domain_t* rt)
+{
+ return !bheap_empty(&rt->ready_queue);
+}
+
+static inline int jobs_pending(rt_domain_t* rt)
+{
+ unsigned long flags;
+ int ret;
+ /* first we need the write lock for rt_ready_queue */
+ raw_spin_lock_irqsave(&rt->ready_lock, flags);
+ ret = !bheap_empty(&rt->ready_queue);
+ raw_spin_unlock_irqrestore(&rt->ready_lock, flags);
+ return ret;
+}
+
+#endif
diff --git a/include/litmus/rt_param.h b/include/litmus/rt_param.h
new file mode 100644
index 0000000..a7a183f
--- /dev/null
+++ b/include/litmus/rt_param.h
@@ -0,0 +1,196 @@
+/*
+ * Definition of the scheduler plugin interface.
+ *
+ */
+#ifndef _LINUX_RT_PARAM_H_
+#define _LINUX_RT_PARAM_H_
+
+/* Litmus time type. */
+typedef unsigned long long lt_t;
+
+static inline int lt_after(lt_t a, lt_t b)
+{
+ return ((long long) b) - ((long long) a) < 0;
+}
+#define lt_before(a, b) lt_after(b, a)
+
+static inline int lt_after_eq(lt_t a, lt_t b)
+{
+ return ((long long) a) - ((long long) b) >= 0;
+}
+#define lt_before_eq(a, b) lt_after_eq(b, a)
+
+/* different types of clients */
+typedef enum {
+ RT_CLASS_HARD,
+ RT_CLASS_SOFT,
+ RT_CLASS_BEST_EFFORT
+} task_class_t;
+
+typedef enum {
+ NO_ENFORCEMENT, /* job may overrun unhindered */
+ QUANTUM_ENFORCEMENT, /* budgets are only checked on quantum boundaries */
+ PRECISE_ENFORCEMENT /* NOT IMPLEMENTED - enforced with hrtimers */
+} budget_policy_t;
+
+struct rt_task {
+ lt_t exec_cost;
+ lt_t period;
+ lt_t phase;
+ unsigned int cpu;
+ task_class_t cls;
+ budget_policy_t budget_policy; /* ignored by pfair */
+};
+
+/* The definition of the data that is shared between the kernel and real-time
+ * tasks via a shared page (see litmus/ctrldev.c).
+ *
+ * WARNING: User space can write to this, so don't trust
+ * the correctness of the fields!
+ *
+ * This servees two purposes: to enable efficient signaling
+ * of non-preemptive sections (user->kernel) and
+ * delayed preemptions (kernel->user), and to export
+ * some real-time relevant statistics such as preemption and
+ * migration data to user space. We can't use a device to export
+ * statistics because we want to avoid system call overhead when
+ * determining preemption/migration overheads).
+ */
+struct control_page {
+ /* Is the task currently in a non-preemptive section? */
+ int np_flag;
+ /* Should the task call into the kernel when it leaves
+ * its non-preemptive section? */
+ int delayed_preemption;
+
+ /* to be extended */
+};
+
+/* don't export internal data structures to user space (liblitmus) */
+#ifdef __KERNEL__
+
+struct _rt_domain;
+struct bheap_node;
+struct release_heap;
+
+struct rt_job {
+ /* Time instant the the job was or will be released. */
+ lt_t release;
+ /* What is the current deadline? */
+ lt_t deadline;
+
+ /* How much service has this job received so far? */
+ lt_t exec_time;
+
+ /* Which job is this. This is used to let user space
+ * specify which job to wait for, which is important if jobs
+ * overrun. If we just call sys_sleep_next_period() then we
+ * will unintentionally miss jobs after an overrun.
+ *
+ * Increase this sequence number when a job is released.
+ */
+ unsigned int job_no;
+};
+
+struct pfair_param;
+
+/* RT task parameters for scheduling extensions
+ * These parameters are inherited during clone and therefore must
+ * be explicitly set up before the task set is launched.
+ */
+struct rt_param {
+ /* is the task sleeping? */
+ unsigned int flags:8;
+
+ /* do we need to check for srp blocking? */
+ unsigned int srp_non_recurse:1;
+
+ /* is the task present? (true if it can be scheduled) */
+ unsigned int present:1;
+
+ /* user controlled parameters */
+ struct rt_task task_params;
+
+ /* timing parameters */
+ struct rt_job job_params;
+
+ /* task representing the current "inherited" task
+ * priority, assigned by inherit_priority and
+ * return priority in the scheduler plugins.
+ * could point to self if PI does not result in
+ * an increased task priority.
+ */
+ struct task_struct* inh_task;
+
+#ifdef CONFIG_NP_SECTION
+ /* For the FMLP under PSN-EDF, it is required to make the task
+ * non-preemptive from kernel space. In order not to interfere with
+ * user space, this counter indicates the kernel space np setting.
+ * kernel_np > 0 => task is non-preemptive
+ */
+ unsigned int kernel_np;
+#endif
+
+ /* This field can be used by plugins to store where the task
+ * is currently scheduled. It is the responsibility of the
+ * plugin to avoid race conditions.
+ *
+ * This used by GSN-EDF and PFAIR.
+ */
+ volatile int scheduled_on;
+
+ /* Is the stack of the task currently in use? This is updated by
+ * the LITMUS core.
+ *
+ * Be careful to avoid deadlocks!
+ */
+ volatile int stack_in_use;
+
+ /* This field can be used by plugins to store where the task
+ * is currently linked. It is the responsibility of the plugin
+ * to avoid race conditions.
+ *
+ * Used by GSN-EDF.
+ */
+ volatile int linked_on;
+
+ /* PFAIR/PD^2 state. Allocated on demand. */
+ struct pfair_param* pfair;
+
+ /* Fields saved before BE->RT transition.
+ */
+ int old_policy;
+ int old_prio;
+
+ /* ready queue for this task */
+ struct _rt_domain* domain;
+
+ /* heap element for this task
+ *
+ * Warning: Don't statically allocate this node. The heap
+ * implementation swaps these between tasks, thus after
+ * dequeuing from a heap you may end up with a different node
+ * then the one you had when enqueuing the task. For the same
+ * reason, don't obtain and store references to this node
+ * other than this pointer (which is updated by the heap
+ * implementation).
+ */
+ struct bheap_node* heap_node;
+ struct release_heap* rel_heap;
+
+ /* Used by rt_domain to queue task in release list.
+ */
+ struct list_head list;
+
+ /* Pointer to the page shared between userspace and kernel. */
+ struct control_page * ctrl_page;
+};
+
+/* Possible RT flags */
+#define RT_F_RUNNING 0x00000000
+#define RT_F_SLEEP 0x00000001
+#define RT_F_EXIT_SEM 0x00000008
+
+#endif
+
+#endif
diff --git a/include/litmus/sched_plugin.h b/include/litmus/sched_plugin.h
new file mode 100644
index 0000000..2d856d5
--- /dev/null
+++ b/include/litmus/sched_plugin.h
@@ -0,0 +1,159 @@
+/*
+ * Definition of the scheduler plugin interface.
+ *
+ */
+#ifndef _LINUX_SCHED_PLUGIN_H_
+#define _LINUX_SCHED_PLUGIN_H_
+
+#include <linux/sched.h>
+
+/* struct for semaphore with priority inheritance */
+struct pi_semaphore {
+ atomic_t count;
+ int sleepers;
+ wait_queue_head_t wait;
+ struct {
+ /* highest-prio holder/waiter */
+ struct task_struct *task;
+ struct task_struct* cpu_task[NR_CPUS];
+ } hp;
+ /* current lock holder */
+ struct task_struct *holder;
+};
+
+/************************ setup/tear down ********************/
+
+typedef long (*activate_plugin_t) (void);
+typedef long (*deactivate_plugin_t) (void);
+
+
+
+/********************* scheduler invocation ******************/
+
+/* Plugin-specific realtime tick handler */
+typedef void (*scheduler_tick_t) (struct task_struct *cur);
+/* Novell make sched decision function */
+typedef struct task_struct* (*schedule_t)(struct task_struct * prev);
+/* Clean up after the task switch has occured.
+ * This function is called after every (even non-rt) task switch.
+ */
+typedef void (*finish_switch_t)(struct task_struct *prev);
+
+
+/********************* task state changes ********************/
+
+/* Called to setup a new real-time task.
+ * Release the first job, enqueue, etc.
+ * Task may already be running.
+ */
+typedef void (*task_new_t) (struct task_struct *task,
+ int on_rq,
+ int running);
+
+/* Called to re-introduce a task after blocking.
+ * Can potentially be called multiple times.
+ */
+typedef void (*task_wake_up_t) (struct task_struct *task);
+/* called to notify the plugin of a blocking real-time task
+ * it will only be called for real-time tasks and before schedule is called */
+typedef void (*task_block_t) (struct task_struct *task);
+/* Called when a real-time task exits or changes to a different scheduling
+ * class.
+ * Free any allocated resources
+ */
+typedef void (*task_exit_t) (struct task_struct *);
+
+/* Called when the new_owner is released from the wait queue
+ * it should now inherit the priority from sem, _before_ it gets readded
+ * to any queue
+ */
+typedef long (*inherit_priority_t) (struct pi_semaphore *sem,
+ struct task_struct *new_owner);
+
+/* Called when the current task releases a semahpore where it might have
+ * inherited a piority from
+ */
+typedef long (*return_priority_t) (struct pi_semaphore *sem);
+
+/* Called when a task tries to acquire a semaphore and fails. Check if its
+ * priority is higher than that of the current holder.
+ */
+typedef long (*pi_block_t) (struct pi_semaphore *sem, struct task_struct *t);
+
+
+
+
+/********************* sys call backends ********************/
+/* This function causes the caller to sleep until the next release */
+typedef long (*complete_job_t) (void);
+
+typedef long (*admit_task_t)(struct task_struct* tsk);
+
+typedef void (*release_at_t)(struct task_struct *t, lt_t start);
+
+struct sched_plugin {
+ struct list_head list;
+ /* basic info */
+ char *plugin_name;
+
+ /* setup */
+ activate_plugin_t activate_plugin;
+ deactivate_plugin_t deactivate_plugin;
+
+#ifdef CONFIG_SRP
+ unsigned int srp_active;
+#endif
+
+ /* scheduler invocation */
+ scheduler_tick_t tick;
+ schedule_t schedule;
+ finish_switch_t finish_switch;
+
+ /* syscall backend */
+ complete_job_t complete_job;
+ release_at_t release_at;
+
+ /* task state changes */
+ admit_task_t admit_task;
+
+ task_new_t task_new;
+ task_wake_up_t task_wake_up;
+ task_block_t task_block;
+ task_exit_t task_exit;
+
+#ifdef CONFIG_FMLP
+ /* priority inheritance */
+ unsigned int fmlp_active;
+ inherit_priority_t inherit_priority;
+ return_priority_t return_priority;
+ pi_block_t pi_block;
+#endif
+} __attribute__ ((__aligned__(SMP_CACHE_BYTES)));
+
+
+extern struct sched_plugin *litmus;
+
+int register_sched_plugin(struct sched_plugin* plugin);
+struct sched_plugin* find_sched_plugin(const char* name);
+int print_sched_plugins(char* buf, int max);
+
+static inline int srp_active(void)
+{
+#ifdef CONFIG_SRP
+ return litmus->srp_active;
+#else
+ return 0;
+#endif
+}
+static inline int fmlp_active(void)
+{
+#ifdef CONFIG_FMLP
+ return litmus->fmlp_active;
+#else
+ return 0;
+#endif
+}
+
+extern struct sched_plugin linux_sched_plugin;
+
+#endif
diff --git a/include/litmus/sched_trace.h b/include/litmus/sched_trace.h
new file mode 100644
index 0000000..a5f7373
--- /dev/null
+++ b/include/litmus/sched_trace.h
@@ -0,0 +1,183 @@
+/*
+ * sched_trace.h -- record scheduler events to a byte stream for offline analysis.
+ */
+#ifndef _LINUX_SCHED_TRACE_H_
+#define _LINUX_SCHED_TRACE_H_
+
+/* all times in nanoseconds */
+
+struct st_trace_header {
+ u8 type; /* Of what type is this record? */
+ u8 cpu; /* On which CPU was it recorded? */
+ u16 pid; /* PID of the task. */
+ u32 job; /* The job sequence number. */
+};
+
+#define ST_NAME_LEN 16
+struct st_name_data {
+ char cmd[ST_NAME_LEN];/* The name of the executable of this process. */
+};
+
+struct st_param_data { /* regular params */
+ u32 wcet;
+ u32 period;
+ u32 phase;
+ u8 partition;
+ u8 __unused[3];
+};
+
+struct st_release_data { /* A job is was/is going to be released. */
+ u64 release; /* What's the release time? */
+ u64 deadline; /* By when must it finish? */
+};
+
+struct st_assigned_data { /* A job was asigned to a CPU. */
+ u64 when;
+ u8 target; /* Where should it execute? */
+ u8 __unused[3];
+};
+
+struct st_switch_to_data { /* A process was switched to on a given CPU. */
+ u64 when; /* When did this occur? */
+ u32 exec_time; /* Time the current job has executed. */
+
+};
+
+struct st_switch_away_data { /* A process was switched away from on a given CPU. */
+ u64 when;
+ u64 exec_time;
+};
+
+struct st_completion_data { /* A job completed. */
+ u64 when;
+ u8 forced:1; /* Set to 1 if job overran and kernel advanced to the
+ * next task automatically; set to 0 otherwise.
+ */
+ u8 __uflags:7;
+ u8 __unused[3];
+};
+
+struct st_block_data { /* A task blocks. */
+ u64 when;
+ u64 __unused;
+};
+
+struct st_resume_data { /* A task resumes. */
+ u64 when;
+ u64 __unused;
+};
+
+struct st_sys_release_data {
+ u64 when;
+ u64 release;
+};
+
+#define DATA(x) struct st_ ## x ## _data x;
+
+typedef enum {
+ ST_NAME = 1, /* Start at one, so that we can spot
+ * uninitialized records. */
+ ST_PARAM,
+ ST_RELEASE,
+ ST_ASSIGNED,
+ ST_SWITCH_TO,
+ ST_SWITCH_AWAY,
+ ST_COMPLETION,
+ ST_BLOCK,
+ ST_RESUME,
+ ST_SYS_RELEASE,
+} st_event_record_type_t;
+
+struct st_event_record {
+ struct st_trace_header hdr;
+ union {
+ u64 raw[2];
+
+ DATA(name);
+ DATA(param);
+ DATA(release);
+ DATA(assigned);
+ DATA(switch_to);
+ DATA(switch_away);
+ DATA(completion);
+ DATA(block);
+ DATA(resume);
+ DATA(sys_release);
+
+ } data;
+};
+
+#undef DATA
+
+#ifdef __KERNEL__
+
+#include <linux/sched.h>
+#include <litmus/feather_trace.h>
+
+#ifdef CONFIG_SCHED_TASK_TRACE
+
+#define SCHED_TRACE(id, callback, task) \
+ ft_event1(id, callback, task)
+#define SCHED_TRACE2(id, callback, task, xtra) \
+ ft_event2(id, callback, task, xtra)
+
+/* provide prototypes; needed on sparc64 */
+#ifndef NO_TASK_TRACE_DECLS
+feather_callback void do_sched_trace_task_name(unsigned long id,
+ struct task_struct* task);
+feather_callback void do_sched_trace_task_param(unsigned long id,
+ struct task_struct* task);
+feather_callback void do_sched_trace_task_release(unsigned long id,
+ struct task_struct* task);
+feather_callback void do_sched_trace_task_switch_to(unsigned long id,
+ struct task_struct* task);
+feather_callback void do_sched_trace_task_switch_away(unsigned long id,
+ struct task_struct* task);
+feather_callback void do_sched_trace_task_completion(unsigned long id,
+ struct task_struct* task,
+ unsigned long forced);
+feather_callback void do_sched_trace_task_block(unsigned long id,
+ struct task_struct* task);
+feather_callback void do_sched_trace_task_resume(unsigned long id,
+ struct task_struct* task);
+feather_callback void do_sched_trace_sys_release(unsigned long id,
+ lt_t* start);
+#endif
+
+#else
+
+#define SCHED_TRACE(id, callback, task) /* no tracing */
+#define SCHED_TRACE2(id, callback, task, xtra) /* no tracing */
+
+#endif
+
+
+#define SCHED_TRACE_BASE_ID 500
+
+
+#define sched_trace_task_name(t) \
+ SCHED_TRACE(SCHED_TRACE_BASE_ID + 1, do_sched_trace_task_name, t)
+#define sched_trace_task_param(t) \
+ SCHED_TRACE(SCHED_TRACE_BASE_ID + 2, do_sched_trace_task_param, t)
+#define sched_trace_task_release(t) \
+ SCHED_TRACE(SCHED_TRACE_BASE_ID + 3, do_sched_trace_task_release, t)
+#define sched_trace_task_switch_to(t) \
+ SCHED_TRACE(SCHED_TRACE_BASE_ID + 4, do_sched_trace_task_switch_to, t)
+#define sched_trace_task_switch_away(t) \
+ SCHED_TRACE(SCHED_TRACE_BASE_ID + 5, do_sched_trace_task_switch_away, t)
+#define sched_trace_task_completion(t, forced) \
+ SCHED_TRACE2(SCHED_TRACE_BASE_ID + 6, do_sched_trace_task_completion, t, \
+ (unsigned long) forced)
+#define sched_trace_task_block(t) \
+ SCHED_TRACE(SCHED_TRACE_BASE_ID + 7, do_sched_trace_task_block, t)
+#define sched_trace_task_resume(t) \
+ SCHED_TRACE(SCHED_TRACE_BASE_ID + 8, do_sched_trace_task_resume, t)
+/* when is a pointer, it does not need an explicit cast to unsigned long */
+#define sched_trace_sys_release(when) \
+ SCHED_TRACE(SCHED_TRACE_BASE_ID + 9, do_sched_trace_sys_release, when)
+
+#define sched_trace_quantum_boundary() /* NOT IMPLEMENTED */
+
+#endif /* __KERNEL__ */
+
+#endif
diff --git a/include/litmus/trace.h b/include/litmus/trace.h
new file mode 100644
index 0000000..b32c711
--- /dev/null
+++ b/include/litmus/trace.h
@@ -0,0 +1,113 @@
+#ifndef _SYS_TRACE_H_
+#define _SYS_TRACE_H_
+
+#ifdef CONFIG_SCHED_OVERHEAD_TRACE
+
+#include <litmus/feather_trace.h>
+#include <litmus/feather_buffer.h>
+
+
+/*********************** TIMESTAMPS ************************/
+
+enum task_type_marker {
+ TSK_BE,
+ TSK_RT,
+ TSK_UNKNOWN
+};
+
+struct timestamp {
+ uint64_t timestamp;
+ uint32_t seq_no;
+ uint8_t cpu;
+ uint8_t event;
+ uint8_t task_type;
+};
+
+/* tracing callbacks */
+feather_callback void save_timestamp(unsigned long event);
+feather_callback void save_timestamp_def(unsigned long event, unsigned long type);
+feather_callback void save_timestamp_task(unsigned long event, unsigned long t_ptr);
+feather_callback void save_timestamp_cpu(unsigned long event, unsigned long cpu);
+
+
+#define TIMESTAMP(id) ft_event0(id, save_timestamp)
+
+#define DTIMESTAMP(id, def) ft_event1(id, save_timestamp_def, (unsigned long) def)
+
+#define TTIMESTAMP(id, task) \
+ ft_event1(id, save_timestamp_task, (unsigned long) task)
+
+#define CTIMESTAMP(id, cpu) \
+ ft_event1(id, save_timestamp_cpu, (unsigned long) cpu)
+
+#else /* !CONFIG_SCHED_OVERHEAD_TRACE */
+
+#define TIMESTAMP(id) /* no tracing */
+
+#define DTIMESTAMP(id, def) /* no tracing */
+
+#define TTIMESTAMP(id, task) /* no tracing */
+
+#define CTIMESTAMP(id, cpu) /* no tracing */
+
+#endif
+
+
+/* Convention for timestamps
+ * =========================
+ *
+ * In order to process the trace files with a common tool, we use the following
+ * convention to measure execution times: The end time id of a code segment is
+ * always the next number after the start time event id.
+ */
+
+#define TS_SCHED_START DTIMESTAMP(100, TSK_UNKNOWN) /* we only
+ * care
+ * about
+ * next */
+#define TS_SCHED_END(t) TTIMESTAMP(101, t)
+#define TS_SCHED2_START(t) TTIMESTAMP(102, t)
+#define TS_SCHED2_END(t) TTIMESTAMP(103, t)
+
+#define TS_CXS_START(t) TTIMESTAMP(104, t)
+#define TS_CXS_END(t) TTIMESTAMP(105, t)
+
+#define TS_RELEASE_START DTIMESTAMP(106, TSK_RT)
+#define TS_RELEASE_END DTIMESTAMP(107, TSK_RT)
+
+#define TS_TICK_START(t) TTIMESTAMP(110, t)
+#define TS_TICK_END(t) TTIMESTAMP(111, t)
+
+
+#define TS_PLUGIN_SCHED_START /* TIMESTAMP(120) */ /* currently unused */
+#define TS_PLUGIN_SCHED_END /* TIMESTAMP(121) */
+
+#define TS_PLUGIN_TICK_START /* TIMESTAMP(130) */
+#define TS_PLUGIN_TICK_END /* TIMESTAMP(131) */
+
+#define TS_ENTER_NP_START TIMESTAMP(140)
+#define TS_ENTER_NP_END TIMESTAMP(141)
+
+#define TS_EXIT_NP_START TIMESTAMP(150)
+#define TS_EXIT_NP_END TIMESTAMP(151)
+
+#define TS_SRP_UP_START TIMESTAMP(160)
+#define TS_SRP_UP_END TIMESTAMP(161)
+#define TS_SRP_DOWN_START TIMESTAMP(162)
+#define TS_SRP_DOWN_END TIMESTAMP(163)
+
+#define TS_PI_UP_START TIMESTAMP(170)
+#define TS_PI_UP_END TIMESTAMP(171)
+#define TS_PI_DOWN_START TIMESTAMP(172)
+#define TS_PI_DOWN_END TIMESTAMP(173)
+
+#define TS_FIFO_UP_START TIMESTAMP(180)
+#define TS_FIFO_UP_END TIMESTAMP(181)
+#define TS_FIFO_DOWN_START TIMESTAMP(182)
+#define TS_FIFO_DOWN_END TIMESTAMP(183)
+
+#define TS_SEND_RESCHED_START(c) CTIMESTAMP(190, c)
+#define TS_SEND_RESCHED_END DTIMESTAMP(191, TSK_UNKNOWN)
+
+
+#endif /* !_SYS_TRACE_H_ */
diff --git a/include/litmus/unistd_32.h b/include/litmus/unistd_32.h
new file mode 100644
index 0000000..dbddc65
--- /dev/null
+++ b/include/litmus/unistd_32.h
@@ -0,0 +1,23 @@
+/*
+ * included from arch/x86/include/asm/unistd_32.h
+ *
+ * LITMUS^RT syscalls with "relative" numbers
+ */
+#define __LSC(x) (__NR_LITMUS + x)
+
+#define __NR_set_rt_task_param __LSC(0)
+#define __NR_get_rt_task_param __LSC(1)
+#define __NR_complete_job __LSC(2)
+#define __NR_od_open __LSC(3)
+#define __NR_od_close __LSC(4)
+#define __NR_fmlp_down __LSC(5)
+#define __NR_fmlp_up __LSC(6)
+#define __NR_srp_down __LSC(7)
+#define __NR_srp_up __LSC(8)
+#define __NR_query_job_no __LSC(9)
+#define __NR_wait_for_job_release __LSC(10)
+#define __NR_wait_for_ts_release __LSC(11)
+#define __NR_release_ts __LSC(12)
+#define __NR_null_call __LSC(13)
+
+#define NR_litmus_syscalls 14
diff --git a/include/litmus/unistd_64.h b/include/litmus/unistd_64.h
new file mode 100644
index 0000000..f0618e7
--- /dev/null
+++ b/include/litmus/unistd_64.h
@@ -0,0 +1,37 @@
+/*
+ * included from arch/x86/include/asm/unistd_64.h
+ *
+ * LITMUS^RT syscalls with "relative" numbers
+ */
+#define __LSC(x) (__NR_LITMUS + x)
+
+#define __NR_set_rt_task_param __LSC(0)
+__SYSCALL(__NR_set_rt_task_param, sys_set_rt_task_param)
+#define __NR_get_rt_task_param __LSC(1)
+__SYSCALL(__NR_get_rt_task_param, sys_get_rt_task_param)
+#define __NR_complete_job __LSC(2)
+__SYSCALL(__NR_complete_job, sys_complete_job)
+#define __NR_od_open __LSC(3)
+__SYSCALL(__NR_od_open, sys_od_open)
+#define __NR_od_close __LSC(4)
+__SYSCALL(__NR_od_close, sys_od_close)
+#define __NR_fmlp_down __LSC(5)
+__SYSCALL(__NR_fmlp_down, sys_fmlp_down)
+#define __NR_fmlp_up __LSC(6)
+__SYSCALL(__NR_fmlp_up, sys_fmlp_up)
+#define __NR_srp_down __LSC(7)
+__SYSCALL(__NR_srp_down, sys_srp_down)
+#define __NR_srp_up __LSC(8)
+__SYSCALL(__NR_srp_up, sys_srp_up)
+#define __NR_query_job_no __LSC(9)
+__SYSCALL(__NR_query_job_no, sys_query_job_no)
+#define __NR_wait_for_job_release __LSC(10)
+__SYSCALL(__NR_wait_for_job_release, sys_wait_for_job_release)
+#define __NR_wait_for_ts_release __LSC(11)
+__SYSCALL(__NR_wait_for_ts_release, sys_wait_for_ts_release)
+#define __NR_release_ts __LSC(12)
+__SYSCALL(__NR_release_ts, sys_release_ts)
+#define __NR_null_call __LSC(13)
+__SYSCALL(__NR_null_call, sys_null_call)
+
+#define NR_litmus_syscalls 14
diff --git a/kernel/exit.c b/kernel/exit.c
index 0312022..b9d3bc6 100644
--- a/kernel/exit.c
+++ b/kernel/exit.c
@@ -56,6 +56,8 @@
#include <asm/pgtable.h>
#include <asm/mmu_context.h>
+extern void exit_od_table(struct task_struct *t);
+
static void exit_mm(struct task_struct * tsk);
static void __unhash_process(struct task_struct *p, bool group_dead)
@@ -960,6 +962,8 @@ NORET_TYPE void do_exit(long code)
if (unlikely(tsk->audit_context))
audit_free(tsk);
+ exit_od_table(tsk);
+
tsk->exit_code = code;
taskstats_exit(tsk, group_dead);
diff --git a/kernel/fork.c b/kernel/fork.c
index c445f8c..ab7f29d 100644
--- a/kernel/fork.c
+++ b/kernel/fork.c
@@ -75,6 +75,9 @@
#include <trace/events/sched.h>
+#include <litmus/litmus.h>
+#include <litmus/sched_plugin.h>
+
/*
* Protected counters by write_lock_irq(&tasklist_lock)
*/
@@ -183,6 +186,7 @@ void __put_task_struct(struct task_struct *tsk)
WARN_ON(atomic_read(&tsk->usage));
WARN_ON(tsk == current);
+ exit_litmus(tsk);
exit_creds(tsk);
delayacct_tsk_free(tsk);
put_signal_struct(tsk->signal);
@@ -266,6 +270,9 @@ static struct task_struct *dup_task_struct(struct task_struct *orig)
tsk->stack = ti;
+ /* Don't let the new task be a real-time task. */
+ litmus_fork(tsk);
+
err = prop_local_init_single(&tsk->dirties);
if (err)
goto out;
diff --git a/kernel/hrtimer.c b/kernel/hrtimer.c
index 72206cf..cb49883 100644
--- a/kernel/hrtimer.c
+++ b/kernel/hrtimer.c
@@ -46,6 +46,8 @@
#include <linux/sched.h>
#include <linux/timer.h>
+#include <litmus/litmus.h>
+
#include <asm/uaccess.h>
#include <trace/events/timer.h>
@@ -1042,6 +1044,98 @@ hrtimer_start(struct hrtimer *timer, ktime_t tim, const enum hrtimer_mode mode)
}
EXPORT_SYMBOL_GPL(hrtimer_start);
+#ifdef CONFIG_ARCH_HAS_SEND_PULL_TIMERS
+
+/**
+ * hrtimer_start_on_info_init - Initialize hrtimer_start_on_info
+ */
+void hrtimer_start_on_info_init(struct hrtimer_start_on_info *info)
+{
+ memset(info, 0, sizeof(struct hrtimer_start_on_info));
+ atomic_set(&info->state, HRTIMER_START_ON_INACTIVE);
+}
+
+/**
+ * hrtimer_pull - PULL_TIMERS_VECTOR callback on remote cpu
+ */
+void hrtimer_pull(void)
+{
+ struct hrtimer_cpu_base *base = &__get_cpu_var(hrtimer_bases);
+ struct hrtimer_start_on_info *info;
+ struct list_head *pos, *safe, list;
+
+ raw_spin_lock(&base->lock);
+ list_replace_init(&base->to_pull, &list);
+ raw_spin_unlock(&base->lock);
+
+ list_for_each_safe(pos, safe, &list) {
+ info = list_entry(pos, struct hrtimer_start_on_info, list);
+ TRACE("pulled timer 0x%x\n", info->timer);
+ list_del(pos);
+ hrtimer_start(info->timer, info->time, info->mode);
+ }
+}
+
+/**
+ * hrtimer_start_on - trigger timer arming on remote cpu
+ * @cpu: remote cpu
+ * @info: save timer information for enqueuing on remote cpu
+ * @timer: timer to be pulled
+ * @time: expire time
+ * @mode: timer mode
+ */
+int hrtimer_start_on(int cpu, struct hrtimer_start_on_info* info,
+ struct hrtimer *timer, ktime_t time,
+ const enum hrtimer_mode mode)
+{
+ unsigned long flags;
+ struct hrtimer_cpu_base* base;
+ int in_use = 0, was_empty;
+
+ /* serialize access to info through the timer base */
+ lock_hrtimer_base(timer, &flags);
+
+ in_use = (atomic_read(&info->state) != HRTIMER_START_ON_INACTIVE);
+ if (!in_use) {
+ INIT_LIST_HEAD(&info->list);
+ info->timer = timer;
+ info->time = time;
+ info->mode = mode;
+ /* mark as in use */
+ atomic_set(&info->state, HRTIMER_START_ON_QUEUED);
+ }
+
+ unlock_hrtimer_base(timer, &flags);
+
+ if (!in_use) {
+ /* initiate pull */
+ preempt_disable();
+ if (cpu == smp_processor_id()) {
+ /* start timer locally; we may get called
+ * with rq->lock held, do not wake up anything
+ */
+ TRACE("hrtimer_start_on: starting on local CPU\n");
+ __hrtimer_start_range_ns(info->timer, info->time,
+ 0, info->mode, 0);
+ } else {
+ TRACE("hrtimer_start_on: pulling to remote CPU\n");
+ base = &per_cpu(hrtimer_bases, cpu);
+ raw_spin_lock_irqsave(&base->lock, flags);
+ was_empty = list_empty(&base->to_pull);
+ list_add(&info->list, &base->to_pull);
+ raw_spin_unlock_irqrestore(&base->lock, flags);
+ if (was_empty)
+ /* only send IPI if other no else
+ * has done so already
+ */
+ smp_send_pull_timers(cpu);
+ }
+ preempt_enable();
+ }
+ return in_use;
+}
+
+#endif
/**
* hrtimer_try_to_cancel - try to deactivate a timer
@@ -1634,6 +1728,7 @@ static void __cpuinit init_hrtimers_cpu(int cpu)
cpu_base->clock_base[i].cpu_base = cpu_base;
hrtimer_init_hres(cpu_base);
+ INIT_LIST_HEAD(&cpu_base->to_pull);
}
#ifdef CONFIG_HOTPLUG_CPU
diff --git a/kernel/printk.c b/kernel/printk.c
index 8fe465a..9dc8ea1 100644
--- a/kernel/printk.c
+++ b/kernel/printk.c
@@ -74,6 +74,13 @@ int console_printk[4] = {
};
/*
+ * divert printk() messages when there is a LITMUS^RT debug listener
+ */
+#include <litmus/litmus.h>
+int trace_override = 0;
+int trace_recurse = 0;
+
+/*
* Low level drivers may need that to know if they can schedule in
* their unblank() callback or not. So let's export it.
*/
@@ -735,6 +742,9 @@ asmlinkage int vprintk(const char *fmt, va_list args)
/* Emit the output into the temporary buffer */
printed_len += vscnprintf(printk_buf + printed_len,
sizeof(printk_buf) - printed_len, fmt, args);
+ /* if LITMUS^RT tracer is active divert printk() msgs */
+ if (trace_override && !trace_recurse)
+ TRACE("%s", printk_buf);
p = printk_buf;
@@ -804,7 +814,7 @@ asmlinkage int vprintk(const char *fmt, va_list args)
* Try to acquire and then immediately release the
* console semaphore. The release will do all the
* actual magic (print out buffers, wake up klogd,
- * etc).
+ * etc).
*
* The acquire_console_semaphore_for_printk() function
* will release 'logbuf_lock' regardless of whether it
@@ -1067,7 +1077,7 @@ int printk_needs_cpu(int cpu)
void wake_up_klogd(void)
{
- if (waitqueue_active(&log_wait))
+ if (!trace_override && waitqueue_active(&log_wait))
__raw_get_cpu_var(printk_pending) = 1;
}
diff --git a/kernel/sched.c b/kernel/sched.c
index dc85ceb..1b13c8e 100644
--- a/kernel/sched.c
+++ b/kernel/sched.c
@@ -79,6 +79,11 @@
#include "sched_cpupri.h"
#include "workqueue_sched.h"
+#include <litmus/sched_trace.h>
+#include <litmus/trace.h>
+
+static void litmus_tick(struct rq*, struct task_struct*);
+
#define CREATE_TRACE_POINTS
#include <trace/events/sched.h>
@@ -405,6 +410,12 @@ struct rt_rq {
#endif
};
+/* Litmus related fields in a runqueue */
+struct litmus_rq {
+ unsigned long nr_running;
+ struct task_struct *prev;
+};
+
#ifdef CONFIG_SMP
/*
@@ -471,6 +482,7 @@ struct rq {
struct cfs_rq cfs;
struct rt_rq rt;
+ struct litmus_rq litmus;
#ifdef CONFIG_FAIR_GROUP_SCHED
/* list of leaf cfs_rq on this cpu: */
@@ -566,8 +578,14 @@ void check_preempt_curr(struct rq *rq, struct task_struct *p, int flags)
* A queue event has occurred, and we're going to schedule. In
* this case, we can save a useless back to back clock update.
*/
+ /* LITMUS^RT: turning off the clock update is buggy in Linux 2.6.36;
+ * the scheduler can "forget" to renable the runqueue clock in some
+ * cases. LITMUS^RT amplifies the effects of this problem. Hence, we
+ * turn it off to avoid stalling clocks. */
+ /*
if (test_tsk_need_resched(p))
rq->skip_clock_update = 1;
+ */
}
static inline int cpu_of(struct rq *rq)
@@ -1042,6 +1060,7 @@ static enum hrtimer_restart hrtick(struct hrtimer *timer)
raw_spin_lock(&rq->lock);
update_rq_clock(rq);
rq->curr->sched_class->task_tick(rq, rq->curr, 1);
+ litmus_tick(rq, rq->curr);
raw_spin_unlock(&rq->lock);
return HRTIMER_NORESTART;
@@ -1840,7 +1859,7 @@ static inline void __set_task_cpu(struct task_struct *p, unsigned int cpu)
static const struct sched_class rt_sched_class;
-#define sched_class_highest (&rt_sched_class)
+#define sched_class_highest (&litmus_sched_class)
#define for_each_class(class) \
for (class = sched_class_highest; class; class = class->next)
@@ -1920,6 +1939,7 @@ static void deactivate_task(struct rq *rq, struct task_struct *p, int flags)
#include "sched_idletask.c"
#include "sched_fair.c"
#include "sched_rt.c"
+#include "../litmus/sched_litmus.c"
#ifdef CONFIG_SCHED_DEBUG
# include "sched_debug.c"
#endif
@@ -2352,6 +2372,9 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
unsigned long en_flags = ENQUEUE_WAKEUP;
struct rq *rq;
+ if (is_realtime(p))
+ TRACE_TASK(p, "try_to_wake_up() state:%d\n", p->state);
+
this_cpu = get_cpu();
smp_wmb();
@@ -2366,7 +2389,7 @@ static int try_to_wake_up(struct task_struct *p, unsigned int state,
orig_cpu = cpu;
#ifdef CONFIG_SMP
- if (unlikely(task_running(rq, p)))
+ if (unlikely(task_running(rq, p)) || is_realtime(p))
goto out_activate;
/*
@@ -2428,6 +2451,8 @@ out_activate:
out_running:
ttwu_post_activation(p, rq, wake_flags, success);
out:
+ if (is_realtime(p))
+ TRACE_TASK(p, "try_to_wake_up() done state:%d\n", p->state);
task_rq_unlock(rq, &flags);
put_cpu();
@@ -2748,6 +2773,8 @@ static void finish_task_switch(struct rq *rq, struct task_struct *prev)
*/
prev_state = prev->state;
finish_arch_switch(prev);
+ litmus->finish_switch(prev);
+ prev->rt_param.stack_in_use = NO_CPU;
#ifdef __ARCH_WANT_INTERRUPTS_ON_CTXSW
local_irq_disable();
#endif /* __ARCH_WANT_INTERRUPTS_ON_CTXSW */
@@ -2777,6 +2804,15 @@ static inline void pre_schedule(struct rq *rq, struct task_struct *prev)
{
if (prev->sched_class->pre_schedule)
prev->sched_class->pre_schedule(rq, prev);
+
+ /* LITMUS^RT not very clean hack: we need to save the prev task
+ * as our scheduling decision rely on it (as we drop the rq lock
+ * something in prev can change...); there is no way to escape
+ * this ack apart from modifying pick_nex_task(rq, _prev_) or
+ * falling back on the previous solution of decoupling
+ * scheduling decisions
+ */
+ rq->litmus.prev = prev;
}
/* rq->lock is NOT held, but preemption is disabled */
@@ -3578,18 +3614,26 @@ void scheduler_tick(void)
sched_clock_tick();
+ TS_TICK_START(current);
+
raw_spin_lock(&rq->lock);
update_rq_clock(rq);
update_cpu_load_active(rq);
curr->sched_class->task_tick(rq, curr, 0);
+
+ /* litmus_tick may force current to resched */
+ litmus_tick(rq, curr);
+
raw_spin_unlock(&rq->lock);
perf_event_task_tick(curr);
#ifdef CONFIG_SMP
rq->idle_at_tick = idle_cpu(cpu);
- trigger_load_balance(rq, cpu);
+ if (!is_realtime(current))
+ trigger_load_balance(rq, cpu);
#endif
+ TS_TICK_END(current);
}
notrace unsigned long get_parent_ip(unsigned long addr)
@@ -3716,12 +3760,20 @@ pick_next_task(struct rq *rq)
/*
* Optimization: we know that if all tasks are in
* the fair class we can call that function directly:
- */
- if (likely(rq->nr_running == rq->cfs.nr_running)) {
+
+ * NOT IN LITMUS^RT!
+
+ * This breaks many assumptions in the plugins.
+ * Do not uncomment without thinking long and hard
+ * about how this affects global plugins such as GSN-EDF.
+
+ if (rq->nr_running == rq->cfs.nr_running) {
+ TRACE("taking shortcut in pick_next_task()\n");
p = fair_sched_class.pick_next_task(rq);
if (likely(p))
return p;
}
+ */
class = sched_class_highest;
for ( ; ; ) {
@@ -3748,6 +3800,7 @@ asmlinkage void __sched schedule(void)
need_resched:
preempt_disable();
+ sched_state_entered_schedule();
cpu = smp_processor_id();
rq = cpu_rq(cpu);
rcu_note_context_switch(cpu);
@@ -3755,6 +3808,8 @@ need_resched:
release_kernel_lock(prev);
need_resched_nonpreemptible:
+ TS_SCHED_START;
+ sched_trace_task_switch_away(prev);
schedule_debug(prev);
@@ -3803,7 +3858,10 @@ need_resched_nonpreemptible:
rq->curr = next;
++*switch_count;
+ TS_SCHED_END(next);
+ TS_CXS_START(next);
context_switch(rq, prev, next); /* unlocks the rq */
+ TS_CXS_END(current);
/*
* The context switch have flipped the stack from under us
* and restored the local variables which were saved when
@@ -3812,17 +3870,24 @@ need_resched_nonpreemptible:
*/
cpu = smp_processor_id();
rq = cpu_rq(cpu);
- } else
+ } else {
+ TS_SCHED_END(prev);
raw_spin_unlock_irq(&rq->lock);
+ }
+
+ sched_trace_task_switch_to(current);
post_schedule(rq);
- if (unlikely(reacquire_kernel_lock(prev)))
+ if (sched_state_validate_switch() || unlikely(reacquire_kernel_lock(prev)))
goto need_resched_nonpreemptible;
preempt_enable_no_resched();
if (need_resched())
goto need_resched;
+
+ if (srp_active())
+ srp_ceiling_block();
}
EXPORT_SYMBOL(schedule);
@@ -4108,6 +4173,17 @@ void complete_all(struct completion *x)
}
EXPORT_SYMBOL(complete_all);
+void complete_n(struct completion *x, int n)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&x->wait.lock, flags);
+ x->done += n;
+ __wake_up_common(&x->wait, TASK_NORMAL, n, 0, NULL);
+ spin_unlock_irqrestore(&x->wait.lock, flags);
+}
+EXPORT_SYMBOL(complete_n);
+
static inline long __sched
do_wait_for_common(struct completion *x, long timeout, int state)
{
@@ -4550,7 +4626,9 @@ __setscheduler(struct rq *rq, struct task_struct *p, int policy, int prio)
p->normal_prio = normal_prio(p);
/* we are holding p->pi_lock already */
p->prio = rt_mutex_getprio(p);
- if (rt_prio(p->prio))
+ if (p->policy == SCHED_LITMUS)
+ p->sched_class = &litmus_sched_class;
+ else if (rt_prio(p->prio))
p->sched_class = &rt_sched_class;
else
p->sched_class = &fair_sched_class;
@@ -4595,7 +4673,7 @@ recheck:
if (policy != SCHED_FIFO && policy != SCHED_RR &&
policy != SCHED_NORMAL && policy != SCHED_BATCH &&
- policy != SCHED_IDLE)
+ policy != SCHED_IDLE && policy != SCHED_LITMUS)
return -EINVAL;
}
@@ -4610,6 +4688,8 @@ recheck:
return -EINVAL;
if (rt_policy(policy) != (param->sched_priority != 0))
return -EINVAL;
+ if (policy == SCHED_LITMUS && policy == p->policy)
+ return -EINVAL;
/*
* Allow unprivileged RT tasks to decrease priority:
@@ -4650,6 +4730,12 @@ recheck:
return retval;
}
+ if (policy == SCHED_LITMUS) {
+ retval = litmus_admit_task(p);
+ if (retval)
+ return retval;
+ }
+
/*
* make sure no PI-waiters arrive (or leave) while we are
* changing the priority of the task:
@@ -4692,10 +4778,19 @@ recheck:
p->sched_reset_on_fork = reset_on_fork;
+ if (p->policy == SCHED_LITMUS)
+ litmus_exit_task(p);
+
oldprio = p->prio;
prev_class = p->sched_class;
__setscheduler(rq, p, policy, param->sched_priority);
+ if (policy == SCHED_LITMUS) {
+ p->rt_param.stack_in_use = running ? rq->cpu : NO_CPU;
+ p->rt_param.present = running;
+ litmus->task_new(p, on_rq, running);
+ }
+
if (running)
p->sched_class->set_curr_task(rq);
if (on_rq) {
@@ -4865,10 +4960,11 @@ long sched_setaffinity(pid_t pid, const struct cpumask *in_mask)
rcu_read_lock();
p = find_process_by_pid(pid);
- if (!p) {
+ /* Don't set affinity if task not found and for LITMUS tasks */
+ if (!p || is_realtime(p)) {
rcu_read_unlock();
put_online_cpus();
- return -ESRCH;
+ return p ? -EPERM : -ESRCH;
}
/* Prevent p going away */
diff --git a/kernel/sched_fair.c b/kernel/sched_fair.c
index db3f674..e0e8d5c 100644
--- a/kernel/sched_fair.c
+++ b/kernel/sched_fair.c
@@ -1654,7 +1654,7 @@ static void check_preempt_wakeup(struct rq *rq, struct task_struct *p, int wake_
struct cfs_rq *cfs_rq = task_cfs_rq(curr);
int scale = cfs_rq->nr_running >= sched_nr_latency;
- if (unlikely(rt_prio(p->prio)))
+ if (unlikely(rt_prio(p->prio)) || p->policy == SCHED_LITMUS)
goto preempt;
if (unlikely(p->sched_class != &fair_sched_class))
diff --git a/kernel/sched_rt.c b/kernel/sched_rt.c
index d10c80e..e40e7fe 100644
--- a/kernel/sched_rt.c
+++ b/kernel/sched_rt.c
@@ -1013,7 +1013,7 @@ static void check_preempt_equal_prio(struct rq *rq, struct task_struct *p)
*/
static void check_preempt_curr_rt(struct rq *rq, struct task_struct *p, int flags)
{
- if (p->prio < rq->curr->prio) {
+ if (p->prio < rq->curr->prio || p->policy == SCHED_LITMUS) {
resched_task(rq->curr);
return;
}
diff --git a/kernel/time/tick-sched.c b/kernel/time/tick-sched.c
index 3e216e0..bb2d8b7 100644
--- a/kernel/time/tick-sched.c
+++ b/kernel/time/tick-sched.c
@@ -768,12 +768,53 @@ static enum hrtimer_restart tick_sched_timer(struct hrtimer *timer)
}
/**
+ * tick_set_quanta_type - get the quanta type as a boot option
+ * Default is standard setup with ticks staggered over first
+ * half of tick period.
+ */
+int quanta_type = LINUX_DEFAULT_TICKS;
+static int __init tick_set_quanta_type(char *str)
+{
+ if (strcmp("aligned", str) == 0) {
+ quanta_type = LITMUS_ALIGNED_TICKS;
+ printk(KERN_INFO "LITMUS^RT: setting aligned quanta\n");
+ }
+ else if (strcmp("staggered", str) == 0) {
+ quanta_type = LITMUS_STAGGERED_TICKS;
+ printk(KERN_INFO "LITMUS^RT: setting staggered quanta\n");
+ }
+ return 1;
+}
+__setup("quanta=", tick_set_quanta_type);
+
+u64 cpu_stagger_offset(int cpu)
+{
+ u64 offset = 0;
+ switch (quanta_type) {
+ case LITMUS_ALIGNED_TICKS:
+ offset = 0;
+ break;
+ case LITMUS_STAGGERED_TICKS:
+ offset = ktime_to_ns(tick_period);
+ do_div(offset, num_possible_cpus());
+ offset *= cpu;
+ break;
+ default:
+ offset = ktime_to_ns(tick_period) >> 1;
+ do_div(offset, num_possible_cpus());
+ offset *= cpu;
+ }
+ return offset;
+}
+
+/**
* tick_setup_sched_timer - setup the tick emulation timer
*/
void tick_setup_sched_timer(void)
{
struct tick_sched *ts = &__get_cpu_var(tick_cpu_sched);
ktime_t now = ktime_get();
+ u64 offset;
/*
* Emulate tick processing via per-CPU hrtimers:
@@ -784,6 +825,12 @@ void tick_setup_sched_timer(void)
/* Get the next period (per cpu) */
hrtimer_set_expires(&ts->sched_timer, tick_init_jiffy_update());
+ /* Offset must be set correctly to achieve desired quanta type. */
+ offset = cpu_stagger_offset(smp_processor_id());
+
+ /* Add the correct offset to expiration time */
+ hrtimer_add_expires_ns(&ts->sched_timer, offset);
+
for (;;) {
hrtimer_forward(&ts->sched_timer, now, tick_period);
hrtimer_start_expires(&ts->sched_timer,
diff --git a/litmus/Kconfig b/litmus/Kconfig
new file mode 100644
index 0000000..a2f2678
--- /dev/null
+++ b/litmus/Kconfig
@@ -0,0 +1,194 @@
+menu "LITMUS^RT"
+
+menu "Scheduling"
+
+config PLUGIN_CEDF
+ bool "Clustered-EDF"
+ depends on X86 && SYSFS
+ default y
+ help
+ Include the Clustered EDF (C-EDF) plugin in the kernel.
+ This is appropriate for large platforms with shared caches.
+ On smaller platforms (e.g., ARM PB11MPCore), using C-EDF
+ makes little sense since there aren't any shared caches.
+
+config PLUGIN_PFAIR
+ bool "PFAIR"
+ depends on HIGH_RES_TIMERS && !NO_HZ
+ default y
+ help
+ Include the PFAIR plugin (i.e., the PD^2 scheduler) in the kernel.
+ The PFAIR plugin requires high resolution timers (for staggered quanta)
+ and does not support NO_HZ (quanta could be missed when the system is idle).
+
+ If unsure, say Yes.
+
+config RELEASE_MASTER
+ bool "Release-master Support"
+ depends on ARCH_HAS_SEND_PULL_TIMERS
+ default n
+ help
+ Allow one processor to act as a dedicated interrupt processor
+ that services all timer interrupts, but that does not schedule
+ real-time tasks. See RTSS'09 paper for details
+ (http://www.cs.unc.edu/~anderson/papers.html).
+ Currently only supported by GSN-EDF.
+
+endmenu
+
+menu "Real-Time Synchronization"
+
+config NP_SECTION
+ bool "Non-preemptive section support"
+ default n
+ help
+ Allow tasks to become non-preemptable.
+ Note that plugins still need to explicitly support non-preemptivity.
+ Currently, only GSN-EDF and PSN-EDF have such support.
+
+ This is required to support the FMLP.
+ If disabled, all tasks will be considered preemptable at all times.
+
+config SRP
+ bool "Stack Resource Policy (SRP)"
+ default n
+ help
+ Include support for Baker's Stack Resource Policy.
+
+ Say Yes if you want FMLP local long critical section
+ synchronization support.
+
+config FMLP
+ bool "FMLP support"
+ depends on NP_SECTION
+ default n
+ help
+ Include support for deterministic multiprocessor real-time
+ synchronization support.
+
+ Say Yes if you want FMLP long critical section
+ synchronization support.
+
+endmenu
+
+menu "Tracing"
+
+config FEATHER_TRACE
+ bool "Feather-Trace Infrastructure"
+ default y
+ help
+ Feather-Trace basic tracing infrastructure. Includes device file
+ driver and instrumentation point support.
+
+ There are actually two implementations of Feather-Trace.
+ 1) A slower, but portable, default implementation.
+ 2) Architecture-specific implementations that rewrite kernel .text at runtime.
+
+ If enabled, Feather-Trace will be based on 2) if available (currently only for x86).
+ However, if DEBUG_RODATA=y, then Feather-Trace will choose option 1) in any case
+ to avoid problems with write-protected .text pages.
+
+ Bottom line: to avoid increased overheads, choose DEBUG_RODATA=n.
+
+ Note that this option only enables the basic Feather-Trace infrastructure;
+ you still need to enable SCHED_TASK_TRACE and/or SCHED_OVERHEAD_TRACE to
+ actually enable any events.
+
+config SCHED_TASK_TRACE
+ bool "Trace real-time tasks"
+ depends on FEATHER_TRACE
+ default y
+ help
+ Include support for the sched_trace_XXX() tracing functions. This
+ allows the collection of real-time task events such as job
+ completions, job releases, early completions, etc. This results in a
+ small overhead in the scheduling code. Disable if the overhead is not
+ acceptable (e.g., benchmarking).
+
+ Say Yes for debugging.
+ Say No for overhead tracing.
+
+config SCHED_TASK_TRACE_SHIFT
+ int "Buffer size for sched_trace_xxx() events"
+ depends on SCHED_TASK_TRACE
+ range 8 13
+ default 9
+ help
+
+ Select the buffer size of sched_trace_xxx() events as a power of two.
+ These buffers are statically allocated as per-CPU data. Each event
+ requires 24 bytes storage plus one additional flag byte. Too large
+ buffers can cause issues with the per-cpu allocator (and waste
+ memory). Too small buffers can cause scheduling events to be lost. The
+ "right" size is workload dependent and depends on the number of tasks,
+ each task's period, each task's number of suspensions, and how often
+ the buffer is flushed.
+
+ Examples: 12 => 4k events
+ 10 => 1k events
+ 8 => 512 events
+
+config SCHED_OVERHEAD_TRACE
+ bool "Record timestamps for overhead measurements"
+ depends on FEATHER_TRACE
+ default n
+ help
+ Export event stream for overhead tracing.
+ Say Yes for overhead tracing.
+
+config SCHED_DEBUG_TRACE
+ bool "TRACE() debugging"
+ default y
+ help
+ Include support for sched_trace_log_messageg(), which is used to
+ implement TRACE(). If disabled, no TRACE() messages will be included
+ in the kernel, and no overheads due to debugging statements will be
+ incurred by the scheduler. Disable if the overhead is not acceptable
+ (e.g. benchmarking).
+
+ Say Yes for debugging.
+ Say No for overhead tracing.
+
+config SCHED_DEBUG_TRACE_SHIFT
+ int "Buffer size for TRACE() buffer"
+ depends on SCHED_DEBUG_TRACE
+ range 14 22
+ default 18
+ help
+
+ Select the amount of memory needed per for the TRACE() buffer, as a
+ power of two. The TRACE() buffer is global and statically allocated. If
+ the buffer is too small, there will be holes in the TRACE() log if the
+ buffer-flushing task is starved.
+
+ The default should be sufficient for most systems. Increase the buffer
+ size if the log contains holes. Reduce the buffer size when running on
+ a memory-constrained system.
+
+ Examples: 14 => 16KB
+ 18 => 256KB
+ 20 => 1MB
+
+ This buffer is exported to usespace using a misc device as
+ 'litmus/log'. On a system with default udev rules, a corresponding
+ character device node should be created at /dev/litmus/log. The buffer
+ can be flushed using cat, e.g., 'cat /dev/litmus/log > my_log_file.txt'.
+
+config SCHED_DEBUG_TRACE_CALLER
+ bool "Include [function@file:line] tag in TRACE() log"
+ depends on SCHED_DEBUG_TRACE
+ default n
+ help
+ With this option enabled, TRACE() prepends
+
+ "[<function name>@<filename>:<line number>]"
+
+ to each message in the debug log. Enable this to aid in figuring out
+ what was called in which order. The downside is that it adds a lot of
+ clutter.
+
+ If unsure, say No.
+
+endmenu
+
+endmenu
diff --git a/litmus/Makefile b/litmus/Makefile
new file mode 100644
index 0000000..b7366b5
--- /dev/null
+++ b/litmus/Makefile
@@ -0,0 +1,27 @@
+#
+# Makefile for LITMUS^RT
+#
+
+obj-y = sched_plugin.o litmus.o \
+ preempt.o \
+ litmus_proc.o \
+ budget.o \
+ jobs.o \
+ sync.o \
+ rt_domain.o \
+ edf_common.o \
+ fdso.o \
+ srp.o \
+ fmlp.o \
+ bheap.o \
+ ctrldev.o \
+ sched_gsn_edf.o \
+ sched_psn_edf.o
+
+obj-$(CONFIG_PLUGIN_CEDF) += sched_cedf.o
+obj-$(CONFIG_PLUGIN_PFAIR) += sched_pfair.o
+
+obj-$(CONFIG_FEATHER_TRACE) += ft_event.o ftdev.o
+obj-$(CONFIG_SCHED_TASK_TRACE) += sched_task_trace.o
+obj-$(CONFIG_SCHED_DEBUG_TRACE) += sched_trace.o
+obj-$(CONFIG_SCHED_OVERHEAD_TRACE) += trace.o
diff --git a/litmus/bheap.c b/litmus/bheap.c
new file mode 100644
index 0000000..528af97
--- /dev/null
+++ b/litmus/bheap.c
@@ -0,0 +1,314 @@
+#include "linux/kernel.h"
+#include "litmus/bheap.h"
+
+void bheap_init(struct bheap* heap)
+{
+ heap->head = NULL;
+ heap->min = NULL;
+}
+
+void bheap_node_init(struct bheap_node** _h, void* value)
+{
+ struct bheap_node* h = *_h;
+ h->parent = NULL;
+ h->next = NULL;
+ h->child = NULL;
+ h->degree = NOT_IN_HEAP;
+ h->value = value;
+ h->ref = _h;
+}
+
+
+/* make child a subtree of root */
+static void __bheap_link(struct bheap_node* root,
+ struct bheap_node* child)
+{
+ child->parent = root;
+ child->next = root->child;
+ root->child = child;
+ root->degree++;
+}
+
+/* merge root lists */
+static struct bheap_node* __bheap_merge(struct bheap_node* a,
+ struct bheap_node* b)
+{
+ struct bheap_node* head = NULL;
+ struct bheap_node** pos = &head;
+
+ while (a && b) {
+ if (a->degree < b->degree) {
+ *pos = a;
+ a = a->next;
+ } else {
+ *pos = b;
+ b = b->next;
+ }
+ pos = &(*pos)->next;
+ }
+ if (a)
+ *pos = a;
+ else
+ *pos = b;
+ return head;
+}
+
+/* reverse a linked list of nodes. also clears parent pointer */
+static struct bheap_node* __bheap_reverse(struct bheap_node* h)
+{
+ struct bheap_node* tail = NULL;
+ struct bheap_node* next;
+
+ if (!h)
+ return h;
+
+ h->parent = NULL;
+ while (h->next) {
+ next = h->next;
+ h->next = tail;
+ tail = h;
+ h = next;
+ h->parent = NULL;
+ }
+ h->next = tail;
+ return h;
+}
+
+static void __bheap_min(bheap_prio_t higher_prio, struct bheap* heap,
+ struct bheap_node** prev, struct bheap_node** node)
+{
+ struct bheap_node *_prev, *cur;
+ *prev = NULL;
+
+ if (!heap->head) {
+ *node = NULL;
+ return;
+ }
+
+ *node = heap->head;
+ _prev = heap->head;
+ cur = heap->head->next;
+ while (cur) {
+ if (higher_prio(cur, *node)) {
+ *node = cur;
+ *prev = _prev;
+ }
+ _prev = cur;
+ cur = cur->next;
+ }
+}
+
+static void __bheap_union(bheap_prio_t higher_prio, struct bheap* heap,
+ struct bheap_node* h2)
+{
+ struct bheap_node* h1;
+ struct bheap_node *prev, *x, *next;
+ if (!h2)
+ return;
+ h1 = heap->head;
+ if (!h1) {
+ heap->head = h2;
+ return;
+ }
+ h1 = __bheap_merge(h1, h2);
+ prev = NULL;
+ x = h1;
+ next = x->next;
+ while (next) {
+ if (x->degree != next->degree ||
+ (next->next && next->next->degree == x->degree)) {
+ /* nothing to do, advance */
+ prev = x;
+ x = next;
+ } else if (higher_prio(x, next)) {
+ /* x becomes the root of next */
+ x->next = next->next;
+ __bheap_link(x, next);
+ } else {
+ /* next becomes the root of x */
+ if (prev)
+ prev->next = next;
+ else
+ h1 = next;
+ __bheap_link(next, x);
+ x = next;
+ }
+ next = x->next;
+ }
+ heap->head = h1;
+}
+
+static struct bheap_node* __bheap_extract_min(bheap_prio_t higher_prio,
+ struct bheap* heap)
+{
+ struct bheap_node *prev, *node;
+ __bheap_min(higher_prio, heap, &prev, &node);
+ if (!node)
+ return NULL;
+ if (prev)
+ prev->next = node->next;
+ else
+ heap->head = node->next;
+ __bheap_union(higher_prio, heap, __bheap_reverse(node->child));
+ return node;
+}
+
+/* insert (and reinitialize) a node into the heap */
+void bheap_insert(bheap_prio_t higher_prio, struct bheap* heap,
+ struct bheap_node* node)
+{
+ struct bheap_node *min;
+ node->child = NULL;
+ node->parent = NULL;
+ node->next = NULL;
+ node->degree = 0;
+ if (heap->min && higher_prio(node, heap->min)) {
+ /* swap min cache */
+ min = heap->min;
+ min->child = NULL;
+ min->parent = NULL;
+ min->next = NULL;
+ min->degree = 0;
+ __bheap_union(higher_prio, heap, min);
+ heap->min = node;
+ } else
+ __bheap_union(higher_prio, heap, node);
+}
+
+void bheap_uncache_min(bheap_prio_t higher_prio, struct bheap* heap)
+{
+ struct bheap_node* min;
+ if (heap->min) {
+ min = heap->min;
+ heap->min = NULL;
+ bheap_insert(higher_prio, heap, min);
+ }
+}
+
+/* merge addition into target */
+void bheap_union(bheap_prio_t higher_prio,
+ struct bheap* target, struct bheap* addition)
+{
+ /* first insert any cached minima, if necessary */
+ bheap_uncache_min(higher_prio, target);
+ bheap_uncache_min(higher_prio, addition);
+ __bheap_union(higher_prio, target, addition->head);
+ /* this is a destructive merge */
+ addition->head = NULL;
+}
+
+struct bheap_node* bheap_peek(bheap_prio_t higher_prio,
+ struct bheap* heap)
+{
+ if (!heap->min)
+ heap->min = __bheap_extract_min(higher_prio, heap);
+ return heap->min;
+}
+
+struct bheap_node* bheap_take(bheap_prio_t higher_prio,
+ struct bheap* heap)
+{
+ struct bheap_node *node;
+ if (!heap->min)
+ heap->min = __bheap_extract_min(higher_prio, heap);
+ node = heap->min;
+ heap->min = NULL;
+ if (node)
+ node->degree = NOT_IN_HEAP;
+ return node;
+}
+
+int bheap_decrease(bheap_prio_t higher_prio, struct bheap_node* node)
+{
+ struct bheap_node *parent;
+ struct bheap_node** tmp_ref;
+ void* tmp;
+
+ /* bubble up */
+ parent = node->parent;
+ while (parent && higher_prio(node, parent)) {
+ /* swap parent and node */
+ tmp = parent->value;
+ parent->value = node->value;
+ node->value = tmp;
+ /* swap references */
+ *(parent->ref) = node;
+ *(node->ref) = parent;
+ tmp_ref = parent->ref;
+ parent->ref = node->ref;
+ node->ref = tmp_ref;
+ /* step up */
+ node = parent;
+ parent = node->parent;
+ }
+
+ return parent != NULL;
+}
+
+void bheap_delete(bheap_prio_t higher_prio, struct bheap* heap,
+ struct bheap_node* node)
+{
+ struct bheap_node *parent, *prev, *pos;
+ struct bheap_node** tmp_ref;
+ void* tmp;
+
+ if (heap->min != node) {
+ /* bubble up */
+ parent = node->parent;
+ while (parent) {
+ /* swap parent and node */
+ tmp = parent->value;
+ parent->value = node->value;
+ node->value = tmp;
+ /* swap references */
+ *(parent->ref) = node;
+ *(node->ref) = parent;
+ tmp_ref = parent->ref;
+ parent->ref = node->ref;
+ node->ref = tmp_ref;
+ /* step up */
+ node = parent;
+ parent = node->parent;
+ }
+ /* now delete:
+ * first find prev */
+ prev = NULL;
+ pos = heap->head;
+ while (pos != node) {
+ prev = pos;
+ pos = pos->next;
+ }
+ /* we have prev, now remove node */
+ if (prev)
+ prev->next = node->next;
+ else
+ heap->head = node->next;
+ __bheap_union(higher_prio, heap, __bheap_reverse(node->child));
+ } else
+ heap->min = NULL;
+ node->degree = NOT_IN_HEAP;
+}
+
+/* allocate a heap node for value and insert into the heap */
+int bheap_add(bheap_prio_t higher_prio, struct bheap* heap,
+ void* value, int gfp_flags)
+{
+ struct bheap_node* hn = bheap_node_alloc(gfp_flags);
+ if (likely(hn)) {
+ bheap_node_init(&hn, value);
+ bheap_insert(higher_prio, heap, hn);
+ }
+ return hn != NULL;
+}
+
+void* bheap_take_del(bheap_prio_t higher_prio,
+ struct bheap* heap)
+{
+ struct bheap_node* hn = bheap_take(higher_prio, heap);
+ void* ret = NULL;
+ if (hn) {
+ ret = hn->value;
+ bheap_node_free(hn);
+ }
+ return ret;
+}
diff --git a/litmus/budget.c b/litmus/budget.c
new file mode 100644
index 0000000..310e9a3
--- /dev/null
+++ b/litmus/budget.c
@@ -0,0 +1,111 @@
+#include <linux/sched.h>
+#include <linux/percpu.h>
+#include <linux/hrtimer.h>
+
+#include <litmus/litmus.h>
+#include <litmus/preempt.h>
+
+struct enforcement_timer {
+ /* The enforcement timer is used to accurately police
+ * slice budgets. */
+ struct hrtimer timer;
+ int armed;
+};
+
+DEFINE_PER_CPU(struct enforcement_timer, budget_timer);
+
+static enum hrtimer_restart on_enforcement_timeout(struct hrtimer *timer)
+{
+ struct enforcement_timer* et = container_of(timer,
+ struct enforcement_timer,
+ timer);
+ unsigned long flags;
+
+ local_irq_save(flags);
+ TRACE("enforcement timer fired.\n");
+ et->armed = 0;
+ /* activate scheduler */
+ litmus_reschedule_local();
+ local_irq_restore(flags);
+
+ return HRTIMER_NORESTART;
+}
+
+/* assumes called with IRQs off */
+static void cancel_enforcement_timer(struct enforcement_timer* et)
+{
+ int ret;
+
+ TRACE("cancelling enforcement timer.\n");
+
+ /* Since interrupts are disabled and et->armed is only
+ * modified locally, we do not need any locks.
+ */
+
+ if (et->armed) {
+ ret = hrtimer_try_to_cancel(&et->timer);
+ /* Should never be inactive. */
+ BUG_ON(ret == 0);
+ /* Should never be running concurrently. */
+ BUG_ON(ret == -1);
+
+ et->armed = 0;
+ }
+}
+
+/* assumes called with IRQs off */
+static void arm_enforcement_timer(struct enforcement_timer* et,
+ struct task_struct* t)
+{
+ lt_t when_to_fire;
+ TRACE_TASK(t, "arming enforcement timer.\n");
+
+ /* Calling this when there is no budget left for the task
+ * makes no sense, unless the task is non-preemptive. */
+ BUG_ON(budget_exhausted(t) && (!is_np(t)));
+
+ /* __hrtimer_start_range_ns() cancels the timer
+ * anyway, so we don't have to check whether it is still armed */
+
+ if (likely(!is_np(t))) {
+ when_to_fire = litmus_clock() + budget_remaining(t);
+ __hrtimer_start_range_ns(&et->timer,
+ ns_to_ktime(when_to_fire),
+ 0 /* delta */,
+ HRTIMER_MODE_ABS_PINNED,
+ 0 /* no wakeup */);
+ et->armed = 1;
+ }
+}
+
+
+/* expects to be called with IRQs off */
+void update_enforcement_timer(struct task_struct* t)
+{
+ struct enforcement_timer* et = &__get_cpu_var(budget_timer);
+
+ if (t && budget_precisely_enforced(t)) {
+ /* Make sure we call into the scheduler when this budget
+ * expires. */
+ arm_enforcement_timer(et, t);
+ } else if (et->armed) {
+ /* Make sure we don't cause unnecessary interrupts. */
+ cancel_enforcement_timer(et);
+ }
+}
+
+
+static int __init init_budget_enforcement(void)
+{
+ int cpu;
+ struct enforcement_timer* et;
+
+ for (cpu = 0; cpu < NR_CPUS; cpu++) {
+ et = &per_cpu(budget_timer, cpu);
+ hrtimer_init(&et->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
+ et->timer.function = on_enforcement_timeout;
+ }
+ return 0;
+}
+
+module_init(init_budget_enforcement);
diff --git a/litmus/ctrldev.c b/litmus/ctrldev.c
new file mode 100644
index 0000000..6677a67
--- /dev/null
+++ b/litmus/ctrldev.c
@@ -0,0 +1,150 @@
+#include <linux/sched.h>
+#include <linux/mm.h>
+#include <linux/fs.h>
+#include <linux/miscdevice.h>
+#include <linux/module.h>
+
+#include <litmus/litmus.h>
+
+/* only one page for now, but we might want to add a RO version at some point */
+
+#define CTRL_NAME "litmus/ctrl"
+
+/* allocate t->rt_param.ctrl_page*/
+static int alloc_ctrl_page(struct task_struct *t)
+{
+ int err = 0;
+
+ /* only allocate if the task doesn't have one yet */
+ if (!tsk_rt(t)->ctrl_page) {
+ tsk_rt(t)->ctrl_page = (void*) get_zeroed_page(GFP_KERNEL);
+ if (!tsk_rt(t)->ctrl_page)
+ err = -ENOMEM;
+ /* will get de-allocated in task teardown */
+ TRACE_TASK(t, "%s ctrl_page = %p\n", __FUNCTION__,
+ tsk_rt(t)->ctrl_page);
+ }
+ return err;
+}
+
+static int map_ctrl_page(struct task_struct *t, struct vm_area_struct* vma)
+{
+ int err;
+ unsigned long pfn;
+
+ struct page* ctrl = virt_to_page(tsk_rt(t)->ctrl_page);
+
+ /* Increase ref count. Is decreased when vma is destroyed. */
+ get_page(ctrl);
+
+ /* compute page frame number */
+ pfn = page_to_pfn(ctrl);
+
+ TRACE_CUR(CTRL_NAME
+ ": mapping %p (pfn:%lx, %lx) to 0x%lx (prot:%lx)\n",
+ tsk_rt(t)->ctrl_page, pfn, page_to_pfn(ctrl), vma->vm_start,
+ vma->vm_page_prot);
+
+ /* Map it into the vma. Make sure to use PAGE_SHARED, otherwise
+ * userspace actually gets a copy-on-write page. */
+ err = remap_pfn_range(vma, vma->vm_start, pfn, PAGE_SIZE, PAGE_SHARED);
+
+ if (err)
+ TRACE_CUR(CTRL_NAME ": remap_pfn_range() failed (%d)\n", err);
+
+ return err;
+}
+
+static void litmus_ctrl_vm_close(struct vm_area_struct* vma)
+{
+ TRACE_CUR("%s flags=0x%x prot=0x%x\n", __FUNCTION__,
+ vma->vm_flags, vma->vm_page_prot);
+
+ TRACE_CUR(CTRL_NAME
+ ": %p:%p vma:%p vma->vm_private_data:%p closed.\n",
+ (void*) vma->vm_start, (void*) vma->vm_end, vma,
+ vma->vm_private_data, current->comm,
+ current->pid);
+}
+
+static int litmus_ctrl_vm_fault(struct vm_area_struct* vma,
+ struct vm_fault* vmf)
+{
+ /* This function should never be called, since
+ * all pages should have been mapped by mmap()
+ * already. */
+ TRACE_CUR("%s flags=0x%x\n", __FUNCTION__, vma->vm_flags);
+
+ /* nope, you only get one page */
+ return VM_FAULT_SIGBUS;
+}
+
+static struct vm_operations_struct litmus_ctrl_vm_ops = {
+ .close = litmus_ctrl_vm_close,
+ .fault = litmus_ctrl_vm_fault,
+};
+
+static int litmus_ctrl_mmap(struct file* filp, struct vm_area_struct* vma)
+{
+ int err = 0;
+
+ /* first make sure mapper knows what he's doing */
+
+ /* you can only get one page */
+ if (vma->vm_end - vma->vm_start != PAGE_SIZE)
+ return -EINVAL;
+
+ /* you can only map the "first" page */
+ if (vma->vm_pgoff != 0)
+ return -EINVAL;
+
+ /* you can't share it with anyone */
+ if (vma->vm_flags & (VM_MAYSHARE | VM_SHARED))
+ return -EINVAL;
+
+ vma->vm_ops = &litmus_ctrl_vm_ops;
+ /* this mapping should not be kept across forks,
+ * and cannot be expanded */
+ vma->vm_flags |= VM_DONTCOPY | VM_DONTEXPAND;
+
+ err = alloc_ctrl_page(current);
+ if (!err)
+ err = map_ctrl_page(current, vma);
+
+ TRACE_CUR("%s flags=0x%x prot=0x%lx\n",
+ __FUNCTION__, vma->vm_flags, vma->vm_page_prot);
+
+ return err;
+}
+
+static struct file_operations litmus_ctrl_fops = {
+ .owner = THIS_MODULE,
+ .mmap = litmus_ctrl_mmap,
+};
+
+static struct miscdevice litmus_ctrl_dev = {
+ .name = CTRL_NAME,
+ .minor = MISC_DYNAMIC_MINOR,
+ .fops = &litmus_ctrl_fops,
+};
+
+static int __init init_litmus_ctrl_dev(void)
+{
+ int err;
+
+ BUILD_BUG_ON(sizeof(struct control_page) > PAGE_SIZE);
+
+ printk("Initializing LITMUS^RT control device.\n");
+ err = misc_register(&litmus_ctrl_dev);
+ if (err)
+ printk("Could not allocate %s device (%d).\n", CTRL_NAME, err);
+ return err;
+}
+
+static void __exit exit_litmus_ctrl_dev(void)
+{
+ misc_deregister(&litmus_ctrl_dev);
+}
+
+module_init(init_litmus_ctrl_dev);
+module_exit(exit_litmus_ctrl_dev);
diff --git a/litmus/edf_common.c b/litmus/edf_common.c
new file mode 100644
index 0000000..06daec6
--- /dev/null
+++ b/litmus/edf_common.c
@@ -0,0 +1,102 @@
+/*
+ * kernel/edf_common.c
+ *
+ * Common functions for EDF based scheduler.
+ */
+
+#include <linux/percpu.h>
+#include <linux/sched.h>
+#include <linux/list.h>
+
+#include <litmus/litmus.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/sched_trace.h>
+
+#include <litmus/edf_common.h>
+
+/* edf_higher_prio - returns true if first has a higher EDF priority
+ * than second. Deadline ties are broken by PID.
+ *
+ * both first and second may be NULL
+ */
+int edf_higher_prio(struct task_struct* first,
+ struct task_struct* second)
+{
+ struct task_struct *first_task = first;
+ struct task_struct *second_task = second;
+
+ /* There is no point in comparing a task to itself. */
+ if (first && first == second) {
+ TRACE_TASK(first,
+ "WARNING: pointless edf priority comparison.\n");
+ return 0;
+ }
+
+
+ /* Check for inherited priorities. Change task
+ * used for comparison in such a case.
+ */
+ if (first && first->rt_param.inh_task)
+ first_task = first->rt_param.inh_task;
+ if (second && second->rt_param.inh_task)
+ second_task = second->rt_param.inh_task;
+
+ return
+ /* it has to exist in order to have higher priority */
+ first_task && (
+ /* does the second task exist and is it a real-time task? If
+ * not, the first task (which is a RT task) has higher
+ * priority.
+ */
+ !second_task || !is_realtime(second_task) ||
+
+ /* is the deadline of the first task earlier?
+ * Then it has higher priority.
+ */
+ earlier_deadline(first_task, second_task) ||
+
+ /* Do we have a deadline tie?
+ * Then break by PID.
+ */
+ (get_deadline(first_task) == get_deadline(second_task) &&
+ (first_task->pid < second_task->pid ||
+
+ /* If the PIDs are the same then the task with the inherited
+ * priority wins.
+ */
+ (first_task->pid == second_task->pid &&
+ !second->rt_param.inh_task))));
+}
+
+int edf_ready_order(struct bheap_node* a, struct bheap_node* b)
+{
+ return edf_higher_prio(bheap2task(a), bheap2task(b));
+}
+
+void edf_domain_init(rt_domain_t* rt, check_resched_needed_t resched,
+ release_jobs_t release)
+{
+ rt_domain_init(rt, edf_ready_order, resched, release);
+}
+
+/* need_to_preempt - check whether the task t needs to be preempted
+ * call only with irqs disabled and with ready_lock acquired
+ * THIS DOES NOT TAKE NON-PREEMPTIVE SECTIONS INTO ACCOUNT!
+ */
+int edf_preemption_needed(rt_domain_t* rt, struct task_struct *t)
+{
+ /* we need the read lock for edf_ready_queue */
+ /* no need to preempt if there is nothing pending */
+ if (!__jobs_pending(rt))
+ return 0;
+ /* we need to reschedule if t doesn't exist */
+ if (!t)
+ return 1;
+
+ /* NOTE: We cannot check for non-preemptibility since we
+ * don't know what address space we're currently in.
+ */
+
+ /* make sure to get non-rt stuff out of the way */
+ return !is_realtime(t) || edf_higher_prio(__next_ready(rt), t);
+}
diff --git a/litmus/fdso.c b/litmus/fdso.c
new file mode 100644
index 0000000..85be716
--- /dev/null
+++ b/litmus/fdso.c
@@ -0,0 +1,281 @@
+/* fdso.c - file descriptor attached shared objects
+ *
+ * (c) 2007 B. Brandenburg, LITMUS^RT project
+ *
+ * Notes:
+ * - objects descriptor (OD) tables are not cloned during a fork.
+ * - objects are created on-demand, and freed after the last reference
+ * is dropped.
+ * - for now, object types are hard coded.
+ * - As long as we have live objects, we keep a reference to the inode.
+ */
+
+#include <linux/errno.h>
+#include <linux/sched.h>
+#include <linux/mutex.h>
+#include <linux/file.h>
+#include <asm/uaccess.h>
+
+#include <litmus/fdso.h>
+
+extern struct fdso_ops fmlp_sem_ops;
+extern struct fdso_ops srp_sem_ops;
+
+static const struct fdso_ops* fdso_ops[] = {
+ &fmlp_sem_ops,
+ &srp_sem_ops,
+};
+
+static void* fdso_create(obj_type_t type)
+{
+ if (fdso_ops[type]->create)
+ return fdso_ops[type]->create();
+ else
+ return NULL;
+}
+
+static void fdso_destroy(obj_type_t type, void* obj)
+{
+ fdso_ops[type]->destroy(obj);
+}
+
+static int fdso_open(struct od_table_entry* entry, void* __user config)
+{
+ if (fdso_ops[entry->obj->type]->open)
+ return fdso_ops[entry->obj->type]->open(entry, config);
+ else
+ return 0;
+}
+
+static int fdso_close(struct od_table_entry* entry)
+{
+ if (fdso_ops[entry->obj->type]->close)
+ return fdso_ops[entry->obj->type]->close(entry);
+ else
+ return 0;
+}
+
+/* inode must be locked already */
+static struct inode_obj_id* alloc_inode_obj(struct inode* inode,
+ obj_type_t type,
+ unsigned int id)
+{
+ struct inode_obj_id* obj;
+ void* raw_obj;
+
+ raw_obj = fdso_create(type);
+ if (!raw_obj)
+ return NULL;
+
+ obj = kmalloc(sizeof(*obj), GFP_KERNEL);
+ if (!obj)
+ return NULL;
+ INIT_LIST_HEAD(&obj->list);
+ atomic_set(&obj->count, 1);
+ obj->type = type;
+ obj->id = id;
+ obj->obj = raw_obj;
+ obj->inode = inode;
+
+ list_add(&obj->list, &inode->i_obj_list);
+ atomic_inc(&inode->i_count);
+
+ printk(KERN_DEBUG "alloc_inode_obj(%p, %d, %d): object created\n", inode, type, id);
+ return obj;
+}
+
+/* inode must be locked already */
+static struct inode_obj_id* get_inode_obj(struct inode* inode,
+ obj_type_t type,
+ unsigned int id)
+{
+ struct list_head* pos;
+ struct inode_obj_id* obj = NULL;
+
+ list_for_each(pos, &inode->i_obj_list) {
+ obj = list_entry(pos, struct inode_obj_id, list);
+ if (obj->id == id && obj->type == type) {
+ atomic_inc(&obj->count);
+ return obj;
+ }
+ }
+ printk(KERN_DEBUG "get_inode_obj(%p, %d, %d): couldn't find object\n", inode, type, id);
+ return NULL;
+}
+
+
+static void put_inode_obj(struct inode_obj_id* obj)
+{
+ struct inode* inode;
+ int let_go = 0;
+
+ inode = obj->inode;
+ if (atomic_dec_and_test(&obj->count)) {
+
+ mutex_lock(&inode->i_obj_mutex);
+ /* no new references can be obtained */
+ if (!atomic_read(&obj->count)) {
+ list_del(&obj->list);
+ fdso_destroy(obj->type, obj->obj);
+ kfree(obj);
+ let_go = 1;
+ }
+ mutex_unlock(&inode->i_obj_mutex);
+ if (let_go)
+ iput(inode);
+ }
+}
+
+static struct od_table_entry* get_od_entry(struct task_struct* t)
+{
+ struct od_table_entry* table;
+ int i;
+
+
+ table = t->od_table;
+ if (!table) {
+ table = kzalloc(sizeof(*table) * MAX_OBJECT_DESCRIPTORS,
+ GFP_KERNEL);
+ t->od_table = table;
+ }
+
+ for (i = 0; table && i < MAX_OBJECT_DESCRIPTORS; i++)
+ if (!table[i].used) {
+ table[i].used = 1;
+ return table + i;
+ }
+ return NULL;
+}
+
+static int put_od_entry(struct od_table_entry* od)
+{
+ put_inode_obj(od->obj);
+ od->used = 0;
+ return 0;
+}
+
+void exit_od_table(struct task_struct* t)
+{
+ int i;
+
+ if (t->od_table) {
+ for (i = 0; i < MAX_OBJECT_DESCRIPTORS; i++)
+ if (t->od_table[i].used)
+ put_od_entry(t->od_table + i);
+ kfree(t->od_table);
+ t->od_table = NULL;
+ }
+}
+
+static int do_sys_od_open(struct file* file, obj_type_t type, int id,
+ void* __user config)
+{
+ int idx = 0, err;
+ struct inode* inode;
+ struct inode_obj_id* obj = NULL;
+ struct od_table_entry* entry;
+
+ inode = file->f_dentry->d_inode;
+
+ entry = get_od_entry(current);
+ if (!entry)
+ return -ENOMEM;
+
+ mutex_lock(&inode->i_obj_mutex);
+ obj = get_inode_obj(inode, type, id);
+ if (!obj)
+ obj = alloc_inode_obj(inode, type, id);
+ if (!obj) {
+ idx = -ENOMEM;
+ entry->used = 0;
+ } else {
+ entry->obj = obj;
+ entry->extra = NULL;
+ idx = entry - current->od_table;
+ }
+
+ mutex_unlock(&inode->i_obj_mutex);
+
+ err = fdso_open(entry, config);
+ if (err < 0) {
+ /* The class rejected the open call.
+ * We need to clean up and tell user space.
+ */
+ put_od_entry(entry);
+ idx = err;
+ }
+
+ return idx;
+}
+
+
+struct od_table_entry* __od_lookup(int od)
+{
+ struct task_struct *t = current;
+
+ if (!t->od_table)
+ return NULL;
+ if (od < 0 || od >= MAX_OBJECT_DESCRIPTORS)
+ return NULL;
+ if (!t->od_table[od].used)
+ return NULL;
+ return t->od_table + od;
+}
+
+
+asmlinkage long sys_od_open(int fd, int type, int obj_id, void* __user config)
+{
+ int ret = 0;
+ struct file* file;
+
+ /*
+ 1) get file from fd, get inode from file
+ 2) lock inode
+ 3) try to lookup object
+ 4) if not present create and enqueue object, inc inode refcnt
+ 5) increment refcnt of object
+ 6) alloc od_table_entry, setup ptrs
+ 7) unlock inode
+ 8) return offset in od_table as OD
+ */
+
+ if (type < MIN_OBJ_TYPE || type > MAX_OBJ_TYPE) {
+ ret = -EINVAL;
+ goto out;
+ }
+
+ file = fget(fd);
+ if (!file) {
+ ret = -EBADF;
+ goto out;
+ }
+
+ ret = do_sys_od_open(file, type, obj_id, config);
+
+ fput(file);
+
+out:
+ return ret;
+}
+
+
+asmlinkage long sys_od_close(int od)
+{
+ int ret = -EINVAL;
+ struct task_struct *t = current;
+
+ if (od < 0 || od >= MAX_OBJECT_DESCRIPTORS)
+ return ret;
+
+ if (!t->od_table || !t->od_table[od].used)
+ return ret;
+
+
+ /* give the class a chance to reject the close
+ */
+ ret = fdso_close(t->od_table + od);
+ if (ret == 0)
+ ret = put_od_entry(t->od_table + od);
+
+ return ret;
+}
diff --git a/litmus/fmlp.c b/litmus/fmlp.c
new file mode 100644
index 0000000..a9a6385
--- /dev/null
+++ b/litmus/fmlp.c
@@ -0,0 +1,268 @@
+/*
+ * FMLP implementation.
+ * Much of the code here is borrowed from include/asm-i386/semaphore.h
+ */
+
+#include <asm/atomic.h>
+
+#include <linux/semaphore.h>
+#include <linux/sched.h>
+#include <linux/wait.h>
+#include <linux/spinlock.h>
+
+#include <litmus/litmus.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/edf_common.h>
+
+#include <litmus/fdso.h>
+
+#include <litmus/trace.h>
+
+#ifdef CONFIG_FMLP
+
+static void* create_fmlp_semaphore(void)
+{
+ struct pi_semaphore* sem;
+ int i;
+
+ sem = kmalloc(sizeof(*sem), GFP_KERNEL);
+ if (!sem)
+ return NULL;
+ atomic_set(&sem->count, 1);
+ sem->sleepers = 0;
+ init_waitqueue_head(&sem->wait);
+ sem->hp.task = NULL;
+ sem->holder = NULL;
+ for (i = 0; i < NR_CPUS; i++)
+ sem->hp.cpu_task[i] = NULL;
+ return sem;
+}
+
+static int open_fmlp_semaphore(struct od_table_entry* entry, void* __user arg)
+{
+ if (!fmlp_active())
+ return -EBUSY;
+ return 0;
+}
+
+static void destroy_fmlp_semaphore(void* sem)
+{
+ /* XXX assert invariants */
+ kfree(sem);
+}
+
+struct fdso_ops fmlp_sem_ops = {
+ .create = create_fmlp_semaphore,
+ .open = open_fmlp_semaphore,
+ .destroy = destroy_fmlp_semaphore
+};
+
+struct wq_pair {
+ struct task_struct* tsk;
+ struct pi_semaphore* sem;
+};
+
+static int rt_pi_wake_up(wait_queue_t *wait, unsigned mode, int sync,
+ void *key)
+{
+ struct wq_pair* wqp = (struct wq_pair*) wait->private;
+ set_rt_flags(wqp->tsk, RT_F_EXIT_SEM);
+ litmus->inherit_priority(wqp->sem, wqp->tsk);
+ TRACE_TASK(wqp->tsk,
+ "woken up by rt_pi_wake_up() (RT_F_SEM_EXIT, PI)\n");
+ /* point to task for default_wake_function() */
+ wait->private = wqp->tsk;
+ default_wake_function(wait, mode, sync, key);
+
+ /* Always return true since we know that if we encountered a task
+ * that was already running the wake_up raced with the schedule in
+ * rt_pi_down(). In that case the task in rt_pi_down() will be scheduled
+ * immediately and own the lock. We must not wake up another task in
+ * any case.
+ */
+ return 1;
+}
+
+/* caller is responsible for locking */
+int edf_set_hp_task(struct pi_semaphore *sem)
+{
+ struct list_head *tmp, *next;
+ struct task_struct *queued;
+ int ret = 0;
+
+ sem->hp.task = NULL;
+ list_for_each_safe(tmp, next, &sem->wait.task_list) {
+ queued = ((struct wq_pair*)
+ list_entry(tmp, wait_queue_t,
+ task_list)->private)->tsk;
+
+ /* Compare task prios, find high prio task. */
+ if (edf_higher_prio(queued, sem->hp.task)) {
+ sem->hp.task = queued;
+ ret = 1;
+ }
+ }
+ return ret;
+}
+
+/* caller is responsible for locking */
+int edf_set_hp_cpu_task(struct pi_semaphore *sem, int cpu)
+{
+ struct list_head *tmp, *next;
+ struct task_struct *queued;
+ int ret = 0;
+
+ sem->hp.cpu_task[cpu] = NULL;
+ list_for_each_safe(tmp, next, &sem->wait.task_list) {
+ queued = ((struct wq_pair*)
+ list_entry(tmp, wait_queue_t,
+ task_list)->private)->tsk;
+
+ /* Compare task prios, find high prio task. */
+ if (get_partition(queued) == cpu &&
+ edf_higher_prio(queued, sem->hp.cpu_task[cpu])) {
+ sem->hp.cpu_task[cpu] = queued;
+ ret = 1;
+ }
+ }
+ return ret;
+}
+
+static int do_fmlp_down(struct pi_semaphore* sem)
+{
+ unsigned long flags;
+ struct task_struct *tsk = current;
+ struct wq_pair pair;
+ int suspended = 1;
+ wait_queue_t wait = {
+ .private = &pair,
+ .func = rt_pi_wake_up,
+ .task_list = {NULL, NULL}
+ };
+
+ pair.tsk = tsk;
+ pair.sem = sem;
+ spin_lock_irqsave(&sem->wait.lock, flags);
+
+ if (atomic_dec_return(&sem->count) < 0 ||
+ waitqueue_active(&sem->wait)) {
+ /* we need to suspend */
+ tsk->state = TASK_UNINTERRUPTIBLE;
+ __add_wait_queue_tail_exclusive(&sem->wait, &wait);
+
+ TRACE_CUR("suspends on PI lock %p\n", sem);
+ litmus->pi_block(sem, tsk);
+
+ /* release lock before sleeping */
+ spin_unlock_irqrestore(&sem->wait.lock, flags);
+
+ TS_PI_DOWN_END;
+ preempt_enable_no_resched();
+
+
+ /* we depend on the FIFO order
+ * Thus, we don't need to recheck when we wake up, we
+ * are guaranteed to have the lock since there is only one
+ * wake up per release
+ */
+ schedule();
+
+ TRACE_CUR("woke up, now owns PI lock %p\n", sem);
+
+ /* try_to_wake_up() set our state to TASK_RUNNING,
+ * all we need to do is to remove our wait queue entry
+ */
+ remove_wait_queue(&sem->wait, &wait);
+ } else {
+ /* no priority inheritance necessary, since there are no queued
+ * tasks.
+ */
+ suspended = 0;
+ TRACE_CUR("acquired PI lock %p, no contention\n", sem);
+ sem->holder = tsk;
+
+ /* don't know if we're global or partitioned. */
+ sem->hp.task = tsk;
+ sem->hp.cpu_task[get_partition(tsk)] = tsk;
+
+ litmus->inherit_priority(sem, tsk);
+ spin_unlock_irqrestore(&sem->wait.lock, flags);
+ }
+ return suspended;
+}
+
+static void do_fmlp_up(struct pi_semaphore* sem)
+{
+ unsigned long flags;
+
+ spin_lock_irqsave(&sem->wait.lock, flags);
+
+ TRACE_CUR("releases PI lock %p\n", sem);
+ litmus->return_priority(sem);
+ sem->holder = NULL;
+ if (atomic_inc_return(&sem->count) < 1)
+ /* there is a task queued */
+ wake_up_locked(&sem->wait);
+
+ spin_unlock_irqrestore(&sem->wait.lock, flags);
+}
+
+asmlinkage long sys_fmlp_down(int sem_od)
+{
+ long ret = 0;
+ struct pi_semaphore * sem;
+ int suspended = 0;
+
+ preempt_disable();
+ TS_PI_DOWN_START;
+
+ sem = lookup_fmlp_sem(sem_od);
+ if (sem)
+ suspended = do_fmlp_down(sem);
+ else
+ ret = -EINVAL;
+
+ if (!suspended) {
+ TS_PI_DOWN_END;
+ preempt_enable();
+ }
+
+ return ret;
+}
+
+asmlinkage long sys_fmlp_up(int sem_od)
+{
+ long ret = 0;
+ struct pi_semaphore * sem;
+
+ preempt_disable();
+ TS_PI_UP_START;
+
+ sem = lookup_fmlp_sem(sem_od);
+ if (sem)
+ do_fmlp_up(sem);
+ else
+ ret = -EINVAL;
+
+
+ TS_PI_UP_END;
+ preempt_enable();
+
+ return ret;
+}
+
+#else
+
+struct fdso_ops fmlp_sem_ops = {};
+
+asmlinkage long sys_fmlp_down(int sem_od)
+{
+ return -ENOSYS;
+}
+
+asmlinkage long sys_fmlp_up(int sem_od)
+{
+ return -ENOSYS;
+}
+
+#endif
diff --git a/litmus/ft_event.c b/litmus/ft_event.c
new file mode 100644
index 0000000..399a07b
--- /dev/null
+++ b/litmus/ft_event.c
@@ -0,0 +1,43 @@
+#include <linux/types.h>
+
+#include <litmus/feather_trace.h>
+
+#if !defined(CONFIG_ARCH_HAS_FEATHER_TRACE) || defined(CONFIG_DEBUG_RODATA)
+/* provide dummy implementation */
+
+int ft_events[MAX_EVENTS];
+
+int ft_enable_event(unsigned long id)
+{
+ if (id < MAX_EVENTS) {
+ ft_events[id]++;
+ return 1;
+ } else
+ return 0;
+}
+
+int ft_disable_event(unsigned long id)
+{
+ if (id < MAX_EVENTS && ft_events[id]) {
+ ft_events[id]--;
+ return 1;
+ } else
+ return 0;
+}
+
+int ft_disable_all_events(void)
+{
+ int i;
+
+ for (i = 0; i < MAX_EVENTS; i++)
+ ft_events[i] = 0;
+
+ return MAX_EVENTS;
+}
+
+int ft_is_event_enabled(unsigned long id)
+{
+ return id < MAX_EVENTS && ft_events[id];
+}
+
+#endif
diff --git a/litmus/ftdev.c b/litmus/ftdev.c
new file mode 100644
index 0000000..4a4b2e3
--- /dev/null
+++ b/litmus/ftdev.c
@@ -0,0 +1,440 @@
+#include <linux/sched.h>
+#include <linux/fs.h>
+#include <linux/slab.h>
+#include <linux/cdev.h>
+#include <asm/uaccess.h>
+#include <linux/module.h>
+#include <linux/device.h>
+
+#include <litmus/litmus.h>
+#include <litmus/feather_trace.h>
+#include <litmus/ftdev.h>
+
+struct ft_buffer* alloc_ft_buffer(unsigned int count, size_t size)
+{
+ struct ft_buffer* buf;
+ size_t total = (size + 1) * count;
+ char* mem;
+ int order = 0, pages = 1;
+
+ buf = kmalloc(sizeof(*buf), GFP_KERNEL);
+ if (!buf)
+ return NULL;
+
+ total = (total / PAGE_SIZE) + (total % PAGE_SIZE != 0);
+ while (pages < total) {
+ order++;
+ pages *= 2;
+ }
+
+ mem = (char*) __get_free_pages(GFP_KERNEL, order);
+ if (!mem) {
+ kfree(buf);
+ return NULL;
+ }
+
+ if (!init_ft_buffer(buf, count, size,
+ mem + (count * size), /* markers at the end */
+ mem)) { /* buffer objects */
+ free_pages((unsigned long) mem, order);
+ kfree(buf);
+ return NULL;
+ }
+ return buf;
+}
+
+void free_ft_buffer(struct ft_buffer* buf)
+{
+ int order = 0, pages = 1;
+ size_t total;
+
+ if (buf) {
+ total = (buf->slot_size + 1) * buf->slot_count;
+ total = (total / PAGE_SIZE) + (total % PAGE_SIZE != 0);
+ while (pages < total) {
+ order++;
+ pages *= 2;
+ }
+ free_pages((unsigned long) buf->buffer_mem, order);
+ kfree(buf);
+ }
+}
+
+struct ftdev_event {
+ int id;
+ struct ftdev_event* next;
+};
+
+static int activate(struct ftdev_event** chain, int id)
+{
+ struct ftdev_event* ev = kmalloc(sizeof(*ev), GFP_KERNEL);
+ if (ev) {
+ printk(KERN_INFO
+ "Enabling feather-trace event %d.\n", (int) id);
+ ft_enable_event(id);
+ ev->id = id;
+ ev->next = *chain;
+ *chain = ev;
+ }
+ return ev ? 0 : -ENOMEM;
+}
+
+static void deactivate(struct ftdev_event** chain, int id)
+{
+ struct ftdev_event **cur = chain;
+ struct ftdev_event *nxt;
+ while (*cur) {
+ if ((*cur)->id == id) {
+ nxt = (*cur)->next;
+ kfree(*cur);
+ *cur = nxt;
+ printk(KERN_INFO
+ "Disabling feather-trace event %d.\n", (int) id);
+ ft_disable_event(id);
+ break;
+ }
+ cur = &(*cur)->next;
+ }
+}
+
+static int ftdev_open(struct inode *in, struct file *filp)
+{
+ struct ftdev* ftdev;
+ struct ftdev_minor* ftdm;
+ unsigned int buf_idx = iminor(in);
+ int err = 0;
+
+ ftdev = container_of(in->i_cdev, struct ftdev, cdev);
+
+ if (buf_idx >= ftdev->minor_cnt) {
+ err = -ENODEV;
+ goto out;
+ }
+ if (ftdev->can_open && (err = ftdev->can_open(ftdev, buf_idx)))
+ goto out;
+
+ ftdm = ftdev->minor + buf_idx;
+ filp->private_data = ftdm;
+
+ if (mutex_lock_interruptible(&ftdm->lock)) {
+ err = -ERESTARTSYS;
+ goto out;
+ }
+
+ if (!ftdm->readers && ftdev->alloc)
+ err = ftdev->alloc(ftdev, buf_idx);
+ if (0 == err)
+ ftdm->readers++;
+
+ mutex_unlock(&ftdm->lock);
+out:
+ return err;
+}
+
+static int ftdev_release(struct inode *in, struct file *filp)
+{
+ struct ftdev* ftdev;
+ struct ftdev_minor* ftdm;
+ unsigned int buf_idx = iminor(in);
+ int err = 0;
+
+ ftdev = container_of(in->i_cdev, struct ftdev, cdev);
+
+ if (buf_idx >= ftdev->minor_cnt) {
+ err = -ENODEV;
+ goto out;
+ }
+ ftdm = ftdev->minor + buf_idx;
+
+ if (mutex_lock_interruptible(&ftdm->lock)) {
+ err = -ERESTARTSYS;
+ goto out;
+ }
+
+ if (ftdm->readers == 1) {
+ while (ftdm->events)
+ deactivate(&ftdm->events, ftdm->events->id);
+
+ /* wait for any pending events to complete */
+ set_current_state(TASK_UNINTERRUPTIBLE);
+ schedule_timeout(HZ);
+
+ printk(KERN_ALERT "Failed trace writes: %u\n",
+ ftdm->buf->failed_writes);
+
+ if (ftdev->free)
+ ftdev->free(ftdev, buf_idx);
+ }
+
+ ftdm->readers--;
+ mutex_unlock(&ftdm->lock);
+out:
+ return err;
+}
+
+/* based on ft_buffer_read
+ * @returns < 0 : page fault
+ * = 0 : no data available
+ * = 1 : one slot copied
+ */
+static int ft_buffer_copy_to_user(struct ft_buffer* buf, char __user *dest)
+{
+ unsigned int idx;
+ int err = 0;
+ if (buf->free_count != buf->slot_count) {
+ /* data available */
+ idx = buf->read_idx % buf->slot_count;
+ if (buf->slots[idx] == SLOT_READY) {
+ err = copy_to_user(dest, ((char*) buf->buffer_mem) +
+ idx * buf->slot_size,
+ buf->slot_size);
+ if (err == 0) {
+ /* copy ok */
+ buf->slots[idx] = SLOT_FREE;
+ buf->read_idx++;
+ fetch_and_inc(&buf->free_count);
+ err = 1;
+ }
+ }
+ }
+ return err;
+}
+
+static ssize_t ftdev_read(struct file *filp,
+ char __user *to, size_t len, loff_t *f_pos)
+{
+ /* we ignore f_pos, this is strictly sequential */
+
+ ssize_t err = 0;
+ size_t chunk;
+ int copied;
+ struct ftdev_minor* ftdm = filp->private_data;
+
+ if (mutex_lock_interruptible(&ftdm->lock)) {
+ err = -ERESTARTSYS;
+ goto out;
+ }
+
+
+ chunk = ftdm->buf->slot_size;
+ while (len >= chunk) {
+ copied = ft_buffer_copy_to_user(ftdm->buf, to);
+ if (copied == 1) {
+ len -= chunk;
+ to += chunk;
+ err += chunk;
+ } else if (err == 0 && copied == 0 && ftdm->events) {
+ /* Only wait if there are any events enabled and only
+ * if we haven't copied some data yet. We cannot wait
+ * here with copied data because that data would get
+ * lost if the task is interrupted (e.g., killed).
+ */
+ set_current_state(TASK_INTERRUPTIBLE);
+ schedule_timeout(50);
+ if (signal_pending(current)) {
+ if (err == 0)
+ /* nothing read yet, signal problem */
+ err = -ERESTARTSYS;
+ break;
+ }
+ } else if (copied < 0) {
+ /* page fault */
+ err = copied;
+ break;
+ } else
+ /* nothing left to get, return to user space */
+ break;
+ }
+ mutex_unlock(&ftdm->lock);
+out:
+ return err;
+}
+
+typedef uint32_t cmd_t;
+
+static ssize_t ftdev_write(struct file *filp, const char __user *from,
+ size_t len, loff_t *f_pos)
+{
+ struct ftdev_minor* ftdm = filp->private_data;
+ ssize_t err = -EINVAL;
+ cmd_t cmd;
+ cmd_t id;
+
+ if (len % sizeof(cmd) || len < 2 * sizeof(cmd))
+ goto out;
+
+ if (copy_from_user(&cmd, from, sizeof(cmd))) {
+ err = -EFAULT;
+ goto out;
+ }
+ len -= sizeof(cmd);
+ from += sizeof(cmd);
+
+ if (cmd != FTDEV_ENABLE_CMD && cmd != FTDEV_DISABLE_CMD)
+ goto out;
+
+ if (mutex_lock_interruptible(&ftdm->lock)) {
+ err = -ERESTARTSYS;
+ goto out;
+ }
+
+ err = sizeof(cmd);
+ while (len) {
+ if (copy_from_user(&id, from, sizeof(cmd))) {
+ err = -EFAULT;
+ goto out_unlock;
+ }
+ /* FIXME: check id against list of acceptable events */
+ len -= sizeof(cmd);
+ from += sizeof(cmd);
+ if (cmd == FTDEV_DISABLE_CMD)
+ deactivate(&ftdm->events, id);
+ else if (activate(&ftdm->events, id) != 0) {
+ err = -ENOMEM;
+ goto out_unlock;
+ }
+ err += sizeof(cmd);
+ }
+
+out_unlock:
+ mutex_unlock(&ftdm->lock);
+out:
+ return err;
+}
+
+struct file_operations ftdev_fops = {
+ .owner = THIS_MODULE,
+ .open = ftdev_open,
+ .release = ftdev_release,
+ .write = ftdev_write,
+ .read = ftdev_read,
+};
+
+int ftdev_init( struct ftdev* ftdev, struct module* owner,
+ const int minor_cnt, const char* name)
+{
+ int i, err;
+
+ BUG_ON(minor_cnt < 1);
+
+ cdev_init(&ftdev->cdev, &ftdev_fops);
+ ftdev->name = name;
+ ftdev->minor_cnt = minor_cnt;
+ ftdev->cdev.owner = owner;
+ ftdev->cdev.ops = &ftdev_fops;
+ ftdev->alloc = NULL;
+ ftdev->free = NULL;
+ ftdev->can_open = NULL;
+
+ ftdev->minor = kcalloc(ftdev->minor_cnt, sizeof(*ftdev->minor),
+ GFP_KERNEL);
+ if (!ftdev->minor) {
+ printk(KERN_WARNING "ftdev(%s): Could not allocate memory\n",
+ ftdev->name);
+ err = -ENOMEM;
+ goto err_out;
+ }
+
+ for (i = 0; i < ftdev->minor_cnt; i++) {
+ mutex_init(&ftdev->minor[i].lock);
+ ftdev->minor[i].readers = 0;
+ ftdev->minor[i].buf = NULL;
+ ftdev->minor[i].events = NULL;
+ }
+
+ ftdev->class = class_create(owner, ftdev->name);
+ if (IS_ERR(ftdev->class)) {
+ err = PTR_ERR(ftdev->class);
+ printk(KERN_WARNING "ftdev(%s): "
+ "Could not create device class.\n", ftdev->name);
+ goto err_dealloc;
+ }
+
+ return 0;
+
+err_dealloc:
+ kfree(ftdev->minor);
+err_out:
+ return err;
+}
+
+/*
+ * Destroy minor devices up to, but not including, up_to.
+ */
+static void ftdev_device_destroy(struct ftdev* ftdev, unsigned int up_to)
+{
+ dev_t minor_cntr;
+
+ if (up_to < 1)
+ up_to = (ftdev->minor_cnt < 1) ? 0 : ftdev->minor_cnt;
+
+ for (minor_cntr = 0; minor_cntr < up_to; ++minor_cntr)
+ device_destroy(ftdev->class, MKDEV(ftdev->major, minor_cntr));
+}
+
+void ftdev_exit(struct ftdev* ftdev)
+{
+ printk("ftdev(%s): Exiting\n", ftdev->name);
+ ftdev_device_destroy(ftdev, -1);
+ cdev_del(&ftdev->cdev);
+ unregister_chrdev_region(MKDEV(ftdev->major, 0), ftdev->minor_cnt);
+ class_destroy(ftdev->class);
+ kfree(ftdev->minor);
+}
+
+int register_ftdev(struct ftdev* ftdev)
+{
+ struct device **device;
+ dev_t trace_dev_tmp, minor_cntr;
+ int err;
+
+ err = alloc_chrdev_region(&trace_dev_tmp, 0, ftdev->minor_cnt,
+ ftdev->name);
+ if (err) {
+ printk(KERN_WARNING "ftdev(%s): "
+ "Could not allocate char. device region (%d minors)\n",
+ ftdev->name, ftdev->minor_cnt);
+ goto err_out;
+ }
+
+ ftdev->major = MAJOR(trace_dev_tmp);
+
+ err = cdev_add(&ftdev->cdev, trace_dev_tmp, ftdev->minor_cnt);
+ if (err) {
+ printk(KERN_WARNING "ftdev(%s): "
+ "Could not add cdev for major %u with %u minor(s).\n",
+ ftdev->name, ftdev->major, ftdev->minor_cnt);
+ goto err_unregister;
+ }
+
+ /* create the minor device(s) */
+ for (minor_cntr = 0; minor_cntr < ftdev->minor_cnt; ++minor_cntr)
+ {
+ trace_dev_tmp = MKDEV(ftdev->major, minor_cntr);
+ device = &ftdev->minor[minor_cntr].device;
+
+ *device = device_create(ftdev->class, NULL, trace_dev_tmp, NULL,
+ "litmus/%s%d", ftdev->name, minor_cntr);
+ if (IS_ERR(*device)) {
+ err = PTR_ERR(*device);
+ printk(KERN_WARNING "ftdev(%s): "
+ "Could not create device major/minor number "
+ "%u/%u\n", ftdev->name, ftdev->major,
+ minor_cntr);
+ printk(KERN_WARNING "ftdev(%s): "
+ "will attempt deletion of allocated devices.\n",
+ ftdev->name);
+ goto err_minors;
+ }
+ }
+
+ return 0;
+
+err_minors:
+ ftdev_device_destroy(ftdev, minor_cntr);
+ cdev_del(&ftdev->cdev);
+err_unregister:
+ unregister_chrdev_region(MKDEV(ftdev->major, 0), ftdev->minor_cnt);
+err_out:
+ return err;
+}
diff --git a/litmus/jobs.c b/litmus/jobs.c
new file mode 100644
index 0000000..36e3146
--- /dev/null
+++ b/litmus/jobs.c
@@ -0,0 +1,43 @@
+/* litmus/jobs.c - common job control code
+ */
+
+#include <linux/sched.h>
+
+#include <litmus/litmus.h>
+#include <litmus/jobs.h>
+
+void prepare_for_next_period(struct task_struct *t)
+{
+ BUG_ON(!t);
+ /* prepare next release */
+ t->rt_param.job_params.release = t->rt_param.job_params.deadline;
+ t->rt_param.job_params.deadline += get_rt_period(t);
+ t->rt_param.job_params.exec_time = 0;
+ /* update job sequence number */
+ t->rt_param.job_params.job_no++;
+
+ /* don't confuse Linux */
+ t->rt.time_slice = 1;
+}
+
+void release_at(struct task_struct *t, lt_t start)
+{
+ t->rt_param.job_params.deadline = start;
+ prepare_for_next_period(t);
+ set_rt_flags(t, RT_F_RUNNING);
+}
+
+
+/*
+ * Deactivate current task until the beginning of the next period.
+ */
+long complete_job(void)
+{
+ /* Mark that we do not excute anymore */
+ set_rt_flags(current, RT_F_SLEEP);
+ /* call schedule, this will return when a new job arrives
+ * it also takes care of preparing for the next release
+ */
+ schedule();
+ return 0;
+}
diff --git a/litmus/litmus.c b/litmus/litmus.c
new file mode 100644
index 0000000..8efd3f9
--- /dev/null
+++ b/litmus/litmus.c
@@ -0,0 +1,547 @@
+/*
+ * litmus.c -- Implementation of the LITMUS syscalls,
+ * the LITMUS intialization code,
+ * and the procfs interface..
+ */
+#include <asm/uaccess.h>
+#include <linux/uaccess.h>
+#include <linux/sysrq.h>
+#include <linux/sched.h>
+#include <linux/module.h>
+#include <linux/slab.h>
+
+#include <litmus/litmus.h>
+#include <litmus/bheap.h>
+#include <litmus/trace.h>
+#include <litmus/rt_domain.h>
+#include <litmus/litmus_proc.h>
+#include <litmus/sched_trace.h>
+
+/* Number of RT tasks that exist in the system */
+atomic_t rt_task_count = ATOMIC_INIT(0);
+static DEFINE_RAW_SPINLOCK(task_transition_lock);
+/* synchronize plugin switching */
+atomic_t cannot_use_plugin = ATOMIC_INIT(0);
+
+/* Give log messages sequential IDs. */
+atomic_t __log_seq_no = ATOMIC_INIT(0);
+
+#ifdef CONFIG_RELEASE_MASTER
+/* current master CPU for handling timer IRQs */
+atomic_t release_master_cpu = ATOMIC_INIT(NO_CPU);
+#endif
+
+static struct kmem_cache * bheap_node_cache;
+extern struct kmem_cache * release_heap_cache;
+
+struct bheap_node* bheap_node_alloc(int gfp_flags)
+{
+ return kmem_cache_alloc(bheap_node_cache, gfp_flags);
+}
+
+void bheap_node_free(struct bheap_node* hn)
+{
+ kmem_cache_free(bheap_node_cache, hn);
+}
+
+struct release_heap* release_heap_alloc(int gfp_flags);
+void release_heap_free(struct release_heap* rh);
+
+/*
+ * sys_set_task_rt_param
+ * @pid: Pid of the task which scheduling parameters must be changed
+ * @param: New real-time extension parameters such as the execution cost and
+ * period
+ * Syscall for manipulating with task rt extension params
+ * Returns EFAULT if param is NULL.
+ * ESRCH if pid is not corrsponding
+ * to a valid task.
+ * EINVAL if either period or execution cost is <=0
+ * EPERM if pid is a real-time task
+ * 0 if success
+ *
+ * Only non-real-time tasks may be configured with this system call
+ * to avoid races with the scheduler. In practice, this means that a
+ * task's parameters must be set _before_ calling sys_prepare_rt_task()
+ *
+ * find_task_by_vpid() assumes that we are in the same namespace of the
+ * target.
+ */
+asmlinkage long sys_set_rt_task_param(pid_t pid, struct rt_task __user * param)
+{
+ struct rt_task tp;
+ struct task_struct *target;
+ int retval = -EINVAL;
+
+ printk("Setting up rt task parameters for process %d.\n", pid);
+
+ if (pid < 0 || param == 0) {
+ goto out;
+ }
+ if (copy_from_user(&tp, param, sizeof(tp))) {
+ retval = -EFAULT;
+ goto out;
+ }
+
+ /* Task search and manipulation must be protected */
+ read_lock_irq(&tasklist_lock);
+ if (!(target = find_task_by_vpid(pid))) {
+ retval = -ESRCH;
+ goto out_unlock;
+ }
+
+ if (is_realtime(target)) {
+ /* The task is already a real-time task.
+ * We cannot not allow parameter changes at this point.
+ */
+ retval = -EBUSY;
+ goto out_unlock;
+ }
+
+ if (tp.exec_cost <= 0)
+ goto out_unlock;
+ if (tp.period <= 0)
+ goto out_unlock;
+ if (!cpu_online(tp.cpu))
+ goto out_unlock;
+ if (tp.period < tp.exec_cost)
+ {
+ printk(KERN_INFO "litmus: real-time task %d rejected "
+ "because wcet > period\n", pid);
+ goto out_unlock;
+ }
+ if (tp.budget_policy != NO_ENFORCEMENT &&
+ tp.budget_policy != QUANTUM_ENFORCEMENT &&
+ tp.budget_policy != PRECISE_ENFORCEMENT)
+ {
+ printk(KERN_INFO "litmus: real-time task %d rejected "
+ "because unsupported budget enforcement policy "
+ "specified (%d)\n",
+ pid, tp.budget_policy);
+ goto out_unlock;
+ }
+
+ target->rt_param.task_params = tp;
+
+ retval = 0;
+ out_unlock:
+ read_unlock_irq(&tasklist_lock);
+ out:
+ return retval;
+}
+
+/*
+ * Getter of task's RT params
+ * returns EINVAL if param or pid is NULL
+ * returns ESRCH if pid does not correspond to a valid task
+ * returns EFAULT if copying of parameters has failed.
+ *
+ * find_task_by_vpid() assumes that we are in the same namespace of the
+ * target.
+ */
+asmlinkage long sys_get_rt_task_param(pid_t pid, struct rt_task __user * param)
+{
+ int retval = -EINVAL;
+ struct task_struct *source;
+ struct rt_task lp;
+ if (param == 0 || pid < 0)
+ goto out;
+ read_lock(&tasklist_lock);
+ if (!(source = find_task_by_vpid(pid))) {
+ retval = -ESRCH;
+ goto out_unlock;
+ }
+ lp = source->rt_param.task_params;
+ read_unlock(&tasklist_lock);
+ /* Do copying outside the lock */
+ retval =
+ copy_to_user(param, &lp, sizeof(lp)) ? -EFAULT : 0;
+ return retval;
+ out_unlock:
+ read_unlock(&tasklist_lock);
+ out:
+ return retval;
+
+}
+
+/*
+ * This is the crucial function for periodic task implementation,
+ * It checks if a task is periodic, checks if such kind of sleep
+ * is permitted and calls plugin-specific sleep, which puts the
+ * task into a wait array.
+ * returns 0 on successful wakeup
+ * returns EPERM if current conditions do not permit such sleep
+ * returns EINVAL if current task is not able to go to sleep
+ */
+asmlinkage long sys_complete_job(void)
+{
+ int retval = -EPERM;
+ if (!is_realtime(current)) {
+ retval = -EINVAL;
+ goto out;
+ }
+ /* Task with negative or zero period cannot sleep */
+ if (get_rt_period(current) <= 0) {
+ retval = -EINVAL;
+ goto out;
+ }
+ /* The plugin has to put the task into an
+ * appropriate queue and call schedule
+ */
+ retval = litmus->complete_job();
+ out:
+ return retval;
+}
+
+/* This is an "improved" version of sys_complete_job that
+ * addresses the problem of unintentionally missing a job after
+ * an overrun.
+ *
+ * returns 0 on successful wakeup
+ * returns EPERM if current conditions do not permit such sleep
+ * returns EINVAL if current task is not able to go to sleep
+ */
+asmlinkage long sys_wait_for_job_release(unsigned int job)
+{
+ int retval = -EPERM;
+ if (!is_realtime(current)) {
+ retval = -EINVAL;
+ goto out;
+ }
+
+ /* Task with negative or zero period cannot sleep */
+ if (get_rt_period(current) <= 0) {
+ retval = -EINVAL;
+ goto out;
+ }
+
+ retval = 0;
+
+ /* first wait until we have "reached" the desired job
+ *
+ * This implementation has at least two problems:
+ *
+ * 1) It doesn't gracefully handle the wrap around of
+ * job_no. Since LITMUS is a prototype, this is not much
+ * of a problem right now.
+ *
+ * 2) It is theoretically racy if a job release occurs
+ * between checking job_no and calling sleep_next_period().
+ * A proper solution would requiring adding another callback
+ * in the plugin structure and testing the condition with
+ * interrupts disabled.
+ *
+ * FIXME: At least problem 2 should be taken care of eventually.
+ */
+ while (!retval && job > current->rt_param.job_params.job_no)
+ /* If the last job overran then job <= job_no and we
+ * don't send the task to sleep.
+ */
+ retval = litmus->complete_job();
+ out:
+ return retval;
+}
+
+/* This is a helper syscall to query the current job sequence number.
+ *
+ * returns 0 on successful query
+ * returns EPERM if task is not a real-time task.
+ * returns EFAULT if &job is not a valid pointer.
+ */
+asmlinkage long sys_query_job_no(unsigned int __user *job)
+{
+ int retval = -EPERM;
+ if (is_realtime(current))
+ retval = put_user(current->rt_param.job_params.job_no, job);
+
+ return retval;
+}
+
+/* sys_null_call() is only used for determining raw system call
+ * overheads (kernel entry, kernel exit). It has no useful side effects.
+ * If ts is non-NULL, then the current Feather-Trace time is recorded.
+ */
+asmlinkage long sys_null_call(cycles_t __user *ts)
+{
+ long ret = 0;
+ cycles_t now;
+
+ if (ts) {
+ now = get_cycles();
+ ret = put_user(now, ts);
+ }
+
+ return ret;
+}
+
+/* p is a real-time task. Re-init its state as a best-effort task. */
+static void reinit_litmus_state(struct task_struct* p, int restore)
+{
+ struct rt_task user_config = {};
+ void* ctrl_page = NULL;
+
+ if (restore) {
+ /* Safe user-space provided configuration data.
+ * and allocated page. */
+ user_config = p->rt_param.task_params;
+ ctrl_page = p->rt_param.ctrl_page;
+ }
+
+ /* We probably should not be inheriting any task's priority
+ * at this point in time.
+ */
+ WARN_ON(p->rt_param.inh_task);
+
+ /* We need to restore the priority of the task. */
+// __setscheduler(p, p->rt_param.old_policy, p->rt_param.old_prio); XXX why is this commented?
+
+ /* Cleanup everything else. */
+ memset(&p->rt_param, 0, sizeof(p->rt_param));
+
+ /* Restore preserved fields. */
+ if (restore) {
+ p->rt_param.task_params = user_config;
+ p->rt_param.ctrl_page = ctrl_page;
+ }
+}
+
+long litmus_admit_task(struct task_struct* tsk)
+{
+ long retval = 0;
+ unsigned long flags;
+
+ BUG_ON(is_realtime(tsk));
+
+ if (get_rt_period(tsk) == 0 ||
+ get_exec_cost(tsk) > get_rt_period(tsk)) {
+ TRACE_TASK(tsk, "litmus admit: invalid task parameters "
+ "(%lu, %lu)\n",
+ get_exec_cost(tsk), get_rt_period(tsk));
+ retval = -EINVAL;
+ goto out;
+ }
+
+ if (!cpu_online(get_partition(tsk))) {
+ TRACE_TASK(tsk, "litmus admit: cpu %d is not online\n",
+ get_partition(tsk));
+ retval = -EINVAL;
+ goto out;
+ }
+
+ INIT_LIST_HEAD(&tsk_rt(tsk)->list);
+
+ /* avoid scheduler plugin changing underneath us */
+ raw_spin_lock_irqsave(&task_transition_lock, flags);
+
+ /* allocate heap node for this task */
+ tsk_rt(tsk)->heap_node = bheap_node_alloc(GFP_ATOMIC);
+ tsk_rt(tsk)->rel_heap = release_heap_alloc(GFP_ATOMIC);
+
+ if (!tsk_rt(tsk)->heap_node || !tsk_rt(tsk)->rel_heap) {
+ printk(KERN_WARNING "litmus: no more heap node memory!?\n");
+
+ bheap_node_free(tsk_rt(tsk)->heap_node);
+ release_heap_free(tsk_rt(tsk)->rel_heap);
+
+ retval = -ENOMEM;
+ goto out_unlock;
+ } else {
+ bheap_node_init(&tsk_rt(tsk)->heap_node, tsk);
+ }
+
+ retval = litmus->admit_task(tsk);
+
+ if (!retval) {
+ sched_trace_task_name(tsk);
+ sched_trace_task_param(tsk);
+ atomic_inc(&rt_task_count);
+ }
+
+out_unlock:
+ raw_spin_unlock_irqrestore(&task_transition_lock, flags);
+out:
+ return retval;
+}
+
+void litmus_exit_task(struct task_struct* tsk)
+{
+ if (is_realtime(tsk)) {
+ sched_trace_task_completion(tsk, 1);
+
+ litmus->task_exit(tsk);
+
+ BUG_ON(bheap_node_in_heap(tsk_rt(tsk)->heap_node));
+ bheap_node_free(tsk_rt(tsk)->heap_node);
+ release_heap_free(tsk_rt(tsk)->rel_heap);
+
+ atomic_dec(&rt_task_count);
+ reinit_litmus_state(tsk, 1);
+ }
+}
+
+/* IPI callback to synchronize plugin switching */
+static void synch_on_plugin_switch(void* info)
+{
+ atomic_inc(&cannot_use_plugin);
+ while (atomic_read(&cannot_use_plugin) > 0)
+ cpu_relax();
+}
+
+/* Switching a plugin in use is tricky.
+ * We must watch out that no real-time tasks exists
+ * (and that none is created in parallel) and that the plugin is not
+ * currently in use on any processor (in theory).
+ */
+int switch_sched_plugin(struct sched_plugin* plugin)
+{
+ unsigned long flags;
+ int ret = 0;
+
+ BUG_ON(!plugin);
+
+ /* forbid other cpus to use the plugin */
+ atomic_set(&cannot_use_plugin, 1);
+ /* send IPI to force other CPUs to synch with us */
+ smp_call_function(synch_on_plugin_switch, NULL, 0);
+
+ /* wait until all other CPUs have started synch */
+ while (atomic_read(&cannot_use_plugin) < num_online_cpus())
+ cpu_relax();
+
+ /* stop task transitions */
+ raw_spin_lock_irqsave(&task_transition_lock, flags);
+
+ /* don't switch if there are active real-time tasks */
+ if (atomic_read(&rt_task_count) == 0) {
+ ret = litmus->deactivate_plugin();
+ if (0 != ret)
+ goto out;
+ ret = plugin->activate_plugin();
+ if (0 != ret) {
+ printk(KERN_INFO "Can't activate %s (%d).\n",
+ plugin->plugin_name, ret);
+ plugin = &linux_sched_plugin;
+ }
+ printk(KERN_INFO "Switching to LITMUS^RT plugin %s.\n", plugin->plugin_name);
+ litmus = plugin;
+ } else
+ ret = -EBUSY;
+out:
+ raw_spin_unlock_irqrestore(&task_transition_lock, flags);
+ atomic_set(&cannot_use_plugin, 0);
+ return ret;
+}
+
+/* Called upon fork.
+ * p is the newly forked task.
+ */
+void litmus_fork(struct task_struct* p)
+{
+ if (is_realtime(p))
+ /* clean out any litmus related state, don't preserve anything */
+ reinit_litmus_state(p, 0);
+ else
+ /* non-rt tasks might have ctrl_page set */
+ tsk_rt(p)->ctrl_page = NULL;
+
+ /* od tables are never inherited across a fork */
+ p->od_table = NULL;
+}
+
+/* Called upon execve().
+ * current is doing the exec.
+ * Don't let address space specific stuff leak.
+ */
+void litmus_exec(void)
+{
+ struct task_struct* p = current;
+
+ if (is_realtime(p)) {
+ WARN_ON(p->rt_param.inh_task);
+ if (tsk_rt(p)->ctrl_page) {
+ free_page((unsigned long) tsk_rt(p)->ctrl_page);
+ tsk_rt(p)->ctrl_page = NULL;
+ }
+ }
+}
+
+void exit_litmus(struct task_struct *dead_tsk)
+{
+ /* We also allow non-RT tasks to
+ * allocate control pages to allow
+ * measurements with non-RT tasks.
+ * So check if we need to free the page
+ * in any case.
+ */
+ if (tsk_rt(dead_tsk)->ctrl_page) {
+ TRACE_TASK(dead_tsk,
+ "freeing ctrl_page %p\n",
+ tsk_rt(dead_tsk)->ctrl_page);
+ free_page((unsigned long) tsk_rt(dead_tsk)->ctrl_page);
+ }
+
+ /* main cleanup only for RT tasks */
+ if (is_realtime(dead_tsk))
+ litmus_exit_task(dead_tsk);
+}
+
+
+#ifdef CONFIG_MAGIC_SYSRQ
+int sys_kill(int pid, int sig);
+
+static void sysrq_handle_kill_rt_tasks(int key)
+{
+ struct task_struct *t;
+ read_lock(&tasklist_lock);
+ for_each_process(t) {
+ if (is_realtime(t)) {
+ sys_kill(t->pid, SIGKILL);
+ }
+ }
+ read_unlock(&tasklist_lock);
+}
+
+static struct sysrq_key_op sysrq_kill_rt_tasks_op = {
+ .handler = sysrq_handle_kill_rt_tasks,
+ .help_msg = "quit-rt-tasks(X)",
+ .action_msg = "sent SIGKILL to all LITMUS^RT real-time tasks",
+};
+#endif
+
+extern struct sched_plugin linux_sched_plugin;
+
+static int __init _init_litmus(void)
+{
+ /* Common initializers,
+ * mode change lock is used to enforce single mode change
+ * operation.
+ */
+ printk("Starting LITMUS^RT kernel\n");
+
+ register_sched_plugin(&linux_sched_plugin);
+
+ bheap_node_cache = KMEM_CACHE(bheap_node, SLAB_PANIC);
+ release_heap_cache = KMEM_CACHE(release_heap, SLAB_PANIC);
+
+#ifdef CONFIG_MAGIC_SYSRQ
+ /* offer some debugging help */
+ if (!register_sysrq_key('x', &sysrq_kill_rt_tasks_op))
+ printk("Registered kill rt tasks magic sysrq.\n");
+ else
+ printk("Could not register kill rt tasks magic sysrq.\n");
+#endif
+
+ init_litmus_proc();
+
+ return 0;
+}
+
+static void _exit_litmus(void)
+{
+ exit_litmus_proc();
+ kmem_cache_destroy(bheap_node_cache);
+ kmem_cache_destroy(release_heap_cache);
+}
+
+module_init(_init_litmus);
+module_exit(_exit_litmus);
diff --git a/litmus/litmus_proc.c b/litmus/litmus_proc.c
new file mode 100644
index 0000000..81ea5c3
--- /dev/null
+++ b/litmus/litmus_proc.c
@@ -0,0 +1,259 @@
+/*
+ * litmus_proc.c -- Implementation of the /proc/litmus directory tree.
+ */
+
+#include <linux/sched.h>
+#include <linux/uaccess.h>
+
+#include <litmus/litmus.h>
+#include <litmus/litmus_proc.h>
+
+/* in litmus/litmus.c */
+extern atomic_t rt_task_count;
+
+static struct proc_dir_entry *litmus_dir = NULL,
+ *curr_file = NULL,
+ *stat_file = NULL,
+ *plugs_dir = NULL,
+#ifdef CONFIG_RELEASE_MASTER
+ *release_master_file = NULL,
+#endif
+ *plugs_file = NULL;
+
+/* in litmus/sync.c */
+int count_tasks_waiting_for_release(void);
+
+static int proc_read_stats(char *page, char **start,
+ off_t off, int count,
+ int *eof, void *data)
+{
+ int len;
+
+ len = snprintf(page, PAGE_SIZE,
+ "real-time tasks = %d\n"
+ "ready for release = %d\n",
+ atomic_read(&rt_task_count),
+ count_tasks_waiting_for_release());
+ return len;
+}
+
+static int proc_read_plugins(char *page, char **start,
+ off_t off, int count,
+ int *eof, void *data)
+{
+ int len;
+
+ len = print_sched_plugins(page, PAGE_SIZE);
+ return len;
+}
+
+static int proc_read_curr(char *page, char **start,
+ off_t off, int count,
+ int *eof, void *data)
+{
+ int len;
+
+ len = snprintf(page, PAGE_SIZE, "%s\n", litmus->plugin_name);
+ return len;
+}
+
+/* in litmus/litmus.c */
+int switch_sched_plugin(struct sched_plugin*);
+
+static int proc_write_curr(struct file *file,
+ const char *buffer,
+ unsigned long count,
+ void *data)
+{
+ int len, ret;
+ char name[65];
+ struct sched_plugin* found;
+
+ if(count > 64)
+ len = 64;
+ else
+ len = count;
+
+ if(copy_from_user(name, buffer, len))
+ return -EFAULT;
+
+ name[len] = '\0';
+ /* chomp name */
+ if (len > 1 && name[len - 1] == '\n')
+ name[len - 1] = '\0';
+
+ found = find_sched_plugin(name);
+
+ if (found) {
+ ret = switch_sched_plugin(found);
+ if (ret != 0)
+ printk(KERN_INFO "Could not switch plugin: %d\n", ret);
+ } else
+ printk(KERN_INFO "Plugin '%s' is unknown.\n", name);
+
+ return len;
+}
+
+#ifdef CONFIG_RELEASE_MASTER
+static int proc_read_release_master(char *page, char **start,
+ off_t off, int count,
+ int *eof, void *data)
+{
+ int len, master;
+ master = atomic_read(&release_master_cpu);
+ if (master == NO_CPU)
+ len = snprintf(page, PAGE_SIZE, "NO_CPU\n");
+ else
+ len = snprintf(page, PAGE_SIZE, "%d\n", master);
+ return len;
+}
+
+static int proc_write_release_master(struct file *file,
+ const char *buffer,
+ unsigned long count,
+ void *data)
+{
+ int cpu, err, online = 0;
+ char msg[64];
+
+ if (count > 63)
+ return -EINVAL;
+
+ if (copy_from_user(msg, buffer, count))
+ return -EFAULT;
+
+ /* terminate */
+ msg[count] = '\0';
+ /* chomp */
+ if (count > 1 && msg[count - 1] == '\n')
+ msg[count - 1] = '\0';
+
+ if (strcmp(msg, "NO_CPU") == 0) {
+ atomic_set(&release_master_cpu, NO_CPU);
+ return count;
+ } else {
+ err = sscanf(msg, "%d", &cpu);
+ if (err == 1 && cpu >= 0 && (online = cpu_online(cpu))) {
+ atomic_set(&release_master_cpu, cpu);
+ return count;
+ } else {
+ TRACE("invalid release master: '%s' "
+ "(err:%d cpu:%d online:%d)\n",
+ msg, err, cpu, online);
+ return -EINVAL;
+ }
+ }
+}
+#endif
+
+int __init init_litmus_proc(void)
+{
+ litmus_dir = proc_mkdir("litmus", NULL);
+ if (!litmus_dir) {
+ printk(KERN_ERR "Could not allocate LITMUS^RT procfs entry.\n");
+ return -ENOMEM;
+ }
+
+ curr_file = create_proc_entry("active_plugin",
+ 0644, litmus_dir);
+ if (!curr_file) {
+ printk(KERN_ERR "Could not allocate active_plugin "
+ "procfs entry.\n");
+ return -ENOMEM;
+ }
+ curr_file->read_proc = proc_read_curr;
+ curr_file->write_proc = proc_write_curr;
+
+#ifdef CONFIG_RELEASE_MASTER
+ release_master_file = create_proc_entry("release_master",
+ 0644, litmus_dir);
+ if (!release_master_file) {
+ printk(KERN_ERR "Could not allocate release_master "
+ "procfs entry.\n");
+ return -ENOMEM;
+ }
+ release_master_file->read_proc = proc_read_release_master;
+ release_master_file->write_proc = proc_write_release_master;
+#endif
+
+ stat_file = create_proc_read_entry("stats", 0444, litmus_dir,
+ proc_read_stats, NULL);
+
+ plugs_dir = proc_mkdir("plugins", litmus_dir);
+ if (!plugs_dir){
+ printk(KERN_ERR "Could not allocate plugins directory "
+ "procfs entry.\n");
+ return -ENOMEM;
+ }
+
+ plugs_file = create_proc_read_entry("loaded", 0444, plugs_dir,
+ proc_read_plugins, NULL);
+
+ return 0;
+}
+
+void exit_litmus_proc(void)
+{
+ if (plugs_file)
+ remove_proc_entry("loaded", plugs_dir);
+ if (plugs_dir)
+ remove_proc_entry("plugins", litmus_dir);
+ if (stat_file)
+ remove_proc_entry("stats", litmus_dir);
+ if (curr_file)
+ remove_proc_entry("active_plugin", litmus_dir);
+#ifdef CONFIG_RELEASE_MASTER
+ if (release_master_file)
+ remove_proc_entry("release_master", litmus_dir);
+#endif
+ if (litmus_dir)
+ remove_proc_entry("litmus", NULL);
+}
+
+long make_plugin_proc_dir(struct sched_plugin* plugin,
+ struct proc_dir_entry** pde_in)
+{
+ struct proc_dir_entry *pde_new = NULL;
+ long rv;
+
+ if (!plugin || !plugin->plugin_name){
+ printk(KERN_ERR "Invalid plugin struct passed to %s.\n",
+ __func__);
+ rv = -EINVAL;
+ goto out_no_pde;
+ }
+
+ if (!plugs_dir){
+ printk(KERN_ERR "Could not make plugin sub-directory, because "
+ "/proc/litmus/plugins does not exist.\n");
+ rv = -ENOENT;
+ goto out_no_pde;
+ }
+
+ pde_new = proc_mkdir(plugin->plugin_name, plugs_dir);
+ if (!pde_new){
+ printk(KERN_ERR "Could not make plugin sub-directory: "
+ "out of memory?.\n");
+ rv = -ENOMEM;
+ goto out_no_pde;
+ }
+
+ rv = 0;
+ *pde_in = pde_new;
+ goto out_ok;
+
+out_no_pde:
+ *pde_in = NULL;
+out_ok:
+ return rv;
+}
+
+void remove_plugin_proc_dir(struct sched_plugin* plugin)
+{
+ if (!plugin || !plugin->plugin_name){
+ printk(KERN_ERR "Invalid plugin struct passed to %s.\n",
+ __func__);
+ return;
+ }
+ remove_proc_entry(plugin->plugin_name, plugs_dir);
+}
diff --git a/litmus/preempt.c b/litmus/preempt.c
new file mode 100644
index 0000000..ebe2e34
--- /dev/null
+++ b/litmus/preempt.c
@@ -0,0 +1,131 @@
+#include <linux/sched.h>
+
+#include <litmus/litmus.h>
+#include <litmus/preempt.h>
+
+/* The rescheduling state of each processor.
+ */
+DEFINE_PER_CPU_SHARED_ALIGNED(atomic_t, resched_state);
+
+void sched_state_will_schedule(struct task_struct* tsk)
+{
+ /* Litmus hack: we only care about processor-local invocations of
+ * set_tsk_need_resched(). We can't reliably set the flag remotely
+ * since it might race with other updates to the scheduling state. We
+ * can't rely on the runqueue lock protecting updates to the sched
+ * state since processors do not acquire the runqueue locks for all
+ * updates to the sched state (to avoid acquiring two runqueue locks at
+ * the same time). Further, if tsk is residing on a remote processor,
+ * then that processor doesn't actually know yet that it is going to
+ * reschedule; it still must receive an IPI (unless a local invocation
+ * races).
+ */
+ if (likely(task_cpu(tsk) == smp_processor_id())) {
+ VERIFY_SCHED_STATE(TASK_SCHEDULED | SHOULD_SCHEDULE | TASK_PICKED | WILL_SCHEDULE);
+ if (is_in_sched_state(TASK_PICKED | PICKED_WRONG_TASK))
+ set_sched_state(PICKED_WRONG_TASK);
+ else
+ set_sched_state(WILL_SCHEDULE);
+ } else
+ /* Litmus tasks should never be subject to a remote
+ * set_tsk_need_resched(). */
+ BUG_ON(is_realtime(tsk));
+ TRACE_TASK(tsk, "set_tsk_need_resched() ret:%p\n",
+ __builtin_return_address(0));
+}
+
+/* Called by the IPI handler after another CPU called smp_send_resched(). */
+void sched_state_ipi(void)
+{
+ /* If the IPI was slow, we might be in any state right now. The IPI is
+ * only meaningful if we are in SHOULD_SCHEDULE. */
+ if (is_in_sched_state(SHOULD_SCHEDULE)) {
+ /* Cause scheduler to be invoked.
+ * This will cause a transition to WILL_SCHEDULE. */
+ set_tsk_need_resched(current);
+ TRACE_STATE("IPI -> set_tsk_need_resched(%s/%d)\n",
+ current->comm, current->pid);
+ } else {
+ /* ignore */
+ TRACE_STATE("ignoring IPI in state %x (%s)\n",
+ get_sched_state(),
+ sched_state_name(get_sched_state()));
+ }
+}
+
+/* Called by plugins to cause a CPU to reschedule. IMPORTANT: the caller must
+ * hold the lock that is used to serialize scheduling decisions. */
+void litmus_reschedule(int cpu)
+{
+ int picked_transition_ok = 0;
+ int scheduled_transition_ok = 0;
+
+ /* The (remote) CPU could be in any state. */
+
+ /* The critical states are TASK_PICKED and TASK_SCHEDULED, as the CPU
+ * is not aware of the need to reschedule at this point. */
+
+ /* is a context switch in progress? */
+ if (cpu_is_in_sched_state(cpu, TASK_PICKED))
+ picked_transition_ok = sched_state_transition_on(
+ cpu, TASK_PICKED, PICKED_WRONG_TASK);
+
+ if (!picked_transition_ok &&
+ cpu_is_in_sched_state(cpu, TASK_SCHEDULED)) {
+ /* We either raced with the end of the context switch, or the
+ * CPU was in TASK_SCHEDULED anyway. */
+ scheduled_transition_ok = sched_state_transition_on(
+ cpu, TASK_SCHEDULED, SHOULD_SCHEDULE);
+ }
+
+ /* If the CPU was in state TASK_SCHEDULED, then we need to cause the
+ * scheduler to be invoked. */
+ if (scheduled_transition_ok) {
+ if (smp_processor_id() == cpu)
+ set_tsk_need_resched(current);
+ else
+ smp_send_reschedule(cpu);
+ }
+
+ TRACE_STATE("%s picked-ok:%d sched-ok:%d\n",
+ __FUNCTION__,
+ picked_transition_ok,
+ scheduled_transition_ok);
+}
+
+void litmus_reschedule_local(void)
+{
+ if (is_in_sched_state(TASK_PICKED))
+ set_sched_state(PICKED_WRONG_TASK);
+ else if (is_in_sched_state(TASK_SCHEDULED | SHOULD_SCHEDULE)) {
+ set_sched_state(WILL_SCHEDULE);
+ set_tsk_need_resched(current);
+ }
+}
+
+#ifdef CONFIG_DEBUG_KERNEL
+
+void sched_state_plugin_check(void)
+{
+ if (!is_in_sched_state(TASK_PICKED | PICKED_WRONG_TASK)) {
+ TRACE("!!!! plugin did not call sched_state_task_picked()!"
+ "Calling sched_state_task_picked() is mandatory---fix this.\n");
+ set_sched_state(TASK_PICKED);
+ }
+}
+
+#define NAME_CHECK(x) case x: return #x
+const char* sched_state_name(int s)
+{
+ switch (s) {
+ NAME_CHECK(TASK_SCHEDULED);
+ NAME_CHECK(SHOULD_SCHEDULE);
+ NAME_CHECK(WILL_SCHEDULE);
+ NAME_CHECK(TASK_PICKED);
+ NAME_CHECK(PICKED_WRONG_TASK);
+ default:
+ return "UNKNOWN";
+ };
+}
+
+#endif
diff --git a/litmus/rt_domain.c b/litmus/rt_domain.c
new file mode 100644
index 0000000..81a5ac1
--- /dev/null
+++ b/litmus/rt_domain.c
@@ -0,0 +1,355 @@
+/*
+ * litmus/rt_domain.c
+ *
+ * LITMUS real-time infrastructure. This file contains the
+ * functions that manipulate RT domains. RT domains are an abstraction
+ * of a ready queue and a release queue.
+ */
+
+#include <linux/percpu.h>
+#include <linux/sched.h>
+#include <linux/list.h>
+#include <linux/slab.h>
+
+#include <litmus/litmus.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/sched_trace.h>
+
+#include <litmus/rt_domain.h>
+
+#include <litmus/trace.h>
+
+#include <litmus/bheap.h>
+
+/* Uncomment when debugging timer races... */
+#if 0
+#define VTRACE_TASK TRACE_TASK
+#define VTRACE TRACE
+#else
+#define VTRACE_TASK(t, fmt, args...) /* shut up */
+#define VTRACE(fmt, args...) /* be quiet already */
+#endif
+
+static int dummy_resched(rt_domain_t *rt)
+{
+ return 0;
+}
+
+static int dummy_order(struct bheap_node* a, struct bheap_node* b)
+{
+ return 0;
+}
+
+/* default implementation: use default lock */
+static void default_release_jobs(rt_domain_t* rt, struct bheap* tasks)
+{
+ merge_ready(rt, tasks);
+}
+
+static unsigned int time2slot(lt_t time)
+{
+ return (unsigned int) time2quanta(time, FLOOR) % RELEASE_QUEUE_SLOTS;
+}
+
+static enum hrtimer_restart on_release_timer(struct hrtimer *timer)
+{
+ unsigned long flags;
+ struct release_heap* rh;
+
+ VTRACE("on_release_timer(0x%p) starts.\n", timer);
+
+ TS_RELEASE_START;
+
+ rh = container_of(timer, struct release_heap, timer);
+
+ raw_spin_lock_irqsave(&rh->dom->release_lock, flags);
+ VTRACE("CB has the release_lock 0x%p\n", &rh->dom->release_lock);
+ /* remove from release queue */
+ list_del(&rh->list);
+ raw_spin_unlock_irqrestore(&rh->dom->release_lock, flags);
+ VTRACE("CB returned release_lock 0x%p\n", &rh->dom->release_lock);
+
+ /* call release callback */
+ rh->dom->release_jobs(rh->dom, &rh->heap);
+ /* WARNING: rh can be referenced from other CPUs from now on. */
+
+ TS_RELEASE_END;
+
+ VTRACE("on_release_timer(0x%p) ends.\n", timer);
+
+ return HRTIMER_NORESTART;
+}
+
+/* allocated in litmus.c */
+struct kmem_cache * release_heap_cache;
+
+struct release_heap* release_heap_alloc(int gfp_flags)
+{
+ struct release_heap* rh;
+ rh= kmem_cache_alloc(release_heap_cache, gfp_flags);
+ if (rh) {
+ /* initialize timer */
+ hrtimer_init(&rh->timer, CLOCK_MONOTONIC, HRTIMER_MODE_ABS);
+ rh->timer.function = on_release_timer;
+ }
+ return rh;
+}
+
+void release_heap_free(struct release_heap* rh)
+{
+ /* make sure timer is no longer in use */
+ hrtimer_cancel(&rh->timer);
+ kmem_cache_free(release_heap_cache, rh);
+}
+
+/* Caller must hold release lock.
+ * Will return heap for given time. If no such heap exists prior to
+ * the invocation it will be created.
+ */
+static struct release_heap* get_release_heap(rt_domain_t *rt,
+ struct task_struct* t,
+ int use_task_heap)
+{
+ struct list_head* pos;
+ struct release_heap* heap = NULL;
+ struct release_heap* rh;
+ lt_t release_time = get_release(t);
+ unsigned int slot = time2slot(release_time);
+
+ /* initialize pos for the case that the list is empty */
+ pos = rt->release_queue.slot[slot].next;
+ list_for_each(pos, &rt->release_queue.slot[slot]) {
+ rh = list_entry(pos, struct release_heap, list);
+ if (release_time == rh->release_time) {
+ /* perfect match -- this happens on hyperperiod
+ * boundaries
+ */
+ heap = rh;
+ break;
+ } else if (lt_before(release_time, rh->release_time)) {
+ /* we need to insert a new node since rh is
+ * already in the future
+ */
+ break;
+ }
+ }
+ if (!heap && use_task_heap) {
+ /* use pre-allocated release heap */
+ rh = tsk_rt(t)->rel_heap;
+
+ rh->dom = rt;
+ rh->release_time = release_time;
+
+ /* add to release queue */
+ list_add(&rh->list, pos->prev);
+ heap = rh;
+ }
+ return heap;
+}
+
+static void reinit_release_heap(struct task_struct* t)
+{
+ struct release_heap* rh;
+
+ /* use pre-allocated release heap */
+ rh = tsk_rt(t)->rel_heap;
+
+ /* Make sure it is safe to use. The timer callback could still
+ * be executing on another CPU; hrtimer_cancel() will wait
+ * until the timer callback has completed. However, under no
+ * circumstances should the timer be active (= yet to be
+ * triggered).
+ *
+ * WARNING: If the CPU still holds the release_lock at this point,
+ * deadlock may occur!
+ */
+ BUG_ON(hrtimer_cancel(&rh->timer));
+
+ /* initialize */
+ bheap_init(&rh->heap);
+#ifdef CONFIG_RELEASE_MASTER
+ atomic_set(&rh->info.state, HRTIMER_START_ON_INACTIVE);
+#endif
+}
+/* arm_release_timer() - start local release timer or trigger
+ * remote timer (pull timer)
+ *
+ * Called by add_release() with:
+ * - tobe_lock taken
+ * - IRQ disabled
+ */
+#ifdef CONFIG_RELEASE_MASTER
+#define arm_release_timer(t) arm_release_timer_on((t), NO_CPU)
+static void arm_release_timer_on(rt_domain_t *_rt , int target_cpu)
+#else
+static void arm_release_timer(rt_domain_t *_rt)
+#endif
+{
+ rt_domain_t *rt = _rt;
+ struct list_head list;
+ struct list_head *pos, *safe;
+ struct task_struct* t;
+ struct release_heap* rh;
+
+ VTRACE("arm_release_timer() at %llu\n", litmus_clock());
+ list_replace_init(&rt->tobe_released, &list);
+
+ list_for_each_safe(pos, safe, &list) {
+ /* pick task of work list */
+ t = list_entry(pos, struct task_struct, rt_param.list);
+ sched_trace_task_release(t);
+ list_del(pos);
+
+ /* put into release heap while holding release_lock */
+ raw_spin_lock(&rt->release_lock);
+ VTRACE_TASK(t, "I have the release_lock 0x%p\n", &rt->release_lock);
+
+ rh = get_release_heap(rt, t, 0);
+ if (!rh) {
+ /* need to use our own, but drop lock first */
+ raw_spin_unlock(&rt->release_lock);
+ VTRACE_TASK(t, "Dropped release_lock 0x%p\n",
+ &rt->release_lock);
+
+ reinit_release_heap(t);
+ VTRACE_TASK(t, "release_heap ready\n");
+
+ raw_spin_lock(&rt->release_lock);
+ VTRACE_TASK(t, "Re-acquired release_lock 0x%p\n",
+ &rt->release_lock);
+
+ rh = get_release_heap(rt, t, 1);
+ }
+ bheap_insert(rt->order, &rh->heap, tsk_rt(t)->heap_node);
+ VTRACE_TASK(t, "arm_release_timer(): added to release heap\n");
+
+ raw_spin_unlock(&rt->release_lock);
+ VTRACE_TASK(t, "Returned the release_lock 0x%p\n", &rt->release_lock);
+
+ /* To avoid arming the timer multiple times, we only let the
+ * owner do the arming (which is the "first" task to reference
+ * this release_heap anyway).
+ */
+ if (rh == tsk_rt(t)->rel_heap) {
+ VTRACE_TASK(t, "arming timer 0x%p\n", &rh->timer);
+ /* we cannot arm the timer using hrtimer_start()
+ * as it may deadlock on rq->lock
+ *
+ * PINNED mode is ok on both local and remote CPU
+ */
+#ifdef CONFIG_RELEASE_MASTER
+ if (rt->release_master == NO_CPU &&
+ target_cpu == NO_CPU)
+#endif
+ __hrtimer_start_range_ns(&rh->timer,
+ ns_to_ktime(rh->release_time),
+ 0, HRTIMER_MODE_ABS_PINNED, 0);
+#ifdef CONFIG_RELEASE_MASTER
+ else
+ hrtimer_start_on(
+ /* target_cpu overrides release master */
+ (target_cpu != NO_CPU ?
+ target_cpu : rt->release_master),
+ &rh->info, &rh->timer,
+ ns_to_ktime(rh->release_time),
+ HRTIMER_MODE_ABS_PINNED);
+#endif
+ } else
+ VTRACE_TASK(t, "0x%p is not my timer\n", &rh->timer);
+ }
+}
+
+void rt_domain_init(rt_domain_t *rt,
+ bheap_prio_t order,
+ check_resched_needed_t check,
+ release_jobs_t release
+ )
+{
+ int i;
+
+ BUG_ON(!rt);
+ if (!check)
+ check = dummy_resched;
+ if (!release)
+ release = default_release_jobs;
+ if (!order)
+ order = dummy_order;
+
+#ifdef CONFIG_RELEASE_MASTER
+ rt->release_master = NO_CPU;
+#endif
+
+ bheap_init(&rt->ready_queue);
+ INIT_LIST_HEAD(&rt->tobe_released);
+ for (i = 0; i < RELEASE_QUEUE_SLOTS; i++)
+ INIT_LIST_HEAD(&rt->release_queue.slot[i]);
+
+ raw_spin_lock_init(&rt->ready_lock);
+ raw_spin_lock_init(&rt->release_lock);
+ raw_spin_lock_init(&rt->tobe_lock);
+
+ rt->check_resched = check;
+ rt->release_jobs = release;
+ rt->order = order;
+}
+
+/* add_ready - add a real-time task to the rt ready queue. It must be runnable.
+ * @new: the newly released task
+ */
+void __add_ready(rt_domain_t* rt, struct task_struct *new)
+{
+ TRACE("rt: adding %s/%d (%llu, %llu) rel=%llu to ready queue at %llu\n",
+ new->comm, new->pid, get_exec_cost(new), get_rt_period(new),
+ get_release(new), litmus_clock());
+
+ BUG_ON(bheap_node_in_heap(tsk_rt(new)->heap_node));
+
+ bheap_insert(rt->order, &rt->ready_queue, tsk_rt(new)->heap_node);
+ rt->check_resched(rt);
+}
+
+/* merge_ready - Add a sorted set of tasks to the rt ready queue. They must be runnable.
+ * @tasks - the newly released tasks
+ */
+void __merge_ready(rt_domain_t* rt, struct bheap* tasks)
+{
+ bheap_union(rt->order, &rt->ready_queue, tasks);
+ rt->check_resched(rt);
+}
+
+
+#ifdef CONFIG_RELEASE_MASTER
+void __add_release_on(rt_domain_t* rt, struct task_struct *task,
+ int target_cpu)
+{
+ TRACE_TASK(task, "add_release_on(), rel=%llu, target=%d\n",
+ get_release(task), target_cpu);
+ list_add(&tsk_rt(task)->list, &rt->tobe_released);
+ task->rt_param.domain = rt;
+
+ /* start release timer */
+ TS_SCHED2_START(task);
+
+ arm_release_timer_on(rt, target_cpu);
+
+ TS_SCHED2_END(task);
+}
+#endif
+
+/* add_release - add a real-time task to the rt release queue.
+ * @task: the sleeping task
+ */
+void __add_release(rt_domain_t* rt, struct task_struct *task)
+{
+ TRACE_TASK(task, "add_release(), rel=%llu\n", get_release(task));
+ list_add(&tsk_rt(task)->list, &rt->tobe_released);
+ task->rt_param.domain = rt;
+
+ /* start release timer */
+ TS_SCHED2_START(task);
+
+ arm_release_timer(rt);
+
+ TS_SCHED2_END(task);
+}
+
diff --git a/litmus/sched_cedf.c b/litmus/sched_cedf.c
new file mode 100644
index 0000000..098a449
--- /dev/null
+++ b/litmus/sched_cedf.c
@@ -0,0 +1,873 @@
+/*
+ * litmus/sched_cedf.c
+ *
+ * Implementation of the C-EDF scheduling algorithm.
+ *
+ * This implementation is based on G-EDF:
+ * - CPUs are clustered around L2 or L3 caches.
+ * - Clusters topology is automatically detected (this is arch dependent
+ * and is working only on x86 at the moment --- and only with modern
+ * cpus that exports cpuid4 information)
+ * - The plugins _does not_ attempt to put tasks in the right cluster i.e.
+ * the programmer needs to be aware of the topology to place tasks
+ * in the desired cluster
+ * - default clustering is around L2 cache (cache index = 2)
+ * supported clusters are: L1 (private cache: pedf), L2, L3, ALL (all
+ * online_cpus are placed in a single cluster).
+ *
+ * For details on functions, take a look at sched_gsn_edf.c
+ *
+ * Currently, we do not support changes in the number of online cpus.
+ * If the num_online_cpus() dynamically changes, the plugin is broken.
+ *
+ * This version uses the simple approach and serializes all scheduling
+ * decisions by the use of a queue lock. This is probably not the
+ * best way to do it, but it should suffice for now.
+ */
+
+#include <linux/spinlock.h>
+#include <linux/percpu.h>
+#include <linux/sched.h>
+#include <linux/slab.h>
+
+#include <linux/module.h>
+
+#include <litmus/litmus.h>
+#include <litmus/jobs.h>
+#include <litmus/preempt.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/edf_common.h>
+#include <litmus/sched_trace.h>
+
+#include <litmus/bheap.h>
+
+/* to configure the cluster size */
+#include <litmus/litmus_proc.h>
+#include <linux/uaccess.h>
+
+/* Reference configuration variable. Determines which cache level is used to
+ * group CPUs into clusters. GLOBAL_CLUSTER, which is the default, means that
+ * all CPUs form a single cluster (just like GSN-EDF).
+ */
+static enum {
+ GLOBAL_CLUSTER = 0,
+ L1_CLUSTER = 1,
+ L2_CLUSTER = 2,
+ L3_CLUSTER = 3
+} cluster_config = GLOBAL_CLUSTER;
+
+struct clusterdomain;
+
+/* cpu_entry_t - maintain the linked and scheduled state
+ *
+ * A cpu also contains a pointer to the cedf_domain_t cluster
+ * that owns it (struct clusterdomain*)
+ */
+typedef struct {
+ int cpu;
+ struct clusterdomain* cluster; /* owning cluster */
+ struct task_struct* linked; /* only RT tasks */
+ struct task_struct* scheduled; /* only RT tasks */
+ atomic_t will_schedule; /* prevent unneeded IPIs */
+ struct bheap_node* hn;
+} cpu_entry_t;
+
+/* one cpu_entry_t per CPU */
+DEFINE_PER_CPU(cpu_entry_t, cedf_cpu_entries);
+
+#define set_will_schedule() \
+ (atomic_set(&__get_cpu_var(cedf_cpu_entries).will_schedule, 1))
+#define clear_will_schedule() \
+ (atomic_set(&__get_cpu_var(cedf_cpu_entries).will_schedule, 0))
+#define test_will_schedule(cpu) \
+ (atomic_read(&per_cpu(cedf_cpu_entries, cpu).will_schedule))
+
+/*
+ * In C-EDF there is a cedf domain _per_ cluster
+ * The number of clusters is dynamically determined accordingly to the
+ * total cpu number and the cluster size
+ */
+typedef struct clusterdomain {
+ /* rt_domain for this cluster */
+ rt_domain_t domain;
+ /* cpus in this cluster */
+ cpu_entry_t* *cpus;
+ /* map of this cluster cpus */
+ cpumask_var_t cpu_map;
+ /* the cpus queue themselves according to priority in here */
+ struct bheap_node *heap_node;
+ struct bheap cpu_heap;
+ /* lock for this cluster */
+#define lock domain.ready_lock
+} cedf_domain_t;
+
+/* a cedf_domain per cluster; allocation is done at init/activation time */
+cedf_domain_t *cedf;
+
+#define remote_cluster(cpu) ((cedf_domain_t *) per_cpu(cedf_cpu_entries, cpu).cluster)
+#define task_cpu_cluster(task) remote_cluster(get_partition(task))
+
+/* Uncomment WANT_ALL_SCHED_EVENTS if you want to see all scheduling
+ * decisions in the TRACE() log; uncomment VERBOSE_INIT for verbose
+ * information during the initialization of the plugin (e.g., topology)
+#define WANT_ALL_SCHED_EVENTS
+ */
+#define VERBOSE_INIT
+
+static int cpu_lower_prio(struct bheap_node *_a, struct bheap_node *_b)
+{
+ cpu_entry_t *a, *b;
+ a = _a->value;
+ b = _b->value;
+ /* Note that a and b are inverted: we want the lowest-priority CPU at
+ * the top of the heap.
+ */
+ return edf_higher_prio(b->linked, a->linked);
+}
+
+/* update_cpu_position - Move the cpu entry to the correct place to maintain
+ * order in the cpu queue. Caller must hold cedf lock.
+ */
+static void update_cpu_position(cpu_entry_t *entry)
+{
+ cedf_domain_t *cluster = entry->cluster;
+
+ if (likely(bheap_node_in_heap(entry->hn)))
+ bheap_delete(cpu_lower_prio,
+ &cluster->cpu_heap,
+ entry->hn);
+
+ bheap_insert(cpu_lower_prio, &cluster->cpu_heap, entry->hn);
+}
+
+/* caller must hold cedf lock */
+static cpu_entry_t* lowest_prio_cpu(cedf_domain_t *cluster)
+{
+ struct bheap_node* hn;
+ hn = bheap_peek(cpu_lower_prio, &cluster->cpu_heap);
+ return hn->value;
+}
+
+
+/* link_task_to_cpu - Update the link of a CPU.
+ * Handles the case where the to-be-linked task is already
+ * scheduled on a different CPU.
+ */
+static noinline void link_task_to_cpu(struct task_struct* linked,
+ cpu_entry_t *entry)
+{
+ cpu_entry_t *sched;
+ struct task_struct* tmp;
+ int on_cpu;
+
+ BUG_ON(linked && !is_realtime(linked));
+
+ /* Currently linked task is set to be unlinked. */
+ if (entry->linked) {
+ entry->linked->rt_param.linked_on = NO_CPU;
+ }
+
+ /* Link new task to CPU. */
+ if (linked) {
+ set_rt_flags(linked, RT_F_RUNNING);
+ /* handle task is already scheduled somewhere! */
+ on_cpu = linked->rt_param.scheduled_on;
+ if (on_cpu != NO_CPU) {
+ sched = &per_cpu(cedf_cpu_entries, on_cpu);
+ /* this should only happen if not linked already */
+ BUG_ON(sched->linked == linked);
+
+ /* If we are already scheduled on the CPU to which we
+ * wanted to link, we don't need to do the swap --
+ * we just link ourselves to the CPU and depend on
+ * the caller to get things right.
+ */
+ if (entry != sched) {
+ TRACE_TASK(linked,
+ "already scheduled on %d, updating link.\n",
+ sched->cpu);
+ tmp = sched->linked;
+ linked->rt_param.linked_on = sched->cpu;
+ sched->linked = linked;
+ update_cpu_position(sched);
+ linked = tmp;
+ }
+ }
+ if (linked) /* might be NULL due to swap */
+ linked->rt_param.linked_on = entry->cpu;
+ }
+ entry->linked = linked;
+#ifdef WANT_ALL_SCHED_EVENTS
+ if (linked)
+ TRACE_TASK(linked, "linked to %d.\n", entry->cpu);
+ else
+ TRACE("NULL linked to %d.\n", entry->cpu);
+#endif
+ update_cpu_position(entry);
+}
+
+/* unlink - Make sure a task is not linked any longer to an entry
+ * where it was linked before. Must hold cedf_lock.
+ */
+static noinline void unlink(struct task_struct* t)
+{
+ cpu_entry_t *entry;
+
+ if (t->rt_param.linked_on != NO_CPU) {
+ /* unlink */
+ entry = &per_cpu(cedf_cpu_entries, t->rt_param.linked_on);
+ t->rt_param.linked_on = NO_CPU;
+ link_task_to_cpu(NULL, entry);
+ } else if (is_queued(t)) {
+ /* This is an interesting situation: t is scheduled,
+ * but was just recently unlinked. It cannot be
+ * linked anywhere else (because then it would have
+ * been relinked to this CPU), thus it must be in some
+ * queue. We must remove it from the list in this
+ * case.
+ *
+ * in C-EDF case is should be somewhere in the queue for
+ * its domain, therefore and we can get the domain using
+ * task_cpu_cluster
+ */
+ remove(&(task_cpu_cluster(t))->domain, t);
+ }
+}
+
+
+/* preempt - force a CPU to reschedule
+ */
+static void preempt(cpu_entry_t *entry)
+{
+ preempt_if_preemptable(entry->scheduled, entry->cpu);
+}
+
+/* requeue - Put an unlinked task into gsn-edf domain.
+ * Caller must hold cedf_lock.
+ */
+static noinline void requeue(struct task_struct* task)
+{
+ cedf_domain_t *cluster = task_cpu_cluster(task);
+ BUG_ON(!task);
+ /* sanity check before insertion */
+ BUG_ON(is_queued(task));
+
+ if (is_released(task, litmus_clock()))
+ __add_ready(&cluster->domain, task);
+ else {
+ /* it has got to wait */
+ add_release(&cluster->domain, task);
+ }
+}
+
+/* check for any necessary preemptions */
+static void check_for_preemptions(cedf_domain_t *cluster)
+{
+ struct task_struct *task;
+ cpu_entry_t* last;
+
+ for(last = lowest_prio_cpu(cluster);
+ edf_preemption_needed(&cluster->domain, last->linked);
+ last = lowest_prio_cpu(cluster)) {
+ /* preemption necessary */
+ task = __take_ready(&cluster->domain);
+ TRACE("check_for_preemptions: attempting to link task %d to %d\n",
+ task->pid, last->cpu);
+ if (last->linked)
+ requeue(last->linked);
+ link_task_to_cpu(task, last);
+ preempt(last);
+ }
+}
+
+/* cedf_job_arrival: task is either resumed or released */
+static noinline void cedf_job_arrival(struct task_struct* task)
+{
+ cedf_domain_t *cluster = task_cpu_cluster(task);
+ BUG_ON(!task);
+
+ requeue(task);
+ check_for_preemptions(cluster);
+}
+
+static void cedf_release_jobs(rt_domain_t* rt, struct bheap* tasks)
+{
+ cedf_domain_t* cluster = container_of(rt, cedf_domain_t, domain);
+ unsigned long flags;
+
+ raw_spin_lock_irqsave(&cluster->lock, flags);
+
+ __merge_ready(&cluster->domain, tasks);
+ check_for_preemptions(cluster);
+
+ raw_spin_unlock_irqrestore(&cluster->lock, flags);
+}
+
+/* caller holds cedf_lock */
+static noinline void job_completion(struct task_struct *t, int forced)
+{
+ BUG_ON(!t);
+
+ sched_trace_task_completion(t, forced);
+
+ TRACE_TASK(t, "job_completion().\n");
+
+ /* set flags */
+ set_rt_flags(t, RT_F_SLEEP);
+ /* prepare for next period */
+ prepare_for_next_period(t);
+ if (is_released(t, litmus_clock()))
+ sched_trace_task_release(t);
+ /* unlink */
+ unlink(t);
+ /* requeue
+ * But don't requeue a blocking task. */
+ if (is_running(t))
+ cedf_job_arrival(t);
+}
+
+/* cedf_tick - this function is called for every local timer
+ * interrupt.
+ *
+ * checks whether the current task has expired and checks
+ * whether we need to preempt it if it has not expired
+ */
+static void cedf_tick(struct task_struct* t)
+{
+ if (is_realtime(t) && budget_enforced(t) && budget_exhausted(t)) {
+ if (!is_np(t)) {
+ /* np tasks will be preempted when they become
+ * preemptable again
+ */
+ litmus_reschedule_local();
+ set_will_schedule();
+ TRACE("cedf_scheduler_tick: "
+ "%d is preemptable "
+ " => FORCE_RESCHED\n", t->pid);
+ } else if (is_user_np(t)) {
+ TRACE("cedf_scheduler_tick: "
+ "%d is non-preemptable, "
+ "preemption delayed.\n", t->pid);
+ request_exit_np(t);
+ }
+ }
+}
+
+/* Getting schedule() right is a bit tricky. schedule() may not make any
+ * assumptions on the state of the current task since it may be called for a
+ * number of reasons. The reasons include a scheduler_tick() determined that it
+ * was necessary, because sys_exit_np() was called, because some Linux
+ * subsystem determined so, or even (in the worst case) because there is a bug
+ * hidden somewhere. Thus, we must take extreme care to determine what the
+ * current state is.
+ *
+ * The CPU could currently be scheduling a task (or not), be linked (or not).
+ *
+ * The following assertions for the scheduled task could hold:
+ *
+ * - !is_running(scheduled) // the job blocks
+ * - scheduled->timeslice == 0 // the job completed (forcefully)
+ * - get_rt_flag() == RT_F_SLEEP // the job completed (by syscall)
+ * - linked != scheduled // we need to reschedule (for any reason)
+ * - is_np(scheduled) // rescheduling must be delayed,
+ * sys_exit_np must be requested
+ *
+ * Any of these can occur together.
+ */
+static struct task_struct* cedf_schedule(struct task_struct * prev)
+{
+ cpu_entry_t* entry = &__get_cpu_var(cedf_cpu_entries);
+ cedf_domain_t *cluster = entry->cluster;
+ int out_of_time, sleep, preempt, np, exists, blocks;
+ struct task_struct* next = NULL;
+
+ raw_spin_lock(&cluster->lock);
+ clear_will_schedule();
+
+ /* sanity checking */
+ BUG_ON(entry->scheduled && entry->scheduled != prev);
+ BUG_ON(entry->scheduled && !is_realtime(prev));
+ BUG_ON(is_realtime(prev) && !entry->scheduled);
+
+ /* (0) Determine state */
+ exists = entry->scheduled != NULL;
+ blocks = exists && !is_running(entry->scheduled);
+ out_of_time = exists &&
+ budget_enforced(entry->scheduled) &&
+ budget_exhausted(entry->scheduled);
+ np = exists && is_np(entry->scheduled);
+ sleep = exists && get_rt_flags(entry->scheduled) == RT_F_SLEEP;
+ preempt = entry->scheduled != entry->linked;
+
+#ifdef WANT_ALL_SCHED_EVENTS
+ TRACE_TASK(prev, "invoked cedf_schedule.\n");
+#endif
+
+ if (exists)
+ TRACE_TASK(prev,
+ "blocks:%d out_of_time:%d np:%d sleep:%d preempt:%d "
+ "state:%d sig:%d\n",
+ blocks, out_of_time, np, sleep, preempt,
+ prev->state, signal_pending(prev));
+ if (entry->linked && preempt)
+ TRACE_TASK(prev, "will be preempted by %s/%d\n",
+ entry->linked->comm, entry->linked->pid);
+
+
+ /* If a task blocks we have no choice but to reschedule.
+ */
+ if (blocks)
+ unlink(entry->scheduled);
+
+ /* Request a sys_exit_np() call if we would like to preempt but cannot.
+ * We need to make sure to update the link structure anyway in case
+ * that we are still linked. Multiple calls to request_exit_np() don't
+ * hurt.
+ */
+ if (np && (out_of_time || preempt || sleep)) {
+ unlink(entry->scheduled);
+ request_exit_np(entry->scheduled);
+ }
+
+ /* Any task that is preemptable and either exhausts its execution
+ * budget or wants to sleep completes. We may have to reschedule after
+ * this. Don't do a job completion if we block (can't have timers running
+ * for blocked jobs). Preemption go first for the same reason.
+ */
+ if (!np && (out_of_time || sleep) && !blocks && !preempt)
+ job_completion(entry->scheduled, !sleep);
+
+ /* Link pending task if we became unlinked.
+ */
+ if (!entry->linked)
+ link_task_to_cpu(__take_ready(&cluster->domain), entry);
+
+ /* The final scheduling decision. Do we need to switch for some reason?
+ * If linked is different from scheduled, then select linked as next.
+ */
+ if ((!np || blocks) &&
+ entry->linked != entry->scheduled) {
+ /* Schedule a linked job? */
+ if (entry->linked) {
+ entry->linked->rt_param.scheduled_on = entry->cpu;
+ next = entry->linked;
+ }
+ if (entry->scheduled) {
+ /* not gonna be scheduled soon */
+ entry->scheduled->rt_param.scheduled_on = NO_CPU;
+ TRACE_TASK(entry->scheduled, "scheduled_on = NO_CPU\n");
+ }
+ } else
+ /* Only override Linux scheduler if we have a real-time task
+ * scheduled that needs to continue.
+ */
+ if (exists)
+ next = prev;
+
+ sched_state_task_picked();
+ raw_spin_unlock(&cluster->lock);
+
+#ifdef WANT_ALL_SCHED_EVENTS
+ TRACE("cedf_lock released, next=0x%p\n", next);
+
+ if (next)
+ TRACE_TASK(next, "scheduled at %llu\n", litmus_clock());
+ else if (exists && !next)
+ TRACE("becomes idle at %llu.\n", litmus_clock());
+#endif
+
+
+ return next;
+}
+
+
+/* _finish_switch - we just finished the switch away from prev
+ */
+static void cedf_finish_switch(struct task_struct *prev)
+{
+ cpu_entry_t* entry = &__get_cpu_var(cedf_cpu_entries);
+
+ entry->scheduled = is_realtime(current) ? current : NULL;
+#ifdef WANT_ALL_SCHED_EVENTS
+ TRACE_TASK(prev, "switched away from\n");
+#endif
+}
+
+
+/* Prepare a task for running in RT mode
+ */
+static void cedf_task_new(struct task_struct * t, int on_rq, int running)
+{
+ unsigned long flags;
+ cpu_entry_t* entry;
+ cedf_domain_t* cluster;
+
+ TRACE("gsn edf: task new %d\n", t->pid);
+
+ /* the cluster doesn't change even if t is running */
+ cluster = task_cpu_cluster(t);
+
+ raw_spin_lock_irqsave(&cluster->domain.ready_lock, flags);
+
+ /* setup job params */
+ release_at(t, litmus_clock());
+
+ if (running) {
+ entry = &per_cpu(cedf_cpu_entries, task_cpu(t));
+ BUG_ON(entry->scheduled);
+
+ entry->scheduled = t;
+ tsk_rt(t)->scheduled_on = task_cpu(t);
+ } else {
+ t->rt_param.scheduled_on = NO_CPU;
+ }
+ t->rt_param.linked_on = NO_CPU;
+
+ cedf_job_arrival(t);
+ raw_spin_unlock_irqrestore(&(cluster->domain.ready_lock), flags);
+}
+
+static void cedf_task_wake_up(struct task_struct *task)
+{
+ unsigned long flags;
+ lt_t now;
+ cedf_domain_t *cluster;
+
+ TRACE_TASK(task, "wake_up at %llu\n", litmus_clock());
+
+ cluster = task_cpu_cluster(task);
+
+ raw_spin_lock_irqsave(&cluster->lock, flags);
+ /* We need to take suspensions because of semaphores into
+ * account! If a job resumes after being suspended due to acquiring
+ * a semaphore, it should never be treated as a new job release.
+ */
+ if (get_rt_flags(task) == RT_F_EXIT_SEM) {
+ set_rt_flags(task, RT_F_RUNNING);
+ } else {
+ now = litmus_clock();
+ if (is_tardy(task, now)) {
+ /* new sporadic release */
+ release_at(task, now);
+ sched_trace_task_release(task);
+ }
+ else {
+ if (task->rt.time_slice) {
+ /* came back in time before deadline
+ */
+ set_rt_flags(task, RT_F_RUNNING);
+ }
+ }
+ }
+ cedf_job_arrival(task);
+ raw_spin_unlock_irqrestore(&cluster->lock, flags);
+}
+
+static void cedf_task_block(struct task_struct *t)
+{
+ unsigned long flags;
+ cedf_domain_t *cluster;
+
+ TRACE_TASK(t, "block at %llu\n", litmus_clock());
+
+ cluster = task_cpu_cluster(t);
+
+ /* unlink if necessary */
+ raw_spin_lock_irqsave(&cluster->lock, flags);
+ unlink(t);
+ raw_spin_unlock_irqrestore(&cluster->lock, flags);
+
+ BUG_ON(!is_realtime(t));
+}
+
+
+static void cedf_task_exit(struct task_struct * t)
+{
+ unsigned long flags;
+ cedf_domain_t *cluster = task_cpu_cluster(t);
+
+ /* unlink if necessary */
+ raw_spin_lock_irqsave(&cluster->lock, flags);
+ unlink(t);
+ if (tsk_rt(t)->scheduled_on != NO_CPU) {
+ cpu_entry_t *cpu;
+ cpu = &per_cpu(cedf_cpu_entries, tsk_rt(t)->scheduled_on);
+ cpu->scheduled = NULL;
+ tsk_rt(t)->scheduled_on = NO_CPU;
+ }
+ raw_spin_unlock_irqrestore(&cluster->lock, flags);
+
+ BUG_ON(!is_realtime(t));
+ TRACE_TASK(t, "RIP\n");
+}
+
+static long cedf_admit_task(struct task_struct* tsk)
+{
+ return task_cpu(tsk) == tsk->rt_param.task_params.cpu ? 0 : -EINVAL;
+}
+
+/* total number of cluster */
+static int num_clusters;
+/* we do not support cluster of different sizes */
+static unsigned int cluster_size;
+
+#ifdef VERBOSE_INIT
+static void print_cluster_topology(cpumask_var_t mask, int cpu)
+{
+ int chk;
+ char buf[255];
+
+ chk = cpulist_scnprintf(buf, 254, mask);
+ buf[chk] = '\0';
+ printk(KERN_INFO "CPU = %d, shared cpu(s) = %s\n", cpu, buf);
+
+}
+#endif
+
+static int clusters_allocated = 0;
+
+static void cleanup_cedf(void)
+{
+ int i;
+
+ if (clusters_allocated) {
+ for (i = 0; i < num_clusters; i++) {
+ kfree(cedf[i].cpus);
+ kfree(cedf[i].heap_node);
+ free_cpumask_var(cedf[i].cpu_map);
+ }
+
+ kfree(cedf);
+ }
+}
+
+static long cedf_activate_plugin(void)
+{
+ int i, j, cpu, ccpu, cpu_count;
+ cpu_entry_t *entry;
+
+ cpumask_var_t mask;
+ int chk = 0;
+
+ /* de-allocate old clusters, if any */
+ cleanup_cedf();
+
+ printk(KERN_INFO "C-EDF: Activate Plugin, cluster configuration = %d\n",
+ cluster_config);
+
+ /* need to get cluster_size first */
+ if(!zalloc_cpumask_var(&mask, GFP_ATOMIC))
+ return -ENOMEM;
+
+ if (unlikely(cluster_config == GLOBAL_CLUSTER)) {
+ cluster_size = num_online_cpus();
+ } else {
+ chk = get_shared_cpu_map(mask, 0, cluster_config);
+ if (chk) {
+ /* if chk != 0 then it is the max allowed index */
+ printk(KERN_INFO "C-EDF: Cluster configuration = %d "
+ "is not supported on this hardware.\n",
+ cluster_config);
+ /* User should notice that the configuration failed, so
+ * let's bail out. */
+ return -EINVAL;
+ }
+
+ cluster_size = cpumask_weight(mask);
+ }
+
+ if ((num_online_cpus() % cluster_size) != 0) {
+ /* this can't be right, some cpus are left out */
+ printk(KERN_ERR "C-EDF: Trying to group %d cpus in %d!\n",
+ num_online_cpus(), cluster_size);
+ return -1;
+ }
+
+ num_clusters = num_online_cpus() / cluster_size;
+ printk(KERN_INFO "C-EDF: %d cluster(s) of size = %d\n",
+ num_clusters, cluster_size);
+
+ /* initialize clusters */
+ cedf = kmalloc(num_clusters * sizeof(cedf_domain_t), GFP_ATOMIC);
+ for (i = 0; i < num_clusters; i++) {
+
+ cedf[i].cpus = kmalloc(cluster_size * sizeof(cpu_entry_t),
+ GFP_ATOMIC);
+ cedf[i].heap_node = kmalloc(
+ cluster_size * sizeof(struct bheap_node),
+ GFP_ATOMIC);
+ bheap_init(&(cedf[i].cpu_heap));
+ edf_domain_init(&(cedf[i].domain), NULL, cedf_release_jobs);
+
+ if(!zalloc_cpumask_var(&cedf[i].cpu_map, GFP_ATOMIC))
+ return -ENOMEM;
+ }
+
+ /* cycle through cluster and add cpus to them */
+ for (i = 0; i < num_clusters; i++) {
+
+ for_each_online_cpu(cpu) {
+ /* check if the cpu is already in a cluster */
+ for (j = 0; j < num_clusters; j++)
+ if (cpumask_test_cpu(cpu, cedf[j].cpu_map))
+ break;
+ /* if it is in a cluster go to next cpu */
+ if (j < num_clusters &&
+ cpumask_test_cpu(cpu, cedf[j].cpu_map))
+ continue;
+
+ /* this cpu isn't in any cluster */
+ /* get the shared cpus */
+ if (unlikely(cluster_config == GLOBAL_CLUSTER))
+ cpumask_copy(mask, cpu_online_mask);
+ else
+ get_shared_cpu_map(mask, cpu, cluster_config);
+
+ cpumask_copy(cedf[i].cpu_map, mask);
+#ifdef VERBOSE_INIT
+ print_cluster_topology(mask, cpu);
+#endif
+ /* add cpus to current cluster and init cpu_entry_t */
+ cpu_count = 0;
+ for_each_cpu(ccpu, cedf[i].cpu_map) {
+
+ entry = &per_cpu(cedf_cpu_entries, ccpu);
+ cedf[i].cpus[cpu_count] = entry;
+ atomic_set(&entry->will_schedule, 0);
+ entry->cpu = ccpu;
+ entry->cluster = &cedf[i];
+ entry->hn = &(cedf[i].heap_node[cpu_count]);
+ bheap_node_init(&entry->hn, entry);
+
+ cpu_count++;
+
+ entry->linked = NULL;
+ entry->scheduled = NULL;
+ update_cpu_position(entry);
+ }
+ /* done with this cluster */
+ break;
+ }
+ }
+
+ free_cpumask_var(mask);
+ clusters_allocated = 1;
+ return 0;
+}
+
+/* Plugin object */
+static struct sched_plugin cedf_plugin __cacheline_aligned_in_smp = {
+ .plugin_name = "C-EDF",
+ .finish_switch = cedf_finish_switch,
+ .tick = cedf_tick,
+ .task_new = cedf_task_new,
+ .complete_job = complete_job,
+ .task_exit = cedf_task_exit,
+ .schedule = cedf_schedule,
+ .task_wake_up = cedf_task_wake_up,
+ .task_block = cedf_task_block,
+ .admit_task = cedf_admit_task,
+ .activate_plugin = cedf_activate_plugin,
+};
+
+
+/* proc file interface to configure the cluster size */
+
+static int proc_read_cluster_size(char *page, char **start,
+ off_t off, int count,
+ int *eof, void *data)
+{
+ int len;
+ switch (cluster_config) {
+ case GLOBAL_CLUSTER:
+ len = snprintf(page, PAGE_SIZE, "ALL\n");
+ break;
+ case L1_CLUSTER:
+ case L2_CLUSTER:
+ case L3_CLUSTER:
+ len = snprintf(page, PAGE_SIZE, "L%d\n", cluster_config);
+ break;
+ default:
+ /* This should be impossible, but let's be paranoid. */
+ len = snprintf(page, PAGE_SIZE, "INVALID (%d)\n",
+ cluster_config);
+ break;
+ }
+ return len;
+}
+
+static int proc_write_cluster_size(struct file *file,
+ const char *buffer,
+ unsigned long count,
+ void *data)
+{
+ int len;
+ /* L2, L3 */
+ char cache_name[33];
+
+ if(count > 32)
+ len = 32;
+ else
+ len = count;
+
+ if(copy_from_user(cache_name, buffer, len))
+ return -EFAULT;
+
+ cache_name[len] = '\0';
+ /* chomp name */
+ if (len > 1 && cache_name[len - 1] == '\n')
+ cache_name[len - 1] = '\0';
+
+ /* do a quick and dirty comparison to find the cluster size */
+ if (!strcmp(cache_name, "L2"))
+ cluster_config = L2_CLUSTER;
+ else if (!strcmp(cache_name, "L3"))
+ cluster_config = L3_CLUSTER;
+ else if (!strcmp(cache_name, "L1"))
+ cluster_config = L1_CLUSTER;
+ else if (!strcmp(cache_name, "ALL"))
+ cluster_config = GLOBAL_CLUSTER;
+ else
+ printk(KERN_INFO "Cluster '%s' is unknown.\n", cache_name);
+
+ return len;
+}
+
+
+static struct proc_dir_entry *cluster_file = NULL, *cedf_dir = NULL;
+
+
+static int __init init_cedf(void)
+{
+ int err, fs;
+
+ err = register_sched_plugin(&cedf_plugin);
+ if (!err) {
+ fs = make_plugin_proc_dir(&cedf_plugin, &cedf_dir);
+ if (!fs) {
+ cluster_file = create_proc_entry("cluster", 0644, cedf_dir);
+ if (!cluster_file) {
+ printk(KERN_ERR "Could not allocate C-EDF/cluster "
+ "procfs entry.\n");
+ } else {
+ cluster_file->read_proc = proc_read_cluster_size;
+ cluster_file->write_proc = proc_write_cluster_size;
+ }
+ } else {
+ printk(KERN_ERR "Could not allocate C-EDF procfs dir.\n");
+ }
+ }
+ return err;
+}
+
+static void clean_cedf(void)
+{
+ cleanup_cedf();
+ if (cluster_file)
+ remove_proc_entry("cluster", cedf_dir);
+ if (cedf_dir)
+ remove_plugin_proc_dir(&cedf_plugin);
+}
+
+module_init(init_cedf);
+module_exit(clean_cedf);
diff --git a/litmus/sched_gsn_edf.c b/litmus/sched_gsn_edf.c
new file mode 100644
index 0000000..e9c5e53
--- /dev/null
+++ b/litmus/sched_gsn_edf.c
@@ -0,0 +1,828 @@
+/*
+ * litmus/sched_gsn_edf.c
+ *
+ * Implementation of the GSN-EDF scheduling algorithm.
+ *
+ * This version uses the simple approach and serializes all scheduling
+ * decisions by the use of a queue lock. This is probably not the
+ * best way to do it, but it should suffice for now.
+ */
+
+#include <linux/spinlock.h>
+#include <linux/percpu.h>
+#include <linux/sched.h>
+
+#include <litmus/litmus.h>
+#include <litmus/jobs.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/edf_common.h>
+#include <litmus/sched_trace.h>
+
+#include <litmus/preempt.h>
+
+#include <litmus/bheap.h>
+
+#include <linux/module.h>
+
+/* Overview of GSN-EDF operations.
+ *
+ * For a detailed explanation of GSN-EDF have a look at the FMLP paper. This
+ * description only covers how the individual operations are implemented in
+ * LITMUS.
+ *
+ * link_task_to_cpu(T, cpu) - Low-level operation to update the linkage
+ * structure (NOT the actually scheduled
+ * task). If there is another linked task To
+ * already it will set To->linked_on = NO_CPU
+ * (thereby removing its association with this
+ * CPU). However, it will not requeue the
+ * previously linked task (if any). It will set
+ * T's state to RT_F_RUNNING and check whether
+ * it is already running somewhere else. If T
+ * is scheduled somewhere else it will link
+ * it to that CPU instead (and pull the linked
+ * task to cpu). T may be NULL.
+ *
+ * unlink(T) - Unlink removes T from all scheduler data
+ * structures. If it is linked to some CPU it
+ * will link NULL to that CPU. If it is
+ * currently queued in the gsnedf queue it will
+ * be removed from the rt_domain. It is safe to
+ * call unlink(T) if T is not linked. T may not
+ * be NULL.
+ *
+ * requeue(T) - Requeue will insert T into the appropriate
+ * queue. If the system is in real-time mode and
+ * the T is released already, it will go into the
+ * ready queue. If the system is not in
+ * real-time mode is T, then T will go into the
+ * release queue. If T's release time is in the
+ * future, it will go into the release
+ * queue. That means that T's release time/job
+ * no/etc. has to be updated before requeu(T) is
+ * called. It is not safe to call requeue(T)
+ * when T is already queued. T may not be NULL.
+ *
+ * gsnedf_job_arrival(T) - This is the catch all function when T enters
+ * the system after either a suspension or at a
+ * job release. It will queue T (which means it
+ * is not safe to call gsnedf_job_arrival(T) if
+ * T is already queued) and then check whether a
+ * preemption is necessary. If a preemption is
+ * necessary it will update the linkage
+ * accordingly and cause scheduled to be called
+ * (either with an IPI or need_resched). It is
+ * safe to call gsnedf_job_arrival(T) if T's
+ * next job has not been actually released yet
+ * (releast time in the future). T will be put
+ * on the release queue in that case.
+ *
+ * job_completion(T) - Take care of everything that needs to be done
+ * to prepare T for its next release and place
+ * it in the right queue with
+ * gsnedf_job_arrival().
+ *
+ *
+ * When we now that T is linked to CPU then link_task_to_cpu(NULL, CPU) is
+ * equivalent to unlink(T). Note that if you unlink a task from a CPU none of
+ * the functions will automatically propagate pending task from the ready queue
+ * to a linked task. This is the job of the calling function ( by means of
+ * __take_ready).
+ */
+
+
+/* cpu_entry_t - maintain the linked and scheduled state
+ */
+typedef struct {
+ int cpu;
+ struct task_struct* linked; /* only RT tasks */
+ struct task_struct* scheduled; /* only RT tasks */
+ struct bheap_node* hn;
+} cpu_entry_t;
+DEFINE_PER_CPU(cpu_entry_t, gsnedf_cpu_entries);
+
+cpu_entry_t* gsnedf_cpus[NR_CPUS];
+
+/* the cpus queue themselves according to priority in here */
+static struct bheap_node gsnedf_heap_node[NR_CPUS];
+static struct bheap gsnedf_cpu_heap;
+
+static rt_domain_t gsnedf;
+#define gsnedf_lock (gsnedf.ready_lock)
+
+
+/* Uncomment this if you want to see all scheduling decisions in the
+ * TRACE() log.
+#define WANT_ALL_SCHED_EVENTS
+ */
+
+static int cpu_lower_prio(struct bheap_node *_a, struct bheap_node *_b)
+{
+ cpu_entry_t *a, *b;
+ a = _a->value;
+ b = _b->value;
+ /* Note that a and b are inverted: we want the lowest-priority CPU at
+ * the top of the heap.
+ */
+ return edf_higher_prio(b->linked, a->linked);
+}
+
+/* update_cpu_position - Move the cpu entry to the correct place to maintain
+ * order in the cpu queue. Caller must hold gsnedf lock.
+ */
+static void update_cpu_position(cpu_entry_t *entry)
+{
+ if (likely(bheap_node_in_heap(entry->hn)))
+ bheap_delete(cpu_lower_prio, &gsnedf_cpu_heap, entry->hn);
+ bheap_insert(cpu_lower_prio, &gsnedf_cpu_heap, entry->hn);
+}
+
+/* caller must hold gsnedf lock */
+static cpu_entry_t* lowest_prio_cpu(void)
+{
+ struct bheap_node* hn;
+ hn = bheap_peek(cpu_lower_prio, &gsnedf_cpu_heap);
+ return hn->value;
+}
+
+
+/* link_task_to_cpu - Update the link of a CPU.
+ * Handles the case where the to-be-linked task is already
+ * scheduled on a different CPU.
+ */
+static noinline void link_task_to_cpu(struct task_struct* linked,
+ cpu_entry_t *entry)
+{
+ cpu_entry_t *sched;
+ struct task_struct* tmp;
+ int on_cpu;
+
+ BUG_ON(linked && !is_realtime(linked));
+
+ /* Currently linked task is set to be unlinked. */
+ if (entry->linked) {
+ entry->linked->rt_param.linked_on = NO_CPU;
+ }
+
+ /* Link new task to CPU. */
+ if (linked) {
+ set_rt_flags(linked, RT_F_RUNNING);
+ /* handle task is already scheduled somewhere! */
+ on_cpu = linked->rt_param.scheduled_on;
+ if (on_cpu != NO_CPU) {
+ sched = &per_cpu(gsnedf_cpu_entries, on_cpu);
+ /* this should only happen if not linked already */
+ BUG_ON(sched->linked == linked);
+
+ /* If we are already scheduled on the CPU to which we
+ * wanted to link, we don't need to do the swap --
+ * we just link ourselves to the CPU and depend on
+ * the caller to get things right.
+ */
+ if (entry != sched) {
+ TRACE_TASK(linked,
+ "already scheduled on %d, updating link.\n",
+ sched->cpu);
+ tmp = sched->linked;
+ linked->rt_param.linked_on = sched->cpu;
+ sched->linked = linked;
+ update_cpu_position(sched);
+ linked = tmp;
+ }
+ }
+ if (linked) /* might be NULL due to swap */
+ linked->rt_param.linked_on = entry->cpu;
+ }
+ entry->linked = linked;
+#ifdef WANT_ALL_SCHED_EVENTS
+ if (linked)
+ TRACE_TASK(linked, "linked to %d.\n", entry->cpu);
+ else
+ TRACE("NULL linked to %d.\n", entry->cpu);
+#endif
+ update_cpu_position(entry);
+}
+
+/* unlink - Make sure a task is not linked any longer to an entry
+ * where it was linked before. Must hold gsnedf_lock.
+ */
+static noinline void unlink(struct task_struct* t)
+{
+ cpu_entry_t *entry;
+
+ if (t->rt_param.linked_on != NO_CPU) {
+ /* unlink */
+ entry = &per_cpu(gsnedf_cpu_entries, t->rt_param.linked_on);
+ t->rt_param.linked_on = NO_CPU;
+ link_task_to_cpu(NULL, entry);
+ } else if (is_queued(t)) {
+ /* This is an interesting situation: t is scheduled,
+ * but was just recently unlinked. It cannot be
+ * linked anywhere else (because then it would have
+ * been relinked to this CPU), thus it must be in some
+ * queue. We must remove it from the list in this
+ * case.
+ */
+ remove(&gsnedf, t);
+ }
+}
+
+
+/* preempt - force a CPU to reschedule
+ */
+static void preempt(cpu_entry_t *entry)
+{
+ preempt_if_preemptable(entry->scheduled, entry->cpu);
+}
+
+/* requeue - Put an unlinked task into gsn-edf domain.
+ * Caller must hold gsnedf_lock.
+ */
+static noinline void requeue(struct task_struct* task)
+{
+ BUG_ON(!task);
+ /* sanity check before insertion */
+ BUG_ON(is_queued(task));
+
+ if (is_released(task, litmus_clock()))
+ __add_ready(&gsnedf, task);
+ else {
+ /* it has got to wait */
+ add_release(&gsnedf, task);
+ }
+}
+
+/* check for any necessary preemptions */
+static void check_for_preemptions(void)
+{
+ struct task_struct *task;
+ cpu_entry_t* last;
+
+ for(last = lowest_prio_cpu();
+ edf_preemption_needed(&gsnedf, last->linked);
+ last = lowest_prio_cpu()) {
+ /* preemption necessary */
+ task = __take_ready(&gsnedf);
+ TRACE("check_for_preemptions: attempting to link task %d to %d\n",
+ task->pid, last->cpu);
+ if (last->linked)
+ requeue(last->linked);
+ link_task_to_cpu(task, last);
+ preempt(last);
+ }
+}
+
+/* gsnedf_job_arrival: task is either resumed or released */
+static noinline void gsnedf_job_arrival(struct task_struct* task)
+{
+ BUG_ON(!task);
+
+ requeue(task);
+ check_for_preemptions();
+}
+
+static void gsnedf_release_jobs(rt_domain_t* rt, struct bheap* tasks)
+{
+ unsigned long flags;
+
+ raw_spin_lock_irqsave(&gsnedf_lock, flags);
+
+ __merge_ready(rt, tasks);
+ check_for_preemptions();
+
+ raw_spin_unlock_irqrestore(&gsnedf_lock, flags);
+}
+
+/* caller holds gsnedf_lock */
+static noinline void job_completion(struct task_struct *t, int forced)
+{
+ BUG_ON(!t);
+
+ sched_trace_task_completion(t, forced);
+
+ TRACE_TASK(t, "job_completion().\n");
+
+ /* set flags */
+ set_rt_flags(t, RT_F_SLEEP);
+ /* prepare for next period */
+ prepare_for_next_period(t);
+ if (is_released(t, litmus_clock()))
+ sched_trace_task_release(t);
+ /* unlink */
+ unlink(t);
+ /* requeue
+ * But don't requeue a blocking task. */
+ if (is_running(t))
+ gsnedf_job_arrival(t);
+}
+
+/* gsnedf_tick - this function is called for every local timer
+ * interrupt.
+ *
+ * checks whether the current task has expired and checks
+ * whether we need to preempt it if it has not expired
+ */
+static void gsnedf_tick(struct task_struct* t)
+{
+ if (is_realtime(t) && budget_enforced(t) && budget_exhausted(t)) {
+ if (!is_np(t)) {
+ /* np tasks will be preempted when they become
+ * preemptable again
+ */
+ litmus_reschedule_local();
+ TRACE("gsnedf_scheduler_tick: "
+ "%d is preemptable "
+ " => FORCE_RESCHED\n", t->pid);
+ } else if (is_user_np(t)) {
+ TRACE("gsnedf_scheduler_tick: "
+ "%d is non-preemptable, "
+ "preemption delayed.\n", t->pid);
+ request_exit_np(t);
+ }
+ }
+}
+
+/* Getting schedule() right is a bit tricky. schedule() may not make any
+ * assumptions on the state of the current task since it may be called for a
+ * number of reasons. The reasons include a scheduler_tick() determined that it
+ * was necessary, because sys_exit_np() was called, because some Linux
+ * subsystem determined so, or even (in the worst case) because there is a bug
+ * hidden somewhere. Thus, we must take extreme care to determine what the
+ * current state is.
+ *
+ * The CPU could currently be scheduling a task (or not), be linked (or not).
+ *
+ * The following assertions for the scheduled task could hold:
+ *
+ * - !is_running(scheduled) // the job blocks
+ * - scheduled->timeslice == 0 // the job completed (forcefully)
+ * - get_rt_flag() == RT_F_SLEEP // the job completed (by syscall)
+ * - linked != scheduled // we need to reschedule (for any reason)
+ * - is_np(scheduled) // rescheduling must be delayed,
+ * sys_exit_np must be requested
+ *
+ * Any of these can occur together.
+ */
+static struct task_struct* gsnedf_schedule(struct task_struct * prev)
+{
+ cpu_entry_t* entry = &__get_cpu_var(gsnedf_cpu_entries);
+ int out_of_time, sleep, preempt, np, exists, blocks;
+ struct task_struct* next = NULL;
+
+#ifdef CONFIG_RELEASE_MASTER
+ /* Bail out early if we are the release master.
+ * The release master never schedules any real-time tasks.
+ */
+ if (gsnedf.release_master == entry->cpu)
+ return NULL;
+#endif
+
+ raw_spin_lock(&gsnedf_lock);
+
+ /* sanity checking */
+ BUG_ON(entry->scheduled && entry->scheduled != prev);
+ BUG_ON(entry->scheduled && !is_realtime(prev));
+ BUG_ON(is_realtime(prev) && !entry->scheduled);
+
+ /* (0) Determine state */
+ exists = entry->scheduled != NULL;
+ blocks = exists && !is_running(entry->scheduled);
+ out_of_time = exists &&
+ budget_enforced(entry->scheduled) &&
+ budget_exhausted(entry->scheduled);
+ np = exists && is_np(entry->scheduled);
+ sleep = exists && get_rt_flags(entry->scheduled) == RT_F_SLEEP;
+ preempt = entry->scheduled != entry->linked;
+
+#ifdef WANT_ALL_SCHED_EVENTS
+ TRACE_TASK(prev, "invoked gsnedf_schedule.\n");
+#endif
+
+ if (exists)
+ TRACE_TASK(prev,
+ "blocks:%d out_of_time:%d np:%d sleep:%d preempt:%d "
+ "state:%d sig:%d\n",
+ blocks, out_of_time, np, sleep, preempt,
+ prev->state, signal_pending(prev));
+ if (entry->linked && preempt)
+ TRACE_TASK(prev, "will be preempted by %s/%d\n",
+ entry->linked->comm, entry->linked->pid);
+
+
+ /* If a task blocks we have no choice but to reschedule.
+ */
+ if (blocks)
+ unlink(entry->scheduled);
+
+ /* Request a sys_exit_np() call if we would like to preempt but cannot.
+ * We need to make sure to update the link structure anyway in case
+ * that we are still linked. Multiple calls to request_exit_np() don't
+ * hurt.
+ */
+ if (np && (out_of_time || preempt || sleep)) {
+ unlink(entry->scheduled);
+ request_exit_np(entry->scheduled);
+ }
+
+ /* Any task that is preemptable and either exhausts its execution
+ * budget or wants to sleep completes. We may have to reschedule after
+ * this. Don't do a job completion if we block (can't have timers running
+ * for blocked jobs). Preemption go first for the same reason.
+ */
+ if (!np && (out_of_time || sleep) && !blocks && !preempt)
+ job_completion(entry->scheduled, !sleep);
+
+ /* Link pending task if we became unlinked.
+ */
+ if (!entry->linked)
+ link_task_to_cpu(__take_ready(&gsnedf), entry);
+
+ /* The final scheduling decision. Do we need to switch for some reason?
+ * If linked is different from scheduled, then select linked as next.
+ */
+ if ((!np || blocks) &&
+ entry->linked != entry->scheduled) {
+ /* Schedule a linked job? */
+ if (entry->linked) {
+ entry->linked->rt_param.scheduled_on = entry->cpu;
+ next = entry->linked;
+ }
+ if (entry->scheduled) {
+ /* not gonna be scheduled soon */
+ entry->scheduled->rt_param.scheduled_on = NO_CPU;
+ TRACE_TASK(entry->scheduled, "scheduled_on = NO_CPU\n");
+ }
+ } else
+ /* Only override Linux scheduler if we have a real-time task
+ * scheduled that needs to continue.
+ */
+ if (exists)
+ next = prev;
+
+ sched_state_task_picked();
+
+ raw_spin_unlock(&gsnedf_lock);
+
+#ifdef WANT_ALL_SCHED_EVENTS
+ TRACE("gsnedf_lock released, next=0x%p\n", next);
+
+ if (next)
+ TRACE_TASK(next, "scheduled at %llu\n", litmus_clock());
+ else if (exists && !next)
+ TRACE("becomes idle at %llu.\n", litmus_clock());
+#endif
+
+
+ return next;
+}
+
+
+/* _finish_switch - we just finished the switch away from prev
+ */
+static void gsnedf_finish_switch(struct task_struct *prev)
+{
+ cpu_entry_t* entry = &__get_cpu_var(gsnedf_cpu_entries);
+
+ entry->scheduled = is_realtime(current) ? current : NULL;
+#ifdef WANT_ALL_SCHED_EVENTS
+ TRACE_TASK(prev, "switched away from\n");
+#endif
+}
+
+
+/* Prepare a task for running in RT mode
+ */
+static void gsnedf_task_new(struct task_struct * t, int on_rq, int running)
+{
+ unsigned long flags;
+ cpu_entry_t* entry;
+
+ TRACE("gsn edf: task new %d\n", t->pid);
+
+ raw_spin_lock_irqsave(&gsnedf_lock, flags);
+
+ /* setup job params */
+ release_at(t, litmus_clock());
+
+ if (running) {
+ entry = &per_cpu(gsnedf_cpu_entries, task_cpu(t));
+ BUG_ON(entry->scheduled);
+
+#ifdef CONFIG_RELEASE_MASTER
+ if (entry->cpu != gsnedf.release_master) {
+#endif
+ entry->scheduled = t;
+ tsk_rt(t)->scheduled_on = task_cpu(t);
+#ifdef CONFIG_RELEASE_MASTER
+ } else {
+ /* do not schedule on release master */
+ preempt(entry); /* force resched */
+ tsk_rt(t)->scheduled_on = NO_CPU;
+ }
+#endif
+ } else {
+ t->rt_param.scheduled_on = NO_CPU;
+ }
+ t->rt_param.linked_on = NO_CPU;
+
+ gsnedf_job_arrival(t);
+ raw_spin_unlock_irqrestore(&gsnedf_lock, flags);
+}
+
+static void gsnedf_task_wake_up(struct task_struct *task)
+{
+ unsigned long flags;
+ lt_t now;
+
+ TRACE_TASK(task, "wake_up at %llu\n", litmus_clock());
+
+ raw_spin_lock_irqsave(&gsnedf_lock, flags);
+ /* We need to take suspensions because of semaphores into
+ * account! If a job resumes after being suspended due to acquiring
+ * a semaphore, it should never be treated as a new job release.
+ */
+ if (get_rt_flags(task) == RT_F_EXIT_SEM) {
+ set_rt_flags(task, RT_F_RUNNING);
+ } else {
+ now = litmus_clock();
+ if (is_tardy(task, now)) {
+ /* new sporadic release */
+ release_at(task, now);
+ sched_trace_task_release(task);
+ }
+ else {
+ if (task->rt.time_slice) {
+ /* came back in time before deadline
+ */
+ set_rt_flags(task, RT_F_RUNNING);
+ }
+ }
+ }
+ gsnedf_job_arrival(task);
+ raw_spin_unlock_irqrestore(&gsnedf_lock, flags);
+}
+
+static void gsnedf_task_block(struct task_struct *t)
+{
+ unsigned long flags;
+
+ TRACE_TASK(t, "block at %llu\n", litmus_clock());
+
+ /* unlink if necessary */
+ raw_spin_lock_irqsave(&gsnedf_lock, flags);
+ unlink(t);
+ raw_spin_unlock_irqrestore(&gsnedf_lock, flags);
+
+ BUG_ON(!is_realtime(t));
+}
+
+
+static void gsnedf_task_exit(struct task_struct * t)
+{
+ unsigned long flags;
+
+ /* unlink if necessary */
+ raw_spin_lock_irqsave(&gsnedf_lock, flags);
+ unlink(t);
+ if (tsk_rt(t)->scheduled_on != NO_CPU) {
+ gsnedf_cpus[tsk_rt(t)->scheduled_on]->scheduled = NULL;
+ tsk_rt(t)->scheduled_on = NO_CPU;
+ }
+ raw_spin_unlock_irqrestore(&gsnedf_lock, flags);
+
+ BUG_ON(!is_realtime(t));
+ TRACE_TASK(t, "RIP\n");
+}
+
+#ifdef CONFIG_FMLP
+
+/* Update the queue position of a task that got it's priority boosted via
+ * priority inheritance. */
+static void update_queue_position(struct task_struct *holder)
+{
+ /* We don't know whether holder is in the ready queue. It should, but
+ * on a budget overrun it may already be in a release queue. Hence,
+ * calling unlink() is not possible since it assumes that the task is
+ * not in a release queue. However, we can safely check whether
+ * sem->holder is currently in a queue or scheduled after locking both
+ * the release and the ready queue lock. */
+
+ /* Assumption: caller holds gsnedf_lock */
+
+ int check_preempt = 0;
+
+ if (tsk_rt(holder)->linked_on != NO_CPU) {
+ TRACE_TASK(holder, "%s: linked on %d\n",
+ __FUNCTION__, tsk_rt(holder)->linked_on);
+ /* Holder is scheduled; need to re-order CPUs.
+ * We can't use heap_decrease() here since
+ * the cpu_heap is ordered in reverse direction, so
+ * it is actually an increase. */
+ bheap_delete(cpu_lower_prio, &gsnedf_cpu_heap,
+ gsnedf_cpus[tsk_rt(holder)->linked_on]->hn);
+ bheap_insert(cpu_lower_prio, &gsnedf_cpu_heap,
+ gsnedf_cpus[tsk_rt(holder)->linked_on]->hn);
+ } else {
+ /* holder may be queued: first stop queue changes */
+ raw_spin_lock(&gsnedf.release_lock);
+ if (is_queued(holder)) {
+ TRACE_TASK(holder, "%s: is queued\n",
+ __FUNCTION__);
+ /* We need to update the position
+ * of holder in some heap. Note that this
+ * may be a release heap. */
+ check_preempt =
+ !bheap_decrease(edf_ready_order,
+ tsk_rt(holder)->heap_node);
+ } else {
+ /* Nothing to do: if it is not queued and not linked
+ * then it is currently being moved by other code
+ * (e.g., a timer interrupt handler) that will use the
+ * correct priority when enqueuing the task. */
+ TRACE_TASK(holder, "%s: is NOT queued => Done.\n",
+ __FUNCTION__);
+ }
+ raw_spin_unlock(&gsnedf.release_lock);
+
+ /* If holder was enqueued in a release heap, then the following
+ * preemption check is pointless, but we can't easily detect
+ * that case. If you want to fix this, then consider that
+ * simply adding a state flag requires O(n) time to update when
+ * releasing n tasks, which conflicts with the goal to have
+ * O(log n) merges. */
+ if (check_preempt) {
+ /* heap_decrease() hit the top level of the heap: make
+ * sure preemption checks get the right task, not the
+ * potentially stale cache. */
+ bheap_uncache_min(edf_ready_order,
+ &gsnedf.ready_queue);
+ check_for_preemptions();
+ }
+ }
+}
+
+static long gsnedf_pi_block(struct pi_semaphore *sem,
+ struct task_struct *new_waiter)
+{
+ /* This callback has to handle the situation where a new waiter is
+ * added to the wait queue of the semaphore.
+ *
+ * We must check if has a higher priority than the currently
+ * highest-priority task, and then potentially reschedule.
+ */
+
+ BUG_ON(!new_waiter);
+
+ if (edf_higher_prio(new_waiter, sem->hp.task)) {
+ TRACE_TASK(new_waiter, " boosts priority via %p\n", sem);
+ /* called with IRQs disabled */
+ raw_spin_lock(&gsnedf_lock);
+ /* store new highest-priority task */
+ sem->hp.task = new_waiter;
+ if (sem->holder) {
+ TRACE_TASK(sem->holder,
+ " holds %p and will inherit from %s/%d\n",
+ sem,
+ new_waiter->comm, new_waiter->pid);
+ /* let holder inherit */
+ sem->holder->rt_param.inh_task = new_waiter;
+ update_queue_position(sem->holder);
+ }
+ raw_spin_unlock(&gsnedf_lock);
+ }
+
+ return 0;
+}
+
+static long gsnedf_inherit_priority(struct pi_semaphore *sem,
+ struct task_struct *new_owner)
+{
+ /* We don't need to acquire the gsnedf_lock since at the time of this
+ * call new_owner isn't actually scheduled yet (it's still sleeping)
+ * and since the calling function already holds sem->wait.lock, which
+ * prevents concurrent sem->hp.task changes.
+ */
+
+ if (sem->hp.task && sem->hp.task != new_owner) {
+ new_owner->rt_param.inh_task = sem->hp.task;
+ TRACE_TASK(new_owner, "inherited priority from %s/%d\n",
+ sem->hp.task->comm, sem->hp.task->pid);
+ } else
+ TRACE_TASK(new_owner,
+ "cannot inherit priority, "
+ "no higher priority job waits.\n");
+ return 0;
+}
+
+/* This function is called on a semaphore release, and assumes that
+ * the current task is also the semaphore holder.
+ */
+static long gsnedf_return_priority(struct pi_semaphore *sem)
+{
+ struct task_struct* t = current;
+ int ret = 0;
+
+ /* Find new highest-priority semaphore task
+ * if holder task is the current hp.task.
+ *
+ * Calling function holds sem->wait.lock.
+ */
+ if (t == sem->hp.task)
+ edf_set_hp_task(sem);
+
+ TRACE_CUR("gsnedf_return_priority for lock %p\n", sem);
+
+ if (t->rt_param.inh_task) {
+ /* interrupts already disabled by PI code */
+ raw_spin_lock(&gsnedf_lock);
+
+ /* Reset inh_task to NULL. */
+ t->rt_param.inh_task = NULL;
+
+ /* Check if rescheduling is necessary */
+ unlink(t);
+ gsnedf_job_arrival(t);
+ raw_spin_unlock(&gsnedf_lock);
+ }
+
+ return ret;
+}
+
+#endif
+
+static long gsnedf_admit_task(struct task_struct* tsk)
+{
+ return 0;
+}
+
+static long gsnedf_activate_plugin(void)
+{
+ int cpu;
+ cpu_entry_t *entry;
+
+ bheap_init(&gsnedf_cpu_heap);
+#ifdef CONFIG_RELEASE_MASTER
+ gsnedf.release_master = atomic_read(&release_master_cpu);
+#endif
+
+ for_each_online_cpu(cpu) {
+ entry = &per_cpu(gsnedf_cpu_entries, cpu);
+ bheap_node_init(&entry->hn, entry);
+ entry->linked = NULL;
+ entry->scheduled = NULL;
+#ifdef CONFIG_RELEASE_MASTER
+ if (cpu != gsnedf.release_master) {
+#endif
+ TRACE("GSN-EDF: Initializing CPU #%d.\n", cpu);
+ update_cpu_position(entry);
+#ifdef CONFIG_RELEASE_MASTER
+ } else {
+ TRACE("GSN-EDF: CPU %d is release master.\n", cpu);
+ }
+#endif
+ }
+ return 0;
+}
+
+/* Plugin object */
+static struct sched_plugin gsn_edf_plugin __cacheline_aligned_in_smp = {
+ .plugin_name = "GSN-EDF",
+ .finish_switch = gsnedf_finish_switch,
+ .tick = gsnedf_tick,
+ .task_new = gsnedf_task_new,
+ .complete_job = complete_job,
+ .task_exit = gsnedf_task_exit,
+ .schedule = gsnedf_schedule,
+ .task_wake_up = gsnedf_task_wake_up,
+ .task_block = gsnedf_task_block,
+#ifdef CONFIG_FMLP
+ .fmlp_active = 1,
+ .pi_block = gsnedf_pi_block,
+ .inherit_priority = gsnedf_inherit_priority,
+ .return_priority = gsnedf_return_priority,
+#endif
+ .admit_task = gsnedf_admit_task,
+ .activate_plugin = gsnedf_activate_plugin,
+};
+
+
+static int __init init_gsn_edf(void)
+{
+ int cpu;
+ cpu_entry_t *entry;
+
+ bheap_init(&gsnedf_cpu_heap);
+ /* initialize CPU state */
+ for (cpu = 0; cpu < NR_CPUS; cpu++) {
+ entry = &per_cpu(gsnedf_cpu_entries, cpu);
+ gsnedf_cpus[cpu] = entry;
+ entry->cpu = cpu;
+ entry->hn = &gsnedf_heap_node[cpu];
+ bheap_node_init(&entry->hn, entry);
+ }
+ edf_domain_init(&gsnedf, NULL, gsnedf_release_jobs);
+ return register_sched_plugin(&gsn_edf_plugin);
+}
+
+
+module_init(init_gsn_edf);
diff --git a/litmus/sched_litmus.c b/litmus/sched_litmus.c
new file mode 100644
index 0000000..e695289
--- /dev/null
+++ b/litmus/sched_litmus.c
@@ -0,0 +1,320 @@
+/* This file is included from kernel/sched.c */
+
+#include <litmus/litmus.h>
+#include <litmus/budget.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/preempt.h>
+
+static void update_time_litmus(struct rq *rq, struct task_struct *p)
+{
+ u64 delta = rq->clock - p->se.exec_start;
+ if (unlikely((s64)delta < 0))
+ delta = 0;
+ /* per job counter */
+ p->rt_param.job_params.exec_time += delta;
+ /* task counter */
+ p->se.sum_exec_runtime += delta;
+ /* sched_clock() */
+ p->se.exec_start = rq->clock;
+ cpuacct_charge(p, delta);
+}
+
+static void double_rq_lock(struct rq *rq1, struct rq *rq2);
+static void double_rq_unlock(struct rq *rq1, struct rq *rq2);
+
+/*
+ * litmus_tick gets called by scheduler_tick() with HZ freq
+ * Interrupts are disabled
+ */
+static void litmus_tick(struct rq *rq, struct task_struct *p)
+{
+ TS_PLUGIN_TICK_START;
+
+ if (is_realtime(p))
+ update_time_litmus(rq, p);
+
+ /* plugin tick */
+ litmus->tick(p);
+
+ TS_PLUGIN_TICK_END;
+
+ return;
+}
+
+static struct task_struct *
+litmus_schedule(struct rq *rq, struct task_struct *prev)
+{
+ struct rq* other_rq;
+ struct task_struct *next;
+
+ long was_running;
+ lt_t _maybe_deadlock = 0;
+
+ /* let the plugin schedule */
+ next = litmus->schedule(prev);
+
+ sched_state_plugin_check();
+
+ /* check if a global plugin pulled a task from a different RQ */
+ if (next && task_rq(next) != rq) {
+ /* we need to migrate the task */
+ other_rq = task_rq(next);
+ TRACE_TASK(next, "migrate from %d\n", other_rq->cpu);
+
+ /* while we drop the lock, the prev task could change its
+ * state
+ */
+ was_running = is_running(prev);
+ mb();
+ raw_spin_unlock(&rq->lock);
+
+ /* Don't race with a concurrent switch. This could deadlock in
+ * the case of cross or circular migrations. It's the job of
+ * the plugin to make sure that doesn't happen.
+ */
+ TRACE_TASK(next, "stack_in_use=%d\n",
+ next->rt_param.stack_in_use);
+ if (next->rt_param.stack_in_use != NO_CPU) {
+ TRACE_TASK(next, "waiting to deschedule\n");
+ _maybe_deadlock = litmus_clock();
+ }
+ while (next->rt_param.stack_in_use != NO_CPU) {
+ cpu_relax();
+ mb();
+ if (next->rt_param.stack_in_use == NO_CPU)
+ TRACE_TASK(next,"descheduled. Proceeding.\n");
+
+ if (lt_before(_maybe_deadlock + 10000000,
+ litmus_clock())) {
+ /* We've been spinning for 10ms.
+ * Something can't be right!
+ * Let's abandon the task and bail out; at least
+ * we will have debug info instead of a hard
+ * deadlock.
+ */
+ TRACE_TASK(next,"stack too long in use. "
+ "Deadlock?\n");
+ next = NULL;
+
+ /* bail out */
+ raw_spin_lock(&rq->lock);
+ return next;
+ }
+ }
+#ifdef __ARCH_WANT_UNLOCKED_CTXSW
+ if (next->oncpu)
+ TRACE_TASK(next, "waiting for !oncpu");
+ while (next->oncpu) {
+ cpu_relax();
+ mb();
+ }
+#endif
+ double_rq_lock(rq, other_rq);
+ mb();
+ if (is_realtime(prev) && is_running(prev) != was_running) {
+ TRACE_TASK(prev,
+ "state changed while we dropped"
+ " the lock: is_running=%d, was_running=%d\n",
+ is_running(prev), was_running);
+ if (is_running(prev) && !was_running) {
+ /* prev task became unblocked
+ * we need to simulate normal sequence of events
+ * to scheduler plugins.
+ */
+ litmus->task_block(prev);
+ litmus->task_wake_up(prev);
+ }
+ }
+
+ set_task_cpu(next, smp_processor_id());
+
+ /* DEBUG: now that we have the lock we need to make sure a
+ * couple of things still hold:
+ * - it is still a real-time task
+ * - it is still runnable (could have been stopped)
+ * If either is violated, then the active plugin is
+ * doing something wrong.
+ */
+ if (!is_realtime(next) || !is_running(next)) {
+ /* BAD BAD BAD */
+ TRACE_TASK(next,"BAD: migration invariant FAILED: "
+ "rt=%d running=%d\n",
+ is_realtime(next),
+ is_running(next));
+ /* drop the task */
+ next = NULL;
+ }
+ /* release the other CPU's runqueue, but keep ours */
+ raw_spin_unlock(&other_rq->lock);
+ }
+ if (next) {
+ next->rt_param.stack_in_use = rq->cpu;
+ next->se.exec_start = rq->clock;
+ }
+
+ update_enforcement_timer(next);
+ return next;
+}
+
+static void enqueue_task_litmus(struct rq *rq, struct task_struct *p,
+ int flags)
+{
+ if (flags & ENQUEUE_WAKEUP) {
+ sched_trace_task_resume(p);
+ tsk_rt(p)->present = 1;
+ /* LITMUS^RT plugins need to update the state
+ * _before_ making it available in global structures.
+ * Linux gets away with being lazy about the task state
+ * update. We can't do that, hence we update the task
+ * state already here.
+ *
+ * WARNING: this needs to be re-evaluated when porting
+ * to newer kernel versions.
+ */
+ p->state = TASK_RUNNING;
+ litmus->task_wake_up(p);
+
+ rq->litmus.nr_running++;
+ } else
+ TRACE_TASK(p, "ignoring an enqueue, not a wake up.\n");
+}
+
+static void dequeue_task_litmus(struct rq *rq, struct task_struct *p,
+ int flags)
+{
+ if (flags & DEQUEUE_SLEEP) {
+ litmus->task_block(p);
+ tsk_rt(p)->present = 0;
+ sched_trace_task_block(p);
+
+ rq->litmus.nr_running--;
+ } else
+ TRACE_TASK(p, "ignoring a dequeue, not going to sleep.\n");
+}
+
+static void yield_task_litmus(struct rq *rq)
+{
+ BUG_ON(rq->curr != current);
+ /* sched_yield() is called to trigger delayed preemptions.
+ * Thus, mark the current task as needing to be rescheduled.
+ * This will cause the scheduler plugin to be invoked, which can
+ * then determine if a preemption is still required.
+ */
+ clear_exit_np(current);
+ litmus_reschedule_local();
+}
+
+/* Plugins are responsible for this.
+ */
+static void check_preempt_curr_litmus(struct rq *rq, struct task_struct *p, int flags)
+{
+}
+
+static void put_prev_task_litmus(struct rq *rq, struct task_struct *p)
+{
+}
+
+static void pre_schedule_litmus(struct rq *rq, struct task_struct *prev)
+{
+ update_time_litmus(rq, prev);
+ if (!is_running(prev))
+ tsk_rt(prev)->present = 0;
+}
+
+/* pick_next_task_litmus() - litmus_schedule() function
+ *
+ * return the next task to be scheduled
+ */
+static struct task_struct *pick_next_task_litmus(struct rq *rq)
+{
+ /* get the to-be-switched-out task (prev) */
+ struct task_struct *prev = rq->litmus.prev;
+ struct task_struct *next;
+
+ /* if not called from schedule() but from somewhere
+ * else (e.g., migration), return now!
+ */
+ if(!rq->litmus.prev)
+ return NULL;
+
+ rq->litmus.prev = NULL;
+
+ TS_PLUGIN_SCHED_START;
+ next = litmus_schedule(rq, prev);
+ TS_PLUGIN_SCHED_END;
+
+ return next;
+}
+
+static void task_tick_litmus(struct rq *rq, struct task_struct *p, int queued)
+{
+ /* nothing to do; tick related tasks are done by litmus_tick() */
+ return;
+}
+
+static void switched_to_litmus(struct rq *rq, struct task_struct *p, int running)
+{
+}
+
+static void prio_changed_litmus(struct rq *rq, struct task_struct *p,
+ int oldprio, int running)
+{
+}
+
+unsigned int get_rr_interval_litmus(struct rq *rq, struct task_struct *p)
+{
+ /* return infinity */
+ return 0;
+}
+
+/* This is called when a task became a real-time task, either due to a SCHED_*
+ * class transition or due to PI mutex inheritance. We don't handle Linux PI
+ * mutex inheritance yet (and probably never will). Use LITMUS provided
+ * synchronization primitives instead.
+ */
+static void set_curr_task_litmus(struct rq *rq)
+{
+ rq->curr->se.exec_start = rq->clock;
+}
+
+
+#ifdef CONFIG_SMP
+/* execve tries to rebalance task in this scheduling domain.
+ * We don't care about the scheduling domain; can gets called from
+ * exec, fork, wakeup.
+ */
+static int select_task_rq_litmus(struct rq *rq, struct task_struct *p,
+ int sd_flag, int flags)
+{
+ /* preemption is already disabled.
+ * We don't want to change cpu here
+ */
+ return task_cpu(p);
+}
+#endif
+
+static const struct sched_class litmus_sched_class = {
+ .next = &rt_sched_class,
+ .enqueue_task = enqueue_task_litmus,
+ .dequeue_task = dequeue_task_litmus,
+ .yield_task = yield_task_litmus,
+
+ .check_preempt_curr = check_preempt_curr_litmus,
+
+ .pick_next_task = pick_next_task_litmus,
+ .put_prev_task = put_prev_task_litmus,
+
+#ifdef CONFIG_SMP
+ .select_task_rq = select_task_rq_litmus,
+
+ .pre_schedule = pre_schedule_litmus,
+#endif
+
+ .set_curr_task = set_curr_task_litmus,
+ .task_tick = task_tick_litmus,
+
+ .get_rr_interval = get_rr_interval_litmus,
+
+ .prio_changed = prio_changed_litmus,
+ .switched_to = switched_to_litmus,
+};
diff --git a/litmus/sched_pfair.c b/litmus/sched_pfair.c
new file mode 100644
index 0000000..c7d5cf7
--- /dev/null
+++ b/litmus/sched_pfair.c
@@ -0,0 +1,894 @@
+/*
+ * kernel/sched_pfair.c
+ *
+ * Implementation of the (global) Pfair scheduling algorithm.
+ *
+ */
+
+#include <asm/div64.h>
+#include <linux/delay.h>
+#include <linux/module.h>
+#include <linux/spinlock.h>
+#include <linux/percpu.h>
+#include <linux/sched.h>
+#include <linux/list.h>
+#include <linux/slab.h>
+
+#include <litmus/litmus.h>
+#include <litmus/jobs.h>
+#include <litmus/preempt.h>
+#include <litmus/rt_domain.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/sched_trace.h>
+
+#include <litmus/bheap.h>
+
+struct subtask {
+ /* measured in quanta relative to job release */
+ quanta_t release;
+ quanta_t deadline;
+ quanta_t overlap; /* called "b bit" by PD^2 */
+ quanta_t group_deadline;
+};
+
+struct pfair_param {
+ quanta_t quanta; /* number of subtasks */
+ quanta_t cur; /* index of current subtask */
+
+ quanta_t release; /* in quanta */
+ quanta_t period; /* in quanta */
+
+ quanta_t last_quantum; /* when scheduled last */
+ int last_cpu; /* where scheduled last */
+
+ unsigned int sporadic_release; /* On wakeup, new sporadic release? */
+
+ struct subtask subtasks[0]; /* allocate together with pfair_param */
+};
+
+#define tsk_pfair(tsk) ((tsk)->rt_param.pfair)
+
+struct pfair_state {
+ int cpu;
+ volatile quanta_t cur_tick; /* updated by the CPU that is advancing
+ * the time */
+ volatile quanta_t local_tick; /* What tick is the local CPU currently
+ * executing? Updated only by the local
+ * CPU. In QEMU, this may lag behind the
+ * current tick. In a real system, with
+ * proper timers and aligned quanta,
+ * that should only be the
+ * case for a very short time after the
+ * time advanced. With staggered quanta,
+ * it will lag for the duration of the
+ * offset.
+ */
+
+ struct task_struct* linked; /* the task that should be executing */
+ struct task_struct* local; /* the local copy of linked */
+ struct task_struct* scheduled; /* what is actually scheduled */
+
+ unsigned long missed_quanta;
+ lt_t offset; /* stagger offset */
+};
+
+/* Currently, we limit the maximum period of any task to 2000 quanta.
+ * The reason is that it makes the implementation easier since we do not
+ * need to reallocate the release wheel on task arrivals.
+ * In the future
+ */
+#define PFAIR_MAX_PERIOD 2000
+
+/* This is the release queue wheel. It is indexed by pfair_time %
+ * PFAIR_MAX_PERIOD. Each heap is ordered by PFAIR priority, so that it can be
+ * merged with the ready queue.
+ */
+static struct bheap release_queue[PFAIR_MAX_PERIOD];
+
+DEFINE_PER_CPU(struct pfair_state, pfair_state);
+struct pfair_state* *pstate; /* short cut */
+
+static quanta_t pfair_time = 0; /* the "official" PFAIR clock */
+static quanta_t merge_time = 0; /* Updated after the release queue has been
+ * merged. Used by drop_all_references().
+ */
+
+static rt_domain_t pfair;
+
+/* The pfair_lock is used to serialize all scheduling events.
+ */
+#define pfair_lock pfair.ready_lock
+
+/* Enable for lots of trace info.
+ * #define PFAIR_DEBUG
+ */
+
+#ifdef PFAIR_DEBUG
+#define PTRACE_TASK(t, f, args...) TRACE_TASK(t, f, ## args)
+#define PTRACE(f, args...) TRACE(f, ## args)
+#else
+#define PTRACE_TASK(t, f, args...)
+#define PTRACE(f, args...)
+#endif
+
+/* gcc will inline all of these accessor functions... */
+static struct subtask* cur_subtask(struct task_struct* t)
+{
+ return tsk_pfair(t)->subtasks + tsk_pfair(t)->cur;
+}
+
+static quanta_t cur_deadline(struct task_struct* t)
+{
+ return cur_subtask(t)->deadline + tsk_pfair(t)->release;
+}
+
+
+static quanta_t cur_sub_release(struct task_struct* t)
+{
+ return cur_subtask(t)->release + tsk_pfair(t)->release;
+}
+
+static quanta_t cur_release(struct task_struct* t)
+{
+#ifdef EARLY_RELEASE
+ /* only the release of the first subtask counts when we early
+ * release */
+ return tsk_pfair(t)->release;
+#else
+ return cur_sub_release(t);
+#endif
+}
+
+static quanta_t cur_overlap(struct task_struct* t)
+{
+ return cur_subtask(t)->overlap;
+}
+
+static quanta_t cur_group_deadline(struct task_struct* t)
+{
+ quanta_t gdl = cur_subtask(t)->group_deadline;
+ if (gdl)
+ return gdl + tsk_pfair(t)->release;
+ else
+ return gdl;
+}
+
+
+static int pfair_higher_prio(struct task_struct* first,
+ struct task_struct* second)
+{
+ return /* first task must exist */
+ first && (
+ /* Does the second task exist and is it a real-time task? If
+ * not, the first task (which is a RT task) has higher
+ * priority.
+ */
+ !second || !is_realtime(second) ||
+
+ /* Is the (subtask) deadline of the first task earlier?
+ * Then it has higher priority.
+ */
+ time_before(cur_deadline(first), cur_deadline(second)) ||
+
+ /* Do we have a deadline tie?
+ * Then break by B-bit.
+ */
+ (cur_deadline(first) == cur_deadline(second) &&
+ (cur_overlap(first) > cur_overlap(second) ||
+
+ /* Do we have a B-bit tie?
+ * Then break by group deadline.
+ */
+ (cur_overlap(first) == cur_overlap(second) &&
+ (time_after(cur_group_deadline(first),
+ cur_group_deadline(second)) ||
+
+ /* Do we have a group deadline tie?
+ * Then break by PID, which are unique.
+ */
+ (cur_group_deadline(first) ==
+ cur_group_deadline(second) &&
+ first->pid < second->pid))))));
+}
+
+int pfair_ready_order(struct bheap_node* a, struct bheap_node* b)
+{
+ return pfair_higher_prio(bheap2task(a), bheap2task(b));
+}
+
+/* return the proper release queue for time t */
+static struct bheap* relq(quanta_t t)
+{
+ struct bheap* rq = &release_queue[t % PFAIR_MAX_PERIOD];
+ return rq;
+}
+
+static void prepare_release(struct task_struct* t, quanta_t at)
+{
+ tsk_pfair(t)->release = at;
+ tsk_pfair(t)->cur = 0;
+}
+
+static void __pfair_add_release(struct task_struct* t, struct bheap* queue)
+{
+ bheap_insert(pfair_ready_order, queue,
+ tsk_rt(t)->heap_node);
+}
+
+static void pfair_add_release(struct task_struct* t)
+{
+ BUG_ON(bheap_node_in_heap(tsk_rt(t)->heap_node));
+ __pfair_add_release(t, relq(cur_release(t)));
+}
+
+/* pull released tasks from the release queue */
+static void poll_releases(quanta_t time)
+{
+ __merge_ready(&pfair, relq(time));
+ merge_time = time;
+}
+
+static void check_preempt(struct task_struct* t)
+{
+ int cpu = NO_CPU;
+ if (tsk_rt(t)->linked_on != tsk_rt(t)->scheduled_on &&
+ tsk_rt(t)->present) {
+ /* the task can be scheduled and
+ * is not scheduled where it ought to be scheduled
+ */
+ cpu = tsk_rt(t)->linked_on != NO_CPU ?
+ tsk_rt(t)->linked_on :
+ tsk_rt(t)->scheduled_on;
+ PTRACE_TASK(t, "linked_on:%d, scheduled_on:%d\n",
+ tsk_rt(t)->linked_on, tsk_rt(t)->scheduled_on);
+ /* preempt */
+ litmus_reschedule(cpu);
+ }
+}
+
+/* caller must hold pfair_lock */
+static void drop_all_references(struct task_struct *t)
+{
+ int cpu;
+ struct pfair_state* s;
+ struct bheap* q;
+ if (bheap_node_in_heap(tsk_rt(t)->heap_node)) {
+ /* figure out what queue the node is in */
+ if (time_before_eq(cur_release(t), merge_time))
+ q = &pfair.ready_queue;
+ else
+ q = relq(cur_release(t));
+ bheap_delete(pfair_ready_order, q,
+ tsk_rt(t)->heap_node);
+ }
+ for (cpu = 0; cpu < num_online_cpus(); cpu++) {
+ s = &per_cpu(pfair_state, cpu);
+ if (s->linked == t)
+ s->linked = NULL;
+ if (s->local == t)
+ s->local = NULL;
+ if (s->scheduled == t)
+ s->scheduled = NULL;
+ }
+}
+
+/* returns 1 if the task needs to go the release queue */
+static int advance_subtask(quanta_t time, struct task_struct* t, int cpu)
+{
+ struct pfair_param* p = tsk_pfair(t);
+ int to_relq;
+ p->cur = (p->cur + 1) % p->quanta;
+ if (!p->cur) {
+ sched_trace_task_completion(t, 1);
+ if (tsk_rt(t)->present) {
+ /* we start a new job */
+ prepare_for_next_period(t);
+ sched_trace_task_release(t);
+ get_rt_flags(t) = RT_F_RUNNING;
+ p->release += p->period;
+ } else {
+ /* remove task from system until it wakes */
+ drop_all_references(t);
+ tsk_pfair(t)->sporadic_release = 1;
+ TRACE_TASK(t, "on %d advanced to subtask %lu (not present)\n",
+ cpu, p->cur);
+ return 0;
+ }
+ }
+ to_relq = time_after(cur_release(t), time);
+ TRACE_TASK(t, "on %d advanced to subtask %lu -> to_relq=%d\n",
+ cpu, p->cur, to_relq);
+ return to_relq;
+}
+
+static void advance_subtasks(quanta_t time)
+{
+ int cpu, missed;
+ struct task_struct* l;
+ struct pfair_param* p;
+
+ for_each_online_cpu(cpu) {
+ l = pstate[cpu]->linked;
+ missed = pstate[cpu]->linked != pstate[cpu]->local;
+ if (l) {
+ p = tsk_pfair(l);
+ p->last_quantum = time;
+ p->last_cpu = cpu;
+ if (advance_subtask(time, l, cpu)) {
+ pstate[cpu]->linked = NULL;
+ pfair_add_release(l);
+ }
+ }
+ }
+}
+
+static int target_cpu(quanta_t time, struct task_struct* t, int default_cpu)
+{
+ int cpu;
+ if (tsk_rt(t)->scheduled_on != NO_CPU) {
+ /* always observe scheduled_on linkage */
+ default_cpu = tsk_rt(t)->scheduled_on;
+ } else if (tsk_pfair(t)->last_quantum == time - 1) {
+ /* back2back quanta */
+ /* Only observe last_quantum if no scheduled_on is in the way.
+ * This should only kick in if a CPU missed quanta, and that
+ * *should* only happen in QEMU.
+ */
+ cpu = tsk_pfair(t)->last_cpu;
+ if (!pstate[cpu]->linked ||
+ tsk_rt(pstate[cpu]->linked)->scheduled_on != cpu) {
+ default_cpu = cpu;
+ }
+ }
+ return default_cpu;
+}
+
+/* returns one if linking was redirected */
+static int pfair_link(quanta_t time, int cpu,
+ struct task_struct* t)
+{
+ int target = target_cpu(time, t, cpu);
+ struct task_struct* prev = pstate[cpu]->linked;
+ struct task_struct* other;
+
+ if (target != cpu) {
+ other = pstate[target]->linked;
+ pstate[target]->linked = t;
+ tsk_rt(t)->linked_on = target;
+ if (!other)
+ /* linked ok, but reschedule this CPU */
+ return 1;
+ if (target < cpu) {
+ /* link other to cpu instead */
+ tsk_rt(other)->linked_on = cpu;
+ pstate[cpu]->linked = other;
+ if (prev) {
+ /* prev got pushed back into the ready queue */
+ tsk_rt(prev)->linked_on = NO_CPU;
+ __add_ready(&pfair, prev);
+ }
+ /* we are done with this cpu */
+ return 0;
+ } else {
+ /* re-add other, it's original CPU was not considered yet */
+ tsk_rt(other)->linked_on = NO_CPU;
+ __add_ready(&pfair, other);
+ /* reschedule this CPU */
+ return 1;
+ }
+ } else {
+ pstate[cpu]->linked = t;
+ tsk_rt(t)->linked_on = cpu;
+ if (prev) {
+ /* prev got pushed back into the ready queue */
+ tsk_rt(prev)->linked_on = NO_CPU;
+ __add_ready(&pfair, prev);
+ }
+ /* we are done with this CPU */
+ return 0;
+ }
+}
+
+static void schedule_subtasks(quanta_t time)
+{
+ int cpu, retry;
+
+ for_each_online_cpu(cpu) {
+ retry = 1;
+ while (retry) {
+ if (pfair_higher_prio(__peek_ready(&pfair),
+ pstate[cpu]->linked))
+ retry = pfair_link(time, cpu,
+ __take_ready(&pfair));
+ else
+ retry = 0;
+ }
+ }
+}
+
+static void schedule_next_quantum(quanta_t time)
+{
+ int cpu;
+
+ /* called with interrupts disabled */
+ PTRACE("--- Q %lu at %llu PRE-SPIN\n",
+ time, litmus_clock());
+ raw_spin_lock(&pfair_lock);
+ PTRACE("<<< Q %lu at %llu\n",
+ time, litmus_clock());
+
+ sched_trace_quantum_boundary();
+
+ advance_subtasks(time);
+ poll_releases(time);
+ schedule_subtasks(time);
+
+ for (cpu = 0; cpu < num_online_cpus(); cpu++)
+ if (pstate[cpu]->linked)
+ PTRACE_TASK(pstate[cpu]->linked,
+ " linked on %d.\n", cpu);
+ else
+ PTRACE("(null) linked on %d.\n", cpu);
+
+ /* We are done. Advance time. */
+ mb();
+ for (cpu = 0; cpu < num_online_cpus(); cpu++) {
+ if (pstate[cpu]->local_tick != pstate[cpu]->cur_tick) {
+ TRACE("BAD Quantum not acked on %d "
+ "(l:%lu c:%lu p:%lu)\n",
+ cpu,
+ pstate[cpu]->local_tick,
+ pstate[cpu]->cur_tick,
+ pfair_time);
+ pstate[cpu]->missed_quanta++;
+ }
+ pstate[cpu]->cur_tick = time;
+ }
+ PTRACE(">>> Q %lu at %llu\n",
+ time, litmus_clock());
+ raw_spin_unlock(&pfair_lock);
+}
+
+static noinline void wait_for_quantum(quanta_t q, struct pfair_state* state)
+{
+ quanta_t loc;
+
+ goto first; /* skip mb() on first iteration */
+ do {
+ cpu_relax();
+ mb();
+ first: loc = state->cur_tick;
+ /* FIXME: what if loc > cur? */
+ } while (time_before(loc, q));
+ PTRACE("observed cur_tick:%lu >= q:%lu\n",
+ loc, q);
+}
+
+static quanta_t current_quantum(struct pfair_state* state)
+{
+ lt_t t = litmus_clock() - state->offset;
+ return time2quanta(t, FLOOR);
+}
+
+static void catchup_quanta(quanta_t from, quanta_t target,
+ struct pfair_state* state)
+{
+ quanta_t cur = from, time;
+ TRACE("+++< BAD catching up quanta from %lu to %lu\n",
+ from, target);
+ while (time_before(cur, target)) {
+ wait_for_quantum(cur, state);
+ cur++;
+ time = cmpxchg(&pfair_time,
+ cur - 1, /* expected */
+ cur /* next */
+ );
+ if (time == cur - 1)
+ schedule_next_quantum(cur);
+ }
+ TRACE("+++> catching up done\n");
+}
+
+/* pfair_tick - this function is called for every local timer
+ * interrupt.
+ */
+static void pfair_tick(struct task_struct* t)
+{
+ struct pfair_state* state = &__get_cpu_var(pfair_state);
+ quanta_t time, cur;
+ int retry = 10;
+
+ do {
+ cur = current_quantum(state);
+ PTRACE("q %lu at %llu\n", cur, litmus_clock());
+
+ /* Attempt to advance time. First CPU to get here
+ * will prepare the next quantum.
+ */
+ time = cmpxchg(&pfair_time,
+ cur - 1, /* expected */
+ cur /* next */
+ );
+ if (time == cur - 1) {
+ /* exchange succeeded */
+ wait_for_quantum(cur - 1, state);
+ schedule_next_quantum(cur);
+ retry = 0;
+ } else if (time_before(time, cur - 1)) {
+ /* the whole system missed a tick !? */
+ catchup_quanta(time, cur, state);
+ retry--;
+ } else if (time_after(time, cur)) {
+ /* our timer lagging behind!? */
+ TRACE("BAD pfair_time:%lu > cur:%lu\n", time, cur);
+ retry--;
+ } else {
+ /* Some other CPU already started scheduling
+ * this quantum. Let it do its job and then update.
+ */
+ retry = 0;
+ }
+ } while (retry);
+
+ /* Spin locally until time advances. */
+ wait_for_quantum(cur, state);
+
+ /* copy assignment */
+ /* FIXME: what if we race with a future update? Corrupted state? */
+ state->local = state->linked;
+ /* signal that we are done */
+ mb();
+ state->local_tick = state->cur_tick;
+
+ if (state->local != current
+ && (is_realtime(current) || is_present(state->local)))
+ litmus_reschedule_local();
+}
+
+static int safe_to_schedule(struct task_struct* t, int cpu)
+{
+ int where = tsk_rt(t)->scheduled_on;
+ if (where != NO_CPU && where != cpu) {
+ TRACE_TASK(t, "BAD: can't be scheduled on %d, "
+ "scheduled already on %d.\n", cpu, where);
+ return 0;
+ } else
+ return tsk_rt(t)->present && get_rt_flags(t) == RT_F_RUNNING;
+}
+
+static struct task_struct* pfair_schedule(struct task_struct * prev)
+{
+ struct pfair_state* state = &__get_cpu_var(pfair_state);
+ int blocks;
+ struct task_struct* next = NULL;
+
+ raw_spin_lock(&pfair_lock);
+
+ blocks = is_realtime(prev) && !is_running(prev);
+
+ if (state->local && safe_to_schedule(state->local, state->cpu))
+ next = state->local;
+
+ if (prev != next) {
+ tsk_rt(prev)->scheduled_on = NO_CPU;
+ if (next)
+ tsk_rt(next)->scheduled_on = state->cpu;
+ }
+ sched_state_task_picked();
+ raw_spin_unlock(&pfair_lock);
+
+ if (next)
+ TRACE_TASK(next, "scheduled rel=%lu at %lu (%llu)\n",
+ tsk_pfair(next)->release, pfair_time, litmus_clock());
+ else if (is_realtime(prev))
+ TRACE("Becomes idle at %lu (%llu)\n", pfair_time, litmus_clock());
+
+ return next;
+}
+
+static void pfair_task_new(struct task_struct * t, int on_rq, int running)
+{
+ unsigned long flags;
+
+ TRACE("pfair: task new %d state:%d\n", t->pid, t->state);
+
+ raw_spin_lock_irqsave(&pfair_lock, flags);
+ if (running)
+ t->rt_param.scheduled_on = task_cpu(t);
+ else
+ t->rt_param.scheduled_on = NO_CPU;
+
+ prepare_release(t, pfair_time + 1);
+ tsk_pfair(t)->sporadic_release = 0;
+ pfair_add_release(t);
+ check_preempt(t);
+
+ raw_spin_unlock_irqrestore(&pfair_lock, flags);
+}
+
+static void pfair_task_wake_up(struct task_struct *t)
+{
+ unsigned long flags;
+ lt_t now;
+
+ TRACE_TASK(t, "wakes at %llu, release=%lu, pfair_time:%lu\n",
+ litmus_clock(), cur_release(t), pfair_time);
+
+ raw_spin_lock_irqsave(&pfair_lock, flags);
+
+ /* It is a little unclear how to deal with Pfair
+ * tasks that block for a while and then wake. For now,
+ * if a task blocks and wakes before its next job release,
+ * then it may resume if it is currently linked somewhere
+ * (as if it never blocked at all). Otherwise, we have a
+ * new sporadic job release.
+ */
+ if (tsk_pfair(t)->sporadic_release) {
+ now = litmus_clock();
+ release_at(t, now);
+ prepare_release(t, time2quanta(now, CEIL));
+ sched_trace_task_release(t);
+ /* FIXME: race with pfair_time advancing */
+ pfair_add_release(t);
+ tsk_pfair(t)->sporadic_release = 0;
+ }
+
+ check_preempt(t);
+
+ raw_spin_unlock_irqrestore(&pfair_lock, flags);
+ TRACE_TASK(t, "wake up done at %llu\n", litmus_clock());
+}
+
+static void pfair_task_block(struct task_struct *t)
+{
+ BUG_ON(!is_realtime(t));
+ TRACE_TASK(t, "blocks at %llu, state:%d\n",
+ litmus_clock(), t->state);
+}
+
+static void pfair_task_exit(struct task_struct * t)
+{
+ unsigned long flags;
+
+ BUG_ON(!is_realtime(t));
+
+ /* Remote task from release or ready queue, and ensure
+ * that it is not the scheduled task for ANY CPU. We
+ * do this blanket check because occassionally when
+ * tasks exit while blocked, the task_cpu of the task
+ * might not be the same as the CPU that the PFAIR scheduler
+ * has chosen for it.
+ */
+ raw_spin_lock_irqsave(&pfair_lock, flags);
+
+ TRACE_TASK(t, "RIP, state:%d\n", t->state);
+ drop_all_references(t);
+
+ raw_spin_unlock_irqrestore(&pfair_lock, flags);
+
+ kfree(t->rt_param.pfair);
+ t->rt_param.pfair = NULL;
+}
+
+
+static void pfair_release_at(struct task_struct* task, lt_t start)
+{
+ unsigned long flags;
+ quanta_t release;
+
+ BUG_ON(!is_realtime(task));
+
+ raw_spin_lock_irqsave(&pfair_lock, flags);
+ release_at(task, start);
+ release = time2quanta(start, CEIL);
+
+ if (release - pfair_time >= PFAIR_MAX_PERIOD)
+ release = pfair_time + PFAIR_MAX_PERIOD;
+
+ TRACE_TASK(task, "sys release at %lu\n", release);
+
+ drop_all_references(task);
+ prepare_release(task, release);
+ pfair_add_release(task);
+
+ /* Clear sporadic release flag, since this release subsumes any
+ * sporadic release on wake.
+ */
+ tsk_pfair(task)->sporadic_release = 0;
+
+ raw_spin_unlock_irqrestore(&pfair_lock, flags);
+}
+
+static void init_subtask(struct subtask* sub, unsigned long i,
+ lt_t quanta, lt_t period)
+{
+ /* since i is zero-based, the formulas are shifted by one */
+ lt_t tmp;
+
+ /* release */
+ tmp = period * i;
+ do_div(tmp, quanta); /* floor */
+ sub->release = (quanta_t) tmp;
+
+ /* deadline */
+ tmp = period * (i + 1);
+ if (do_div(tmp, quanta)) /* ceil */
+ tmp++;
+ sub->deadline = (quanta_t) tmp;
+
+ /* next release */
+ tmp = period * (i + 1);
+ do_div(tmp, quanta); /* floor */
+ sub->overlap = sub->deadline - (quanta_t) tmp;
+
+ /* Group deadline.
+ * Based on the formula given in Uma's thesis.
+ */
+ if (2 * quanta >= period) {
+ /* heavy */
+ tmp = (sub->deadline - (i + 1)) * period;
+ if (period > quanta &&
+ do_div(tmp, (period - quanta))) /* ceil */
+ tmp++;
+ sub->group_deadline = (quanta_t) tmp;
+ } else
+ sub->group_deadline = 0;
+}
+
+static void dump_subtasks(struct task_struct* t)
+{
+ unsigned long i;
+ for (i = 0; i < t->rt_param.pfair->quanta; i++)
+ TRACE_TASK(t, "SUBTASK %lu: rel=%lu dl=%lu bbit:%lu gdl:%lu\n",
+ i + 1,
+ t->rt_param.pfair->subtasks[i].release,
+ t->rt_param.pfair->subtasks[i].deadline,
+ t->rt_param.pfair->subtasks[i].overlap,
+ t->rt_param.pfair->subtasks[i].group_deadline);
+}
+
+static long pfair_admit_task(struct task_struct* t)
+{
+ lt_t quanta;
+ lt_t period;
+ s64 quantum_length = ktime_to_ns(tick_period);
+ struct pfair_param* param;
+ unsigned long i;
+
+ /* Pfair is a tick-based method, so the time
+ * of interest is jiffies. Calculate tick-based
+ * times for everything.
+ * (Ceiling of exec cost, floor of period.)
+ */
+
+ quanta = get_exec_cost(t);
+ period = get_rt_period(t);
+
+ quanta = time2quanta(get_exec_cost(t), CEIL);
+
+ if (do_div(period, quantum_length))
+ printk(KERN_WARNING
+ "The period of %s/%d is not a multiple of %llu.\n",
+ t->comm, t->pid, (unsigned long long) quantum_length);
+
+ if (period >= PFAIR_MAX_PERIOD) {
+ printk(KERN_WARNING
+ "PFAIR: Rejecting task %s/%d; its period is too long.\n",
+ t->comm, t->pid);
+ return -EINVAL;
+ }
+
+ if (quanta == period) {
+ /* special case: task has weight 1.0 */
+ printk(KERN_INFO
+ "Admitting weight 1.0 task. (%s/%d, %llu, %llu).\n",
+ t->comm, t->pid, quanta, period);
+ quanta = 1;
+ period = 1;
+ }
+
+ param = kmalloc(sizeof(*param) +
+ quanta * sizeof(struct subtask), GFP_ATOMIC);
+
+ if (!param)
+ return -ENOMEM;
+
+ param->quanta = quanta;
+ param->cur = 0;
+ param->release = 0;
+ param->period = period;
+
+ for (i = 0; i < quanta; i++)
+ init_subtask(param->subtasks + i, i, quanta, period);
+
+ if (t->rt_param.pfair)
+ /* get rid of stale allocation */
+ kfree(t->rt_param.pfair);
+
+ t->rt_param.pfair = param;
+
+ /* spew out some debug info */
+ dump_subtasks(t);
+
+ return 0;
+}
+
+static long pfair_activate_plugin(void)
+{
+ int cpu;
+ struct pfair_state* state;
+
+ state = &__get_cpu_var(pfair_state);
+ pfair_time = current_quantum(state);
+
+ TRACE("Activating PFAIR at q=%lu\n", pfair_time);
+
+ for (cpu = 0; cpu < num_online_cpus(); cpu++) {
+ state = &per_cpu(pfair_state, cpu);
+ state->cur_tick = pfair_time;
+ state->local_tick = pfair_time;
+ state->missed_quanta = 0;
+ state->offset = cpu_stagger_offset(cpu);
+ }
+
+ return 0;
+}
+
+/* Plugin object */
+static struct sched_plugin pfair_plugin __cacheline_aligned_in_smp = {
+ .plugin_name = "PFAIR",
+ .tick = pfair_tick,
+ .task_new = pfair_task_new,
+ .task_exit = pfair_task_exit,
+ .schedule = pfair_schedule,
+ .task_wake_up = pfair_task_wake_up,
+ .task_block = pfair_task_block,
+ .admit_task = pfair_admit_task,
+ .release_at = pfair_release_at,
+ .complete_job = complete_job,
+ .activate_plugin = pfair_activate_plugin,
+};
+
+static int __init init_pfair(void)
+{
+ int cpu, i;
+ struct pfair_state *state;
+
+
+ /*
+ * initialize short_cut for per-cpu pfair state;
+ * there may be a problem here if someone removes a cpu
+ * while we are doing this initialization... and if cpus
+ * are added / removed later... is it a _real_ problem?
+ */
+ pstate = kmalloc(sizeof(struct pfair_state*) * num_online_cpus(), GFP_KERNEL);
+
+ /* initialize release queue */
+ for (i = 0; i < PFAIR_MAX_PERIOD; i++)
+ bheap_init(&release_queue[i]);
+
+ /* initialize CPU state */
+ for (cpu = 0; cpu < num_online_cpus(); cpu++) {
+ state = &per_cpu(pfair_state, cpu);
+ state->cpu = cpu;
+ state->cur_tick = 0;
+ state->local_tick = 0;
+ state->linked = NULL;
+ state->local = NULL;
+ state->scheduled = NULL;
+ state->missed_quanta = 0;
+ state->offset = cpu_stagger_offset(cpu);
+ pstate[cpu] = state;
+ }
+
+ rt_domain_init(&pfair, pfair_ready_order, NULL, NULL);
+ return register_sched_plugin(&pfair_plugin);
+}
+
+static void __exit clean_pfair(void)
+{
+ kfree(pstate);
+}
+
+module_init(init_pfair);
+module_exit(clean_pfair);
diff --git a/litmus/sched_plugin.c b/litmus/sched_plugin.c
new file mode 100644
index 0000000..d912a64
--- /dev/null
+++ b/litmus/sched_plugin.c
@@ -0,0 +1,253 @@
+/* sched_plugin.c -- core infrastructure for the scheduler plugin system
+ *
+ * This file includes the initialization of the plugin system, the no-op Linux
+ * scheduler plugin, some dummy functions, and some helper functions.
+ */
+
+#include <linux/list.h>
+#include <linux/spinlock.h>
+#include <linux/sched.h>
+
+#include <litmus/litmus.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/preempt.h>
+#include <litmus/jobs.h>
+
+/*
+ * Generic function to trigger preemption on either local or remote cpu
+ * from scheduler plugins. The key feature is that this function is
+ * non-preemptive section aware and does not invoke the scheduler / send
+ * IPIs if the to-be-preempted task is actually non-preemptive.
+ */
+void preempt_if_preemptable(struct task_struct* t, int cpu)
+{
+ /* t is the real-time task executing on CPU on_cpu If t is NULL, then
+ * on_cpu is currently scheduling background work.
+ */
+
+ int reschedule = 0;
+
+ if (!t)
+ /* move non-real-time task out of the way */
+ reschedule = 1;
+ else {
+ if (smp_processor_id() == cpu) {
+ /* local CPU case */
+ /* check if we need to poke userspace */
+ if (is_user_np(t))
+ /* yes, poke it */
+ request_exit_np(t);
+ else if (!is_kernel_np(t))
+ /* only if we are allowed to preempt the
+ * currently-executing task */
+ reschedule = 1;
+ } else {
+ /* remote CPU case */
+ if (is_user_np(t)) {
+ /* need to notify user space of delayed
+ * preemption */
+
+ /* to avoid a race, set the flag, then test
+ * again */
+ request_exit_np(t);
+ /* make sure it got written */
+ mb();
+ }
+ /* Only send an ipi if remote task might have raced our
+ * request, i.e., send an IPI to make sure in case it
+ * exited its critical section.
+ */
+ reschedule = !is_np(t) && !is_kernel_np(t);
+ }
+ }
+ if (likely(reschedule))
+ litmus_reschedule(cpu);
+}
+
+
+/*************************************************************
+ * Dummy plugin functions *
+ *************************************************************/
+
+static void litmus_dummy_finish_switch(struct task_struct * prev)
+{
+}
+
+static struct task_struct* litmus_dummy_schedule(struct task_struct * prev)
+{
+ sched_state_task_picked();
+ return NULL;
+}
+
+static void litmus_dummy_tick(struct task_struct* tsk)
+{
+}
+
+static long litmus_dummy_admit_task(struct task_struct* tsk)
+{
+ printk(KERN_CRIT "LITMUS^RT: Linux plugin rejects %s/%d.\n",
+ tsk->comm, tsk->pid);
+ return -EINVAL;
+}
+
+static void litmus_dummy_task_new(struct task_struct *t, int on_rq, int running)
+{
+}
+
+static void litmus_dummy_task_wake_up(struct task_struct *task)
+{
+}
+
+static void litmus_dummy_task_block(struct task_struct *task)
+{
+}
+
+static void litmus_dummy_task_exit(struct task_struct *task)
+{
+}
+
+static long litmus_dummy_complete_job(void)
+{
+ return -ENOSYS;
+}
+
+static long litmus_dummy_activate_plugin(void)
+{
+ return 0;
+}
+
+static long litmus_dummy_deactivate_plugin(void)
+{
+ return 0;
+}
+
+#ifdef CONFIG_FMLP
+
+static long litmus_dummy_inherit_priority(struct pi_semaphore *sem,
+ struct task_struct *new_owner)
+{
+ return -ENOSYS;
+}
+
+static long litmus_dummy_return_priority(struct pi_semaphore *sem)
+{
+ return -ENOSYS;
+}
+
+static long litmus_dummy_pi_block(struct pi_semaphore *sem,
+ struct task_struct *new_waiter)
+{
+ return -ENOSYS;
+}
+
+#endif
+
+
+/* The default scheduler plugin. It doesn't do anything and lets Linux do its
+ * job.
+ */
+struct sched_plugin linux_sched_plugin = {
+ .plugin_name = "Linux",
+ .tick = litmus_dummy_tick,
+ .task_new = litmus_dummy_task_new,
+ .task_exit = litmus_dummy_task_exit,
+ .task_wake_up = litmus_dummy_task_wake_up,
+ .task_block = litmus_dummy_task_block,
+ .complete_job = litmus_dummy_complete_job,
+ .schedule = litmus_dummy_schedule,
+ .finish_switch = litmus_dummy_finish_switch,
+ .activate_plugin = litmus_dummy_activate_plugin,
+ .deactivate_plugin = litmus_dummy_deactivate_plugin,
+#ifdef CONFIG_FMLP
+ .inherit_priority = litmus_dummy_inherit_priority,
+ .return_priority = litmus_dummy_return_priority,
+ .pi_block = litmus_dummy_pi_block,
+#endif
+ .admit_task = litmus_dummy_admit_task
+};
+
+/*
+ * The reference to current plugin that is used to schedule tasks within
+ * the system. It stores references to actual function implementations
+ * Should be initialized by calling "init_***_plugin()"
+ */
+struct sched_plugin *litmus = &linux_sched_plugin;
+
+/* the list of registered scheduling plugins */
+static LIST_HEAD(sched_plugins);
+static DEFINE_RAW_SPINLOCK(sched_plugins_lock);
+
+#define CHECK(func) {\
+ if (!plugin->func) \
+ plugin->func = litmus_dummy_ ## func;}
+
+/* FIXME: get reference to module */
+int register_sched_plugin(struct sched_plugin* plugin)
+{
+ printk(KERN_INFO "Registering LITMUS^RT plugin %s.\n",
+ plugin->plugin_name);
+
+ /* make sure we don't trip over null pointers later */
+ CHECK(finish_switch);
+ CHECK(schedule);
+ CHECK(tick);
+ CHECK(task_wake_up);
+ CHECK(task_exit);
+ CHECK(task_block);
+ CHECK(task_new);
+ CHECK(complete_job);
+ CHECK(activate_plugin);
+ CHECK(deactivate_plugin);
+#ifdef CONFIG_FMLP
+ CHECK(inherit_priority);
+ CHECK(return_priority);
+ CHECK(pi_block);
+#endif
+ CHECK(admit_task);
+
+ if (!plugin->release_at)
+ plugin->release_at = release_at;
+
+ raw_spin_lock(&sched_plugins_lock);
+ list_add(&plugin->list, &sched_plugins);
+ raw_spin_unlock(&sched_plugins_lock);
+
+ return 0;
+}
+
+
+/* FIXME: reference counting, etc. */
+struct sched_plugin* find_sched_plugin(const char* name)
+{
+ struct list_head *pos;
+ struct sched_plugin *plugin;
+
+ raw_spin_lock(&sched_plugins_lock);
+ list_for_each(pos, &sched_plugins) {
+ plugin = list_entry(pos, struct sched_plugin, list);
+ if (!strcmp(plugin->plugin_name, name))
+ goto out_unlock;
+ }
+ plugin = NULL;
+
+out_unlock:
+ raw_spin_unlock(&sched_plugins_lock);
+ return plugin;
+}
+
+int print_sched_plugins(char* buf, int max)
+{
+ int count = 0;
+ struct list_head *pos;
+ struct sched_plugin *plugin;
+
+ raw_spin_lock(&sched_plugins_lock);
+ list_for_each(pos, &sched_plugins) {
+ plugin = list_entry(pos, struct sched_plugin, list);
+ count += snprintf(buf + count, max - count, "%s\n", plugin->plugin_name);
+ if (max - count <= 0)
+ break;
+ }
+ raw_spin_unlock(&sched_plugins_lock);
+ return count;
+}
diff --git a/litmus/sched_psn_edf.c b/litmus/sched_psn_edf.c
new file mode 100644
index 0000000..b89823d
--- /dev/null
+++ b/litmus/sched_psn_edf.c
@@ -0,0 +1,483 @@
+/*
+ * kernel/sched_psn_edf.c
+ *
+ * Implementation of the PSN-EDF scheduler plugin.
+ * Based on kern/sched_part_edf.c and kern/sched_gsn_edf.c.
+ *
+ * Suspensions and non-preemptable sections are supported.
+ * Priority inheritance is not supported.
+ */
+
+#include <linux/percpu.h>
+#include <linux/sched.h>
+#include <linux/list.h>
+#include <linux/spinlock.h>
+#include <linux/module.h>
+
+#include <litmus/litmus.h>
+#include <litmus/jobs.h>
+#include <litmus/preempt.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/edf_common.h>
+#include <litmus/sched_trace.h>
+
+typedef struct {
+ rt_domain_t domain;
+ int cpu;
+ struct task_struct* scheduled; /* only RT tasks */
+/*
+ * scheduling lock slock
+ * protects the domain and serializes scheduling decisions
+ */
+#define slock domain.ready_lock
+
+} psnedf_domain_t;
+
+DEFINE_PER_CPU(psnedf_domain_t, psnedf_domains);
+
+#define local_edf (&__get_cpu_var(psnedf_domains).domain)
+#define local_pedf (&__get_cpu_var(psnedf_domains))
+#define remote_edf(cpu) (&per_cpu(psnedf_domains, cpu).domain)
+#define remote_pedf(cpu) (&per_cpu(psnedf_domains, cpu))
+#define task_edf(task) remote_edf(get_partition(task))
+#define task_pedf(task) remote_pedf(get_partition(task))
+
+
+static void psnedf_domain_init(psnedf_domain_t* pedf,
+ check_resched_needed_t check,
+ release_jobs_t release,
+ int cpu)
+{
+ edf_domain_init(&pedf->domain, check, release);
+ pedf->cpu = cpu;
+ pedf->scheduled = NULL;
+}
+
+static void requeue(struct task_struct* t, rt_domain_t *edf)
+{
+ if (t->state != TASK_RUNNING)
+ TRACE_TASK(t, "requeue: !TASK_RUNNING\n");
+
+ set_rt_flags(t, RT_F_RUNNING);
+ if (is_released(t, litmus_clock()))
+ __add_ready(edf, t);
+ else
+ add_release(edf, t); /* it has got to wait */
+}
+
+/* we assume the lock is being held */
+static void preempt(psnedf_domain_t *pedf)
+{
+ preempt_if_preemptable(pedf->scheduled, pedf->cpu);
+}
+
+/* This check is trivial in partioned systems as we only have to consider
+ * the CPU of the partition.
+ */
+static int psnedf_check_resched(rt_domain_t *edf)
+{
+ psnedf_domain_t *pedf = container_of(edf, psnedf_domain_t, domain);
+
+ /* because this is a callback from rt_domain_t we already hold
+ * the necessary lock for the ready queue
+ */
+ if (edf_preemption_needed(edf, pedf->scheduled)) {
+ preempt(pedf);
+ return 1;
+ } else
+ return 0;
+}
+
+static void job_completion(struct task_struct* t, int forced)
+{
+ sched_trace_task_completion(t,forced);
+ TRACE_TASK(t, "job_completion().\n");
+
+ set_rt_flags(t, RT_F_SLEEP);
+ prepare_for_next_period(t);
+}
+
+static void psnedf_tick(struct task_struct *t)
+{
+ psnedf_domain_t *pedf = local_pedf;
+
+ /* Check for inconsistency. We don't need the lock for this since
+ * ->scheduled is only changed in schedule, which obviously is not
+ * executing in parallel on this CPU
+ */
+ BUG_ON(is_realtime(t) && t != pedf->scheduled);
+
+ if (is_realtime(t) && budget_enforced(t) && budget_exhausted(t)) {
+ if (!is_np(t)) {
+ litmus_reschedule_local();
+ TRACE("psnedf_scheduler_tick: "
+ "%d is preemptable "
+ " => FORCE_RESCHED\n", t->pid);
+ } else if (is_user_np(t)) {
+ TRACE("psnedf_scheduler_tick: "
+ "%d is non-preemptable, "
+ "preemption delayed.\n", t->pid);
+ request_exit_np(t);
+ }
+ }
+}
+
+static struct task_struct* psnedf_schedule(struct task_struct * prev)
+{
+ psnedf_domain_t* pedf = local_pedf;
+ rt_domain_t* edf = &pedf->domain;
+ struct task_struct* next;
+
+ int out_of_time, sleep, preempt,
+ np, exists, blocks, resched;
+
+ raw_spin_lock(&pedf->slock);
+
+ /* sanity checking
+ * differently from gedf, when a task exits (dead)
+ * pedf->schedule may be null and prev _is_ realtime
+ */
+ BUG_ON(pedf->scheduled && pedf->scheduled != prev);
+ BUG_ON(pedf->scheduled && !is_realtime(prev));
+
+ /* (0) Determine state */
+ exists = pedf->scheduled != NULL;
+ blocks = exists && !is_running(pedf->scheduled);
+ out_of_time = exists &&
+ budget_enforced(pedf->scheduled) &&
+ budget_exhausted(pedf->scheduled);
+ np = exists && is_np(pedf->scheduled);
+ sleep = exists && get_rt_flags(pedf->scheduled) == RT_F_SLEEP;
+ preempt = edf_preemption_needed(edf, prev);
+
+ /* If we need to preempt do so.
+ * The following checks set resched to 1 in case of special
+ * circumstances.
+ */
+ resched = preempt;
+
+ /* If a task blocks we have no choice but to reschedule.
+ */
+ if (blocks)
+ resched = 1;
+
+ /* Request a sys_exit_np() call if we would like to preempt but cannot.
+ * Multiple calls to request_exit_np() don't hurt.
+ */
+ if (np && (out_of_time || preempt || sleep))
+ request_exit_np(pedf->scheduled);
+
+ /* Any task that is preemptable and either exhausts its execution
+ * budget or wants to sleep completes. We may have to reschedule after
+ * this.
+ */
+ if (!np && (out_of_time || sleep) && !blocks) {
+ job_completion(pedf->scheduled, !sleep);
+ resched = 1;
+ }
+
+ /* The final scheduling decision. Do we need to switch for some reason?
+ * Switch if we are in RT mode and have no task or if we need to
+ * resched.
+ */
+ next = NULL;
+ if ((!np || blocks) && (resched || !exists)) {
+ /* When preempting a task that does not block, then
+ * re-insert it into either the ready queue or the
+ * release queue (if it completed). requeue() picks
+ * the appropriate queue.
+ */
+ if (pedf->scheduled && !blocks)
+ requeue(pedf->scheduled, edf);
+ next = __take_ready(edf);
+ } else
+ /* Only override Linux scheduler if we have a real-time task
+ * scheduled that needs to continue.
+ */
+ if (exists)
+ next = prev;
+
+ if (next) {
+ TRACE_TASK(next, "scheduled at %llu\n", litmus_clock());
+ set_rt_flags(next, RT_F_RUNNING);
+ } else {
+ TRACE("becoming idle at %llu\n", litmus_clock());
+ }
+
+ pedf->scheduled = next;
+ sched_state_task_picked();
+ raw_spin_unlock(&pedf->slock);
+
+ return next;
+}
+
+
+/* Prepare a task for running in RT mode
+ */
+static void psnedf_task_new(struct task_struct * t, int on_rq, int running)
+{
+ rt_domain_t* edf = task_edf(t);
+ psnedf_domain_t* pedf = task_pedf(t);
+ unsigned long flags;
+
+ TRACE_TASK(t, "psn edf: task new, cpu = %d\n",
+ t->rt_param.task_params.cpu);
+
+ /* setup job parameters */
+ release_at(t, litmus_clock());
+
+ /* The task should be running in the queue, otherwise signal
+ * code will try to wake it up with fatal consequences.
+ */
+ raw_spin_lock_irqsave(&pedf->slock, flags);
+ if (running) {
+ /* there shouldn't be anything else running at the time */
+ BUG_ON(pedf->scheduled);
+ pedf->scheduled = t;
+ } else {
+ requeue(t, edf);
+ /* maybe we have to reschedule */
+ preempt(pedf);
+ }
+ raw_spin_unlock_irqrestore(&pedf->slock, flags);
+}
+
+static void psnedf_task_wake_up(struct task_struct *task)
+{
+ unsigned long flags;
+ psnedf_domain_t* pedf = task_pedf(task);
+ rt_domain_t* edf = task_edf(task);
+ lt_t now;
+
+ TRACE_TASK(task, "wake_up at %llu\n", litmus_clock());
+ raw_spin_lock_irqsave(&pedf->slock, flags);
+ BUG_ON(is_queued(task));
+ /* We need to take suspensions because of semaphores into
+ * account! If a job resumes after being suspended due to acquiring
+ * a semaphore, it should never be treated as a new job release.
+ *
+ * FIXME: This should be done in some more predictable and userspace-controlled way.
+ */
+ now = litmus_clock();
+ if (is_tardy(task, now) &&
+ get_rt_flags(task) != RT_F_EXIT_SEM) {
+ /* new sporadic release */
+ release_at(task, now);
+ sched_trace_task_release(task);
+ }
+
+ /* Only add to ready queue if it is not the currently-scheduled
+ * task. This could be the case if a task was woken up concurrently
+ * on a remote CPU before the executing CPU got around to actually
+ * de-scheduling the task, i.e., wake_up() raced with schedule()
+ * and won.
+ */
+ if (pedf->scheduled != task)
+ requeue(task, edf);
+
+ raw_spin_unlock_irqrestore(&pedf->slock, flags);
+ TRACE_TASK(task, "wake up done\n");
+}
+
+static void psnedf_task_block(struct task_struct *t)
+{
+ /* only running tasks can block, thus t is in no queue */
+ TRACE_TASK(t, "block at %llu, state=%d\n", litmus_clock(), t->state);
+
+ BUG_ON(!is_realtime(t));
+ BUG_ON(is_queued(t));
+}
+
+static void psnedf_task_exit(struct task_struct * t)
+{
+ unsigned long flags;
+ psnedf_domain_t* pedf = task_pedf(t);
+ rt_domain_t* edf;
+
+ raw_spin_lock_irqsave(&pedf->slock, flags);
+ if (is_queued(t)) {
+ /* dequeue */
+ edf = task_edf(t);
+ remove(edf, t);
+ }
+ if (pedf->scheduled == t)
+ pedf->scheduled = NULL;
+
+ TRACE_TASK(t, "RIP, now reschedule\n");
+
+ preempt(pedf);
+ raw_spin_unlock_irqrestore(&pedf->slock, flags);
+}
+
+#ifdef CONFIG_FMLP
+static long psnedf_pi_block(struct pi_semaphore *sem,
+ struct task_struct *new_waiter)
+{
+ psnedf_domain_t* pedf;
+ rt_domain_t* edf;
+ struct task_struct* t;
+ int cpu = get_partition(new_waiter);
+
+ BUG_ON(!new_waiter);
+
+ if (edf_higher_prio(new_waiter, sem->hp.cpu_task[cpu])) {
+ TRACE_TASK(new_waiter, " boosts priority\n");
+ pedf = task_pedf(new_waiter);
+ edf = task_edf(new_waiter);
+
+ /* interrupts already disabled */
+ raw_spin_lock(&pedf->slock);
+
+ /* store new highest-priority task */
+ sem->hp.cpu_task[cpu] = new_waiter;
+ if (sem->holder &&
+ get_partition(sem->holder) == get_partition(new_waiter)) {
+ /* let holder inherit */
+ sem->holder->rt_param.inh_task = new_waiter;
+ t = sem->holder;
+ if (is_queued(t)) {
+ /* queued in domain*/
+ remove(edf, t);
+ /* readd to make priority change take place */
+ /* FIXME: this looks outdated */
+ if (is_released(t, litmus_clock()))
+ __add_ready(edf, t);
+ else
+ add_release(edf, t);
+ }
+ }
+
+ /* check if we need to reschedule */
+ if (edf_preemption_needed(edf, current))
+ preempt(pedf);
+
+ raw_spin_unlock(&pedf->slock);
+ }
+
+ return 0;
+}
+
+static long psnedf_inherit_priority(struct pi_semaphore *sem,
+ struct task_struct *new_owner)
+{
+ int cpu = get_partition(new_owner);
+
+ new_owner->rt_param.inh_task = sem->hp.cpu_task[cpu];
+ if (sem->hp.cpu_task[cpu] && new_owner != sem->hp.cpu_task[cpu]) {
+ TRACE_TASK(new_owner,
+ "inherited priority from %s/%d\n",
+ sem->hp.cpu_task[cpu]->comm,
+ sem->hp.cpu_task[cpu]->pid);
+ } else
+ TRACE_TASK(new_owner,
+ "cannot inherit priority: "
+ "no higher priority job waits on this CPU!\n");
+ /* make new owner non-preemptable as required by FMLP under
+ * PSN-EDF.
+ */
+ make_np(new_owner);
+ return 0;
+}
+
+
+/* This function is called on a semaphore release, and assumes that
+ * the current task is also the semaphore holder.
+ */
+static long psnedf_return_priority(struct pi_semaphore *sem)
+{
+ struct task_struct* t = current;
+ psnedf_domain_t* pedf = task_pedf(t);
+ rt_domain_t* edf = task_edf(t);
+ int ret = 0;
+ int cpu = get_partition(current);
+ int still_np;
+
+
+ /* Find new highest-priority semaphore task
+ * if holder task is the current hp.cpu_task[cpu].
+ *
+ * Calling function holds sem->wait.lock.
+ */
+ if (t == sem->hp.cpu_task[cpu])
+ edf_set_hp_cpu_task(sem, cpu);
+
+ still_np = take_np(current);
+
+ /* Since we don't nest resources, this
+ * should always be zero */
+ BUG_ON(still_np);
+
+ if (current->rt_param.inh_task) {
+ TRACE_CUR("return priority of %s/%d\n",
+ current->rt_param.inh_task->comm,
+ current->rt_param.inh_task->pid);
+ } else
+ TRACE_CUR(" no priority to return %p\n", sem);
+
+
+ /* Always check for delayed preemptions that might have become
+ * necessary due to non-preemptive execution.
+ */
+ raw_spin_lock(&pedf->slock);
+
+ /* Reset inh_task to NULL. */
+ current->rt_param.inh_task = NULL;
+
+ /* check if we need to reschedule */
+ if (edf_preemption_needed(edf, current))
+ preempt(pedf);
+
+ raw_spin_unlock(&pedf->slock);
+
+
+ return ret;
+}
+
+#endif
+
+static long psnedf_admit_task(struct task_struct* tsk)
+{
+ return task_cpu(tsk) == tsk->rt_param.task_params.cpu ? 0 : -EINVAL;
+}
+
+/* Plugin object */
+static struct sched_plugin psn_edf_plugin __cacheline_aligned_in_smp = {
+ .plugin_name = "PSN-EDF",
+#ifdef CONFIG_SRP
+ .srp_active = 1,
+#endif
+ .tick = psnedf_tick,
+ .task_new = psnedf_task_new,
+ .complete_job = complete_job,
+ .task_exit = psnedf_task_exit,
+ .schedule = psnedf_schedule,
+ .task_wake_up = psnedf_task_wake_up,
+ .task_block = psnedf_task_block,
+#ifdef CONFIG_FMLP
+ .fmlp_active = 1,
+ .pi_block = psnedf_pi_block,
+ .inherit_priority = psnedf_inherit_priority,
+ .return_priority = psnedf_return_priority,
+#endif
+ .admit_task = psnedf_admit_task
+};
+
+
+static int __init init_psn_edf(void)
+{
+ int i;
+
+ /* We do not really want to support cpu hotplug, do we? ;)
+ * However, if we are so crazy to do so,
+ * we cannot use num_online_cpu()
+ */
+ for (i = 0; i < num_online_cpus(); i++) {
+ psnedf_domain_init(remote_pedf(i),
+ psnedf_check_resched,
+ NULL, i);
+ }
+ return register_sched_plugin(&psn_edf_plugin);
+}
+
+module_init(init_psn_edf);
+
diff --git a/litmus/sched_task_trace.c b/litmus/sched_task_trace.c
new file mode 100644
index 0000000..a15b25d
--- /dev/null
+++ b/litmus/sched_task_trace.c
@@ -0,0 +1,226 @@
+/*
+ * sched_task_trace.c -- record scheduling events to a byte stream
+ */
+
+#define NO_TASK_TRACE_DECLS
+
+#include <linux/module.h>
+#include <linux/sched.h>
+#include <linux/percpu.h>
+
+#include <litmus/ftdev.h>
+#include <litmus/litmus.h>
+
+#include <litmus/sched_trace.h>
+#include <litmus/feather_trace.h>
+#include <litmus/ftdev.h>
+
+
+#define NO_EVENTS (1 << CONFIG_SCHED_TASK_TRACE_SHIFT)
+
+#define now() litmus_clock()
+
+struct local_buffer {
+ struct st_event_record record[NO_EVENTS];
+ char flag[NO_EVENTS];
+ struct ft_buffer ftbuf;
+};
+
+DEFINE_PER_CPU(struct local_buffer, st_event_buffer);
+
+static struct ftdev st_dev;
+
+static int st_dev_can_open(struct ftdev *dev, unsigned int cpu)
+{
+ return cpu_online(cpu) ? 0 : -ENODEV;
+}
+
+static int __init init_sched_task_trace(void)
+{
+ struct local_buffer* buf;
+ int i, ok = 0, err;
+ printk("Allocated %u sched_trace_xxx() events per CPU "
+ "(buffer size: %d bytes)\n",
+ NO_EVENTS, (int) sizeof(struct local_buffer));
+
+ err = ftdev_init(&st_dev, THIS_MODULE,
+ num_online_cpus(), "sched_trace");
+ if (err)
+ goto err_out;
+
+ for (i = 0; i < st_dev.minor_cnt; i++) {
+ buf = &per_cpu(st_event_buffer, i);
+ ok += init_ft_buffer(&buf->ftbuf, NO_EVENTS,
+ sizeof(struct st_event_record),
+ buf->flag,
+ buf->record);
+ st_dev.minor[i].buf = &buf->ftbuf;
+ }
+ if (ok == st_dev.minor_cnt) {
+ st_dev.can_open = st_dev_can_open;
+ err = register_ftdev(&st_dev);
+ if (err)
+ goto err_dealloc;
+ } else {
+ err = -EINVAL;
+ goto err_dealloc;
+ }
+
+ return 0;
+
+err_dealloc:
+ ftdev_exit(&st_dev);
+err_out:
+ printk(KERN_WARNING "Could not register sched_trace module\n");
+ return err;
+}
+
+static void __exit exit_sched_task_trace(void)
+{
+ ftdev_exit(&st_dev);
+}
+
+module_init(init_sched_task_trace);
+module_exit(exit_sched_task_trace);
+
+
+static inline struct st_event_record* get_record(u8 type, struct task_struct* t)
+{
+ struct st_event_record* rec = NULL;
+ struct local_buffer* buf;
+
+ buf = &get_cpu_var(st_event_buffer);
+ if (ft_buffer_start_write(&buf->ftbuf, (void**) &rec)) {
+ rec->hdr.type = type;
+ rec->hdr.cpu = smp_processor_id();
+ rec->hdr.pid = t ? t->pid : 0;
+ rec->hdr.job = t ? t->rt_param.job_params.job_no : 0;
+ } else {
+ put_cpu_var(st_event_buffer);
+ }
+ /* rec will be NULL if it failed */
+ return rec;
+}
+
+static inline void put_record(struct st_event_record* rec)
+{
+ struct local_buffer* buf;
+ buf = &__get_cpu_var(st_event_buffer);
+ ft_buffer_finish_write(&buf->ftbuf, rec);
+ put_cpu_var(st_event_buffer);
+}
+
+feather_callback void do_sched_trace_task_name(unsigned long id, unsigned long _task)
+{
+ struct task_struct *t = (struct task_struct*) _task;
+ struct st_event_record* rec = get_record(ST_NAME, t);
+ int i;
+ if (rec) {
+ for (i = 0; i < min(TASK_COMM_LEN, ST_NAME_LEN); i++)
+ rec->data.name.cmd[i] = t->comm[i];
+ put_record(rec);
+ }
+}
+
+feather_callback void do_sched_trace_task_param(unsigned long id, unsigned long _task)
+{
+ struct task_struct *t = (struct task_struct*) _task;
+ struct st_event_record* rec = get_record(ST_PARAM, t);
+ if (rec) {
+ rec->data.param.wcet = get_exec_cost(t);
+ rec->data.param.period = get_rt_period(t);
+ rec->data.param.phase = get_rt_phase(t);
+ rec->data.param.partition = get_partition(t);
+ put_record(rec);
+ }
+}
+
+feather_callback void do_sched_trace_task_release(unsigned long id, unsigned long _task)
+{
+ struct task_struct *t = (struct task_struct*) _task;
+ struct st_event_record* rec = get_record(ST_RELEASE, t);
+ if (rec) {
+ rec->data.release.release = get_release(t);
+ rec->data.release.deadline = get_deadline(t);
+ put_record(rec);
+ }
+}
+
+/* skipped: st_assigned_data, we don't use it atm */
+
+feather_callback void do_sched_trace_task_switch_to(unsigned long id,
+ unsigned long _task)
+{
+ struct task_struct *t = (struct task_struct*) _task;
+ struct st_event_record* rec;
+ if (is_realtime(t)) {
+ rec = get_record(ST_SWITCH_TO, t);
+ if (rec) {
+ rec->data.switch_to.when = now();
+ rec->data.switch_to.exec_time = get_exec_time(t);
+ put_record(rec);
+ }
+ }
+}
+
+feather_callback void do_sched_trace_task_switch_away(unsigned long id,
+ unsigned long _task)
+{
+ struct task_struct *t = (struct task_struct*) _task;
+ struct st_event_record* rec;
+ if (is_realtime(t)) {
+ rec = get_record(ST_SWITCH_AWAY, t);
+ if (rec) {
+ rec->data.switch_away.when = now();
+ rec->data.switch_away.exec_time = get_exec_time(t);
+ put_record(rec);
+ }
+ }
+}
+
+feather_callback void do_sched_trace_task_completion(unsigned long id,
+ unsigned long _task,
+ unsigned long forced)
+{
+ struct task_struct *t = (struct task_struct*) _task;
+ struct st_event_record* rec = get_record(ST_COMPLETION, t);
+ if (rec) {
+ rec->data.completion.when = now();
+ rec->data.completion.forced = forced;
+ put_record(rec);
+ }
+}
+
+feather_callback void do_sched_trace_task_block(unsigned long id,
+ unsigned long _task)
+{
+ struct task_struct *t = (struct task_struct*) _task;
+ struct st_event_record* rec = get_record(ST_BLOCK, t);
+ if (rec) {
+ rec->data.block.when = now();
+ put_record(rec);
+ }
+}
+
+feather_callback void do_sched_trace_task_resume(unsigned long id,
+ unsigned long _task)
+{
+ struct task_struct *t = (struct task_struct*) _task;
+ struct st_event_record* rec = get_record(ST_RESUME, t);
+ if (rec) {
+ rec->data.resume.when = now();
+ put_record(rec);
+ }
+}
+
+feather_callback void do_sched_trace_sys_release(unsigned long id,
+ unsigned long _start)
+{
+ lt_t *start = (lt_t*) _start;
+ struct st_event_record* rec = get_record(ST_SYS_RELEASE, NULL);
+ if (rec) {
+ rec->data.sys_release.when = now();
+ rec->data.sys_release.release = *start;
+ put_record(rec);
+ }
+}
diff --git a/litmus/sched_trace.c b/litmus/sched_trace.c
new file mode 100644
index 0000000..f4171fd
--- /dev/null
+++ b/litmus/sched_trace.c
@@ -0,0 +1,252 @@
+/*
+ * sched_trace.c -- record scheduling events to a byte stream.
+ */
+#include <linux/spinlock.h>
+#include <linux/mutex.h>
+
+#include <linux/fs.h>
+#include <linux/slab.h>
+#include <linux/miscdevice.h>
+#include <asm/uaccess.h>
+#include <linux/module.h>
+#include <linux/sysrq.h>
+
+#include <linux/kfifo.h>
+
+#include <litmus/sched_trace.h>
+#include <litmus/litmus.h>
+
+#define SCHED_TRACE_NAME "litmus/log"
+
+/* Compute size of TRACE() buffer */
+#define LITMUS_TRACE_BUF_SIZE (1 << CONFIG_SCHED_DEBUG_TRACE_SHIFT)
+
+/* Max length of one read from the buffer */
+#define MAX_READ_LEN (64 * 1024)
+
+/* Max length for one write --- by TRACE() --- to the buffer. This is used to
+ * allocate a per-cpu buffer for printf() formatting. */
+#define MSG_SIZE 255
+
+
+static DEFINE_MUTEX(reader_mutex);
+static atomic_t reader_cnt = ATOMIC_INIT(0);
+static DEFINE_KFIFO(debug_buffer, char, LITMUS_TRACE_BUF_SIZE);
+
+
+static DEFINE_RAW_SPINLOCK(log_buffer_lock);
+static DEFINE_PER_CPU(char[MSG_SIZE], fmt_buffer);
+
+/*
+ * sched_trace_log_message - Write to the trace buffer (log_buffer)
+ *
+ * This is the only function accessing the log_buffer from inside the
+ * kernel for writing.
+ * Concurrent access to sched_trace_log_message must be serialized using
+ * log_buffer_lock
+ * The maximum length of a formatted message is 255
+ */
+void sched_trace_log_message(const char* fmt, ...)
+{
+ unsigned long flags;
+ va_list args;
+ size_t len;
+ char* buf;
+
+ if (!atomic_read(&reader_cnt))
+ /* early exit if nobody is listening */
+ return;
+
+ va_start(args, fmt);
+ local_irq_save(flags);
+
+ /* format message */
+ buf = __get_cpu_var(fmt_buffer);
+ len = vscnprintf(buf, MSG_SIZE, fmt, args);
+
+ raw_spin_lock(&log_buffer_lock);
+ /* Don't copy the trailing null byte, we don't want null bytes in a
+ * text file.
+ */
+ kfifo_in(&debug_buffer, buf, len);
+ raw_spin_unlock(&log_buffer_lock);
+
+ local_irq_restore(flags);
+ va_end(args);
+}
+
+
+/*
+ * log_read - Read the trace buffer
+ *
+ * This function is called as a file operation from userspace.
+ * Readers can sleep. Access is serialized through reader_mutex
+ */
+static ssize_t log_read(struct file *filp,
+ char __user *to, size_t len,
+ loff_t *f_pos)
+{
+ /* we ignore f_pos, this is strictly sequential */
+
+ ssize_t error = -EINVAL;
+ char* mem;
+
+ if (mutex_lock_interruptible(&reader_mutex)) {
+ error = -ERESTARTSYS;
+ goto out;
+ }
+
+ if (len > MAX_READ_LEN)
+ len = MAX_READ_LEN;
+
+ mem = kmalloc(len, GFP_KERNEL);
+ if (!mem) {
+ error = -ENOMEM;
+ goto out_unlock;
+ }
+
+ error = kfifo_out(&debug_buffer, mem, len);
+ while (!error) {
+ set_current_state(TASK_INTERRUPTIBLE);
+ schedule_timeout(110);
+ if (signal_pending(current))
+ error = -ERESTARTSYS;
+ else
+ error = kfifo_out(&debug_buffer, mem, len);
+ }
+
+ if (error > 0 && copy_to_user(to, mem, error))
+ error = -EFAULT;
+
+ kfree(mem);
+ out_unlock:
+ mutex_unlock(&reader_mutex);
+ out:
+ return error;
+}
+
+/*
+ * Enable redirection of printk() messages to the trace buffer.
+ * Defined in kernel/printk.c
+ */
+extern int trace_override;
+extern int trace_recurse;
+
+/*
+ * log_open - open the global log message ring buffer.
+ */
+static int log_open(struct inode *in, struct file *filp)
+{
+ int error = -EINVAL;
+
+ if (mutex_lock_interruptible(&reader_mutex)) {
+ error = -ERESTARTSYS;
+ goto out;
+ }
+
+ atomic_inc(&reader_cnt);
+ error = 0;
+
+ printk(KERN_DEBUG
+ "sched_trace kfifo with buffer starting at: 0x%p\n",
+ debug_buffer.buf);
+
+ /* override printk() */
+ trace_override++;
+
+ mutex_unlock(&reader_mutex);
+ out:
+ return error;
+}
+
+static int log_release(struct inode *in, struct file *filp)
+{
+ int error = -EINVAL;
+
+ if (mutex_lock_interruptible(&reader_mutex)) {
+ error = -ERESTARTSYS;
+ goto out;
+ }
+
+ atomic_dec(&reader_cnt);
+
+ /* release printk() overriding */
+ trace_override--;
+
+ printk(KERN_DEBUG "sched_trace kfifo released\n");
+
+ mutex_unlock(&reader_mutex);
+ out:
+ return error;
+}
+
+/*
+ * log_fops - The file operations for accessing the global LITMUS log message
+ * buffer.
+ *
+ * Except for opening the device file it uses the same operations as trace_fops.
+ */
+static struct file_operations log_fops = {
+ .owner = THIS_MODULE,
+ .open = log_open,
+ .release = log_release,
+ .read = log_read,
+};
+
+static struct miscdevice litmus_log_dev = {
+ .name = SCHED_TRACE_NAME,
+ .minor = MISC_DYNAMIC_MINOR,
+ .fops = &log_fops,
+};
+
+#ifdef CONFIG_MAGIC_SYSRQ
+void dump_trace_buffer(int max)
+{
+ char line[80];
+ int len;
+ int count = 0;
+
+ /* potential, but very unlikely, race... */
+ trace_recurse = 1;
+ while ((max == 0 || count++ < max) &&
+ (len = kfifo_out(&debug_buffer, line, sizeof(line - 1))) > 0) {
+ line[len] = '\0';
+ printk("%s", line);
+ }
+ trace_recurse = 0;
+}
+
+static void sysrq_dump_trace_buffer(int key)
+{
+ dump_trace_buffer(100);
+}
+
+static struct sysrq_key_op sysrq_dump_trace_buffer_op = {
+ .handler = sysrq_dump_trace_buffer,
+ .help_msg = "dump-trace-buffer(Y)",
+ .action_msg = "writing content of TRACE() buffer",
+};
+#endif
+
+static int __init init_sched_trace(void)
+{
+ printk("Initializing TRACE() device\n");
+
+#ifdef CONFIG_MAGIC_SYSRQ
+ /* offer some debugging help */
+ if (!register_sysrq_key('y', &sysrq_dump_trace_buffer_op))
+ printk("Registered dump-trace-buffer(Y) magic sysrq.\n");
+ else
+ printk("Could not register dump-trace-buffer(Y) magic sysrq.\n");
+#endif
+
+ return misc_register(&litmus_log_dev);
+}
+
+static void __exit exit_sched_trace(void)
+{
+ misc_deregister(&litmus_log_dev);
+}
+
+module_init(init_sched_trace);
+module_exit(exit_sched_trace);
diff --git a/litmus/srp.c b/litmus/srp.c
new file mode 100644
index 0000000..cb57759
--- /dev/null
+++ b/litmus/srp.c
@@ -0,0 +1,318 @@
+/* ************************************************************************** */
+/* STACK RESOURCE POLICY */
+/* ************************************************************************** */
+
+#include <asm/atomic.h>
+#include <linux/sched.h>
+#include <linux/wait.h>
+
+#include <litmus/litmus.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/fdso.h>
+#include <litmus/trace.h>
+
+
+#ifdef CONFIG_SRP
+
+struct srp_priority {
+ struct list_head list;
+ unsigned int period;
+ pid_t pid;
+};
+
+#define list2prio(l) list_entry(l, struct srp_priority, list)
+
+/* SRP task priority comparison function. Smaller periods have highest
+ * priority, tie-break is PID. Special case: period == 0 <=> no priority
+ */
+static int srp_higher_prio(struct srp_priority* first,
+ struct srp_priority* second)
+{
+ if (!first->period)
+ return 0;
+ else
+ return !second->period ||
+ first->period < second->period || (
+ first->period == second->period &&
+ first->pid < second->pid);
+}
+
+struct srp {
+ struct list_head ceiling;
+ wait_queue_head_t ceiling_blocked;
+};
+
+
+atomic_t srp_objects_in_use = ATOMIC_INIT(0);
+
+DEFINE_PER_CPU(struct srp, srp);
+
+
+/* Initialize SRP semaphores at boot time. */
+static int __init srp_init(void)
+{
+ int i;
+
+ printk("Initializing SRP per-CPU ceilings...");
+ for (i = 0; i < NR_CPUS; i++) {
+ init_waitqueue_head(&per_cpu(srp, i).ceiling_blocked);
+ INIT_LIST_HEAD(&per_cpu(srp, i).ceiling);
+ }
+ printk(" done!\n");
+
+ return 0;
+}
+module_init(srp_init);
+
+
+#define system_ceiling(srp) list2prio(srp->ceiling.next)
+
+
+#define UNDEF_SEM -2
+
+
+/* struct for uniprocessor SRP "semaphore" */
+struct srp_semaphore {
+ struct srp_priority ceiling;
+ struct task_struct* owner;
+ int cpu; /* cpu associated with this "semaphore" and resource */
+};
+
+#define ceiling2sem(c) container_of(c, struct srp_semaphore, ceiling)
+
+static int srp_exceeds_ceiling(struct task_struct* first,
+ struct srp* srp)
+{
+ return list_empty(&srp->ceiling) ||
+ get_rt_period(first) < system_ceiling(srp)->period ||
+ (get_rt_period(first) == system_ceiling(srp)->period &&
+ first->pid < system_ceiling(srp)->pid) ||
+ ceiling2sem(system_ceiling(srp))->owner == first;
+}
+
+static void srp_add_prio(struct srp* srp, struct srp_priority* prio)
+{
+ struct list_head *pos;
+ if (in_list(&prio->list)) {
+ printk(KERN_CRIT "WARNING: SRP violation detected, prio is already in "
+ "ceiling list! cpu=%d, srp=%p\n", smp_processor_id(), ceiling2sem(prio));
+ return;
+ }
+ list_for_each(pos, &srp->ceiling)
+ if (unlikely(srp_higher_prio(prio, list2prio(pos)))) {
+ __list_add(&prio->list, pos->prev, pos);
+ return;
+ }
+
+ list_add_tail(&prio->list, &srp->ceiling);
+}
+
+
+static void* create_srp_semaphore(void)
+{
+ struct srp_semaphore* sem;
+
+ sem = kmalloc(sizeof(*sem), GFP_KERNEL);
+ if (!sem)
+ return NULL;
+
+ INIT_LIST_HEAD(&sem->ceiling.list);
+ sem->ceiling.period = 0;
+ sem->cpu = UNDEF_SEM;
+ sem->owner = NULL;
+ atomic_inc(&srp_objects_in_use);
+ return sem;
+}
+
+static noinline int open_srp_semaphore(struct od_table_entry* entry, void* __user arg)
+{
+ struct srp_semaphore* sem = (struct srp_semaphore*) entry->obj->obj;
+ int ret = 0;
+ struct task_struct* t = current;
+ struct srp_priority t_prio;
+
+ TRACE("opening SRP semaphore %p, cpu=%d\n", sem, sem->cpu);
+ if (!srp_active())
+ return -EBUSY;
+
+ if (sem->cpu == UNDEF_SEM)
+ sem->cpu = get_partition(t);
+ else if (sem->cpu != get_partition(t))
+ ret = -EPERM;
+
+ if (ret == 0) {
+ t_prio.period = get_rt_period(t);
+ t_prio.pid = t->pid;
+ if (srp_higher_prio(&t_prio, &sem->ceiling)) {
+ sem->ceiling.period = t_prio.period;
+ sem->ceiling.pid = t_prio.pid;
+ }
+ }
+
+ return ret;
+}
+
+static void destroy_srp_semaphore(void* sem)
+{
+ /* XXX invariants */
+ atomic_dec(&srp_objects_in_use);
+ kfree(sem);
+}
+
+struct fdso_ops srp_sem_ops = {
+ .create = create_srp_semaphore,
+ .open = open_srp_semaphore,
+ .destroy = destroy_srp_semaphore
+};
+
+
+static void do_srp_down(struct srp_semaphore* sem)
+{
+ /* Update ceiling. */
+ srp_add_prio(&__get_cpu_var(srp), &sem->ceiling);
+ WARN_ON(sem->owner != NULL);
+ sem->owner = current;
+ TRACE_CUR("acquired srp 0x%p\n", sem);
+}
+
+static void do_srp_up(struct srp_semaphore* sem)
+{
+ /* Determine new system priority ceiling for this CPU. */
+ WARN_ON(!in_list(&sem->ceiling.list));
+ if (in_list(&sem->ceiling.list))
+ list_del(&sem->ceiling.list);
+
+ sem->owner = NULL;
+
+ /* Wake tasks on this CPU, if they exceed current ceiling. */
+ TRACE_CUR("released srp 0x%p\n", sem);
+ wake_up_all(&__get_cpu_var(srp).ceiling_blocked);
+}
+
+/* Adjust the system-wide priority ceiling if resource is claimed. */
+asmlinkage long sys_srp_down(int sem_od)
+{
+ int cpu;
+ int ret = -EINVAL;
+ struct srp_semaphore* sem;
+
+ /* disabling preemptions is sufficient protection since
+ * SRP is strictly per CPU and we don't interfere with any
+ * interrupt handlers
+ */
+ preempt_disable();
+ TS_SRP_DOWN_START;
+
+ cpu = smp_processor_id();
+ sem = lookup_srp_sem(sem_od);
+ if (sem && sem->cpu == cpu) {
+ do_srp_down(sem);
+ ret = 0;
+ }
+
+ TS_SRP_DOWN_END;
+ preempt_enable();
+ return ret;
+}
+
+/* Adjust the system-wide priority ceiling if resource is freed. */
+asmlinkage long sys_srp_up(int sem_od)
+{
+ int cpu;
+ int ret = -EINVAL;
+ struct srp_semaphore* sem;
+
+ preempt_disable();
+ TS_SRP_UP_START;
+
+ cpu = smp_processor_id();
+ sem = lookup_srp_sem(sem_od);
+
+ if (sem && sem->cpu == cpu) {
+ do_srp_up(sem);
+ ret = 0;
+ }
+
+ TS_SRP_UP_END;
+ preempt_enable();
+ return ret;
+}
+
+static int srp_wake_up(wait_queue_t *wait, unsigned mode, int sync,
+ void *key)
+{
+ int cpu = smp_processor_id();
+ struct task_struct *tsk = wait->private;
+ if (cpu != get_partition(tsk))
+ TRACE_TASK(tsk, "srp_wake_up on wrong cpu, partition is %d\b",
+ get_partition(tsk));
+ else if (srp_exceeds_ceiling(tsk, &__get_cpu_var(srp)))
+ return default_wake_function(wait, mode, sync, key);
+ return 0;
+}
+
+
+
+static void do_ceiling_block(struct task_struct *tsk)
+{
+ wait_queue_t wait = {
+ .private = tsk,
+ .func = srp_wake_up,
+ .task_list = {NULL, NULL}
+ };
+
+ tsk->state = TASK_UNINTERRUPTIBLE;
+ add_wait_queue(&__get_cpu_var(srp).ceiling_blocked, &wait);
+ tsk->rt_param.srp_non_recurse = 1;
+ preempt_enable_no_resched();
+ schedule();
+ preempt_disable();
+ tsk->rt_param.srp_non_recurse = 0;
+ remove_wait_queue(&__get_cpu_var(srp).ceiling_blocked, &wait);
+}
+
+/* Wait for current task priority to exceed system-wide priority ceiling.
+ */
+void srp_ceiling_block(void)
+{
+ struct task_struct *tsk = current;
+
+ /* Only applies to real-time tasks, but optimize for RT tasks. */
+ if (unlikely(!is_realtime(tsk)))
+ return;
+
+ /* Avoid recursive ceiling blocking. */
+ if (unlikely(tsk->rt_param.srp_non_recurse))
+ return;
+
+ /* Bail out early if there aren't any SRP resources around. */
+ if (likely(!atomic_read(&srp_objects_in_use)))
+ return;
+
+ preempt_disable();
+ if (!srp_exceeds_ceiling(tsk, &__get_cpu_var(srp))) {
+ TRACE_CUR("is priority ceiling blocked.\n");
+ while (!srp_exceeds_ceiling(tsk, &__get_cpu_var(srp)))
+ do_ceiling_block(tsk);
+ TRACE_CUR("finally exceeds system ceiling.\n");
+ } else
+ TRACE_CUR("is not priority ceiling blocked\n");
+ preempt_enable();
+}
+
+
+#else
+
+asmlinkage long sys_srp_down(int sem_od)
+{
+ return -ENOSYS;
+}
+
+asmlinkage long sys_srp_up(int sem_od)
+{
+ return -ENOSYS;
+}
+
+struct fdso_ops srp_sem_ops = {};
+
+#endif
diff --git a/litmus/sync.c b/litmus/sync.c
new file mode 100644
index 0000000..bf75fde
--- /dev/null
+++ b/litmus/sync.c
@@ -0,0 +1,104 @@
+/* litmus/sync.c - Support for synchronous and asynchronous task system releases.
+ *
+ *
+ */
+
+#include <asm/atomic.h>
+#include <asm/uaccess.h>
+#include <linux/spinlock.h>
+#include <linux/list.h>
+#include <linux/sched.h>
+#include <linux/completion.h>
+
+#include <litmus/litmus.h>
+#include <litmus/sched_plugin.h>
+#include <litmus/jobs.h>
+
+#include <litmus/sched_trace.h>
+
+static DECLARE_COMPLETION(ts_release);
+
+static long do_wait_for_ts_release(void)
+{
+ long ret = 0;
+
+ /* If the interruption races with a release, the completion object
+ * may have a non-zero counter. To avoid this problem, this should
+ * be replaced by wait_for_completion().
+ *
+ * For debugging purposes, this is interruptible for now.
+ */
+ ret = wait_for_completion_interruptible(&ts_release);
+
+ return ret;
+}
+
+int count_tasks_waiting_for_release(void)
+{
+ unsigned long flags;
+ int task_count = 0;
+ struct list_head *pos;
+
+ spin_lock_irqsave(&ts_release.wait.lock, flags);
+ list_for_each(pos, &ts_release.wait.task_list) {
+ task_count++;
+ }
+ spin_unlock_irqrestore(&ts_release.wait.lock, flags);
+
+ return task_count;
+}
+
+static long do_release_ts(lt_t start)
+{
+ int task_count = 0;
+ unsigned long flags;
+ struct list_head *pos;
+ struct task_struct *t;
+
+
+ spin_lock_irqsave(&ts_release.wait.lock, flags);
+ TRACE("<<<<<< synchronous task system release >>>>>>\n");
+
+ sched_trace_sys_release(&start);
+ list_for_each(pos, &ts_release.wait.task_list) {
+ t = (struct task_struct*) list_entry(pos,
+ struct __wait_queue,
+ task_list)->private;
+ task_count++;
+ litmus->release_at(t, start + t->rt_param.task_params.phase);
+ sched_trace_task_release(t);
+ }
+
+ spin_unlock_irqrestore(&ts_release.wait.lock, flags);
+
+ complete_n(&ts_release, task_count);
+
+ return task_count;
+}
+
+
+asmlinkage long sys_wait_for_ts_release(void)
+{
+ long ret = -EPERM;
+ struct task_struct *t = current;
+
+ if (is_realtime(t))
+ ret = do_wait_for_ts_release();
+
+ return ret;
+}
+
+
+asmlinkage long sys_release_ts(lt_t __user *__delay)
+{
+ long ret;
+ lt_t delay;
+
+ /* FIXME: check capabilities... */
+
+ ret = copy_from_user(&delay, __delay, sizeof(delay));
+ if (ret == 0)
+ ret = do_release_ts(litmus_clock() + delay);
+
+ return ret;
+}
diff --git a/litmus/trace.c b/litmus/trace.c
new file mode 100644
index 0000000..e7ea1c2
--- /dev/null
+++ b/litmus/trace.c
@@ -0,0 +1,122 @@
+#include <linux/sched.h>
+#include <linux/module.h>
+
+#include <litmus/ftdev.h>
+#include <litmus/litmus.h>
+#include <litmus/trace.h>
+
+/******************************************************************************/
+/* Allocation */
+/******************************************************************************/
+
+static struct ftdev overhead_dev;
+
+#define trace_ts_buf overhead_dev.minor[0].buf
+
+static unsigned int ts_seq_no = 0;
+
+static inline void __save_timestamp_cpu(unsigned long event,
+ uint8_t type, uint8_t cpu)
+{
+ unsigned int seq_no;
+ struct timestamp *ts;
+ seq_no = fetch_and_inc((int *) &ts_seq_no);
+ if (ft_buffer_start_write(trace_ts_buf, (void**) &ts)) {
+ ts->event = event;
+ ts->timestamp = ft_timestamp();
+ ts->seq_no = seq_no;
+ ts->cpu = cpu;
+ ts->task_type = type;
+ ft_buffer_finish_write(trace_ts_buf, ts);
+ }
+}
+
+static inline void __save_timestamp(unsigned long event,
+ uint8_t type)
+{
+ __save_timestamp_cpu(event, type, raw_smp_processor_id());
+}
+
+feather_callback void save_timestamp(unsigned long event)
+{
+ __save_timestamp(event, TSK_UNKNOWN);
+}
+
+feather_callback void save_timestamp_def(unsigned long event,
+ unsigned long type)
+{
+ __save_timestamp(event, (uint8_t) type);
+}
+
+feather_callback void save_timestamp_task(unsigned long event,
+ unsigned long t_ptr)
+{
+ int rt = is_realtime((struct task_struct *) t_ptr);
+ __save_timestamp(event, rt ? TSK_RT : TSK_BE);
+}
+
+feather_callback void save_timestamp_cpu(unsigned long event,
+ unsigned long cpu)
+{
+ __save_timestamp_cpu(event, TSK_UNKNOWN, cpu);
+}
+
+/******************************************************************************/
+/* DEVICE FILE DRIVER */
+/******************************************************************************/
+
+/*
+ * should be 8M; it is the max we can ask to buddy system allocator (MAX_ORDER)
+ * and we might not get as much
+ */
+#define NO_TIMESTAMPS (2 << 11)
+
+static int alloc_timestamp_buffer(struct ftdev* ftdev, unsigned int idx)
+{
+ unsigned int count = NO_TIMESTAMPS;
+ while (count && !trace_ts_buf) {
+ printk("time stamp buffer: trying to allocate %u time stamps.\n", count);
+ ftdev->minor[idx].buf = alloc_ft_buffer(count, sizeof(struct timestamp));
+ count /= 2;
+ }
+ return ftdev->minor[idx].buf ? 0 : -ENOMEM;
+}
+
+static void free_timestamp_buffer(struct ftdev* ftdev, unsigned int idx)
+{
+ free_ft_buffer(ftdev->minor[idx].buf);
+ ftdev->minor[idx].buf = NULL;
+}
+
+static int __init init_ft_overhead_trace(void)
+{
+ int err;
+
+ printk("Initializing Feather-Trace overhead tracing device.\n");
+ err = ftdev_init(&overhead_dev, THIS_MODULE, 1, "ft_trace");
+ if (err)
+ goto err_out;
+
+ overhead_dev.alloc = alloc_timestamp_buffer;
+ overhead_dev.free = free_timestamp_buffer;
+
+ err = register_ftdev(&overhead_dev);
+ if (err)
+ goto err_dealloc;
+
+ return 0;
+
+err_dealloc:
+ ftdev_exit(&overhead_dev);
+err_out:
+ printk(KERN_WARNING "Could not register ft_trace module.\n");
+ return err;
+}
+
+static void __exit exit_ft_overhead_trace(void)
+{
+ ftdev_exit(&overhead_dev);
+}
+
+module_init(init_ft_overhead_trace);
+module_exit(exit_ft_overhead_trace);
|