1 | /* |
---|
2 | * dqdt.c - Distributed Quaternary Decision Tree implementation. |
---|
3 | * |
---|
4 | * Author : Alain Greiner (2016,2017,2018,2019) |
---|
5 | * |
---|
6 | * Copyright (c) UPMC Sorbonne Universites |
---|
7 | * |
---|
8 | * This file is part of ALMOS-MKH. |
---|
9 | * |
---|
10 | * ALMOS-MKH is free software; you can redistribute it and/or modify it |
---|
11 | * under the terms of the GNU General Public License as published by |
---|
12 | * the Free Software Foundation; version 2.0 of the License. |
---|
13 | * |
---|
14 | * ALMOS-MKH is distributed in the hope that it will be useful, but |
---|
15 | * WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
---|
17 | * General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with ALMOS-MKH; if not, write to the Free Software Foundation, |
---|
21 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
---|
22 | */ |
---|
23 | |
---|
24 | #include <kernel_config.h> |
---|
25 | #include <hal_kernel_types.h> |
---|
26 | #include <hal_special.h> |
---|
27 | #include <hal_macros.h> |
---|
28 | #include <hal_atomic.h> |
---|
29 | #include <hal_remote.h> |
---|
30 | #include <thread.h> |
---|
31 | #include <printk.h> |
---|
32 | #include <chdev.h> |
---|
33 | #include <cluster.h> |
---|
34 | #include <bits.h> |
---|
35 | #include <dqdt.h> |
---|
36 | |
---|
37 | |
---|
38 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
39 | // Extern variables |
---|
40 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
41 | |
---|
42 | extern chdev_directory_t chdev_dir; // defined in chdev.h / allocated in kernel_init.c |
---|
43 | |
---|
44 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
45 | // This static recursive function traverse the DQDT quad-tree from root to bottom. |
---|
46 | /////////////////////////////////////////////////////////////////////////////////////////// |
---|
47 | static void dqdt_recursive_print( xptr_t node_xp ) |
---|
48 | { |
---|
49 | uint32_t x; |
---|
50 | uint32_t y; |
---|
51 | dqdt_node_t node; |
---|
52 | |
---|
53 | // get node local copy |
---|
54 | hal_remote_memcpy( XPTR( local_cxy , &node ), node_xp , sizeof(dqdt_node_t) ); |
---|
55 | |
---|
56 | // display node content |
---|
57 | nolock_printk("- [%d,%x] : threads %x / pages %x / clusters %d / cores %d / parent_cxy %x\n", |
---|
58 | node.level, GET_CXY( node_xp ), |
---|
59 | node.threads, node.pages, |
---|
60 | node.clusters, node.cores, |
---|
61 | GET_CXY( node.parent ) ); |
---|
62 | |
---|
63 | // recursive call on children if node is not terminal |
---|
64 | if ( node.level > 0 ) |
---|
65 | { |
---|
66 | for ( x = 0 ; x < 2 ; x++ ) |
---|
67 | { |
---|
68 | for ( y = 0 ; y < 2 ; y++ ) |
---|
69 | { |
---|
70 | xptr_t iter_xp = node.children[x][y]; |
---|
71 | if ( iter_xp != XPTR_NULL ) dqdt_recursive_print( iter_xp ); |
---|
72 | } |
---|
73 | } |
---|
74 | } |
---|
75 | } |
---|
76 | |
---|
77 | ///////////////////////// |
---|
78 | void dqdt_display( void ) |
---|
79 | { |
---|
80 | // get extended pointer on DQDT root node |
---|
81 | cluster_t * cluster = &cluster_manager; |
---|
82 | xptr_t root_xp = cluster->dqdt_root_xp; |
---|
83 | |
---|
84 | // get pointers on TXT0 chdev |
---|
85 | xptr_t txt0_xp = chdev_dir.txt_tx[0]; |
---|
86 | cxy_t txt0_cxy = GET_CXY( txt0_xp ); |
---|
87 | chdev_t * txt0_ptr = GET_PTR( txt0_xp ); |
---|
88 | |
---|
89 | // get extended pointer on remote TXT0 lock |
---|
90 | xptr_t lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock ); |
---|
91 | |
---|
92 | // get TXT0 lock |
---|
93 | remote_busylock_acquire( lock_xp ); |
---|
94 | |
---|
95 | // print header |
---|
96 | nolock_printk("\n***** DQDT state\n\n"); |
---|
97 | |
---|
98 | // call recursive function |
---|
99 | dqdt_recursive_print( root_xp ); |
---|
100 | |
---|
101 | // release TXT0 lock |
---|
102 | remote_busylock_release( lock_xp ); |
---|
103 | } |
---|
104 | |
---|
105 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
106 | // This static function initializes recursively, from top to bottom, the quad-tree |
---|
107 | // infrastructure. The DQDT nodes are allocated as global variables in each local |
---|
108 | // cluster manager. At each level in the quad-tree, this function initializes the |
---|
109 | // node identified by the <cxy> and <level> arguments, selects in each child |
---|
110 | // macro-cluster the precise cluster where will be placed the subtree root node, |
---|
111 | // and call recursively itself to initialize the child node in the selected cluster. |
---|
112 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
113 | // @ node cxy : cluster containing the node to initialize |
---|
114 | // @ level : level of node to be initialised |
---|
115 | // @ parent_xp : extended pointer on the parent node |
---|
116 | /////////////////////////////////////////////////////////////////////////////////////// |
---|
117 | static void dqdt_recursive_build( cxy_t node_cxy, |
---|
118 | uint32_t level, |
---|
119 | xptr_t parent_xp ) |
---|
120 | { |
---|
121 | assert( (level <= 5) , __FUNCTION__, "illegal DQDT level %d\n", level ); |
---|
122 | |
---|
123 | uint32_t node_x; // node X coordinate |
---|
124 | uint32_t node_y; // node Y coordinate |
---|
125 | uint32_t mask; // to compute associated macro-cluster coordinates |
---|
126 | uint32_t node_base_x; // associated macro_cluster X coordinate |
---|
127 | uint32_t node_base_y; // associated macro_cluster y coordinate |
---|
128 | uint32_t half; // associated macro-cluster half size |
---|
129 | uint32_t cores; // number of cores in macro cluster |
---|
130 | uint32_t clusters; // number of clusters in macro cluster |
---|
131 | |
---|
132 | // get node cluster coordinates |
---|
133 | node_x = HAL_X_FROM_CXY( node_cxy ); |
---|
134 | node_y = HAL_Y_FROM_CXY( node_cxy ); |
---|
135 | |
---|
136 | // get macro-cluster mask and half-size |
---|
137 | mask = (1 << level) - 1; |
---|
138 | half = (level > 0) ? (1 << (level - 1)) : 0; |
---|
139 | |
---|
140 | // get macro-cluster coordinates |
---|
141 | node_base_x = node_x & ~mask; |
---|
142 | node_base_y = node_y & ~mask; |
---|
143 | |
---|
144 | // get pointer on local cluster manager |
---|
145 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
146 | |
---|
147 | // build local and extended pointer on node to be initialized |
---|
148 | dqdt_node_t * node_ptr = &cluster->dqdt_tbl[level]; |
---|
149 | xptr_t node_xp = XPTR( node_cxy , node_ptr ); |
---|
150 | |
---|
151 | #if DEBUG_DQDT_INIT |
---|
152 | printk("\n[%s] thread[%x,%x] : cxy(%d,%d) / level %d / mask %x / half %d / ptr %x\n", |
---|
153 | __FUNCTION__, node_x, node_y, level, mask, half, node_ptr ); |
---|
154 | #endif |
---|
155 | |
---|
156 | // make remote node default initialisation |
---|
157 | hal_remote_memset( node_xp , 0 , sizeof( dqdt_node_t ) ); |
---|
158 | |
---|
159 | // initialize <parent> field |
---|
160 | hal_remote_s64( XPTR( node_cxy , &node_ptr->parent ) , parent_xp ); |
---|
161 | |
---|
162 | // initialize <level> field |
---|
163 | hal_remote_s32( XPTR( node_cxy , &node_ptr->level ) , level ); |
---|
164 | |
---|
165 | // recursive initialisation |
---|
166 | if( level == 0 ) // terminal case : cluster |
---|
167 | { |
---|
168 | // initialize <clusters> field in node |
---|
169 | hal_remote_s32( XPTR( node_cxy , &node_ptr->clusters ) , 1 ); |
---|
170 | |
---|
171 | // initialize <cores> field in node |
---|
172 | cores = hal_remote_l32( XPTR ( node_cxy , &cluster->cores_nr ) ); |
---|
173 | hal_remote_s32( XPTR( node_cxy , &node_ptr->cores ) , cores ); |
---|
174 | } |
---|
175 | else // non terminal : macro-cluster |
---|
176 | { |
---|
177 | bool_t found; |
---|
178 | uint32_t x; |
---|
179 | uint32_t y; |
---|
180 | cxy_t child_cxy; |
---|
181 | xptr_t child_xp; |
---|
182 | dqdt_node_t * child_ptr = &cluster->dqdt_tbl[level-1]; |
---|
183 | |
---|
184 | // search an active cluster in child[0][0] macro-cluster |
---|
185 | found = false; |
---|
186 | for( x = node_base_x ; |
---|
187 | (x < (node_base_x + half)) && (found == false) ; x++ ) |
---|
188 | { |
---|
189 | for( y = node_base_y ; |
---|
190 | (y < (node_base_y + half)) && (found == false) ; y++ ) |
---|
191 | { |
---|
192 | child_cxy = HAL_CXY_FROM_XY( x , y ); |
---|
193 | |
---|
194 | if( cluster_is_active( child_cxy ) ) |
---|
195 | { |
---|
196 | // initialize recursively selected child[0][0] node |
---|
197 | dqdt_recursive_build( child_cxy , level-1 , node_xp ); |
---|
198 | |
---|
199 | // build extended pointer on child[0][0] node |
---|
200 | child_xp = XPTR( child_cxy , child_ptr ); |
---|
201 | |
---|
202 | // update <cores> field in node |
---|
203 | cores = hal_remote_l32( XPTR ( child_cxy , &child_ptr->cores ) ); |
---|
204 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->cores ) , cores ); |
---|
205 | |
---|
206 | // update <clusters> field in node |
---|
207 | clusters = hal_remote_l32( XPTR ( child_cxy , &child_ptr->clusters ) ); |
---|
208 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->clusters ) , clusters ); |
---|
209 | |
---|
210 | // update <child[0][0]> field in node |
---|
211 | hal_remote_s64( XPTR( node_cxy , &node_ptr->children[0][0] ), child_xp ); |
---|
212 | |
---|
213 | // udate <arity> field in node |
---|
214 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->arity ) , 1 ); |
---|
215 | |
---|
216 | // exit loops |
---|
217 | found = true; |
---|
218 | } |
---|
219 | } |
---|
220 | } |
---|
221 | |
---|
222 | // search an active cluster in child[0][1] macro-cluster |
---|
223 | found = false; |
---|
224 | for( x = node_base_x ; |
---|
225 | (x < (node_base_x + half)) && (found == false) ; x++ ) |
---|
226 | { |
---|
227 | for( y = (node_base_y + half) ; |
---|
228 | (y < (node_base_y + (half<<1))) && (found == false) ; y++ ) |
---|
229 | { |
---|
230 | child_cxy = HAL_CXY_FROM_XY( x , y ); |
---|
231 | |
---|
232 | if( cluster_is_active( child_cxy ) ) |
---|
233 | { |
---|
234 | // initialize recursively selected child[0][1] node |
---|
235 | dqdt_recursive_build( child_cxy , level-1 , node_xp ); |
---|
236 | |
---|
237 | // build extended pointer on child[0][1] node |
---|
238 | child_xp = XPTR( child_cxy , child_ptr ); |
---|
239 | |
---|
240 | // update <cores> field in node |
---|
241 | cores = hal_remote_l32( XPTR ( child_cxy , &child_ptr->cores ) ); |
---|
242 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->cores ) , cores ); |
---|
243 | |
---|
244 | // update <clusters> field in node |
---|
245 | clusters = hal_remote_l32( XPTR ( child_cxy , &child_ptr->clusters ) ); |
---|
246 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->clusters ) , clusters ); |
---|
247 | |
---|
248 | // update <child[0][1]> field in node |
---|
249 | hal_remote_s64( XPTR( node_cxy , &node_ptr->children[0][1] ), child_xp ); |
---|
250 | |
---|
251 | // udate <arity> field in node |
---|
252 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->arity ) , 1 ); |
---|
253 | |
---|
254 | // exit loops |
---|
255 | found = true; |
---|
256 | } |
---|
257 | } |
---|
258 | } |
---|
259 | |
---|
260 | // search an active cluster in child[1][0] macro-cluster |
---|
261 | found = false; |
---|
262 | for( x = (node_base_x +half) ; |
---|
263 | (x < (node_base_x + (half<<1))) && (found == false) ; x++ ) |
---|
264 | { |
---|
265 | for( y = node_base_y ; |
---|
266 | (y < (node_base_y + half)) && (found == false) ; y++ ) |
---|
267 | { |
---|
268 | child_cxy = HAL_CXY_FROM_XY( x , y ); |
---|
269 | |
---|
270 | if( cluster_is_active( child_cxy ) ) |
---|
271 | { |
---|
272 | // initialize recursively selected child[1][0] node |
---|
273 | dqdt_recursive_build( child_cxy , level-1 , node_xp ); |
---|
274 | |
---|
275 | // build extended pointer on child[1][0] node |
---|
276 | child_xp = XPTR( child_cxy , child_ptr ); |
---|
277 | |
---|
278 | // update <cores> field in node |
---|
279 | cores = hal_remote_l32( XPTR ( child_cxy , &child_ptr->cores ) ); |
---|
280 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->cores ) , cores ); |
---|
281 | |
---|
282 | // update <clusters> field in node |
---|
283 | clusters = hal_remote_l32( XPTR ( child_cxy , &child_ptr->clusters ) ); |
---|
284 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->clusters ) , clusters ); |
---|
285 | |
---|
286 | // update <child[1][0]> field in node |
---|
287 | hal_remote_s64( XPTR( node_cxy , &node_ptr->children[1][0] ), child_xp ); |
---|
288 | |
---|
289 | // udate <arity> field in node |
---|
290 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->arity ) , 1 ); |
---|
291 | |
---|
292 | // exit loops |
---|
293 | found = true; |
---|
294 | } |
---|
295 | } |
---|
296 | } |
---|
297 | |
---|
298 | // search an active cluster in child[1][1] macro-cluster |
---|
299 | found = false; |
---|
300 | for( x = (node_base_x + half) ; |
---|
301 | (x < (node_base_x + (half<<1))) && (found == false) ; x++ ) |
---|
302 | { |
---|
303 | for( y = (node_base_y + half) ; |
---|
304 | (y < (node_base_y + (half<<1))) && (found == false) ; y++ ) |
---|
305 | { |
---|
306 | child_cxy = HAL_CXY_FROM_XY( x , y ); |
---|
307 | |
---|
308 | if( cluster_is_active( child_cxy ) ) |
---|
309 | { |
---|
310 | // initialize recursively selected child[1][1] node |
---|
311 | dqdt_recursive_build( child_cxy , level-1 , node_xp ); |
---|
312 | |
---|
313 | // build extended pointer on child[1][1] node |
---|
314 | child_xp = XPTR( child_cxy , child_ptr ); |
---|
315 | |
---|
316 | // update <cores> field in node |
---|
317 | cores = hal_remote_l32( XPTR ( child_cxy , &child_ptr->cores ) ); |
---|
318 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->cores ) , cores ); |
---|
319 | |
---|
320 | // update <clusters> field in node |
---|
321 | clusters = hal_remote_l32( XPTR ( child_cxy , &child_ptr->clusters ) ); |
---|
322 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->clusters ) , clusters ); |
---|
323 | |
---|
324 | // update <child[1][1]> field in node |
---|
325 | hal_remote_s64( XPTR( node_cxy , &node_ptr->children[1][1] ), child_xp ); |
---|
326 | |
---|
327 | // udate <arity> field in node |
---|
328 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->arity ) , 1 ); |
---|
329 | |
---|
330 | // exit loops |
---|
331 | found = true; |
---|
332 | } |
---|
333 | } |
---|
334 | } |
---|
335 | } |
---|
336 | } // end dqdt_recursive_build() |
---|
337 | |
---|
338 | ////////////////////// |
---|
339 | void dqdt_init( void ) |
---|
340 | { |
---|
341 | // get x_size & y_size |
---|
342 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
343 | uint32_t x_size = cluster->x_size; |
---|
344 | uint32_t y_size = cluster->y_size; |
---|
345 | |
---|
346 | assert( ((x_size <= 16) && (y_size <= 16)) , "illegal mesh size\n"); |
---|
347 | |
---|
348 | // compute level_max |
---|
349 | uint32_t x_size_ext = POW2_ROUNDUP( x_size ); |
---|
350 | uint32_t y_size_ext = POW2_ROUNDUP( y_size ); |
---|
351 | uint32_t size_ext = MAX( x_size_ext , y_size_ext ); |
---|
352 | uint32_t level_max = bits_log2( size_ext ); |
---|
353 | |
---|
354 | // all CP0s register the DQDT root in local cluster manager |
---|
355 | cluster->dqdt_root_xp = XPTR( 0 , &cluster->dqdt_tbl[level_max] ); |
---|
356 | |
---|
357 | // only CP0 in cluster 0 build the DQDT |
---|
358 | if( local_cxy == 0 ) |
---|
359 | { |
---|
360 | |
---|
361 | #if DEBUG_DQDT_INIT |
---|
362 | thread_t * this = CURRENT_THREAD; |
---|
363 | printk("\n[%s] thread[%x,%x] enters : x_size = %d / y_size = %d / level_max = %d\n", |
---|
364 | __FUNCTION__, this->process->pid, this->trdid, x_size, y_size, level_max ); |
---|
365 | #endif |
---|
366 | |
---|
367 | // only CP0 in cluster 0 call the recursive function to build the quad-tree |
---|
368 | if (local_cxy == 0) dqdt_recursive_build( local_cxy , level_max , XPTR_NULL ); |
---|
369 | |
---|
370 | #if DEBUG_DQDT_INIT |
---|
371 | dqdt_display(); |
---|
372 | #endif |
---|
373 | |
---|
374 | } |
---|
375 | } // end dqdt_init() |
---|
376 | |
---|
377 | |
---|
378 | /////////////////////////////////////////////////////////////////////////// |
---|
379 | // This recursive function is called by both the dqdt_increment_pages() |
---|
380 | // and by the dqdt_decrement_pages() functions. |
---|
381 | // It traverses the quad tree from clusters to root. |
---|
382 | /////////////////////////////////////////////////////////////////////////// |
---|
383 | // @ node_xp : extended pointer on current node |
---|
384 | // @ increment : number of pages variation |
---|
385 | /////////////////////////////////////////////////////////////////////////// |
---|
386 | static void dqdt_propagate_pages( xptr_t node_xp, |
---|
387 | int32_t increment ) |
---|
388 | { |
---|
389 | // get current node cluster identifier and local pointer |
---|
390 | cxy_t node_cxy = GET_CXY( node_xp ); |
---|
391 | dqdt_node_t * node_ptr = GET_PTR( node_xp ); |
---|
392 | |
---|
393 | // update current node pages number |
---|
394 | hal_remote_atomic_add( XPTR( node_cxy , &node_ptr->pages ) , increment ); |
---|
395 | |
---|
396 | // get extended pointer on parent node |
---|
397 | xptr_t parent_xp = (xptr_t)hal_remote_l64( XPTR( node_cxy , &node_ptr->parent ) ); |
---|
398 | |
---|
399 | // propagate if required |
---|
400 | if ( parent_xp != XPTR_NULL ) dqdt_propagate_pages( parent_xp, increment ); |
---|
401 | } |
---|
402 | |
---|
403 | //////////////////////////////////////// |
---|
404 | void dqdt_increment_pages( cxy_t cxy, |
---|
405 | uint32_t order ) |
---|
406 | { |
---|
407 | // get local pointer on node[0] (same in all clusters) |
---|
408 | dqdt_node_t * node_ptr = &LOCAL_CLUSTER->dqdt_tbl[0]; |
---|
409 | |
---|
410 | // update DQDT node[0] in remote cluster cxy |
---|
411 | hal_remote_atomic_add( XPTR( cxy , &node_ptr->pages ) , (1 << order) ); |
---|
412 | |
---|
413 | // get extended pointer on parent node in remote cluster cxy |
---|
414 | xptr_t parent_xp = hal_remote_l64( XPTR( cxy , &node_ptr->parent ) ); |
---|
415 | |
---|
416 | // propagate to DQDT upper levels |
---|
417 | if( parent_xp != XPTR_NULL ) dqdt_propagate_pages( parent_xp , (1 << order) ); |
---|
418 | |
---|
419 | #if DEBUG_DQDT_UPDATE_PAGES |
---|
420 | uint32_t cycle = hal_get_cycles(); |
---|
421 | if( cycle > DEBUG_DQDT_UPDATE_PAGES ) |
---|
422 | printk("\n[DBG] %s : thread %x in process %x / %x pages in cluster %x / cycle %d\n", |
---|
423 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, |
---|
424 | hal_remote_l32( XPTR( cxy , &node_ptr->pages ), cxy, cycle ); |
---|
425 | #endif |
---|
426 | |
---|
427 | } |
---|
428 | |
---|
429 | //////////////////////////////////////// |
---|
430 | void dqdt_decrement_pages( cxy_t cxy, |
---|
431 | uint32_t order ) |
---|
432 | { |
---|
433 | // get local pointer on node[0] (same in all clusters) |
---|
434 | dqdt_node_t * node_ptr = &LOCAL_CLUSTER->dqdt_tbl[0]; |
---|
435 | |
---|
436 | // update DQDT node[0] in remote cluster cxy |
---|
437 | hal_remote_atomic_add( XPTR( cxy , &node_ptr->pages ) , -(1 << order) ); |
---|
438 | |
---|
439 | // get extended pointer on parent node in remote cluster cxy |
---|
440 | xptr_t parent_xp = hal_remote_l64( XPTR( cxy , &node_ptr->parent ) ); |
---|
441 | |
---|
442 | // propagate to DQDT upper levels |
---|
443 | if( parent_xp != XPTR_NULL ) dqdt_propagate_pages( parent_xp , -(1 << order) ); |
---|
444 | |
---|
445 | #if DEBUG_DQDT_UPDATE_PAGES |
---|
446 | uint32_t cycle = hal_get_cycles(); |
---|
447 | if( cycle > DEBUG_DQDT_UPDATE_PAGES ) |
---|
448 | printk("\n[DBG] %s : thread %x in process %x / %x pages in cluster %x / cycle %d\n", |
---|
449 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, |
---|
450 | hal_remote_l32( XPTR( cxy , &node_ptr->pages ), cxy, cycle ); |
---|
451 | #endif |
---|
452 | |
---|
453 | } |
---|
454 | |
---|
455 | |
---|
456 | |
---|
457 | /////////////////////////////////////////////////////////////////////////// |
---|
458 | // This recursive function is called by both the dqdt_increment_threads() |
---|
459 | // and by the dqdt_decrement_threads functions. |
---|
460 | // It traverses the quad tree from clusters to root. |
---|
461 | /////////////////////////////////////////////////////////////////////////// |
---|
462 | // @ node : extended pointer on current node |
---|
463 | // @ increment : number of pages variation |
---|
464 | /////////////////////////////////////////////////////////////////////////// |
---|
465 | static void dqdt_propagate_threads( xptr_t node, |
---|
466 | int32_t increment ) |
---|
467 | { |
---|
468 | // get current node cluster identifier and local pointer |
---|
469 | cxy_t cxy = GET_CXY( node ); |
---|
470 | dqdt_node_t * ptr = GET_PTR( node ); |
---|
471 | |
---|
472 | // update current node threads number |
---|
473 | hal_remote_atomic_add( XPTR( cxy , &ptr->threads ) , increment ); |
---|
474 | |
---|
475 | // get extended pointer on parent node |
---|
476 | xptr_t parent = (xptr_t)hal_remote_l64( XPTR( cxy , &ptr->parent ) ); |
---|
477 | |
---|
478 | // propagate if required |
---|
479 | if ( parent != XPTR_NULL ) dqdt_propagate_threads( parent, increment ); |
---|
480 | } |
---|
481 | |
---|
482 | /////////////////////////////////// |
---|
483 | void dqdt_increment_threads( void ) |
---|
484 | { |
---|
485 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
486 | dqdt_node_t * node = &cluster->dqdt_tbl[0]; |
---|
487 | |
---|
488 | // update DQDT node level 0 |
---|
489 | hal_atomic_add( &node->threads , 1 ); |
---|
490 | |
---|
491 | // propagate to DQDT upper levels |
---|
492 | if( node->parent != XPTR_NULL ) dqdt_propagate_threads( node->parent , 1 ); |
---|
493 | |
---|
494 | #if DEBUG_DQDT_UPDATE_THREADS |
---|
495 | uint32_t cycle = hal_get_cycles(); |
---|
496 | if( cycle > DEBUG_DQDT_UPDATE_THREADS ) |
---|
497 | printk("\n[DBG] %s : thread %x in process %x / %d threads in cluster %x / cycle %d\n", |
---|
498 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, |
---|
499 | node->threads, local_cxy, cycle ); |
---|
500 | #endif |
---|
501 | |
---|
502 | } |
---|
503 | |
---|
504 | /////////////////////////////////// |
---|
505 | void dqdt_decrement_threads( void ) |
---|
506 | { |
---|
507 | cluster_t * cluster = LOCAL_CLUSTER; |
---|
508 | dqdt_node_t * node = &cluster->dqdt_tbl[0]; |
---|
509 | |
---|
510 | // update DQDT node level 0 |
---|
511 | hal_atomic_add( &node->threads , -1 ); |
---|
512 | |
---|
513 | // propagate to DQDT upper levels |
---|
514 | if( node->parent != XPTR_NULL ) dqdt_propagate_threads( node->parent , -1 ); |
---|
515 | |
---|
516 | #if DEBUG_DQDT_UPDATE_THREADS |
---|
517 | uint32_t cycle = hal_get_cycles(); |
---|
518 | if( cycle > DEBUG_DQDT_UPDATE_THREADS ) |
---|
519 | printk("\n[DBG] %s : thread %x in process %x / %d threads in cluster %x / cycle %d\n", |
---|
520 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, |
---|
521 | node->threads, local_cxy, cycle ); |
---|
522 | #endif |
---|
523 | |
---|
524 | } |
---|
525 | |
---|
526 | /////////////////////////////////// |
---|
527 | xptr_t dqdt_get_root( cxy_t cxy, |
---|
528 | uint32_t level ) |
---|
529 | { |
---|
530 | xptr_t node_xp; |
---|
531 | cxy_t node_cxy; |
---|
532 | dqdt_node_t * node_ptr; |
---|
533 | uint32_t current_level; |
---|
534 | |
---|
535 | assert( (level <= 5) , __FUNCTION__, "illegal DQDT level %d\n", level ); |
---|
536 | |
---|
537 | #if DEBUG_DQDT_GET_ROOT |
---|
538 | thread_t * this = CURRENT_THREAD; |
---|
539 | printk("\n[%s] thread[%x,%x] enters / cxy %x / level %d\n", |
---|
540 | __FUNCTION__, this->process->pid, this->trdid, cxy, level ); |
---|
541 | #endif |
---|
542 | |
---|
543 | // check macro-cluster |
---|
544 | if( cluster_is_active( cxy ) ) |
---|
545 | { |
---|
546 | // initialise node_xp and current_level |
---|
547 | node_xp = XPTR( cxy , &LOCAL_CLUSTER->dqdt_tbl[0] ); |
---|
548 | current_level = 0; |
---|
549 | |
---|
550 | // traverse the quad-tree from bottom to root |
---|
551 | while( current_level < level ) |
---|
552 | { |
---|
553 | node_cxy = GET_CXY( node_xp ); |
---|
554 | node_ptr = GET_PTR( node_xp ); |
---|
555 | |
---|
556 | node_xp = hal_remote_l64( XPTR( node_cxy , &node_ptr->parent ) ); |
---|
557 | current_level++; |
---|
558 | } |
---|
559 | } |
---|
560 | else |
---|
561 | { |
---|
562 | node_xp = XPTR_NULL; |
---|
563 | } |
---|
564 | |
---|
565 | #if DEBUG_DQDT_GET_ROOT |
---|
566 | printk("\n[%s] thread[%x,%x] exit / root_xp[%x,%x]\n", |
---|
567 | __FUNCTION__, this->process->pid, this->trdid, GET_CXY( node_xp ), GET_PTR( node_xp ) ); |
---|
568 | #endif |
---|
569 | |
---|
570 | return node_xp; |
---|
571 | |
---|
572 | } |
---|
573 | |
---|
574 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
575 | // This recursive function is called by both the dqdt_get_cluster_for_process() |
---|
576 | // and by the dqdt_get_cluster_for_memory() functions to select the cluster with the |
---|
577 | // smallest number of threads per core, or the smallest number of pages per cluster. |
---|
578 | // It traverses the quad tree from root to clusters. |
---|
579 | ///////////////////////////////////////////////////////////////////////////////////// |
---|
580 | static cxy_t dqdt_select_cluster( xptr_t node, |
---|
581 | bool_t for_memory ) |
---|
582 | { |
---|
583 | dqdt_node_t node_copy; // local copy of the current DQDT node |
---|
584 | xptr_t child_xp; // extended pointer on a DQDT child node |
---|
585 | uint32_t x; // child node X coordinate |
---|
586 | uint32_t y; // child node Y coordinate |
---|
587 | uint32_t select_x; // selected child X coordinate |
---|
588 | uint32_t select_y; // selected child Y coordinate |
---|
589 | uint32_t load; // load of the child (threads or pages) |
---|
590 | uint32_t load_min; // current value of the minimal load |
---|
591 | |
---|
592 | // get DQDT node local copy |
---|
593 | hal_remote_memcpy( XPTR( local_cxy , &node_copy ), node , sizeof(dqdt_node_t) ); |
---|
594 | |
---|
595 | // return cluster identifier for a terminal mode |
---|
596 | if( node_copy.level == 0 ) return GET_CXY(node); |
---|
597 | |
---|
598 | // analyse load for all children in non terminal node |
---|
599 | load_min = 0xFFFFFFFF; |
---|
600 | select_x = 0; |
---|
601 | select_y = 0; |
---|
602 | for( x = 0 ; x < 2 ; x++ ) |
---|
603 | { |
---|
604 | for( y = 0 ; y < 2 ; y++ ) |
---|
605 | { |
---|
606 | child_xp = node_copy.children[x][y]; |
---|
607 | if( child_xp != XPTR_NULL ) |
---|
608 | { |
---|
609 | cxy_t cxy = GET_CXY( child_xp ); |
---|
610 | dqdt_node_t * ptr = GET_PTR( child_xp ); |
---|
611 | |
---|
612 | // compute average load for each child |
---|
613 | if( for_memory ) |
---|
614 | { |
---|
615 | load = hal_remote_l32( XPTR( cxy , &ptr->pages ) ) / |
---|
616 | hal_remote_l32( XPTR( cxy , &ptr->clusters ) ); |
---|
617 | } |
---|
618 | else |
---|
619 | { |
---|
620 | load = hal_remote_l32( XPTR( cxy , &ptr->threads ) ) / |
---|
621 | hal_remote_l32( XPTR( cxy , &ptr->cores ) ); |
---|
622 | } |
---|
623 | |
---|
624 | // select children with smallest load |
---|
625 | if( load <= load_min ) |
---|
626 | { |
---|
627 | load_min = load; |
---|
628 | select_x = x; |
---|
629 | select_y = y; |
---|
630 | } |
---|
631 | } |
---|
632 | } |
---|
633 | } |
---|
634 | |
---|
635 | // select the child with the lowest load |
---|
636 | return dqdt_select_cluster( node_copy.children[select_x][select_y], for_memory ); |
---|
637 | |
---|
638 | } // end dqdt_select_cluster() |
---|
639 | |
---|
640 | |
---|
641 | /////////////////////////////////////////////////// |
---|
642 | cxy_t dqdt_get_cluster_for_thread( xptr_t root_xp ) |
---|
643 | { |
---|
644 | // call recursive function |
---|
645 | cxy_t cxy = dqdt_select_cluster( root_xp , false ); |
---|
646 | |
---|
647 | #if DEBUG_DQDT_SELECT_FOR_THREAD |
---|
648 | uint32_t cycle = hal_get_cycles(); |
---|
649 | if( cycle > DEBUG_DQDT_SELECT_FOR_PROCESS ) |
---|
650 | printk("\n[DBG] %s : thread %x in process %x select cluster %x / cycle %d\n", |
---|
651 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cxy, cycle ); |
---|
652 | #endif |
---|
653 | |
---|
654 | return cxy; |
---|
655 | } |
---|
656 | |
---|
657 | /////////////////////////////////////////////////// |
---|
658 | cxy_t dqdt_get_cluster_for_memory( xptr_t root_xp ) |
---|
659 | { |
---|
660 | // call recursive function |
---|
661 | cxy_t cxy = dqdt_select_cluster( root_xp , true ); |
---|
662 | |
---|
663 | #if DEBUG_DQDT_SELECT_FOR_MEMORY |
---|
664 | uint32_t cycle = hal_get_cycles(); |
---|
665 | if( cycle > DEBUG_DQDT_SELECT_FOR_MEMORY ) |
---|
666 | printk("\n[DBG] %s : thread %x in process %x select cluster %x / cycle %d\n", |
---|
667 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cxy, cycle ); |
---|
668 | #endif |
---|
669 | |
---|
670 | return cxy; |
---|
671 | } |
---|
672 | |
---|