1 /*
2    Copyright 2013-2014 EditShare, 2013-2015 Skytechnology sp. z o.o.
3 
4    This file is part of LizardFS.
5 
6    LizardFS is free software: you can redistribute it and/or modify
7    it under the terms of the GNU General Public License as published by
8    the Free Software Foundation, version 3.
9 
10    LizardFS is distributed in the hope that it will be useful,
11    but WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13    GNU General Public License for more details.
14 
15    You should have received a copy of the GNU General Public License
16    along with LizardFS. If not, see <http://www.gnu.org/licenses/>.
17  */
18 
19 #include "common/platform.h"
20 #include "mount/tweaks.h"
21 
22 #include <list>
23 #include <sstream>
24 
25 class Variable {
26 public:
~Variable()27 	virtual ~Variable() {}
28 	virtual void setValue(const std::string& value) = 0;
29 	virtual std::string getValue() const = 0;
30 };
31 
32 template <typename T>
33 class VariableImpl : public Variable {
34 public:
VariableImpl(std::atomic<T> & v)35 	VariableImpl(std::atomic<T>& v) : value_(&v) {}
36 
setValue(const std::string & value)37 	void setValue(const std::string& value) override {
38 		std::stringstream ss(value);
39 		T t;
40 		ss >> std::boolalpha >> t;
41 		if (!ss.fail()) {
42 			value_->store(t);
43 		}
44 	}
45 
getValue() const46 	std::string getValue() const override {
47 		std::stringstream ss;
48 		ss << std::boolalpha << value_->load();
49 		return ss.str();
50 	}
51 
52 private:
53 	std::atomic<T>* value_;
54 };
55 
56 template <typename T>
makeVariable(std::atomic<T> & v)57 std::unique_ptr<Variable> makeVariable(std::atomic<T>& v) {
58 	return std::unique_ptr<Variable>(new VariableImpl<T>(v));
59 }
60 
61 class Tweaks::Impl {
62 public:
63 	std::list<std::pair<std::string, std::unique_ptr<Variable>>> variables;
64 };
65 
Tweaks()66 Tweaks::Tweaks() : impl_(new Impl) {}
67 
~Tweaks()68 Tweaks::~Tweaks() {}
69 
registerVariable(const std::string & name,std::atomic<bool> & variable)70 void Tweaks::registerVariable(const std::string& name, std::atomic<bool>& variable) {
71 	impl_->variables.push_back({name, makeVariable(variable)});
72 }
73 
registerVariable(const std::string & name,std::atomic<uint32_t> & variable)74 void Tweaks::registerVariable(const std::string& name, std::atomic<uint32_t>& variable) {
75 	impl_->variables.push_back({name, makeVariable(variable)});
76 }
77 
registerVariable(const std::string & name,std::atomic<uint64_t> & variable)78 void Tweaks::registerVariable(const std::string& name, std::atomic<uint64_t>& variable) {
79 	impl_->variables.push_back({name, makeVariable(variable)});
80 }
81 
setValue(const std::string & name,const std::string & value)82 void Tweaks::setValue(const std::string& name, const std::string& value) {
83 	for (auto& nameAndVariable : impl_->variables) {
84 		if (nameAndVariable.first == name) {
85 			nameAndVariable.second->setValue(value);
86 		}
87 	}
88 }
89 
getAllValues() const90 std::string Tweaks::getAllValues() const {
91 	std::stringstream ss;
92 	for (const auto& nameAndVariable : impl_->variables) {
93 		ss << nameAndVariable.first << "\t" << nameAndVariable.second->getValue() << "\n";
94 	}
95 	return ss.str();
96 }
97 
98 Tweaks gTweaks;
99