forked from OpenAtomFoundation/pika
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: support network IO traffic monitoring
Support network IO traffic monitoring. Including IO bytes and kps of Redis requests and master-slave replication. Fixes: OpenAtomFoundation#1732 Signed-off-by: yaoyinnan <[email protected]>
- Loading branch information
Showing
27 changed files
with
3,910 additions
and
2,192 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
// Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. An additional grant | ||
// of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
#ifndef PIKA_PIKA_INSTANT_H | ||
#define PIKA_PIKA_INSTANT_H | ||
|
||
#include <string> | ||
#include <unordered_map> | ||
|
||
#define STATS_METRIC_SAMPLES 16 /* Number of samples per metric. */ | ||
inline const std::string STATS_METRIC_NET_INPUT = "stats_metric_net_input"; | ||
inline const std::string STATS_METRIC_NET_OUTPUT = "stats_metric_net_output"; | ||
inline const std::string STATS_METRIC_NET_INPUT_REPLICATION = "stats_metric_net_input_replication"; | ||
inline const std::string STATS_METRIC_NET_OUTPUT_REPLICATION = "stats_metric_net_output_replication"; | ||
|
||
#define run_with_period(_ms_) if (((_ms_) <= 1000/server.hz) || !(server.cronloops%((_ms_)/(1000/server.hz)))) | ||
|
||
/* The following two are used to track instantaneous metrics, like | ||
* number of operations per second, network traffic. */ | ||
struct inst_metric{ | ||
uint64_t last_sample_base; /* The divisor of last sample window */ | ||
uint64_t last_sample_value; /* The dividend of last sample window */ | ||
uint64_t samples[STATS_METRIC_SAMPLES]; | ||
int idx; | ||
}; | ||
|
||
class Instant { | ||
public: | ||
Instant() = default; | ||
~Instant() = default; | ||
|
||
void trackInstantaneousMetric(std::string metric, uint64_t current_value, uint64_t current_base, uint64_t factor); | ||
uint64_t getInstantaneousMetric(std::string metric); | ||
|
||
private: | ||
std::unordered_map<std::string, inst_metric> inst_metrics_; | ||
}; | ||
|
||
#endif // PIKA_PIKA_INSTANT_H |
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 (c) 2023-present, Qihoo, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. An additional grant | ||
// of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
#ifndef PIKA_MONOTONIC_TIME_H | ||
#define PIKA_MONOTONIC_TIME_H | ||
|
||
#include <cstdint> | ||
|
||
/* A counter in micro-seconds. The 'monotime' type is provided for variables | ||
* holding a monotonic time. This will help distinguish & document that the | ||
* variable is associated with the monotonic clock and should not be confused | ||
* with other types of time.*/ | ||
typedef uint64_t monotime; | ||
|
||
// Get monotonic time in microseconds | ||
monotime getMonotonicUs(); | ||
|
||
#endif // PIKA_MONOTONIC_TIME_H | ||
|
||
|
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,36 @@ | ||
// Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. An additional grant | ||
// of patent rights can be found in the PATENTS file in the same directory. | ||
// | ||
#ifndef NET_INCLUDE_REDIS_STSTS_H_ | ||
#define NET_INCLUDE_REDIS_STSTS_H_ | ||
|
||
#include <atomic> | ||
|
||
namespace net { | ||
|
||
class NetworkStatistic { | ||
public: | ||
NetworkStatistic() = default; | ||
~NetworkStatistic() = default; | ||
|
||
uint64_t NetInputBytes(); | ||
uint64_t NetOutputBytes(); | ||
uint64_t NetReplInputBytes(); | ||
uint64_t NetReplOutputBytes(); | ||
void IncrInputBytes(uint64_t bytes); | ||
void IncrOutputBytes(uint64_t bytes); | ||
void IncrReplInputBytes(uint64_t bytes); | ||
void IncrReplOutputBytes(uint64_t bytes); | ||
|
||
private: | ||
std::atomic<uint64_t> stat_net_input_bytes = {0}; /* Bytes read from network. */ | ||
std::atomic<uint64_t> stat_net_output_bytes = {0}; /* Bytes written to network. */ | ||
std::atomic<uint64_t> stat_net_repl_input_bytes = {0}; /* Bytes read during replication, added to stat_net_input_bytes in 'info'. */ | ||
std::atomic<uint64_t> stat_net_repl_output_bytes = {0}; /* Bytes written during replication, added to stat_net_output_bytes in 'info'. */ | ||
}; | ||
|
||
} | ||
|
||
#endif // NET_INCLUDE_REDIS_STSTS_H_ |
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,30 @@ | ||
// Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. An additional grant | ||
// of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
#include <atomic> | ||
#include <string> | ||
#include "net/include/net_stats.h" | ||
|
||
std::unique_ptr<net::NetworkStatistic> g_network_statistic; | ||
|
||
namespace net { | ||
|
||
uint64_t NetworkStatistic::NetInputBytes() { return stat_net_input_bytes.load(std::memory_order_relaxed); } | ||
|
||
uint64_t NetworkStatistic::NetOutputBytes() { return stat_net_output_bytes.load(std::memory_order_relaxed); } | ||
|
||
uint64_t NetworkStatistic::NetReplInputBytes() { return stat_net_repl_input_bytes.load(std::memory_order_relaxed); } | ||
|
||
uint64_t NetworkStatistic::NetReplOutputBytes() { return stat_net_repl_output_bytes.load(std::memory_order_relaxed); } | ||
|
||
void NetworkStatistic::IncrInputBytes(uint64_t bytes) { stat_net_input_bytes.fetch_add(bytes, std::memory_order_relaxed); } | ||
|
||
void NetworkStatistic::IncrOutputBytes(uint64_t bytes) { stat_net_output_bytes.fetch_add(bytes, std::memory_order_relaxed); } | ||
|
||
void NetworkStatistic::IncrReplInputBytes(uint64_t bytes) { stat_net_repl_input_bytes.fetch_add(bytes, std::memory_order_relaxed); } | ||
|
||
void NetworkStatistic::IncrReplOutputBytes(uint64_t bytes) { stat_net_repl_output_bytes.fetch_add(bytes, std::memory_order_relaxed); } | ||
|
||
} |
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,39 @@ | ||
// Copyright (c) 2023-present, Qihoo, Inc. All rights reserved. | ||
// This source code is licensed under the BSD-style license found in the | ||
// LICENSE file in the root directory of this source tree. An additional grant | ||
// of patent rights can be found in the PATENTS file in the same directory. | ||
|
||
#include <string> | ||
#include "../include/pika_instant.h" | ||
|
||
/* Return the mean of all the samples. */ | ||
uint64_t Instant::getInstantaneousMetric(std::string metric) { | ||
int j; | ||
size_t sum = 0; | ||
|
||
for (j = 0; j < STATS_METRIC_SAMPLES; j++) | ||
sum += inst_metrics_[metric].samples[j]; | ||
return sum / STATS_METRIC_SAMPLES; | ||
} | ||
|
||
/* ======================= Cron: called every 100 ms ======================== */ | ||
|
||
/* Add a sample to the instantaneous metric. This function computes the quotient | ||
* of the increment of value and base, which is useful to record operation count | ||
* per second, or the average time consumption of an operation. | ||
* | ||
* current_value - The dividend | ||
* current_base - The divisor | ||
* */ | ||
void Instant::trackInstantaneousMetric(std::string metric, uint64_t current_value, uint64_t current_base, uint64_t factor) { | ||
if (inst_metrics_[metric].last_sample_base > 0) { | ||
uint64_t base = current_base - inst_metrics_[metric].last_sample_base; | ||
uint64_t value = current_value - inst_metrics_[metric].last_sample_value; | ||
uint64_t avg = base > 0 ? (value * factor / base) : 0; | ||
inst_metrics_[metric].samples[inst_metrics_[metric].idx] = avg; | ||
inst_metrics_[metric].idx++; | ||
inst_metrics_[metric].idx %= STATS_METRIC_SAMPLES; | ||
} | ||
inst_metrics_[metric].last_sample_base = current_base; | ||
inst_metrics_[metric].last_sample_value = current_value; | ||
} |
Oops, something went wrong.