Skip to content

Commit d3a90b9

Browse files
authored
Merge branch 'main' into snmalloc-rs-integration
2 parents 84a0548 + aee25d3 commit d3a90b9

File tree

15 files changed

+80
-47
lines changed

15 files changed

+80
-47
lines changed

src/snmalloc/aal/aal.h

Lines changed: 5 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,10 @@
99
#include "../ds_core/ds_core.h"
1010
#include "aal_concept.h"
1111
#include "aal_consts.h"
12-
13-
#if __has_include(<time.h>)
14-
# include <time.h>
15-
# ifdef CLOCK_MONOTONIC
16-
# define SNMALLOC_TICK_USE_CLOCK_GETTIME
17-
# endif
18-
#endif
1912
#include "snmalloc/stl/utility.h"
2013

2114
#include <stdint.h>
2215

23-
#ifndef SNMALLOC_TICK_USE_CLOCK_GETTIME
24-
# include <chrono>
25-
#endif
26-
2716
#if ( \
2817
defined(__i386__) || defined(_M_IX86) || defined(_X86_) || \
2918
defined(__amd64__) || defined(__x86_64__) || defined(_M_X64) || \
@@ -169,37 +158,17 @@ namespace snmalloc
169158
/**
170159
* Return an architecture-specific cycle counter.
171160
*
172-
* If the compiler provides a portable prefetch builtin, use it directly,
173-
* otherwise delegate to the architecture-specific layer. This allows new
174-
* architectures to avoid needing to implement a custom `tick` method
175-
* if they are used only with a compiler that provides the builtin.
161+
* If the architecture reports that CPU cycle counters are unavailable,
162+
* use any architecture-specific implementation that exists, otherwise
163+
* fall back to zero. When counters are available, prefer a compiler
164+
* builtin and then the architecture-specific implementation.
176165
*/
177166
static inline uint64_t tick() noexcept
178167
{
179168
if constexpr (
180169
(Arch::aal_features & NoCpuCycleCounters) == NoCpuCycleCounters)
181170
{
182-
#ifdef SNMALLOC_TICK_USE_CLOCK_GETTIME
183-
// the buf is populated by clock_gettime
184-
SNMALLOC_UNINITIALISED timespec buf;
185-
// we can skip the error checking here:
186-
// * EFAULT: for out-of-bound pointers (buf is always valid stack
187-
// memory)
188-
// * EINVAL: for invalid clock_id (we only use CLOCK_MONOTONIC enforced
189-
// by POSIX.1)
190-
// Notice that clock_gettime is a usually a vDSO call, so the overhead
191-
// is minimal.
192-
::clock_gettime(CLOCK_MONOTONIC, &buf);
193-
return static_cast<uint64_t>(buf.tv_sec) * 1000'000'000 +
194-
static_cast<uint64_t>(buf.tv_nsec);
195-
# undef SNMALLOC_TICK_USE_CLOCK_GETTIME
196-
#else
197-
auto tick = std::chrono::high_resolution_clock::now();
198-
return static_cast<uint64_t>(
199-
std::chrono::duration_cast<std::chrono::nanoseconds>(
200-
tick.time_since_epoch())
201-
.count());
202-
#endif
171+
return 0;
203172
}
204173
else
205174
{

src/snmalloc/backend_helpers/backend_helpers.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "../mem/mem.h"
24
#include "authmap.h"
35
#include "buddy.h"

src/snmalloc/ds_aal/ds_aal.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@
77
#include "../aal/aal.h"
88
#include "flaglock.h"
99
#include "prevent_fork.h"
10+
#include "seqset.h"
1011
#include "singleton.h"

src/snmalloc/ds_core/cheri.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "mitigations.h"
24

35
namespace snmalloc

src/snmalloc/ds_core/ds_core.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@
1515
#include "mitigations.h"
1616
#include "ptrwrap.h"
1717
#include "redblacktree.h"
18-
#include "seqset.h"
1918
#include "tid.h"

src/snmalloc/global/global.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "bounds_checks.h"
24
#include "globalalloc.h"
35
#include "libc.h"

src/snmalloc/mem/mem.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
#pragma once
2+
13
#include "backend_concept.h"
24
#include "backend_wrappers.h"
35
#include "check_init.h"

src/snmalloc/pal/pal_posix.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include <strings.h>
1818
#include <sys/mman.h>
1919
#include <sys/uio.h>
20+
#include <time.h>
2021
#include <unistd.h>
2122

2223
#if __has_include(<sys/random.h>)
@@ -404,6 +405,28 @@ namespace snmalloc
404405
(static_cast<uint64_t>(ts.tv_nsec) / 1000000);
405406
}
406407

408+
static uint64_t tick()
409+
{
410+
if constexpr (
411+
(Aal::aal_features & NoCpuCycleCounters) != NoCpuCycleCounters)
412+
{
413+
return Aal::tick();
414+
}
415+
else
416+
{
417+
auto hold = KeepErrno();
418+
419+
struct timespec ts;
420+
if (clock_gettime(CLOCK_MONOTONIC, &ts) == -1)
421+
{
422+
error("Failed to get monotonic time");
423+
}
424+
425+
return (static_cast<uint64_t>(ts.tv_sec) * 1'000'000'000) +
426+
static_cast<uint64_t>(ts.tv_nsec);
427+
}
428+
}
429+
407430
static uint64_t dev_urandom()
408431
{
409432
union

src/snmalloc/pal/pal_windows.h

Lines changed: 29 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
#ifdef _WIN32
88
# ifndef _MSC_VER
99
# include <errno.h>
10-
# include <stdio.h>
1110
# endif
11+
# include <cstdio>
1212
# ifndef WIN32_LEAN_AND_MEAN
1313
# define WIN32_LEAN_AND_MEAN
1414
# endif
@@ -309,12 +309,11 @@ namespace snmalloc
309309
return result;
310310
}
311311

312-
static uint64_t internal_time_in_ms()
312+
static uint64_t performance_counter_frequency()
313313
{
314-
// Performance counter is a high-precision monotonic clock.
315314
static stl::Atomic<uint64_t> freq_cache = 0;
316-
constexpr uint64_t ms_per_second = 1000;
317315
SNMALLOC_UNINITIALISED LARGE_INTEGER buf;
316+
318317
auto freq = freq_cache.load(stl::memory_order_relaxed);
319318
if (SNMALLOC_UNLIKELY(freq == 0))
320319
{
@@ -324,10 +323,36 @@ namespace snmalloc
324323
freq = static_cast<uint64_t>(buf.QuadPart);
325324
freq_cache.store(freq, stl::memory_order_relaxed);
326325
}
326+
327+
return freq;
328+
}
329+
330+
static uint64_t internal_time_in_ms()
331+
{
332+
constexpr uint64_t ms_per_second = 1000;
333+
SNMALLOC_UNINITIALISED LARGE_INTEGER buf;
334+
auto freq = performance_counter_frequency();
327335
::QueryPerformanceCounter(&buf);
328336
return (static_cast<uint64_t>(buf.QuadPart) * ms_per_second) / freq;
329337
}
330338

339+
static uint64_t tick()
340+
{
341+
if constexpr (
342+
(Aal::aal_features & NoCpuCycleCounters) != NoCpuCycleCounters)
343+
{
344+
return Aal::tick();
345+
}
346+
else
347+
{
348+
constexpr uint64_t ns_per_second = 1'000'000'000;
349+
SNMALLOC_UNINITIALISED LARGE_INTEGER buf;
350+
auto freq = performance_counter_frequency();
351+
::QueryPerformanceCounter(&buf);
352+
return (static_cast<uint64_t>(buf.QuadPart) * ns_per_second) / freq;
353+
}
354+
}
355+
331356
# ifdef PLATFORM_HAS_WAITONADDRESS
332357
using WaitingWord = char;
333358

0 commit comments

Comments
 (0)