-
Notifications
You must be signed in to change notification settings - Fork 55
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Base][Core][Example][Lib][ML] Add Memory, MemoryPool, Cache, PageSto…
…re, Page to support swapping of ObjLists issue #256 Modify base/disk_store, base/serialization, core/config, core/objlist, core/objlist_data. Add default constructor to objects.
- Loading branch information
Showing
31 changed files
with
1,710 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
// Copyright 2016 Husky Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "base/memory.hpp" | ||
|
||
namespace husky { | ||
namespace base { | ||
|
||
struct sysinfo Memory::mem_info; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
// Copyright 2016 Husky Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include "sys/sysinfo.h" | ||
#include "sys/types.h" | ||
|
||
namespace husky { | ||
namespace base { | ||
class Memory { | ||
public: | ||
static int64_t total_phys_mem() { | ||
sysinfo(&mem_info); | ||
int64_t total_phys_mem = mem_info.totalram; | ||
total_phys_mem *= mem_info.mem_unit; | ||
return total_phys_mem; | ||
} | ||
|
||
static int64_t total_virtual_mem() { | ||
sysinfo(&mem_info); | ||
int64_t total_vir_mem = mem_info.totalram; | ||
total_vir_mem += mem_info.totalswap; | ||
total_vir_mem *= mem_info.mem_unit; | ||
return total_vir_mem; | ||
} | ||
|
||
private: | ||
static struct sysinfo mem_info; | ||
}; | ||
} // namespace base | ||
} // namespace husky |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
// Copyright 2016 Husky Team | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#pragma once | ||
|
||
#include <list> | ||
#include <unordered_map> | ||
#include <utility> | ||
|
||
#include "boost/optional.hpp" | ||
|
||
namespace husky { | ||
|
||
template <typename KeyT, typename ValT> | ||
class ICache { | ||
public: | ||
explicit ICache(size_t size) { size_ = size; } | ||
|
||
size_t get_max_size() { return size_; } | ||
|
||
protected: | ||
size_t size_; | ||
}; | ||
|
||
template <typename KeyT, typename ValT> | ||
class LRUCache : public ICache<KeyT, ValT> { | ||
public: | ||
using ListIterator = typename std::list<std::pair<KeyT, ValT>>::iterator; | ||
|
||
explicit LRUCache(size_t size) : ICache<KeyT, ValT>(size) {} | ||
|
||
~LRUCache() = default; | ||
|
||
template <typename KeyU, typename ValU> | ||
void put(KeyU&& key, ValU&& val) { | ||
auto it_cache = cache_map_.find(key); | ||
cache_list_.push_front(std::make_pair(key, val)); | ||
if (it_cache != cache_map_.end()) { | ||
cache_list_.erase(it_cache->second); | ||
cache_map_.erase(it_cache); | ||
} | ||
cache_map_[key] = cache_list_.begin(); | ||
|
||
if (cache_map_.size() > this->size_) { | ||
del(); | ||
} | ||
} | ||
|
||
boost::optional<std::pair<KeyT, ValT>> del() { | ||
if (cache_map_.size() == 0) { | ||
return boost::none; | ||
} | ||
auto last = cache_list_.end(); | ||
last--; | ||
KeyT key = last->first; | ||
ValT objlist = last->second; | ||
cache_map_.erase(key); | ||
cache_list_.pop_back(); | ||
return *last; | ||
} | ||
|
||
boost::optional<std::pair<KeyT, ValT>> poll() { | ||
if (cache_map_.size() == 0) { | ||
return boost::none; | ||
} | ||
return cache_list_.back(); | ||
} | ||
|
||
size_t get_size() { return cache_map_.size(); } | ||
|
||
template <typename KeyU> | ||
bool exists(KeyU&& key) { | ||
return cache_map_.find(std::forward<KeyU>(key)) != cache_map_.end(); | ||
} | ||
|
||
private: | ||
std::unordered_map<KeyT, ListIterator> cache_map_; | ||
std::list<std::pair<KeyT, ValT>> cache_list_; | ||
}; | ||
|
||
template <typename KeyT, typename ValT> | ||
class FIFOCache : public ICache<KeyT, ValT> { | ||
public: | ||
using ListIterator = typename std::list<std::pair<KeyT, ValT>>::iterator; | ||
|
||
explicit FIFOCache(size_t size) : ICache<KeyT, ValT>(size) {} | ||
|
||
~FIFOCache() = default; | ||
|
||
template <typename KeyU, typename ValU> | ||
void put(KeyU&& key, ValU&& val) { | ||
auto it_cache = cache_map_.find(key); | ||
if (it_cache != cache_map_.end() && it_cache->second->second != val) { | ||
cache_list_.erase(it_cache->second); | ||
cache_map_.erase(it_cache); | ||
} else if (it_cache != cache_map_.end() && it_cache->second->second == val) { | ||
return; | ||
} | ||
cache_list_.push_front(std::make_pair(key, val)); | ||
cache_map_[key] = cache_list_.begin(); | ||
|
||
if (cache_map_.size() > this->size_) { | ||
del(); | ||
} | ||
} | ||
|
||
boost::optional<std::pair<KeyT, ValT>> del() { | ||
if (cache_map_.size() == 0) { | ||
return boost::none; | ||
} | ||
auto last = cache_list_.end(); | ||
last--; | ||
KeyT key = last->first; | ||
ValT objlist = last->second; | ||
cache_map_.erase(key); | ||
cache_list_.pop_back(); | ||
return *last; | ||
} | ||
|
||
boost::optional<std::pair<KeyT, ValT>> poll() { | ||
if (cache_map_.size() == 0) { | ||
return boost::none; | ||
} | ||
return cache_list_.back(); | ||
} | ||
|
||
template <typename KeyU> | ||
bool exists(KeyU&& key) { | ||
return cache_map_.find(key) != cache_map_.end(); | ||
} | ||
|
||
size_t get_size() { return cache_map_.size(); } | ||
|
||
private: | ||
std::unordered_map<KeyT, ListIterator> cache_map_; | ||
std::list<std::pair<KeyT, ValT>> cache_list_; | ||
}; | ||
} // namespace husky |
Oops, something went wrong.