| 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       : extended pointer on current node | 
|---|
| 376 | // @ increment  : number of pages variation | 
|---|
| 377 | /////////////////////////////////////////////////////////////////////////// | 
|---|
| 378 | static void dqdt_propagate_pages( xptr_t  node, | 
|---|
| 379 | int32_t increment ) | 
|---|
| 380 | { | 
|---|
| 381 | // get current node cluster identifier and local pointer | 
|---|
| 382 | cxy_t         cxy = GET_CXY( node ); | 
|---|
| 383 | dqdt_node_t * ptr = GET_PTR( node ); | 
|---|
| 384 |  | 
|---|
| 385 | // update current node pages number | 
|---|
| 386 | hal_remote_atomic_add( XPTR( cxy , &ptr->pages ) , increment ); | 
|---|
| 387 |  | 
|---|
| 388 | // get extended pointer on parent node | 
|---|
| 389 | xptr_t parent = (xptr_t)hal_remote_l64( XPTR( cxy , &ptr->parent ) ); | 
|---|
| 390 |  | 
|---|
| 391 | // propagate if required | 
|---|
| 392 | if ( parent != XPTR_NULL ) dqdt_propagate_pages( parent, increment ); | 
|---|
| 393 | } | 
|---|
| 394 |  | 
|---|
| 395 | /////////////////////////////////////////// | 
|---|
| 396 | void dqdt_increment_pages( uint32_t order ) | 
|---|
| 397 | { | 
|---|
| 398 | cluster_t   * cluster = LOCAL_CLUSTER; | 
|---|
| 399 | dqdt_node_t * node    = &cluster->dqdt_tbl[0]; | 
|---|
| 400 |  | 
|---|
| 401 | // update DQDT node level 0 | 
|---|
| 402 | hal_atomic_add( &node->pages , (1 << order) ); | 
|---|
| 403 |  | 
|---|
| 404 | // propagate to DQDT upper levels | 
|---|
| 405 | if( node->parent != XPTR_NULL ) dqdt_propagate_pages( node->parent , (1 << order) ); | 
|---|
| 406 |  | 
|---|
| 407 | #if DEBUG_DQDT_UPDATE_PAGES | 
|---|
| 408 | uint32_t cycle = hal_get_cycles(); | 
|---|
| 409 | if( cycle > DEBUG_DQDT_UPDATE_PAGES ) | 
|---|
| 410 | printk("\n[DBG] %s : thread %x in process %x / %x pages in cluster %x / cycle %d\n", | 
|---|
| 411 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, node->pages, local_cxy, cycle ); | 
|---|
| 412 | #endif | 
|---|
| 413 |  | 
|---|
| 414 | } | 
|---|
| 415 |  | 
|---|
| 416 | /////////////////////////////////////////// | 
|---|
| 417 | void dqdt_decrement_pages( uint32_t order ) | 
|---|
| 418 | { | 
|---|
| 419 | cluster_t   * cluster = LOCAL_CLUSTER; | 
|---|
| 420 | dqdt_node_t * node    = &cluster->dqdt_tbl[0]; | 
|---|
| 421 |  | 
|---|
| 422 | // update DQDT node level 0 | 
|---|
| 423 | hal_atomic_add( &node->pages , -(1 << order) ); | 
|---|
| 424 |  | 
|---|
| 425 | // propagate to DQDT upper levels | 
|---|
| 426 | if( node->parent != XPTR_NULL ) dqdt_propagate_pages( node->parent , -(1 << order) ); | 
|---|
| 427 |  | 
|---|
| 428 | #if DEBUG_DQDT_UPDATE_PAGES | 
|---|
| 429 | uint32_t cycle = hal_get_cycles(); | 
|---|
| 430 | if( cycle > DEBUG_DQDT_UPDATE_PAGES ) | 
|---|
| 431 | printk("\n[DBG] %s : thread %x in process %x / %x pages in cluster %x / cycle %d\n", | 
|---|
| 432 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, node->pages, local_cxy, cycle ); | 
|---|
| 433 | #endif | 
|---|
| 434 |  | 
|---|
| 435 | } | 
|---|
| 436 |  | 
|---|
| 437 |  | 
|---|
| 438 |  | 
|---|
| 439 | /////////////////////////////////////////////////////////////////////////// | 
|---|
| 440 | // This recursive function is called by both the dqdt_increment_threads() | 
|---|
| 441 | // and by the dqdt_decrement_threads functions. | 
|---|
| 442 | // It traverses the quad tree from clusters to root. | 
|---|
| 443 | /////////////////////////////////////////////////////////////////////////// | 
|---|
| 444 | // @ node       : extended pointer on current node | 
|---|
| 445 | // @ increment  : number of pages variation | 
|---|
| 446 | /////////////////////////////////////////////////////////////////////////// | 
|---|
| 447 | static void dqdt_propagate_threads( xptr_t  node, | 
|---|
| 448 | int32_t increment ) | 
|---|
| 449 | { | 
|---|
| 450 | // get current node cluster identifier and local pointer | 
|---|
| 451 | cxy_t         cxy = GET_CXY( node ); | 
|---|
| 452 | dqdt_node_t * ptr = GET_PTR( node ); | 
|---|
| 453 |  | 
|---|
| 454 | // update current node threads number | 
|---|
| 455 | hal_remote_atomic_add( XPTR( cxy , &ptr->threads ) , increment ); | 
|---|
| 456 |  | 
|---|
| 457 | // get extended pointer on parent node | 
|---|
| 458 | xptr_t parent = (xptr_t)hal_remote_l64( XPTR( cxy , &ptr->parent ) ); | 
|---|
| 459 |  | 
|---|
| 460 | // propagate if required | 
|---|
| 461 | if ( parent != XPTR_NULL ) dqdt_propagate_threads( parent, increment ); | 
|---|
| 462 | } | 
|---|
| 463 |  | 
|---|
| 464 | /////////////////////////////////// | 
|---|
| 465 | void dqdt_increment_threads( void ) | 
|---|
| 466 | { | 
|---|
| 467 | cluster_t   * cluster = LOCAL_CLUSTER; | 
|---|
| 468 | dqdt_node_t * node    = &cluster->dqdt_tbl[0]; | 
|---|
| 469 |  | 
|---|
| 470 | // update DQDT node level 0 | 
|---|
| 471 | hal_atomic_add( &node->threads , 1 ); | 
|---|
| 472 |  | 
|---|
| 473 | // propagate to DQDT upper levels | 
|---|
| 474 | if( node->parent != XPTR_NULL ) dqdt_propagate_threads( node->parent , 1 ); | 
|---|
| 475 |  | 
|---|
| 476 | #if DEBUG_DQDT_UPDATE_THREADS | 
|---|
| 477 | uint32_t cycle = hal_get_cycles(); | 
|---|
| 478 | if( cycle > DEBUG_DQDT_UPDATE_THREADS ) | 
|---|
| 479 | printk("\n[DBG] %s : thread %x in process %x / %d threads in cluster %x / cycle %d\n", | 
|---|
| 480 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, node->threads, local_cxy, cycle ); | 
|---|
| 481 | #endif | 
|---|
| 482 |  | 
|---|
| 483 | } | 
|---|
| 484 |  | 
|---|
| 485 | /////////////////////////////////// | 
|---|
| 486 | void dqdt_decrement_threads( void ) | 
|---|
| 487 | { | 
|---|
| 488 | cluster_t   * cluster = LOCAL_CLUSTER; | 
|---|
| 489 | dqdt_node_t * node    = &cluster->dqdt_tbl[0]; | 
|---|
| 490 |  | 
|---|
| 491 | // update DQDT node level 0 | 
|---|
| 492 | hal_atomic_add( &node->threads , -1 ); | 
|---|
| 493 |  | 
|---|
| 494 | // propagate to DQDT upper levels | 
|---|
| 495 | if( node->parent != XPTR_NULL ) dqdt_propagate_threads( node->parent , -1 ); | 
|---|
| 496 |  | 
|---|
| 497 | #if DEBUG_DQDT_UPDATE_THREADS | 
|---|
| 498 | uint32_t cycle = hal_get_cycles(); | 
|---|
| 499 | if( cycle > DEBUG_DQDT_UPDATE_THREADS ) | 
|---|
| 500 | printk("\n[DBG] %s : thread %x in process %x / %d threads in cluster %x / cycle %d\n", | 
|---|
| 501 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, node->threads, local_cxy, cycle ); | 
|---|
| 502 | #endif | 
|---|
| 503 |  | 
|---|
| 504 | } | 
|---|
| 505 |  | 
|---|
| 506 |  | 
|---|
| 507 | ///////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 508 | // This recursive function is called by both the dqdt_get_cluster_for_process() | 
|---|
| 509 | // and by the dqdt_get_cluster_for_memory() functions to select the cluster with the | 
|---|
| 510 | // smallest number of threads per core, or the smallest number of pages per cluster. | 
|---|
| 511 | // It traverses the quad tree from root to clusters. | 
|---|
| 512 | ///////////////////////////////////////////////////////////////////////////////////// | 
|---|
| 513 | static cxy_t dqdt_select_cluster( xptr_t node, | 
|---|
| 514 | bool_t for_memory ) | 
|---|
| 515 | { | 
|---|
| 516 | dqdt_node_t   node_copy;     // local copy of the current DQDT node | 
|---|
| 517 | xptr_t        child_xp;      // extended pointer on a DQDT child node | 
|---|
| 518 | uint32_t      x;             // child node X coordinate | 
|---|
| 519 | uint32_t      y;             // child node Y coordinate | 
|---|
| 520 | uint32_t      select_x;      // selected child X coordinate | 
|---|
| 521 | uint32_t      select_y;      // selected child Y coordinate | 
|---|
| 522 | uint32_t      load;          // load of the child (threads or pages) | 
|---|
| 523 | uint32_t      load_min;      // current value of the minimal load | 
|---|
| 524 |  | 
|---|
| 525 | // get DQDT node local copy | 
|---|
| 526 | hal_remote_memcpy( XPTR( local_cxy , &node_copy ), node , sizeof(dqdt_node_t) ); | 
|---|
| 527 |  | 
|---|
| 528 | // return cluster identifier for a terminal mode | 
|---|
| 529 | if( node_copy.level == 0 ) return GET_CXY(node); | 
|---|
| 530 |  | 
|---|
| 531 | // analyse load for all children in non terminal node | 
|---|
| 532 | load_min = 0xFFFFFFFF; | 
|---|
| 533 | select_x = 0; | 
|---|
| 534 | select_y = 0; | 
|---|
| 535 | for( x = 0 ; x < 2 ; x++ ) | 
|---|
| 536 | { | 
|---|
| 537 | for( y = 0 ; y < 2 ; y++ ) | 
|---|
| 538 | { | 
|---|
| 539 | child_xp = node_copy.children[x][y]; | 
|---|
| 540 | if( child_xp != XPTR_NULL ) | 
|---|
| 541 | { | 
|---|
| 542 | cxy_t         cxy  = GET_CXY( child_xp ); | 
|---|
| 543 | dqdt_node_t * ptr  = GET_PTR( child_xp ); | 
|---|
| 544 |  | 
|---|
| 545 | // compute average load  for each child | 
|---|
| 546 | if( for_memory ) | 
|---|
| 547 | { | 
|---|
| 548 | load = hal_remote_l32( XPTR( cxy , &ptr->pages ) ) / | 
|---|
| 549 | hal_remote_l32( XPTR( cxy , &ptr->clusters ) ); | 
|---|
| 550 | } | 
|---|
| 551 | else | 
|---|
| 552 | { | 
|---|
| 553 | load = hal_remote_l32( XPTR( cxy , &ptr->threads ) ) / | 
|---|
| 554 | hal_remote_l32( XPTR( cxy , &ptr->cores ) ); | 
|---|
| 555 | } | 
|---|
| 556 |  | 
|---|
| 557 | // select children with smallest load | 
|---|
| 558 | if( load <= load_min ) | 
|---|
| 559 | { | 
|---|
| 560 | load_min = load; | 
|---|
| 561 | select_x = x; | 
|---|
| 562 | select_y = y; | 
|---|
| 563 | } | 
|---|
| 564 | } | 
|---|
| 565 | } | 
|---|
| 566 | } | 
|---|
| 567 |  | 
|---|
| 568 | // select the child with the lowest load | 
|---|
| 569 | return dqdt_select_cluster( node_copy.children[select_x][select_y], for_memory ); | 
|---|
| 570 |  | 
|---|
| 571 | }  // end dqdt_select_cluster() | 
|---|
| 572 |  | 
|---|
| 573 |  | 
|---|
| 574 | ////////////////////////////////////////// | 
|---|
| 575 | cxy_t dqdt_get_cluster_for_process( void ) | 
|---|
| 576 | { | 
|---|
| 577 | // call recursive function | 
|---|
| 578 | cxy_t cxy = dqdt_select_cluster( LOCAL_CLUSTER->dqdt_root_xp , false ); | 
|---|
| 579 |  | 
|---|
| 580 | #if DEBUG_DQDT_SELECT_FOR_PROCESS | 
|---|
| 581 | uint32_t cycle = hal_get_cycles(); | 
|---|
| 582 | if( cycle > DEBUG_DQDT_SELECT_FOR_PROCESS ) | 
|---|
| 583 | printk("\n[DBG] %s : thread %x in process %x select cluster %x / cycle %d\n", | 
|---|
| 584 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cxy, cycle ); | 
|---|
| 585 | #endif | 
|---|
| 586 |  | 
|---|
| 587 | return cxy; | 
|---|
| 588 | } | 
|---|
| 589 |  | 
|---|
| 590 | ///////////////////////////////////////// | 
|---|
| 591 | cxy_t dqdt_get_cluster_for_memory( void ) | 
|---|
| 592 | { | 
|---|
| 593 | // call recursive function | 
|---|
| 594 | cxy_t cxy = dqdt_select_cluster( LOCAL_CLUSTER->dqdt_root_xp , true ); | 
|---|
| 595 |  | 
|---|
| 596 | #if DEBUG_DQDT_SELECT_FOR_MEMORY | 
|---|
| 597 | uint32_t cycle = hal_get_cycles(); | 
|---|
| 598 | if( cycle > DEBUG_DQDT_SELECT_FOR_MEMORY ) | 
|---|
| 599 | printk("\n[DBG] %s : thread %x in process %x select cluster %x / cycle %d\n", | 
|---|
| 600 | __FUNCTION__, CURRENT_THREAD->trdid, CURRENT_THREAD->process->pid, cxy, cycle ); | 
|---|
| 601 | #endif | 
|---|
| 602 |  | 
|---|
| 603 | return cxy; | 
|---|
| 604 | } | 
|---|
| 605 |  | 
|---|