File: thread_sched.c

package info (click to toggle)
mesa 26.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 326,356 kB
  • sloc: ansic: 2,261,063; xml: 1,035,283; cpp: 528,134; python: 83,460; asm: 40,568; yacc: 12,040; lisp: 3,663; lex: 3,461; sh: 1,035; makefile: 223
file content (91 lines) | stat: -rw-r--r-- 2,643 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
 * Copyright 2023 Advanced Micro Devices, Inc.
 *
 * SPDX-License-Identifier: MIT
 */

#include "thread_sched.h"
#include "u_cpu_detect.h"
#include "u_debug.h"

DEBUG_GET_ONCE_BOOL_OPTION(pin_threads, "mesa_pin_threads", false)

bool
util_thread_scheduler_enabled(void)
{
#if DETECT_ARCH_X86 || DETECT_ARCH_X86_64
   return util_get_cpu_caps()->num_L3_caches > 1 ||
          debug_get_option_pin_threads();
#else
   return false;
#endif
}

void
util_thread_scheduler_init_state(unsigned *state)
{
   *state = UINT32_MAX;

   util_thread_sched_apply_policy(thrd_current(), UTIL_THREAD_APP_CALLER, 0,
                                  NULL); /* keep as NULL */
}

/**
 * Apply the optimal thread scheduling policy for the given thread.
 *
 * "name" determines which thread the policy is being applied to.
 *
 * "app_thread_cpu" is the CPU where the application thread currently
 * resides.
 *
 * "sched_state" is a per-gl_context state that this function uses to track
 * what happened in previous invocations.
 */
bool
util_thread_sched_apply_policy(thrd_t thread, enum util_thread_name name,
                               unsigned app_thread_cpu, unsigned *sched_state)
{
#if DETECT_ARCH_X86 || DETECT_ARCH_X86_64
   if (debug_get_option_pin_threads()) {
      /* Pin threads to a specific CPU. This is done only once. *sched_state
       * is true if this is the first time we are doing it.
       */
      if (sched_state && !*sched_state)
         return false;

      /* Each thread is assigned to a different CPU. */
      unsigned mask = BITFIELD_BIT(name);
      if (sched_state)
         *sched_state = 0;
      return util_set_thread_affinity(thread, &mask, NULL, 32);
   }

   /* Don't do anything for the app thread with the L3 chasing policy. */
   if (name == UTIL_THREAD_APP_CALLER)
      return false;

   /* Move Mesa threads to the L3 core complex where the app thread
    * resides. We call this "L3 chasing".
    *
    * This improves multithreading performance by up to 33% on Ryzen 3900X.
    */
   const struct util_cpu_caps_t *caps = util_get_cpu_caps();
   int L3_cache = caps->cpu_to_L3[app_thread_cpu];

   /* Don't do anything if the app thread hasn't moved to a different
    * core complex. (*sched_state contains the last set L3 index)
    */
   if (L3_cache == U_CPU_INVALID_L3 ||
       (sched_state && L3_cache == *sched_state))
      return false;

   /* Apply the policy. */
   if (sched_state)
      *sched_state = L3_cache;

   return util_set_thread_affinity(thread, caps->L3_affinity_mask[L3_cache],
                                   NULL, caps->num_cpu_mask_bits);
#else
   return false;
#endif
}