summaryrefslogtreecommitdiffstats
path: root/service/system/resource_manager/server/src/resm.cpp
blob: 6bed6bd19154487660679804e9f5c695dd7f212e (plain)
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
/*
 * @copyright Copyright (c) 2016-2020 TOYOTA MOTOR CORPORATION.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <pthread.h>
#include <ctype.h>
#include <sys/ioctl.h>
#include <net/if.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdint.h>
#include <errno.h>
#include <fcntl.h>
#include <sys/timerfd.h>
#include <sys/wait.h>
#include <sched.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <signal.h>

// NSFW
#include <native_service/frameworkunified_dispatcher.h>
#include <native_service/frameworkunified_framework_if.h>
#include <native_service/ns_version_if.h>
#include <system_service/ss_services.h>
#include <native_service/frameworkunified_application.h>
#include <system_service/ss_system_if.h>
#include <system_service/ss_version.h>
#include <native_service/cl_process.h>
#include <system_service/ss_system_process.h>
#include <other_service/rpc.h>
#include "resmgr_srvr.h"
#include "resm_internal.h"
#include "resmgr_api.h"
#include "ss_resm_resourcemanagerlog.h"
#include "resm_cfg.h"
#include "proc_watch.h"


// NSFW
CFrameworkunifiedVersion g_FrameworkunifiedVersion(MAJORNO, MINORNO, REVISION);
/**********************************************
 * Constant definitions
 **********************************************/
#define Resm_Flag_ID_Base EV_Flag_ID_Base(RESMGR_MID)
#define RESM_SUB_PRIORITY (0)

// #define WTC_CPU_INTERVAL (3)

#define CPULOAD_INVALID (-2)
#define CPULOAD_READY (-1)
#define CPULOG_TIMER_CLEAR (1)
#define CPULOG_LOGGING (0)
#define CPULOG_NO_LOGGING (-1)

#define PROCNET_DEV_FILE "/proc/net/dev"
#define MEMINFO_FILE "/proc/meminfo"
#define PROC_STAT_FILE "/proc/stat"

#define BYTE_TO_KIBIBYTE (1024)
#define KIBIBYTE_TO_BYTE (1024)
#define PERF_PNAME_MAX  128  // Max path name of process
#define PERF_PATH  "/usr/bin/perf"
#define PERF_FILE  "/tmp/perf"
#define PERF_DUMP  "/tmp/perf_dump"
#define PERF_REPORT_DELAY  1   // 1 sec
#define PERF_REPORT_RETRY  3   // retry 3 times
#define CPULOAD_NICEVAL   10   // value of nice() to lower priority

#define DEBUG_INFO_DIRPATH  "/tmp/diag_analysis"
#define DEBUG_INFO_FPATH DEBUG_INFO_DIRPATH"/dispinfo_resource.dbg"
#define DEBUG_INFO_TMPPATH DEBUG_INFO_DIRPATH"/dispinfo_resource.tmp"
#define DEBUG_INFO_MEM_LETTERS  (25)
#define DEBUG_INFO_CPU_TOP_LINES  (10)
#define DEBUG_INFO_CMA_MIN  (160000)  // in KB
#define DEBUG_INFO_CMA_LETTERS  (20)
#define DEBUG_INFO_DSP_PG_PATH  "/usr/bin/bs_analysis_dispinfo_debug"

#define DROP_CACHES_PG_PATH "/usr/bin/drop_caches"

#define READLINE_MAX_SIZE       512
#define READ_MAX_SIZE          4096
#define LF (0x0A)

/**********************************************
 * Structure definitions
 **********************************************/
// App session information
typedef struct {
  BOOL useFlag;
  RESM_REQ_EVENT_t reqEv;     // event request
} SSN_INFO_t;

// CPU usage information
typedef struct {
  char cpuname[8];
  int32_t user;
  int32_t nice;
  int32_t system;
  int32_t idle;
  int32_t iowait;
  int32_t irq;
  int32_t softirq;
  int32_t steal;
  int32_t guest;
  int32_t guest_nice;
} CPU_INFO_t;

//Context
typedef struct {
  char procName[128];
  HANDLE hApp;      // DispatcherHandle
  int32_t nsFd;     // For receiving from the NSFW

  // Session information
  SSN_INFO_t ssnInfo[EV_MAX_IDS_IN_THREAD];

  // Memory information
  uint32_t restMem;     // Remaining memory information
  BOOL restMemFlag;

  // CMA information
  uint32_t restCma;
  BOOL restCmaFlag;


  // CPU load information
//  int32_t cpuloadRate;
} SVC_COMMON_t;

// meminfo table
typedef struct {
  const char* name;
  uint32_t* value;
} meminfo_tbl;

/**********************************************
 * External variable definitions
 **********************************************/
// FRAMEWORKUNIFIEDLOG
FRAMEWORKUNIFIEDLOGPARAM g_FrameworkunifiedLogParams = {
  FRAMEWORKUNIFIEDLOGOPTIONS, {
    ZONE_TEXT_10, ZONE_TEXT_11, ZONE_TEXT_12,
    ZONE_TEXT_13, ZONE_TEXT_14, ZONE_TEXT_15,
    ZONE_TEXT_16, ZONE_TEXT_17, ZONE_TEXT_18,
    ZONE_TEXT_19, ZONE_TEXT_20, ZONE_TEXT_21,
    ZONE_TEXT_22, ZONE_TEXT_23, ZONE_TEXT_24,
    ZONE_TEXT_25, ZONE_TEXT_26, ZONE_TEXT_27,
    ZONE_TEXT_28, ZONE_TEXT_29, ZONE_TEXT_30,
    ZONE_TEXT_31
  }, FRAMEWORKUNIFIEDLOGZONES
};

int isNfs;                     // NFS env : 1

static SVC_COMMON_t g_resmgr;  // NOLINT (readability/nolint)
static int32_t g_sock = -1;
static uint32_t inactFile_kib;
static uint32_t mainFree_kib;
static uint32_t memTotal_kib;
static uint32_t cmaFree_kib;
static uint32_t cmaTotal_kib;
static uint32_t minRestMem;    // Minimum memory available
static uint32_t minRestCma;    // Minimum CMA available
static int32_t g_cpuloadRate1000;
static int g_cpu_num;
static CPU_INFO_t *g_cpuload_pre;
static CPU_INFO_t *g_cpuload;

static int32_t g_fifo_status = STATUS_IDOL;
static int32_t g_tss_status = STATUS_IDOL;
static int32_t g_fifo_timer = 0;
static int32_t g_tss_timer = 0;


/**********************************************
 * Local function definition
 **********************************************/
static void ctxCreate(SVC_COMMON_t* p_ctx, int32_t argc, char* argv[]);

// Session related
static int32_t get_new_id(uint32_t* ssnId);

// Memory monitoring
void watchMem(void);
static int32_t comp_meminfo_tbl(const void* a, const void* b);
static int32_t get_meminfo(void);

// Network monitoring
static int32_t getInetStatus(RESM_INET_STATUS_t* p_iStatus);
static char* get_aliasName(char* name, char* line);
static int32_t get_hwaddr(char* name, char* hwaddr);

// CPU monitoring

static void watchCPUStatus(void);
static void watchCPU(void);
static int32_t get_cpuload(CPU_INFO_t* p_cpu);
static int32_t calc_cpuloadRate(void);
static int32_t calc_cpuloadRate_each(int num);

// static int32_t chk_logging(int32_t cpuload, int32_t timer);
// static void logging_cpuload(void);

static void trim_end(char* buf);

// static void escape_percent(char* in, char* out);
static void init_cpuload(void);

// extern void logging_cpuload_custom(void);
static void exec_perf(int32_t t_pid);
extern unsigned long logging_cpuload_custom(int32_t tmode);


// Debug information output
void outputResouceInfo(void);
static int write_meminfo_work(FILE *wfp);
static int write_cpuinfo_work(FILE *wfp);
static int write_cmainfo_work(FILE *wfp);
static int write_resourcemanagerloginfo_work(void);

/*******************************************************************************
 * RPC public API
 *******************************************************************************/
/* Event issuance request */
RESM_ERR_t RESM_ReqEvent(uint32_t ssnId, const RESM_REQ_EVENT_t* p_reqEvent) {
  // Argument check
  if (p_reqEvent == NULL) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr ReqEvent Invalid Arg");
    return RESM_E_PAR;
  }

  if (ssnId >= EV_MAX_IDS_IN_THREAD) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr ReqEvent Invalid Arg");
    return RESM_E_PAR;
  }

  if (!g_resmgr.ssnInfo[ssnId].useFlag) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr ReqEvent Invalid Arg");
    return RESM_E_PAR;
  }

  // Event type check
  if (p_reqEvent->reqEvent == RESM_EV_MEM) {
    // Record event issuance request
    memcpy(&g_resmgr.ssnInfo[ssnId].reqEv, p_reqEvent,
           sizeof(RESM_REQ_EVENT_t));
  }

  return RESM_E_OK;
}

/* Get system status */
RESM_ERR_t RESM_GetStatus(uint32_t ssnId, RESM_STATUS_t* p_status) {
  int32_t ret;

  // Argument check
  if (p_status == NULL) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr GetStatus Invalid Arg");
    return RESM_E_PAR;
  }

  if (ssnId >= EV_MAX_IDS_IN_THREAD) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr GetStatus Invalid Arg");
    return RESM_E_PAR;
  }

  if (!g_resmgr.ssnInfo[ssnId].useFlag) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr GetStatus Invalid Arg");
    return RESM_E_PAR;
  }

  if (!g_resmgr.restMemFlag) {
    // No remaining memory information
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr GetStatus get restmem Error");  // LCOV_EXCL_BR_LINE 15:marco defined in "native_service/ns_logger_if.h"  // NOLINT[whitespace/line_length]
    return RESM_E_NG;
  }
  p_status->restMemSize = g_resmgr.restMem;
  p_status->nandWriteStatus = RESM_NAND_WRITE_ENABLE;   //[]Always possible

  ret = getInetStatus(&p_status->inetStatus);
  if (ret != 0) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
           "ResMgr GetStatus getInetStatus Error");
    return RESM_E_NG;
  }

  return RESM_E_OK;
}

/*******************************************************************************
 * Internal API
 *******************************************************************************/
/* Session connection */
RESM_ERR_t RESM_SV_Open(const RESM_RSV_t* p_prim, uint32_t* p_ssnId) {
  int32_t ret;

  ret = get_new_id(p_ssnId);
  if (ret != 0) {
    return RESM_E_NG;
  }
  // FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "ResMgr Open Session:ID=[%d]", *p_ssnId);

  return RESM_E_OK;
}

/* Session disconnection */
RESM_ERR_t RESM_SV_Close(uint32_t ssnId) {
  // Argument check
  if (ssnId >= EV_MAX_IDS_IN_THREAD) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr Close Invalid Arg");
    return RESM_E_PAR;
  }

  if (!g_resmgr.ssnInfo[ssnId].useFlag) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr Close Invalid Arg");
    return RESM_E_PAR;
  }

  // Turn off used flag
  g_resmgr.ssnInfo[ssnId].useFlag = FALSE;

  // Clear event request
  memset(&g_resmgr.ssnInfo[ssnId].reqEv, 0, sizeof(RESM_REQ_EVENT_t));

  return RESM_E_OK;
}

/* Session ID check */
RESM_ERR_t RESM_SV_ChkSsnId(uint32_t ssnId) {
  if (ssnId >= EV_MAX_IDS_IN_THREAD) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr ssnId[%d] over MAX", ssnId);
    return RESM_E_NG;
  }

  if (!g_resmgr.ssnInfo[ssnId].useFlag) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr ssnId[%d] is unidentified",
           ssnId);
    return RESM_E_NG;
  }

  return RESM_E_OK;
}

/**********************************************
 *  Initialization function
 **********************************************/
/* Context Initialization */
static void ctxCreate(SVC_COMMON_t* p_ctx, int32_t argc, char* argv[]) {
  EFrameworkunifiedStatus resourcemanagerRet;
  FrameworkunifiedDefaultCallbackHandler cbFuncs;
  FRAMEWORKUNIFIED_MAKE_DEFAULT_CALLBACK(cbFuncs);
  FRAMEWORKUNIFIED_SET_ZONES();

  memset(p_ctx, 0, sizeof(SVC_COMMON_t));
  strcpy(p_ctx->procName, SS_RESOURCE_MANAGER);  // NOLINT

  // Create a Dispatcher
  resourcemanagerRet = FrameworkunifiedCreateDispatcherWithoutLoop(p_ctx->procName, p_ctx->hApp, argc,
                                          argv, &cbFuncs, TRUE);
  if (resourcemanagerRet != eFrameworkunifiedStatusOK) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
    // LCOV_EXCL_START 4: NSFW error case.
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "[RESM_ERR]ResMgr Create Dispatcher Failed. ret[%d]", resourcemanagerRet);
    exit(EXIT_FAILURE);
    // LCOV_EXCL_STOP 4: NSFW error case.
  }

  resourcemanagerRet = FrameworkunifiedGetDispatcherFD(p_ctx->hApp, &p_ctx->nsFd);
  if (resourcemanagerRet != eFrameworkunifiedStatusOK) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
    // LCOV_EXCL_START 4: NSFW error case.
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "[RESM_ERR]ResMgr Get Dispatcher FD Failed. ret[%d]", resourcemanagerRet);
    exit(EXIT_FAILURE);
    // LCOV_EXCL_STOP 4: NSFW error case.
  }

  // Initialize session
  memset(p_ctx->ssnInfo, 0, sizeof(p_ctx->ssnInfo));


//  // Initialize CPU Information
//  g_resmgr.cpuloadRate = CPULOAD_INVALID;


  return;
}

/**********************************************
 *  Get FRAMEWORKUNIFIEDLOG BasePF Flag
 **********************************************/
static EFrameworkunifiedStatus resourcemanagerlog_output_debug_info;
#define RESOURCEMANAGERLOG_BASEPF_FLAG_ID (11)

EFrameworkunifiedStatus resourcemanagerlog_flag_check(UI_8 *mode) {
  if (resourcemanagerlog_output_debug_info != eFrameworkunifiedStatusOK)
    return eFrameworkunifiedStatusFail;

  return NsLogGetFrameworkunifiedLogFlag(RESOURCEMANAGERLOG_BASEPF_FLAG_ID, mode);
}

/* drop_caches Start update Task */
static void start_drop_caches(void) {
  pid_t pid = fork();
  if (pid == 0) {
    if (setuid(0) == -1) {  // LCOV_EXCL_BR_LINE 5: setuid's error case
      // LCOV_EXCL_START 5: setuid's error case
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to setuid(0)-1, errno=%d", errno);
      // LCOV_EXCL_STOP
    } else {
      struct sched_param param;
      // Exec drop_caches
      param.sched_priority = 0;
      if (sched_setscheduler(getpid(), SCHED_OTHER, &param) < 0) {  // LCOV_EXCL_BR_LINE 5: sched_setscheduler's error case
        // LCOV_EXCL_START 5: sched_setscheduler's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to sched_setscheduler errno=%d",
               errno);
        // LCOV_EXCL_STOP
      } else {
        execl(DROP_CACHES_PG_PATH, basename(DROP_CACHES_PG_PATH), NULL);
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to execl %s errno=%d",
               DROP_CACHES_PG_PATH,
               errno);
      }
    }
    exit(1);
  }
  if (pid == (pid_t) -1) {  // LCOV_EXCL_BR_LINE 5: fork's error case
    // LCOV_EXCL_START 5: fork's error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to fork=-1, errno=%d", errno);
    // LCOV_EXCL_STOP
  }
}

/**********************************************
 *  Main Function
 **********************************************/
int32_t main(int32_t argc, char* argv[]) {
  int32_t mainRet = -1;
  int32_t rpcFd;
  int32_t timerFd;
  struct itimerspec tm;
  int32_t timerFd2;
  struct itimerspec tm2;
  uint64_t exp;
  int32_t maxFd = 0;
  fd_set fds;
  struct sched_param prcwParam;
  pthread_t ptPRCW;
  int32_t sec = 0;
  int32_t ret;

  int32_t cpu_ret;
  bool fork_dsp_task = false;
  CL_ProcessAttr_t attr;
  const char *prName;
  const char *args[iProcess_MAXIMUM_NUMBER_OF_PROCESS_ARGUMENTS];
  UI_8 mode;
  EFrameworkunifiedStatus eRet;
  struct stat statbuf;

  RPC_ID rpcId = RESMGR_RPC_ID;
  SVC_COMMON_t* p_ctx = &g_resmgr;
  EFrameworkunifiedStatus resourcemanagerRet = eFrameworkunifiedStatusOK;

  {
    const char* nfsenv = getenv("AGL_NFS");
    isNfs = (nfsenv && strcmp(nfsenv, "y") == 0) ? 1 : 0;
  }

  /* Clear context */
  ctxCreate(p_ctx, argc, argv);

  /* Start debug information display task */
  {
    char *tmp;
    tmp = getenv("AGL_DEVDIAG_FLAG");
    if ((tmp == NULL) || strcmp(tmp, "ON")) {  // != "ON"
      resourcemanagerlog_output_debug_info = eFrameworkunifiedStatusFail;
    } else {
      resourcemanagerlog_output_debug_info = eFrameworkunifiedStatusOK;
    }
  }
  eRet = resourcemanagerlog_flag_check(&mode);
  if (eRet == eFrameworkunifiedStatusOK && mode == FRAMEWORKUNIFIEDLOG_FLAG_MODE_DEBUG) {
    fork_dsp_task = true;
  }
  if (fork_dsp_task) {
    // LCOV_EXCL_START 8: there is no bs_analysis_dispinfo_debug
    if (0 == stat(DEBUG_INFO_DSP_PG_PATH, &statbuf)) {
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      if (0 != CL_ProcessCreateAttrInit(&attr)) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "CL_ProcessCreateAttrInit Error");  // LCOV_EXCL_LINE 4: NSFW error case.
      }
      prName = basename(DEBUG_INFO_DSP_PG_PATH);
      if (0 != CL_ProcessCreateAttrSetName(&attr, prName)) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Set Name Error");  // LCOV_EXCL_LINE 4: NSFW error case.
      }
      args[0] = prName;
      args[1] = NULL;
      if (CL_ProcessCreate(DEBUG_INFO_DSP_PG_PATH, (char* const *) args, NULL,
                           &attr) < 0) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "CL_ProcessCreate Error");  // LCOV_EXCL_LINE 4: NSFW error case.
      }
    }
    // LCOV_EXCL_STOP 8: there is no bs_analysis_dispinfo_debug
  }

  /* Create socket */
  g_sock = socket(AF_INET, SOCK_DGRAM, 0);
  if (g_sock < 0) {  // LCOV_EXCL_BR_LINE 5: socket error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "[RESM_ERR]ResMgr Create Socket Failed");  // LCOV_EXCL_LINE 5: socket error case
  }

  FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "[%s:%d] ResMgr Wakeup", p_ctx->procName,
         getpid());

  RPC_START_SERVER(rpcId);    // Start RPC Server
  RPC_get_fd(rpcId, &rpcFd);      // Event reception FD of RPC

  /* Create proc_watch thread */
  ret = pthread_create(&ptPRCW, NULL, &PRCW_main, reinterpret_cast<void*>(NULL));
  if (ret != 0) {  // LCOV_EXCL_BR_LINE 5: pthread_create error case
    // LCOV_EXCL_START 5: pthread_create error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "[RESM_ERR]ResMgr Create Thread Failed. code[%d]", ret);
    // LCOV_EXCL_STOP 5: pthread_create error case
  } else {
    prcwParam.sched_priority = RESM_SUB_PRIORITY;
    ret = pthread_setschedparam(ptPRCW, SCHED_OTHER, &prcwParam);
    if (ret != 0) {  // LCOV_EXCL_BR_LINE 5: pthread_setschedparam error case
      // LCOV_EXCL_START 5: pthread_setschedparam error case
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "[RESM_ERR]ResMgr Set Thread Schedparam Failed. code[%d]", ret);
      // LCOV_EXCL_STOP 5: pthread_setschedparam error case
    }
  }

  /* Create timer */
  if ((timerFd = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC)) == -1) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "[RESM_ERR]ResMgr Create timerFd Failed. errno[%d]", errno);
    exit(EXIT_FAILURE);
  }
  // for drop_caches
  {
    if ((timerFd2 = timerfd_create(CLOCK_MONOTONIC, TFD_CLOEXEC)) == -1) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "[RESM_ERR]ResMgr Create timerFd2 Failed. errno[%d]", errno);
      exit(EXIT_FAILURE);
    }
  }

  // Initialize remaining memory acquisition
  mainFree_kib = 0;
  inactFile_kib = 0;
  memTotal_kib = 0;
  cmaFree_kib = 0;
  cmaTotal_kib = 0;
  get_meminfo();
  g_resmgr.restMem = mainFree_kib + inactFile_kib;
  minRestMem = g_resmgr.restMem;
  g_resmgr.restMemFlag = TRUE;
  g_resmgr.restCma = cmaFree_kib;
  minRestCma = g_resmgr.restCma;
  g_resmgr.restCmaFlag = TRUE;

  tm.it_value.tv_sec = MONITORING_START_DELAT_TIME;
  tm.it_value.tv_nsec = 0;
  tm.it_interval.tv_sec = 1;
  tm.it_interval.tv_nsec = 0;
  if (timerfd_settime(timerFd, 0, &tm, NULL) == -1) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "[RESM_ERR]ResMgr Set timerFd Failed. errno[%d]", errno);
    exit(EXIT_FAILURE);
  }

  tm2.it_value.tv_sec = DROP_CACHES_START_DELAT_TIME;
  tm2.it_value.tv_nsec = 0;
  tm2.it_interval.tv_sec = 0;
  tm2.it_interval.tv_nsec = 0;
  if (timerfd_settime(timerFd2, 0, &tm2, NULL) == -1) {  // LCOV_EXCL_BR_LINE 11:Gcov constraints (coverage measurement revision by DeathTest)
    // LCOV_EXCL_START 11:Gcov constraints (coverage measurement revision by DeathTest)
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "[RESM_ERR]ResMgr Set timerFd2 Failed. errno[%d]", errno);
    exit(EXIT_FAILURE);
    // LCOV_EXCL_END 11:Gcov constraints (coverage measurement revision by DeathTest)
  }

  /* API to Publish Service Availability Notification. */
  resourcemanagerRet = FrameworkunifiedPublishServiceAvailability(p_ctx->hApp, TRUE);
  if (eFrameworkunifiedStatusOK != resourcemanagerRet) {  // LCOV_EXCL_BR_LINE 4: NSFW error case.
    // LCOV_EXCL_START 4: NSFW error case.
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "Failed to Publish Service Availability Notification:0x%x", resourcemanagerRet);
    exit(EXIT_FAILURE);
    // LCOV_EXCL_STOP 4: NSFW error case.
  }


  /* CPU Load init */
  g_fifo_status = STATUS_CHECK_CPU;
  g_tss_status = STATUS_CHECK_CPU;
  g_cpuloadRate1000 = 0;
  init_cpuload();
  cpu_ret = get_cpuload(g_cpuload);
  if ( cpu_ret != 0 ) {  // LCOV_EXCL_BR_LINE 200: cpu_ret must be 0
    // LCOV_EXCL_START 200: cpu_ret must be 0
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "Get cpuload Failed");
    g_cpuloadRate1000 = 0;
    // LCOV_EXCL_STOP
  }


  while (1) {
    FD_ZERO(&fds);
    FD_SET(rpcFd, &fds);
    maxFd = MY_MAX(maxFd, rpcFd);

    FD_SET(p_ctx->nsFd, &fds);
    maxFd = MY_MAX(maxFd, p_ctx->nsFd);

    FD_SET(timerFd, &fds);
    maxFd = MY_MAX(maxFd, timerFd);

    if (timerFd2 != -1) {
      FD_SET(timerFd2, &fds);
      maxFd = MY_MAX(maxFd, timerFd2);
    }
    ret = select(maxFd + 1, &fds, NULL, NULL, NULL);
    if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: select error case
      // LCOV_EXCL_START 5: select error case
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      if (errno != EINTR) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "[RESM_ERR]ResMgr Handle Error");
        exit(EXIT_FAILURE);
      }
      continue;
      // LCOV_EXCL_STOP 5: select error case
    }
    /* API CALL from RPC */
    if ((timerFd2 != -1) && FD_ISSET(timerFd2, &fds)) {
      start_drop_caches();
      timerFd2 = -1;
    }

    /* API CALL from RPC */
    if (FD_ISSET(rpcFd, &fds)) {
      RPC_process_API_request(rpcId);
    }
    /* Events from the NSFW */
    if (FD_ISSET(p_ctx->nsFd, &fds)) {
      FrameworkunifiedDispatchProcessWithoutLoop(p_ctx->hApp);
    }

    /* Timer expiration */
    if (FD_ISSET(timerFd, &fds)) {
      read(timerFd, &exp, sizeof(uint64_t));
      /* CPU load monitoring */
      if ((sec % WTC_CPU_INTERVAL) == 0) {
        watchCPUStatus();
      }

      if (sec >= RESET_SEC) {
        sec = 0;
      }
      sec++;
    }
  }

  // Close socket
  close(g_sock);

  mainRet = 0;
  RPC_end(rpcId);

  return mainRet;
}

/*********************************************************************************
 * Session Related
 *********************************************************************************/
/* Numbering session ID */
static int32_t get_new_id(uint32_t* ssnId) {
  int32_t i;
  SSN_INFO_t* p_ssnInfo;

  for (i = 0; i < EV_MAX_IDS_IN_THREAD; i++) {
    p_ssnInfo = &g_resmgr.ssnInfo[i];

    if (p_ssnInfo->useFlag) {
      // in-use
      continue;
    }
    *ssnId = i;
    p_ssnInfo->useFlag = TRUE;

    return 0;
  }

  return -1;
}

/*********************************************************************************
 * Memory monitoring
 *********************************************************************************/
/* Memory monitoring */
void watchMem(void) {
  uint32_t restMem_b;
  SSN_INFO_t* p_ssnInfo;
  uint32_t ssnId;
  int32_t i;
  int32_t ret;

  // Get remaining memory
  mainFree_kib = 0;
  inactFile_kib = 0;
  memTotal_kib = 0;
  cmaFree_kib = 0;
  cmaTotal_kib = 0;
  g_resmgr.restMemFlag = FALSE;
  g_resmgr.restCmaFlag = FALSE;
  ret = get_meminfo();
  // KiB -> Byte
  // [Note] Unit of the value gotten by the meminfo is KiB
  restMem_b = (mainFree_kib + inactFile_kib) * KIBIBYTE_TO_BYTE;

  if (ret != 0) {
    // Failed to get remaining memory info
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr get_meminfo Failed");
    return;
  }
  g_resmgr.restMem = mainFree_kib + inactFile_kib;
  g_resmgr.restMemFlag = TRUE;
  g_resmgr.restCma = cmaFree_kib;
  g_resmgr.restCmaFlag = TRUE;

  if (g_resmgr.restMem < minRestMem) {
    // Update minimum available memory
    minRestMem = g_resmgr.restMem;
  }
  if (g_resmgr.restCma < minRestCma) {
    // Update minimum available CMA
    minRestCma = g_resmgr.restCma;
  }

  for (i = 0; i < RESM_INET_IF_MAX; i++) {
    p_ssnInfo = &g_resmgr.ssnInfo[i];
    if (!p_ssnInfo->useFlag) {
      // Unused sessions
      continue;
    }
    ssnId = i;

    // Check event type
    if (p_ssnInfo->reqEv.reqEvent != RESM_EV_MEM) {
      // Other than memory monitoring
      continue;
    }
    // Check free memory
    if (p_ssnInfo->reqEv.prm.restMemThresh > restMem_b) {
      // Issue event
      ret = EV_set_flag(Resm_Flag_ID_Base + ssnId, RESM_EV_MEM);
      if (ret != EV_OK) {  // LCOV_EXCL_BR_LINE 200: EV_set_flag will not return ng
        // LCOV_EXCL_START 200: EV_set_flag will not return ng
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
               "ResMgr EV_set_flag Failed. ssnId[%d]", ssnId);
        // LCOV_EXCL_STOP 200: EV_set_flag will not return ng
      }
    }
  }

  return;
}

void FlushMemInfo(void) {
  FRAMEWORKUNIFIEDLOG(
    ZONE_INFO,
    __FUNCTION__,
    "MEMORY Info(KB):<PEAK>REST(%d) [Memtotal(%d)] / CMA Info(KB):<PEAK>REST(%d) [CMAtotal(%d)]",
    minRestMem, memTotal_kib, minRestCma, cmaTotal_kib);
}

/* Compare memory information */
static int32_t comp_meminfo_tbl(const void* data1, const void* data2) {
  return strcmp(((const meminfo_tbl*) data1)->name,
                ((const meminfo_tbl*) data2)->name);
}

/* Get memory information */
static int32_t get_meminfo(void) {
  int32_t meminfo_fd = -1;
  char srch_name[16];
  char buf[2048];
  meminfo_tbl target = { srch_name, NULL };
  meminfo_tbl* found;
  char* head;
  char* tail;
  int32_t read_byte;
  /* Strings must be in ascending order when adding entries to this table (for bsearch) */
  static const meminfo_tbl mem_table[] = {
    { "CmaFree", &cmaFree_kib },
    { "CmaTotal", &cmaTotal_kib },
    { "Inactive(file)", &inactFile_kib },
    { "MemFree", &mainFree_kib },
    { "MemTotal", &memTotal_kib },
  };
  const int32_t mem_table_count = sizeof(mem_table) / sizeof(meminfo_tbl);

  if (meminfo_fd == -1) {
    meminfo_fd = open(MEMINFO_FILE, O_RDONLY);
    if (meminfo_fd == -1) {
      fflush(NULL);
      return -1;
    }
  }

  lseek(meminfo_fd, 0L, SEEK_SET);
  read_byte = read(meminfo_fd, buf, sizeof buf - 1);
  if (read_byte < 0) {  // LCOV_EXCL_BR_LINE 200: if file exist, it will not be -1
    // LCOV_EXCL_START 200: if file exist, it will not be -1
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    fflush(NULL);
    close(meminfo_fd);
    return -1;
    // LCOV_EXCL_STOP 200: if file exis, it will not be -1
  }
  buf[read_byte] = '\0';

  head = buf;
  while (1) {
    tail = strchr(head, ':');
    if (!tail) {
      break;
    }

    *tail = '\0';
    if (strlen(head) >= sizeof(srch_name)) {
      head = tail + 1;
    } else {
      strcpy(srch_name, head);  // NOLINT
      found = reinterpret_cast<meminfo_tbl *>(bsearch(&target, mem_table, mem_table_count,
                                      sizeof(meminfo_tbl), comp_meminfo_tbl));
      head = tail + 1;
      if (found) {
        *(found->value) = strtoul(head, &tail, 10);
      }
    }
    tail = strchr(head, '\n');
    if (!tail)
      break;
    head = tail + 1;
  }
  close(meminfo_fd);

  return 0;
}

/*********************************************************************************
 * Network monitoring
 *********************************************************************************/
/* Get system information */
static int32_t getInetStatus(RESM_INET_STATUS_t* p_iStatus) {
  FILE* fh;
  char buf[READLINE_MAX_SIZE];
  char* tmp;
  char name[IFNAMSIZ];
  char hwaddr[HWADDR_LEN];
  uint32_t rx_b;
  uint32_t rx_pckt;
  uint32_t rx_err;
  uint32_t rx_drp;
  uint32_t rx_fifo;
  uint32_t rx_frm;
  uint32_t rx_mlt;
  uint32_t tx_b;
  uint32_t tx_pckt;
  uint32_t tx_err;
  uint32_t tx_drp;
  uint32_t tx_fifo;
  uint32_t tx_cll;
  uint32_t tx_crr;
  int32_t ret;

  // Open file
  fh = fopen(PROCNET_DEV_FILE, "r");
  if (fh == NULL) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
           "ResMgr getInetStatus() File Open Error. errno[%d]", errno);
    return -1;
  }

  // Initialize interface count
  p_iStatus->ifNum = 0;

  while (fgets(buf, sizeof(buf), fh)) {
    // Get Alias name
    tmp = get_aliasName(name, buf);
    if (tmp == NULL) {
      // No alias name
      continue;
    }
    // Get amount of receive/transmit data
    ret = sscanf(tmp, "%u%u%u%u%u%u%u%*u%u%u%u%u%u%u%u", &rx_b, &rx_pckt,
                 &rx_err, &rx_drp, &rx_fifo, &rx_frm, &rx_mlt, &tx_b, &tx_pckt,
                 &tx_err, &tx_drp, &tx_fifo, &tx_cll, &tx_crr);
    if (ret != 14) {  // LCOV_EXCL_BR_LINE 200: ret is always 14
      // LCOV_EXCL_START 200: ret is always 14
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
             "ResMgr getInetStatus() GET Rx and Tx size Failed");
      continue;
      // LCOV_EXCL_STOP 200: ret is always 14
    }
    // Get hardware address
    ret = get_hwaddr(name, hwaddr);
    if (ret != 0) {  // LCOV_EXCL_BR_LINE 5: get_hwaddr will not return !0
      // LCOV_EXCL_START 5: get_hwaddr will not return !0
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
             "ResMgr getInetStatus() GET hwaddr Failed");
      continue;
      // LCOV_EXCL_STOP 5: get_hwaddr will not return !0
    }

    // Set values in the system information structure
    strcpy(p_iStatus->ifInfo[p_iStatus->ifNum].name, name);  // NOLINT
    p_iStatus->ifInfo[p_iStatus->ifNum].rxSize = rx_b / BYTE_TO_KIBIBYTE;
    p_iStatus->ifInfo[p_iStatus->ifNum].txSize = tx_b / BYTE_TO_KIBIBYTE;
    memcpy(p_iStatus->ifInfo[p_iStatus->ifNum].hwaddr, hwaddr, HWADDR_LEN);

    p_iStatus->ifNum++;

    if (p_iStatus->ifNum >= RESM_INET_IF_MAX) {
      break;
    }
  }

  // Termination processing
  fclose(fh);

  if (p_iStatus->ifNum == 0) {
    return -1;
  }
  return 0;
}

/* Get Alias name */
static char *get_aliasName(char* name, char* line) {
  char* dot;
  char* dotname;

  while (isspace(*line)) {
    line++;
  }
  while (*line) {
    if (isspace(*line)) {
      *name++ = '\0';
      return NULL;
    }

    if (*line == ':') {
      dot = line, dotname = name;
      *name++ = *line++;

      while (isdigit(*line)) {
        *name++ = *line++;
      }
      if (*line != ':') {
        line = dot;
        name = dotname;
      }
      if (*line == '\0') {
        return NULL;
      }
      line++;
      break;
    }
    *name++ = *line++;
  }
  *name++ = '\0';

  return line;
}

/* Get hardware address */
static int32_t get_hwaddr(char* name, char* hwaddr) {
  struct ifreq ifr;
  int32_t ret;

  if (g_sock < 0) {  // LCOV_EXCL_BR_LINE 6: g_sock is not -1
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    return -1;    // LCOV_EXCL_LINE 6: g_sock is not -1
  }
  // Initialization
  memset(&ifr, 0, sizeof(ifr));

  // Set alias name
  strncpy(ifr.ifr_name, name, (sizeof(ifr.ifr_name) - 1));

  // Get hardware address
  ret = ioctl(g_sock, SIOCGIFHWADDR, &ifr);
  if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: ioctl error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    return -1;  // LCOV_EXCL_LINE 5: ioctl error case
  }
  memcpy(hwaddr, ifr.ifr_hwaddr.sa_data, HWADDR_LEN);

  return 0;
}

/*********************************************************************************
 * CPU monitoring
 *********************************************************************************/
// // CPU monitoring
// static void watchCPU() {
// Main processing of CPU monitoring
static void watchCPUStatus() {
  unsigned long fifo_task_occ = 0;
  unsigned long tss_task_occ = 0;
  int32_t fifo_status = g_fifo_status;
  int32_t tss_status = g_tss_status;
  int32_t cpu_load_status = CPU_TASK_INIT;

//  int32_t ret;
//  static int32_t cpuLogTimer = 0;
  if ((g_fifo_status == STATUS_CHECK_CPU) || (g_tss_status == STATUS_CHECK_CPU)) {
    watchCPU();

//  if (cpuLogTimer > 0) {  // LCOV_EXCL_BR_LINE 6:Because the condition cannot be set
//    // Make to progress the timer
//    cpuLogTimer -= WTC_CPU_INTERVAL;  // LCOV_EXCL_LINE 6:Because the condition cannot be set
//  }
    if (g_cpuloadRate1000 >= CPU_LOAD_THRESHOLD) {
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "CPU TOTAL:(%d.%d%%)",g_cpuloadRate1000/10, g_cpuloadRate1000%10);
      int i;
      int32_t cpuloadRate1000;
      for (i = 1; i < g_cpu_num; i++) {
        char buf[16];
        cpuloadRate1000 = calc_cpuloadRate_each(i);
        sprintf (buf, "%s(%d.%d%%) ", g_cpuload[i].cpuname, cpuloadRate1000/10, cpuloadRate1000%10);
        FRAMEWORKUNIFIEDLOG (ZONE_INFO, __FUNCTION__, "%s", buf);
      }
      if (g_fifo_status == STATUS_CHECK_CPU) {  // LCOV_EXCL_BR_LINE 6: g_fifo_status must be STATUS_CHECK_CPU
        fifo_status = STATUS_WATCH_PROCESS;
        g_fifo_timer = 0;
      }
      if (g_tss_status == STATUS_CHECK_CPU) {  // LCOV_EXCL_BR_LINE 6: g_tss_status must be STATUS_CHECK_CPU
        tss_status = STATUS_WATCH_PROCESS;
        g_tss_timer = 0;
      }
      if ((g_fifo_status != STATUS_WATCH_PROCESS ) && ( g_tss_status != STATUS_WATCH_PROCESS)) {  // LCOV_EXCL_BR_LINE 6: g_fifo_status must be STATUS_CHECK_CPU and g_tss_status must be STATUS_CHECK_CPU   // NOLINT[whitespace/line_length]
        logging_cpuload_custom(CPU_TASK_INIT);
      }
    }
  }

#if 0
  // Get CPU usage
  if (g_resmgr.cpuloadRate == CPULOAD_INVALID) {
    // First time
    init_cpuload();
    ret = get_cpuload(g_cpuload);
    if (ret == 0) {  // LCOV_EXCL_BR_LINE 5: get_cpuload will not return -1
      g_resmgr.cpuloadRate = CPULOAD_READY;
    } else {
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "watchCPU() Get cpuload Failed");  // LCOV_EXCL_LINE 5: get_cpuload will not return -1  // NOLINT[whitespace/line_length]
    }
#endif
  if ((g_fifo_status == STATUS_WATCH_PROCESS) || (g_tss_status == STATUS_WATCH_PROCESS)) {
    if (g_fifo_status == STATUS_WATCH_PROCESS) {
      g_fifo_timer = g_fifo_timer + WTC_CPU_INTERVAL;
      if ((g_fifo_timer == WTC_CPU_INTERVAL) || (g_fifo_timer >= FIFO_TIMER_LIMIT)) {  // LCOV_EXCL_BR_LINE 200: g_fifo_timer must be bigger than WTC_CPU_INTERVAL/FIFO_TIMER_LIMIT  // NOLINT[whitespace/line_length]
        cpu_load_status = CPU_TASK_SHOW_AF;
      } else {
        // LCOV_EXCL_START 200: g_fifo_timer must be bigger than WTC_CPU_INTERVAL/FIFO_TIMER_LIMIT
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        logging_cpuload_custom(FIFO_TASK_SHOW);
        // LCOV_EXCL_STOP
      }
    }
#if 0
    return;
  } else {
    memcpy(g_cpuload_pre, g_cpuload, sizeof(*g_cpuload_pre)*g_cpu_num);
    ret = get_cpuload(g_cpuload);
    if (ret != 0) {  // LCOV_EXCL_BR_LINE 5: get_cpuload will not return -1
      // LCOV_EXCL_START 5: get_cpuload will not return -1
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "watchCPU() Get cpuload Failed");
      return;
      // LCOV_EXCL_STOP 5: get_cpuload will not return -1
#endif
    if (g_tss_status == STATUS_WATCH_PROCESS) {
      g_tss_timer = g_tss_timer + WTC_CPU_INTERVAL;
      if ((g_tss_timer == WTC_CPU_INTERVAL) || (g_tss_timer >= TSS_TIMER_LIMIT)) {
        cpu_load_status = CPU_TASK_SHOW_AF;
      } else {
        logging_cpuload_custom(TSS_TASK_SHOW);
      }
    }
#if 0
  }
  // Calculate CPU usage (Notes! Return as a thousandth rate(10 times of %)
  g_cpuloadRate1000 = calc_cpuloadRate();
  g_resmgr.cpuloadRate = g_cpuloadRate1000 / 10;

  ret = chk_logging(g_resmgr.cpuloadRate, cpuLogTimer);
  if (ret == CPULOG_LOGGING) {
    // Logging
#endif
    if (cpu_load_status == CPU_TASK_SHOW_AF) {
#if 0
      int i;
      int32_t cpuloadRate1000;
      char *cpunames = (char *) malloc(  // NOLINT
          sizeof("[CpuHighLoad]") + (sizeof("cpuXX(xxx%) ") * g_cpu_num));
      cpuloadRate1000 = calc_cpuloadRate();
      sprintf(cpunames, "[CpuHighLoad]%s(%d%%) ", g_cpuload[0].cpuname, cpuloadRate1000/10);  // NOLINT
      for (i = 1; i < g_cpu_num; i++) {
        char buf[16];
        cpuloadRate1000 = calc_cpuloadRate_each(i);
        sprintf(buf, "%s(%d%%) ", g_cpuload[i].cpuname, cpuloadRate1000 / 10);  // NOLINT
        strcat(cpunames, buf);  // NOLINT
      }
      free(cpunames);
      FRAMEWORKUNIFIEDLOG(ZONE_INFO, __FUNCTION__, "%s", cpunames);
    }
#endif
      logging_cpuload_custom(cpu_load_status);
    }
    if (g_fifo_status == STATUS_WATCH_PROCESS) {
      fifo_task_occ = logging_cpuload_custom(CPU_FIFO_TASK_GET_OCCUPANCY);
      if ((fifo_task_occ >= TASK_STAT_THRESHOLD) && (g_fifo_timer >= FIFO_TIMER_LIMIT)) {
        fifo_status = STATUS_IDOL;
        g_fifo_timer = 0;
        exec_perf(logging_cpuload_custom(CPU_FIFO_TASK_GET_ID));
      } else if (fifo_task_occ < TASK_STAT_THRESHOLD) {
        fifo_status = STATUS_CHECK_CPU;
        g_fifo_timer = 0;
        logging_cpuload_custom(CPU_FIFO_TASK_GET_ID);
      }
    }
    if (g_tss_status == STATUS_WATCH_PROCESS) {
      tss_task_occ = logging_cpuload_custom(CPU_TSS_TASK_GET_OCCUPANCY);
      if ((tss_task_occ >= TASK_STAT_THRESHOLD) && (g_tss_timer >= TSS_TIMER_LIMIT)) {
        tss_status = STATUS_IDOL;
        g_tss_timer = 0;
        exec_perf(logging_cpuload_custom(CPU_TSS_TASK_GET_ID));
      } else if(tss_task_occ < TASK_STAT_THRESHOLD) {
        tss_status = STATUS_CHECK_CPU;
        g_tss_timer = 0;
        logging_cpuload_custom(CPU_TSS_TASK_GET_ID);
      }
    }
#if 0
    logging_cpuload_custom();
    logging_cpuload();
    // Set timer
    cpuLogTimer = CPU_HIGH_LOAD_LOG_FREQ;
  } else if (ret == CPULOG_TIMER_CLEAR) {
    // Clear Timer
    cpuLogTimer = 0;
  }

  return;
#endif
    logging_cpuload_custom(CPU_TASK_SHOW_BF);
  }
  if ((g_fifo_status == STATUS_IDOL) || (g_tss_status == STATUS_IDOL)) {
    if (g_fifo_status == STATUS_IDOL) {
      g_fifo_timer = g_fifo_timer + WTC_CPU_INTERVAL;
      if (g_fifo_timer >= CPU_HIGH_LOAD_LOG_FREQ) {
        fifo_status = STATUS_CHECK_CPU;
        g_fifo_timer = 0;
      }
    }
    if (g_tss_status == STATUS_IDOL) {
      g_tss_timer = g_tss_timer + WTC_CPU_INTERVAL;
      if (g_tss_timer >= CPU_HIGH_LOAD_LOG_FREQ) {
        tss_status = STATUS_CHECK_CPU;
        g_tss_timer = 0;
      }
    }
  }
  g_fifo_status = fifo_status;
  g_tss_status = tss_status;

  return;
}



// CPU monitoring
static void
watchCPU() {
  int32_t ret;

  memcpy(g_cpuload_pre, g_cpuload, sizeof(*g_cpuload_pre)*g_cpu_num);
  ret = get_cpuload(g_cpuload);
  if ( ret != 0 ) {
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "watchCPU() Get cpuload Failed");
    g_cpuloadRate1000 = 0;
  } else {
    // Calculate CPU usage (Notes! Return as a thousandth rate(10 times of percentage)
    g_cpuloadRate1000 = calc_cpuloadRate();
  }

  return;
}


static void init_cpuload(void) {
  int fd;
  char buf[READ_MAX_SIZE];
  int32_t ret;
  CPU_INFO_t p_cpu;
  ssize_t rsize;
  char *p, *ep;
  struct timespec req = { 0, 1000000 };  // 1msec

  g_cpu_num = 0;
  g_cpuload_pre = NULL;

  fd = open(PROC_STAT_FILE, O_RDONLY);
  if (fd == -1) {  // LCOV_EXCL_BR_LINE 5: open error case
    // LCOV_EXCL_START 5: open error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr File Open Error. errno[%d]",
           errno);
    return;
    // LCOV_EXCL_STOP 5: open error case
  }
  rsize = read(fd, buf, sizeof(buf));
  if (rsize == -1) {  // LCOV_EXCL_BR_LINE 5: read error case
    // LCOV_EXCL_START 5: read error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr File Read Error. errno[%d]",
           errno);
    close(fd);
    return;
    // LCOV_EXCL_STOP 5: read error case
  }
  nanosleep(&req, NULL);
  p = buf;
  ep = buf + rsize;
  while (p < ep) {
    if (strncmp(p, "cpu", 3) == 0) {
      ret = sscanf(p, "%8s %d %d %d %d %d %d %d %d %d %d", &p_cpu.cpuname[0],
                   &p_cpu.user, &p_cpu.nice, &p_cpu.system, &p_cpu.idle,
                   &p_cpu.iowait, &p_cpu.irq, &p_cpu.softirq, &p_cpu.steal,
                   &p_cpu.guest, &p_cpu.guest_nice);
      if (ret < 11) {  // LCOV_EXCL_BR_LINE 200: ret will always 11
        // LCOV_EXCL_START 200: ret will always 11
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
               "ResMgr get_cpuload() File Read Error");
        close(fd);
        return;
        // LCOV_EXCL_STOP 200: ret will always 11
      }
      g_cpu_num++;
      while (++p < ep) {
        if (*p == LF) {
          p++;
          break;
        }
      }
    } else {
      break;
    }
  }
  close(fd);

  g_cpuload_pre = reinterpret_cast<CPU_INFO_t *>(malloc(sizeof(CPU_INFO_t) * g_cpu_num));
  g_cpuload = reinterpret_cast<CPU_INFO_t *>(malloc(sizeof(CPU_INFO_t) * g_cpu_num));
  if ((g_cpuload_pre == NULL) || (g_cpuload == NULL)) {  // LCOV_EXCL_BR_LINE 5: malloc error case
    // LCOV_EXCL_START 5: malloc error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
           "ResMgr malloc Error. g_cpu_num[%d] errno[%d]", g_cpu_num, errno);
    return;
    // LCOV_EXCL_STOP 5: malloc error case
  }

  FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr cpu_num:%d", g_cpu_num);

  return;
}

// Get CPU usage
static int32_t get_cpuload(CPU_INFO_t* p_cpu) {
  int fd;
  char buf[READ_MAX_SIZE];
  int32_t ret;
  int i = 0;
  ssize_t rsize;
  char *p, *ep;
  struct timespec req = { 0, 1000000 };  // 1msec

  // Open file
  fd = open(PROC_STAT_FILE, O_RDONLY);
  if (fd == -1) {  // LCOV_EXCL_BR_LINE 5: open's error case
    // LCOV_EXCL_START 5: open's error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
           "ResMgr get_cpuload() File Open Error. errno[%d]", errno);
    return -1;
    // LCOV_EXCL_STOP
  }
  rsize = read(fd, buf, sizeof(buf));
  if (rsize == -1) {  // LCOV_EXCL_BR_LINE 5: read error case
    // LCOV_EXCL_START 5: read error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr File Read Error. errno[%d]",
           errno);
    close(fd);
    return -1;
    // LCOV_EXCL_STOP 5: read error case
  }
  nanosleep(&req, NULL);
  p = buf;
  ep = buf + rsize;
  while (p < ep) {
    if (strncmp(p, "cpu", 3) == 0) {
      ret = sscanf(p, "%8s %d %d %d %d %d %d %d %d %d %d", &p_cpu[i].cpuname[0],
                   &p_cpu[i].user, &p_cpu[i].nice, &p_cpu[i].system,
                   &p_cpu[i].idle, &p_cpu[i].iowait, &p_cpu[i].irq,
                   &p_cpu[i].softirq, &p_cpu[i].steal, &p_cpu[i].guest,
                   &p_cpu[i].guest_nice);
      if (ret < 11) {  // LCOV_EXCL_BR_LINE 200: ret will always 11
        // LCOV_EXCL_START 200: ret will always 11
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
               "ResMgr get_cpuload() File Read Error");
        close(fd);
        return -1;
        // LCOV_EXCL_STOP 200: ret will always 11
      }
      i++;
      while (++p < ep) {
        if (*p == LF) {
          p++;
          break;
        }
      }
    } else {
      break;
    }
  }
  close(fd);

  return 0;
}

// Calcurate CPU usage
static int32_t calc_cpuloadRate(void) {
  int val;
  int val_others;
  int total;
  int rate;
  CPU_INFO_t* info;
  CPU_INFO_t* info_pre;

  info = &g_cpuload[0];
  info_pre = &g_cpuload_pre[0];

  val = (info->user - info_pre->user)
      + (info->system - info_pre->system)
      + (info->irq - info_pre->irq)
      + (info->softirq - info_pre->softirq);

  val_others = (info->idle - info_pre->idle)
      + (info->iowait - info_pre->iowait);

  total = val + val_others;
  rate = (total > 0) ? (val*1000 / total) : 0;

  return rate;
}

// Calcurate CPU usage for each CPU
static int32_t calc_cpuloadRate_each(int num) {
  int val, valn;
  int rate;
  CPU_INFO_t* info;
  CPU_INFO_t* info_pre;

  // cpu
  info = &g_cpuload[0];
  info_pre = &g_cpuload_pre[0];
  val = (info->user - info_pre->user)
      + (info->system - info_pre->system)
      + (info->irq - info_pre->irq)
      + (info->softirq - info_pre->softirq);

  // cpu-num
  info = &g_cpuload[num];
  info_pre = &g_cpuload_pre[num];
  valn = (info->user - info_pre->user)
    + (info->system - info_pre->system)
    + (info->softirq - info_pre->softirq);

  rate = valn * 1000 / val;
  return rate;
}


//// Logging check at CPU overload
//static int32_t chk_logging(int32_t cpuload, int32_t timer) {
//  if (cpuload >= CPU_LOAD_THRESHOLD) {
//    if (timer > 0) {
//      return CPULOG_NO_LOGGING;
//    }
//    return CPULOG_LOGGING;
//  }
//
//  return CPULOG_TIMER_CLEAR;
//}


// Check if cmd to perf
  // LCOV_EXCL_START 8: dead code
bool valid_perf_cmd(char *buf) {  // LCOV_EXCL_BR_LINE 8: dead code
  const char *cmd2exclude[] = { "top", "init", "bash", NULL };
  char cmdstr[64];

  for (int i = 0;; i++) {
    if (cmd2exclude[i] == NULL) {
      break;
    }
    sprintf(cmdstr, " %s ", cmd2exclude[i]);  // NOLINT
    if (strstr(buf, cmdstr) == NULL) {
      continue;
    } else {
      return false;
    }
  }
  return true;
}
  // LCOV_EXCL_STOP 8: dead code

//// exec perf to pids
//static pid_t pids[PERF_MAX_PROCS];


static bool lower_sched_priority(int niceval) {
  struct sched_param sparam = { };
  sparam.sched_priority = 0;
  if (sched_setscheduler(0, SCHED_OTHER, &sparam) == -1) {  // LCOV_EXCL_BR_LINE 5: sched_setscheduler's error case
    // LCOV_EXCL_START 5: sched_setscheduler's error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed change scheduler to TSS, errno=%d",
           errno);
    return false;
    // LCOV_EXCL_STOP
  }
  if (nice(niceval) == -1) {  // LCOV_EXCL_BR_LINE 5: nice's error case
    // LCOV_EXCL_START 5: nice's error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to add nice val %d, errno=%d",
           niceval, errno);
    return false;
    // LCOV_EXCL_STOP
  }
  return true;
}

/*********************************************************************************
 * exec_perf sub function RECORD perf data processing
 *********************************************************************************/
static void exec_perf_Record_Perf_Data(
    pid_t* c_pids, char* perf_file, char* pidstr, int* status,

//    char pnames[PERF_MAX_PROCS][PERF_PNAME_MAX]) {
    pid_t pids[PERF_MAX_PROCS]) {

  int i;
  int fd;
  pid_t term_pid;
  int waitret;
  bool do_sigkill = false;

  FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "+");

  /* RECORD perf data */
  for (i = 0; i < PERF_MAX_PROCS; i++) {
    if (pids[i] <= 0) {
      break;
    }
    sprintf(perf_file, PERF_FILE"%06d", pids[i]);  // NOLINT
    sprintf(pidstr, "%d", pids[i]);  // NOLINT
    if ((c_pids[i] = fork()) == 0) {
      if (lower_sched_priority(CPULOAD_NICEVAL) == false) {  // LCOV_EXCL_BR_LINE 200: lower_sched_priority() must be true
        // LCOV_EXCL_START 200: lower_sched_priority() must be true
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to lower scheduler-1");
        exit(1);
        // LCOV_EXCL_STOP
      }
      if (setuid(0) == -1) {  // LCOV_EXCL_BR_LINE 5: setuid's error case
        // LCOV_EXCL_START 5: setuid's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to setuid(0)-1, errno=%d",
               errno);
        exit(1);
        // LCOV_EXCL_STOP
      }
      // Redirect STDERR
      fd = open("/dev/null", O_WRONLY);
      if (fd > 0) {  // LCOV_EXCL_BR_LINE 5: open's error case
        dup2(fd, 2);
      }
      // Exec perf
      execl(PERF_PATH, basename(PERF_PATH), "record", "-p", pidstr, "-o",
            perf_file, NULL);
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to execl %s record, errno=%d",
             PERF_PATH,
             errno);
      exit(1);
    }
    if (c_pids[i] == (pid_t) -1) {  // LCOV_EXCL_BR_LINE 5: fork's error case
      // LCOV_EXCL_START 5: fork's error case
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to fork-1, errno=%d", errno);
      continue;
      // LCOV_EXCL_STOP
    }
    // Kill perf after PERF_RECORD_SPAN sec
    // (Killed by child process to avoid making resm process super-user with setuid.)
    if ((term_pid = fork()) == 0) {
      const struct timespec delay = { PERF_RECORD_SPAN, 0 };
      if (setuid(0) == -1) {  // LCOV_EXCL_BR_LINE 5: setuid's error case
        // LCOV_EXCL_START 5: setuid's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to setuid(0)-2, errno=%d",
               errno);
        exit(1);
        // LCOV_EXCL_STOP
      }
      nanosleep(&delay, NULL);  // Let perf record run for a while before kill it.
      if (kill(c_pids[i], SIGINT) == -1) {  // LCOV_EXCL_BR_LINE 5: kill's error case
        // LCOV_EXCL_START 5: kill's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Failed to kill(SIGINT), pid=%d, errno=%d", (int) c_pids[i],
               errno);
        // LCOV_EXCL_STOP
      }
      nanosleep(&delay, NULL);  // Allow perf to do ending procedure.
      exit(0);
    } else if (term_pid == (pid_t) -1) {  // LCOV_EXCL_BR_LINE 5: fork's error case
      // LCOV_EXCL_START 5: fork's error case
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to fork-2, errno=%d", errno);
      continue;
      // LCOV_EXCL_STOP
    } else {
      if (waitpid(term_pid, status, 0) == -1) {  // LCOV_EXCL_BR_LINE 5: waitpid's error case
        // LCOV_EXCL_START 5: waitpid's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Failed to waitpid of killer %d, errno=%d", term_pid, errno);
        continue;
        // LCOV_EXCL_STOP
      }
    }
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "");
    // NOT block even if perf is not terminated
    if ((waitret = waitpid(c_pids[i], status, WNOHANG)) == -1) {  // LCOV_EXCL_BR_LINE 5: waitpid's error case
      // LCOV_EXCL_START 5: waitpid's error case
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to waitpid of RECORD %d, errno=%d",
             c_pids[i], errno);
      // LCOV_EXCL_STOP
    } else if (waitret == 0) {
      // NOT exited
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to terminate perf record, pid=%d",
             c_pids[i]);
      pids[i] = -2;  // Skip following sequences
      do_sigkill = true;
    } else if (WEXITSTATUS(*status) != 0 && WEXITSTATUS(*status) != 2) {

//      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
//             "perf record %d exited abnormally, target=%s(%d), status=%d",
//             c_pids[i], pnames[i], pids[i], WEXITSTATUS(*status));
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
             "perf record %d exited abnormally, target=(%d), status=%d",
             c_pids[i], pids[i], WEXITSTATUS(*status));

      pids[i] = -2;  // Skip following sequences
      do_sigkill = false;
    }
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "");
    if (pids[i] == -2) {
      if ((term_pid = fork()) == 0) {
        const struct timespec delay = { PERF_RECORD_SPAN, 0 };
        if (setuid(0) == -1) {  // LCOV_EXCL_BR_LINE 5: setuid's error case
          // LCOV_EXCL_START 5: setuid's error case
          AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to setuid(0)-2B, errno=%d",
                 errno);
          exit(1);
          // LCOV_EXCL_STOP
        }
        // Kill "perf record" by SIGKILL signal
        if (do_sigkill) {
          int sigret = kill(c_pids[i], SIGKILL);
          if (sigret == 0) {  // LCOV_EXCL_BR_LINE 5: kill case
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                   "SIGKILL has been sent to perf record process, pid=%d",
                   c_pids[i]);
          } else {
            // LCOV_EXCL_START 5: kill case
            AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                   "sending SIGKILL to perf record failed, pid=%d, errno=%d",
                   c_pids[i], errno);
            // LCOV_EXCL_STOP 5: kill case
          }
        }
        nanosleep(&delay, NULL);
        // remove perf data file possibly made
        unlink(perf_file);
        FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "");
        exit(0);
      } else if (term_pid == (pid_t) -1) {  // LCOV_EXCL_BR_LINE 5: fork's error case
        // LCOV_EXCL_START 5: fork's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to fork-2B, errno=%d", errno);
        // LCOV_EXCL_STOP
      } else {
        if (waitpid(term_pid, status, 0) == -1) {    // LCOV_EXCL_BR_LINE 5: waitpid's error case
          // LCOV_EXCL_START 5: waitpid's error case
          AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                 "Failed to waitpid of killer-2 %d, errno=%d", term_pid, errno);
          // LCOV_EXCL_STOP
        }
        if (do_sigkill) {
          if ((waitret = waitpid(c_pids[i], status, WNOHANG)) == -1) { // LCOV_EXCL_BR_LINE 5: waitpid's error case
            // LCOV_EXCL_START 5: waitpid's error case
            AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                   "Failed to waitpid of RECORD(2) %d, errno=%d", c_pids[i],
                   errno);
            // LCOV_EXCL_STOP
          } else if (waitret == 0) {
            FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                   "Failed to terminate perf record by SIGKILL, pid=%d",
                   c_pids[i]);
          }
        }
      }
    }
  }
  FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "");
}

/*********************************************************************************
 * exec_perf sub function make perf file available to default user processing
 *********************************************************************************/

//static int32_t exec_perf_Make_Perf_File(pid_t* c_pids, char* perf_file) {
static int32_t exec_perf_Make_Perf_File(pid_t* c_pids, char* perf_file, pid_t pids[PERF_MAX_PROCS]) {

  int i;

  /* make perf file available to default user */
  if ((c_pids[0] = fork()) == 0) {
    if (setuid(0) == -1) {  // LCOV_EXCL_BR_LINE 5: setuid's error case
      // LCOV_EXCL_START 5: setuid's error case
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to setuid(0)-3, errno=%d", errno);
      exit(1);
      // LCOV_EXCL_STOP
    }
    for (i = 0; i < PERF_MAX_PROCS; i++) {
      if (pids[i] == -2) {
        // killed by SIGKILL
        continue;
      }
      if (pids[i] <= 0) {
        break;
      }
      sprintf(perf_file, PERF_FILE"%06d", pids[i]);  // NOLINT
      if (chmod(perf_file, 0666) != 0) {  // LCOV_EXCL_BR_LINE 5: chmod's error case
        // LCOV_EXCL_START 5: chmod's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to chmod %s, errno=%d\n",
               perf_file, errno);
        // LCOV_EXCL_STOP
      }
    }
    exit(0);
  }
  if (c_pids[0] == (pid_t) -1) {  // LCOV_EXCL_BR_LINE 5: fork's error case
    // LCOV_EXCL_START 5: fork's error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to fork-3, errno=%d", errno);
    return -1;
    // LCOV_EXCL_STOP
  }
  if (waitpid(c_pids[0], NULL, 0) == -1) {  // LCOV_EXCL_BR_LINE 5: waitpid's error case
    // LCOV_EXCL_START 5: waitpid's error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to waitpid of CHMOD %d, errno=%d\n",
           c_pids[0], errno);
  // LCOV_EXCL_STOP
  }
  FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "");
  return 0;
}

/*********************************************************************************
 * exec_perf sub function REPORT perf data into dump file processing
 *********************************************************************************/
static void exec_perf_Report_Perf_Data(pid_t* c_pids, char* perf_file,

//                                       char* perf_dump, int* status) {
                                       char* perf_dump, int* status, pid_t pids[PERF_MAX_PROCS]) {

  int i;
  int fd;
  int waitret;

  FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "");
  /* REPORT perf data into dump file */
  for (i = 0; i < PERF_MAX_PROCS; i++) {
    const struct timespec delay = { PERF_REPORT_DELAY, 0 };
    if (pids[i] == -2) {
      // killed by SIGKILL
      continue;
    }
    if (pids[i] <= 0) {
      break;
    }
    sprintf(perf_file, PERF_FILE"%06d", pids[i]);  // NOLINT
    sprintf(perf_dump, PERF_DUMP"%06d", pids[i]);  // NOLINT
    if ((c_pids[i] = fork()) == 0) {
      if (lower_sched_priority(CPULOAD_NICEVAL) == false) {  // LCOV_EXCL_BR_LINE 200: lower_sched_priority() will not return false
        // LCOV_EXCL_START 200: lower_sched_priority() will not return false
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to lower scheduler-2");
        exit(1);
        // LCOV_EXCL_STOP
      }
      if ((fd = open(perf_dump, (O_CREAT | O_WRONLY), 0666)) < 0) {  // LCOV_EXCL_BR_LINE 5: open error case
        // LCOV_EXCL_START 5: open error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to open %s, errno=%d\n",
               perf_dump, errno);
        // LCOV_EXCL_STOP 5: open error case
      } else {
        // Redirect STDOUT
        dup2(fd, 1);
        close(fd);
        // Redirect STDERR
        fd = open("/dev/null", O_WRONLY);
        if (fd > 0) {  // redirect stderr  // LCOV_EXCL_BR_LINE 5: open's error case
          dup2(fd, 2);
          close(fd);
        }
        // Exec perf
        FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "execl perf report");
        execl(PERF_PATH, basename(PERF_PATH), "report", "-i", perf_file, NULL);
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to execl %s report, errno=%d",
               PERF_PATH,
               errno);
      }
      exit(1);
    }
    if (c_pids[i] == (pid_t) -1) {  // LCOV_EXCL_BR_LINE 5: fork's error case
      // LCOV_EXCL_START 5: fork's error case
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to fork-4, errno=%d", errno);
      continue;
      // LCOV_EXCL_STOP
    }
    int ii = 0;
    for (; ii < PERF_REPORT_RETRY; ii++) {
      nanosleep(&delay, NULL);  // Make sure waitpid() to killer returns after perf process exited.
      if ((waitret = waitpid(c_pids[i], status, WNOHANG)) == -1) {  // LCOV_EXCL_BR_LINE 5: waitpid's error case
        // LCOV_EXCL_START 5: waitpid's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Failed to waitpid for REPORT %d, errno=%d", c_pids[i], errno);
        pids[i] = -2;  // Skip FRAMEWORKUNIFIEDLOG perf data
        break;
        // LCOV_EXCL_STOP
      } else if (waitret == c_pids[i]) {
        break;
      }
    }
    if (ii >= PERF_REPORT_RETRY) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "perf report Failed to exit, pid=%d",
             c_pids[i]);
      pids[i] = -2;  // Skip FRAMEWORKUNIFIEDLOG perf data
    }
    if (pids[i] == -2) {
      // Terminate perf report with SIGKILL
      const struct timespec delay = { PERF_RECORD_SPAN, 0 };
      int sigret = kill(c_pids[i], SIGKILL);
      if (sigret != 0) {  // LCOV_EXCL_BR_LINE 5: kill's error case
        // LCOV_EXCL_START 5: kill's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Failed in sending SIGKILL to perf report, pid=%d, errno=%d",
               c_pids[i], errno);
        // LCOV_EXCL_STOP
      } else {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "SIGKILL has been sent to perf report process, pid=%d",
               c_pids[i]);
      }
      nanosleep(&delay, NULL);  // Make sure waitpid() to killer returns after perf process exited.
      if ((waitret = waitpid(c_pids[i], status, WNOHANG)) == -1) {  // LCOV_EXCL_BR_LINE 5: waitpid's error case
        // LCOV_EXCL_START 5: waitpid's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Failed to waitpid of REPORT(2) %d, errno=%d", c_pids[i], errno);
        // LCOV_EXCL_STOP
      } else if (waitret == 0) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "Failed to terminate perf report by SIGKILL, pid=%d", c_pids[i]);
      }
      // Cleanup
      unlink(perf_dump);
      unlink(perf_file);
    }
  }
}

/*********************************************************************************
 * exec_perf FRAMEWORKUNIFIEDLOG perf data processing
 *********************************************************************************/
static void exec_perf_Resourcemanagerlog_Perf_Data(
    char* perf_file, char* perf_dump,

//    char pnames[PERF_MAX_PROCS][PERF_PNAME_MAX]) {
    pid_t pids[PERF_MAX_PROCS]) {

  int i;
  int perf_lines;
  char buf[READLINE_MAX_SIZE];
  FILE *rfd;

  FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "");
  /* FRAMEWORKUNIFIEDLOG perf data */
  for (i = 0; i < PERF_MAX_PROCS; i++) {
    if (pids[i] == -2) {
      // killed by SIGKILL
      continue;
    }
    if (pids[i] <= 0) {
      break;
    }
    sprintf(perf_file, PERF_FILE"%06d", pids[i]);  // NOLINT
    sprintf(perf_dump, PERF_DUMP"%06d", pids[i]);  // NOLINT
    if ((rfd = fopen(perf_dump, "r")) == NULL) {  // LCOV_EXCL_BR_LINE 5: fopen error case
      // LCOV_EXCL_START 5: fopen error case
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to open-2 %s, errno=%d\n",
             perf_dump, errno);
      continue;
      // LCOV_EXCL_STOP 5: fopen error case
    }
    perf_lines = 0;
    while (fgets(buf, sizeof(buf), rfd) > 0) {
      if (perf_lines >= PERF_MAX_LINES) {
        break;
      }
      /* skip header */
      if (buf[0] == '#') {
        continue;
      }
      if (buf[0] == '\n' || buf[0] == '\r') {
        continue;
      }
      trim_end(buf);
      if (perf_lines == 0) {

//        FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "[CpuHLPerf:%s(%d)]report by result of 'perf record -p %d'",
//               pnames[i], pids[i], pids[i]);
        FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "[CpuHLPerf:(%d)]report by result of 'perf record -p %d'",
               pids[i], pids[i]);

      }
      FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "[CpuHLPerf]%s\n", buf);
      perf_lines++;
    }
    fclose(rfd);
    // Check if no line is acquired
    if (perf_lines == 0) {

//      FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "[CpuHLPerf:%s(%d)] NO_DATA_acquired",
//             pnames[i], pids[i]);
      FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "[CpuHLPerf:(%d)] NO_DATA_acquired",
             pids[i]);

    }
    // Cleanup
    unlink(perf_dump);
    unlink(perf_file);
  }
  FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "-");
}

/*********************************************************************************
 * exec_perf Main processing
 *********************************************************************************/

//static void exec_perf(char pnames[PERF_MAX_PROCS][PERF_PNAME_MAX]) {
static void exec_perf(int32_t t_pid) {

  pid_t c_pids[PERF_MAX_PROCS];   // max process to exec perf(forked child pids)

  pid_t pids[PERF_MAX_PROCS];
  char perf_file[128];
  char perf_dump[128];
  char pidstr[64];
  struct stat statbuf;
  int status;

  /* Check existance of perf exec file */
  if (stat(PERF_PATH, &statbuf) == -1 || !(statbuf.st_mode & S_IXUSR)) {
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "%s is not available", PERF_PATH);
    return;
  }

  FRAMEWORKUNIFIEDLOG (ZONE_INFO, __FUNCTION__, "[CpuHighLoad]Please check User backtrace of following processes in kernel.log");
//  print_backtrace_pid(t_pid);
  for (int i = 0; i < PERF_MAX_PROCS; i++) {
    pids[i] = -1;
  }

  pids[0] = t_pid;


  /* RECORD perf data */

//  exec_perf_Record_Perf_Data(c_pids, perf_file, pidstr, &status, pnames);
  exec_perf_Record_Perf_Data(c_pids, perf_file, pidstr, &status, pids);


  /* make perf file available to default user */

//  if (exec_perf_Make_Perf_File(c_pids, perf_file) != 0)
  if (exec_perf_Make_Perf_File(c_pids, perf_file, pids) != 0)

    return;

  /* REPORT perf data into dump file */

//  exec_perf_Report_Perf_Data(c_pids, perf_file, perf_dump, &status);
  exec_perf_Report_Perf_Data(c_pids, perf_file, perf_dump, &status, pids);


  /* FRAMEWORKUNIFIEDLOG perf data */

//  exec_perf_Resourcemanagerlog_Perf_Data(perf_file, perf_dump, pnames);
  exec_perf_Resourcemanagerlog_Perf_Data(perf_file, perf_dump, pids);

}

// Logging at CPU overload
#define WAIT_RETRY 3           // 3sec


#if 0
static void logging_cpuload(void) {
  int32_t pipe_fd[2];  // 0:stdin,1:stdout
  pid_t c_pid;
  char buf[READLINE_MAX_SIZE];
  char buf2[READLINE_MAX_SIZE];
  char tmp[READLINE_MAX_SIZE];
  int32_t logLine = 0;
  char* ptr;
  int32_t ret;
  int32_t status;
  int32_t perfNum = 0;
  char pnames[PERF_MAX_PROCS][PERF_PNAME_MAX] = { };
  int save_0 = -1;
  int kill_flag = 1;
  int waitret;

  FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "+");

  // Create pipe
  ret = pipe(pipe_fd);

  if (ret != 0) {  // LCOV_EXCL_BR_LINE 5: pipe error case
    // LCOV_EXCL_START 5: pipe error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
           "ResMgr logging_cpuload() pipe Error");
    return;
    // LCOV_EXCL_STOP 5: pipe error case
  }

  // Create child process
  c_pid = fork();
  if (c_pid < 0) {  // LCOV_EXCL_BR_LINE 5: fork error case
    // LCOV_EXCL_START 5: fork error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__,
           "ResMgr logging_cpuload() fork Error");
    close(pipe_fd[0]);
    close(pipe_fd[1]);

    return;
    // LCOV_EXCL_STOP 5: fork error case
  }

  if (c_pid == 0) {
    /*******************************************************
     * Child-process
     *   The use of dup() and Close() between fork()-> exec() has been
     *   confirmed no probrem.
     *******************************************************/
    if (lower_sched_priority(CPULOAD_NICEVAL) == false) {
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to lower scheduler");
      exit(1);
    }
    close(pipe_fd[0]);     // Unneeded pipes (close stdin)

    close(1);              // Close stdout
    dup2(pipe_fd[1], 1);   // Duplicate stdout to pipe_fd[1]
    close(pipe_fd[1]);

    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "execl top");
    execl("/usr/bin/top", "top", "-n", "1", "-b", NULL);
    // error
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "ResMgr logging_cpuload() execl Error");
    exit(1);
  } else {
    close(pipe_fd[1]);      // Unneeded pipes(Close stdout)

    save_0 = dup(0);
    close(0);               // Close stdin
    dup2(pipe_fd[0], 0);    // Duplicate stdin to pipe_fd[0]
    close(pipe_fd[0]);

    for (int i = 0; i < PERF_MAX_PROCS; i++) {
      pids[i] = -1;
    }

    {
      fd_set fds;
      int32_t maxFd;
      int ret;
      struct timeval tmout = { TOP_TIMEOUT, 0 };

      FD_ZERO(&fds);
      FD_SET(STDIN_FILENO, &fds);
      maxFd = STDIN_FILENO;
      ret = select(maxFd + 1, &fds, NULL, NULL, &tmout);
      if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: select error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "[RESM]Handle Error errno:%d", errno);  // LCOV_EXCL_LINE 5: select error case
      } else if (FD_ISSET(STDIN_FILENO, &fds)) {
        kill_flag = 0;
      } else {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "[RESM]'top': No response during %d seconds", TOP_TIMEOUT);
      }
      if (kill_flag) {
        // Kill top after TOP_TIMEOUT sec
        // (Killed by child process to avoid making resm process super-user with setuid.)
        if (kill(c_pid, SIGKILL) == -1) {  // LCOV_EXCL_BR_LINE 5: kill error case
          // LCOV_EXCL_START 5: kill error case
          AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                 "Failed to kill(SIGKILL), pid=%d, errno=%d", (int) c_pid,
                 errno);
          // LCOV_EXCL_STOP 5: kill error case
        }
      } else {
        while (fgets(buf, sizeof(buf), stdin) > 0) {
          // Save ProcessName and Process ID to exec perf
          if (logLine >= 2 && perfNum < PERF_MAX_PROCS) {
            buf2[0] = 0;
            strncat(buf2, buf, sizeof(buf2) - 1);
            buf2[sizeof(buf2) - 1] = 0;
            if (valid_perf_cmd(buf2)) {
              pids[perfNum] = atoi(buf2);
              trim_end(buf2);
              strncat(pnames[perfNum], rindex(buf2, ' ') + 1,
                      sizeof(pnames[0]) - 1);
              if (pids[perfNum] >= 0
                  && strnlen(pnames[perfNum], sizeof(pnames[perfNum]) - 1)) {
                perfNum++;
              } else {
                pids[perfNum] = -1;
              }
            }
          }
          if (logLine == 0) {
            if ((buf[0] != 'C') && (buf[0] != '%')) {
              continue;
            }
            ptr = strstr(buf, "sy");
            if (ptr == NULL) {
              continue;
            }
            while (isalpha(*ptr)) {
              ptr++;
            }
            *ptr = '\0';
            escape_percent(buf, tmp);
            FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "[CpuHighLoad]%s", tmp);
            logLine++;
          } else if (logLine == 1) {
            ptr = strstr(buf, "PID");
            if (ptr == NULL) {
              continue;
            }
            trim_end(buf);
            escape_percent(buf, tmp);
            FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "[CpuHighLoad]%s", tmp);
            logLine++;
          } else if (logLine < (CPU_HIGH_LOAD_P_LOG_NUM + 2)) {
            trim_end(buf);
            FRAMEWORKUNIFIEDLOG(ZONE_WARN, __FUNCTION__, "[CpuHighLoad]%s", buf);
            logLine++;
          }
        }
      }
    }
    FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "wait pid(%d) kill_flag(%d)", c_pid, kill_flag);
    if (kill_flag) {
      const struct timespec delay = {1, 0};
      int i;
      for (i = 0; i < WAIT_RETRY; i++) {
        nanosleep(&delay, NULL);
        if ((waitret = waitpid(c_pid, &status, WNOHANG)) == -1) {
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to waitpid for top %d, errno=%d", c_pid, errno);
          break;
        } else if (waitret == c_pid) {
          FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "waitpid OK");
          break;
        }
      }
      if (i >= WAIT_RETRY) {
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "top Failed to exit, pid=%d", c_pid);
      }
    } else {
      if ((waitret = waitpid(static_cast<int>(c_pid), &status, 0)) < 0) {
        FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "waitpid(%d) Error errno(%d)", c_pid, errno);
      }
    }
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "waitpid(%d) returned (%d) errno(%d) status=%d",
           c_pid, waitret, errno, WEXITSTATUS(*status));
    if (save_0 >= 0) {
      dup2(save_0, 0);    // Reset the stdin to 0
      close(save_0);
    }
    if (!kill_flag) {
      exec_perf(pnames);
    }
  }
  FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "-");

  return;
}
#endif


// Tail adjustment
static void trim_end(char* buf) {
  int32_t len;

  len = strlen(buf);
  while (len > 0) {
    if (isspace(buf[len - 1])) {
      buf[len - 1] = '\0';
      len--;
      continue;
    }
    break;
  }

  return;
}


#if 0
// Escape character "%"
static void escape_percent(char* in, char* out) {
  char* head;
  char* tail;

  head = in;

  out[0] = '\0';

  while (1) {
    tail = strchr(head, '%');
    if (tail == NULL) {
      strcat(out, head);  // NOLINT
      break;
    }
    *tail = '\0';

    strcat(out, head);  // NOLINT
    strcat(out, "%%");  // NOLINT

    tail++;
    head = tail;
  }

  return;
}
#endif


/*********************************************************************************
 * Output debug information display
 *********************************************************************************/
void outputResouceInfo(void) {
  static bool madedir = false;
  struct stat sbuf;
  FILE *wfp;

  // Create directory
  if (!madedir) {
    if (stat(DEBUG_INFO_DIRPATH, &sbuf) != 0) {  // LCOV_EXCL_BR_LINE 5: stat's error case
      if (mkdir(DEBUG_INFO_DIRPATH, 0777) != 0) {  // LCOV_EXCL_BR_LINE 5: mkdir's error case
        // LCOV_EXCL_START 5: mkdir's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to mkdir %s, errno=%d",
               DEBUG_INFO_DIRPATH,
               errno);
        return;
        // LCOV_EXCL_STOP
      }
    }
    madedir = true;
  }
  // Open files to work
  if ((wfp = fopen(DEBUG_INFO_TMPPATH, "w")) == NULL) {  // LCOV_EXCL_BR_LINE 5: fopen error case
    // LCOV_EXCL_START 5: fopen error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to open %s, errno=%d",
           DEBUG_INFO_TMPPATH,
           errno);
    return;
    // LCOV_EXCL_STOP 5: fopen error case
  }
  // Output memory information work
  if (write_meminfo_work(wfp) != 0) {  // LCOV_EXCL_BR_LINE 6: write_meminfo_work will not be error
    // LCOV_EXCL_START 6: write_meminfo_work will not be error
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "Failed to edit and output in write_meminfo_work()");
    fclose(wfp);
    return;
    // LCOV_EXCL_STOP 6: write_meminfo_work will not be error
  }
  // Get CMA MEMORY information and output working info
  if (write_cmainfo_work(wfp) != 0) {  // LCOV_EXCL_BR_LINE 6: write_cmainfo_work will not be error
    // LCOV_EXCL_START 6: write_cmainfo_work will not be error
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "Failed to edit and output in write_cmainfo_work()");
    fclose(wfp);
    return;
    // LCOV_EXCL_STOP 6: write_cmainfo_work will not be error
  }
  // Get top information and output work info
  if (write_cpuinfo_work(wfp) != 0) {  // LCOV_EXCL_BR_LINE 6: write_cpuinfo_work will not be error
    // LCOV_EXCL_START 6: write_cpuinfo_work will not be error
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "Failed to edit and output in write_cpuinfo_work()");
    fclose(wfp);
    return;
    // LCOV_EXCL_STOP 6: write_cpuinfo_work will not be error
  }
  fclose(wfp);
  // Create output file
  if (rename(DEBUG_INFO_TMPPATH, DEBUG_INFO_FPATH) != 0) {  // LCOV_EXCL_BR_LINE 5:rename error case
    // LCOV_EXCL_START 5:rename error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to make output file %s, errno=%d",
           DEBUG_INFO_FPATH,
           errno);
    // LCOV_EXCL_STOP 5:rename error case
  }
  // Write information and output FRAMEWORKUNIFIEDLOG
  if (write_resourcemanagerloginfo_work() != 0) {  // LCOV_EXCL_BR_LINE 6: write_resourcemanagerloginfo_work will not be error
    // LCOV_EXCL_START 6: write_resourcemanagerloginfo_work will not be error
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
           "Failed to output in write_resourcemanagerloginfo_work()");
    // LCOV_EXCL_STOP 6: write_resourcemanagerloginfo_work will not be error
  }
}

// Output memory information work
static int write_meminfo_work(FILE *wfp) {
  float total;
  float avail;
  float used;
  float min_remain;
  uint32_t used_rate;
  uint32_t used_letters;
  // Output meminfo: Getting info from the proc/meminfo is performed at 5-second intervals in watchMem, so it is diverted.
  avail = static_cast<float>((mainFree_kib + inactFile_kib));
  total = static_cast<float>(memTotal_kib);
  used = total - avail;
  min_remain = static_cast<float>(minRestMem);
  // "*MEMORY   @@@@@@ Warning!! @@@@@"
  fprintf(wfp, "*MEMORY");
  if (avail * 10 < total) {  // (Less than 1/10)
    fprintf(wfp, "   @@@@@@ Warning!! @@@@@\n");
  } else {
    fprintf(wfp, "   \n");
  }
  // "used/avail/total/used max   xxx.xMB / xxx.xMB / xxx.xMB(xx.x%) / xxx.xMB
  used /= 1024;
  avail /= 1024;
  total /= 1024;
  min_remain /= 1024;
  if (total == 0) {
    used_rate = 0;
  } else {
    used_rate = (uint32_t) (used * 1000 / total);
    if (used_rate >= 1000) {
      used_rate = 999;
    }
  }
  fprintf(
    wfp,
    " used/avail/total/min remain   %5.1fMB / %5.1fMB / %5.1fMB(%2d.%d%%) / %5.1fMB\n",
    used, avail, total, used_rate / 10, used_rate % 10, min_remain);
  if (total == 0) {
    used_letters = 0;
  } else {
    used_letters = (uint32_t) (DEBUG_INFO_MEM_LETTERS * used / total);
    if (used_letters > DEBUG_INFO_MEM_LETTERS) {
      used_letters = DEBUG_INFO_MEM_LETTERS;
    }
  }
  // "------------------*******"
  int i;
  for (i = 0; i < static_cast<int>(used_letters); i++) {
    fprintf(wfp, "-");
  }
  for (; i < DEBUG_INFO_MEM_LETTERS; i++) {
    fprintf(wfp, "*");
  }
  fprintf(wfp, "\n\n");

  return 0;
}
// Get top information and Output work
static int write_cpuinfo_work(FILE *wfp) {
  int32_t pipe_fd[2];  // 0:stdin,1:stdout
  pid_t c_pid;
  char buf[READLINE_MAX_SIZE];
  int32_t logLine = 0;
  char* ptr;
  int32_t ret;
  int32_t status;
  int save_0 = -1;
  int32_t cpu_rate;
  char fields[12][128];
  int kill_flag = 1;
  int waitret;

  // Output CPU load
  cpu_rate = g_cpuloadRate1000 < 0 ? 0 : g_cpuloadRate1000;
  fprintf(wfp, "*CPU      %2d.%d%%\n", cpu_rate / 10, cpu_rate % 10);

  // Create pipe
  ret = pipe(pipe_fd);

  if (ret != 0) {  // LCOV_EXCL_BR_LINE 5: pipe error case
    // LCOV_EXCL_START 5: pipe error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "pipe Error");
    return -1;
    // LCOV_EXCL_STOP 5: pipe error case
  }

  // Create child process
  c_pid = fork();
  if (c_pid < 0) {  // LCOV_EXCL_BR_LINE 5: fork error case
    // LCOV_EXCL_START 5: fork error case
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "fork Error");
    close(pipe_fd[0]);
    close(pipe_fd[1]);

    return -1;
    // LCOV_EXCL_STOP 5: fork error case
  }

  if (c_pid == 0) {
    /*******************************************************
     * Child process
     *******************************************************/
    if (lower_sched_priority(CPULOAD_NICEVAL) == false) {  // LCOV_EXCL_BR_LINE 200: lower_sched_priority can't be false
      // LCOV_EXCL_START 200: lower_sched_priority can't be false
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to lower scheduler");
      exit(1);
      // LCOV_EXCL_STOP
    }
    close(pipe_fd[0]);     // Unneeded pipes (close stdin)

    close(1);              // Close stdout
    dup2(pipe_fd[1], 1);   // Duplicate stdout to pipe_fd[1]
    close(pipe_fd[1]);

    execl("/usr/bin/top", "top", "-n", "1", "-b", NULL);
    // error
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "execl top Error");
    exit(1);
  } else {
    close(pipe_fd[1]);      // Unneeded pipes(Close stdout)

    save_0 = dup(0);
    close(0);               // Close stdin
    dup2(pipe_fd[0], 0);    // Duplicate stdin to pipe_fd[0]
    close(pipe_fd[0]);

    {
      fd_set fds;
      int32_t maxFd;
      int ret;
      struct timeval tmout = { TOP_TIMEOUT, 0 };

      FD_ZERO(&fds);
      FD_SET(STDIN_FILENO, &fds);
      maxFd = STDIN_FILENO;
      ret = select(maxFd + 1, &fds, NULL, NULL, &tmout);
      if (ret < 0) {  // LCOV_EXCL_BR_LINE 5: select error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "[RESM]Handle Error errno:%d", errno);  // LCOV_EXCL_LINE 5: select error case
      } else if (FD_ISSET(STDIN_FILENO, &fds)) {  // LCOV_EXCL_BR_LINE 5: FD_ISSET's error case
        kill_flag = 0;
      } else {
        // LCOV_EXCL_START 5: FD_ISSET's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
               "[RESM]'top': No response during %d seconds", TOP_TIMEOUT);
        // LCOV_EXCL_STOP
      }
      if (kill_flag) {  // LCOV_EXCL_BR_LINE 200: kill_flag must be 0
        // LCOV_EXCL_START 200: kill_flag must be 0
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        // Kill top after TOP_TIMEOUT sec
        // (Killed by child process to avoid making resm process super-user with setuid.)
        if (kill(c_pid, SIGKILL) == -1) {
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__,
                 "Failed to kill(SIGKILL), pid=%d, errno=%d", (int) c_pid,
                 errno);
        }
        // LCOV_EXCL_STOP
      } else {
        while (fgets(buf, sizeof(buf), stdin) > 0) {
          if (logLine == 0) {
            if (buf[0] != 'C') {
              continue;
            }
            if (strstr(buf, "Cpu(s)") == NULL || strstr(buf, "sy") == NULL) {
              continue;
            }
            logLine++;
          } else if (logLine == 1) {
            ptr = strstr(buf, "PID");
            if (ptr == NULL) {
              continue;
            }
            logLine++;
          } else if (logLine < (DEBUG_INFO_CPU_TOP_LINES + 2)) {
            ret = sscanf(buf, "%128s %128s %128s %128s %128s %128s %128s %128s %128s %128s %128s %128s", fields[0],
                         fields[1], fields[2], fields[3], fields[4], fields[5],
                         fields[6], fields[7], fields[8], fields[9], fields[10],
                         fields[11]);
            fprintf(wfp, "%4s%%  %s\n", fields[8], fields[11]);
            logLine++;
          }
        }
        fprintf(wfp, "\n\n");
      }
    }
    ret = 0;
    if (kill_flag) {  // LCOV_EXCL_BR_LINE 200: kill_flag must be 0
      // LCOV_EXCL_START 200: kill_flag must be 0
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      const struct timespec delay = {1, 0};
      int i;
      for (i = 0; i < WAIT_RETRY; i++) {
        nanosleep(&delay, NULL);
        if ((waitret = waitpid(c_pid, &status, WNOHANG)) == -1) {
          ret = -1;
          FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to waitpid for top %d, errno=%d", c_pid, errno);
          break;
        } else if (waitret == c_pid) {
          FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "waitpid OK");
          break;
        }
      }
      if (i >= WAIT_RETRY) {
        ret = -1;
        FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "top Failed to exit, pid=%d", c_pid);
      }
      // LCOV_EXCL_STOP
    } else {
      if ((waitret = waitpid(static_cast<int>(c_pid), &status, 0)) < 0) {  // LCOV_EXCL_BR_LINE 5: waitpid's error case
        // LCOV_EXCL_START 5: waitpid's error case
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        ret = -1;
        FRAMEWORKUNIFIEDLOG(ZONE_RESM_DEBUG, __FUNCTION__, "waitpid(%d) Error errno(%d)", c_pid, errno);
        // LCOV_EXCL_STOP
      }
    }
    if (ret < 0) {  // LCOV_EXCL_BR_LINE 200: ret must be 0
      // LCOV_EXCL_START 200: ret must be 0
      AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
      FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "wait Error");
      if (save_0 >= 0) {
        close(save_0);
      }
      return -1;
      // LCOV_EXCL_STOP
    }
    if (save_0 >= 0) {
      dup2(save_0, 0);    // Reset the stdin to 0
      close(save_0);
    }
  }

  return 0;
}
// Get CMA information and Output work
static int write_cmainfo_work(FILE *wfp) {
  float total;
  float avail;
  float used;
  float min_remain;
  uint32_t used_rate;
  uint32_t used_letters;

  avail = static_cast<float>(cmaFree_kib);
  total = static_cast<float>(cmaTotal_kib);
  used = total - avail;
  min_remain = static_cast<float>(minRestCma);
  // "*CMA MEMORY   @@@@@@ Warning!! @@@@@"
  fprintf(wfp, "*CMA MEMORY");
  if (used * 5 > total * 4) {  // (4/5 Or more)
    fprintf(wfp, "   @@@@@@ Warning!! @@@@@\n");
  } else {
    fprintf(wfp, "   \n");
  }
  // "used/avail/total   xxx.xMB / xxx.xMB / xxx.xMB(xx.x%)
  used /= 1024;
  avail /= 1024;
  total /= 1024;
  min_remain /= 1024;
  if (total != 0) {
    used_rate = (uint32_t) (used * 1000 / total);
  } else {
    used_rate = 0;
  }
  if (used_rate >= 1000) {
    used_rate = 999;
  }
  fprintf(
    wfp,
    " used/avail/total/min remain   %5.1fMB / %5.1fMB / %5.1fMB(%2d.%d%%) / %5.1fMB\n",
    used, avail, total, used_rate / 10, used_rate % 10, min_remain);
  if (total == 0) {
    used_letters = 0;
  } else {
    used_letters = (uint32_t) (DEBUG_INFO_CMA_LETTERS * used / total);
    if (used_letters > DEBUG_INFO_CMA_LETTERS) {
      used_letters = DEBUG_INFO_CMA_LETTERS;
    }
  }
  // "------------------*******"
  int i;
  for (i = 0; i < static_cast<int>(used_letters); i++) {
    fprintf(wfp, "-");
  }
  for (; i < DEBUG_INFO_CMA_LETTERS; i++) {
    fprintf(wfp, "*");
  }
  fprintf(wfp, "\n\n");

  return 0;
}
// Write information and Output FRAMEWORKUNIFIEDLOG
static int write_resourcemanagerloginfo_work(void) {
  FILE *wfp;
  char l_read[READLINE_MAX_SIZE];
  int ret;

  wfp = fopen(DEBUG_INFO_FPATH, "r");
  if (wfp == NULL) {  // LCOV_EXCL_BR_LINE 5: fopen error case
    // LCOV_EXCL_START 5: fopen case error
    AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
    FRAMEWORKUNIFIEDLOG(ZONE_ERR, __FUNCTION__, "Failed to open %s, errno=%d",
           DEBUG_INFO_FPATH,
           errno);
    return -1;
    // LCOV_EXCL_STOP 5: fopen case error
  }
  while (1) {
    if (fgets(l_read, READLINE_MAX_SIZE, wfp) == NULL) {
      ret = feof(wfp);
      if (ret == 0) {  // LCOV_EXCL_BR_LINE 5: feof case error
        // LCOV_EXCL_START 5: feof case error
        AGL_ASSERT_NOT_TESTED();  // LCOV_EXCL_LINE 200: test assert
        FRAMEWORKUNIFIEDLOG(ZONE_PERFORMANCE, __FUNCTION__, "Failed to fgets %s",
               DEBUG_INFO_FPATH);
        // LCOV_EXCL_STOP 5: feof case error
      }
      break;
    } else {
      char *line;
      line = strchr(l_read, '\n');
      if (line != NULL) {
        *line = '\0';
      }
      if (l_read[0] != '\0') {
        FRAMEWORKUNIFIEDLOG(ZONE_PERFORMANCE, __FUNCTION__, "%s", l_read);
      }
    }
  }
  fclose(wfp);
  return 0;
}