Skip to content
This repository has been archived by the owner on May 28, 2019. It is now read-only.

Commit

Permalink
sync changes from icode
Browse files Browse the repository at this point in the history
  • Loading branch information
taotaowill committed Jan 5, 2017
1 parent 03cbc71 commit b709442
Show file tree
Hide file tree
Showing 26 changed files with 694 additions and 90 deletions.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
*.o
*.a
*.so
*.pyc

# Folders
_obj
_test
Expand Down Expand Up @@ -56,4 +56,5 @@ galaxy.flag
*.INFO*
*.WARNING*
*.ERROR*
*~
galaxy.flag
10 changes: 5 additions & 5 deletions build_deps.sh
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,9 @@ fi
if [ ! -f "${FLAG_DIR}/sofa-pbrpc_1_0_0" ] \
|| [ ! -f "${DEPS_PREFIX}/lib/libsofa-pbrpc.a" ] \
|| [ ! -d "${DEPS_PREFIX}/include/sofa/pbrpc" ]; then
wget --no-check-certificate -O sofa-pbrpc-1.0.0.tar.gz https://github.com/baidu/sofa-pbrpc/archive/v1.0.0.tar.gz
tar zxf sofa-pbrpc-1.0.0.tar.gz
cd sofa-pbrpc-1.0.0
wget --no-check-certificate -O sofa-pbrpc-1.1.2.tar.gz https://github.com/baidu/sofa-pbrpc/archive/v1.1.2.tar.gz
tar zxf sofa-pbrpc-1.1.2.tar.gz
cd sofa-pbrpc-1.1.2
sed -i '/BOOST_HEADER_DIR=/ d' depends.mk
sed -i '/PROTOBUF_DIR=/ d' depends.mk
sed -i '/SNAPPY_DIR=/ d' depends.mk
Expand All @@ -86,7 +86,7 @@ if [ ! -f "${FLAG_DIR}/sofa-pbrpc_1_0_0" ] \
echo "SNAPPY_DIR=${DEPS_PREFIX}" >> depends.mk
echo "PREFIX=${DEPS_PREFIX}" >> depends.mk
cd src
PROTOBUF_DIR=${DEPS_PREFIX} sh compile_proto.sh
#PROTOBUF_DIR=${DEPS_PREFIX} sh compile_proto.sh
cd ..
make -j4
make install
Expand Down Expand Up @@ -185,7 +185,7 @@ if [ ! -f "${FLAG_DIR}/ins" ] \
|| [ ! -f "${DEPS_PREFIX}/lib/libins_sdk.a" ] \
|| [ ! -f "${DEPS_PREFIX}/include/ins_sdk.h" ]; then
rm -rf ins
git clone https://github.com/fxsjy/ins
git clone https://github.com/baidu/ins
cd ins
sed -i "s|^PREFIX=.*|PREFIX=${DEPS_PREFIX}|" Makefile
sed -i "s|^PROTOC=.*|PROTOC=${DEPS_PREFIX}/bin/protoc|" Makefile
Expand Down
1 change: 1 addition & 0 deletions src/agent/agent_main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ int main(int argc, char* argv[])
while (!s_quit) {
sleep(1);
}
_exit(0);

return 0;
}
Expand Down
7 changes: 6 additions & 1 deletion src/agent/cgroup/cgroup.cc
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,14 @@ baidu::galaxy::util::ErrorCode Cgroup::Construct() {
factory_->GetSubsystems(subsystems);
assert(subsystems.size() > 0);
bool ok = true;
bool use_galaxy_killer = false;
if (cgroup_->memory().has_use_galaxy_killer()
&& cgroup_->memory().use_galaxy_killer()) {
use_galaxy_killer = true;
}

for (size_t i = 0; i < subsystems.size(); i++) {
boost::shared_ptr<Subsystem> ss = factory_->CreateSubsystem(subsystems[i]);
boost::shared_ptr<Subsystem> ss = factory_->CreateSubsystem(subsystems[i], use_galaxy_killer);
assert(NULL != ss.get());
ss->SetContainerId(container_id_);
ss->SetCgroup(cgroup_);
Expand Down
101 changes: 101 additions & 0 deletions src/agent/cgroup/galaxy_memory_subsystem.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
// Copyright (c) 2016, Baidu.com, Inc. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "galaxy_memory_subsystem.h"
#include "agent/util/path_tree.h"
#include "protocol/galaxy.pb.h"
#include "protocol/agent.pb.h"
#include "util/input_stream_file.h"

#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <iostream>

namespace baidu {
namespace galaxy {
namespace cgroup {

GalaxyMemorySubsystem::GalaxyMemorySubsystem() {
}

GalaxyMemorySubsystem::~GalaxyMemorySubsystem() {
}

std::string GalaxyMemorySubsystem::Name() {
return "memory";
}


baidu::galaxy::util::ErrorCode GalaxyMemorySubsystem::Collect(boost::shared_ptr<baidu::galaxy::proto::CgroupMetrix> metrix) {
assert(NULL != metrix.get());
boost::filesystem::path path(Path());
path.append("memory.usage_in_bytes");
baidu::galaxy::file::InputStreamFile in(path.string());

if (!in.IsOpen()) {
baidu::galaxy::util::ErrorCode ec = in.GetLastError();
return ERRORCODE(-1, "open file(%s) failed: %s",
path.string().c_str(),
ec.Message().c_str());
}

std::string data;
baidu::galaxy::util::ErrorCode ec = in.ReadLine(data);

if (ec.Code() != 0) {
return ERRORCODE(-1, "read failed: %s", ec.Message().c_str());
}

metrix->set_memory_used_in_byte(::atol(data.c_str()));
return ERRORCODE_OK;
}

baidu::galaxy::util::ErrorCode GalaxyMemorySubsystem::Construct() {
assert(!this->container_id_.empty());
assert(NULL != this->cgroup_.get());
std::string path = this->Path();
boost::system::error_code ec;

if (!boost::filesystem::exists(path, ec)
&& !baidu::galaxy::file::create_directories(path, ec)) {
return ERRORCODE(-1, "failed in creating file %s: %s",
path.c_str(),
ec.message().c_str());
}

boost::filesystem::path memory_limit_path = path;
memory_limit_path.append("memory.limit_in_bytes");
baidu::galaxy::util::ErrorCode err;
err = baidu::galaxy::cgroup::Attach(memory_limit_path.c_str(),
-1, false);

if (0 != err.Code()) {
return ERRORCODE(-1,
"attch memory.limit_in_bytes failed: %s",
err.Message().c_str());
}

boost::filesystem::path kill_mode_path = path;
kill_mode_path.append("memory.kill_mode");
err = baidu::galaxy::cgroup::Attach(kill_mode_path.c_str(),
0L,
false);

if (0 != err.Code()) {
return ERRORCODE(-1,
"attch memory.kill_mode failed: %s",
err.Message().c_str());
}

return ERRORCODE_OK;
}

boost::shared_ptr<Subsystem> GalaxyMemorySubsystem::Clone() {
boost::shared_ptr<Subsystem> ret(new GalaxyMemorySubsystem());
return ret;
}


} //namespace cgroup
} //namespace galaxy
} //namespace baidu
26 changes: 26 additions & 0 deletions src/agent/cgroup/galaxy_memory_subsystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// Copyright (c) 2016, Baidu.com, Inc. All Rights Reserved
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#pragma once

#include "subsystem.h"
#include <boost/shared_ptr.hpp>

namespace baidu {
namespace galaxy {
namespace cgroup {

class GalaxyMemorySubsystem : public Subsystem {
public:
GalaxyMemorySubsystem();
~GalaxyMemorySubsystem();

boost::shared_ptr<Subsystem> Clone();
std::string Name();
baidu::galaxy::util::ErrorCode Construct();
baidu::galaxy::util::ErrorCode Collect(boost::shared_ptr<baidu::galaxy::proto::CgroupMetrix> metrix);
};

} //namespace cgroup
} //namespace galaxy
} //namespace baidu
21 changes: 17 additions & 4 deletions src/agent/cgroup/subsystem_factory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include "subsystem_factory.h"
#include "cpu_subsystem.h"
#include "memory_subsystem.h"
#include "galaxy_memory_subsystem.h"
#include "freezer_subsystem.h"
#include "netcls_subsystem.h"
#include "cpuacct_subsystem.h"
Expand All @@ -16,7 +17,13 @@ namespace baidu {
namespace galaxy {
namespace cgroup {

SubsystemFactory::SubsystemFactory() {
boost::shared_ptr<Subsystem> memory_;
boost::shared_ptr<Subsystem> galaxy_memory_;


SubsystemFactory::SubsystemFactory() :
memory_(new baidu::galaxy::cgroup::MemorySubsystem()),
galaxy_memory_(new baidu::galaxy::cgroup::GalaxyMemorySubsystem()) {
}

boost::shared_ptr<SubsystemFactory> SubsystemFactory::s_instance_(new SubsystemFactory());
Expand All @@ -36,20 +43,25 @@ SubsystemFactory* SubsystemFactory::Register(Subsystem* malloc_subsystem) {

void SubsystemFactory::Setup() {
this->Register(new baidu::galaxy::cgroup::CpuSubsystem())
->Register(new baidu::galaxy::cgroup::MemorySubsystem())
->Register(new baidu::galaxy::cgroup::FreezerSubsystem())
->Register(new baidu::galaxy::cgroup::TcpThrotSubsystem())
->Register(new baidu::galaxy::cgroup::CpuacctSubsystem())
->Register(new baidu::galaxy::cgroup::NetclsSubsystem());
//->Register(new baidu::galaxy::)
}

boost::shared_ptr<Subsystem> SubsystemFactory::CreateSubsystem(const std::string& name) {

boost::shared_ptr<Subsystem> SubsystemFactory::CreateSubsystem(const std::string& name, bool use_galaxy) {
boost::shared_ptr<Subsystem> ret;
std::map<std::string, boost::shared_ptr<Subsystem> >::iterator iter = cgroups_seed_.find(name);

if (cgroups_seed_.end() != iter) {
ret = iter->second->Clone();
} else if (name == "memory") {
if (use_galaxy) {
ret = galaxy_memory_->Clone();
} else {
ret = memory_->Clone();
}
}

return ret;
Expand All @@ -64,6 +76,7 @@ void SubsystemFactory::GetSubsystems(std::vector<std::string>& subsystems) {
subsystems.push_back(iter->first);
iter++;
}
subsystems.push_back("memory");
}

} //namespace cgroup
Expand Down
4 changes: 3 additions & 1 deletion src/agent/cgroup/subsystem_factory.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,16 @@ class SubsystemFactory : public boost::noncopyable {
static boost::shared_ptr<SubsystemFactory> GetInstance();
void Setup();

boost::shared_ptr<Subsystem> CreateSubsystem(const std::string& name);
boost::shared_ptr<Subsystem> CreateSubsystem(const std::string& name, bool use_galaxy_killer = false);
void GetSubsystems(std::vector<std::string>& subsystems);

private:
SubsystemFactory();
SubsystemFactory* Register(Subsystem* malloc_subsystem);
static boost::shared_ptr<SubsystemFactory> s_instance_;
std::map<const std::string, boost::shared_ptr<Subsystem> > cgroups_seed_;
boost::shared_ptr<Subsystem> memory_;
boost::shared_ptr<Subsystem> galaxy_memory_;
};

} //namespace cgroup
Expand Down
1 change: 1 addition & 0 deletions src/agent/util/dict_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ baidu::galaxy::util::ErrorCode DictFile::Scan(const std::string& begin_key,
it->Next();
}

delete it;
return ERRORCODE_OK;
}

Expand Down
14 changes: 12 additions & 2 deletions src/agent/util/input_stream_file.cc
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,24 @@ baidu::galaxy::util::ErrorCode InputStreamFile::ReadLine(std::string& line) {
ssize_t rsize = getline(&buf, &size, file_);

if (rsize == -1 && !feof(file_)) {
if (buf) {
free(buf);
buf = NULL;
}
return PERRORCODE(-1, errno_, "get line failed");
} else if (feof(file_)) {
if (buf) {
free(buf);
buf = NULL;
}
return ERRORCODE_OK;
}

line.assign(buf, rsize);
free(buf);
buf = NULL;
if (buf) {
free(buf);
buf = NULL;
}
return ERRORCODE_OK;
}

Expand Down
Loading

0 comments on commit b709442

Please sign in to comment.