Welcome to mirror list, hosted at ThFree Co, Russian Federation.

timing.cpp « lib « carve « extern - git.blender.org/blender.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: a98796cd8a73a3031c2f4dd3669fb820b8d22973 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
// Begin License:
// Copyright (C) 2006-2011 Tobias Sargeant (tobias.sargeant@gmail.com).
// All rights reserved.
//
// This file is part of the Carve CSG Library (http://carve-csg.com/)
//
// This file may be used under the terms of the GNU General Public
// License version 2.0 as published by the Free Software Foundation
// and appearing in the file LICENSE.GPL2 included in the packaging of
// this file.
//
// This file is provided "AS IS" with NO WARRANTY OF ANY KIND,
// INCLUDING THE WARRANTIES OF DESIGN, MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE.
// End:


#if CARVE_USE_TIMINGS

#if defined(HAVE_CONFIG_H)
#  include <carve_config.h>
#endif

#include <carve/timing.hpp>

#include <cstring>
#include <list>
#include <stack>
#include <vector>
#include <map>
#include <iostream>
#include <string>
#include <algorithm>

#ifdef WIN32
#include <windows.h>
#else 
#include <time.h>
#include <sys/time.h>
#endif

#ifndef CARVE_USE_GLOBAL_NEW_DELETE
#define CARVE_USE_GLOBAL_NEW_DELETE 0
#endif

namespace carve {
  static uint64_t memoryCurr = 0;
  static uint64_t memoryTotal = 0;
  unsigned blkCntCurr[32] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };
  unsigned blkCntTotal[32] = { 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 };

  void addBlk(unsigned size) {
    unsigned i = 0;
    while (i < 31 && (1U<<i) < size) ++i;
    blkCntCurr[i]++;
    blkCntTotal[i]++;
  }
  void remBlk(unsigned size) {
    unsigned i = 0;
    while (i < 31 && (1<<i) < size) ++i;
    blkCntCurr[i]--;
  }
}

// lets provide a global new and delete as well

#if CARVE_USE_GLOBAL_NEW_DELETE

#if defined(__APPLE__)

#include <stdlib.h>
#include <malloc/malloc.h>

void* carve_alloc(size_t size) {
  void *p = malloc(size); 
  if (p == 0) throw std::bad_alloc(); // ANSI/ISO compliant behavior

  unsigned sz = malloc_size(p);
  carve::memoryCurr += sz;
  carve::memoryTotal += sz;
  carve::addBlk(sz);
  return p;
}

void carve_free(void *p) {
  unsigned sz = malloc_size(p);
  carve::memoryCurr -= sz;
  carve::remBlk(sz);
  free(p); 
}

#else

void* carve_alloc(size_t size) {
  void *p = malloc(size + 4); 
  if (p == 0) throw std::bad_alloc(); // ANSI/ISO compliant behavior

  int *sizePtr = (int*)p;
  *sizePtr = size;
  ++sizePtr;
  carve::memoryCurr += size;
  carve::memoryTotal += size;
  carve::addBlk(size);
  return sizePtr;
}

void carve_free(void *p) {
  // our memory block is actually a size of an int behind this pointer.
  int *sizePtr = (int*)p;

  --sizePtr;

  carve::memoryCurr -= *sizePtr;
  int size = *sizePtr;
  carve::remBlk(size);
  free(sizePtr); 
}

#endif


void* operator new (size_t size) {
  return carve_alloc(size);
}

void* operator new[](size_t size) {
  return carve_alloc(size);
}


void operator delete (void *p) {
  carve_free(p);
}

void operator delete[](void *p) {
  carve_free(p);
}

#endif

namespace carve {



#ifdef WIN32

  typedef __int64 precise_time_t;
  
  precise_time_t g_frequency;
  
  void initTime() {
    ::QueryPerformanceFrequency((LARGE_INTEGER*)&g_frequency);  
  }
  
  void getTime(precise_time_t &t) {
    ::QueryPerformanceCounter((LARGE_INTEGER*)&t);
  }
  
  double diffTime(precise_time_t from, precise_time_t to) {
    return (double)(to - from) / (double)g_frequency;
  }
  
#else 

  typedef double precise_time_t;

  void initTime() {
  }

  void getTime(precise_time_t &t) {
    struct timeval tv;
    gettimeofday(&tv, NULL);
    t = tv.tv_sec + tv.tv_usec / 1000000.0;
  }

  double diffTime(precise_time_t from, precise_time_t to) {
    return to - from;
  }

#endif

  struct Entry {
    Entry(int _id) {
      id = _id;
      time = 0;
      parent = NULL;
    }
    int id;
    double time;
    int64_t memoryDiff;
    int64_t allocTotal;
    int delta_blk_cnt_curr[32];
    int delta_blk_cnt_total[32];
    Entry *parent;
    std::vector<Entry *> children;
  };

  struct Timer {
    struct cmp {
      bool operator()(const std::pair<int, double> &a, const std::pair<int, double> &b) const {
        return b.second < a.second;
      }
      bool operator()(const Entry * const &a, const Entry * const &b) const {
        return b->time < a->time;
      }
    };

    Timer() {
      initTime();
    }

    struct Snapshot {
      precise_time_t time;
      uint64_t memory_curr;
      uint64_t memory_total;
      unsigned blk_cnt_curr[32];
      unsigned blk_cnt_total[32];
    };
    
    static void getSnapshot(Snapshot &snapshot) {
      getTime(snapshot.time);
      snapshot.memory_curr = carve::memoryCurr;
      snapshot.memory_total = carve::memoryTotal;
      std::memcpy(snapshot.blk_cnt_curr, carve::blkCntCurr, sizeof(carve::blkCntCurr));
      std::memcpy(snapshot.blk_cnt_total, carve::blkCntTotal, sizeof(carve::blkCntTotal));
    }

    static void compareSnapshot(const Snapshot &from, const Snapshot &to, Entry *entry) {
      entry->time = diffTime(from.time, to.time);
      entry->memoryDiff = to.memory_curr - from.memory_curr;
      entry->allocTotal = to.memory_total - from.memory_total;
      for (int i = 0; i < 32; i++) {
        entry->delta_blk_cnt_curr[i] = to.blk_cnt_curr[i] - from.blk_cnt_curr[i];
        entry->delta_blk_cnt_total[i] = to.blk_cnt_total[i] - from.blk_cnt_total[i];
      }
    }
    
    std::stack<std::pair<Entry*, Snapshot> > currentTimers;
    
    void startTiming(int id) {
      entries.push_back(Entry(id)); 
      currentTimers.push(std::make_pair(&entries.back(), Snapshot()));  
      getSnapshot(currentTimers.top().second);
    }
    
    double endTiming() {
      Snapshot end;
      getSnapshot(end);

      Entry *entry = currentTimers.top().first;
      compareSnapshot(currentTimers.top().second, end, entry);
      
      currentTimers.pop();
      if (!currentTimers.empty()) {
        entry->parent = currentTimers.top().first;
        entry->parent->children.push_back(entry);
      } else {
        root_entries.push_back(entry);
      }
      //std::sort(entry->children.begin(), entry->children.end(), cmp());
      return entry->time;
    }

    typedef std::list<Entry> EntryList;
    EntryList entries;
    std::vector<Entry *> root_entries;

    std::map<int, std::string> names;
    
    static std::string formatMemory(int64_t value) {
      
      std::ostringstream result;

      result << (value >= 0 ? "+" : "-");
      if (value < 0) {
        value = -value;
      }
      
      int power = 1;
      while (value > pow(10.0, power)) {
        power++;
      }
      
      for (power--; power >= 0; power--) {
        int64_t base = pow(10.0, power);
        int64_t amount = value / base;
        result <<
#if defined(_MSC_VER) && _MSC_VER < 1300
          (long)
#endif
          amount;
        if (power > 0 && (power % 3) == 0) {
          result << ",";
        }
        value -= amount * base;
      }
      
      result << " bytes";

      return result.str();
    }

    void printEntries(std::ostream &o, const std::vector<Entry *> &entries, const std::string &indent, double parent_time) {
      if (parent_time <= 0.0) {
        parent_time = 0.0;
        for (size_t i = 0; i < entries.size(); ++i) {
          parent_time += entries[i]->time;
        }
      }
      double t_tot = 0.0;
      for (size_t i = 0; i < entries.size(); ++i) {
        const Entry *entry = entries[i];

        std::ostringstream r;
        r << indent;
        std::string str = names[entry->id];
        if (str.empty()) {
          r << "(" << entry->id << ")";
        } else {
          r << str;
        }
        r << " ";
        std::string pad(r.str().size(), ' ');
        r << " - exectime: " << entry->time << "s (" << (entry->time * 100.0 / parent_time) << "%)" << std::endl;
        if (entry->allocTotal || entry->memoryDiff) {
          r << pad << " - alloc: " << formatMemory(entry->allocTotal) << " delta: " << formatMemory(entry->memoryDiff) << std::endl;
          r << pad << " - alloc blks:";
          for (int i = 0; i < 32; i++) { if (entry->delta_blk_cnt_total[i]) r << ' ' << ((1 << (i - 1)) + 1) << '-' << (1 << i) << ':' << entry->delta_blk_cnt_total[i]; }
          r << std::endl;
          r << pad << " - delta blks:";
          for (int i = 0; i < 32; i++) { if (entry->delta_blk_cnt_curr[i]) r << ' ' << ((1 << (i - 1)) + 1) << '-' << (1 << i) << ':' << entry->delta_blk_cnt_curr[i]; }
          r << std::endl;
        }
        o << r.str();
        t_tot += entry->time;
        if (entry->children.size()) printEntries(o, entry->children, indent + "   ", entry->time);
      }
      if (t_tot < parent_time) {
        o << indent << "*** unaccounted: " << (parent_time - t_tot) << "s   (" << (100.0 - t_tot * 100.0 / parent_time) << "%)" << std::endl;
      }
    }

    void print() {
      std::map<int, double> totals;
      std::cerr << "Timings: " << std::endl;
      // print out all the entries.

      //std::sort(root_entries.begin(), root_entries.end(), cmp());

      printEntries(std::cerr, root_entries, "   ", -1.0);

      for (EntryList::const_iterator it = entries.begin(); it != entries.end(); ++it) {
        totals[(*it).id] += (*it).time;
      }

      std::cerr << std::endl;
      std::cerr << "Totals: " << std::endl;

      std::vector<std::pair<int, double> > sorted_totals;
      sorted_totals.reserve(totals.size());
      for (std::map<int,double>::iterator it = totals.begin(); it != totals.end(); ++it) {
        sorted_totals.push_back(*it);
      }

      std::sort(sorted_totals.begin(), sorted_totals.end(), cmp());

      for (std::vector<std::pair<int,double> >::iterator it = sorted_totals.begin(); it != sorted_totals.end(); ++it) {
        std::cerr << "  ";
        std::string str = names[it->first];
        if (str.empty()) {
          std::cerr << "(" << it->first << ")";
        } else {
          std::cerr << str;
        }
        std::cerr << " - " << it->second << "s " << std::endl;
      }
    }
    void registerID(int id, const char *name) {
      names[id] = name;
    }
    int registerID(const char *name) {
      int id = names.size() + 1;
      names[id] = name;
      return id;
    }

  };

  Timer timer;


  TimingBlock::TimingBlock(int id) {
#if CARVE_USE_TIMINGS
    timer.startTiming(id);
#endif
  }

  TimingBlock::TimingBlock(const TimingName &name) {
#if CARVE_USE_TIMINGS
    timer.startTiming(name.id);
#endif
  }

  
  TimingBlock::~TimingBlock() {
#if CARVE_USE_TIMINGS
    timer.endTiming();
#endif
  }
  void Timing::start(int id) {
#if CARVE_USE_TIMINGS
    timer.startTiming(id);
#endif
  }
  
  double Timing::stop() {
#if CARVE_USE_TIMINGS
    return timer.endTiming();
#endif
  }
  
  void Timing::printTimings() {
    timer.print();
  }
  
  void Timing::registerID(int id, const char *name) {
    timer.registerID(id, name);
  }
 
  TimingName::TimingName(const char *name) {
    id = timer.registerID(name);
  }

}

#endif