source: trunk/kernel/kern/dqdt.c @ 447

Last change on this file since 447 was 438, checked in by alain, 7 years ago

Fix a bug in scheduler related to RPC blocking.

File size: 12.2 KB
Line 
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_types.h>
26#include <hal_special.h>
27#include <hal_atomic.h>
28#include <hal_remote.h>
29#include <printk.h>
30#include <chdev.h>
31#include <cluster.h>
32#include <bits.h>
33#include <dqdt.h>
34
35
36///////////////////////////////////////////////////////////////////////////////////////////
37//      Extern variables
38///////////////////////////////////////////////////////////////////////////////////////////
39
40extern chdev_directory_t  chdev_dir;  // defined in chdev.h / allocated in kernel_init.c
41
42
43///////////////////////////////////////////////////////////////////////////////////////////
44// This static recursive function traverse the DQDT quad-tree from root to bottom.
45///////////////////////////////////////////////////////////////////////////////////////////
46static void dqdt_recursive_print( xptr_t  node_xp )
47{
48        uint32_t i;
49    dqdt_node_t node;
50
51    // get node local copy
52    hal_remote_memcpy( XPTR( local_cxy , &node ), node_xp , sizeof(dqdt_node_t) );
53
54    // display node content
55        nolock_printk("- level %d in cluster %x (node %x) : threads = %x / pages = %x\n",
56    node.level, GET_CXY( node_xp ), GET_PTR( node_xp ), node.threads, node.pages );
57
58    // recursive call on children if node is not terminal
59    if ( node.level > 0 )
60    {
61        for ( i = 0 ; i < 4 ; i++ )
62        {
63            if ( node.children[i] != XPTR_NULL ) dqdt_recursive_print( node.children[i] );
64        }
65    }
66}
67
68///////////////////
69void dqdt_display()
70{
71    reg_t   save_sr;
72
73    // build extended pointer on DQDT root node
74        cluster_t * cluster = LOCAL_CLUSTER;
75    uint32_t    level   = cluster->dqdt_root_level;
76    xptr_t      root_xp = XPTR( 0 , &cluster->dqdt_tbl[level] );
77
78    // get pointers on TXT0 chdev
79    xptr_t    txt0_xp  = chdev_dir.txt_tx[0];
80    cxy_t     txt0_cxy = GET_CXY( txt0_xp );
81    chdev_t * txt0_ptr = GET_PTR( txt0_xp );
82
83    // get extended pointer on remote TXT0 chdev lock
84    xptr_t  lock_xp = XPTR( txt0_cxy , &txt0_ptr->wait_lock );
85
86    // get TXT0 lock in busy waiting mode
87    remote_spinlock_lock_busy( lock_xp , &save_sr );
88
89    // print header
90    nolock_printk("\n***** DQDT state\n\n");
91
92    // call recursive function
93    dqdt_recursive_print( root_xp );
94
95    // release lock
96    remote_spinlock_unlock_busy( lock_xp , save_sr );
97}
98
99////////////////////////////////////
100uint32_t dqdt_init( uint32_t x_size,
101                    uint32_t y_size,
102                    uint32_t y_width )
103{
104    assert( ((x_size <= 32) && (y_size <= 32)) , __FUNCTION__ , "illegal mesh size\n");
105
106        dqdt_node_t * node;
107    cxy_t         p_cxy;         // cluster coordinates for parent node
108    cxy_t         c_cxy;         // cluster coordinates for child node
109    uint32_t      level;         // node level in quad tree
110    uint32_t      mask;          // mask on node coordinates to compute existence condition
111    uint32_t      pmask;         // mask to compute parent coordinates from child coordinates
112    cluster_t   * cluster;       // pointer on local cluster
113
114    cluster = LOCAL_CLUSTER;
115
116    // compute level_max
117    uint32_t  x_size_ext = POW2_ROUNDUP( x_size );
118    uint32_t  y_size_ext = POW2_ROUNDUP( y_size );
119    uint32_t  size_ext   = MAX(x_size_ext , y_size_ext);
120    uint32_t  level_max  = (bits_log2(size_ext * size_ext) >> 1) + 1;
121
122    // get cluster coordinates
123    uint32_t    x       = local_cxy >> y_width;
124    uint32_t    y       = local_cxy & ((1<<y_width)-1);
125
126    // loop on local dqdt nodes (at most one node per level)
127    for( level = 0 ; level < level_max ; level++ )
128    {
129        // get pointer on the node to be initialised
130        node = &cluster->dqdt_tbl[level];
131
132        // set default values
133        node->level       = level;
134        node->arity       = 0;
135        node->threads     = 0;
136        node->pages       = 0;
137        node->parent      = XPTR_NULL;
138        node->children[0] = XPTR_NULL;
139        node->children[1] = XPTR_NULL;
140        node->children[2] = XPTR_NULL;
141        node->children[3] = XPTR_NULL;
142
143        // compute masks depending on level : 0x1, 0x3, 0x7, 0xF, 0x1F etc.
144        mask  = (1<<level)-1;
145        pmask = (1<<(level+1))-1;
146
147        // check the node  existence condition at each level
148        if( ((x & mask) == 0) && ((y & mask) == 0) )
149        {
150            // set parent extended pointer
151            p_cxy = ((x & ~pmask)<<y_width) + (y & ~pmask);
152            node->parent = XPTR( p_cxy , &cluster->dqdt_tbl[level+1] );
153
154            // set child[0] extended pointer (same [x,y] coordinates)
155            if ( level > 0 )
156            {
157                c_cxy = local_cxy;
158                node->children[0] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
159                node->arity++;
160            }
161
162            // set child[1] extended pointer (coordinates may overflow)
163            if ( (level > 0) && ((y + (1<<(level-1))) < y_size) )
164            {
165                c_cxy = local_cxy + (1<<(level-1));
166                node->children[1] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1] );
167                node->arity++;
168            }
169
170            // set child[2] extended pointer (coordinates may overflow)
171            if ( (level > 0) && ((x + (1<<(level-1))) < x_size) )
172            {
173                c_cxy = local_cxy + ((1<<(level-1))<<y_width);
174                node->children[2] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
175                node->arity++;
176            }
177
178            // set child[3] extended pointer (coordinates may overflow)
179            if ( (level > 0) && 
180                 ((x + (1<<(level-1))) < x_size) && 
181                 ((y + (1<<(level-1))) < y_size) )
182            {
183                c_cxy = local_cxy + ((1<<(level-1))<<y_width) + (1<<(level-1));
184                node->children[3] = XPTR( c_cxy , &cluster->dqdt_tbl[level-1]);
185                node->arity++;
186            }
187        }  // end if existence condition
188    }  // end for level
189
190    return level_max;
191
192} // end dqdt_init()
193
194///////////////////////////////////////////////////////////////////////////
195// This recursive function is called by the dqdt_update_threads() function.
196// It traverses the quad tree from clusters to root.
197///////////////////////////////////////////////////////////////////////////
198// @ node       : extended pointer on current node
199// @ increment  : number of threads variation
200///////////////////////////////////////////////////////////////////////////
201static void dqdt_propagate_threads( xptr_t  node,
202                                    int32_t increment )
203{
204    // get current node cluster identifier and local pointer
205    cxy_t         cxy = GET_CXY( node );
206    dqdt_node_t * ptr = GET_PTR( node );
207
208    // update current node threads number
209    hal_remote_atomic_add( XPTR( cxy , &ptr->threads ) , increment );
210
211    // get extended pointer on parent node
212    xptr_t parent = (xptr_t)hal_remote_lwd( XPTR( cxy , &ptr->parent ) );
213
214    // propagate if required
215    if ( parent != XPTR_NULL ) dqdt_propagate_threads( parent, increment );
216}
217
218///////////////////////////////////////////////////////////////////////////
219// This recursive function is called by the dqdt_update_pages() function.
220// It traverses the quad tree from clusters to root.
221///////////////////////////////////////////////////////////////////////////
222// @ node       : extended pointer on current node
223// @ increment  : number of pages variation
224///////////////////////////////////////////////////////////////////////////
225static void dqdt_propagate_pages( xptr_t  node,
226                                  int32_t increment )
227{
228    // get current node cluster identifier and local pointer
229    cxy_t         cxy = GET_CXY( node );
230    dqdt_node_t * ptr = GET_PTR( node );
231
232    // update current node threads number
233    hal_remote_atomic_add( XPTR( cxy , &ptr->pages ) , increment );
234
235    // get extended pointer on parent node
236    xptr_t parent = (xptr_t)hal_remote_lwd( XPTR( cxy , &ptr->parent ) );
237
238    // propagate if required
239    if ( parent != XPTR_NULL ) dqdt_propagate_pages( parent, increment );
240}
241
242/////////////////////////////////////////////
243void dqdt_update_threads( int32_t increment )
244{
245        cluster_t   * cluster = LOCAL_CLUSTER;
246    dqdt_node_t * node    = &cluster->dqdt_tbl[0];
247
248    // update DQDT node level 0
249    hal_atomic_add( &node->threads , increment );
250
251    // propagate to DQDT upper levels
252    if( node->parent != XPTR_NULL ) dqdt_propagate_threads( node->parent , increment );
253}
254
255///////////////////////////////////////////
256void dqdt_update_pages( int32_t increment )
257{
258        cluster_t   * cluster = LOCAL_CLUSTER;
259    dqdt_node_t * node    = &cluster->dqdt_tbl[0];
260
261    // update DQDT node level 0
262    hal_atomic_add( &node->pages , increment );
263
264    // propagate to DQDT upper levels
265    if( node->parent != XPTR_NULL ) dqdt_propagate_pages( node->parent , increment );
266}
267
268
269////////////////////////////////////////////////////////////////////////////////
270// This recursive function is called by both the dqdt_get_cluster_for_process()
271// and by the dqdt_get_cluster_for_memory() functions to select the cluster
272// with smallest number of thread, or smallest number of allocated pages.
273// It traverses the quad tree from root to clusters.
274///////////////////////////////////////////////////////////////////////////////
275static cxy_t dqdt_select_cluster( xptr_t node,
276                                  bool_t for_memory )
277{
278    dqdt_node_t   node_copy;     // local copy of the current DQDT node
279    uint32_t      i;             // index in the loop on children
280    uint32_t      select;        // index of selected child
281    xptr_t        child;         // extended pointer on a DQDT child node
282    cxy_t         cxy;           // DQDT child node cluster identifier
283    dqdt_node_t * ptr;           // pointer on a DQDT child node
284    uint32_t      load;          // load of the child (threads or pages)
285    uint32_t      load_min;      // current value of the minimal load
286
287    // get DQDT node local copy
288    hal_remote_memcpy( XPTR( local_cxy , &node_copy ), node , sizeof(dqdt_node_t) );
289
290    // return cluster identifier for a terminal mode
291    if( node_copy.level == 0 ) return GET_CXY(node);
292
293    // analyse load for all children in non terminal node
294    load_min = 0xFFFFFFFF;
295    select   = 0;
296    for( i = 0 ; i < 4 ; i++ )
297    {
298        child = node_copy.children[i];
299        if( child != XPTR_NULL )
300        {
301            cxy  = (cxy_t)GET_CXY( child );
302            ptr  = (dqdt_node_t *)GET_PTR( child );
303            if( for_memory ) load = hal_remote_lw( XPTR( cxy , &ptr->pages ) );
304            else             load = hal_remote_lw( XPTR( cxy , &ptr->threads ) );
305            if( load < load_min )
306            {
307                load_min = load;
308                select   = i;
309            }
310        }
311    }
312
313    // select the child with the lowest load
314    return dqdt_select_cluster( node_copy.children[select], for_memory );
315}
316
317////////////////////////////////////
318cxy_t dqdt_get_cluster_for_process()
319{
320    // build extended pointer on DQDT root node
321        cluster_t * cluster = LOCAL_CLUSTER;
322    uint32_t    level   = cluster->dqdt_root_level;
323    xptr_t      root_xp = XPTR( 0 , &cluster->dqdt_tbl[level] );
324
325    // call recursive function
326    return dqdt_select_cluster( root_xp , false );
327}
328
329////////////////////////////////////
330cxy_t dqdt_get_cluster_for_memory()
331{
332    // build extended pointer on DQDT root node
333        cluster_t * cluster = LOCAL_CLUSTER;
334    uint32_t    level   = cluster->dqdt_root_level;
335    xptr_t      root_xp = XPTR( 0 , &cluster->dqdt_tbl[level] );
336
337    // call recursive function
338    return dqdt_select_cluster( root_xp , true );
339}
340
Note: See TracBrowser for help on using the repository browser.