• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

realm / realm-core / github_pull_request_312964

19 Feb 2025 07:31PM UTC coverage: 90.814% (-0.3%) from 91.119%
github_pull_request_312964

Pull #8071

Evergreen

web-flow
Bump serialize-javascript and mocha

Bumps [serialize-javascript](https://github.com/yahoo/serialize-javascript) to 6.0.2 and updates ancestor dependency [mocha](https://github.com/mochajs/mocha). These dependencies need to be updated together.


Updates `serialize-javascript` from 6.0.0 to 6.0.2
- [Release notes](https://github.com/yahoo/serialize-javascript/releases)
- [Commits](https://github.com/yahoo/serialize-javascript/compare/v6.0.0...v6.0.2)

Updates `mocha` from 10.2.0 to 10.8.2
- [Release notes](https://github.com/mochajs/mocha/releases)
- [Changelog](https://github.com/mochajs/mocha/blob/main/CHANGELOG.md)
- [Commits](https://github.com/mochajs/mocha/compare/v10.2.0...v10.8.2)

---
updated-dependencies:
- dependency-name: serialize-javascript
  dependency-type: indirect
- dependency-name: mocha
  dependency-type: direct:development
...

Signed-off-by: dependabot[bot] <support@github.com>
Pull Request #8071: Bump serialize-javascript and mocha

96552 of 179126 branches covered (53.9%)

212672 of 234185 relevant lines covered (90.81%)

3115802.0 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

88.57
/src/realm/utilities.cpp
1
/*************************************************************************
2
 *
3
 * Copyright 2016 Realm Inc.
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 * http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 **************************************************************************/
18

19
#include <cstdlib> // size_t
20
#include <string>
21
#include <cstdint>
22
#include <atomic>
23
#include <fstream>
24

25
#ifdef _WIN32
26
#include "windows.h"
27
#include "psapi.h"
28
#include <chrono>
29
#include <thread>
30
#else
31
#include <unistd.h>
32
#endif
33

34
#include <realm/utilities.hpp>
35
#include <realm/unicode.hpp>
36
#include <realm/util/thread.hpp>
37

38
#ifdef REALM_COMPILER_SSE
39
#ifdef _MSC_VER
40
#include <intrin.h>
41
#endif
42
#endif
43

44
namespace {
45

46
#ifdef REALM_COMPILER_SSE
47
#if !defined __clang__ && ((defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 160040219) || defined __GNUC__)
48
#if defined REALM_COMPILER_AVX && defined __GNUC__
49
#define _XCR_XFEATURE_ENABLED_MASK 0
50

51
inline unsigned long long _xgetbv(unsigned index)
52
{
53
#if REALM_HAVE_AT_LEAST_GCC(4, 4)
54
    unsigned int eax, edx;
55
    __asm__ __volatile__("xgetbv" : "=a"(eax), "=d"(edx) : "c"(index));
56
    return (static_cast<unsigned long long>(edx) << 32) | eax;
57
#else
58
    static_cast<void>(index);
59
    return 0;
60
#endif
61
}
62

63
#endif
64
#endif
65
#endif
66

67
} // anonymous namespace
68

69

70
namespace realm {
71

72
signed char sse_support = -1;
73
signed char avx_support = -1;
74

75
void cpuid_init()
76
{
12✔
77
#ifdef REALM_COMPILER_SSE
12✔
78
    int cret;
12✔
79
#ifdef _MSC_VER
80
    int CPUInfo[4];
81
    __cpuid(CPUInfo, 1);
82
    cret = CPUInfo[2];
83
#else
84
    int a = 1;
12✔
85
    __asm("mov %1, %%eax; " // a into eax
12✔
86
          "cpuid;"
12✔
87
          "mov %%ecx, %0;"                 // ecx into b
12✔
88
          : "=r"(cret)                     // output
12✔
89
          : "r"(a)                         // input
12✔
90
          : "%eax", "%ebx", "%ecx", "%edx" // clobbered register
12✔
91
    );
12✔
92
#endif
12✔
93

94
    // Byte is atomic. Race can/will occur but that's fine
95
    if (cret & 0x100000) { // test for 4.2
12✔
96
        sse_support = 1;
12✔
97
    }
12✔
98
    else if (cret & 0x1) { // Test for 3
×
99
        sse_support = 0;
×
100
    }
×
101
    else {
×
102
        sse_support = -2;
×
103
    }
×
104

105
    bool avxSupported = false;
12✔
106

107
// seems like in jenkins builds, __GNUC__ is defined for clang?! todo fixme
108
#if !defined __clang__ && ((defined(_MSC_FULL_VER) && _MSC_FULL_VER >= 160040219) || defined __GNUC__)
109
    bool osUsesXSAVE_XRSTORE = cret & (1 << 27) || false;
110
    bool cpuAVXSuport = cret & (1 << 28) || false;
111

112
    if (osUsesXSAVE_XRSTORE && cpuAVXSuport) {
113
        // Check if the OS will save the YMM registers
114
        unsigned long long xcrFeatureMask = _xgetbv(_XCR_XFEATURE_ENABLED_MASK);
115
        avxSupported = (xcrFeatureMask & 0x6) || false;
116
    }
117
#endif
118

119
    if (avxSupported) {
12✔
120
        avx_support = 0; // AVX1 supported
×
121
    }
×
122
    else {
12✔
123
        avx_support = -1; // No AVX supported
12✔
124
    }
12✔
125

126
    // 1 is reserved for AVX2
127

128
#endif
12✔
129
}
12✔
130
} // namespace realm
131

132

133
// popcount, counts number of set (1) bits in argument. Intrinsics has been disabled because it's just 10-20% faster
134
// than fallback method, so a runtime-detection of support would be more expensive in total. Popcount is supported
135
// with SSE42 but not with SSE3, and we don't want separate builds for each architecture - hence a runtime check would
136
// be required.
137
#if 0 // defined(_MSC_VER) && _MSC_VER >= 1500
138
#include <intrin.h>
139

140
namespace realm {
141

142
int fast_popcount32(int32_t x)
143
{
144
    return __popcnt(x);
145
}
146
#if defined(_M_X64)
147
int fast_popcount64(int64_t x)
148
{
149
    return int(__popcnt64(x));
150
}
151
#else
152
int fast_popcount64(int64_t x)
153
{
154
    return __popcnt(unsigned(x)) + __popcnt(unsigned(x >> 32));
155
}
156
#endif
157

158
} // namespace realm
159

160
#elif 0 // defined(__GNUC__) && __GNUC__ >= 4 || defined(__INTEL_COMPILER) && __INTEL_COMPILER >= 900
161
#define fast_popcount32 __builtin_popcount
162

163
namespace realm {
164

165
#if ULONG_MAX == 0xffffffff
166
int fast_popcount64(int64_t x)
167
{
168
    return __builtin_popcount(unsigned(x)) + __builtin_popcount(unsigned(x >> 32));
169
}
170
#else
171
int fast_popcount64(int64_t x)
172
{
173
    return __builtin_popcountll(x);
174
}
175
#endif
176

177
} // namespace realm
178

179
#else
180

181
namespace {
182

183
const char a_popcount_bits[256] = {
184
    0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 1, 2, 2, 3, 2,
185
    3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3,
186
    3, 4, 3, 4, 4, 5, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5,
187
    6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5, 2, 3, 3, 4,
188
    3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4,
189
    5, 5, 6, 5, 6, 6, 7, 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6,
190
    6, 7, 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7, 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8,
191
};
192

193
} // anonymous namespace
194

195
namespace realm {
196

197
// Masking away bits might be faster than bit shifting (which can be slow). Note that the compiler may optimize this
198
// automatically. Todo, investigate.
199
int fast_popcount32(int32_t x)
200
{
393,204✔
201
    return a_popcount_bits[255 & x] + a_popcount_bits[255 & x >> 8] + a_popcount_bits[255 & x >> 16] +
393,204✔
202
           a_popcount_bits[255 & x >> 24];
393,204✔
203
}
393,204✔
204
int fast_popcount64(int64_t x)
205
{
196,602✔
206
    return fast_popcount32(static_cast<int32_t>(x)) + fast_popcount32(static_cast<int32_t>(x >> 32));
196,602✔
207
}
196,602✔
208

209
// Mutex only to make Helgrind happy
210
// If it was declared as a static inside fastrand() then it could have a race being initialised and
211
// being locked because a static inside a function is constructed the first time execution
212
// hits the function declaration (lazily). To avoid that race we declare it outside the function
213
// which means it will be constructed on program start. Post C++11, there should be no race on a
214
// construction of a local static, but tsan seems to report this as a false positive sometimes.
215
namespace {
216
util::Mutex fastrand_mutex;
217
} // end anonymous namespace
218

219
// A fast, thread safe, mediocre-quality random number generator named Xorshift
220
uint64_t fastrand(uint64_t max, bool is_seed)
221
{
393,243✔
222
    util::LockGuard lg(fastrand_mutex);
393,243✔
223

224
    // All the atomics (except the add) may be eliminated completely by the compiler on x64
225
    static std::atomic<uint64_t> state(1);
393,243✔
226

227
    // Thread safe increment to prevent two threads from producing the same value if called at the exact same time
228
    state.fetch_add(1, std::memory_order_release);
393,243✔
229
    uint64_t x = is_seed ? max : state.load(std::memory_order_acquire);
393,243✔
230
    // The result of this arithmetic may be overwritten by another thread, but that's fine in a rand generator
231
    x ^= x >> 12; // a
393,243✔
232
    x ^= x << 25; // b
393,243✔
233
    x ^= x >> 27; // c
393,243✔
234
    state.store(x, std::memory_order_release);
393,243✔
235
    return (x * 2685821657736338717ULL) % (max + 1 == 0 ? 0xffffffffffffffffULL : max + 1);
393,243✔
236
}
393,243✔
237

238
uint64_t FastRand::operator()(uint64_t max)
239
{
1,800✔
240
    uint64_t x = m_state;
1,800✔
241
    x ^= x >> 12; // a
1,800✔
242
    x ^= x << 25; // b
1,800✔
243
    x ^= x >> 27; // c
1,800✔
244
    m_state = x;
1,800✔
245
    return (x * 2685821657736338717ULL) % (max + 1 == 0 ? 0xffffffffffffffffULL : max + 1);
1,800✔
246
}
1,800✔
247

248
void millisleep(unsigned long milliseconds)
249
{
11,496✔
250
#ifdef _WIN32
251
    std::this_thread::sleep_for(std::chrono::milliseconds(milliseconds));
252
#else
253
    // sleep() takes seconds and usleep() is deprecated, so use nanosleep()
254
    timespec ts;
11,496✔
255
    size_t secs = milliseconds / 1000;
11,496✔
256
    milliseconds = milliseconds % 1000;
11,496✔
257
    ts.tv_sec = secs;
11,496✔
258
    ts.tv_nsec = milliseconds * 1000 * 1000;
11,496✔
259
    nanosleep(&ts, 0);
11,496✔
260
#endif
11,496✔
261
}
11,496✔
262

263
#ifdef REALM_SLAB_ALLOC_TUNE
264
void process_mem_usage(double& vm_usage, double& resident_set)
265
{
266
    vm_usage = 0.0;
267
    resident_set = 0.0;
268
#ifdef _WIN32
269
    HANDLE hProc = GetCurrentProcess();
270
    PROCESS_MEMORY_COUNTERS_EX info;
271
    info.cb = sizeof(info);
272
    BOOL okay = GetProcessMemoryInfo(hProc, (PROCESS_MEMORY_COUNTERS*)&info, info.cb);
273

274
    SIZE_T PrivateUsage = info.PrivateUsage;
275
    resident_set = PrivateUsage;
276
#else
277
    // the two fields we want
278
    unsigned long vsize;
279
    long rss;
280
    {
281
        std::string ignore;
282
        std::ifstream ifs("/proc/self/stat", std::ios_base::in);
283
        ifs >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >>
284
            ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >> ignore >>
285
            ignore >> ignore >> vsize >> rss;
286
    }
287

288
    long page_size_kb = sysconf(_SC_PAGE_SIZE); // in case x86-64 is configured to use 2MB pages
289
    vm_usage = vsize / 1024.0;
290
    resident_set = rss * page_size_kb;
291
#endif
292
}
293
#endif
294

295
#ifdef _WIN32
296
int gettimeofday(struct timeval* tp, struct timezone* tzp)
297
{
298
    FILETIME file_time;
299
    SYSTEMTIME system_time;
300
    ULARGE_INTEGER ularge;
301

302
    GetSystemTime(&system_time);
303
    SystemTimeToFileTime(&system_time, &file_time);
304
    ularge.LowPart = file_time.dwLowDateTime;
305
    ularge.HighPart = file_time.dwHighDateTime;
306
    const uint64_t epoch = 116444736000000000;
307
    tp->tv_sec = (long)((ularge.QuadPart - epoch) / 10000000L);
308
    tp->tv_usec = (long)(system_time.wMilliseconds * 1000);
309
    return 0;
310
}
311
#endif
312

313
int64_t platform_timegm(tm time)
314
{
45✔
315
#ifdef _WIN32
316
    // limitation of _mktime64 on windows is January 1, 1970, UTC to 23:59:59, December 31, 3000, UTC
317
    return static_cast<int64_t>(_mkgmtime64(&time));
318
#elif REALM_ANDROID
319
    // Android-9 as well as others don't have timegm support
320
    time_t t = mktime(&time);
321
    return int64_t(static_cast<int32_t>(t + localtime(&t)->tm_gmtoff));
322
#else
323
    // limitation of a 32 bit timegm (UTC) is December 13, 1901 @ 12:45:53 to January 19 2038 @ 03:14:07
324
    time_t unix_time = timegm(&time);
45✔
325
    return int64_t(static_cast<int32_t>(unix_time)); // unix_time comes as a int32_t
45✔
326
#endif
45✔
327
}
45✔
328

329

330
} // namespace realm
331

332
#endif // select best popcount implementations
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc