Skip to content

Commit bbafd6d

Browse files
author
larryhou
committed
Cache summary of native/managed objects
1 parent a8406df commit bbafd6d

6 files changed

Lines changed: 81 additions & 6 deletions

File tree

MemoryCrawler/MemoryCrawler/Crawler/cache.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,18 +54,22 @@ void SnapshotCrawlerCache::createNativeTypeTable()
5454
"typeIndex INTEGER PRIMARY KEY," \
5555
"name TEXT NOT NULL," \
5656
"nativeBaseTypeArrayIndex INTEGER," \
57-
"managedTypeArrayIndex INTEGER);");
57+
"managedTypeArrayIndex INTEGER,"\
58+
"instanceCount INTEGER," \
59+
"instanceMemory INTEGER);");
5860
}
5961

6062
void SnapshotCrawlerCache::insert(Array<PackedNativeType> &nativeTypes)
6163
{
62-
insert<PackedNativeType>("INSERT INTO nativeTypes VALUES (?1, ?2, ?3, ?4);",
64+
insert<PackedNativeType>("INSERT INTO nativeTypes VALUES (?1, ?2, ?3, ?4, ?5, ?6);",
6365
nativeTypes, [](PackedNativeType &nt, sqlite3_stmt *stmt)
6466
{
6567
sqlite3_bind_int(stmt, 1, nt.typeIndex);
6668
sqlite3_bind_text(stmt, 2, nt.name->c_str(), (int)nt.name->size(), SQLITE_STATIC);
6769
sqlite3_bind_int64(stmt, 3, nt.nativeBaseTypeArrayIndex);
6870
sqlite3_bind_int(stmt, 4, nt.managedTypeArrayIndex);
71+
sqlite3_bind_int(stmt, 5, nt.instanceCount);
72+
sqlite3_bind_int(stmt, 6, nt.instanceMemory);
6973
});
7074
}
7175

@@ -118,7 +122,10 @@ void SnapshotCrawlerCache::createTypeTable()
118122
"typeIndex INTEGER PRIMARY KEY," \
119123
"typeInfoAddress INTEGER," \
120124
"nativeTypeArrayIndex INTEGER," \
121-
"fields INTEGER);");
125+
"fields INTEGER," \
126+
"instanceCount INTEGER," \
127+
"instanceMemory INTEGER," \
128+
"nativeMemory INTEGER);");
122129
}
123130

124131
void SnapshotCrawlerCache::createFieldTable()
@@ -133,7 +140,7 @@ void SnapshotCrawlerCache::createFieldTable()
133140

134141
void SnapshotCrawlerCache::insert(Array<TypeDescription> &types)
135142
{
136-
insert<TypeDescription>("INSERT INTO types VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12);",
143+
insert<TypeDescription>("INSERT INTO types VALUES (?1, ?2, ?3, ?4, ?5, ?6, ?7, ?8, ?9, ?10, ?11, ?12, ?13, ?14, ?15);",
137144
types, [this](TypeDescription &t, sqlite3_stmt *stmt)
138145
{
139146
sqlite3_bind_int(stmt, 1, t.arrayRank);
@@ -151,6 +158,9 @@ void SnapshotCrawlerCache::insert(Array<TypeDescription> &types)
151158
sqlite3_bind_int64(stmt, 10, t.typeInfoAddress);
152159
sqlite3_bind_int(stmt, 11, t.nativeTypeArrayIndex);
153160
sqlite3_bind_int(stmt, 12, t.fields->size);
161+
sqlite3_bind_int(stmt, 13, t.instanceCount);
162+
sqlite3_bind_int(stmt, 14, t.instanceMemory);
163+
sqlite3_bind_int(stmt, 15, t.nativeMemory);
154164
});
155165

156166
// type fields
@@ -337,6 +347,8 @@ MemorySnapshotCrawler &SnapshotCrawlerCache::read(const char *uuid)
337347
nt.name = new string((char *)sqlite3_column_text(stmt, 1));
338348
nt.nativeBaseTypeArrayIndex = sqlite3_column_int(stmt, 2);
339349
nt.managedTypeArrayIndex = sqlite3_column_int(stmt, 3);
350+
nt.instanceCount = sqlite3_column_int(stmt, 4);
351+
nt.instanceMemory = sqlite3_column_int(stmt, 5);
340352
});
341353
__sampler.end(); // read_native_types
342354

@@ -390,6 +402,9 @@ MemorySnapshotCrawler &SnapshotCrawlerCache::read(const char *uuid)
390402
mt.fields = new Array<FieldDescription>(size);
391403
}
392404
}
405+
mt.instanceCount = sqlite3_column_int(stmt, 12);
406+
mt.instanceMemory = sqlite3_column_int(stmt, 13);
407+
mt.nativeMemory = sqlite3_column_int(stmt, 14);
393408
});
394409
__sampler.begin("read_type_fields");
395410
int32_t typeIndex = 0;

MemoryCrawler/MemoryCrawler/Crawler/crawler.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ void MemorySnapshotCrawler::crawl()
1414
initManagedTypes();
1515
crawlGCHandles();
1616
crawlStatic();
17+
summarize();
1718
__sampler.end();
1819
}
1920

@@ -34,6 +35,35 @@ void MemorySnapshotCrawler::debug()
3435
#endif
3536
}
3637

38+
void MemorySnapshotCrawler::summarize()
39+
{
40+
__sampler.begin("summarize_managed_objects");
41+
auto &nativeObjects = *snapshot.nativeObjects;
42+
auto &typeDescriptions = *snapshot.typeDescriptions;
43+
for (auto i = 0; i < typeDescriptions.size; i++)
44+
{
45+
auto &type = typeDescriptions[i];
46+
type.instanceMemory = 0;
47+
type.instanceCount = 0;
48+
type.nativeMemory = 0;
49+
}
50+
51+
for (auto i = 0; i < managedObjects.size(); i++)
52+
{
53+
auto &mo = managedObjects[i];
54+
auto &type = typeDescriptions[mo.typeIndex];
55+
type.instanceMemory += mo.size;
56+
type.instanceCount += 1;
57+
58+
if (mo.nativeObjectIndex >= 0)
59+
{
60+
auto &no = nativeObjects[mo.nativeObjectIndex];
61+
type.nativeMemory += no.size;
62+
}
63+
}
64+
__sampler.end();
65+
}
66+
3767
void MemorySnapshotCrawler::initManagedTypes()
3868
{
3969
__sampler.begin("initManagedTypes");

MemoryCrawler/MemoryCrawler/Crawler/crawler.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,8 @@ class MemorySnapshotCrawler
168168
EntityJoint &joint,
169169
bool isRealType,
170170
int32_t depth);
171+
172+
void summarize();
171173
};
172174

173175
template<class T>

MemoryCrawler/MemoryCrawler/Crawler/serialize.cpp

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,7 +454,34 @@ void MemorySnapshotReader::postSnapshot()
454454
{
455455
nativeObjects[i].nativeObjectArrayIndex = i;
456456
}
457+
sampler.end(); // set_native_object_index
458+
459+
summarize();
460+
457461
sampler.end();
462+
}
463+
464+
void MemorySnapshotReader::summarize()
465+
{
466+
sampler.begin("summarize_native_objects");
467+
468+
auto &nativeTypes = *snapshot->nativeTypes;
469+
for (auto i = 0; i < nativeTypes.size; i++)
470+
{
471+
auto &type = nativeTypes[i];
472+
type.instanceMemory = 0;
473+
type.instanceCount = 0;
474+
}
475+
476+
auto &nativeObjects = *snapshot->nativeObjects;
477+
for (auto i = 0; i < nativeObjects.size; i++)
478+
{
479+
auto &no = nativeObjects[i];
480+
auto &type = nativeTypes[no.nativeTypeArrayIndex];
481+
type.instanceMemory += no.size;
482+
type.instanceCount += 1;
483+
}
484+
458485
sampler.end();
459486
}
460487

MemoryCrawler/MemoryCrawler/Crawler/serialize.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ class MemorySnapshotReader
3838
void readHeader(FileStream &fs);
3939
void readSnapshot(FileStream &fs);
4040
void postSnapshot();
41+
void summarize();
4142
};
4243

4344
#endif /* serialize_h */

MemoryCrawler/MemoryCrawler/Crawler/snapshot.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ struct TypeDescription
4848
int32_t typeIndex;
4949

5050
int32_t instanceCount = 0;
51-
int32_t managedMemory = 0;
51+
int32_t instanceMemory = 0;
5252
int32_t nativeMemory = 0;
5353
int32_t nativeTypeArrayIndex = -1;
5454

@@ -107,7 +107,7 @@ struct PackedNativeType
107107
int32_t typeIndex = -1;
108108
int32_t managedTypeArrayIndex = -1;
109109
int32_t instanceCount = 0;
110-
int32_t nativeMemory = 0;
110+
int32_t instanceMemory = 0;
111111

112112
~PackedNativeType();
113113
};

0 commit comments

Comments
 (0)