Advanced Strategic Command
memory-measurement.cpp
Go to the documentation of this file.
1 
2 /***************************************************************************
3  * *
4  * This program is free software; you can redistribute it and/or modify *
5  * it under the terms of the GNU General Public License as published by *
6  * the Free Software Foundation; either version 2 of the License, or *
7  * (at your option) any later version. *
8  * *
9  ***************************************************************************/
10 
11 #include <sstream>
12 
13 #include "memory-measurement.h"
14 #include "memsize_interface.h"
15 #include "iconrepository.h"
16 
17 #include "gamemap.h"
18 
19 
20 
22 {
23 
24  Node* node = new Node( "Type Repositories" );
25  root.add( node );
26 
27  node->add( measure( vehicleTypeRepository ));
28  node->add( measure( terrainTypeRepository ));
29  node->add( measure( objectTypeRepository ));
30  node->add( measure( buildingTypeRepository ));
31  // node->add( measure( technologyRepository ));
32 
33 }
34 
35 
36 void MemoryMeasurement::measure( GameMap* gamemap )
37 {
38  Node* node = new Node( "Current Map" );
39  root.add( node );
40  node->usage = gamemap->getMemoryFootprint();
41 
42  Node* buildings = new Node("Buildings");
43  node->add( buildings );
44  Node* units = new Node("Units");
45  node->add( units );
46 
47  for ( int i = 0; i < gamemap->getPlayerCount(); ++i ) {
48  Player& p = gamemap->getPlayer(i);
49  for ( Player::VehicleList::iterator i = p.vehicleList.begin(); i != p.vehicleList.end(); ++i )
50  units->usage += (*i)->getMemoryFootprint();
51 
52  for ( Player::BuildingList::iterator i = p.buildingList.begin(); i != p.buildingList.end(); ++i )
53  buildings->usage += (*i)->getMemoryFootprint();
54  }
55 
56  Node* fields = new Node("Fields");
57  node->add( fields);
58 
59 
60  Node* objects = new Node("Objects");
61  fields->add(objects);
62 
63  for ( int y = 0; y < gamemap->ysize; ++y )
64  for ( int x = 0; x < gamemap->xsize; ++x ) {
65  MapField* fld = gamemap->getField(x,y);
66  fields->usage += fld->getMemoryFootprint();
67  objects->usage += sizeof( Object ) * fld->objects.size();
68  }
69 
70 
71 }
72 
74 {
75  Node* node = new Node( "Global Images" );
76  root.add( node );
77  node->usage = IconRepository::getMemoryFootprint();
78 }
79 
80 
81 
82 int MemoryMeasurement::Node::sum()
83 {
84  int sum = usage;
85  for ( deallocating_vector<Node*>::iterator i = childs.begin(); i != childs.end(); ++i )
86  sum += (*i)->sum();
87  return sum;
88 }
89 
90 
91 ASCString MemoryMeasurement::Node::prettyPrint( int indent )
92 {
93  ASCString result; // ( 2 * indent, ' ' );
94  result += "#indent=" + ASCString::toString(indent*20) + "," + ASCString::toString(indent*20) + "#";
95  result += name + ": " + ASCString::toString(usage/1024) + " / " + ASCString::toString( sum()/1024) + "\n";
96  for ( deallocating_vector<Node*>::iterator i = childs.begin(); i != childs.end(); ++i )
97  result += (*i)->prettyPrint( indent + 1);
98  return result;
99 }
100 
101 
102 
103 template<class T>
104 MemoryMeasurement::Node* MemoryMeasurement::measure( ItemRepository<T>& repository )
105 {
106 
107  Node* node = new Node( repository.getTypeName() );
108 
109  node->usage += for_each ( repository.getVector().begin(), repository.getVector().end(), MemorySum<T>() ).size;
110 
111  return node;
112 }
113 
114 
115 
116  template<class T>
117  struct ObjectStorage : public unary_function<T, void>
118  {
119  static bool compare( const pair<int,int>& a1, const pair<int,int>& a2 )
120  {
121  return a1.second > a2.second;
122  }
123 
124  typedef vector< pair<int,int> > Storage;
126  void operator() (const T& x) { storage.push_back( make_pair(x->id, getMemoryFootprint(*x))); };
128  {
129  sort( storage.begin(), storage.end(), &compare );
130 
131  ASCString s;
132  for ( Storage::iterator i = storage.begin(); i != storage.end(); ++i ) {
133  ObjectType* o = objectTypeRepository.getObject_byID(i->first);
134  s += ASCString::toString(i->second/1024) + " " + o->name + "(ID: " + ASCString::toString(i->first) + "; " + o->filename + ")\n";
135  }
136 
137  return s;
138  }
139  };
140 
141 
142 
144 {
145  ASCString result = "#fontsize=16#Memory Footprint\n#fontsize=12#all sizes in kB\n\n";
146  result += root.prettyPrint(0);
147 
148  result += "#indent=0,0#";
149 
150  result += "\n\n#fontsize=16#Objects#fontsize=12#\n\n";
151 
152  // for_each ( objectTypeRepository.getVector().begin(), objectTypeRepository.getVector().end(), ObjectStorage<ObjectType*>() );
153  result += for_each ( objectTypeRepository.getVector().begin(), objectTypeRepository.getVector().end(), ObjectStorage<ObjectType*>() ).print();
154  return result;
155 }
156 
157 
158 
159 
160 
ASCString filename
The name of the file from which the item was loaded.
Definition: typen.h:290
ItemRepositoryLoader< ObjectType > objectTypeRepository("objecttype")
Player & getPlayer(PlayerID p)
Definition: gamemap.h:257
int xsize
the size of the map
Definition: gamemap.h:201
vector< pair< int, int > > Storage
static bool compare(const pair< int, int > &a1, const pair< int, int > &a2)
ItemRepositoryLoader< BuildingType > buildingTypeRepository("buildingtype")
a single field of the map
Definition: mapfield.h:26
The ASCString class provides an abstract way to manipulate strings.
Definition: ascstring.h:14
static int getMemoryFootprint()
vector< T * > & getVector()
static ASCString toString(int i)
converts the parameter to a String
Definition: ascstring.cpp:193
int getMemoryFootprint() const
Definition: gamemap.cpp:1970
VehicleList vehicleList
a list of all units
Definition: player.h:135
ItemRepositoryLoader< VehicleType > vehicleTypeRepository("vehicletype")
ItemRepositoryLoader< TerrainType > terrainTypeRepository("terraintype")
An object that can be placed on fields. Roads, pipelines and ditches are examples of objects...
Definition: objecttype.h:30
Definition of THE central asc class: GameMap.
ASCString getTypeName()
int ysize
Definition: gamemap.h:201
BuildingList buildingList
a list of all units
Definition: player.h:139
a container that stores pointers and deletes the pointed-to objects on destruction ...
Definition: typen.h:314
int getMemoryFootprint(const T &t)
int getMemoryFootprint() const
Definition: mapfield.cpp:666
void operator()(const T &x)
an instance of an object type (ObjectType) on the map
Definition: objects.h:46
the different players in ASC. There may be 8 players (0..7) and neutral units (8) ...
Definition: player.h:99
int getPlayerCount() const
Definition: gamemap.h:255
ObjectContainer objects
Definition: mapfield.h:124
The map. THE central structure of ASC, which holds everything not globally available together...
Definition: gamemap.h:182
MapField * getField(int x, int y)
Definition: gamemap.h:465
ASCString name
The name of the object.
Definition: objecttype.h:122