Raptor
A fast and space-efficient pre-filter
All Classes Namespaces Files Functions Variables Macros Pages Concepts
memory_usage.hpp
Go to the documentation of this file.
1// SPDX-FileCopyrightText: 2006-2024 Knut Reinert & Freie Universität Berlin
2// SPDX-FileCopyrightText: 2016-2024 Knut Reinert & MPI für molekulare Genetik
3// SPDX-License-Identifier: BSD-3-Clause
4
10#pragma once
11
12#include <cassert>
13#include <cstdint>
14#include <string>
15
17
18#if __has_include(<sys/resource.h>)
19# include <sys/resource.h>
20#endif
21
22namespace raptor
23{
24
25#if __has_include(<sys/resource.h>)
26// Returns -1 if not available. Actually returns bytes instead of KiB on macOS.
27inline long peak_ram_in_KiB()
28{
29 rusage usage;
30# if __APPLE__
31 return getrusage(RUSAGE_SELF, &usage) == 0 ? (usage.ru_maxrss >> 10) : -1L;
32# else
33 return getrusage(RUSAGE_SELF, &usage) == 0 ? usage.ru_maxrss : -1L;
34# endif
35}
36#else
37inline long peak_ram_in_KiB()
38{
39 return -1L;
40}
41#endif
42
43[[nodiscard]] inline std::string formatted_peak_ram()
44{
45 long const peak_ram_KiB = peak_ram_in_KiB();
46 if (peak_ram_KiB == -1L)
47 return {": Not available"}; // GCOVR_EXCL_LINE
48
49 return formatted_bytes(static_cast<size_t>(peak_ram_KiB) << 10);
50}
51
52} // namespace raptor
Provides raptor::formatted_bytes.
Hide me