1
+ #include " functions.hpp"
1
2
2
- #include < string>
3
- #include < vector>
4
3
#include < cstdint>
4
+ #include < cstdlib>
5
5
#include < cstring>
6
- #include < sys/types.h>
7
- #include < unistd.h>
8
- #include < sstream>
9
- #include < numeric>
10
- #include < fstream>
11
6
#include < filesystem>
7
+ #include < fstream>
8
+ #include < numeric>
9
+ #include < sstream>
10
+ #include < string>
11
+ #include < vector>
12
12
13
- #include " functions.hpp"
14
-
15
- #define CGROUP_CPUSET_PATH_PREFIX " /sys/fs/cgroup/cpuset"
16
- #define CPUSET_FILE " /cpuset.cpus"
13
+ const std::filesystem::path get_tmp () {
14
+ auto tmp = std::getenv (" TMPDIR" );
15
+ if (tmp != nullptr ) {
16
+ return std::filesystem::path{tmp};
17
+ }
18
+ tmp = std::getenv (" PBS_JOBFS" );
19
+ if (tmp != nullptr ) {
20
+ return std::filesystem::path{tmp};
21
+ } else {
22
+ return std::filesystem::path{" /tmp" };
23
+ }
24
+ };
17
25
18
- const char *get_tmp ()
19
- {
20
- const char *out = getenv (" TMPDIR" );
21
- if (out == nullptr )
22
- {
23
- out = " /tmp" ;
24
- }
25
- return out;
26
+ void die_with_err (std::string msg, int status) {
27
+ throw std::runtime_error (msg + " stat=" + std::to_string (status));
26
28
};
27
- void die_with_err (std::string msg, int status)
28
- {
29
- std::string out (msg);
30
- out.append (" \n stat=" + std::to_string (status) + " , errno=" + std::to_string (errno));
31
- out.append (std::string (" \n " ) + strerror (errno));
32
- throw std::runtime_error (out);
29
+
30
+ void die_with_err_errno (std::string msg, int status) {
31
+ std::string out (msg);
32
+ out.append (" \n stat=" + std::to_string (status) +
33
+ " , errno=" + std::to_string (errno));
34
+ out.append (std::string (" \n " ) + strerror (errno));
35
+ throw std::runtime_error (out);
33
36
};
34
- std::vector<uint32_t > parse_cpuset_range (std::string in)
35
- {
36
- std::stringstream ss1 (in);
37
- std::string token;
38
- std::vector<std::uint32_t > out;
39
- while (std::getline (ss1, token, ' ,' ))
40
- {
41
- if (token.find (' -' ) == std::string::npos)
42
- {
43
- out.push_back (std::stoul (token));
44
- }
45
- else
46
- {
47
- std::stringstream ss2 (token);
48
- std::string starts, ends;
49
- std::getline (ss2, starts, ' -' );
50
- std::getline (ss2, ends, ' -' );
51
- std::vector<std::uint32_t > tmp (std::stoul (ends) - std::stoul (starts) + 1 );
52
- std::iota (tmp.begin (), tmp.end (), std::stoul (starts));
53
- out.insert (out.end (), tmp.begin (), tmp.end ());
54
- }
37
+
38
+ std::vector<uint32_t > parse_cpuset_range (std::string in) {
39
+ std::stringstream ss1 (in);
40
+ std::string token;
41
+ std::vector<std::uint32_t > out;
42
+ while (std::getline (ss1, token, ' ,' )) {
43
+ if (token.find (' -' ) == std::string::npos) {
44
+ out.push_back (std::stoul (token));
45
+ } else {
46
+ std::stringstream ss2 (token);
47
+ std::string starts, ends;
48
+ std::getline (ss2, starts, ' -' );
49
+ std::getline (ss2, ends, ' -' );
50
+ std::vector<std::uint32_t > tmp (std::stoul (ends) - std::stoul (starts) + 1 );
51
+ std::iota (tmp.begin (), tmp.end (), std::stoul (starts));
52
+ out.insert (out.end (), tmp.begin (), tmp.end ());
55
53
}
56
- return out;
54
+ }
55
+ return out;
57
56
};
58
- std::vector<std::uint32_t > get_cgroup ()
59
- {
60
- std::filesystem::path cgroup_fn (std::string (" /proc/" + std::to_string (getpid ()) + " /cgroup" ));
61
- if (!std::filesystem::exists (cgroup_fn))
62
- {
63
- throw std::runtime_error (" Cgroup file for process " + std::to_string (getpid ()) + " not found" );
64
- }
65
- std::string line;
66
- std::filesystem::path cpuset_path;
67
- // get cpuset path
68
- std::ifstream cgroup_file (cgroup_fn);
69
- if (cgroup_file.is_open ())
70
- {
71
- while (std::getline (cgroup_file, line))
72
- {
73
- std::vector<std::string> seglist;
74
- std::string segment;
75
- std::stringstream ss (line);
76
- while (std::getline (ss, segment, ' :' ))
77
- {
78
- seglist.push_back (segment);
79
- };
80
- if (seglist[1 ] == " cpuset" )
81
- {
82
- cpuset_path = CGROUP_CPUSET_PATH_PREFIX;
83
- cpuset_path += seglist[2 ];
84
- cpuset_path += CPUSET_FILE;
85
- }
86
- if (!cpuset_path.empty ())
87
- {
88
- break ;
89
- }
90
- }
91
- cgroup_file.close ();
92
- }
93
- else
94
- {
95
- throw std::runtime_error (" Unable to open cgroup file " + cgroup_fn.string ());
96
- }
97
- // read cpuset file
98
- std::ifstream cpuset_file (cpuset_path);
99
- if (cpuset_file.is_open ())
100
- {
101
- std::getline (cpuset_file, line);
102
- return parse_cpuset_range (line);
103
- }
104
- else
105
- {
106
- throw std::runtime_error (" Unable to open cpuset file " + cpuset_path.string ());
57
+
58
+ std::vector<uint32_t > get_cgroup () {
59
+ static std::string cgroup_fn (" /proc/self/cgroup" );
60
+ static std::string cgroup_cpuset_path_prefix (" /sys/fs/cgroup/cpuset" );
61
+ static std::string cpuset_filename (" /cpuset.cpus" );
62
+ if (!std::filesystem::exists (cgroup_fn)) {
63
+ throw std::runtime_error (" Cgroup file for current process not found" );
64
+ }
65
+ std::string line;
66
+ std::filesystem::path cpuset_path;
67
+ // get cpuset path
68
+ std::ifstream cgroup_file (cgroup_fn);
69
+ if (cgroup_file.is_open ()) {
70
+ while (std::getline (cgroup_file, line)) {
71
+ std::vector<std::string> seglist;
72
+ std::string segment;
73
+ std::stringstream ss (line);
74
+ while (std::getline (ss, segment, ' :' )) {
75
+ seglist.push_back (segment);
76
+ };
77
+ if (seglist[1 ] == " cpuset" ) {
78
+ cpuset_path = cgroup_cpuset_path_prefix;
79
+ cpuset_path += seglist[2 ];
80
+ cpuset_path += cpuset_filename;
81
+ }
82
+ if (!cpuset_path.empty ()) {
83
+ break ;
84
+ }
107
85
}
86
+ cgroup_file.close ();
87
+ } else {
88
+ throw std::runtime_error (" Unable to open cgroup file " + cgroup_fn);
89
+ }
90
+ // read cpuset file
91
+ std::ifstream cpuset_file (cpuset_path);
92
+ if (cpuset_file.is_open ()) {
93
+ std::getline (cpuset_file, line);
94
+ return parse_cpuset_range (line);
95
+ } else {
96
+ throw std::runtime_error (" Unable to open cpuset file " +
97
+ cpuset_path.string ());
98
+ }
108
99
};
0 commit comments