Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in / Register
Toggle navigation
Menu
Open sidebar
Josh Wilcox
crown-core
Commits
c03aee5a
Commit
c03aee5a
authored
9 years ago
by
Pieter Wuille
Committed by
Alastair Clark
7 years ago
Browse files
Options
Download
Email Patches
Plain Diff
Separate core memory usage computation in core_memusage.h
parent
9e386425
Changes
11
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
src/Makefile.am
+1
-0
src/Makefile.am
src/coins.cpp
+6
-6
src/coins.cpp
src/coins.h
+2
-2
src/coins.h
src/core_memusage.h
+62
-0
src/core_memusage.h
src/memusage.h
+1
-65
src/memusage.h
src/primitives/transaction.cpp
+0
-5
src/primitives/transaction.cpp
src/primitives/transaction.h
+0
-9
src/primitives/transaction.h
src/script/script.cpp
+0
-5
src/script/script.cpp
src/script/script.h
+0
-3
src/script/script.h
src/test/coins_tests.cpp
+2
-2
src/test/coins_tests.cpp
src/txmempool.cpp
+1
-1
src/txmempool.cpp
with
75 additions
and
98 deletions
+75
-98
src/Makefile.am
View file @
c03aee5a
...
...
@@ -90,6 +90,7 @@ BITCOIN_CORE_H = \
compat.h
\
compressor.h
\
core_io.h
\
core_memusage.h
\
crypter.h
\
darksend.h
\
darksend-relay.h
\
...
...
This diff is collapsed.
Click to expand it.
src/coins.cpp
View file @
c03aee5a
...
...
@@ -83,7 +83,7 @@ CCoinsMap::const_iterator CCoinsViewCache::FetchCoins(const uint256 &txid) const
// version as fresh.
ret
->
second
.
flags
=
CCoinsCacheEntry
::
FRESH
;
}
cachedCoinsUsage
+=
memusage
::
DynamicUsage
(
ret
->
second
.
coins
);
cachedCoinsUsage
+=
ret
->
second
.
coins
.
DynamicMemoryUsage
(
);
return
ret
;
}
...
...
@@ -168,7 +168,7 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
assert
(
it
->
second
.
flags
&
CCoinsCacheEntry
::
FRESH
);
CCoinsCacheEntry
&
entry
=
cacheCoins
[
it
->
first
];
entry
.
coins
.
swap
(
it
->
second
.
coins
);
cachedCoinsUsage
+=
memusage
::
DynamicUsage
(
entry
.
coins
);
cachedCoinsUsage
+=
entry
.
coins
.
DynamicMemoryUsage
(
);
entry
.
flags
=
CCoinsCacheEntry
::
DIRTY
|
CCoinsCacheEntry
::
FRESH
;
}
}
else
{
...
...
@@ -176,13 +176,13 @@ bool CCoinsViewCache::BatchWrite(CCoinsMap &mapCoins, const uint256 &hashBlockIn
// The grandparent does not have an entry, and the child is
// modified and being pruned. This means we can just delete
// it from the parent.
cachedCoinsUsage
-=
memusage
::
DynamicUsage
(
itUs
->
second
.
coins
);
cachedCoinsUsage
-=
itUs
->
second
.
coins
.
DynamicMemoryUsage
(
);
cacheCoins
.
erase
(
itUs
);
}
else
{
// A normal modification.
cachedCoinsUsage
-=
memusage
::
DynamicUsage
(
itUs
->
second
.
coins
);
cachedCoinsUsage
-=
itUs
->
second
.
coins
.
DynamicMemoryUsage
(
);
itUs
->
second
.
coins
.
swap
(
it
->
second
.
coins
);
cachedCoinsUsage
+=
memusage
::
DynamicUsage
(
itUs
->
second
.
coins
);
cachedCoinsUsage
+=
itUs
->
second
.
coins
.
DynamicMemoryUsage
(
);
itUs
->
second
.
flags
|=
CCoinsCacheEntry
::
DIRTY
;
}
}
...
...
@@ -270,6 +270,6 @@ CCoinsModifier::~CCoinsModifier()
cache
.
cacheCoins
.
erase
(
it
);
}
else
{
// If the coin still exists after the modification, add the new usage
cache
.
cachedCoinsUsage
+=
memusage
::
DynamicUsage
(
it
->
second
.
coins
);
cache
.
cachedCoinsUsage
+=
it
->
second
.
coins
.
DynamicMemoryUsage
(
);
}
}
This diff is collapsed.
Click to expand it.
src/coins.h
View file @
c03aee5a
...
...
@@ -7,6 +7,7 @@
#define BITCOIN_COINS_H
#include "compressor.h"
#include "core_memusage.h"
#include "memusage.h"
#include "serialize.h"
#include "uint256.h"
...
...
@@ -257,8 +258,7 @@ public:
size_t
DynamicMemoryUsage
()
const
{
size_t
ret
=
memusage
::
DynamicUsage
(
vout
);
BOOST_FOREACH
(
const
CTxOut
&
out
,
vout
)
{
const
std
::
vector
<
unsigned
char
>
*
script
=
&
out
.
scriptPubKey
;
ret
+=
memusage
::
DynamicUsage
(
*
script
);
ret
+=
RecursiveDynamicUsage
(
out
.
scriptPubKey
);
}
return
ret
;
}
...
...
This diff is collapsed.
Click to expand it.
src/core_memusage.h
0 → 100644
View file @
c03aee5a
// Copyright (c) 2015 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#ifndef BITCOIN_CORE_MEMUSAGE_H
#define BITCOIN_CORE_MEMUSAGE_H
#include "primitives/transaction.h"
#include "primitives/block.h"
#include "memusage.h"
static
inline
size_t
RecursiveDynamicUsage
(
const
CScript
&
script
)
{
return
memusage
::
DynamicUsage
(
*
static_cast
<
const
std
::
vector
<
unsigned
char
>*>
(
&
script
));
}
static
inline
size_t
RecursiveDynamicUsage
(
const
COutPoint
&
out
)
{
return
0
;
}
static
inline
size_t
RecursiveDynamicUsage
(
const
CTxIn
&
in
)
{
return
RecursiveDynamicUsage
(
in
.
scriptSig
)
+
RecursiveDynamicUsage
(
in
.
prevout
);
}
static
inline
size_t
RecursiveDynamicUsage
(
const
CTxOut
&
out
)
{
return
RecursiveDynamicUsage
(
out
.
scriptPubKey
);
}
static
inline
size_t
RecursiveDynamicUsage
(
const
CTransaction
&
tx
)
{
size_t
mem
=
memusage
::
DynamicUsage
(
tx
.
vin
)
+
memusage
::
DynamicUsage
(
tx
.
vout
);
for
(
std
::
vector
<
CTxIn
>::
const_iterator
it
=
tx
.
vin
.
begin
();
it
!=
tx
.
vin
.
end
();
it
++
)
{
mem
+=
RecursiveDynamicUsage
(
*
it
);
}
for
(
std
::
vector
<
CTxOut
>::
const_iterator
it
=
tx
.
vout
.
begin
();
it
!=
tx
.
vout
.
end
();
it
++
)
{
mem
+=
RecursiveDynamicUsage
(
*
it
);
}
return
mem
;
}
static
inline
size_t
RecursiveDynamicUsage
(
const
CMutableTransaction
&
tx
)
{
size_t
mem
=
memusage
::
DynamicUsage
(
tx
.
vin
)
+
memusage
::
DynamicUsage
(
tx
.
vout
);
for
(
std
::
vector
<
CTxIn
>::
const_iterator
it
=
tx
.
vin
.
begin
();
it
!=
tx
.
vin
.
end
();
it
++
)
{
mem
+=
RecursiveDynamicUsage
(
*
it
);
}
for
(
std
::
vector
<
CTxOut
>::
const_iterator
it
=
tx
.
vout
.
begin
();
it
!=
tx
.
vout
.
end
();
it
++
)
{
mem
+=
RecursiveDynamicUsage
(
*
it
);
}
return
mem
;
}
static
inline
size_t
RecursiveDynamicUsage
(
const
CBlock
&
block
)
{
size_t
mem
=
memusage
::
DynamicUsage
(
block
.
vtx
)
+
memusage
::
DynamicUsage
(
block
.
vMerkleTree
);
for
(
std
::
vector
<
CTransaction
>::
const_iterator
it
=
block
.
vtx
.
begin
();
it
!=
block
.
vtx
.
end
();
it
++
)
{
mem
+=
RecursiveDynamicUsage
(
*
it
);
}
return
mem
;
}
static
inline
size_t
RecursiveDynamicUsage
(
const
CBlockLocator
&
locator
)
{
return
memusage
::
DynamicUsage
(
locator
.
vHave
);
}
#endif // BITCOIN_CORE_MEMUSAGE_H
This diff is collapsed.
Click to expand it.
src/memusage.h
View file @
c03aee5a
...
...
@@ -34,28 +34,14 @@ static inline size_t DynamicUsage(const float& v) { return 0; }
static
inline
size_t
DynamicUsage
(
const
double
&
v
)
{
return
0
;
}
template
<
typename
X
>
static
inline
size_t
DynamicUsage
(
X
*
const
&
v
)
{
return
0
;
}
template
<
typename
X
>
static
inline
size_t
DynamicUsage
(
const
X
*
const
&
v
)
{
return
0
;
}
template
<
typename
X
,
typename
Y
>
static
inline
size_t
DynamicUsage
(
std
::
pair
<
X
,
Y
>
&
p
)
{
return
0
;
}
/** Compute the memory used for dynamically allocated but owned data structures.
* For generic data types, this is *not* recursive. DynamicUsage(vector<vector<int> >)
* will compute the memory used for the vector<int>'s, but not for the ints inside.
* This is for efficiency reasons, as these functions are intended to be fast. If
* application data structures require more accurate inner accounting, they should
* use RecursiveDynamicUsage, iterate themselves, or use more efficient caching +
* updating on modification.
* iterate themselves, or use more efficient caching + updating on modification.
*/
template
<
typename
X
>
static
size_t
DynamicUsage
(
const
std
::
vector
<
X
>&
v
);
template
<
typename
X
>
static
size_t
DynamicUsage
(
const
std
::
set
<
X
>&
s
);
template
<
typename
X
,
typename
Y
>
static
size_t
DynamicUsage
(
const
std
::
map
<
X
,
Y
>&
m
);
template
<
typename
X
,
typename
Y
>
static
size_t
DynamicUsage
(
const
boost
::
unordered_set
<
X
,
Y
>&
s
);
template
<
typename
X
,
typename
Y
,
typename
Z
>
static
size_t
DynamicUsage
(
const
boost
::
unordered_map
<
X
,
Y
,
Z
>&
s
);
template
<
typename
X
>
static
size_t
DynamicUsage
(
const
X
&
x
);
template
<
typename
X
>
static
size_t
RecursiveDynamicUsage
(
const
std
::
vector
<
X
>&
v
);
template
<
typename
X
>
static
size_t
RecursiveDynamicUsage
(
const
std
::
set
<
X
>&
v
);
template
<
typename
X
,
typename
Y
>
static
size_t
RecursiveDynamicUsage
(
const
std
::
map
<
X
,
Y
>&
v
);
template
<
typename
X
,
typename
Y
>
static
size_t
RecursiveDynamicUsage
(
const
std
::
pair
<
X
,
Y
>&
v
);
template
<
typename
X
>
static
size_t
RecursiveDynamicUsage
(
const
X
&
v
);
static
inline
size_t
MallocUsage
(
size_t
alloc
)
{
...
...
@@ -88,54 +74,18 @@ static inline size_t DynamicUsage(const std::vector<X>& v)
return
MallocUsage
(
v
.
capacity
()
*
sizeof
(
X
));
}
template
<
typename
X
>
static
inline
size_t
RecursiveDynamicUsage
(
const
std
::
vector
<
X
>&
v
)
{
size_t
usage
=
DynamicUsage
(
v
);
BOOST_FOREACH
(
const
X
&
x
,
v
)
{
usage
+=
RecursiveDynamicUsage
(
x
);
}
return
usage
;
}
template
<
typename
X
>
static
inline
size_t
DynamicUsage
(
const
std
::
set
<
X
>&
s
)
{
return
MallocUsage
(
sizeof
(
stl_tree_node
<
X
>
))
*
s
.
size
();
}
template
<
typename
X
>
static
inline
size_t
RecursiveDynamicUsage
(
const
std
::
set
<
X
>&
v
)
{
size_t
usage
=
DynamicUsage
(
v
);
BOOST_FOREACH
(
const
X
&
x
,
v
)
{
usage
+=
RecursiveDynamicUsage
(
x
);
}
return
usage
;
}
template
<
typename
X
,
typename
Y
>
static
inline
size_t
DynamicUsage
(
const
std
::
map
<
X
,
Y
>&
m
)
{
return
MallocUsage
(
sizeof
(
stl_tree_node
<
std
::
pair
<
const
X
,
Y
>
>
))
*
m
.
size
();
}
template
<
typename
X
,
typename
Y
>
static
inline
size_t
RecursiveDynamicUsage
(
const
std
::
map
<
X
,
Y
>&
v
)
{
size_t
usage
=
DynamicUsage
(
v
);
for
(
typename
std
::
map
<
X
,
Y
>::
const_iterator
it
=
v
.
begin
();
it
!=
v
.
end
();
it
++
)
{
usage
+=
RecursiveDynamicUsage
(
*
it
);
}
return
usage
;
}
template
<
typename
X
,
typename
Y
>
static
inline
size_t
RecursiveDynamicUsage
(
const
std
::
pair
<
X
,
Y
>&
v
)
{
return
RecursiveDynamicUsage
(
v
.
first
)
+
RecursiveDynamicUsage
(
v
.
second
);
}
// Boost data structures
template
<
typename
X
>
...
...
@@ -157,20 +107,6 @@ static inline size_t DynamicUsage(const boost::unordered_map<X, Y, Z>& m)
return
MallocUsage
(
sizeof
(
boost_unordered_node
<
std
::
pair
<
const
X
,
Y
>
>
))
*
m
.
size
()
+
MallocUsage
(
sizeof
(
void
*
)
*
m
.
bucket_count
());
}
// Dispatch to class method as fallback
template
<
typename
X
>
static
inline
size_t
DynamicUsage
(
const
X
&
x
)
{
return
x
.
DynamicMemoryUsage
();
}
template
<
typename
X
>
static
inline
size_t
RecursiveDynamicUsage
(
const
X
&
x
)
{
return
DynamicUsage
(
x
);
}
}
#endif
This diff is collapsed.
Click to expand it.
src/primitives/transaction.cpp
View file @
c03aee5a
...
...
@@ -98,11 +98,6 @@ void CTransaction::UpdateHash() const
*
const_cast
<
uint256
*>
(
&
hash
)
=
SerializeHash
(
*
this
);
}
size_t
CTransaction
::
DynamicMemoryUsage
()
const
{
return
memusage
::
RecursiveDynamicUsage
(
vin
)
+
memusage
::
RecursiveDynamicUsage
(
vout
);
}
CTransaction
::
CTransaction
()
:
nVersion
(
CTransaction
::
CURRENT_VERSION
),
vin
(),
vout
(),
nLockTime
(
0
)
{
}
CTransaction
::
CTransaction
(
const
CMutableTransaction
&
tx
)
:
nVersion
(
tx
.
nVersion
),
vin
(
tx
.
vin
),
vout
(
tx
.
vout
),
nLockTime
(
tx
.
nLockTime
)
{
...
...
This diff is collapsed.
Click to expand it.
src/primitives/transaction.h
View file @
c03aee5a
...
...
@@ -7,7 +7,6 @@
#define BITCOIN_PRIMITIVES_TRANSACTION_H
#include "amount.h"
#include "memusage.h"
#include "script/script.h"
#include "serialize.h"
#include "uint256.h"
...
...
@@ -52,8 +51,6 @@ public:
uint256
GetHash
();
size_t
DynamicMemoryUsage
()
const
{
return
0
;
}
};
/** An input of a transaction. It contains the location of the previous
...
...
@@ -103,8 +100,6 @@ public:
}
std
::
string
ToString
()
const
;
size_t
DynamicMemoryUsage
()
const
{
return
scriptSig
.
DynamicMemoryUsage
();
}
};
/** An output of a transaction. It contains the public key that the next input
...
...
@@ -172,8 +167,6 @@ public:
}
std
::
string
ToString
()
const
;
size_t
DynamicMemoryUsage
()
const
{
return
scriptPubKey
.
DynamicMemoryUsage
();
}
};
struct
CMutableTransaction
;
...
...
@@ -257,8 +250,6 @@ public:
}
std
::
string
ToString
()
const
;
size_t
DynamicMemoryUsage
()
const
;
};
/** A mutable version of CTransaction. */
...
...
This diff is collapsed.
Click to expand it.
src/script/script.cpp
View file @
c03aee5a
...
...
@@ -283,8 +283,3 @@ std::string CScript::ToString() const
}
return
str
;
}
size_t
CScript
::
DynamicMemoryUsage
()
const
{
return
memusage
::
DynamicUsage
(
*
(
static_cast
<
const
std
::
vector
<
unsigned
char
>*>
(
this
)));
}
This diff is collapsed.
Click to expand it.
src/script/script.h
View file @
c03aee5a
...
...
@@ -6,7 +6,6 @@
#ifndef BITCOIN_SCRIPT_SCRIPT_H
#define BITCOIN_SCRIPT_SCRIPT_H
#include "memusage.h"
#include "crypto/common.h"
#include <assert.h>
...
...
@@ -606,8 +605,6 @@ public:
// The default std::vector::clear() does not release memory.
std
::
vector
<
unsigned
char
>
().
swap
(
*
this
);
}
size_t
DynamicMemoryUsage
()
const
;
};
#endif // BITCOIN_SCRIPT_SCRIPT_H
This diff is collapsed.
Click to expand it.
src/test/coins_tests.cpp
View file @
c03aee5a
...
...
@@ -69,9 +69,9 @@ public:
// Manually recompute the dynamic usage of the whole data, and compare it.
size_t
ret
=
memusage
::
DynamicUsage
(
cacheCoins
);
for
(
CCoinsMap
::
iterator
it
=
cacheCoins
.
begin
();
it
!=
cacheCoins
.
end
();
it
++
)
{
ret
+=
memusage
::
DynamicUsage
(
it
->
second
.
coins
);
ret
+=
it
->
second
.
coins
.
DynamicMemoryUsage
(
);
}
BOOST_CHECK_EQUAL
(
memusage
::
DynamicUsage
(
*
this
),
ret
);
BOOST_CHECK_EQUAL
(
Dynamic
Memory
Usage
(),
ret
);
}
};
...
...
This diff is collapsed.
Click to expand it.
src/txmempool.cpp
View file @
c03aee5a
...
...
@@ -30,7 +30,7 @@ CTxMemPoolEntry::CTxMemPoolEntry(const CTransaction& _tx, const CAmount& _nFee,
nTxSize
=
::
GetSerializeSize
(
tx
,
SER_NETWORK
,
PROTOCOL_VERSION
);
nModSize
=
tx
.
CalculateModifiedSize
(
nTxSize
);
nUsageSize
=
tx
.
Dynamic
Memory
Usage
();
nUsageSize
=
Recursive
DynamicUsage
(
tx
);
}
CTxMemPoolEntry
::
CTxMemPoolEntry
(
const
CTxMemPoolEntry
&
other
)
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment