File: core_scheduling.cc

package info (click to toggle)
chromium 120.0.6099.224-1~deb11u1
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 6,112,112 kB
  • sloc: cpp: 32,907,025; ansic: 8,148,123; javascript: 3,679,536; python: 2,031,248; asm: 959,718; java: 804,675; xml: 617,256; sh: 111,417; objc: 100,835; perl: 88,443; cs: 53,032; makefile: 29,579; fortran: 24,137; php: 21,162; tcl: 21,147; sql: 20,809; ruby: 17,735; pascal: 12,864; yacc: 8,045; lisp: 3,388; lex: 1,323; ada: 727; awk: 329; jsp: 267; csh: 117; exp: 43; sed: 37
file content (145 lines) | stat: -rw-r--r-- 4,457 bytes parent folder | download
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
// Copyright 2020 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "chromeos/system/core_scheduling.h"

#include <errno.h>
#include <sys/prctl.h>

#include <set>
#include <string>

#include "base/feature_list.h"
#include "base/files/file_util.h"
#include "base/logging.h"
#include "base/metrics/field_trial_params.h"
#include "base/strings/string_util.h"
#include "base/strings/stringprintf.h"
#include "base/system/sys_info.h"
#include "base/threading/thread_restrictions.h"

// Downstream core scheduling interface for 4.19, 5.4 kernels.
// TODO(b/152605392): Remove once those kernel versions are obsolete.
#ifndef PR_SET_CORE_SCHED
#define PR_SET_CORE_SCHED 0x200
#endif

// Upstream interface for core scheduling.
#ifndef PR_SCHED_CORE
#define PR_SCHED_CORE 62
#define PR_SCHED_CORE_GET 0
#define PR_SCHED_CORE_CREATE 1
#define PR_SCHED_CORE_SHARE_TO 2
#define PR_SCHED_CORE_SHARE_FROM 3
#define PR_SCHED_CORE_MAX 4
#endif
#ifndef PID_T_MAX
#define PID_T_MAX (1 << 30)
#endif

enum pid_type { PIDTYPE_PID = 0, PIDTYPE_TGID, PIDTYPE_PGID };

namespace chromeos {
namespace system {

namespace {
BASE_FEATURE(kCoreScheduling,
             "CoreSchedulingEnabled",
             base::FEATURE_ENABLED_BY_DEFAULT);
}

void EnableCoreSchedulingIfAvailable() {
  if (!base::FeatureList::IsEnabled(kCoreScheduling)) {
    return;
  }

  // prctl(2) will return EINVAL for unknown functions. We're tolerant to this
  // and will log an error message for non EINVAL errnos.
  if (prctl(PR_SET_CORE_SCHED, 1) == -1 &&
      prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, 0, PIDTYPE_PID, 0) == -1) {
    PLOG_IF(WARNING, errno != EINVAL) << "Unable to set core scheduling";
  }
}

bool IsCoreSchedulingAvailable() {
  static const bool kernel_support = []() {
    // Test for kernel 4.19, 5.4 downstream support.
    // Pass bad param `prctl(0x200, 2)`. If it is supported, we will get ERANGE
    // rather than EINVAL.
    if (prctl(PR_SET_CORE_SCHED, 2) == -1 && errno == ERANGE) {
      VLOG(1) << "Core scheduling legacy interface supported";
      return true;
    }

    // Test for kernel 5.10 upstream support.
    // Pass bad param pid=PID_T_MAX. If it is supported, we will get ENODEV
    // (supported by not enabled) or ESRCH (bad param).
    int res =
        prctl(PR_SCHED_CORE, PR_SCHED_CORE_CREATE, PID_T_MAX, PIDTYPE_PID, 0);
    int saved_errno = errno;
    if (res == -1 && saved_errno == ENODEV) {
      VLOG(1) << "Core scheduling supported, but not enabled "
                 "(likely because SMT is not enabled)";
      return false;
    }
    if (res == -1 && saved_errno == ESRCH) {
      VLOG(1) << "Core scheduling supported and enabled";
      return true;
    }
    VLOG(1) << "Core scheduling not supported in kernel";
    return false;
  }();
  if (!kernel_support) {
    return false;
  }

  static const bool has_vulns = []() {
    // Reading from sysfs doesn't block.
    base::ScopedAllowBlocking scoped_allow_blocking;

    base::FilePath sysfs_vulns("/sys/devices/system/cpu/vulnerabilities");
    std::string buf;
    for (const std::string& s : {"l1tf", "mds"}) {
      base::FilePath vuln = sysfs_vulns.Append(s);
      if (!base::ReadFileToString(vuln, &buf)) {
        LOG(ERROR) << "Could not read " << vuln;
        continue;
      }
      base::TrimWhitespaceASCII(buf, base::TRIM_ALL, &buf);
      if (buf != "Not affected") {
        VLOG(1) << "Core scheduling available: " << vuln << "=" << buf;
        return true;
      }
    }
    VLOG(1) << "Core scheduling not required";
    return false;
  }();
  return has_vulns;
}

int NumberOfPhysicalCores() {
  // cat /sys/devices/system/cpu/cpu[0-9]*/topology/thread_siblings_list |\
  //   sort -u | wc -l
  static const int num_physical_cores = []() {
    // Reading from sysfs doesn't block.
    base::ScopedAllowBlocking scoped_allow_blocking;

    std::set<std::string> lists;
    std::string buf;
    for (int i = 0; i < base::SysInfo::NumberOfProcessors(); ++i) {
      base::FilePath list(base::StringPrintf(
          "/sys/devices/system/cpu/cpu%d/topology/thread_siblings_list", i));
      if (!base::ReadFileToString(list, &buf)) {
        LOG(ERROR) << "Could not read " << list;
      } else {
        lists.insert(buf);
      }
    }
    return lists.size() > 0 ? lists.size() : 1;
  }();
  return num_physical_cores;
}

}  // namespace system
}  // namespace chromeos