Skip to content
This repository was archived by the owner on Jul 18, 2025. It is now read-only.

cgroup: convert bash to Cpp #401

Open
outscale-hmi wants to merge 1 commit intooutscale:developfrom
outscale-hmi:fixCgroup
Open

cgroup: convert bash to Cpp #401
outscale-hmi wants to merge 1 commit intooutscale:developfrom
outscale-hmi:fixCgroup

Conversation

@outscale-hmi
Copy link
Copy Markdown

Signed-off-by: hanen mizouni hanen.mizouni@outscale.com

@outscale-mgo
Copy link
Copy Markdown

Can one of the admins verify this patch?

Copy link
Copy Markdown

@outscale-mgo outscale-mgo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

small detail, and indentations

Comment thread api/server/app.cc Outdated
LOG_WARNING_("can't create butterfly cgroup, fail cmd '%s'",
create_dir.c_str());
std::string directory = std::string(cgroupPath) + "/butterfly" ;
if (mkdir((char*)directory.c_str(),S_IWGRP) < 0){
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mkdir take a const char * in parameter, so I don't think the cast is necessary.
Also, if you must cast, please use a C++ cast. (const_cast,static_cast,bit_cast,reinterpret_cast)

Comment thread api/server/app.cc Outdated
create_dir.c_str());
std::string directory = std::string(cgroupPath) + "/butterfly" ;
if (mkdir((char*)directory.c_str(),S_IWGRP) < 0){
LOG_WARNING_("can't create directory already exists\n");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2 space instead of 4

Comment thread api/server/app.cc Outdated
std::ifstream ifs(file1);
std::ofstream ofs(file2);
if((ifs.is_open()) && (ofs.is_open())){
ofs << ifs.rdbuf();
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad indentation

Comment thread api/server/app.cc Outdated
std::ifstream in(fileToRead);
std::ofstream out(fileToWrite);
if ((in.is_open()) && (out.is_open())){
std::string s;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad indentation

Comment thread api/server/app.cc Outdated

int
main(int argc, char *argv[]) {
int main(int argc, char *argv[]) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, that might be right, bit that's unrelated to this commit, so if you want to push that fix, please do it in a separated commit.

Comment thread api/server/app.cc Outdated
int setCgroups(std::string file1, std::string file2) {
std::ifstream ifs(file1);
std::ofstream ofs(file2);
if ((ifs.is_open()) && (ofs.is_open())) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could be simplify to:

if (!ifs.is_open() || !ofs.is_open())
  return -1;
ofs << ifs.rdbuf();
return 0;

not a big deal, but economise 3 lines of code

Comment thread api/server/app.cc Outdated
std::ofstream out(fileToWrite);
if ((in.is_open()) && (out.is_open())) {
std::string s;
while (getline(in, s)) {
Copy link
Copy Markdown

@outscale-mgo outscale-mgo Mar 16, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this file should contain a number, I don't think the loop is necessary

Comment thread api/server/app.cc Outdated
fileToRead = std::string(cgroupPath) + "/cpuset.mems";
if (!access(fileToRead.c_str(), R_OK)) {
fileToWrite = std::string(cgroupPath) + "/butterfly/cpuset.mems";
check = setCgroups(fileToRead, fileToWrite);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!setCgroups(fileToRead, fileToWrite)) {
    LOG_WARNING_("can't set cgroup cpuset.mems");
}

Comment thread api/server/app.cc Outdated
fileToRead = std::string(cgroupPath) + "/cpuset.cpus";
fileToWrite = std::string(cgroupPath) + "/butterfly/cpuset.mems";
check = setCgroups(fileToRead, fileToWrite);
if (check != 0) {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!setCgroups(fileToRead, fileToWrite)) {
LOG_WARNING_("can't set cgroup cpuset.cpus");
}

Comment thread api/server/app.cc Outdated
if (oss.str().compare(word)) {
LOG_WARNING_("can't properly set cgroup pid");
return;
} else {
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

as you return, you can remove the else

Comment thread api/server/app.cc Outdated
BASH(setStr) {
int check = 0;
std::string fileToWrite = std::string(SrcCgroup()) + "/butterfly/tasks";
check = setCgroups(oss.str(), fileToWrite);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (!setCgroups(oss.str(), fileToWrite)) {
         LOG_WARNING_("can't set cgroup pid");
}

Comment thread api/server/app.cc Outdated
LOG_WARNING_("can't unset task from butterfly cgroup");

std::string fileToRead = std::string(SrcCgroup()) + "/butterfly/tasks";
std::string fileToWrite = std::string(SrcCgroup()) + "/bin/bash /tasks";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and is wrong here too, sorry, I think it shoud be
std::string(SrcCgroup()) + "/task"

Copy link
Copy Markdown

@outscale-mgo outscale-mgo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It should be the last one,
Also I'd like to have more information on why, what and how you are doing this.

Comment thread api/server/app.cc Outdated
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 already exists\n");
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think the "already exists" is true,
I mean if mkdir fail, all you know is that you was unable to create the dir, assuming it's because the file alerady exist seems odd to me, you should either use errno or something to know why this fail, or just write "can't create directory: %s", directory.c_str()

Comment thread api/server/app.cc
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);
Copy link
Copy Markdown

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 ?

Comment thread api/server/app.cc Outdated
}
BASH("rmdir " + std::string(SrcCgroup()) + "/butterfly") {

int check = 0;
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

well, you can remove this

Comment thread api/server/app.cc Outdated

int check = 0;
std::string dirToRemove = std::string(SrcCgroup()) + "/butterfly";
check = rmdir(dirToRemove.c_str());
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if (rmdir(dirToRemove.c_str()) < 0) {
           LOG_WARNING_("can't destroy cgroup");
}

also rmdir man said that if rmdir fail, it return -1, so using !check is wrong here, and do the exact opposite of what you want

@outscale-hmi outscale-hmi changed the title cgroup: convert bash to Cpp cgroup: convert bash to Cpp Mar 18, 2020
Signed-off-by: hanen mizouni <hanen.mizouni@outscale.com>
Comment thread api/server/app.cc
}

fileToRead = std::string(cgroupPath) + "/cpuset.cpus";
fileToWrite = std::string(cgroupPath) + "/butterfly/cpuset.mems";
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/cpuset.mems/cpuset.cpus/

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants