• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 #include <gtest/gtest.h>
16 #include <cinttypes>
17 #include <climits>
18 #include <cstdio>
19 #include <unistd.h>
20 #include <string>
21 
22 const int CLUSTER_SIZE = 16;
23 const int SIZE_ARR_SIZE = 2048;
24 const int NANOSEC_PER_SEC = 1e9;
25 const int MALLOC_TIME = 131072;
26 
27 using namespace testing::ext;
28 using namespace std;
29 
30 class MallocFreePerformance : public testing::Test {
31 public:
32 
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp();
36     void TearDown();
37 private:
38 };
39 
SetUp()40 void MallocFreePerformance::SetUp()
41 {
42 }
TearDown()43 void MallocFreePerformance::TearDown()
44 {
45 }
SetUpTestCase()46 void MallocFreePerformance::SetUpTestCase()
47 {
48 }
TearDownTestCase()49 void MallocFreePerformance::TearDownTestCase()
50 {
51 }
52 
53 /* A random generated size array for performance test
54  * The sizes array is roughly uniformed distributed in every bin
55  */
56 size_t g_sizes[SIZE_ARR_SIZE] = {
57  70398,   5870,    812,    782, 132092,     44,  10608,    814,   4523,    140,   3989,   2872,  71794,   1503,   3682,
58 199068,     75,  42231,    682,    622, 154292, 130053,    237,    839,    324,  99182, 140794,   4150,    969,   1080,
59   4392,   2978,    202,    174,   4236,  11422,   9935,    449, 164211,   1647,    264,  85939, 125428,  68810,   7330,
60   1077,  50947, 224883,   1211,    775,    899,  13313,  13711, 153171,   2629,  29524, 121541,  22415,    462,    547,
61     39,    422,  52062,  36162,    517,   1259,    585,  46209,    332,   9488,    555,    940,    395,    108,  70242,
62    652,    165,  24921,  10310,    611,  30317,   7061,     43,    170,    553, 127383,    585,  15952,   4790,  82906,
63    235,    836,  12391,    267,  28743,    802,    455,   7593,   1992,    865,   1775,    750,  14738,    617,  81735,
64  66408,    802,    388,    999,   2000,  18975,  67893,      3,    451,    616,    778, 239766,   6633,  45054,    197,
65    303,  12049,    555,   6787,    429,     68,    333, 132413,    937,  12291,  30571,   3302,  44731,   1402,    460,
66    772,   3274,  99137, 220195,   3751,  28575,    299,   1893,    229,    487,  21691, 154058,  40029,   4335,    910,
67 214958,    485,  88161,    900,  14042,    107,  11356,    906,    553,    559,    847,   1593,    815,    271,   8454,
68  95479,    814,    486,    556,    325,  13687,    995,    805,   8407,   1179,    332, 244198,    525,    105,    838,
69 204603,    841,    874,   6653,    647,   9133,     79,    963,    259,    193,    238,   1626, 184019,    968,    360,
70  20162,    388,   1368,  31575,    674,      1,    303,    418,    995,  23078, 183823,  56425, 208634,    577,  36907,
71    705,   3981,  12763,    328,    621,   4939,   1340,    742,    805,    137,    867,    109,   1466,    205,   1002,
72    516,   1007,    742, 258449,  15950,     38,   3818,    295,    552,  39173,  12387,    711,   1376,  29945,   7137,
73   1621,    236,  27868,    452,     46,    171,    683,   2843,   6833,     43,    356,  11609,   1305,    805,  53323,
74   3832, 198683,    808,    429,   1895,   2713,    558,    654,    813,   9032,    905,    333,    773,    781, 247876,
75  47654,   3777,    686,   2506,   5770,  22422,  15557,    292,  79991,     47, 137903,    879,   3736,  20517,   2879,
76    460,    749,    423,  61714,    493,  95574,  13594,     69,  10209,    806,    975,    357,    905,   9232,  27054,
77 235843,   7299,  56998,    106,   7697,    422,    577,    428,  32538,    455,   4304,   1942,   2946,   3531,    424,
78   1834,    780,   2013,    228, 159815,  79256,    751,    204,    973,    965,    194,    776,  98967,   5460,    462,
79  70645,    111,      6,   1003,    389, 101973,   3200,    463,  43516,   3029,    783,    837,    683,  11671,  33303,
80  47768,  10458, 179347,    973,  38940,  75350,  14263, 261886,    333,   4954,    300,    907,  17111,  68577,  19266,
81    782,    802,    937,  26320,  28570,   2830,    686,    292,    484,    334,    143,  64594,  54629,    552,    137,
82    871,   3248,   1684,    327, 203105, 128726,   3817,  55839,   3382,    163, 151956,     98,   5776,    841,   4906,
83 138722, 130420,  57664,   3453,  20918, 166702,    260, 226203,    715,    134,    655,   2196,  33196,    515,    233,
84  11594, 116927,   3203,    164,  30898,  17118,   1816,    715,  58259,    173,    226, 122801,  17628,  80971, 217244,
85  83675,    103,    616,   3842,  47968,    452,    385,    975,    617,    449,    514,  27115,      3, 117059,   2465,
86  64951,   2461,    588,   2003,    482,    682, 216424,    488,    101,  20908, 134882,   3347,    388,    547,    975,
87      5,    616,    513,    876,   1001,    389,    259,    684,   3288, 106796,   5408,    769,   2984,  45442,   8939,
88   9359,   5672,    553,   2258,   9126,    738,  67280,     65,  53874,   3215, 246562,   3820,    996, 156079,    101,
89   4231,    651,  12558,    324,    577,    716,  16016,    269,    934,    841,  14866,    609,  21962,   1920,    267,
90    674,    676,     70,    488,    707,     99,    322, 129879,   2967,    550,    903,    810,    741,   1739,  76817,
91   1514,    875,  75663, 102461,   5887,   5522, 171815,   1220,    206,    559,    134,    744,    900,   5656,   6482,
92   5913,  15317,     69,   1500,  14341,    423,  78476,    362,    107,    939,   4963,     47, 240406,  14778,    903,
93    330,   2130,    323,   1320,   2378,  64207,   4861, 160245,    231,    171,    609,    161,  11773,  22211,  10458,
94  47327,    647,  23595,     41,    333,   1610,    709,  18849, 195435,    558,    866,  18759,  72108,   3244, 102716,
95  73862,    353,    302,   6803,    455,  15788,  53140, 109524,    777,    969, 113077,    454,    875,    235,  23856,
96    644,  21337,  45424,    873,    161,    652,    807,  99721,  27553,    357, 190179,    330,  14428,    589,   3347,
97  65978,   9364,    835,   2002,     47,    995,    930,   1253,    387,   6780,   1998,    905,  29723,    610,     79,
98    290,  37169,   1971,    743,    172,  14856,  11888,    943,   1859,   1208,     40,  47201,  23335,   5233,  12980,
99 171863,   3036,   9107,     45,    490,    869,   6588,    333,    746,  26871, 118844, 127074,    551,    813,  20159,
100   3136, 197941,    996,    654,  12931,   3141,   1300,    610,    804,    805,   1345, 197261,    168,    263,    362,
101   3293,   3166, 178245,    521,   3116,  43376,    262,    933,     72,    170,     34,  19267,    104,    524,    649,
102    139,    387,   1138,  16827,    911,  13562,   1722,   4281,    591,  52262,    193, 238772,  70881,   8430,    453,
103   1945, 126322, 164070,  97918,    354,     98,   3662,  22224,   3309, 102266,    877,    645,  59699,    931,    648,
104   2134, 214595,   4001, 129563,   5792,  52854,     13,    611,   9275,  45407,    679,   4802,   7668,  53087,     97,
105  30461,     73,    203,  14303,    324,    261,    140,    940, 100389,    518,  18613, 253547,    519,  71172,  26630,
106    551,   1739,    769, 167627,   6280,    931,    229, 108315,  89014,  12596,   1953,  19134,  57947,    427,    206,
107   6173,   3532,   8619,  38561,    138,  22338,    519,    106,    932, 259336,    232,   5748,    207,  61851,    487,
108    616,    680,    163,  64600,   7255,    780,    936,    647,  33859,    942, 136598,    708,    866,    196,  17475,
109    524,    492, 182291,   3859,    484,  16731,  13992,    513,  10509,  40595,  77200,  15873,    715,   8122,    929,
110  31017,    962,    616,    739,    517,    397,    452,    425, 146686,   2005,    173,   1348,   5228,      4,    652,
111  14700,    359,    321,   5388,    778,    271,  84182,    966,  34596,     35,    356,  28288, 121659,   1796,  17260,
112    808, 243982,  12292,     75,   7399,    548, 217421,    289,    559,  50038,    673,    769,  10769,   1934,    488,
113    298,  36678,    389,    581,  89654,   4883,  18525,    676,     65,  55459,  74196, 109744,    623,   2527,  94923,
114    328,    900,    328,   1101,  64821,    801,   2197,    201,     68,   1004,     72,    143,   4917,   2409,    449,
115      8,   1000,    745,    201,    195,    546,    930,   5793,  15314,  11095,   2756,   3127,   8121,     38,    870,
116  11231,   3838,  74228,  63376,  56633, 159022,    942, 174037, 182329,  30664, 150490,    801,     40,  24289,    675,
117    547,   4046,    481,  61835,    964,    133,  45929,  35021,   4224,   1000,    910,  30014, 216676, 107110,  59468,
118 127074,    586,     67,     68,   4344,   3758, 129795,    426,    458,   1466,    556,    295,  26140,  98849,  56438,
119 161433, 108530,    649,  16593,  54624,  58346,    131,  83535,   1549,    227,  45126,     15,    967,    196,   1031,
120    289,  12894,   7347,     11,    808,  29130,    619,    101,    264,    652,   6118,   5445,    270,    938,   1001,
121    398,    810,   9316, 194708,    993,   1902,   3191,    293,     98,    527,   1645,   1512,   1988,    586,  76639,
122 182172,    804, 254145,    866,  30406,    588,    418,    428,   1594,   5151,   4881,   8454,    714,  29323,    515,
123    806,  20037,   2379,  77376,   7505, 122331,     67,  46036,   1000,   1046,    333,    621,    582,    207,    555,
124  23285,   1917,    937,    519,    267,     35,  77624,    358,    587,    162,  29879, 141182,    194,  20079,    654,
125   1717,  10064,   3432,    462,   1001,  23544,    804,   1872,    710,    494,   4163,   8624,    325,  29374, 237775,
126 161384,    392,   3701,  27911,   4655,    361,  24420,     78,    300,    321,  36528,    876,    739,    620,   1149,
127    550,  30840,    164,    484,    525,    292,    520,      2,    485,   6254,   1723,    394, 260413,   1940,  11780,
128   2444,    993,    869,  32460,    942,   1384,   1004,   1479,     99,   5976,    335,    389,    299,  90270,    962,
129  15520,   2144,    869,  32604,   5137,    549,  63247, 122549,    557,    303,   8963,    206,   6963,    130, 123812,
130    332,   3419,  74006,  12617,    484, 163211,   1551,    545,    395,    911,    939,   5195,     42,  26251,  80176,
131   4982,    746,  24097,    490, 124671,  70629, 236138,   7829,  79143,  32527,    430,    929,  29291,    335,   2397,
132    589,    166,    868,  66068,    709,    298,   8929,   5659,    778,    484,  53316,    872,    591,  27338,  29233,
133   1002,   1914,  13784,    878,   4677,    870,      7,  93312,    966,    334,    931,    291,    680,    229,  91335,
134    974,      1, 163843, 232509,    719,    295,    815,  66563,  25897,   2112,    137, 206975, 122126,   5636,    840,
135 178041, 121125,  96929,  12271,    525,   1194,  39935,   1812,    359,     70,    586,  28109,    655,   1823,  15971,
136   1165,  50539,   7517,    613,   4334,    869, 143267,     33, 120769,  41743, 181472,    136,    775,    898,    687,
137  87489,  81862,    526,   1041, 119803,    203,    556,   2830,   1753,    995,    968,   1000,    362, 105558,  25289,
138  30313,    167,    897,    483,  87996,    131,   1286,     73,   4778,   3765, 162611,   2258,   4071,    577,    740,
139  19328,  23181,  44833,    356,    300,    481,    101,   1945,  55445,   7862,    586,   1690,    108,   3179,   6265,
140    833,    232,    513,    774,     38,   1006,  16313,      2,   5722,    929,    648, 124091,   3128,     12,    653,
141    289,    168, 113025,    394,  22275,  36138,    934,  33296,  44428,    648,  23347,     36,    966,   7656,  30616,
142    303,  12065,    395,   2175, 110915,   3400,   3985,    547,   1457,  77770,    775,    942,    677,     47,    845,
143 261480,  20514,  26542,    644,    709,   8629, 160291,     43,  12951,    613,    771,    490,    172,  23687,   2678,
144 112194,  15152,    583,   6109,  76361,  48932, 138769,  68824, 256665,   1214,    427, 197349,  11553,     35,    292,
145   7826,    386,  15287, 143431,    139,    742,    783,   7933,    550,  61036, 153864,   5527,   1220, 179326,  52538,
146    297,    331, 116679,  32116,    548,   7180,  24432, 150458,    647,    198, 230679,    492, 183327,     10,    582,
147     46,    742,  19932,    139,    333,    838, 190324,  82554,   3673,    263,    810,  20105,  18894,    801,   3902,
148   3452,    203,  30556,    521,    805,    399,    491, 261614,    132,    745,    646,    621,    815,  21428,  32822,
149    941,     35,    775,    803,   9277,     77,   3531,     73,    877,    353,    582,   1561,   1794,   6272,  14250,
150  10258,  87923,  16317,   1574, 107483, 118292,    230,    492,    193,    522,    903,  21672,    259, 101987,   5379,
151    547,  42742,    131,    909,  11141,  45670,    650,    327,    203,    417,   1822,  73206,   4047,   5651,  31863,
152    364,   6056,    482,   2414,    686,     67, 116497,    233,  89151,    712,   1960,  15023,    457,    911,  87709,
153    326,    647,    131,   1544,    493, 103295,     35,   4201, 120600,    679,   4536,    354,    649,  12877,    514,
154     11,   7599,    492,  66894,    779,   5012,    803,    270,  27587,   1716,    653,     15,   1745,  81311,    142,
155  74662,   2476,    847,    621,   2326,    842, 183990,  48969,  37165,    258,  15920,   1450, 139424,     97,    622,
156 217730,    389,  14629,    682,  82614,  10637,    170,    205,  16924,   1684,    525,    675,      2,  39617,    237,
157    324,    623,   3998, 171709,    130,  44293,    301,    900,  36268,    297,    130,   3905,    364,   8500,   6175,
158    267,  74017,    387,    963,    869,      0,    653,    324,    813, 138388,    673, 132383,    835,   2206,    259,
159  87218,     99,   7984,    521,    808,   9500, 126004,   1614,  20344,    142,    972,   9619,    684,   1709,    430,
160    260, 112449,  73506,    867,  15030,  37867,    417,    874,  43752,   7460,  63448,    582,   1796,   8038,   1495,
161  19720, 234481,  43216,    554,     33,    137,    908,   2959,     11,    106,    642,  37055,    973,    495,    969,
162     73,    836,  84397,  94851,   1000,    354,  18329,  38597,    970,  16503,   1215,    900,   9823,   9582,    132,
163   8123,    801,    236,    642,    929, 120593,    713,    486,     34, 241685,    492,  49871,  30206,   2905,    936,
164   6818,  14692,    461,    714,  70500,    718,    868,    996,  65063,    737,    559,    677,    555,   6234,   5245,
165   1136,    553,    751,    236,     67,   1167,  28052,    199,   2634,  22830,  38653,   1874,    366,    579,   7451,
166 194153,    488,    907,  13417,    616,   1593,    293,  14217,   3553,    419,    492,    835,    334,   3730,    875,
167    775,    969,   3959,    430,    930,    167,    616,    651,    968,    644,    556,   8349,    710,    551,    647,
168    527,    170,    652,   5834,     43,     78,    205,    678,    748,    650,    998,    495,    556,    108,   1917,
169    484,    111,   1540,     11,  86819,    547,    267,    527,  21645,    549,    611,   7878, 163845,    269,   1004,
170    750,    994,    101,  28372,    386,    173,   3086,  48514,   8148,    961,   9005,  12271,    619,    641,   7038,
171    263,   1768,    910,   6053,   6276,  88873,    623, 170360,    685,  16800,   2299,   2431,  35377, 147880,    643,
172 128403,   5086,     97,    131,    583,   1690,    939,   6601, 221042,     79,  30789,    357,  87455,    522,    239,
173    904,      5,   1174,    293,  18785,    587,   3587,    238,    335,    581,  36976,  40494,    933, 198961,   1719,
174    554,    267,    132,    674,    421,   5792,    745,    492,   2013,   2290,   3834, 106626,    773,    270,    175,
175    901,   6109,   4583,    422, 127089,     73,  88055,  43610,  72305,   1878,    425,  44179,   2765,   1200,   2753,
176    749, 150927,   3509,  14040,   1549,    424,    454, 219113,    491,    910,    622, 160490,  21886,    715,    457,
177  63071, 178200,   3760,    770,    705, 255855,   1254,   5798,    527,    676,   7920,    716,  17543,    294,    813,
178     68, 221793,    100,   1636, 106218,    747,    877,    193,    686, 203251,    100,    587,    554,   1477,    335,
179  31222,    324,    398,    711,   5926,   4036,    326,  60222,    233,   3716,  65404,  16007,    142,    419,    874,
180   1001,   1829, 253309,  59056,    973,   3390,   1432,    236,     15,   2726,    642,    617,   3782,    135, 186049,
181    359,  14274,    193,    972,    105,    934,     47,    810,    746,    744,     41,    813,  16071,   8974,  36929,
182    610,    169,  12477,  13763,    259,    259,    845,   2367,    549,    430,  70141,    685,   1962,    580,  68783,
183  66713,    196,    387,    292,      1,   9189,  61834,    558, 107624,    750,    367,    678,   1327,   2686,    229,
184  91802,   4874, 173587,  31323,  13180,    740, 124601,   1433,   7331,  15637,   1713,    579,    876,  16730,   3138,
185    936,  61890,    993,  91003, 102014,    195,     78,   1178,    463, 221454,    451,  59083,    742,    615,    586,
186 255579,    774,  32922,   6243,    325,    675,  44268,    553, 112234,   1506,    903,    778,    492,   3168,  63549,
187    879,   4683,   8721,   7697,    327, 105304,    164,  50339,    171,  55374,   5043,    705,   3741,  16724,    271,
188    196,    932,    771,    391,    999,  85846,  51146,    257,    686,    937,  41264,   1003,    197,  20528,    614,
189    929,    965,    299,    109,  84938,   7700, 126923,  24435,    460,    551,    591,  18712,    589,  57468,   8396,
190 172592,   2621,    105,    557,    101,   4858, 187139,    520,   3930,  47360,    296, 259193,  88797,  18147,    642,
191 122056,   3308,  11404,    452,  20060,    334,      7, 140626,  52316,  13883,    868,  24962,  19392,   7411,    901,
192 191194,    581,  78399,    386, 200180,    774,    684,    514,   5030,  33323,  21820,    421,   7067,    484,    962,
193    683,     75, 209451,   3966,     38,    237,  13130,  23696
194 };
195 
196 /*
197  * @tc.number CAM_FREE_FUN_0100
198  * @tc.name Apply part of the memory camfree reduction
199  * @tc.desc Test the basic features of CamFree
200 */
201 HWTEST_F(MallocFreePerformance, freeperformance0100, Function | MediumTest | Level1)
202 {
203     cpu_set_t mask;
204     void *ptr[CLUSTER_SIZE] = {0};
205     struct timespec ts[2];
206 
207     CPU_ZERO(&mask);
208     CPU_SET(0, &mask);
209     if (sched_setaffinity(0, sizeof(mask), &mask) < 0) {
210         printf("Set CPU affinity failure, ERROR:%s\n", strerror(errno));
211     }
212 
213     clock_gettime(CLOCK_REALTIME, ts);
214     size_t index = 0;
215     for (int i = 0; i < MALLOC_TIME / CLUSTER_SIZE; ++i) {
216         for (int j = 0; j < CLUSTER_SIZE; ++j) {
217             ptr[j] = malloc(g_sizes[index]);
218             if (!ptr[j]) {
219                 printf("Malloc size of %zu byte(s) failed: %s\n", g_sizes[index], strerror(errno));
220             }
221             ++index;
222             index %= SIZE_ARR_SIZE;
223         }
224         for (int j = 0; j < CLUSTER_SIZE; ++j) {
225             free(ptr[j]);
226         }
227     }
228     clock_gettime(CLOCK_REALTIME, ts + 1);
229 
230     double cost = (ts[1].tv_sec - ts[0].tv_sec) * NANOSEC_PER_SEC +
231         (ts[1].tv_nsec - ts[0].tv_nsec);
232 
233     printf("Malloc and free %d times cost %lf s\n", MALLOC_TIME, cost / NANOSEC_PER_SEC);
234     double gettime = cost / NANOSEC_PER_SEC;
235     ASSERT_TRUE(gettime <= 1) << "Performance not up to standard";
236 }
237 
238