1 /*
2 * Copyright (c) 2025 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 "common_components/common/run_type.h"
16
17 namespace common {
18 // REMEMBER TO CHANGE NUM_OF_RUN_TYPES WHEN YOU ADD/REMOVE CONFIGS
19 // this stores a config for each kind of run (represented by an index)
20 const RunType RunType::TYPES[NUM_OF_RUN_TYPES] = {
21 { true, DEFAULT_PAGE_PER_RUN, 16 },
22
23 { true, DEFAULT_PAGE_PER_RUN, 24 },
24 { true, DEFAULT_PAGE_PER_RUN, 32 },
25 { true, DEFAULT_PAGE_PER_RUN, 40 },
26 { true, DEFAULT_PAGE_PER_RUN, 48 },
27 { true, DEFAULT_PAGE_PER_RUN, 56 },
28 { true, DEFAULT_PAGE_PER_RUN, 64 },
29 { true, DEFAULT_PAGE_PER_RUN, 72 },
30 { true, DEFAULT_PAGE_PER_RUN, 80 },
31 { true, DEFAULT_PAGE_PER_RUN, 88 },
32 { true, DEFAULT_PAGE_PER_RUN, 96 },
33 // all sizes smaller than RUN_ALLOC_SMALL_SIZE may use cache run to speed up allocation.
34 // all sizes smaller than RUN_ALLOC_SMALL_SIZE must be increased by 8 bytes.
35 // (these are optimisations, rather than restrictions.)
36 { true, DEFAULT_PAGE_PER_RUN, RUN_ALLOC_SMALL_SIZE },
37
38 { false, DEFAULT_PAGE_PER_RUN, 112 },
39 { false, DEFAULT_PAGE_PER_RUN, 120 },
40 { false, DEFAULT_PAGE_PER_RUN, 128 },
41 { false, DEFAULT_PAGE_PER_RUN, 136 },
42 { false, DEFAULT_PAGE_PER_RUN, 144 },
43 { false, DEFAULT_PAGE_PER_RUN, 152 },
44 { false, DEFAULT_PAGE_PER_RUN, 160 },
45
46 { false, DEFAULT_PAGE_PER_RUN, 168 },
47 { false, DEFAULT_PAGE_PER_RUN, 176 },
48
49 { false, DEFAULT_PAGE_PER_RUN, 184 },
50 { false, DEFAULT_PAGE_PER_RUN, 192 },
51 { false, DEFAULT_PAGE_PER_RUN, 200 },
52 { false, DEFAULT_PAGE_PER_RUN, 208 },
53 { false, DEFAULT_PAGE_PER_RUN, 216 },
54 { false, DEFAULT_PAGE_PER_RUN, 224 },
55 { false, DEFAULT_PAGE_PER_RUN, 232 },
56 { false, DEFAULT_PAGE_PER_RUN, 240 },
57 { false, DEFAULT_PAGE_PER_RUN, 248 },
58 { false, DEFAULT_PAGE_PER_RUN, 256 },
59 { false, DEFAULT_PAGE_PER_RUN, 264 },
60 { false, DEFAULT_PAGE_PER_RUN, 272 },
61 { false, DEFAULT_PAGE_PER_RUN, 280 },
62 { false, DEFAULT_PAGE_PER_RUN, 288 },
63 { false, DEFAULT_PAGE_PER_RUN, 296 },
64 { false, DEFAULT_PAGE_PER_RUN, 304 },
65 { false, DEFAULT_PAGE_PER_RUN, 312 },
66 { false, DEFAULT_PAGE_PER_RUN, 320 },
67 { false, DEFAULT_PAGE_PER_RUN, 328 },
68 { false, DEFAULT_PAGE_PER_RUN, 336 },
69 { false, DEFAULT_PAGE_PER_RUN, 344 },
70 { false, DEFAULT_PAGE_PER_RUN, 352 },
71 { false, DEFAULT_PAGE_PER_RUN, 360 },
72
73 { false, DEFAULT_PAGE_PER_RUN, 400 },
74 { false, DEFAULT_PAGE_PER_RUN, 448 },
75 { false, DEFAULT_PAGE_PER_RUN, 504 },
76 { false, DEFAULT_PAGE_PER_RUN, 576 },
77 { false, DEFAULT_PAGE_PER_RUN, 672 },
78 { false, DEFAULT_PAGE_PER_RUN, 800 },
79 { false, DEFAULT_PAGE_PER_RUN, 1008 },
80 { false, DEFAULT_PAGE_PER_RUN, 1344 },
81 { false, DEFAULT_PAGE_PER_RUN, RUN_ALLOC_LARGE_SIZE }
82 };
83
84 // this map maps a size ((size >> 3 - 1) to be precise) to a run config
85 // this map takes 4 * MAX_NUM_OF_RUN_TYPES == 1k
86 uint32_t RunType::g_size2Idx[MAX_NUM_OF_RUN_TYPES] = { 0 }; // all zero-initialised
87
88 // this function inits the config map using the configs above
89 // so that for any size there is a config for it
90 // if the size doesn't match any of the configs exactly, we choose
91 // the closest config with a greater size, e.g.,
92 // g_size2Idx[456 >> 3 - 1] == g_size2Idx[464 >> 3 - 1] == .. == g_size2Idx[504 >> 3 - 1]
InitRunTypeMap()93 void RunType::InitRunTypeMap()
94 {
95 constexpr uint32_t runSizeShift = 3;
96 ASSERT_LOGF(RunType::NUM_OF_RUN_TYPES <= RunType::MAX_NUM_OF_RUN_TYPES, "too many configs");
97 uint32_t idx = RunType::NUM_OF_RUN_TYPES;
98 uint32_t nextSize = RunType::TYPES[RunType::NUM_OF_RUN_TYPES - 1].size;
99 ASSERT_LOGF(nextSize <= (RunType::MAX_NUM_OF_RUN_TYPES << runSizeShift), "size too big in config");
100 uint32_t i = (RunType::MAX_NUM_OF_RUN_TYPES - 1);
101 while (true) {
102 if (((i + 1) << runSizeShift) > nextSize) {
103 if (idx < RunType::NUM_OF_RUN_TYPES) {
104 RunType::g_size2Idx[i] = idx;
105 }
106 } else {
107 ASSERT_LOGF(((i + 1) << runSizeShift) == nextSize, "init run config error");
108 ASSERT_LOGF(idx > 0, "init run config error");
109 RunType::g_size2Idx[i] = --idx;
110 if (idx > 0 && idx < RunType::NUM_OF_RUN_TYPES) {
111 ASSERT_LOGF(static_cast<size_t>(RunType::TYPES[idx - 1].size) < nextSize, "not in ascending order");
112 nextSize = RunType::TYPES[idx - 1].size;
113 } else {
114 nextSize = 0;
115 }
116 }
117
118 if (i == 0) {
119 break;
120 } else {
121 --i;
122 }
123 }
124
125 ASSERT_LOGF(RUNTYPE_SIZE_TO_RUN_IDX(RUN_ALLOC_LARGE_SIZE) + 1 == RunType::NUM_OF_RUN_TYPES,
126 "run config inconsistent: large size");
127 }
128 } // namespace common
129