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
|
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# cmd/powertop/Makefile
#
PROG = powertop
include ../Makefile.cmd
SUBDIRS += $(MACH64)
all : TARGET = all
install : TARGET = install
clean : TARGET = clean
clobber : TARGET = clobber
.KEEP_STATE:
all clean clobber install: $(SUBDIRS)
install: $(SUBDIRS)
$(SUBDIRS): FRC
@cd $@; pwd; $(MAKE) $(TARGET)
FRC:
include ../Makefile.targ
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
# Copyright (c) 2018, Joyent, Inc.
PROG = powertop
COMMON_OBJS = $(PROG).o \
display.o \
battery.o \
cpufreq.o \
cpuidle.o \
events.o \
util.o \
suggestions.o \
turbo.o
SRCS = $(COMMON_OBJS:%.o=../common/%.c)
include ../../Makefile.cmd
.KEEP_STATE:
CFLAGS += $(CCVERBOSE)
CFLAGS64 += $(CCVERBOSE)
CERRWARN += -Wno-parentheses
CERRWARN += $(CNOWARN_UNINIT)
SMOFF += free
LDLIBS += -lcurses -ldtrace -lkstat
FILEMODE = 0555
CLEANFILES += $(COMMON_OBJS)
all: $(PROG)
clean:
$(RM) $(CLEANFILES)
lint: lint_SRCS
include ../../Makefile.targ
#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#
# Copyright 2009 Sun Microsystems, Inc. All rights reserved.
# Use is subject to license terms.
#
include ../Makefile.com
include ../../Makefile.cmd.64
MACH_OBJS = pt_amd64.o
SRCS += $(MACH_OBJS:%.o=%.c)
.KEEP_STATE:
CLEANFILES += $(MACH_OBJS)
all: $(PROG)
$(PROG): $(MACH_OBJS) $(COMMON_OBJS)
$(LINK.c) -o $@ $(MACH_OBJS) $(COMMON_OBJS) $(LDLIBS)
$(POST_PROCESS)
%.o: ../common/%.c
$(COMPILE.c) -o $@ $<
install: all $(ROOTPROG)
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
/*
* DTrace scripts for observing interrupts, callouts and cyclic events
* that cause CPU activity. Such activity prevents the processor from
* entering lower power states and reducing power consumption.
*
* g_dtp_events is the default script
*/
const char *g_dtp_events =
"interrupt-complete"
"/arg0 != NULL && arg3 !=0/"
"{"
" this->devi = (struct dev_info *)arg0;"
" @interrupts[stringof(`devnamesp[this->devi->devi_major].dn_name),"
" this->devi->devi_instance] = count();"
"}"
""
"sdt:::callout-start"
"/(caddr_t)((callout_t *)arg0)->c_func == (caddr_t)&`setrun/"
"{"
" this->thr = (kthread_t *)(((callout_t *)arg0)->c_arg);"
" @events_u[stringof(this->thr->t_procp->p_user.u_comm)] = count();"
"}"
""
"sdt:::callout-start"
"/(caddr_t)((callout_t *)arg0)->c_func != (caddr_t)&`setrun/"
"{"
" @events_k[(caddr_t)((callout_t *)arg0)->c_func] = count();"
"}"
""
"sdt:::cyclic-start"
"/(caddr_t)((cyclic_t *)arg0)->cy_handler == (caddr_t)&`clock/"
"{"
" @events_k[(caddr_t)((cyclic_t *)arg0)->cy_handler] = count();"
"}"
""
"fbt::xc_common:entry"
"{"
" self->xc_func = arg0;"
"}"
""
"sysinfo:::xcalls"
"/pid != $pid/"
"{"
" @events_x[execname, self->xc_func] = sum(arg0);"
"}"
""
"fbt::xc_common:return"
"/self->xc_func/"
"{"
" self->xc_func = 0;"
"}";
/*
* g_dtp_events_v is enabled through the -v option, it includes cyclic events
* in the report, allowing a complete view of system activity
*/
const char *g_dtp_events_v =
"interrupt-complete"
"/arg0 != NULL && arg3 !=0/"
"{"
" this->devi = (struct dev_info *)arg0;"
" @interrupts[stringof(`devnamesp[this->devi->devi_major].dn_name),"
" this->devi->devi_instance] = count();"
"}"
""
"sdt:::callout-start"
"/(caddr_t)((callout_t *)arg0)->c_func == (caddr_t)&`setrun/"
"{"
" this->thr = (kthread_t *)(((callout_t *)arg0)->c_arg);"
" @events_u[stringof(this->thr->t_procp->p_user.u_comm)] = count();"
"}"
""
"sdt:::callout-start"
"/(caddr_t)((callout_t *)arg0)->c_func != (caddr_t)&`setrun/"
"{"
" @events_k[(caddr_t)((callout_t *)arg0)->c_func] = count();"
"}"
""
"sdt:::cyclic-start"
"/(caddr_t)((cyclic_t *)arg0)->cy_handler != (caddr_t)&`dtrace_state_deadman &&"
" (caddr_t)((cyclic_t *)arg0)->cy_handler != (caddr_t)&`dtrace_state_clean/"
"{"
" @events_k[(caddr_t)((cyclic_t *)arg0)->cy_handler] = count();"
"}"
""
"fbt::xc_common:entry"
"{"
" self->xc_func = arg0;"
"}"
""
"sysinfo:::xcalls"
"/pid != $pid/"
"{"
" @events_x[execname, self->xc_func] = sum(arg0);"
"}"
""
"fbt::xc_common:return"
"/self->xc_func/"
"{"
" self->xc_func = 0;"
"}";
/*
* This script is selected through the -c option, it takes the CPU id as
* argument and observes activity generated by that CPU
*/
const char *g_dtp_events_c =
"interrupt-complete"
"/cpu == $0 &&"
" arg0 != NULL && arg3 != 0/"
"{"
" this->devi = (struct dev_info *)arg0;"
" @interrupts[stringof(`devnamesp[this->devi->devi_major].dn_name),"
" this->devi->devi_instance] = count();"
"}"
""
"sdt:::callout-start"
"/cpu == $0 &&"
" (caddr_t)((callout_t *)arg0)->c_func == (caddr_t)&`setrun/"
"{"
" this->thr = (kthread_t *)(((callout_t *)arg0)->c_arg);"
" @events_u[stringof(this->thr->t_procp->p_user.u_comm)] = count();"
"}"
""
"sdt:::callout-start"
"/cpu == $0 &&"
" (caddr_t)((callout_t *)arg0)->c_func != (caddr_t)&`setrun/"
"{"
" @events_k[(caddr_t)((callout_t *)arg0)->c_func] = count();"
"}"
""
"sdt:::cyclic-start"
"/cpu == $0 &&"
" (caddr_t)((cyclic_t *)arg0)->cy_handler == (caddr_t)&`clock/"
"{"
" @events_k[(caddr_t)((cyclic_t *)arg0)->cy_handler] = count();"
"}"
""
"fbt::xc_common:entry"
"/cpu == $0/"
"{"
" self->xc_func = arg0;"
"}"
""
"sysinfo:::xcalls"
"/pid != $pid &&"
" cpu == $0/"
"{"
" @events_x[execname, self->xc_func] = count();"
"}"
""
"fbt::xc_common:return"
"/cpu == $0 &&"
" self->xc_func/"
"{"
" self->xc_func = 0;"
"}"
""
"fbt::xc_common:entry"
"/cpu != $0/"
"{"
" self->xc_func = arg0;"
" self->xc_cpu = cpu;"
"}"
""
"fbt::send_dirint:entry"
"/pid != $pid &&"
" self->xc_func &&"
" arg0 == $0/"
"{"
" @events_xc[execname, self->xc_func, self->xc_cpu] = count();"
" self->xc_func = 0;"
" self->xc_cpu = 0;"
"}";
/*
* amd64 platform specific display messages
*/
const char *g_msg_idle_state = "C-states (idle power)";
const char *g_msg_freq_state = "P-states (frequencies)";
const char *g_msg_freq_enable = "P - Enable P-states";
GNU GENERAL PUBLIC LICENSE
Version 2, June 1991
Copyright (C) 1989, 1991 Free Software Foundation, Inc.
59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Everyone is permitted to copy and distribute verbatim copies
of this license document, but changing it is not allowed.
Preamble
The licenses for most software are designed to take away your
freedom to share and change it. By contrast, the GNU General Public
License is intended to guarantee your freedom to share and change free
software--to make sure the software is free for all its users. This
General Public License applies to most of the Free Software
Foundation's software and to any other program whose authors commit to
using it. (Some other Free Software Foundation software is covered by
the GNU Library General Public License instead.) You can apply it to
your programs, too.
When we speak of free software, we are referring to freedom, not
price. Our General Public Licenses are designed to make sure that you
have the freedom to distribute copies of free software (and charge for
this service if you wish), that you receive source code or can get it
if you want it, that you can change the software or use pieces of it
in new free programs; and that you know you can do these things.
To protect your rights, we need to make restrictions that forbid
anyone to deny you these rights or to ask you to surrender the rights.
These restrictions translate to certain responsibilities for you if you
distribute copies of the software, or if you modify it.
For example, if you distribute copies of such a program, whether
gratis or for a fee, you must give the recipients all the rights that
you have. You must make sure that they, too, receive or can get the
source code. And you must show them these terms so they know their
rights.
We protect your rights with two steps: (1) copyright the software, and
(2) offer you this license which gives you legal permission to copy,
distribute and/or modify the software.
Also, for each author's protection and ours, we want to make certain
that everyone understands that there is no warranty for this free
software. If the software is modified by someone else and passed on, we
want its recipients to know that what they have is not the original, so
that any problems introduced by others will not reflect on the original
authors' reputations.
Finally, any free program is threatened constantly by software
patents. We wish to avoid the danger that redistributors of a free
program will individually obtain patent licenses, in effect making the
program proprietary. To prevent this, we have made it clear that any
patent must be licensed for everyone's free use or not licensed at all.
The precise terms and conditions for copying, distribution and
modification follow.
GNU GENERAL PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. This License applies to any program or other work which contains
a notice placed by the copyright holder saying it may be distributed
under the terms of this General Public License. The "Program", below,
refers to any such program or work, and a "work based on the Program"
means either the Program or any derivative work under copyright law:
that is to say, a work containing the Program or a portion of it,
either verbatim or with modifications and/or translated into another
language. (Hereinafter, translation is included without limitation in
the term "modification".) Each licensee is addressed as "you".
Activities other than copying, distribution and modification are not
covered by this License; they are outside its scope. The act of
running the Program is not restricted, and the output from the Program
is covered only if its contents constitute a work based on the
Program (independent of having been made by running the Program).
Whether that is true depends on what the Program does.
1. You may copy and distribute verbatim copies of the Program's
source code as you receive it, in any medium, provided that you
conspicuously and appropriately publish on each copy an appropriate
copyright notice and disclaimer of warranty; keep intact all the
notices that refer to this License and to the absence of any warranty;
and give any other recipients of the Program a copy of this License
along with the Program.
You may charge a fee for the physical act of transferring a copy, and
you may at your option offer warranty protection in exchange for a fee.
2. You may modify your copy or copies of the Program or any portion
of it, thus forming a work based on the Program, and copy and
distribute such modifications or work under the terms of Section 1
above, provided that you also meet all of these conditions:
a) You must cause the modified files to carry prominent notices
stating that you changed the files and the date of any change.
b) You must cause any work that you distribute or publish, that in
whole or in part contains or is derived from the Program or any
part thereof, to be licensed as a whole at no charge to all third
parties under the terms of this License.
c) If the modified program normally reads commands interactively
when run, you must cause it, when started running for such
interactive use in the most ordinary way, to print or display an
announcement including an appropriate copyright notice and a
notice that there is no warranty (or else, saying that you provide
a warranty) and that users may redistribute the program under
these conditions, and telling the user how to view a copy of this
License. (Exception: if the Program itself is interactive but
does not normally print such an announcement, your work based on
the Program is not required to print an announcement.)
These requirements apply to the modified work as a whole. If
identifiable sections of that work are not derived from the Program,
and can be reasonably considered independent and separate works in
themselves, then this License, and its terms, do not apply to those
sections when you distribute them as separate works. But when you
distribute the same sections as part of a whole which is a work based
on the Program, the distribution of the whole must be on the terms of
this License, whose permissions for other licensees extend to the
entire whole, and thus to each and every part regardless of who wrote it.
Thus, it is not the intent of this section to claim rights or contest
your rights to work written entirely by you; rather, the intent is to
exercise the right to control the distribution of derivative or
collective works based on the Program.
In addition, mere aggregation of another work not based on the Program
with the Program (or with a work based on the Program) on a volume of
a storage or distribution medium does not bring the other work under
the scope of this License.
3. You may copy and distribute the Program (or a work based on it,
under Section 2) in object code or executable form under the terms of
Sections 1 and 2 above provided that you also do one of the following:
a) Accompany it with the complete corresponding machine-readable
source code, which must be distributed under the terms of Sections
1 and 2 above on a medium customarily used for software interchange; or,
b) Accompany it with a written offer, valid for at least three
years, to give any third party, for a charge no more than your
cost of physically performing source distribution, a complete
machine-readable copy of the corresponding source code, to be
distributed under the terms of Sections 1 and 2 above on a medium
customarily used for software interchange; or,
c) Accompany it with the information you received as to the offer
to distribute corresponding source code. (This alternative is
allowed only for noncommercial distribution and only if you
received the program in object code or executable form with such
an offer, in accord with Subsection b above.)
The source code for a work means the preferred form of the work for
making modifications to it. For an executable work, complete source
code means all the source code for all modules it contains, plus any
associated interface definition files, plus the scripts used to
control compilation and installation of the executable. However, as a
special exception, the source code distributed need not include
anything that is normally distributed (in either source or binary
form) with the major components (compiler, kernel, and so on) of the
operating system on which the executable runs, unless that component
itself accompanies the executable.
If distribution of executable or object code is made by offering
access to copy from a designated place, then offering equivalent
access to copy the source code from the same place counts as
distribution of the source code, even though third parties are not
compelled to copy the source along with the object code.
4. You may not copy, modify, sublicense, or distribute the Program
except as expressly provided under this License. Any attempt
otherwise to copy, modify, sublicense or distribute the Program is
void, and will automatically terminate your rights under this License.
However, parties who have received copies, or rights, from you under
this License will not have their licenses terminated so long as such
parties remain in full compliance.
5. You are not required to accept this License, since you have not
signed it. However, nothing else grants you permission to modify or
distribute the Program or its derivative works. These actions are
prohibited by law if you do not accept this License. Therefore, by
modifying or distributing the Program (or any work based on the
Program), you indicate your acceptance of this License to do so, and
all its terms and conditions for copying, distributing or modifying
the Program or works based on it.
6. Each time you redistribute the Program (or any work based on the
Program), the recipient automatically receives a license from the
original licensor to copy, distribute or modify the Program subject to
these terms and conditions. You may not impose any further
restrictions on the recipients' exercise of the rights granted herein.
You are not responsible for enforcing compliance by third parties to
this License.
7. If, as a consequence of a court judgment or allegation of patent
infringement or for any other reason (not limited to patent issues),
conditions are imposed on you (whether by court order, agreement or
otherwise) that contradict the conditions of this License, they do not
excuse you from the conditions of this License. If you cannot
distribute so as to satisfy simultaneously your obligations under this
License and any other pertinent obligations, then as a consequence you
may not distribute the Program at all. For example, if a patent
license would not permit royalty-free redistribution of the Program by
all those who receive copies directly or indirectly through you, then
the only way you could satisfy both it and this License would be to
refrain entirely from distribution of the Program.
If any portion of this section is held invalid or unenforceable under
any particular circumstance, the balance of the section is intended to
apply and the section as a whole is intended to apply in other
circumstances.
It is not the purpose of this section to induce you to infringe any
patents or other property right claims or to contest validity of any
such claims; this section has the sole purpose of protecting the
integrity of the free software distribution system, which is
implemented by public license practices. Many people have made
generous contributions to the wide range of software distributed
through that system in reliance on consistent application of that
system; it is up to the author/donor to decide if he or she is willing
to distribute software through any other system and a licensee cannot
impose that choice.
This section is intended to make thoroughly clear what is believed to
be a consequence of the rest of this License.
8. If the distribution and/or use of the Program is restricted in
certain countries either by patents or by copyrighted interfaces, the
original copyright holder who places the Program under this License
may add an explicit geographical distribution limitation excluding
those countries, so that distribution is permitted only in or among
countries not thus excluded. In such case, this License incorporates
the limitation as if written in the body of this License.
9. The Free Software Foundation may publish revised and/or new versions
of the General Public License from time to time. Such new versions will
be similar in spirit to the present version, but may differ in detail to
address new problems or concerns.
Each version is given a distinguishing version number. If the Program
specifies a version number of this License which applies to it and "any
later version", you have the option of following the terms and conditions
either of that version or of any later version published by the Free
Software Foundation. If the Program does not specify a version number of
this License, you may choose any version ever published by the Free Software
Foundation.
10. If you wish to incorporate parts of the Program into other free
programs whose distribution conditions are different, write to the author
to ask for permission. For software which is copyrighted by the Free
Software Foundation, write to the Free Software Foundation; we sometimes
make exceptions for this. Our decision will be guided by the two goals
of preserving the free status of all derivatives of our free software and
of promoting the sharing and reuse of software generally.
NO WARRANTY
11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY
FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN
OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES
PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED
OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS
TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE
PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING,
REPAIR OR CORRECTION.
12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING
WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR
REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES,
INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING
OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED
TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY
YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER
PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE
POSSIBILITY OF SUCH DAMAGES.
END OF TERMS AND CONDITIONS
How to Apply These Terms to Your New Programs
If you develop a new program, and you want it to be of the greatest
possible use to the public, the best way to achieve this is to make it
free software which everyone can redistribute and change under these terms.
To do so, attach the following notices to the program. It is safest
to attach them to the start of each source file to most effectively
convey the exclusion of warranty; and each file should have at least
the "copyright" line and a pointer to where the full notice is found.
<one line to give the program's name and a brief idea of what it does.>
Copyright (C) 19yy <name of author>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Also add information on how to contact you by electronic and paper mail.
If the program is interactive, make it output a short notice like this
when it starts in an interactive mode:
Gnomovision version 69, Copyright (C) 19yy name of author
Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'.
This is free software, and you are welcome to redistribute it
under certain conditions; type `show c' for details.
The hypothetical commands `show w' and `show c' should show the appropriate
parts of the General Public License. Of course, the commands you use may
be called something other than `show w' and `show c'; they could even be
mouse-clicks or menu items--whatever suits your program.
You should also get your employer (if you work as a programmer) or your
school, if any, to sign a "copyright disclaimer" for the program, if
necessary. Here is a sample; alter the names:
Yoyodyne, Inc., hereby disclaims all copyright interest in the program
`Gnomovision' (which makes passes at compilers) written by James Hacker.
<signature of Ty Coon>, 1 April 1989
Ty Coon, President of Vice
This General Public License does not permit incorporating your program into
proprietary programs. If your program is a subroutine library, you may
consider it more useful to permit linking proprietary applications with the
library. If this is what you want to do, use the GNU Library General
Public License instead of this License.
POWERTOP
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <string.h>
#include <kstat.h>
#include <errno.h>
#include "powertop.h"
#define mW2W(value) ((value) / 1000)
typedef struct battery_state {
uint32_t exist;
uint32_t power_unit;
uint32_t bst_state;
double present_rate;
double remain_cap;
double last_cap;
} battery_state_t;
static char *kstat_batt_mod[3] = {NULL, "battery", "acpi_drv"};
static uint_t kstat_batt_idx;
static battery_state_t battery_state;
static int pt_battery_stat_snapshot(void);
/*
* Checks if the kstat module for battery information is present and
* whether it's called 'battery' or 'acpi_drv'
*/
void
pt_battery_mod_lookup(void)
{
kstat_ctl_t *kc = kstat_open();
if (kstat_lookup(kc, kstat_batt_mod[1], 0, NULL))
kstat_batt_idx = 1;
else
if (kstat_lookup(kc, kstat_batt_mod[2], 0, NULL))
kstat_batt_idx = 2;
else
kstat_batt_idx = 0;
(void) kstat_close(kc);
}
void
pt_battery_print(void)
{
int err;
(void) memset(&battery_state, 0, sizeof (battery_state_t));
/*
* The return value of pt_battery_stat_snapshot() can be used for
* debug or to show/hide the acpi power line. We currently don't
* make the distinction of a system that runs only on AC and one
* that runs on battery but has no kstat battery info.
*
* We still display the estimate power usage for systems
* running on AC with a fully charged battery because some
* batteries may still consume power.
*
* If pt_battery_mod_lookup() didn't find a kstat battery module, don't
* bother trying to take the snapshot
*/
if (kstat_batt_idx > 0) {
if ((err = pt_battery_stat_snapshot()) < 0)
pt_error("battery kstat not found (%d)\n", err);
}
pt_display_acpi_power(battery_state.exist, battery_state.present_rate,
battery_state.remain_cap, battery_state.last_cap,
battery_state.bst_state);
}
static int
pt_battery_stat_snapshot(void)
{
kstat_ctl_t *kc;
kstat_t *ksp;
kstat_named_t *knp;
kc = kstat_open();
/*
* power unit:
* 0 - Capacity information is reported in [mWh] and
* charge/discharge rate information in [mW]
* 1 - Capacity information is reported in [mAh] and
* charge/discharge rate information in [mA].
*/
ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
"battery BIF0");
if (ksp == NULL) {
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "bif_unit");
if (knp == NULL) {
(void) kstat_close(kc);
return (-1);
}
battery_state.power_unit = knp->value.ui32;
/*
* Present rate:
* the power or current being supplied or accepted
* through the battery's terminal
*/
ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
"battery BST0");
if (ksp == NULL) {
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "bst_rate");
if (knp == NULL) {
(void) kstat_close(kc);
return (-1);
}
if (knp->value.ui32 == 0xFFFFFFFF)
battery_state.present_rate = 0;
else {
battery_state.exist = 1;
battery_state.present_rate = mW2W((double)(knp->value.ui32));
}
/*
* Last Full charge capacity:
* Predicted battery capacity when fully charged.
*/
ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
"battery BIF0");
if (ksp == NULL) {
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "bif_last_cap");
if (knp == NULL) {
(void) kstat_close(kc);
return (-1);
}
battery_state.last_cap = mW2W((double)(knp->value.ui32));
/*
* Remaining capacity:
* the estimated remaining battery capacity
*/
ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
"battery BST0");
if (ksp == NULL) {
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "bst_rem_cap");
if (knp == NULL) {
(void) kstat_close(kc);
return (-1);
}
battery_state.remain_cap = mW2W((double)(knp->value.ui32));
/*
* Battery State:
* Bit0 - 1 : discharging
* Bit1 - 1 : charging
* Bit2 - 1 : critical energy state
*/
ksp = kstat_lookup(kc, kstat_batt_mod[kstat_batt_idx], 0,
"battery BST0");
if (ksp == NULL) {
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "bst_state");
if (knp == NULL) {
(void) kstat_close(kc);
return (-1);
}
battery_state.bst_state = knp->value.ui32;
(void) kstat_close(kc);
return (0);
}
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <stdlib.h>
#include <string.h>
#include <dtrace.h>
#include <kstat.h>
#include <errno.h>
#include "powertop.h"
#define HZ2MHZ(speed) ((speed) / MICROSEC)
#define DTP_ARG_COUNT 2
#define DTP_ARG_LENGTH 5
static uint64_t max_cpufreq = 0;
static dtrace_hdl_t *dtp;
static char **dtp_argv;
/*
* Enabling PM through /etc/power.conf
* See pt_cpufreq_suggest()
*/
static char default_conf[] = "/etc/power.conf";
static char default_pmconf[] = "/usr/sbin/pmconfig";
static char cpupm_enable[] = "echo cpupm enable >> /etc/power.conf";
static char cpupm_treshold[] = "echo cpu-threshold 1s >> /etc/power.conf";
/*
* Buffer containing DTrace program to track CPU frequency transitions
*/
static const char *dtp_cpufreq =
"hrtime_t last[$0];"
""
"BEGIN"
"{"
" begin = timestamp;"
"}"
""
":::cpu-change-speed"
"/last[(processorid_t)arg0] != 0/"
"{"
" this->cpu = (processorid_t)arg0;"
" this->oldspeed = (uint64_t)arg1;"
" @times[this->cpu, this->oldspeed] = sum(timestamp - last[this->cpu]);"
" last[this->cpu] = timestamp;"
"}"
":::cpu-change-speed"
"/last[(processorid_t)arg0] == 0/"
"{"
" this->cpu = (processorid_t)arg0;"
" this->oldspeed = (uint64_t)arg1;"
" @times[this->cpu, this->oldspeed] = sum(timestamp - begin);"
" last[this->cpu] = timestamp;"
"}";
/*
* Same as above, but only for a specific CPU
*/
static const char *dtp_cpufreq_c =
"hrtime_t last;"
""
"BEGIN"
"{"
" begin = timestamp;"
"}"
""
":::cpu-change-speed"
"/(processorid_t)arg0 == $1 &&"
" last != 0/"
"{"
" this->cpu = (processorid_t)arg0;"
" this->oldspeed = (uint64_t)arg1;"
" @times[this->cpu, this->oldspeed] = sum(timestamp - last);"
" last = timestamp;"
"}"
":::cpu-change-speed"
"/(processorid_t)arg0 == $1 &&"
" last == 0/"
"{"
" this->cpu = (processorid_t)arg0;"
" this->oldspeed = (uint64_t)arg1;"
" @times[this->cpu, this->oldspeed] = sum(timestamp - begin);"
" last = timestamp;"
"}";
static int pt_cpufreq_setup(void);
static int pt_cpufreq_snapshot(void);
static int pt_cpufreq_dtrace_walk(const dtrace_aggdata_t *, void *);
static void pt_cpufreq_stat_account(double, uint_t);
static int pt_cpufreq_snapshot_cpu(kstat_ctl_t *, uint_t);
static int pt_cpufreq_check_pm(void);
static void pt_cpufreq_enable(void);
static int
pt_cpufreq_setup(void)
{
if ((dtp_argv = malloc(sizeof (char *) * DTP_ARG_COUNT)) == NULL)
return (1);
if ((dtp_argv[0] = malloc(sizeof (char) * DTP_ARG_LENGTH)) == NULL) {
free(dtp_argv);
return (1);
}
(void) snprintf(dtp_argv[0], 5, "%d\0", g_ncpus_observed);
if (PT_ON_CPU) {
if ((dtp_argv[1] = malloc(sizeof (char) * DTP_ARG_LENGTH))
== NULL) {
free(dtp_argv[0]);
free(dtp_argv);
return (1);
}
(void) snprintf(dtp_argv[1], 5, "%d\0", g_observed_cpu);
}
return (0);
}
/*
* Perform setup necessary to enumerate and track CPU speed changes
*/
int
pt_cpufreq_stat_prepare(void)
{
dtrace_prog_t *prog;
dtrace_proginfo_t info;
dtrace_optval_t statustime;
kstat_ctl_t *kc;
kstat_t *ksp;
kstat_named_t *knp;
freq_state_info_t *state;
char *s, *token, *prog_ptr;
int err;
if ((err = pt_cpufreq_setup()) != 0) {
pt_error("failed to setup %s report (couldn't allocate "
"memory)\n", g_msg_freq_state);
return (errno);
}
state = g_pstate_info;
if ((g_cpu_power_states = calloc((size_t)g_ncpus,
sizeof (cpu_power_info_t))) == NULL)
return (-1);
/*
* Enumerate the CPU frequencies
*/
if ((kc = kstat_open()) == NULL)
return (errno);
ksp = kstat_lookup(kc, "cpu_info", g_cpu_table[g_observed_cpu], NULL);
if (ksp == NULL) {
err = errno;
(void) kstat_close(kc);
return (err);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "supported_frequencies_Hz");
s = knp->value.str.addr.ptr;
g_npstates = 0;
for (token = strtok(s, ":"), s = NULL;
token != NULL && g_npstates < NSTATES;
token = strtok(NULL, ":")) {
state->speed = HZ2MHZ(atoll(token));
if (state->speed > max_cpufreq)
max_cpufreq = state->speed;
state->total_time = (uint64_t)0;
g_npstates++;
state++;
}
if (token != NULL)
pt_error("CPU exceeds the supported number of %s\n",
g_msg_freq_state);
(void) kstat_close(kc);
/*
* Return if speed transition is not supported
*/
if (g_npstates < 2)
return (-1);
/*
* Setup DTrace to look for CPU frequency changes
*/
if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
pt_error("cannot open dtrace library for the %s report: %s\n",
g_msg_freq_state, dtrace_errmsg(NULL, err));
return (-2);
}
/*
* Execute different scripts (defined above) depending on
* user specified options. Default mode uses dtp_cpufreq.
*/
if (PT_ON_CPU)
prog_ptr = (char *)dtp_cpufreq_c;
else
prog_ptr = (char *)dtp_cpufreq;
if ((prog = dtrace_program_strcompile(dtp, prog_ptr,
DTRACE_PROBESPEC_NAME, 0, (1 + g_argc), dtp_argv)) == NULL) {
pt_error("failed to compile %s program\n", g_msg_freq_state);
return (dtrace_errno(dtp));
}
if (dtrace_program_exec(dtp, prog, &info) == -1) {
pt_error("failed to enable %s probes\n", g_msg_freq_state);
return (dtrace_errno(dtp));
}
if (dtrace_setopt(dtp, "aggsize", "128k") == -1)
pt_error("failed to set %s 'aggsize'\n", g_msg_freq_state);
if (dtrace_setopt(dtp, "aggrate", "0") == -1)
pt_error("failed to set %s 'aggrate'\n", g_msg_freq_state);
if (dtrace_setopt(dtp, "aggpercpu", 0) == -1)
pt_error("failed to set %s 'aggpercpu'\n", g_msg_freq_state);
if (dtrace_go(dtp) != 0) {
pt_error("failed to start %s observation\n", g_msg_freq_state);
return (dtrace_errno(dtp));
}
if (dtrace_getopt(dtp, "statusrate", &statustime) == -1) {
pt_error("failed to get %s 'statusrate'\n", g_msg_freq_state);
return (dtrace_errno(dtp));
}
return (0);
}
/*
* The DTrace probes have already been enabled, and are tracking
* CPU speed transitions. Take a snapshot of the aggregations, and
* look for any CPUs that have made a speed transition over the last
* sampling interval. Note that the aggregations may be empty if no
* speed transitions took place over the last interval. In that case,
* notate that we have already accounted for the time, so that when
* we do encounter a speed transition in a future sampling interval
* we can subtract that time back out.
*/
int
pt_cpufreq_stat_collect(double interval)
{
int i, ret;
/*
* Zero out the interval time reported by DTrace for
* this interval
*/
for (i = 0; i < g_npstates; i++)
g_pstate_info[i].total_time = 0;
for (i = 0; i < g_ncpus; i++)
g_cpu_power_states[i].dtrace_time = 0;
if (dtrace_status(dtp) == -1)
return (-1);
if (dtrace_aggregate_snap(dtp) != 0)
pt_error("failed to collect data for %s\n", g_msg_freq_state);
if (dtrace_aggregate_walk_keyvarsorted(dtp, pt_cpufreq_dtrace_walk,
NULL) != 0)
pt_error("failed to sort data for %s\n", g_msg_freq_state);
dtrace_aggregate_clear(dtp);
if ((ret = pt_cpufreq_snapshot()) != 0) {
pt_error("failed to snapshot %s state\n", g_msg_freq_state);
return (ret);
}
switch (g_op_mode) {
case PT_MODE_CPU:
pt_cpufreq_stat_account(interval, g_observed_cpu);
break;
case PT_MODE_DEFAULT:
default:
for (i = 0; i < g_ncpus_observed; i++)
pt_cpufreq_stat_account(interval, i);
break;
}
return (0);
}
static void
pt_cpufreq_stat_account(double interval, uint_t cpu)
{
cpu_power_info_t *cpu_pow;
uint64_t speed;
hrtime_t duration;
int i;
cpu_pow = &g_cpu_power_states[cpu];
speed = cpu_pow->current_pstate;
duration = (hrtime_t)(interval * NANOSEC) - cpu_pow->dtrace_time;
/*
* 'duration' may be a negative value when we're using or forcing a
* small interval, and the amount of time already accounted ends up
* being larger than the the former.
*/
if (duration < 0)
return;
for (i = 0; i < g_npstates; i++) {
if (g_pstate_info[i].speed == speed) {
g_pstate_info[i].total_time += duration;
cpu_pow->time_accounted += duration;
cpu_pow->speed_accounted = speed;
}
}
}
/*
* Take a snapshot of each CPU's speed by looking through the cpu_info kstats.
*/
static int
pt_cpufreq_snapshot(void)
{
kstat_ctl_t *kc;
int ret;
uint_t i;
if ((kc = kstat_open()) == NULL)
return (errno);
switch (g_op_mode) {
case PT_MODE_CPU:
ret = pt_cpufreq_snapshot_cpu(kc, g_observed_cpu);
break;
case PT_MODE_DEFAULT:
default:
for (i = 0; i < g_ncpus_observed; i++)
if ((ret = pt_cpufreq_snapshot_cpu(kc, i)) != 0)
break;
break;
}
if (kstat_close(kc) != 0)
pt_error("couldn't close %s kstat\n", g_msg_freq_state);
return (ret);
}
static int
pt_cpufreq_snapshot_cpu(kstat_ctl_t *kc, uint_t cpu)
{
kstat_t *ksp;
kstat_named_t *knp;
ksp = kstat_lookup(kc, "cpu_info", g_cpu_table[cpu], NULL);
if (ksp == NULL) {
pt_error("couldn't find 'cpu_info' kstat for CPU %d\n while "
"taking a snapshot of %s\n", cpu, g_msg_freq_state);
return (1);
}
if (kstat_read(kc, ksp, NULL) == -1) {
pt_error("couldn't read 'cpu_info' kstat for CPU %d\n while "
"taking a snapshot of %s\n", cpu, g_msg_freq_state);
return (2);
}
knp = kstat_data_lookup(ksp, "current_clock_Hz");
if (knp == NULL) {
pt_error("couldn't find 'current_clock_Hz' kstat for CPU %d "
"while taking a snapshot of %s\n", cpu, g_msg_freq_state);
return (3);
}
g_cpu_power_states[cpu].current_pstate = HZ2MHZ(knp->value.ui64);
return (0);
}
/*
* DTrace aggregation walker that sorts through a snapshot of the
* aggregation data collected during firings of the cpu-change-speed
* probe.
*/
/*ARGSUSED*/
static int
pt_cpufreq_dtrace_walk(const dtrace_aggdata_t *data, void *arg)
{
dtrace_aggdesc_t *aggdesc = data->dtada_desc;
dtrace_recdesc_t *cpu_rec, *speed_rec;
cpu_power_info_t *cp;
int32_t cpu;
uint64_t speed;
hrtime_t res;
int i;
if (strcmp(aggdesc->dtagd_name, "times") == 0) {
cpu_rec = &aggdesc->dtagd_rec[1];
speed_rec = &aggdesc->dtagd_rec[2];
/* LINTED - alignment */
cpu = *(int32_t *)(data->dtada_data + cpu_rec->dtrd_offset);
/* LINTED - alignment */
res = *((hrtime_t *)(data->dtada_percpu[cpu]));
/* LINTED - alignment */
speed = *(uint64_t *)(data->dtada_data +
speed_rec->dtrd_offset);
if (speed == 0)
speed = max_cpufreq;
else
speed = HZ2MHZ(speed);
/*
* We have an aggregation record for "cpu" being at "speed"
* for an interval of "n" nanoseconds. The reported interval
* may exceed the powertop sampling interval, since we only
* notice during potentially infrequent firings of the
* "speed change" DTrace probe. In this case powertop would
* have already accounted for the portions of the interval
* that happened during prior powertop samplings, so subtract
* out time already accounted.
*/
cp = &g_cpu_power_states[cpu];
for (i = 0; i < g_npstates; i++) {
if (g_pstate_info[i].speed == speed) {
if (cp->time_accounted > 0 &&
cp->speed_accounted == speed) {
if (res > cp->time_accounted) {
res -= cp->time_accounted;
cp->time_accounted = 0;
cp->speed_accounted = 0;
} else {
return (DTRACE_AGGWALK_NEXT);
}
}
g_pstate_info[i].total_time += res;
cp->dtrace_time += res;
}
}
}
return (DTRACE_AGGWALK_NEXT);
}
/*
* Checks if PM is enabled in /etc/power.conf, enabling if not
*/
void
pt_cpufreq_suggest(void)
{
int ret = pt_cpufreq_check_pm();
switch (ret) {
case 0:
pt_sugg_add("Suggestion: enable CPU power management by "
"pressing the P key", 40, 'P', (char *)g_msg_freq_enable,
pt_cpufreq_enable);
break;
}
}
/*
* Checks /etc/power.conf and returns:
*
* 0 if CPUPM is not enabled
* 1 if there's nothing for us to do because:
* (a) the system does not support frequency scaling
* (b) there's no power.conf.
* 2 if CPUPM is enabled
* 3 if the system is running in poll-mode, as opposed to event-mode
*
* Notice the ordering of the return values, they will be picked up and
* switched upon ascendingly.
*/
static int
pt_cpufreq_check_pm(void)
{
char line[1024];
FILE *file;
int ret = 0;
if (g_npstates < 2 || (file = fopen(default_conf, "r")) == NULL)
return (1);
(void) memset(line, 0, 1024);
while (fgets(line, 1024, file)) {
if (strstr(line, "cpupm")) {
if (strstr(line, "enable")) {
(void) fclose(file);
return (2);
}
}
if (strstr(line, "poll"))
ret = 3;
}
(void) fclose(file);
return (ret);
}
/*
* Used as a suggestion, sets PM in /etc/power.conf and
* a 1sec threshold, then calls /usr/sbin/pmconfig
*/
static void
pt_cpufreq_enable(void)
{
(void) system(cpupm_enable);
(void) system(cpupm_treshold);
(void) system(default_pmconf);
if (pt_sugg_remove(pt_cpufreq_enable) == 0)
pt_error("failed to remove a %s suggestion\n",
g_msg_freq_state);
}
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <string.h>
#include <dtrace.h>
#include "powertop.h"
#define S2NS(x) ((x) * (NANOSEC))
static dtrace_hdl_t *dtp;
/*
* Buffer containing DTrace program to track CPU idle state transitions
*/
static const char *dtp_cpuidle =
":::idle-state-transition"
"/arg0 != 0/"
"{"
" self->start = timestamp;"
" self->state = arg0;"
"}"
""
":::idle-state-transition"
"/arg0 == 0 && self->start/"
"{"
" @number[self->state] = count();"
" @times[self->state] = sum(timestamp - self->start);"
" self->start = 0;"
" self->state = 0;"
"}";
/*
* Same as above but only for a specific CPU
*/
static const char *dtp_cpuidle_c =
":::idle-state-transition"
"/cpu == $0 &&"
" arg0 != 0/"
"{"
" self->start = timestamp;"
" self->state = arg0;"
"}"
""
":::idle-state-transition"
"/cpu == $0 &&"
" arg0 == 0 && self->start/"
"{"
" @number[self->state] = count();"
" @times[self->state] = sum(timestamp - self->start);"
" self->start = 0;"
" self->state = 0;"
"}";
static int pt_cpuidle_dtrace_walk(const dtrace_aggdata_t *, void *);
/*
* Perform setup necessary to track CPU idle state transitions
*/
int
pt_cpuidle_stat_prepare(void)
{
dtrace_prog_t *prog;
dtrace_proginfo_t info;
dtrace_optval_t statustime;
int err;
char *prog_ptr;
if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
pt_error("cannot open dtrace library for the %s report: %s\n",
g_msg_idle_state, dtrace_errmsg(NULL, err));
return (-1);
}
/*
* Execute different scripts (defined above) depending on
* user specified options.
*/
if (PT_ON_CPU)
prog_ptr = (char *)dtp_cpuidle_c;
else
prog_ptr = (char *)dtp_cpuidle;
if ((prog = dtrace_program_strcompile(dtp, prog_ptr,
DTRACE_PROBESPEC_NAME, 0, g_argc, g_argv)) == NULL) {
pt_error("failed to compile %s program\n", g_msg_idle_state);
return (dtrace_errno(dtp));
}
if (dtrace_program_exec(dtp, prog, &info) == -1) {
pt_error("failed to enable %s probes\n", g_msg_idle_state);
return (dtrace_errno(dtp));
}
if (dtrace_setopt(dtp, "aggsize", "128k") == -1)
pt_error("failed to set %s 'aggsize'\n", g_msg_idle_state);
if (dtrace_setopt(dtp, "aggrate", "0") == -1)
pt_error("failed to set %s 'aggrate'\n", g_msg_idle_state);
if (dtrace_setopt(dtp, "aggpercpu", 0) == -1)
pt_error("failed to set %s 'aggpercpu'\n", g_msg_idle_state);
if (dtrace_go(dtp) != 0) {
pt_error("failed to start %s observation\n", g_msg_idle_state);
return (dtrace_errno(dtp));
}
if (dtrace_getopt(dtp, "statusrate", &statustime) == -1) {
pt_error("failed to get %s 'statusrate'\n", g_msg_idle_state);
return (dtrace_errno(dtp));
}
return (0);
}
/*
* The DTrace probes have been enabled, and are tracking CPU idle state
* transitions. Take a snapshot of the aggregations, and invoke the aggregation
* walker to process any records. The walker does most of the accounting work
* chalking up time spent into the g_cstate_info structure.
*/
int
pt_cpuidle_stat_collect(double interval)
{
int i;
hrtime_t t = 0;
/*
* Assume that all the time spent in this interval will
* be the default "0" state. The DTrace walker will reallocate
* time out of the default bucket as it processes aggregation
* records for time spent in other states.
*/
g_cstate_info[0].total_time = (uint64_t)S2NS(interval *
g_ncpus_observed);
if (dtrace_status(dtp) == -1)
return (-1);
if (dtrace_aggregate_snap(dtp) != 0)
pt_error("failed to collect data for %s\n", g_msg_idle_state);
if (dtrace_aggregate_walk_keyvarsorted(dtp, pt_cpuidle_dtrace_walk,
NULL) != 0)
pt_error("failed to sort %s data\n", g_msg_idle_state);
dtrace_aggregate_clear(dtp);
/*
* Populate g_cstate_info with the correct amount of time spent
* in each C state and update the number of C states in g_max_cstate
*/
g_total_c_time = 0;
for (i = 0; i < NSTATES; i++) {
if (g_cstate_info[i].total_time > 0) {
g_total_c_time += g_cstate_info[i].total_time;
if (i > g_max_cstate)
g_max_cstate = i;
if (g_cstate_info[i].last_time > t) {
t = g_cstate_info[i].last_time;
g_longest_cstate = i;
}
}
}
return (0);
}
/*
* DTrace aggregation walker that sorts through a snapshot of data records
* collected during firings of the idle-state-transition probe.
*
* XXX A way of querying the current idle state for a CPU is needed in addition
* to logic similar to that in cpufreq.c
*/
/*ARGSUSED*/
static int
pt_cpuidle_dtrace_walk(const dtrace_aggdata_t *data, void *arg)
{
dtrace_aggdesc_t *aggdesc = data->dtada_desc;
dtrace_recdesc_t *rec;
uint64_t n = 0, state;
int i;
rec = &aggdesc->dtagd_rec[1];
switch (g_bit_depth) {
case 32:
/* LINTED - alignment */
state = *(uint32_t *)(data->dtada_data +
rec->dtrd_offset);
break;
case 64:
/* LINTED - alignment */
state = *(uint64_t *)(data->dtada_data +
rec->dtrd_offset);
break;
}
if (strcmp(aggdesc->dtagd_name, "number") == 0) {
for (i = 0; i < g_ncpus; i++) {
/* LINTED - alignment */
n += *((uint64_t *)(data->dtada_percpu[i]));
}
g_total_events += n;
g_cstate_info[state].events += n;
}
else
if (strcmp(aggdesc->dtagd_name, "times") == 0) {
for (i = 0; i < g_ncpus; i++) {
/* LINTED - alignment */
n += *((uint64_t *)(data->dtada_percpu[i]));
}
g_cstate_info[state].last_time = n;
g_cstate_info[state].total_time += n;
if (g_cstate_info[0].total_time >= n)
g_cstate_info[0].total_time -= n;
else
g_cstate_info[0].total_time = 0;
}
return (DTRACE_AGGWALK_NEXT);
}
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* Copyright 2013 Nexenta Systems, Inc. All rights reserved.
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <curses.h>
#include <signal.h>
#include <fcntl.h>
#include "powertop.h"
/*
* Minimum terminal height and width to run PowerTOP on curses mode.
*/
#define PT_MIN_COLS 70
#define PT_MIN_ROWS 15
/*
* Display colors
*/
#define PT_COLOR_DEFAULT 1
#define PT_COLOR_HEADER_BAR 2
#define PT_COLOR_ERROR 3
#define PT_COLOR_RED 4
#define PT_COLOR_YELLOW 5
#define PT_COLOR_GREEN 6
#define PT_COLOR_BRIGHT 7
#define PT_COLOR_BLUE 8
/*
* Constants for pt_display_setup()
*/
#define SINGLE_LINE_SW 1
#define LENGTH_SUGG_SW 2
#define TITLE_LINE 1
#define BLANK_LINE 1
#define NEXT_LINE 1
#define print(win, y, x, fmt, args...) \
if (PT_ON_DUMP) \
(void) printf(fmt, ## args); \
else \
(void) mvwprintw(win, y, x, fmt, ## args);
enum pt_subwindows {
SW_TITLE,
SW_IDLE,
SW_FREQ,
SW_WAKEUPS,
SW_POWER,
SW_EVENTS,
SW_SUGG,
SW_STATUS,
SW_COUNT
};
typedef struct sb_slot {
char *msg;
struct sb_slot *prev;
struct sb_slot *next;
} sb_slot_t;
static WINDOW *sw[SW_COUNT];
static int win_cols, win_rows;
static sb_slot_t *status_bar;
/*
* Delete all subwindows and reset the terminal to a non-visual mode. This
* routine is used during resize events and before exiting.
*/
static void
pt_display_cleanup(void)
{
int i;
for (i = 0; i < SW_COUNT; i++) {
if (sw[i] != NULL) {
(void) delwin(sw[i]);
sw[i] = NULL;
}
}
(void) endwin();
(void) fflush(stdout);
(void) putchar('\r');
}
static void
pt_display_get_size(void)
{
getmaxyx(stdscr, win_rows, win_cols);
if (win_rows < PT_MIN_ROWS || win_cols < PT_MIN_COLS) {
pt_display_cleanup();
(void) printf("\n\nPowerTOP cannot run in such a small "
"terminal window. Please resize it.\n\n");
exit(EXIT_FAILURE);
}
}
void
pt_display_resize(void)
{
pt_display_cleanup();
(void) pt_display_init_curses();
pt_display_setup(B_TRUE);
pt_display_title_bar();
pt_display_states();
if (g_features & FEATURE_EVENTS) {
pt_display_wakeups(g_interval_length);
pt_display_events(g_interval_length);
}
pt_battery_print();
pt_sugg_pick();
pt_display_status_bar();
pt_display_update();
g_sig_resize = B_FALSE;
(void) signal(SIGWINCH, pt_sig_handler);
}
/*
* This part was re-written to be human readable and easy to modify. Please
* try to keep it that way and help us save some time.
*
* Friendly reminder:
* subwin(WINDOW *orig, int nlines, int ncols, int begin_y, int begin_x)
*/
void
pt_display_setup(boolean_t resized)
{
/*
* These variables are used to properly set the initial y position and
* number of lines in each subwindow, as the number of supported CPU
* states affects their placement.
*/
int cstate_lines, event_lines, pos_y = 0;
/*
* In theory, all systems have at least two idle states. We add two here
* since we have to use DTrace to figure out how many this box has.
*/
cstate_lines = TITLE_LINE + max((g_max_cstate+2), g_npstates);
sw[SW_TITLE] = subwin(stdscr, SINGLE_LINE_SW, win_cols, pos_y, 0);
pos_y += NEXT_LINE + BLANK_LINE;
sw[SW_IDLE] = subwin(stdscr, cstate_lines, win_cols/2 + 1, pos_y, 0);
sw[SW_FREQ] = subwin(stdscr, cstate_lines, win_cols/2 - 8, pos_y,
win_cols/2 + 8);
pos_y += cstate_lines + BLANK_LINE;
sw[SW_WAKEUPS] = subwin(stdscr, SINGLE_LINE_SW, win_cols, pos_y, 0);
pos_y += NEXT_LINE;
sw[SW_POWER] = subwin(stdscr, SINGLE_LINE_SW, win_cols, pos_y, 0);
pos_y += NEXT_LINE + BLANK_LINE;
event_lines = win_rows - SINGLE_LINE_SW - NEXT_LINE - LENGTH_SUGG_SW -
pos_y;
if (event_lines > 0) {
sw[SW_EVENTS] = subwin(stdscr, event_lines, win_cols, pos_y, 0);
} else {
pt_display_cleanup();
(void) printf("\n\nPowerTOP cannot run in such a small "
"terminal window, please resize it.\n\n");
exit(EXIT_FAILURE);
}
pos_y += event_lines + NEXT_LINE;
sw[SW_SUGG] = subwin(stdscr, SINGLE_LINE_SW, win_cols, pos_y, 0);
pos_y += BLANK_LINE + NEXT_LINE;
sw[SW_STATUS] = subwin(stdscr, SINGLE_LINE_SW, win_cols, pos_y, 0);
if (!resized) {
status_bar = NULL;
pt_display_mod_status_bar("Q - Quit");
pt_display_mod_status_bar("R - Refresh");
}
}
/*
* This routine handles all the necessary curses initialization.
*/
void
pt_display_init_curses(void)
{
(void) initscr();
(void) atexit(pt_display_cleanup);
pt_display_get_size();
(void) start_color();
/*
* Enable keyboard mapping
*/
(void) keypad(stdscr, TRUE);
/*
* Tell curses not to do NL->CR/NL on output
*/
(void) nonl();
/*
* Take input chars one at a time, no wait for \n
*/
(void) cbreak();
/*
* Dont echo input
*/
(void) noecho();
/*
* Turn off cursor
*/
(void) curs_set(0);
(void) init_pair(PT_COLOR_DEFAULT, COLOR_WHITE, COLOR_BLACK);
(void) init_pair(PT_COLOR_HEADER_BAR, COLOR_BLACK, COLOR_WHITE);
(void) init_pair(PT_COLOR_ERROR, COLOR_BLACK, COLOR_RED);
(void) init_pair(PT_COLOR_RED, COLOR_WHITE, COLOR_RED);
(void) init_pair(PT_COLOR_YELLOW, COLOR_WHITE, COLOR_YELLOW);
(void) init_pair(PT_COLOR_GREEN, COLOR_WHITE, COLOR_GREEN);
(void) init_pair(PT_COLOR_BLUE, COLOR_WHITE, COLOR_BLUE);
(void) init_pair(PT_COLOR_BRIGHT, COLOR_WHITE, COLOR_BLACK);
}
void
pt_display_update(void)
{
(void) doupdate();
}
void
pt_display_title_bar(void)
{
char title_pad[10];
(void) wattrset(sw[SW_TITLE], COLOR_PAIR(PT_COLOR_HEADER_BAR));
(void) wbkgd(sw[SW_TITLE], COLOR_PAIR(PT_COLOR_HEADER_BAR));
(void) werase(sw[SW_TITLE]);
(void) snprintf(title_pad, 10, "%%%ds",
(win_cols - strlen(TITLE))/2 + strlen(TITLE));
/* LINTED: E_SEC_PRINTF_VAR_FMT */
print(sw[SW_TITLE], 0, 0, title_pad, TITLE);
(void) wnoutrefresh(sw[SW_TITLE]);
}
void
pt_display_status_bar(void)
{
sb_slot_t *n = status_bar;
int x = 0;
(void) werase(sw[SW_STATUS]);
while (n && x < win_cols) {
(void) wattron(sw[SW_STATUS], A_REVERSE);
print(sw[SW_STATUS], 0, x, "%s", n->msg);
(void) wattroff(sw[SW_STATUS], A_REVERSE);
x += strlen(n->msg) + 1;
n = n->next;
}
(void) wnoutrefresh(sw[SW_STATUS]);
}
/*
* Adds or removes items to the status bar automatically.
* Only one instance of an item allowed.
*/
void
pt_display_mod_status_bar(char *msg)
{
sb_slot_t *new, *n;
boolean_t found = B_FALSE, first = B_FALSE;
if (msg == NULL) {
pt_error("can't add an empty status bar item\n");
return;
}
if (status_bar != NULL) {
/*
* Non-empty status bar. Look for an entry matching this msg.
*/
for (n = status_bar; n != NULL; n = n->next) {
if (strcmp(msg, n->msg) == 0) {
if (n != status_bar)
n->prev->next = n->next;
else
first = B_TRUE;
if (n->next != NULL) {
n->next->prev = n->prev;
if (first)
status_bar = n->next;
} else {
if (first)
status_bar = NULL;
}
free(n);
found = B_TRUE;
}
}
/*
* Found and removed at least one occurrance of msg, refresh
* the bar and return.
*/
if (found) {
return;
} else {
/*
* Inserting a new msg, walk to the end of the bar.
*/
for (n = status_bar; n->next != NULL; n = n->next)
;
}
}
if ((new = calloc(1, sizeof (sb_slot_t))) == NULL) {
pt_error("failed to allocate a new status bar slot\n");
} else {
new->msg = strdup(msg);
/*
* Check if it's the first entry.
*/
if (status_bar == NULL) {
status_bar = new;
new->prev = NULL;
} else {
new->prev = n;
n->next = new;
}
new->next = NULL;
}
}
void
pt_display_states(void)
{
char c[100];
int i;
double total_pstates = 0.0, avg, res;
uint64_t p0_speed, p1_speed;
print(sw[SW_IDLE], 0, 0, "%s\tAvg\tResidency\n", g_msg_idle_state);
if (g_features & FEATURE_CSTATE) {
res = (((double)g_cstate_info[0].total_time / g_total_c_time))
* 100;
(void) sprintf(c, "C0 (cpu running)\t\t(%.1f%%)\n", (float)res);
print(sw[SW_IDLE], 1, 0, "%s", c);
for (i = 1; i <= g_max_cstate; i++) {
/*
* In situations where the load is too intensive, the
* system might not transition at all.
*/
if (g_cstate_info[i].events > 0)
avg = (((double)g_cstate_info[i].total_time/
MICROSEC)/g_cstate_info[i].events);
else
avg = 0;
res = ((double)g_cstate_info[i].total_time/
g_total_c_time) * 100;
(void) sprintf(c, "C%d\t\t\t%.1fms\t(%.1f%%)\n",
i, (float)avg, (float)res);
print(sw[SW_IDLE], i + 1, 0, "%s", c);
}
}
if (!PT_ON_DUMP)
(void) wnoutrefresh(sw[SW_IDLE]);
print(sw[SW_FREQ], 0, 0, "%s\n", g_msg_freq_state);
if (g_features & FEATURE_PSTATE) {
for (i = 0; i < g_npstates; i++) {
total_pstates +=
(double)(g_pstate_info[i].total_time/
g_ncpus_observed/MICROSEC);
}
/*
* display ACPI_PSTATE from P(n) to P(1)
*/
for (i = 0; i < g_npstates - 1; i++) {
(void) sprintf(c, "%4lu Mhz\t%.1f%%",
(long)g_pstate_info[i].speed,
100 * (g_pstate_info[i].total_time/
g_ncpus_observed/MICROSEC/total_pstates));
print(sw[SW_FREQ], i+1, 0, "%s\n", c);
}
/*
* Display ACPI_PSTATE P0 according to if turbo
* mode is supported
*/
if (g_turbo_supported) {
int p_diff = 1;
p0_speed = g_pstate_info[g_npstates - 1].speed;
p1_speed = g_pstate_info[g_npstates - 2].speed;
/*
* AMD systems don't have a visible extra Pstate
* indicating turbo mode as Intel does. Use the
* actual P0 frequency in that case.
*/
if (p0_speed != p1_speed + 1) {
p1_speed = p0_speed;
p_diff = 0;
}
/*
* If g_turbo_ratio <= 1.0, it will be ignored.
* we display P(0) as P(1) + p_diff.
*/
if (g_turbo_ratio <= 1.0) {
p0_speed = p1_speed + p_diff;
} else {
/*
* If g_turbo_ratio > 1.0, that means
* turbo mode works. So, P(0) = ratio *
* P(1);
*/
p0_speed = (uint64_t)(p1_speed *
g_turbo_ratio);
if (p0_speed < (p1_speed + p_diff))
p0_speed = p1_speed + p_diff;
}
/*
* Reset the ratio for the next round
*/
g_turbo_ratio = 0.0;
/*
* Setup the string for the display
*/
(void) sprintf(c, "%4lu Mhz(turbo)\t%.1f%%",
(long)p0_speed,
100 * (g_pstate_info[i].total_time/
g_ncpus_observed/MICROSEC/total_pstates));
} else {
(void) sprintf(c, "%4lu Mhz\t%.1f%%",
(long)g_pstate_info[i].speed,
100 * (g_pstate_info[i].total_time/
g_ncpus_observed/MICROSEC/total_pstates));
}
print(sw[SW_FREQ], i+1, 0, "%s\n", c);
} else {
if (g_npstates == 1) {
(void) sprintf(c, "%4lu Mhz\t%.1f%%",
(long)g_pstate_info[0].speed, 100.0);
print(sw[SW_FREQ], 1, 0, "%s\n", c);
}
}
if (!PT_ON_DUMP)
(void) wnoutrefresh(sw[SW_FREQ]);
}
void
pt_display_acpi_power(uint32_t flag, double rate, double rem_cap, double cap,
uint32_t state)
{
char buffer[1024];
(void) sprintf(buffer, "no ACPI power usage estimate available");
if (!PT_ON_DUMP)
(void) werase(sw[SW_POWER]);
if (flag) {
char *c;
(void) sprintf(buffer, "Power usage (ACPI estimate): %.3fW",
rate);
(void) strcat(buffer, " ");
c = &buffer[strlen(buffer)];
switch (state) {
case 0:
(void) sprintf(c, "(running on AC power, fully "
"charged)");
break;
case 1:
(void) sprintf(c, "(discharging: %3.1f hours)",
(uint32_t)rem_cap/rate);
break;
case 2:
(void) sprintf(c, "(charging: %3.1f hours)",
(uint32_t)(cap - rem_cap)/rate);
break;
case 4:
(void) sprintf(c, "(##critically low battery power##)");
break;
}
}
print(sw[SW_POWER], 0, 0, "%s\n", buffer);
if (!PT_ON_DUMP)
(void) wnoutrefresh(sw[SW_POWER]);
}
void
pt_display_wakeups(double interval)
{
char c[100];
int i, event_sum = 0;
event_info_t *event = g_event_info;
if (!PT_ON_DUMP) {
(void) werase(sw[SW_WAKEUPS]);
(void) wbkgd(sw[SW_WAKEUPS], COLOR_PAIR(PT_COLOR_RED));
(void) wattron(sw[SW_WAKEUPS], A_BOLD);
}
/*
* calculate the actual total event number
*/
for (i = 0; i < g_top_events; i++, event++)
event_sum += event->total_count;
/*
* g_total_events is the sum of the number of Cx->C0 transition,
* So when the system is very busy, the idle thread will have no
* chance or very seldom to be scheduled, this could cause >100%
* event report. Re-assign g_total_events to the actual event
* number is a way to avoid this issue.
*/
if (event_sum > g_total_events)
g_total_events = event_sum;
(void) sprintf(c, "Wakeups-from-idle per second: %4.1f\tinterval: "
"%.1fs", (double)(g_total_events/interval), interval);
print(sw[SW_WAKEUPS], 0, 0, "%s\n", c);
if (!PT_ON_DUMP)
(void) wnoutrefresh(sw[SW_WAKEUPS]);
}
void
pt_display_events(double interval)
{
char c[100];
int i;
double events;
event_info_t *event = g_event_info;
if (!PT_ON_DUMP) {
(void) werase(sw[SW_EVENTS]);
(void) wbkgd(sw[SW_EVENTS], COLOR_PAIR(PT_COLOR_DEFAULT));
(void) wattron(sw[SW_EVENTS], COLOR_PAIR(PT_COLOR_DEFAULT));
}
/*
* Sort the event report list
*/
if (g_top_events > EVENT_NUM_MAX)
g_top_events = EVENT_NUM_MAX;
qsort((void *)g_event_info, g_top_events, sizeof (event_info_t),
pt_event_compare);
if (PT_ON_CPU)
(void) sprintf(c, "Top causes for wakeups on CPU %d:\n",
g_observed_cpu);
else
(void) sprintf(c, "Top causes for wakeups:\n");
print(sw[SW_EVENTS], 0, 0, "%s", c);
for (i = 0; i < g_top_events; i++, event++) {
if (g_total_events > 0 && event->total_count > 0)
events = (double)event->total_count/
(double)g_total_events;
else
continue;
(void) sprintf(c, "%4.1f%% (%5.1f)", 100 * events,
(double)event->total_count/interval);
print(sw[SW_EVENTS], i+1, 0, "%s", c);
print(sw[SW_EVENTS], i+1, 16, "%20s :",
event->offender_name);
print(sw[SW_EVENTS], i+1, 40, "%-64s\n",
event->offense_name);
}
if (!PT_ON_DUMP)
(void) wnoutrefresh(sw[SW_EVENTS]);
}
void
pt_display_suggestions(char *sug)
{
(void) werase(sw[SW_SUGG]);
if (sug != NULL)
print(sw[SW_SUGG], 0, 0, "%s", sug);
(void) wnoutrefresh(sw[SW_SUGG]);
}
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <string.h>
#include <stdlib.h>
#include <dtrace.h>
#include "powertop.h"
static dtrace_hdl_t *dtp;
static event_info_t *event;
/*ARGSUSED*/
static int
pt_events_walk(const dtrace_aggdata_t *data, void *arg)
{
dtrace_aggdesc_t *aggdesc = data->dtada_desc;
dtrace_recdesc_t *rec1, *rec2, *rec3;
dtrace_syminfo_t dts;
GElf_Sym sym;
uint64_t offender_addr;
uint64_t n = 0;
int32_t *instance, *offender_cpu;
int i;
char *offense_name;
if (g_top_events >= EVENT_NUM_MAX)
return (0);
rec1 = &aggdesc->dtagd_rec[1];
rec2 = &aggdesc->dtagd_rec[2];
/*
* Report interrupts
*/
if (strcmp(aggdesc->dtagd_name, "interrupts") == 0) {
offense_name = data->dtada_data + rec1->dtrd_offset;
/* LINTED - alignment */
instance = (int32_t *)(data->dtada_data + rec2->dtrd_offset);
(void) snprintf((char *)(event->offender_name),
EVENT_NAME_MAX, "%s", "<interrupt>");
(void) snprintf((char *)(event->offense_name),
EVENT_NAME_MAX, "%s#%d", offense_name, *instance);
/*
* Report kernel events
*/
} else if (strcmp(aggdesc->dtagd_name, "events_k") == 0) {
(void) snprintf((char *)(event->offender_name),
EVENT_NAME_MAX, "%s", "<kernel>");
/*
* Casting offender_addr to the wrong type will cause
* dtrace_lookup_by_addr to return 0 and the report
* to show an address instead of a name.
*/
switch (g_bit_depth) {
case 32:
/* LINTED - alignment */
offender_addr = *(uint32_t *)(data->dtada_data +
rec1->dtrd_offset);
break;
case 64:
/* LINTED - alignment */
offender_addr = *(uint64_t *)(data->dtada_data +
rec1->dtrd_offset);
break;
}
/*
* We have the address of the kernel callout.
* Try to resolve it into a meaningful symbol
*/
if (offender_addr != 0 && dtrace_lookup_by_addr(dtp,
offender_addr, &sym, &dts) == 0) {
(void) snprintf((char *)(event->offense_name),
EVENT_NAME_MAX, "%s`%s", dts.dts_object,
dts.dts_name);
} else {
(void) snprintf((char *)(event->offense_name),
EVENT_NAME_MAX, "0x%llx", offender_addr);
}
/*
* Report user events
*/
} else if (strcmp(aggdesc->dtagd_name, "events_u") == 0) {
offense_name = data->dtada_data + rec1->dtrd_offset;
(void) snprintf((char *)(event->offender_name),
EVENT_NAME_MAX, "%s", offense_name);
(void) snprintf((char *)(event->offense_name),
EVENT_NAME_MAX, "<scheduled timeout expiration>");
/*
* Report cross calls
*/
} else if (strcmp(aggdesc->dtagd_name, "events_x") == 0) {
offense_name = data->dtada_data + rec1->dtrd_offset;
(void) snprintf((char *)(event->offender_name),
EVENT_NAME_MAX, "%s", offense_name);
switch (g_bit_depth) {
case 32:
/* LINTED - alignment */
offender_addr = *(uint32_t *)(data->dtada_data +
rec2->dtrd_offset);
break;
case 64:
/* LINTED - alignment */
offender_addr = *(uint64_t *)(data->dtada_data +
rec2->dtrd_offset);
break;
}
/*
* Try to resolve the address of the cross call function.
*/
if (offender_addr != 0 && dtrace_lookup_by_addr(dtp,
offender_addr, &sym, &dts) == 0) {
(void) snprintf((char *)(event->offense_name),
EVENT_NAME_MAX, "<xcalls> %s`%s",
dts.dts_object, dts.dts_name);
} else {
(void) snprintf((char *)(event->offense_name),
EVENT_NAME_MAX, "<xcalls>");
}
/*
* Report cross calls from other CPUs than the one we're observing
* with the -C option
*/
} else if (strcmp(aggdesc->dtagd_name, "events_xc") == 0) {
rec3 = &aggdesc->dtagd_rec[3];
offense_name = data->dtada_data + rec1->dtrd_offset;
(void) snprintf((char *)(event->offender_name),
EVENT_NAME_MAX, "%s", offense_name);
switch (g_bit_depth) {
case 32:
/* LINTED - alignment */
offender_addr = *(uint32_t *)(data->dtada_data +
rec2->dtrd_offset);
break;
case 64:
/* LINTED - alignment */
offender_addr = *(uint64_t *)(data->dtada_data +
rec2->dtrd_offset);
break;
}
/* LINTED - alignment */
offender_cpu = (int32_t *)(data->dtada_data +
rec3->dtrd_offset);
/*
* Try to resolve the address of the cross call function.
*/
if (offender_addr != 0 && dtrace_lookup_by_addr(dtp,
offender_addr, &sym, &dts) == 0) {
(void) snprintf((char *)(event->offense_name),
EVENT_NAME_MAX, "<xcalls> %s`%s (CPU %d)",
dts.dts_object, dts.dts_name, *offender_cpu);
} else {
(void) snprintf((char *)(event->offense_name),
EVENT_NAME_MAX, "<xcalls> (CPU %d)",
*offender_cpu);
}
/*
* Report unknown events
*/
} else {
(void) snprintf((char *)(event->offender_name),
EVENT_NAME_MAX, "%s", "<unknown>");
(void) snprintf((char *)(event->offense_name),
EVENT_NAME_MAX, "%s", "<unknown>");
}
for (i = 0; i < g_ncpus; i++) {
/* LINTED - alignment */
n += *((uint64_t *)(data->dtada_percpu[i]));
}
event->total_count = n;
event++;
g_top_events++;
return (DTRACE_AGGWALK_NEXT);
}
int
pt_events_stat_prepare(void)
{
dtrace_prog_t *prog;
dtrace_proginfo_t info;
dtrace_optval_t statustime;
int err;
char *prog_ptr;
event = g_event_info;
if ((dtp = dtrace_open(DTRACE_VERSION, 0, &err)) == NULL) {
pt_error("cannot open dtrace library for the event report: "
"%s\n", dtrace_errmsg(NULL, err));
return (-1);
}
/*
* Execute different scripts (defined in the platform specific file)
* depending on user specified options.
*/
if (PT_ON_VERBOSE) {
prog_ptr = (char *)g_dtp_events_v;
} else {
if (PT_ON_CPU)
prog_ptr = (char *)g_dtp_events_c;
else
prog_ptr = (char *)g_dtp_events;
}
if ((prog = dtrace_program_strcompile(dtp, prog_ptr,
DTRACE_PROBESPEC_NAME, 0, g_argc, g_argv)) == NULL) {
pt_error("failed to compile the event report program\n");
return (dtrace_errno(dtp));
}
if (dtrace_program_exec(dtp, prog, &info) == -1) {
pt_error("failed to enable probes for the event report\n");
return (dtrace_errno(dtp));
}
if (dtrace_setopt(dtp, "aggsize", "128k") == -1) {
pt_error("failed to set 'aggsize' for the event report\n");
return (dtrace_errno(dtp));
}
if (dtrace_setopt(dtp, "aggrate", "0") == -1) {
pt_error("failed to set 'aggrate' for the event report\n");
return (dtrace_errno(dtp));
}
if (dtrace_setopt(dtp, "aggpercpu", 0) == -1) {
pt_error("failed to set 'aggpercpu' for the event report\n");
return (dtrace_errno(dtp));
}
if (dtrace_go(dtp) != 0) {
pt_error("failed to start the event report observation\n");
return (dtrace_errno(dtp));
}
if (dtrace_getopt(dtp, "statusrate", &statustime) == -1) {
pt_error("failed to get 'statusrate' for the event report\n");
return (dtrace_errno(dtp));
}
return (0);
}
int
pt_events_stat_collect(void)
{
g_top_events = 0;
event = g_event_info;
if (dtrace_status(dtp) == -1)
return (-1);
if (dtrace_aggregate_snap(dtp) != 0)
pt_error("failed to collect data for the event report\n");
if (dtrace_aggregate_walk_keyvarsorted(dtp, pt_events_walk, NULL) != 0)
pt_error("failed to sort data for the event report\n");
dtrace_aggregate_clear(dtp);
return (0);
}
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <getopt.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <string.h>
#include <ctype.h>
#include <poll.h>
#include "powertop.h"
/*
* Global variables, see powertop.h for comments and extern declarations.
* These are ordered by type, grouped by usage.
*/
int g_bit_depth;
int g_total_events, g_top_events;
int g_npstates, g_max_cstate, g_longest_cstate;
uint_t g_features;
uint_t g_ncpus;
uint_t g_ncpus_observed;
processorid_t *g_cpu_table;
double g_interval_length;
hrtime_t g_total_c_time;
uchar_t g_op_mode;
boolean_t g_gui;
uint_t g_observed_cpu;
event_info_t g_event_info[EVENT_NUM_MAX];
state_info_t g_cstate_info[NSTATES];
freq_state_info_t g_pstate_info[NSTATES];
cpu_power_info_t *g_cpu_power_states;
boolean_t g_sig_resize;
uint_t g_argc;
char **g_argv;
static const int true = 1;
void
pt_sig_handler(int sig)
{
switch (sig) {
case SIGWINCH:
g_sig_resize = B_TRUE;
break;
}
}
int
main(int argc, char **argv)
{
double interval, interval_usr;
hrtime_t interval_start;
int index2 = 0, c, dump_count = 0;
char *endptr, key;
boolean_t root_user = B_FALSE;
struct pollfd pollset;
static struct option opts[] = {
{ "dump", 1, NULL, 'd' },
{ "time", 1, NULL, 't' },
{ "help", 0, NULL, 'h' },
{ "cpu", 1, NULL, 'c' },
{ "verbose", 0, NULL, 'v' },
{ 0, 0, NULL, 0 }
};
pt_set_progname(argv[0]);
/*
* Enumerate the system's CPUs, populate cpu_table, g_ncpus
*/
if ((g_ncpus = g_ncpus_observed = pt_enumerate_cpus()) == 0)
exit(EXIT_FAILURE);
if ((g_bit_depth = pt_get_bit_depth()) < 0)
exit(EXIT_FAILURE);
g_features = 0;
interval = interval_usr = INTERVAL_DEFAULT;
g_op_mode = PT_MODE_DEFAULT;
g_max_cstate = 0;
g_argv = NULL;
g_argc = 0;
g_observed_cpu = 0;
g_turbo_supported = B_FALSE;
g_sig_resize = B_FALSE;
g_curr_sugg = NULL;
while ((c = getopt_long(argc, argv, "d:t:hvc:", opts, &index2))
!= EOF) {
if (c == -1)
break;
switch (c) {
case 'd':
if (PT_ON_DUMP) {
pt_usage();
exit(EXIT_USAGE);
}
g_op_mode |= PT_MODE_DUMP;
g_gui = B_FALSE;
dump_count = (int)strtod(optarg, &endptr);
if (dump_count <= 0 || *endptr != '\0') {
pt_usage();
exit(EXIT_USAGE);
}
break;
case 't':
if (PT_ON_TIME) {
pt_usage();
exit(EXIT_USAGE);
}
g_op_mode |= PT_MODE_TIME;
interval = interval_usr = (double)strtod(optarg,
&endptr);
if (*endptr != '\0' || interval < 1 ||
interval > INTERVAL_MAX) {
pt_usage();
exit(EXIT_USAGE);
}
break;
case 'v':
if (PT_ON_CPU || PT_ON_VERBOSE) {
pt_usage();
exit(EXIT_USAGE);
}
g_op_mode |= PT_MODE_VERBOSE;
break;
case 'c':
if (PT_ON_CPU || PT_ON_VERBOSE) {
pt_usage();
exit(EXIT_USAGE);
}
g_op_mode |= PT_MODE_CPU;
g_observed_cpu = (uint_t)strtod(optarg, &endptr);
if (g_observed_cpu >= g_ncpus) {
pt_usage();
exit(EXIT_USAGE);
}
g_argc = 1;
g_ncpus_observed = 1;
if ((g_argv = malloc(sizeof (char *))) == NULL)
return (EXIT_FAILURE);
if ((*g_argv = malloc(sizeof (char) * 5)) == NULL)
return (EXIT_FAILURE);
(void) snprintf(*g_argv, 5, "%d\0", g_observed_cpu);
break;
case 'h':
pt_usage();
exit(EXIT_SUCCESS);
default:
pt_usage();
exit(EXIT_USAGE);
}
}
if (optind < argc) {
pt_usage();
exit(EXIT_USAGE);
}
(void) printf("%s %s\n\n", TITLE, COPYRIGHT_INTEL);
(void) printf("Collecting data for %.2f second(s) \n",
(float)interval);
/* Prepare P-state statistics */
if (pt_cpufreq_stat_prepare() == 0)
g_features |= FEATURE_PSTATE;
/* Prepare C-state statistics */
if (pt_cpuidle_stat_prepare() == 0)
g_features |= FEATURE_CSTATE;
else
/*
* PowerTop was unable to run a DTrace program,
* most likely for lack of permissions.
*/
exit(EXIT_FAILURE);
/* Prepare event statistics */
if (pt_events_stat_prepare() != -1)
g_features |= FEATURE_EVENTS;
/*
* If the system is running on battery, find out what's
* the kstat module for it
*/
pt_battery_mod_lookup();
/* Prepare turbo statistics */
if (pt_turbo_stat_prepare() == 0)
g_features |= FEATURE_TURBO;
/*
* Initialize the display.
*/
if (!PT_ON_DUMP) {
pt_display_init_curses();
pt_display_setup(B_FALSE);
(void) signal(SIGWINCH, pt_sig_handler);
pt_display_title_bar();
pt_display_status_bar();
g_gui = B_TRUE;
pollset.fd = STDIN_FILENO;
pollset.events = POLLIN;
}
/*
* Installs the initial suggestions, running as root and turning CPU
* power management ON.
*/
if (geteuid() != 0) {
pt_sugg_as_root();
} else {
root_user = B_TRUE;
pt_cpufreq_suggest();
}
while (true) {
key = 0;
if (g_sig_resize)
pt_display_resize();
interval_start = gethrtime();
if (!PT_ON_DUMP) {
if (poll(&pollset, (nfds_t)1,
(int)(interval * MILLISEC)) > 0)
(void) read(STDIN_FILENO, &key, 1);
} else {
(void) sleep((int)interval);
}
g_interval_length = (double)(gethrtime() - interval_start)
/NANOSEC;
g_top_events = 0;
g_total_events = 0;
(void) memset(g_event_info, 0,
EVENT_NUM_MAX * sizeof (event_info_t));
(void) memset(g_cstate_info, 0,
NSTATES * sizeof (state_info_t));
/* Collect idle state transition stats */
if (g_features & FEATURE_CSTATE &&
pt_cpuidle_stat_collect(g_interval_length) < 0) {
/* Reinitialize C-state statistics */
if (pt_cpuidle_stat_prepare() != 0)
exit(EXIT_FAILURE);
continue;
}
/* Collect frequency change stats */
if (g_features & FEATURE_PSTATE &&
pt_cpufreq_stat_collect(g_interval_length) < 0) {
/* Reinitialize P-state statistics */
if (pt_cpufreq_stat_prepare() != 0)
exit(EXIT_FAILURE);
continue;
}
/* Collect event statistics */
if (g_features & FEATURE_EVENTS &&
pt_events_stat_collect() < 0) {
/* Reinitialize event statistics */
if (pt_events_stat_prepare() != 0)
exit(EXIT_FAILURE);
continue;
}
/* Collect turbo statistics */
if (g_features & FEATURE_TURBO &&
pt_turbo_stat_collect() < 0)
exit(EXIT_FAILURE);
/* Show CPU power states */
pt_display_states();
/* Show wakeups events affecting PM */
if (g_features & FEATURE_EVENTS) {
pt_display_wakeups(g_interval_length);
pt_display_events(g_interval_length);
}
pt_battery_print();
if (key && !PT_ON_DUMP) {
switch (toupper(key)) {
case 'Q':
exit(EXIT_SUCCESS);
break;
case 'R':
interval = 3;
break;
}
/*
* Check if the user has activated the current
* suggestion.
*/
if (g_curr_sugg != NULL &&
toupper(key) == g_curr_sugg->key &&
g_curr_sugg->func)
g_curr_sugg->func();
}
if (dump_count)
dump_count--;
/* Exits if user requested a dump */
if (PT_ON_DUMP && !dump_count)
exit(EXIT_SUCCESS);
/* No key pressed, will suggest something */
if (!key && !dump_count)
pt_sugg_pick();
/* Refresh display */
if (!PT_ON_DUMP)
pt_display_update();
if (root_user)
pt_cpufreq_suggest();
/*
* Update the interval based on how long the CPU was in the
* longest c-state during the last snapshot. If the user
* specified an interval we skip this bit and keep it fixed.
*/
if (g_features & FEATURE_CSTATE && !PT_ON_TIME &&
g_longest_cstate > 0 &&
g_cstate_info[g_longest_cstate].events > 0) {
double deep_idle_res = (((double)
g_cstate_info[g_longest_cstate].total_time/MICROSEC
/g_ncpus)/g_cstate_info[g_longest_cstate].events);
if (deep_idle_res < INTERVAL_DEFAULT ||
(g_total_events/interval) < 1)
interval = INTERVAL_DEFAULT;
else
interval = INTERVAL_UPDATE(deep_idle_res);
} else {
/*
* Restore interval after a refresh.
*/
if (key)
interval = interval_usr;
}
}
return (EXIT_SUCCESS);
}
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#ifndef __INCLUDE_GUARD_POWERTOP_H_
#define __INCLUDE_GUARD_POWERTOP_H_
#include <sys/types.h>
#include <sys/processor.h>
#define max(A, B) (((A) < (B)) ? (B) : (A))
#define TITLE "OpenSolaris PowerTOP version 1.2"
#define COPYRIGHT_INTEL "(C) 2009 Intel Corporation"
/*
* Exit values. stdlib.h defines EXIT_SUCCESS as 0 and
* EXIT_FAILURE as 1
*/
#define EXIT_USAGE 2
/*
* PowerTOP Features
* These may not be available everywhere
*/
#define FEATURE_CSTATE 0x01
#define FEATURE_PSTATE 0x02
#define FEATURE_EVENTS 0x04
#define FEATURE_TURBO 0x08
#define BIT_DEPTH_BUF 10
#define INTERVAL_DEFAULT 5.0
#define INTERVAL_MAX 30.0
#define INTERVAL_UPDATE(l) \
((l/INTERVAL_DEFAULT) * INTERVAL_DEFAULT + INTERVAL_DEFAULT)
#define STATE_NAME_MAX 16
#define EVENT_NAME_MAX 64
#define EVENT_NUM_MAX 100
#define NSTATES 32
/*
* Available op modes. The PT_ON_* macros allow for a simple way of checking
* under which mode PowerTOP is operating.
*/
#define PT_MODE_DEFAULT 0x01
#define PT_MODE_DUMP 0x02
#define PT_MODE_VERBOSE 0x04
#define PT_MODE_CPU 0x08
#define PT_MODE_TIME 0x10
#define PT_ON_DEFAULT (g_op_mode & PT_MODE_DEFAULT)
#define PT_ON_DUMP (g_op_mode & PT_MODE_DUMP)
#define PT_ON_VERBOSE (g_op_mode & PT_MODE_VERBOSE)
#define PT_ON_CPU (g_op_mode & PT_MODE_CPU)
#define PT_ON_TIME (g_op_mode & PT_MODE_TIME)
/*
* Structures and typedefs
*/
struct line {
char *string;
int count;
};
typedef struct event_info {
char offender_name[EVENT_NAME_MAX];
char offense_name[EVENT_NAME_MAX];
uint64_t total_count;
} event_info_t;
/*
* P/C state information
*/
typedef struct state_info {
char name[STATE_NAME_MAX];
hrtime_t total_time;
hrtime_t last_time;
uint64_t events;
} state_info_t;
typedef struct freq_state_info {
uint64_t speed;
hrtime_t total_time;
} freq_state_info_t;
typedef struct cpu_power_info {
uint64_t current_pstate;
uint64_t speed_accounted;
hrtime_t time_accounted;
hrtime_t dtrace_time;
} cpu_power_info_t;
/*
* Turbo mode information
*/
typedef struct turbo_info {
uint64_t t_mcnt;
uint64_t t_acnt;
} turbo_info_t;
/*
* Suggestions
*/
typedef void (sugg_func_t)(void);
typedef struct suggestion {
char *text;
char key;
char *sb_msg;
int weight;
int slice;
sugg_func_t *func;
struct suggestion *prev;
struct suggestion *next;
} sugg_t;
extern int g_bit_depth;
/*
* Event accounting
*/
extern int g_total_events;
extern int g_top_events;
/*
* Command line arguments
*/
extern uchar_t g_op_mode;
extern uint_t g_observed_cpu;
extern boolean_t g_gui;
/*
* Event info array
*/
extern event_info_t g_event_info[EVENT_NUM_MAX];
/*
* Lookup table, sequential CPU id to Solaris CPU id
*/
extern processorid_t *g_cpu_table;
/*
* Number of idle/frequency states
*/
extern int g_npstates;
extern int g_max_cstate;
extern int g_longest_cstate;
/*
* Total time, used to display different idle states
*/
extern hrtime_t g_total_c_time;
/*
* Current interval length
*/
extern double g_interval_length;
/*
* P/C state info arrays
*/
extern state_info_t g_cstate_info[NSTATES];
extern freq_state_info_t g_pstate_info[NSTATES];
extern uint_t g_features;
extern uint_t g_ncpus;
extern uint_t g_ncpus_observed;
extern cpu_power_info_t *g_cpu_power_states;
/*
* Turbo mode related information
*/
extern boolean_t g_turbo_supported;
extern double g_turbo_ratio;
extern sugg_t *g_curr_sugg;
/*
* DTrace scripts for the events report
*/
extern const char *g_dtp_events;
extern const char *g_dtp_events_v;
extern const char *g_dtp_events_c;
/*
* Arguments for dtrace_program_strcompile(). Contents vary according to
* the specified operation mode.
*/
extern uint_t g_argc;
extern char **g_argv;
/*
* Platform specific messages
*/
extern const char *g_msg_idle_state;
extern const char *g_msg_freq_state;
extern const char *g_msg_freq_enable;
/*
* Flags for signal handling
*/
extern boolean_t g_sig_resize;
extern void pt_sig_handler(int);
/*
* Suggestions related
*/
extern void pt_cpufreq_suggest(void);
extern void pt_sugg_as_root(void);
/*
* See util.c
*/
extern void pt_error(char *, ...);
extern void pt_set_progname(char *);
extern uint_t pt_enumerate_cpus(void);
extern void pt_usage(void);
extern int pt_get_bit_depth(void);
extern void pt_battery_mod_lookup(void);
extern int pt_event_compare(const void *, const void *);
/*
* Display/curses related
*/
extern void pt_display_setup(boolean_t);
extern void pt_display_init_curses(void);
extern void pt_display_update(void);
extern void pt_display_title_bar(void);
extern void pt_display_status_bar(void);
extern void pt_display_mod_status_bar(char *);
extern void pt_display_states(void);
extern void pt_display_acpi_power(uint32_t, double, double, double,
uint32_t);
extern void pt_display_wakeups(double);
extern void pt_display_events(double);
extern void pt_display_suggestions(char *);
extern void pt_display_resize(void);
/*
* Suggestions
*/
extern void pt_sugg_add(char *, int, char, char *, sugg_func_t *);
extern int pt_sugg_remove(sugg_func_t *);
extern void pt_sugg_pick(void);
/*
* Battery
*/
extern void pt_battery_print(void);
/*
* DTrace stats
*/
extern int pt_cpufreq_stat_prepare(void);
extern int pt_cpufreq_stat_collect(double);
extern int pt_cpuidle_stat_prepare(void);
extern int pt_cpuidle_stat_collect(double);
extern int pt_events_stat_prepare(void);
extern int pt_events_stat_collect(void);
/*
* Turbo mode related routines
*/
extern int pt_turbo_stat_prepare(void);
extern int pt_turbo_stat_collect(void);
#endif /* __INCLUDE_GUARD_POWERTOP_H_ */
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "powertop.h"
/*
* Default number of intervals we display a suggestion before moving
* to the next.
*/
#define PT_SUGG_DEF_SLICE 3
/*
* Global pointer to the current suggestion.
*/
sugg_t *g_curr_sugg;
/*
* Head of the list of suggestions.
*/
static sugg_t *sugg;
/*
* Add a new suggestion. Only one suggestion per text allowed.
*/
void
pt_sugg_add(char *text, int weight, char key, char *sb_msg, sugg_func_t *func)
{
sugg_t *new, *n, *pos = NULL;
/*
* Text is a required field for suggestions
*/
if (text == NULL)
return;
if (sugg == NULL) {
/*
* Creating first element
*/
if ((new = calloc(1, sizeof (sugg_t))) == NULL)
return;
if (sb_msg != NULL)
new->sb_msg = strdup(sb_msg);
if (text != NULL)
new->text = strdup(text);
new->weight = weight;
new->key = key;
new->func = func;
new->slice = 0;
sugg = new;
new->prev = NULL;
new->next = NULL;
} else {
for (n = sugg; n != NULL; n = n->next) {
if (strcmp(n->text, text) == 0)
return;
if (weight > n->weight && pos == NULL)
pos = n;
}
/*
* Create a new element
*/
if ((new = calloc(1, sizeof (sugg_t))) == NULL)
return;
if (sb_msg != NULL)
new->sb_msg = strdup(sb_msg);
new->text = strdup(text);
new->weight = weight;
new->key = key;
new->func = func;
new->slice = 0;
if (pos == NULL) {
/*
* Ordering placed the new element at the end
*/
for (n = sugg; n->next != NULL; n = n->next)
;
n->next = new;
new->prev = n;
new->next = NULL;
} else {
if (pos == sugg) {
/*
* Ordering placed the new element at the start
*/
new->next = sugg;
new->prev = sugg;
sugg->prev = new;
sugg = new;
} else {
/*
* Ordering placed the new element somewhere in
* the middle
*/
new->next = pos;
new->prev = pos->prev;
pos->prev->next = new;
pos->prev = new;
}
}
}
}
/*
* Removes a suggestion, returning 0 if not found and 1 if so.
*/
int
pt_sugg_remove(sugg_func_t *func)
{
sugg_t *n;
int ret = 0;
for (n = sugg; n != NULL; n = n->next) {
if (n->func == func) {
/* Removing the first element */
if (n == sugg) {
if (sugg->next == NULL) {
/* Removing the only element */
sugg = NULL;
} else {
sugg = n->next;
sugg->prev = NULL;
}
} else {
if (n->next == NULL) {
/* Removing the last element */
n->prev->next = NULL;
} else {
/* Removing an intermediate element */
n->prev->next = n->next;
n->next->prev = n->prev;
}
}
/*
* If this suggestions is currently being suggested,
* remove it and update the screen.
*/
if (n == g_curr_sugg) {
if (n->sb_msg != NULL) {
pt_display_mod_status_bar(n->sb_msg);
pt_display_status_bar();
}
if (n->text != NULL)
pt_display_suggestions(NULL);
}
free(n);
ret = 1;
}
}
return (ret);
}
/*
* Chose a suggestion to display. The list of suggestions is ordered by weight,
* so we only worry about fariness here. Each suggestion, starting with the
* first (the 'heaviest') is displayed during PT_SUGG_DEF_SLICE intervals.
*/
void
pt_sugg_pick(void)
{
sugg_t *n;
if (sugg == NULL) {
g_curr_sugg = NULL;
return;
}
search:
for (n = sugg; n != NULL; n = n->next) {
if (n->slice++ < PT_SUGG_DEF_SLICE) {
/*
* Don't need to re-suggest the current suggestion.
*/
if (g_curr_sugg == n && !g_sig_resize)
return;
/*
* Remove the current suggestion from screen.
*/
if (g_curr_sugg != NULL) {
if (g_curr_sugg->sb_msg != NULL) {
pt_display_mod_status_bar(
g_curr_sugg->sb_msg);
pt_display_status_bar();
}
if (g_curr_sugg->text != NULL)
pt_display_suggestions(NULL);
}
if (n->sb_msg != NULL) {
pt_display_mod_status_bar(n->sb_msg);
pt_display_status_bar();
}
pt_display_suggestions(n->text);
g_curr_sugg = n;
return;
}
}
/*
* All suggestions have run out of slice quotas, so we restart.
*/
for (n = sugg; n != NULL; n = n->next)
n->slice = 0;
goto search;
}
void
pt_sugg_as_root(void)
{
pt_sugg_add("Suggestion: run as root to get suggestions"
" for reducing system power consumption", 40, 0, NULL,
NULL);
}
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <stdlib.h>
#include <string.h>
#include <dtrace.h>
#include <kstat.h>
#include <errno.h>
#include "powertop.h"
/*
* Global turbo related variables definitions
*/
boolean_t g_turbo_supported;
double g_turbo_ratio;
/*
* The variables to store kstat snapshot
*/
static turbo_info_t *cpu_turbo_info = NULL;
static turbo_info_t *t_new = NULL;
/*
* Perform setup necessary to enumerate and track CPU turbo information
*/
static int
pt_turbo_init(void)
{
kstat_ctl_t *kc;
kstat_t *ksp;
kstat_named_t *knp;
/*
* check if the CPU turbo is supported
*/
if ((kc = kstat_open()) == NULL) {
g_turbo_supported = B_FALSE;
return (errno);
}
ksp = kstat_lookup(kc, "turbo", 0, NULL);
if (ksp == NULL) {
g_turbo_supported = B_FALSE;
(void) kstat_close(kc);
return (-1);
}
(void) kstat_read(kc, ksp, NULL);
knp = kstat_data_lookup(ksp, "turbo_supported");
if (knp == NULL) {
pt_error("couldn't find 'turbo_supported' kstat\n");
g_turbo_supported = B_FALSE;
(void) kstat_close(kc);
return (-2);
}
/*
* Initialize turbo information structure if turbo mode is supported
*/
if (knp->value.ui32) {
g_turbo_supported = B_TRUE;
cpu_turbo_info = calloc((size_t)g_ncpus, sizeof (turbo_info_t));
t_new = calloc((size_t)g_ncpus, sizeof (turbo_info_t));
}
(void) kstat_close(kc);
return (0);
}
/*
* Take a snapshot of each CPU's turbo information
* by looking through the turbo kstats.
*/
static int
pt_turbo_snapshot(turbo_info_t *turbo_snapshot)
{
kstat_ctl_t *kc;
kstat_t *ksp;
kstat_named_t *knp;
int cpu;
turbo_info_t *turbo_info;
if ((kc = kstat_open()) == NULL)
return (errno);
for (cpu = 0; cpu < g_ncpus; cpu++) {
turbo_info = &turbo_snapshot[cpu];
ksp = kstat_lookup(kc, "turbo", g_cpu_table[cpu], NULL);
if (ksp == NULL) {
pt_error("couldn't find 'turbo' kstat for CPU %d\n",
cpu);
(void) kstat_close(kc);
return (-1);
}
if (kstat_read(kc, ksp, NULL) == -1) {
pt_error("couldn't read 'turbo' kstat for CPU %d\n",
cpu);
(void) kstat_close(kc);
return (-2);
}
knp = kstat_data_lookup(ksp, "turbo_mcnt");
if (knp == NULL) {
pt_error("couldn't find 'turbo_mcnt' kstat for CPU "
"%d\n", cpu);
(void) kstat_close(kc);
return (-3);
}
/*
* snapshot IA32_MPERF_MSR
*/
turbo_info->t_mcnt = knp->value.ui64;
knp = kstat_data_lookup(ksp, "turbo_acnt");
if (knp == NULL) {
pt_error("couldn't find 'turbo_acnt' kstat for CPU "
"%d\n", cpu);
(void) kstat_close(kc);
return (-4);
}
/*
* snapshot IA32_APERF_MSR
*/
turbo_info->t_acnt = knp->value.ui64;
}
if (kstat_close(kc) != 0)
pt_error("couldn't close 'turbo' kstat\n");
return (0);
}
/*
* Turbo support checking and information initialization
*/
int
pt_turbo_stat_prepare(void)
{
int ret = 0;
if ((ret = pt_turbo_init()) != 0)
return (ret);
if ((ret = pt_turbo_snapshot(cpu_turbo_info)) != 0)
pt_error("failed to snapshot 'turbo' kstat\n");
return (ret);
}
/*
* When doing the statistics collection, we compare two kstat snapshot
* and get a delta. the final ratio of performance boost will be worked
* out according to the kstat delta
*/
int
pt_turbo_stat_collect(void)
{
int cpu;
uint64_t delta_mcnt, delta_acnt;
double ratio;
int ret;
/*
* Take a snapshot of turbo information to setup turbo_info_t
* structure
*/
if ((ret = pt_turbo_snapshot(t_new)) != 0) {
pt_error("failed to snapshot 'turbo' kstat\n");
return (ret);
}
/*
* Calculate the kstat delta and work out the performance boost ratio
*/
for (cpu = 0; cpu < g_ncpus; cpu++) {
delta_mcnt = t_new[cpu].t_mcnt - cpu_turbo_info[cpu].t_mcnt;
delta_acnt = t_new[cpu].t_acnt - cpu_turbo_info[cpu].t_acnt;
if ((delta_mcnt > delta_acnt) || (delta_mcnt == 0))
ratio = 1.0;
else
ratio = (double)delta_acnt / (double)delta_mcnt;
g_turbo_ratio += ratio;
}
g_turbo_ratio = g_turbo_ratio / (double)g_ncpus;
/*
* Update the structure of the kstat for the next time calculation
*/
(void) memcpy(cpu_turbo_info, t_new, g_ncpus * (sizeof (turbo_info_t)));
return (0);
}
/*
* Copyright 2009, Intel Corporation
* Copyright 2009, Sun Microsystems, Inc
*
* This file is part of PowerTOP
*
* This program file is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; version 2 of the License.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program in a file named COPYING; if not, write to the
* Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor,
* Boston, MA 02110-1301 USA
*
* Authors:
* Arjan van de Ven <arjan@linux.intel.com>
* Eric C Saxe <eric.saxe@sun.com>
* Aubrey Li <aubrey.li@intel.com>
*/
/*
* GPL Disclaimer
*
* For the avoidance of doubt, except that if any license choice other
* than GPL or LGPL is available it will apply instead, Sun elects to
* use only the General Public License version 2 (GPLv2) at this time
* for any software where a choice of GPL license versions is made
* available with the language indicating that GPLv2 or any later
* version may be used, or where a choice of which version of the GPL
* is applied is otherwise unspecified.
*/
#include <stdarg.h>
#include <stdlib.h>
#include <libgen.h>
#include <unistd.h>
#include <strings.h>
#include <sys/systeminfo.h>
#include <kstat.h>
#include <errno.h>
#include "powertop.h"
static char PROG_FMT[] = "%s: ";
static char ERR_FMT[] = ": %s";
static char *progname;
void
pt_set_progname(char *name)
{
progname = basename(name);
}
/*PRINTFLIKE1*/
void
pt_error(char *format, ...)
{
int err = errno;
va_list alist;
if (g_gui)
return;
if (progname != NULL)
(void) fprintf(stderr, PROG_FMT, progname);
va_start(alist, format);
(void) vfprintf(stderr, format, alist);
va_end(alist);
if (strchr(format, '\n') == NULL)
(void) fprintf(stderr, ERR_FMT, strerror(err));
}
/*
* Returns the number of online CPUs.
*/
uint_t
pt_enumerate_cpus(void)
{
int cpuid;
int max, cpus_conf;
uint_t ncpus = 0;
max = sysconf(_SC_CPUID_MAX);
cpus_conf = sysconf(_SC_NPROCESSORS_CONF);
/* Fall back to one CPU if any of the sysconf calls above failed */
if (max == -1 || cpus_conf == -1) {
max = cpus_conf = 1;
}
if ((g_cpu_table = malloc(cpus_conf * sizeof (processorid_t))) == NULL)
return (0);
for (cpuid = 0; cpuid < max; cpuid++) {
if (p_online(cpuid, P_STATUS) != -1) {
g_cpu_table[ncpus] = cpuid;
ncpus++;
}
}
return (ncpus);
}
void
pt_usage(void)
{
(void) fprintf(stderr, "%s %s\n\n", TITLE, COPYRIGHT_INTEL);
(void) fprintf(stderr, "usage: powertop [option]\n");
(void) fprintf(stderr, " -d, --dump [count] Read wakeups count "
"times and print list of top offenders\n");
(void) fprintf(stderr, " -t, --time [interval] Default time to gather "
"data in seconds [1-30s]\n");
(void) fprintf(stderr, " -v, --verbose Verbose mode, reports "
"kernel cyclic activity\n");
(void) fprintf(stderr, " -c, --cpu [CPU] Only observe a specific"
" CPU\n");
(void) fprintf(stderr, " -h, --help Show this help "
"message\n");
}
int
pt_get_bit_depth(void)
{
/*
* This little routine was derived from isainfo.c to look up
* the system's bit depth. It feeds a 10 byte long buffer to
* sysinfo (we only need the first word, sysinfo truncates and
* \0 terminates the rest) from which we figure out which isa
* we're running on.
*/
char buf[BIT_DEPTH_BUF];
if (sysinfo(SI_ARCHITECTURE_64, buf, BIT_DEPTH_BUF) == -1)
if (sysinfo(SI_ARCHITECTURE_32, buf, BIT_DEPTH_BUF) == -1)
return (-2);
if (strcmp(buf, "sparc") == 0 || strcmp(buf, "i386") == 0)
return (32);
if (strcmp(buf, "sparcv9") == 0 || strcmp(buf, "amd64") == 0)
return (64);
return (-3);
}
/*
* Simple integer comparison routine for the event report qsort(3C).
*/
int
pt_event_compare(const void *p1, const void *p2)
{
event_info_t i = *((event_info_t *)p1);
event_info_t j = *((event_info_t *)p2);
if (i.total_count > j.total_count)
return (-1);
if (i.total_count < j.total_count)
return (1);
return (0);
}
|