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