-
Notifications
You must be signed in to change notification settings - Fork 19
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
cgroup: convert bash to Cpp #401
Open
outscale-hmi
wants to merge
1
commit into
outscale:develop
Choose a base branch
from
outscale-hmi:fixCgroup
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+65
−36
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -21,12 +21,16 @@ extern "C" { | |
#include <glib.h> | ||
#include <packetgraph/packetgraph.h> | ||
} | ||
#include <sys/types.h> | ||
#include <sys/stat.h> | ||
#include <iostream> | ||
#include <ctime> | ||
#include <thread> | ||
#include <fstream> | ||
#include <string> | ||
#include <sstream> | ||
#include <cerrno> | ||
#include <climits> | ||
#include <memory> | ||
#include "api/server/app.h" | ||
#include "api/server/graph.h" | ||
|
@@ -445,8 +449,6 @@ struct pg_error *pg_error; | |
|
||
} // namespace app | ||
|
||
#define BASH(STR) if (system(("/bin/bash -c \"" + (STR) + "\"").c_str())) | ||
|
||
static const char *SrcCgroup() { | ||
if (!access("/sys/fs/cgroup/cpu/cpu.shares", R_OK)) { | ||
return "/sys/fs/cgroup/cpu"; | ||
|
@@ -456,34 +458,52 @@ static const char *SrcCgroup() { | |
return NULL; | ||
} | ||
|
||
int setCgroups(std::string file1, std::string file2) { | ||
std::ifstream ifs(file1); | ||
std::ofstream ofs(file2); | ||
|
||
if ((!ifs.is_open()) || (!ofs.is_open())) | ||
return -1; | ||
ofs << ifs.rdbuf(); | ||
return 0; | ||
} | ||
|
||
static int InitCgroup(int multiplier) { | ||
const char *cgroupPath = SrcCgroup(); | ||
|
||
if (!cgroupPath) | ||
return -1; | ||
std::string create_dir("mkdir " + std::string(cgroupPath) + "/butterfly"); | ||
|
||
BASH(create_dir) { | ||
LOG_WARNING_("can't create butterfly cgroup, fail cmd '%s'", | ||
create_dir.c_str()); | ||
std::string directory = std::string(cgroupPath) + "/butterfly"; | ||
if (mkdir(directory.c_str(), S_IWGRP) < 0) { | ||
LOG_WARNING_("can't create directory %s\n", directory.c_str()); | ||
} | ||
BASH("echo $(( `cat " + std::string(cgroupPath) + "/cpu.shares` * " + | ||
std::to_string(multiplier) + " )) > " + | ||
cgroupPath + "/butterfly/cpu.shares") { | ||
std::string fileToRead = std::string(cgroupPath) + "/cpu.shares"; | ||
std::string fileToWrite = std::string(cgroupPath) + "/butterfly/cpu.shares"; | ||
std::ifstream in(fileToRead); | ||
std::ofstream out(fileToWrite); | ||
if ((in.is_open()) && (out.is_open())) { | ||
int nb; | ||
in >> nb; | ||
std::string s = std::to_string(nb * multiplier); | ||
out << s << std::endl; | ||
} else { | ||
LOG_WARNING_("can't set cgroup priority"); | ||
} | ||
|
||
// new cgroup use a diferent directory for cpushare and cpu | ||
// if not, we need to initilize some files | ||
BASH(std::string("! cat ") + cgroupPath + "/cpuset.mems >> /dev/null") { | ||
BASH("echo `cat " + std::string(cgroupPath) + "/cpuset.mems` > " + | ||
cgroupPath + "/butterfly/cpuset.mems") { | ||
LOG_WARNING_("can't set cgroup cpuset.mems"); | ||
} | ||
BASH("echo `cat " + std::string(cgroupPath) + "/cpuset.cpus` > " + | ||
cgroupPath + "/butterfly/cpuset.cpus") { | ||
LOG_WARNING_("can't set cgroup cpuset.cpus"); | ||
} | ||
fileToRead = std::string(cgroupPath) + "/cpuset.mems"; | ||
if (!access(fileToRead.c_str(), R_OK)) { | ||
fileToWrite = std::string(cgroupPath) + "/butterfly/cpuset.mems"; | ||
if (setCgroups(fileToRead, fileToWrite) != 0) { | ||
LOG_WARNING_("can't set cgroup cpuset.mems"); | ||
} | ||
|
||
fileToRead = std::string(cgroupPath) + "/cpuset.cpus"; | ||
fileToWrite = std::string(cgroupPath) + "/butterfly/cpuset.mems"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. s/cpuset.mems/cpuset.cpus/ There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this code was useful for centos6, so we might need to test it there before merging |
||
if (setCgroups(fileToRead, fileToWrite) != 0) { | ||
LOG_WARNING_("can't set cgroup cpuset.cpus"); | ||
} | ||
} | ||
return 0; | ||
} | ||
|
@@ -494,36 +514,45 @@ void app::SetCgroup() { | |
std::string setStr; | ||
std::string unsetOtherStr; | ||
std::ostringstream oss; | ||
|
||
oss << app::config.tid; | ||
setStr = "echo " + oss.str() + " > " + SrcCgroup() + "/butterfly/tasks"; | ||
unsetOtherStr = "grep -v " + oss.str() + " " + SrcCgroup() + | ||
"/butterfly/tasks | while read ligne; do echo $ligne > " + | ||
SrcCgroup() + "/tasks ; done"; | ||
|
||
BASH(setStr) { | ||
std::string fileToWrite = std::string(SrcCgroup()) + "/butterfly/tasks"; | ||
if (!setCgroups(oss.str(), fileToWrite)) { | ||
LOG_WARNING_("can't set cgroup pid"); | ||
} | ||
BASH(unsetOtherStr) { | ||
LOG_WARNING_("can't properly set cgroup pid"); | ||
std::ifstream ifs(oss.str()); | ||
std::ofstream ofs(fileToWrite); | ||
std::string word, ligne; | ||
while (ifs >> word) { | ||
if (oss.str().compare(word)) { | ||
LOG_WARNING_("can't properly set cgroup pid"); | ||
return; | ||
} | ||
ofs << word; | ||
} | ||
} | ||
|
||
void app::DestroyCgroup() { | ||
if (!SrcCgroup()) | ||
return; | ||
BASH("cat " + std::string(SrcCgroup()) + | ||
"/butterfly/tasks | while read ligne; do echo $ligne > " + | ||
SrcCgroup() + "/bin/bash /tasks ; done") { | ||
LOG_WARNING_("can't unset task from butterfly cgroup"); | ||
|
||
std::string fileToRead = std::string(SrcCgroup()) + "/butterfly/tasks"; | ||
std::string fileToWrite = std::string(SrcCgroup()) + "/tasks"; | ||
std::ifstream in(fileToRead); | ||
std::ofstream out(fileToWrite); | ||
if ((!in.is_open()) || (!out.is_open())) { | ||
LOG_WARNING_("can't set cgroup priority"); | ||
} else { | ||
std::string s; | ||
if (getline(in, s)) { | ||
out << s << std::endl; | ||
} | ||
} | ||
BASH("rmdir " + std::string(SrcCgroup()) + "/butterfly") { | ||
|
||
std::string dirToRemove = std::string(SrcCgroup()) + "/butterfly"; | ||
if (rmdir(dirToRemove.c_str())) { | ||
LOG_WARNING_("can't destroy cgroup"); | ||
} | ||
} | ||
|
||
#undef BASH | ||
|
||
int | ||
main(int argc, char *argv[]) { | ||
try { | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
I think a blank line is needed between declaration and code ?