Commit cd8b0159 authored by Matthieu Dorier's avatar Matthieu Dorier

PyBake server working

parents
Please see [http://www.mcs.anl.gov/research/projects/mochi/contributing/] for details.
\ No newline at end of file
Copyright (c) 2018, UChicago Argonne, LLC
All Rights Reserved
SDS TOOLS (ANL-SF-16-009)
OPEN SOURCE LICENSE
Under the terms of Contract No. DE-AC02-06CH11357 with UChicago Argonne,
LLC, the U.S. Government retains certain rights in this software.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions
are met:
1. Redistributions of source code must retain the above copyright notice,
this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright
notice, this list of conditions and the following disclaimer in the
documentation and/or other materials provided with the distribution.
3. Neither the names of UChicago Argonne, LLC or the Department of
Energy nor the names of its contributors may be used to endorse or
promote products derived from this software without specific prior
written permission.
******************************************************************************
DISCLAIMER
THE SOFTWARE IS SUPPLIED "AS IS" WITHOUT WARRANTY OF ANY KIND.
NEITHER THE UNTED STATES GOVERNMENT, NOR THE UNITED STATES DEPARTMENT
OF ENERGY, NOR UCHICAGO ARGONNE, LLC, NOR ANY OF THEIR EMPLOYEES,
MAKES ANY WARRANTY, EXPRESS OR IMPLIED, OR ASSUMES ANY LEGAL LIABILITY
OR RESPONSIBILITY FOR THE ACCURACY, COMPLETENESS, OR USEFULNESS OF ANY
INFORMATION, DATA, APPARATUS, PRODUCT, OR PROCESS DISCLOSED, OR REPRESENTS
THAT ITS USE WOULD NOT INFRINGE PRIVATELY OWNED RIGHTS.
******************************************************************************
from pybake.target import BakeStorageTarget
import _pybakeserver
import pymargo
from pybake.target import BakeStorageTarget
def make_pool(name, size, mode):
_pybakeserver.make_pool(name, size, mode)
class BakeProvider(pymargo.Provider):
def __init__(self, mid, mplex_id):
super(BakeProvider, self).__init__(mid, mplex_id)
self._bake_inst = _pybakeserver.register(mid._mid, mplex_id)
def add_storage_target(self, name):
tid = _pybakeserver.add_storage_target(self._bake_inst, name)
return BakeStorageTarget(tid)
def remove_storage_target(self, target):
return _pybakeserver.remove_storage_target(self._bake_inst, target._tid)
def remove_all_storage_targets(self):
return _pybakeserver.remove_all_storage_targets(self._bake_inst)
def count_storage_targets(self):
return _pybakeserver.count_storage_targets(self._bake_inst)
def list_storage_targets(self):
return _pybakeserver.list_storage_targets(self._bake_inst)
#define BOOST_NO_AUTO_PTR
#include <boost/python.hpp>
#include <boost/python/return_opaque_pointer.hpp>
#include <boost/python/handle.hpp>
#include <boost/python/enum.hpp>
#include <boost/python/def.hpp>
#include <boost/python/module.hpp>
#include <boost/python/return_value_policy.hpp>
#include <string>
#include <vector>
#include <cstring>
#include <iostream>
#include <margo.h>
#include <bake.h>
#include <bake-server.h>
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(margo_instance)
BOOST_PYTHON_OPAQUE_SPECIALIZED_TYPE_ID(bake_server_context_t)
namespace bpl = boost::python;
static bake_provider_t pybake_provider_register(margo_instance_id mid, uint8_t mplex_id) {
bake_provider_t provider;
int ret = bake_provider_register(mid, mplex_id, BAKE_ABT_POOL_DEFAULT, &provider);
if(ret != 0) return NULL;
else return provider;
}
static bake_target_id_t pybake_provider_add_storage_target(
bake_provider_t provider,
const std::string& target_name) {
bake_target_id_t target_id;
std::memset(&target_id, 0, sizeof(target_id));
bake_provider_add_storage_target(
provider, target_name.c_str(), &target_id);
return target_id;
}
static std::string pybake_target_id_to_string(bake_target_id_t tid) {
char id[37];
uuid_unparse(tid.id, id);
return std::string(id);
}
static bool pybake_provider_remove_storage_target(
bake_provider_t provider,
bake_target_id_t target_id)
{
return 0 == bake_provider_remove_storage_target(provider, target_id);
}
static bool pybake_provider_remove_all_storage_targets(
bake_provider_t provider)
{
return 0 == bake_provider_remove_all_storage_targets(provider);
}
static uint64_t pybake_provider_count_storage_targets(
bake_provider_t provider)
{
uint64_t n = 0;
bake_provider_count_storage_targets(provider, &n);
return n;
}
static bpl::list pybake_provider_list_storage_targets(
bake_provider_t provider)
{
std::vector<bake_target_id_t> result;
uint64_t n = pybake_provider_count_storage_targets(provider);
if(n == 0) return bpl::list();
result.resize(n);
bake_provider_list_storage_targets(provider, result.data());
bpl::list list_result;
for(const auto& t : result) list_result.append(t);
return list_result;
}
static bool pybake_make_pool(const std::string& pool_name,
size_t pool_size, mode_t mode) {
return 0 == bake_makepool(pool_name.c_str(), pool_size, mode);
}
BOOST_PYTHON_MODULE(_pybakeserver)
{
#define ret_policy_opaque bpl::return_value_policy<bpl::return_opaque_pointer>()
bpl::opaque<bake_server_context_t>();
bpl::class_<bake_target_id_t>("bake_target_id", bpl::no_init)
.def("__str__", pybake_target_id_to_string);
bpl::class_<bake_region_id_t>("bake_region_id", bpl::no_init);
bpl::def("register", &pybake_provider_register, ret_policy_opaque);
bpl::def("add_storage_target", &pybake_provider_add_storage_target);
bpl::def("remove_storage_target", &pybake_provider_remove_storage_target);
bpl::def("remove_all_storage_targets", &pybake_provider_remove_all_storage_targets);
bpl::def("count_storage_targets", &pybake_provider_count_storage_targets);
bpl::def("list_storage_targets", &pybake_provider_list_storage_targets);
bpl::def("make_pool", &pybake_make_pool);
#undef ret_policy_opaque
}
class BakeStorageTarget():
def __init__(self, tid):
self.__tid = tid
def __str__(self):
return str(self.__tid)
from distutils.core import setup
from distutils.extension import Extension
from distutils.sysconfig import get_config_vars
import os
import os.path
import sys
(opt,) = get_config_vars('OPT')
os.environ['OPT'] = " ".join(
flag for flag in opt.split() if flag != '-Wstrict-prototypes'
)
pybake_server_module = Extension('_pybakeserver', ["pybake/src/server.cpp"],
libraries=['boost_python','margo','bake-server'],
include_dirs=['.'],
depends=[])
setup(name='pybake',
version='0.1',
author='Matthieu Dorier',
description="""Python binding for BAKE""",
ext_modules=[ pybake_server_module ],
packages=['pybake']
)
from pymargo import MargoInstance
import pybake
import pybake.server
from pybake.server import BakeProvider
mid = MargoInstance('tcp')
mplex_id = 42
print "Server running at address "+str(mid.addr())+"with mplex_id="+str(mplex_id)
provider = BakeProvider(mid, mplex_id)
target = provider.add_storage_target("/dev/shm/baketarget")
print "target id is "+str(target)
print "number of targets: "+str(provider.count_storage_targets())
pybake.server.make_pool("/dev/shm/baketarget2", 2*8388608, 0664)
target = provider.add_storage_target("/dev/shm/baketarget2")
print "target id is "+str(target)
print "number of targets: "+str(provider.count_storage_targets())
print "storage targets: "
targets = provider.list_storage_targets()
for t in targets:
print str(t)
provider.remove_all_storage_targets()
print "number of targets: "+str(provider.count_storage_targets())
mid.wait_for_finalize()
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment