From 93b7899156ac9fee75c4a6a8b65b92b265d61d4f Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Fri, 18 Dec 2015 00:18:48 +0100 Subject: [PATCH] [surf] new (abstract) class: PropertyHolder, with only one purpose --- src/surf/PropertyHolder.cpp | 43 ++++++++++++++++++++++++++++++++ src/surf/PropertyHolder.hpp | 33 ++++++++++++++++++++++++ src/surf/host_interface.cpp | 14 +++-------- src/surf/host_interface.hpp | 11 ++------ src/surf/network_interface.cpp | 12 ++------- src/surf/network_interface.hpp | 8 ++---- src/surf/storage_interface.cpp | 14 +++-------- src/surf/storage_interface.hpp | 9 ++----- tools/cmake/DefinePackages.cmake | 2 ++ 9 files changed, 93 insertions(+), 53 deletions(-) create mode 100644 src/surf/PropertyHolder.cpp create mode 100644 src/surf/PropertyHolder.hpp diff --git a/src/surf/PropertyHolder.cpp b/src/surf/PropertyHolder.cpp new file mode 100644 index 0000000000..cdd87bee05 --- /dev/null +++ b/src/surf/PropertyHolder.cpp @@ -0,0 +1,43 @@ +/* Copyright (c) 2015. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#include "xbt/sysdep.h" +#include "PropertyHolder.hpp" + +namespace simgrid { +namespace surf { + +PropertyHolder::PropertyHolder(xbt_dict_t props) +: p_properties(props) +{ +} + +PropertyHolder::~PropertyHolder() { + xbt_dict_free(&p_properties); +} + +/** @brief Return the property associated to the provided key (or NULL if not existing) */ +const char *PropertyHolder::getProperty(const char*key) { + if (p_properties == NULL) + return NULL; + return (const char*) xbt_dict_get_or_null(p_properties,key); +} + +/** @brief Change the value of a given key in the property set */ +void PropertyHolder::setProperty(const char*key, const char*value) { + if (!p_properties) + p_properties = xbt_dict_new(); + xbt_dict_set(p_properties, key, xbt_strdup(value), &xbt_free_f); +} + +/** @brief Return the whole set of properties. Don't mess with it, dude! */ +xbt_dict_t PropertyHolder::getProperties() { + if (!p_properties) + p_properties = xbt_dict_new(); + return p_properties; +} + +} /* namespace surf */ +} /* namespace simgrid */ diff --git a/src/surf/PropertyHolder.hpp b/src/surf/PropertyHolder.hpp new file mode 100644 index 0000000000..bd03e3cf1b --- /dev/null +++ b/src/surf/PropertyHolder.hpp @@ -0,0 +1,33 @@ +/* Copyright (c) 2015. The SimGrid Team. All rights reserved. */ + +/* This program is free software; you can redistribute it and/or modify it + * under the terms of the license (GNU LGPL) which comes with this package. */ + +#ifndef SRC_SURF_PROPERTYHOLDER_HPP_ +#define SRC_SURF_PROPERTYHOLDER_HPP_ +#include + +namespace simgrid { +namespace surf { + +/** @brief a PropertyHolder can be given a set of textual properties + * + * Common PropertyHolders are elements of the platform file, such as Host, Link or Storage. + */ +class PropertyHolder { // DO NOT DERIVE THIS CLASS, or the diamond inheritance mayhem will get you + +public: + PropertyHolder(xbt_dict_t props); + ~PropertyHolder(); + + const char *getProperty(const char*id); + void setProperty(const char*id, const char*value); + xbt_dict_t getProperties(); +private: + xbt_dict_t p_properties = NULL; +}; + +} /* namespace surf */ +} /* namespace simgrid */ + +#endif /* SRC_SURF_PROPERTYHOLDER_HPP_ */ diff --git a/src/surf/host_interface.cpp b/src/surf/host_interface.cpp index 2d87c0dbcb..134e10683f 100644 --- a/src/surf/host_interface.cpp +++ b/src/surf/host_interface.cpp @@ -92,7 +92,8 @@ void Host::init() Host::Host(simgrid::surf::Model *model, const char *name, xbt_dict_t props, xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu) : Resource(model, name) - , p_storage(storage), p_netElm(netElm), p_cpu(cpu), p_properties(props) + , PropertyHolder(props) + , p_storage(storage), p_netElm(netElm), p_cpu(cpu) { p_params.ramsize = 0; } @@ -100,14 +101,14 @@ Host::Host(simgrid::surf::Model *model, const char *name, xbt_dict_t props, Host::Host(simgrid::surf::Model *model, const char *name, xbt_dict_t props, lmm_constraint_t constraint, xbt_dynar_t storage, RoutingEdge *netElm, Cpu *cpu) : Resource(model, name, constraint) - , p_storage(storage), p_netElm(netElm), p_cpu(cpu), p_properties(props) +, PropertyHolder(props) + , p_storage(storage), p_netElm(netElm), p_cpu(cpu) { p_params.ramsize = 0; } Host::~Host(){ surf_callback_emit(hostDestructedCallbacks, this); - xbt_dict_free(&p_properties); } void Host::attach(simgrid::Host* host) @@ -126,13 +127,6 @@ void Host::setState(e_surf_resource_state_t state){ p_cpu->setState(state); } -xbt_dict_t Host::getProperties() -{ - if (p_properties==NULL) - p_properties = xbt_dict_new(); - return p_properties; -} - simgrid::surf::Storage *Host::findStorageOnMountList(const char* mount) { simgrid::surf::Storage *st = NULL; diff --git a/src/surf/host_interface.hpp b/src/surf/host_interface.hpp index 090c2ed524..2ae6d5deef 100644 --- a/src/surf/host_interface.hpp +++ b/src/surf/host_interface.hpp @@ -8,6 +8,7 @@ #include "storage_interface.hpp" #include "cpu_interface.hpp" #include "network_interface.hpp" +#include "src/surf/PropertyHolder.hpp" #include @@ -94,7 +95,7 @@ public: * @brief SURF Host interface class * @details An host represents a machine with a aggregation of a Cpu, a Link and a Storage */ -class Host : public simgrid::surf::Resource { +class Host : public simgrid::surf::Resource, public simgrid::surf::PropertyHolder { public: static simgrid::xbt::FacetLevel LEVEL; static void init(); @@ -132,13 +133,6 @@ public: void attach(simgrid::Host* host); void setState(e_surf_resource_state_t state); - /** - * @brief Get the properties of the current Host - * - * @return The properties of the current Host - */ - xbt_dict_t getProperties(); - /** * @brief Execute some quantity of computation * @@ -269,7 +263,6 @@ public: RoutingEdge *p_netElm; Cpu *p_cpu; simgrid::Host* p_host = nullptr; - xbt_dict_t p_properties = NULL; /** @brief Get the list of virtual machines on the current Host */ xbt_dynar_t getVms(); diff --git a/src/surf/network_interface.cpp b/src/surf/network_interface.cpp index a6b79372c6..5ae56794da 100644 --- a/src/surf/network_interface.cpp +++ b/src/surf/network_interface.cpp @@ -195,7 +195,7 @@ double NetworkModel::shareResourcesFull(double now) Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t props) : Resource(model, name), - p_properties(props) + PropertyHolder(props) { links->insert({name, this}); @@ -207,7 +207,7 @@ Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t prop tmgr_history_t history, tmgr_trace_t state_trace) : Resource(model, name, constraint), - p_properties(props) + PropertyHolder(props) { if (state_trace) p_stateEvent = tmgr_history_add_trace(history, state_trace, 0.0, 0, this); @@ -220,7 +220,6 @@ Link::Link(simgrid::surf::NetworkModel *model, const char *name, xbt_dict_t prop Link::~Link() { surf_callback_emit(networkLinkDestructedCallbacks, this); - xbt_dict_free(&p_properties); } bool Link::isUsed() @@ -249,13 +248,6 @@ void Link::setState(e_surf_resource_state_t state){ surf_callback_emit(networkLinkStateChangedCallbacks, this, old, state); } -xbt_dict_t Link::getProperties() -{ - if (p_properties==NULL) - p_properties = xbt_dict_new(); - return p_properties; -} - /********** * Action * **********/ diff --git a/src/surf/network_interface.hpp b/src/surf/network_interface.hpp index dcd3136bd3..415d244d49 100644 --- a/src/surf/network_interface.hpp +++ b/src/surf/network_interface.hpp @@ -15,6 +15,7 @@ #include "xbt/dict.h" #include "surf_interface.hpp" #include "surf_routing.hpp" +#include "src/surf/PropertyHolder.hpp" #include "simgrid/link.h" @@ -187,7 +188,7 @@ public: * @brief SURF network link interface class * @details A Link represents the link between two [hosts](\ref Host) */ -class Link : public simgrid::surf::Resource { +class Link : public simgrid::surf::Resource, public simgrid::surf::PropertyHolder { public: /** * @brief Link constructor @@ -252,11 +253,6 @@ public: private: void *userData = NULL; -public: - xbt_dict_t getProperties(); -protected: - xbt_dict_t p_properties = NULL; - /* List of all links */ private: static boost::unordered_map *links; diff --git a/src/surf/storage_interface.cpp b/src/surf/storage_interface.cpp index 8b64c56daa..2bdecefea7 100644 --- a/src/surf/storage_interface.cpp +++ b/src/surf/storage_interface.cpp @@ -59,11 +59,11 @@ Storage::Storage(Model *model, const char *name, xbt_dict_t props, const char* type_id, char *content_name, char *content_type, sg_size_t size) : Resource(model, name) + , PropertyHolder(props) , p_contentType(content_type) , m_size(size), m_usedSize(0) , p_typeId(xbt_strdup(type_id)) , p_writeActions(xbt_dynar_new(sizeof(Action*),NULL)) - , p_properties(props) { p_content = parseContent(content_name); setState(SURF_RESOURCE_ON); @@ -73,12 +73,12 @@ Storage::Storage(Model *model, const char *name, xbt_dict_t props, lmm_system_t maxminSystem, double bread, double bwrite, double bconnection, const char* type_id, char *content_name, char *content_type, sg_size_t size, char *attach) - : Resource(model, name, lmm_constraint_new(maxminSystem, this, bconnection)) + : Resource(model, name, lmm_constraint_new(maxminSystem, this, bconnection)) + , PropertyHolder(props) , p_contentType(content_type) , m_size(size), m_usedSize(0) , p_typeId(xbt_strdup(type_id)) , p_writeActions(xbt_dynar_new(sizeof(Action*),NULL)) - , p_properties(props) { p_content = parseContent(content_name); p_attach = xbt_strdup(attach); @@ -91,7 +91,6 @@ Storage::Storage(Model *model, const char *name, xbt_dict_t props, Storage::~Storage(){ surf_callback_emit(storageDestructedCallbacks, this); xbt_dict_free(&p_content); - xbt_dict_free(&p_properties); xbt_dynar_free(&p_writeActions); free(p_typeId); free(p_contentType); @@ -180,13 +179,6 @@ sg_size_t Storage::getUsedSize(){ return m_usedSize; } -xbt_dict_t Storage::getProperties() -{ - if (p_properties==NULL) - p_properties = xbt_dict_new(); - return p_properties; -} - /********** * Action * **********/ diff --git a/src/surf/storage_interface.hpp b/src/surf/storage_interface.hpp index 3be52e96e6..8c7be43d37 100644 --- a/src/surf/storage_interface.hpp +++ b/src/surf/storage_interface.hpp @@ -7,6 +7,7 @@ #include #include "surf_interface.hpp" +#include "src/surf/PropertyHolder.hpp" #ifndef STORAGE_INTERFACE_HPP_ #define STORAGE_INTERFACE_HPP_ @@ -101,7 +102,7 @@ public: * @brief SURF storage interface class * @details A Storage represent a storage unit (e.g.: hard drive, usb key) */ -class Storage : public simgrid::surf::Resource { +class Storage : public simgrid::surf::Resource, public simgrid::surf::PropertyHolder { public: /** * @brief Storage constructor @@ -241,12 +242,6 @@ public: lmm_constraint_t p_constraintWrite; /* Constraint for maximum write bandwidth*/ lmm_constraint_t p_constraintRead; /* Constraint for maximum write bandwidth*/ - -public: - xbt_dict_t getProperties(); -protected: - xbt_dict_t p_properties = NULL; - }; /********** diff --git a/tools/cmake/DefinePackages.cmake b/tools/cmake/DefinePackages.cmake index 0a8819c175..0bc0ae4670 100644 --- a/tools/cmake/DefinePackages.cmake +++ b/tools/cmake/DefinePackages.cmake @@ -84,6 +84,7 @@ set(EXTRA_DIST src/surf/surfxml_parse.c src/surf/trace_mgr_private.h src/surf/vm_hl13.hpp + src/surf/PropertyHolder.hpp src/surf/virtual_machine.hpp src/surf/host_clm03.hpp src/surf/host_interface.hpp @@ -326,6 +327,7 @@ set(SURF_SRC src/surf/network_ib.cpp src/surf/platf_generator.c src/surf/plugins/energy.cpp + src/surf/PropertyHolder.cpp src/surf/random_mgr.c src/surf/sg_platf.cpp src/surf/storage_interface.cpp -- 2.20.1