blob: def03d63dae9059f53a304b138a4fc3bb3ea4482 [file] [log] [blame]
// Copyright 2019 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#ifndef SRC_DEVELOPER_MEMORY_METRICS_WATCHER_H_
#define SRC_DEVELOPER_MEMORY_METRICS_WATCHER_H_
#include <lib/async/dispatcher.h>
#include <lib/fit/function.h>
#include "src/developer/memory/metrics/capture.h"
namespace memory {
// Watches memory usage and reports back when memory reaches a new high.
class Watcher {
public:
// Constructs a new Watcher which will check memory usage at the rate
// specified by |poll_frequency|, using the |async_dispatcher|.
// Each time usage increasess by at least |high_water_threshold| the
// |high_water_cb| will be called.
// |capture_cb| is used to access memory usage.
Watcher(zx::duration poll_frequency, uint64_t high_water_threshold,
async_dispatcher_t* dispatcher,
fit::function<zx_status_t(Capture&, CaptureLevel)> capture_cb,
fit::function<void(const Capture&)> high_water_cb);
~Watcher() = default;
void Run();
private:
void CaptureMemory();
void CaptureMemoryRepeatedly();
uint64_t least_free_bytes_;
zx::duration poll_frequency_;
uint64_t high_water_threshold_;
async_dispatcher_t* dispatcher_;
fit::function<zx_status_t(Capture&, CaptureLevel level)> capture_cb_;
fit::function<void(const Capture&)> high_water_cb_;
};
} // namespace memory
#endif // SRC_DEVELOPER_MEMORY_METRICS_WATCHER_H_