Skip to content
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
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

outscale-hmi
Copy link

Signed-off-by: hanen mizouni [email protected]

@outscale-mgo
Copy link
Contributor

Can one of the admins verify this patch?

Copy link
Contributor

@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

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
Contributor

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)

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
Contributor

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

std::ifstream ifs(file1);
std::ofstream ofs(file2);
if((ifs.is_open()) && (ofs.is_open())){
ofs << ifs.rdbuf();
Copy link
Contributor

Choose a reason for hiding this comment

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

bad indentation

std::ifstream in(fileToRead);
std::ofstream out(fileToWrite);
if ((in.is_open()) && (out.is_open())){
std::string s;
Copy link
Contributor

Choose a reason for hiding this comment

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

bad indentation


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

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.

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
Contributor

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

std::ofstream out(fileToWrite);
if ((in.is_open()) && (out.is_open())) {
std::string s;
while (getline(in, s)) {
Copy link
Contributor

@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

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
Contributor

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");
}

fileToRead = std::string(cgroupPath) + "/cpuset.cpus";
fileToWrite = std::string(cgroupPath) + "/butterfly/cpuset.mems";
check = setCgroups(fileToRead, fileToWrite);
if (check != 0) {
Copy link
Contributor

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");
}

if (oss.str().compare(word)) {
LOG_WARNING_("can't properly set cgroup pid");
return;
} else {
Copy link
Contributor

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

BASH(setStr) {
int check = 0;
std::string fileToWrite = std::string(SrcCgroup()) + "/butterfly/tasks";
check = setCgroups(oss.str(), fileToWrite);
Copy link
Contributor

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");
}

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
Contributor

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
Contributor

@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.

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
Contributor

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()

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
Contributor

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 ?

}
BASH("rmdir " + std::string(SrcCgroup()) + "/butterfly") {

int check = 0;
Copy link
Contributor

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


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

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 <[email protected]>
}

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

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
Contributor

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 join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants