-
Notifications
You must be signed in to change notification settings - Fork 0
Bind process to cpu #45
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
#include <fstream> | ||
#include <sstream> | ||
#include <cstring> | ||
#include <thread> | ||
#include <chrono> | ||
#include <unistd.h> | ||
#include <sched.h> | ||
|
||
std::vector<float> getCoreLoads() { | ||
std::vector<float> loads; | ||
std::ifstream stat("/proc/stat"); | ||
|
||
if (!stat.is_open()) { | ||
std::cerr << "Failed to open /proc/stat" << std::endl; | ||
return loads; | ||
} | ||
|
||
// Pass 1st string (with total cpu data) | ||
std::string line; | ||
std::getline(stat, line); | ||
|
||
// Read each cpu data | ||
std::vector<std::vector<unsigned long long>> prevCpuData; | ||
while (std::getline(stat, line)) { | ||
if (line.find("cpu") == 0) { | ||
std::istringstream stream(line); | ||
std::string cpuString; | ||
stream >> cpuString; | ||
if (cpuString.substr(0, 3) != "cpu") break; | ||
|
||
std::vector<unsigned long long> coreData; | ||
unsigned long long value; | ||
while (stream >> value) { | ||
coreData.push_back(value); | ||
} | ||
prevCpuData.push_back(coreData); | ||
} | ||
} | ||
stat.close(); | ||
|
||
// Wait for some time | ||
std::this_thread::sleep_for(std::chrono::milliseconds(500)); | ||
|
||
stat.open("/proc/stat"); | ||
std::getline(stat, line); // Pass 1st string | ||
|
||
std::vector<std::vector<unsigned long long>> currCpuData; | ||
for (size_t i = 0; i < prevCpuData.size(); ++i) { | ||
std::getline(stat, line); | ||
std::istringstream stream(line); | ||
std::string cpuString; | ||
stream >> cpuString; | ||
|
||
std::vector<unsigned long long> coreData; | ||
unsigned long long value; | ||
while (stream >> value) { | ||
coreData.push_back(value); | ||
} | ||
currCpuData.push_back(coreData); | ||
} | ||
stat.close(); | ||
|
||
// Calculate load for all cpus | ||
for (size_t i = 0; i < prevCpuData.size(); ++i) { | ||
unsigned long long prevIdle = prevCpuData[i][3] + prevCpuData[i][4]; | ||
unsigned long long currIdle = currCpuData[i][3] + currCpuData[i][4]; | ||
|
||
unsigned long long prevTotal = 0; | ||
|
||
for(int j = 0; j < prevCpuData[i].size(); j++) { | ||
prevTotal += prevCpuData[i][j]; | ||
} | ||
|
||
unsigned long long currTotal = 0; | ||
|
||
for(int j = 0; j < currCpuData[i].size(); j++) { | ||
currTotal += currCpuData[i][j]; | ||
} | ||
|
||
unsigned long long totalDiff = currTotal - prevTotal; | ||
unsigned long long idleDiff = currIdle - prevIdle; | ||
|
||
float usage = 100.0f * (totalDiff - idleDiff) / totalDiff; | ||
loads.push_back(usage); | ||
} | ||
|
||
return loads; | ||
} | ||
|
||
bool bindProcessToCore(pid_t pid, int core) { | ||
cpu_set_t cpuset; | ||
CPU_ZERO(&cpuset); | ||
CPU_SET(core, &cpuset); | ||
|
||
if (sched_setaffinity(pid, sizeof(cpu_set_t), &cpuset) != 0) { | ||
std::cerr << "Failed to bind process to cpu: " << strerror(errno) << std::endl; | ||
return false; | ||
} | ||
return true; | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Почему бы просто не использовать: https://docs.docker.com/reference/compose-file/services/#cpu_count? Мне кажется, что это лишняя логика для решения задачи
try { | ||
std::string hash = File::calculate_hash(argv[1], argv[2]); | ||
std::cout << "Digest is: " << hash << std::endl; | ||
} catch (const std::exception &e) { | ||
std::cerr << "Error: " << e.what() << std::endl; | ||
return 1; | ||
return 4; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Давайте введем нумерацию ошибок или будем использовать стандартную со знаком "-". https://docs.particle.io/reference/device-os/api/debugging/posix-errors/
No description provided.