change parameter type from KeyRef to Key to avoid extra copy in sampler

This commit is contained in:
Xiaoxi Wang 2023-03-03 15:37:45 -08:00
parent 3d9f37d1d1
commit 303791a060
2 changed files with 13 additions and 13 deletions

View File

@ -85,7 +85,7 @@ StorageMetrics StorageServerMetrics::getMetrics(KeyRangeRef const& keys) const {
// Called when metrics should change (IO for a given key)
// Notifies waiting WaitMetricsRequests through waitMetricsMap, and updates metricsAverageQueue and metricsSampleMap
void StorageServerMetrics::notify(KeyRef key, StorageMetrics& metrics) {
void StorageServerMetrics::notify(const Key& key, StorageMetrics& metrics) {
ASSERT(metrics.bytes == 0); // ShardNotifyMetrics
if (g_network->isSimulated()) {
CODE_PROBE(metrics.bytesWrittenPerKSecond != 0, "ShardNotifyMetrics bytes");
@ -126,7 +126,7 @@ void StorageServerMetrics::notify(KeyRef key, StorageMetrics& metrics) {
// Due to the fact that read sampling will be called on all reads, use this specialized function to avoid overhead
// around branch misses and unnecessary stack allocation which eventually addes up under heavy load.
void StorageServerMetrics::notifyBytesReadPerKSecond(KeyRef key, int64_t in) {
void StorageServerMetrics::notifyBytesReadPerKSecond(const Key& key, int64_t in) {
double expire = now() + SERVER_KNOBS->STORAGE_METRICS_AVERAGE_INTERVAL;
int64_t bytesReadPerKSecond =
bytesReadSample.addAndExpire(key, in, expire) * SERVER_KNOBS->STORAGE_METRICS_AVERAGE_INTERVAL_PER_KSECONDS;
@ -161,7 +161,7 @@ void StorageServerMetrics::notifyBytes(
}
// Called by StorageServerDisk when the size of a key in byteSample changes, to notify WaitMetricsRequest
void StorageServerMetrics::notifyBytes(KeyRef key, int64_t bytes) {
void StorageServerMetrics::notifyBytes(const KeyRef& key, int64_t bytes) {
if (key >= allKeys.end) // Do not notify on changes to internal storage server state
return;
@ -548,7 +548,7 @@ void StorageServerMetrics::add(KeyRangeMap<int>& map, KeyRangeRef const& keys, i
}
// Returns the sampled metric value (possibly 0, possibly increased by the sampling factor)
int64_t TransientStorageMetricSample::addAndExpire(KeyRef key, int64_t metric, double expiration) {
int64_t TransientStorageMetricSample::addAndExpire(const Key& key, int64_t metric, double expiration) {
int64_t x = add(key, metric);
if (x)
queue.emplace_back(expiration, std::make_pair(*sample.find(key), -x));
@ -570,7 +570,7 @@ void TransientStorageMetricSample::erase(KeyRangeRef keys) {
sample.erase(keys.begin, keys.end);
}
bool TransientStorageMetricSample::roll(KeyRef key, int64_t metric) const {
bool TransientStorageMetricSample::roll(int64_t metric) const {
return deterministicRandom()->random01() < (double)metric / metricUnitsPerSample; //< SOMEDAY: Better randomInt64?
}
@ -610,13 +610,13 @@ void TransientStorageMetricSample::poll() {
}
}
int64_t TransientStorageMetricSample::add(KeyRef key, int64_t metric) {
int64_t TransientStorageMetricSample::add(const Key& key, int64_t metric) {
if (!metric)
return 0;
int64_t mag = metric < 0 ? -metric : metric;
if (mag < metricUnitsPerSample) {
if (!roll(key, mag))
if (!roll(mag))
return 0;
metric = metric < 0 ? -metricUnitsPerSample : metricUnitsPerSample;
}

View File

@ -63,7 +63,7 @@ struct TransientStorageMetricSample : StorageMetricSample {
explicit TransientStorageMetricSample(int64_t metricUnitsPerSample) : StorageMetricSample(metricUnitsPerSample) {}
int64_t addAndExpire(KeyRef key, int64_t metric, double expiration);
int64_t addAndExpire(const Key& key, int64_t metric, double expiration);
int64_t erase(KeyRef key);
void erase(KeyRangeRef keys);
@ -73,8 +73,8 @@ struct TransientStorageMetricSample : StorageMetricSample {
void poll();
private:
bool roll(KeyRef key, int64_t metric) const;
int64_t add(KeyRef key, int64_t metric);
bool roll(int64_t metric) const;
int64_t add(const Key& key, int64_t metric);
};
struct StorageServerMetrics {
@ -94,14 +94,14 @@ struct StorageServerMetrics {
StorageMetrics getMetrics(KeyRangeRef const& keys) const;
void notify(KeyRef key, StorageMetrics& metrics);
void notify(const Key& key, StorageMetrics& metrics);
void notifyBytesReadPerKSecond(KeyRef key, int64_t in);
void notifyBytesReadPerKSecond(const Key& key, int64_t in);
void notifyBytes(RangeMap<Key, std::vector<PromiseStream<StorageMetrics>>, KeyRangeRef>::iterator shard,
int64_t bytes);
void notifyBytes(KeyRef key, int64_t bytes);
void notifyBytes(const KeyRef& key, int64_t bytes);
void notifyNotReadable(KeyRangeRef keys);