1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 // Linux kernels since 5.17 have a feature for assigning names to
8 // ranges of anonymous memory (i.e., memory that doesn't have a "name"
9 // in the form of an underlying mapped file). These names are
10 // reported in /proc/<pid>/smaps alongside system-level memory usage
11 // information such as Proportional Set Size (memory usage adjusted
12 // for sharing between processes), which allows reporting this
13 // information at a finer granularity than would otherwise be possible
14 // (e.g., separating malloc() heap from JS heap).
16 // Existing memory can be tagged with MozTagAnonymousMemory(); it will
17 // tag the range of complete pages containing the given interval, so
18 // the results may be inexact if the range isn't page-aligned.
19 // MozTaggedAnonymousMmap() can be used like mmap() with an extra
20 // parameter, and will tag the returned memory if the mapping was
21 // successful (and if it was in fact anonymous).
23 // NOTE: The pointer given as the "tag" argument MUST remain valid as
24 // long as the mapping exists. The referenced string is read when
25 // /proc/<pid>/smaps or /proc/<pid>/maps is read, not when the tag is
26 // established, so freeing it or changing its contents will have
27 // unexpected results. Using a static string is probably best.
29 // Also note that this header can be used by both C and C++ code.
31 #ifndef mozilla_TaggedAnonymousMemory_h
32 #define mozilla_TaggedAnonymousMemory_h
39 # include <sys/types.h>
40 # include <sys/mman.h>
43 # include "mozilla/Types.h"
51 MFBT_API
void MozTagAnonymousMemory(const void* aPtr
, size_t aLength
,
54 MFBT_API
void* MozTaggedAnonymousMmap(void* aAddr
, size_t aLength
, int aProt
,
55 int aFlags
, int aFd
, off_t aOffset
,
64 static inline void MozTagAnonymousMemory(const void* aPtr
, size_t aLength
,
67 static inline void* MozTaggedAnonymousMmap(void* aAddr
, size_t aLength
,
68 int aProt
, int aFlags
, int aFd
,
69 off_t aOffset
, const char* aTag
) {
71 MOZ_CRASH("We don't use this memory for WASI right now.");
74 return mmap(aAddr
, aLength
, aProt
, aFlags
, aFd
, aOffset
);
82 #endif // mozilla_TaggedAnonymousMemory_h