• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2016 Catalysts GmbH
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <fstream>
17 #include <sstream>
18 
19 #include "common.h"
20 #include "vendor/tinyformat.hpp"
21 
22 namespace ebpf {
23 
read_cpu_range(std::string path)24 std::vector<int> read_cpu_range(std::string path) {
25   std::ifstream cpus_range_stream { path };
26   std::vector<int> cpus;
27   std::string cpu_range;
28 
29   while (std::getline(cpus_range_stream, cpu_range, ',')) {
30     std::size_t rangeop = cpu_range.find('-');
31     if (rangeop == std::string::npos) {
32       cpus.push_back(std::stoi(cpu_range));
33     }
34     else {
35       int start = std::stoi(cpu_range.substr(0, rangeop));
36       int end = std::stoi(cpu_range.substr(rangeop + 1));
37       for (int i = start; i <= end; i++)
38         cpus.push_back(i);
39     }
40   }
41   return cpus;
42 }
43 
get_online_cpus()44 std::vector<int> get_online_cpus() {
45   return read_cpu_range("/sys/devices/system/cpu/online");
46 }
47 
get_possible_cpus()48 std::vector<int> get_possible_cpus() {
49   return read_cpu_range("/sys/devices/system/cpu/possible");
50 }
51 
get_pid_exe(pid_t pid)52 std::string get_pid_exe(pid_t pid) {
53   char exe_path[4096];
54   int res;
55 
56   std::string exe_link = tfm::format("/proc/%d/exe", pid);
57   res = readlink(exe_link.c_str(), exe_path, sizeof(exe_path));
58   if (res == -1)
59     return "";
60   if (res >= sizeof(exe_path))
61     res = sizeof(exe_path) - 1;
62   exe_path[res] = '\0';
63   return std::string(exe_path);
64 }
65 
66 } // namespace ebpf
67