File: cpu.cpp

package info (click to toggle)
mergerfs 2.40.2-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 5,000 kB
  • sloc: cpp: 58,559; ansic: 17,241; makefile: 348; python: 156; sh: 119
file content (163 lines) | stat: -rw-r--r-- 2,675 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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif

#include "cpu.hpp"
#include "ghc/filesystem.hpp"
#include "fmt/core.h"

#include <sched.h>

#include <fstream>


int
CPU::getaffinity(const pthread_t  thread_id_,
                 cpu_set_t       *cpuset_)
{
  CPU_ZERO(cpuset_);
  return sched_getaffinity(thread_id_,
                           sizeof(cpu_set_t),
                           cpuset_);
}

int
CPU::setaffinity(const pthread_t  thread_id_,
                 cpu_set_t       *cpuset_)
{
  return pthread_setaffinity_np(thread_id_,
                                sizeof(cpu_set_t),
                                cpuset_);
}

int
CPU::setaffinity(const pthread_t thread_id_,
                 const int       cpu_)
{
  cpu_set_t cpuset;

  CPU_ZERO(&cpuset);
  CPU_SET(cpu_,&cpuset);

  return CPU::setaffinity(thread_id_,&cpuset);
}

int
CPU::setaffinity(const pthread_t     thread_id_,
                 const std::set<int> cpus_)
{
  cpu_set_t cpuset;

  CPU_ZERO(&cpuset);
  for(auto const cpu : cpus_)
    CPU_SET(cpu,&cpuset);

  return CPU::setaffinity(thread_id_,&cpuset);
}

static
ghc::filesystem::path
generate_cpu_core_id_path(const int cpu_id_)
{
  const ghc::filesystem::path basepath{"/sys/devices/system/cpu"};

  return basepath / fmt::format("cpu{}",cpu_id_) / "topology" / "core_id";
}

int
CPU::count()
{
  int rv;
  cpu_set_t cpuset;

  rv = CPU::getaffinity(0,&cpuset);
  if(rv < 0)
    return rv;

  return CPU_COUNT(&cpuset);
}

CPU::CPUVec
CPU::cpus()
{
  cpu_set_t cpuset;
  CPU::CPUVec cpuvec;

  CPU::getaffinity(0,&cpuset);

  for(int i = 0; i < CPU_SETSIZE; i++)
    {
      if(!CPU_ISSET(i,&cpuset))
        continue;

      cpuvec.push_back(i);
    }

  return cpuvec;
}

CPU::CPU2CoreMap
CPU::cpu2core()
{
  cpu_set_t cpuset;
  CPU::CPU2CoreMap c2c;

  CPU::getaffinity(0,&cpuset);

  for(int i = 0; i < CPU_SETSIZE; i++)
    {
      int core_id;
      std::ifstream ifs;
      ghc::filesystem::path path;

      if(!CPU_ISSET(i,&cpuset))
        continue;

      path = ::generate_cpu_core_id_path(i);

      ifs.open(path);
      if(!ifs)
        break;

      ifs >> core_id;

      c2c[i] = core_id;

      ifs.close();
    }

  return c2c;
}

CPU::Core2CPUsMap
CPU::core2cpus()
{
  cpu_set_t cpuset;
  CPU::Core2CPUsMap c2c;

  CPU::getaffinity(0,&cpuset);

  for(int i = 0; i < CPU_SETSIZE; i++)
    {
      int core_id;
      std::ifstream ifs;
      ghc::filesystem::path path;

      if(!CPU_ISSET(i,&cpuset))
        continue;

      path = ::generate_cpu_core_id_path(i);

      ifs.open(path);
      if(!ifs)
        break;

      ifs >> core_id;

      c2c[core_id].insert(i);

      ifs.close();
    }

  return c2c;
}