source: rtems/cpukit/score/src/schedulerpriorityaffinitysmp.c @ 0232b28

5
Last change on this file since 0232b28 was 0232b28, checked in by Sebastian Huber <sebastian.huber@…>, on 07/04/17 at 06:59:37

score: Use processor mask for set affinity

Update #3059.

  • Property mode set to 100644
File size: 18.8 KB
Line 
1/**
2 * @file
3 *
4 * @brief Deterministic Priority Affinity SMP Scheduler Implementation
5 *
6 * @ingroup ScoreSchedulerPriorityAffinitySMP
7 */
8
9/*
10 *  COPYRIGHT (c) 2014.
11 *  On-Line Applications Research Corporation (OAR).
12 *
13 *  The license and distribution terms for this file may be
14 *  found in the file LICENSE in this distribution or at
15 *  http://www.rtems.org/license/LICENSE.
16 */
17
18#if HAVE_CONFIG_H
19  #include "config.h"
20#endif
21
22#include <rtems/score/schedulerpriorityaffinitysmp.h>
23#include <rtems/score/schedulerpriorityimpl.h>
24#include <rtems/score/schedulersmpimpl.h>
25#include <rtems/score/schedulerprioritysmpimpl.h>
26#include <rtems/score/wkspace.h>
27#include <rtems/score/cpusetimpl.h>
28
29#include <rtems/score/priority.h>
30
31/*
32 * The following methods which initially were static in schedulerprioritysmp.c
33 * are shared with this scheduler. They are now public so they can be shared.
34 *
35 *  + _Scheduler_priority_SMP_Get_self
36 *  + _Scheduler_priority_SMP_Insert_ready_fifo
37 *  + _Scheduler_priority_SMP_Insert_ready_lifo
38 *  + _Scheduler_priority_SMP_Thread_get_node
39 *  + _Scheduler_priority_SMP_Move_from_scheduled_to_ready
40 *  + _Scheduler_priority_SMP_Move_from_ready_to_scheduled
41 *  + _Scheduler_priority_SMP_Extract_from_ready
42 *  + _Scheduler_priority_SMP_Do_update
43 */
44
45static bool _Scheduler_priority_affinity_SMP_Insert_priority_lifo_order(
46  const Chain_Node *to_insert,
47  const Chain_Node *next
48)
49{
50  return next != NULL
51    && _Scheduler_SMP_Insert_priority_lifo_order( to_insert, next );
52}
53
54static bool _Scheduler_priority_affinity_SMP_Insert_priority_fifo_order(
55  const Chain_Node *to_insert,
56  const Chain_Node *next
57)
58{
59  return next != NULL
60    && _Scheduler_SMP_Insert_priority_fifo_order( to_insert, next );
61}
62
63/*
64 * This method returns the scheduler node for the specified thread
65 * as a scheduler specific type.
66 */
67static Scheduler_priority_affinity_SMP_Node *
68_Scheduler_priority_affinity_SMP_Thread_get_node(
69  Thread_Control *thread
70)
71{
72  return (Scheduler_priority_affinity_SMP_Node *)
73    _Thread_Scheduler_get_home_node( thread );
74}
75
76static Scheduler_priority_affinity_SMP_Node *
77_Scheduler_priority_affinity_SMP_Node_downcast(
78  Scheduler_Node *node
79)
80{
81  return (Scheduler_priority_affinity_SMP_Node *) node;
82}
83
84/*
85 * This method initializes the scheduler control information for
86 * this scheduler instance.
87 */
88void _Scheduler_priority_affinity_SMP_Node_initialize(
89  const Scheduler_Control *scheduler,
90  Scheduler_Node          *node,
91  Thread_Control          *the_thread,
92  Priority_Control         priority
93)
94{
95  Scheduler_priority_affinity_SMP_Node *the_node;
96
97  _Scheduler_priority_SMP_Node_initialize( scheduler, node, the_thread, priority );
98
99  /*
100   *  All we add is affinity information to the basic SMP node.
101   */
102  the_node = _Scheduler_priority_affinity_SMP_Node_downcast( node );
103  the_node->Affinity     = *_CPU_set_Default();
104  the_node->Affinity.set = &the_node->Affinity.preallocated;
105}
106
107/*
108 * This method is unique to this scheduler because it takes into
109 * account affinity as it determines the highest ready thread.
110 * Since this is used to pick a new thread to replace the victim,
111 * the highest ready thread must have affinity such that it can
112 * be executed on the victim's processor.
113 */
114static Scheduler_Node *_Scheduler_priority_affinity_SMP_Get_highest_ready(
115  Scheduler_Context *context,
116  Scheduler_Node    *victim
117)
118{
119  Scheduler_priority_SMP_Context       *self =
120    _Scheduler_priority_SMP_Get_self( context );
121  Priority_Control                      index;
122  Scheduler_Node                       *highest = NULL;
123  Thread_Control                       *victim_thread;
124  uint32_t                              victim_cpu_index;
125  Scheduler_priority_affinity_SMP_Node *node;
126
127  /*
128   * This is done when we need to check if reevaluations are needed.
129   */
130  if ( victim == NULL ) {
131    node = (Scheduler_priority_affinity_SMP_Node *)
132      _Scheduler_priority_Ready_queue_first(
133        &self->Bit_map,
134        &self->Ready[ 0 ]
135      );
136
137    return &node->Base.Base.Base;
138  }
139
140  victim_thread = _Scheduler_Node_get_owner( victim );
141  victim_cpu_index = _Per_CPU_Get_index( _Thread_Get_CPU( victim_thread ) );
142
143  /**
144   * @todo The deterministic priority scheduler structure is optimized
145   * for insertion, extraction, and finding the highest priority
146   * thread. Scanning the list of ready threads is not a purpose
147   * for which it was optimized. There are optimizations to be
148   * made in this loop.
149   *
150   * + by checking the major bit, we could potentially skip entire
151   *   groups of 16.
152   *
153   * When using this scheduler as implemented, the application's
154   * choice of numeric priorities and their distribution can have
155   * an impact on performance.
156   */
157  for ( index = _Priority_bit_map_Get_highest( &self->Bit_map ) ;
158        index <= PRIORITY_MAXIMUM;
159        index++ )
160  {
161    Chain_Control   *chain =  &self->Ready[index];
162    Chain_Node      *chain_node;
163    for ( chain_node = _Chain_First( chain );
164          chain_node != _Chain_Immutable_tail( chain ) ;
165          chain_node = _Chain_Next( chain_node ) )
166    {
167      node = (Scheduler_priority_affinity_SMP_Node *) chain_node;
168
169      /*
170       * Can this thread run on this CPU?
171       */
172      if ( CPU_ISSET( (int) victim_cpu_index, node->Affinity.set ) ) {
173        highest = &node->Base.Base.Base;
174        break;
175      }
176    }
177    if ( highest )
178      break;
179  }
180
181  _Assert( highest != NULL );
182
183  return highest;
184}
185
186/*
187 * This method is very similar to _Scheduler_priority_affinity_SMP_Block
188 * but has the difference that is invokes this scheduler's
189 * get_highest_ready() support method.
190 */
191void _Scheduler_priority_affinity_SMP_Block(
192  const Scheduler_Control *scheduler,
193  Thread_Control          *thread,
194  Scheduler_Node          *node
195)
196{
197  Scheduler_Context *context = _Scheduler_Get_context( scheduler );
198
199  _Scheduler_SMP_Block(
200    context,
201    thread,
202    node,
203    _Scheduler_priority_SMP_Extract_from_ready,
204    _Scheduler_priority_affinity_SMP_Get_highest_ready,
205    _Scheduler_priority_SMP_Move_from_ready_to_scheduled,
206    _Scheduler_SMP_Allocate_processor_exact
207  );
208
209  /*
210   * Since this removed a single thread from the scheduled set
211   * and selected the most appropriate thread from the ready
212   * set to replace it, there should be no need for thread
213   * migrations.
214   */
215}
216
217/*
218 * This method is unique to this scheduler because it must take into
219 * account affinity as it searches for the lowest priority scheduled
220 * thread. It ignores those which cannot be replaced by the filter
221 * thread because the potential victim thread does not have affinity
222 * for that processor.
223 */
224static Scheduler_Node * _Scheduler_priority_affinity_SMP_Get_lowest_scheduled(
225  Scheduler_Context *context,
226  Scheduler_Node    *filter_base,
227  Chain_Node_order   order
228)
229{
230  Scheduler_SMP_Context *self = _Scheduler_SMP_Get_self( context );
231  Scheduler_Node *lowest_scheduled = NULL;
232  Chain_Control   *scheduled = &self->Scheduled;
233  Chain_Node      *chain_node;
234  Scheduler_priority_affinity_SMP_Node *filter =
235    _Scheduler_priority_affinity_SMP_Node_downcast( filter_base );
236
237  for ( chain_node = _Chain_Last( scheduled );
238        chain_node != _Chain_Immutable_head( scheduled ) ;
239        chain_node = _Chain_Previous( chain_node ) ) {
240    Scheduler_priority_affinity_SMP_Node *node;
241    Thread_Control                       *thread;
242    uint32_t                              cpu_index;
243
244    node = (Scheduler_priority_affinity_SMP_Node *) chain_node;
245
246    /*
247     * If we didn't find a thread which is of equal or lower importance
248     * than filter thread is, then we can't schedule the filter thread
249     * to execute.
250     */
251    if (
252      (*order)(
253        &node->Base.Base.Base.Node.Chain,
254        &filter->Base.Base.Base.Node.Chain
255      )
256    ) {
257      break;
258    }
259
260    /* cpu_index is the processor number thread is executing on */
261    thread = _Scheduler_Node_get_owner( &node->Base.Base.Base );
262    cpu_index = _Per_CPU_Get_index( _Thread_Get_CPU( thread ) );
263
264    if ( CPU_ISSET( (int) cpu_index, filter->Affinity.set ) ) {
265      lowest_scheduled = &node->Base.Base.Base;
266      break;
267    }
268
269  }
270
271  return lowest_scheduled;
272}
273
274/*
275 * This method is unique to this scheduler because it must pass
276 * _Scheduler_priority_affinity_SMP_Get_lowest_scheduled into
277 * _Scheduler_SMP_Enqueue_ordered.
278 */
279static bool _Scheduler_priority_affinity_SMP_Enqueue_fifo(
280  Scheduler_Context *context,
281  Scheduler_Node    *node
282)
283{
284  return _Scheduler_SMP_Enqueue_ordered(
285    context,
286    node,
287    _Scheduler_priority_affinity_SMP_Insert_priority_fifo_order,
288    _Scheduler_priority_SMP_Insert_ready_fifo,
289    _Scheduler_SMP_Insert_scheduled_fifo,
290    _Scheduler_priority_SMP_Move_from_scheduled_to_ready,
291    _Scheduler_priority_affinity_SMP_Get_lowest_scheduled,
292    _Scheduler_SMP_Allocate_processor_exact
293  );
294}
295
296/*
297 * This method is invoked at the end of certain scheduling operations
298 * to ensure that the highest priority ready thread cannot be scheduled
299 * to execute. When we schedule with affinity, there is the possibility
300 * that we need to migrate a thread to another core to ensure that the
301 * highest priority ready threads are in fact scheduled.
302 */
303static void _Scheduler_priority_affinity_SMP_Check_for_migrations(
304  Scheduler_Context *context
305)
306{
307  Scheduler_priority_SMP_Context *self;
308  Scheduler_Node                 *lowest_scheduled;
309  Scheduler_Node                 *highest_ready;
310
311  self = _Scheduler_priority_SMP_Get_self( context );
312
313  while (1) {
314    if ( _Priority_bit_map_Is_empty( &self->Bit_map ) ) {
315      /* Nothing to do */
316      break;
317    }
318
319    highest_ready =
320      _Scheduler_priority_affinity_SMP_Get_highest_ready( context, NULL );
321
322    lowest_scheduled =
323      _Scheduler_priority_affinity_SMP_Get_lowest_scheduled(
324        context,
325        highest_ready,
326        _Scheduler_SMP_Insert_priority_lifo_order
327      );
328
329    /*
330     * If we can't find a thread to displace from the scheduled set,
331     * then we have placed all the highest priority threads possible
332     * in the scheduled set.
333     *
334     * We found the absolute highest priority thread without
335     * considering affinity. But now we have to consider that thread's
336     * affinity as we look to place it.
337     */
338    if ( lowest_scheduled == NULL )
339      break;
340
341    /*
342     * But if we found a thread which is lower priority than one
343     * in the ready set, then we need to swap them out.
344     */
345
346    _Scheduler_priority_SMP_Extract_from_ready( context, highest_ready );
347    _Scheduler_SMP_Enqueue_to_scheduled(
348      context,
349      highest_ready,
350      lowest_scheduled,
351      _Scheduler_SMP_Insert_scheduled_fifo,
352      _Scheduler_priority_SMP_Move_from_scheduled_to_ready,
353      _Scheduler_SMP_Allocate_processor_exact
354    );
355  }
356}
357
358/*
359 * This is the public scheduler specific Unblock operation.
360 */
361void _Scheduler_priority_affinity_SMP_Unblock(
362  const Scheduler_Control *scheduler,
363  Thread_Control          *thread,
364  Scheduler_Node          *node
365)
366{
367  Scheduler_Context *context = _Scheduler_Get_context( scheduler );
368
369  _Scheduler_SMP_Unblock(
370    context,
371    thread,
372    node,
373    _Scheduler_priority_SMP_Do_update,
374    _Scheduler_priority_affinity_SMP_Enqueue_fifo
375  );
376
377  /*
378   * Perform any thread migrations that are needed due to these changes.
379   */
380  _Scheduler_priority_affinity_SMP_Check_for_migrations( context );
381}
382
383/*
384 *  This is unique to this scheduler because it passes scheduler specific
385 *  get_lowest_scheduled helper to _Scheduler_SMP_Enqueue_ordered.
386 */
387static bool _Scheduler_priority_affinity_SMP_Enqueue_ordered(
388  Scheduler_Context     *context,
389  Scheduler_Node        *node,
390  Chain_Node_order       order,
391  Scheduler_SMP_Insert   insert_ready,
392  Scheduler_SMP_Insert   insert_scheduled
393)
394{
395  return _Scheduler_SMP_Enqueue_ordered(
396    context,
397    node,
398    order,
399    insert_ready,
400    insert_scheduled,
401    _Scheduler_priority_SMP_Move_from_scheduled_to_ready,
402    _Scheduler_priority_affinity_SMP_Get_lowest_scheduled,
403    _Scheduler_SMP_Allocate_processor_exact
404  );
405}
406
407/*
408 *  This is unique to this scheduler because it is on the path
409 *  to _Scheduler_priority_affinity_SMP_Enqueue_ordered() which
410 *  invokes a scheduler unique get_lowest_scheduled helper.
411 */
412static bool _Scheduler_priority_affinity_SMP_Enqueue_lifo(
413  Scheduler_Context *context,
414  Scheduler_Node    *node
415)
416{
417  return _Scheduler_priority_affinity_SMP_Enqueue_ordered(
418    context,
419    node,
420    _Scheduler_priority_affinity_SMP_Insert_priority_lifo_order,
421    _Scheduler_priority_SMP_Insert_ready_lifo,
422    _Scheduler_SMP_Insert_scheduled_lifo
423  );
424}
425
426/*
427 * This method is unique to this scheduler because it must
428 * invoke _Scheduler_SMP_Enqueue_scheduled_ordered() with
429 * this scheduler's get_highest_ready() helper.
430 */
431static bool _Scheduler_priority_affinity_SMP_Enqueue_scheduled_ordered(
432  Scheduler_Context    *context,
433  Scheduler_Node       *node,
434  Chain_Node_order      order,
435  Scheduler_SMP_Insert  insert_ready,
436  Scheduler_SMP_Insert  insert_scheduled
437)
438{
439  return _Scheduler_SMP_Enqueue_scheduled_ordered(
440    context,
441    node,
442    order,
443    _Scheduler_priority_SMP_Extract_from_ready,
444    _Scheduler_priority_affinity_SMP_Get_highest_ready,
445    insert_ready,
446    insert_scheduled,
447    _Scheduler_priority_SMP_Move_from_ready_to_scheduled,
448    _Scheduler_SMP_Allocate_processor_exact
449  );
450}
451
452/*
453 *  This is unique to this scheduler because it is on the path
454 *  to _Scheduler_priority_affinity_SMP_Enqueue_scheduled__ordered() which
455 *  invokes a scheduler unique get_lowest_scheduled helper.
456 */
457static bool _Scheduler_priority_affinity_SMP_Enqueue_scheduled_lifo(
458  Scheduler_Context *context,
459  Scheduler_Node    *node
460)
461{
462  return _Scheduler_priority_affinity_SMP_Enqueue_scheduled_ordered(
463    context,
464    node,
465    _Scheduler_SMP_Insert_priority_lifo_order,
466    _Scheduler_priority_SMP_Insert_ready_lifo,
467    _Scheduler_SMP_Insert_scheduled_lifo
468  );
469}
470
471/*
472 *  This is unique to this scheduler because it is on the path
473 *  to _Scheduler_priority_affinity_SMP_Enqueue_scheduled__ordered() which
474 *  invokes a scheduler unique get_lowest_scheduled helper.
475 */
476static bool _Scheduler_priority_affinity_SMP_Enqueue_scheduled_fifo(
477  Scheduler_Context *context,
478  Scheduler_Node    *node
479)
480{
481  return _Scheduler_priority_affinity_SMP_Enqueue_scheduled_ordered(
482    context,
483    node,
484    _Scheduler_SMP_Insert_priority_fifo_order,
485    _Scheduler_priority_SMP_Insert_ready_fifo,
486    _Scheduler_SMP_Insert_scheduled_fifo
487  );
488}
489
490static bool _Scheduler_priority_affinity_SMP_Do_ask_for_help(
491  Scheduler_Context *context,
492  Thread_Control    *the_thread,
493  Scheduler_Node    *node
494)
495{
496  return _Scheduler_SMP_Ask_for_help(
497    context,
498    the_thread,
499    node,
500    _Scheduler_SMP_Insert_priority_lifo_order,
501    _Scheduler_priority_SMP_Insert_ready_lifo,
502    _Scheduler_SMP_Insert_scheduled_lifo,
503    _Scheduler_priority_SMP_Move_from_scheduled_to_ready,
504    _Scheduler_SMP_Get_lowest_scheduled,
505    _Scheduler_SMP_Allocate_processor_lazy
506  );
507}
508
509void _Scheduler_priority_affinity_SMP_Update_priority(
510  const Scheduler_Control *scheduler,
511  Thread_Control          *thread,
512  Scheduler_Node          *node
513)
514{
515  Scheduler_Context *context = _Scheduler_Get_context( scheduler );
516
517  _Scheduler_SMP_Update_priority(
518    context,
519    thread,
520    node,
521    _Scheduler_priority_SMP_Extract_from_ready,
522    _Scheduler_priority_SMP_Do_update,
523    _Scheduler_priority_affinity_SMP_Enqueue_fifo,
524    _Scheduler_priority_affinity_SMP_Enqueue_lifo,
525    _Scheduler_priority_affinity_SMP_Enqueue_scheduled_fifo,
526    _Scheduler_priority_affinity_SMP_Enqueue_scheduled_lifo,
527    _Scheduler_priority_affinity_SMP_Do_ask_for_help
528  );
529
530  /*
531   * Perform any thread migrations that are needed due to these changes.
532   */
533  _Scheduler_priority_affinity_SMP_Check_for_migrations( context );
534}
535
536bool _Scheduler_priority_affinity_SMP_Ask_for_help(
537  const Scheduler_Control *scheduler,
538  Thread_Control          *the_thread,
539  Scheduler_Node          *node
540)
541{
542  Scheduler_Context *context = _Scheduler_Get_context( scheduler );
543
544  return _Scheduler_priority_affinity_SMP_Do_ask_for_help( context, the_thread, node );
545}
546
547void _Scheduler_priority_affinity_SMP_Reconsider_help_request(
548  const Scheduler_Control *scheduler,
549  Thread_Control          *the_thread,
550  Scheduler_Node          *node
551)
552{
553  Scheduler_Context *context = _Scheduler_Get_context( scheduler );
554
555  _Scheduler_SMP_Reconsider_help_request(
556    context,
557    the_thread,
558    node,
559    _Scheduler_priority_SMP_Extract_from_ready
560  );
561}
562
563void _Scheduler_priority_affinity_SMP_Withdraw_node(
564  const Scheduler_Control *scheduler,
565  Thread_Control          *the_thread,
566  Scheduler_Node          *node,
567  Thread_Scheduler_state   next_state
568)
569{
570  Scheduler_Context *context = _Scheduler_Get_context( scheduler );
571
572  _Scheduler_SMP_Withdraw_node(
573    context,
574    the_thread,
575    node,
576    next_state,
577    _Scheduler_priority_SMP_Extract_from_ready,
578    _Scheduler_priority_affinity_SMP_Get_highest_ready,
579    _Scheduler_priority_SMP_Move_from_ready_to_scheduled,
580    _Scheduler_SMP_Allocate_processor_lazy
581  );
582}
583
584void _Scheduler_priority_affinity_SMP_Add_processor(
585  const Scheduler_Control *scheduler,
586  Thread_Control          *idle
587)
588{
589  Scheduler_Context *context = _Scheduler_Get_context( scheduler );
590
591  _Scheduler_SMP_Add_processor(
592    context,
593    idle,
594    _Scheduler_priority_SMP_Has_ready,
595    _Scheduler_priority_affinity_SMP_Enqueue_scheduled_fifo
596  );
597}
598
599Thread_Control *_Scheduler_priority_affinity_SMP_Remove_processor(
600  const Scheduler_Control *scheduler,
601  Per_CPU_Control         *cpu
602)
603{
604  Scheduler_Context *context = _Scheduler_Get_context( scheduler );
605
606  return _Scheduler_SMP_Remove_processor(
607    context,
608    cpu,
609    _Scheduler_priority_SMP_Extract_from_ready,
610    _Scheduler_priority_affinity_SMP_Enqueue_fifo
611  );
612}
613
614bool _Scheduler_priority_affinity_SMP_Set_affinity(
615  const Scheduler_Control *scheduler,
616  Thread_Control          *thread,
617  const Processor_mask    *affinity
618)
619{
620  Scheduler_priority_affinity_SMP_Node *node;
621  States_Control                        current_state;
622  cpu_set_t                             cpuset;
623  size_t                                cpusetsize;
624
625  cpusetsize = sizeof( cpuset );
626  _Processor_mask_To_cpu_set_t( affinity, cpusetsize, &cpuset );
627  /*
628   * Validate that the cpset meets basic requirements.
629   */
630  if ( !_CPU_set_Is_valid( &cpuset, cpusetsize ) ) {
631    return false;
632  }
633
634  node = _Scheduler_priority_affinity_SMP_Thread_get_node( thread );
635
636  /*
637   * The old and new set are the same, there is no point in
638   * doing anything.
639   */
640  if ( CPU_EQUAL_S( cpusetsize, &cpuset, node->Affinity.set ) )
641    return true;
642
643  current_state = thread->current_state;
644
645  if ( _States_Is_ready( current_state ) ) {
646    _Scheduler_priority_affinity_SMP_Block( scheduler, thread, &node->Base.Base.Base );
647  }
648
649  CPU_COPY( &cpuset, node->Affinity.set );
650
651  if ( _States_Is_ready( current_state ) ) {
652    /*
653     * FIXME: Do not ignore threads in need for help.
654     */
655    (void) _Scheduler_priority_affinity_SMP_Unblock( scheduler, thread, &node->Base.Base.Base );
656  }
657
658  return true;
659}
Note: See TracBrowser for help on using the repository browser.