Vlasiator ebf0dd394 on dev (v5.4.0 + 1054 commits)
Loading...
Searching...
No Matches
memoryallocation.cpp
Go to the documentation of this file.
1/*
2 * This file is part of Vlasiator.
3 * Copyright 2010-2016 Finnish Meteorological Institute
4 *
5 * For details of usage, see the COPYING file and read the "Rules of the Road"
6 * at http://www.physics.helsinki.fi/vlasiator/
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22#include <cstdlib>
23#include <string.h>
24#include <iostream>
25#include <math.h>
26#include <unordered_map> // for hasher
27#include <limits>
28#include "logger.h"
29#include "memoryallocation.h"
30#include "common.h"
31#include "parameters.h"
32#ifdef PAPI_MEM
33#include "papi.h"
34#endif
35
37using namespace std;
38
39#ifdef USE_JEMALLOC
40#define STRINGIFY_HELPER(x) #x
41#define STRINGIFY(x) STRINGIFY_HELPER(x)
42
43// Declare global new etc. only if using old legacy versions of jemalloc, prior to 5.0.0
44#if JEMALLOC_VERSION_MAJOR < 5
45
46// Global new using jemalloc
47void *operator new(size_t size)
48{
49 void *p;
50 p = je_malloc(size);
51 if(!p) {
52 bad_alloc ba;
53 throw ba;
54 }
55 return p;
56}
57
58// Global new[] using jemalloc
59void *operator new[](size_t size)
60{
61 void *p;
62 p = je_malloc(size);
63 if(!p) {
64 bad_alloc ba;
65 throw ba;
66 }
67 return p;
68}
69
70// Global delete using jemalloc
71void operator delete(void *p)
72{
73 je_free(p);
74}
75
76// Global delete[] using jemalloc
77void operator delete[](void *p)
78{
79 je_free(p);
80}
81
82#if __cpp_sized_deallocation >= 201309
83void operator delete(void *ptr, std::size_t size) noexcept {
84 je_sdallocx(ptr, size, /*flags=*/0);
85}
86void operator delete[](void *ptr, std::size_t size) noexcept {
87 je_sdallocx(ptr, size, /*flags=*/0);
88}
89#endif // __cpp_sized_deallocation
90#endif // JEMALLOC_VERSION_MAJOR < 5
91#endif // use jemalloc
92
95#ifdef USE_JEMALLOC
96 je_mallctl("arena." STRINGIFY(MALLCTL_ARENAS_ALL) ".purge", NULL, NULL, NULL, 0);
97#endif
98}
99
102#ifdef USE_JEMALLOC
103 bool logResult = false;
104 bool foo {false};
105 size_t bar {1};
106 if (logResult) {
107 // Read initial value
108 je_mallctl("background_thread", &foo, &bar, NULL, 0);
109 logFile << "(MEM) mallctl: background_thread value was ";
110 if (foo) {
111 logFile << "true";
112 } else {
113 logFile << "false";
114 }
115 }
116 // Set background threads to true
117 foo = true;
118 bar = 1;
119 je_mallctl("background_thread", NULL, NULL, &foo, bar);
120 if (logResult) {
121 // Read updated value
122 je_mallctl("background_thread", &foo, &bar, NULL, 0);
123 logFile << ", now set to ";
124 if (foo) {
125 logFile << "true";
126 } else {
127 logFile << "false";
128 }
129 logFile << "." << endl;
130 }
131#endif
132}
Logger logFile
Definition main.cpp:25
Logger diagnostic
Definition ioread.cpp:59
void memory_purge()
void memory_configurator()
constexpr uint size