From a597d37bc1bf5da17b73179c50df745ea8e40d51 Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Wed, 15 Jul 2015 18:08:54 +0300 Subject: [PATCH 1/5] memory: add statistics for reclaim operations and free memory --- core/memory.cc | 5 ++++- core/memory.hh | 12 ++++++++++-- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/core/memory.cc b/core/memory.cc index d60ad295fd..5f57cebf92 100644 --- a/core/memory.cc +++ b/core/memory.cc @@ -88,6 +88,7 @@ class page_list; static thread_local uint64_t g_allocs; static thread_local uint64_t g_frees; static thread_local uint64_t g_cross_cpu_frees; +static thread_local uint64_t g_reclaims; using std::experimental::optional; @@ -653,6 +654,7 @@ void cpu_pages::resize(size_t new_size, allocate_system_memory_fn alloc_memory) void cpu_pages::reclaim() { current_min_free_pages = 0; reclaim_hook([this] { + ++g_reclaims; for (auto&& r : reclaimers) { r->do_reclaim(); } @@ -871,7 +873,8 @@ void configure(std::vector m, } statistics stats() { - return statistics{g_allocs, g_frees, g_cross_cpu_frees}; + return statistics{g_allocs, g_frees, g_cross_cpu_frees, + cpu_mem.nr_free_pages * page_size, g_reclaims}; } bool drain_cross_cpu_freelist() { diff --git a/core/memory.hh b/core/memory.hh index 22bc1e6e31..7e92acfef5 100644 --- a/core/memory.hh +++ b/core/memory.hh @@ -114,9 +114,13 @@ class statistics { uint64_t _mallocs; uint64_t _frees; uint64_t _cross_cpu_frees; + size_t _free_memory; + uint64_t _reclaims; private: - statistics(uint64_t mallocs, uint64_t frees, uint64_t cross_cpu_frees) - : _mallocs(mallocs), _frees(frees), _cross_cpu_frees(cross_cpu_frees) {} + statistics(uint64_t mallocs, uint64_t frees, uint64_t cross_cpu_frees, + uint64_t free_memory, uint64_t reclaims) + : _mallocs(mallocs), _frees(frees), _cross_cpu_frees(cross_cpu_frees) + , _free_memory(free_memory), _reclaims(reclaims) {} public: /// Total number of memory allocations calls since the system was started. uint64_t mallocs() const { return _mallocs; } @@ -127,6 +131,10 @@ public: uint64_t cross_cpu_frees() const { return _cross_cpu_frees; } /// Total number of objects which were allocated but not freed. size_t live_objects() const { return mallocs() - frees(); } + /// Total free memory (in bytes) + size_t free_memory() const { return _free_memory; } + /// Number of reclaims performed due to low memory + uint64_t reclaims() const { return _reclaims; } friend statistics stats(); }; From 248aa4f55ea598da437b206a0ac91d4585f1d40f Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Wed, 15 Jul 2015 18:09:13 +0300 Subject: [PATCH 2/5] reactor: wire up free memory and reclaim counters to collected --- core/reactor.cc | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/core/reactor.cc b/core/reactor.cc index cdfa7e1dbf..9c25a6e6c0 100644 --- a/core/reactor.cc +++ b/core/reactor.cc @@ -999,6 +999,20 @@ reactor::register_collectd_metrics() { scollectd::make_typed(scollectd::data_type::GAUGE, [] { return memory::stats().live_objects(); }) ), + scollectd::add_polled_metric( + scollectd::type_instance_id("memory", + scollectd::per_cpu_plugin_instance, + "memory", "free_memory"), + scollectd::make_typed(scollectd::data_type::GAUGE, + [] { return memory::stats().free_memory(); }) + ), + scollectd::add_polled_metric( + scollectd::type_instance_id("memory", + scollectd::per_cpu_plugin_instance, + "total_operations", "reclaims"), + scollectd::make_typed(scollectd::data_type::DERIVE, + [] { return memory::stats().reclaims(); }) + ), } }; } From f256243ebd78b5d0aac2394df6c513d7f7e111ae Mon Sep 17 00:00:00 2001 From: Avi Kivity Date: Wed, 15 Jul 2015 18:17:49 +0300 Subject: [PATCH 3/5] memory: fix debug build break (statistics) --- core/memory.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/core/memory.cc b/core/memory.cc index 5f57cebf92..5a4532f957 100644 --- a/core/memory.cc +++ b/core/memory.cc @@ -1165,7 +1165,7 @@ void configure(std::vector m, std::experimental::optional Date: Thu, 16 Jul 2015 10:40:10 +0800 Subject: [PATCH 4/5] rpc: Introduce rpc/rpc_types.hh Include rpc_types.hh instead of rpc.hh when no_wait_type and friends are needed. --- rpc/rpc.hh | 20 +------------------- rpc/rpc_types.hh | 49 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 19 deletions(-) create mode 100644 rpc/rpc_types.hh diff --git a/rpc/rpc.hh b/rpc/rpc.hh index f9cb54ab19..12bf0f39b6 100644 --- a/rpc/rpc.hh +++ b/rpc/rpc.hh @@ -28,6 +28,7 @@ #include "core/reactor.hh" #include "core/iostream.hh" #include "core/shared_ptr.hh" +#include "rpc/rpc_types.hh" namespace rpc { @@ -45,10 +46,6 @@ struct SerializerConcept { future<> operator()(input_stream& in, sstring& v); }; -struct client_info { - socket_address addr; -}; - // MsgType is a type that holds type of a message. The type should be hashable // and serializable. It is preferable to use enum for message types, but // do not forget to provide hash function for it @@ -199,21 +196,6 @@ private: _handlers.emplace(t, std::move(handler)); } }; - -class error : public std::runtime_error { -public: - error(const std::string& msg) : std::runtime_error(msg) {} -}; - -class closed_error : public error { -public: - closed_error() : error("connection is closed") {} -}; - -struct no_wait_type {}; - -// return this from a callback if client does not want to waiting for a reply -extern no_wait_type no_wait; } #include "rpc_impl.hh" diff --git a/rpc/rpc_types.hh b/rpc/rpc_types.hh new file mode 100644 index 0000000000..c9c555b377 --- /dev/null +++ b/rpc/rpc_types.hh @@ -0,0 +1,49 @@ +/* + * This file is open source software, licensed to you under the terms + * of the Apache License, Version 2.0 (the "License"). See the NOTICE file + * distributed with this work for additional information regarding copyright + * ownership. 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. + */ +/* + * Copyright (C) 2015 Cloudius Systems, Ltd. + */ + +#pragma once + +#include "net/api.hh" +#include +#include + +namespace rpc { + +struct client_info { + socket_address addr; +}; + +class error : public std::runtime_error { +public: + error(const std::string& msg) : std::runtime_error(msg) {} +}; + +class closed_error : public error { +public: + closed_error() : error("connection is closed") {} +}; + +struct no_wait_type {}; + +// return this from a callback if client does not want to waiting for a reply +extern no_wait_type no_wait; + +} // namespace rpc From 624388ae4b9394b2291f2c9db50c6b1667216af5 Mon Sep 17 00:00:00 2001 From: Asias He Date: Thu, 16 Jul 2015 10:59:04 +0800 Subject: [PATCH 5/5] rpc: Move stats to rpc_types.hh It will be needed by user does not want to include rpc.hh --- rpc/rpc.hh | 9 --------- rpc/rpc_types.hh | 10 ++++++++++ 2 files changed, 10 insertions(+), 9 deletions(-) diff --git a/rpc/rpc.hh b/rpc/rpc.hh index 12bf0f39b6..3f784d4d72 100644 --- a/rpc/rpc.hh +++ b/rpc/rpc.hh @@ -121,15 +121,6 @@ public: virtual ~reply_handler_base() {}; }; public: - struct stats { - using counter_type = uint64_t; - counter_type replied = 0; - counter_type pending = 0; - counter_type exception_received = 0; - counter_type sent_messages = 0; - counter_type wait_reply = 0; - }; - template struct reply_handler final : reply_handler_base { Func func; diff --git a/rpc/rpc_types.hh b/rpc/rpc_types.hh index c9c555b377..41c38b9401 100644 --- a/rpc/rpc_types.hh +++ b/rpc/rpc_types.hh @@ -27,6 +27,16 @@ namespace rpc { +struct stats { + using counter_type = uint64_t; + counter_type replied = 0; + counter_type pending = 0; + counter_type exception_received = 0; + counter_type sent_messages = 0; + counter_type wait_reply = 0; +}; + + struct client_info { socket_address addr; };