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 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
|
/*=========================================================================
Program: Visualization Toolkit
Module: vtkTimerLog.cxx
Copyright (c) Ken Martin, Will Schroeder, Bill Lorensen
All rights reserved.
See Copyright.txt or http://www.kitware.com/Copyright.htm for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notice for more information.
=========================================================================*/
// .NAME vtkTimerLog - Maintains timing table for performance analysis
// .SECTION Description
// vtkTimerLog contains walltime and cputime measurements associated
// with a given event. These results can be later analyzed when
// "dumping out" the table.
//
// In addition, vtkTimerLog allows the user to simply get the current
// time, and to start/stop a simple timer separate from the timing
// table logging.
#include "vtkTimerLog.h"
#include <stdarg.h> // Needed for ...
#ifndef _WIN32
#include <climits> // for CLK_TCK
#include <sys/time.h>
#include <unistd.h>
#endif
#ifndef _WIN32_WCE
#include <sys/types.h>
#include <ctime>
#endif
#include "vtkObjectFactory.h"
vtkStandardNewMacro(vtkTimerLog);
// Create a singleton to cleanup the table. No other singletons
// should be using the timer log, so it is safe to do this without the
// full ClassInitialize/ClassFinalize idiom.
class vtkTimerLogCleanup
{
public:
~vtkTimerLogCleanup()
{
vtkTimerLog::CleanupLog();
}
};
static vtkTimerLogCleanup vtkTimerLogCleanupInstance;
// initialze the class variables
int vtkTimerLog::Logging = 1;
int vtkTimerLog::Indent = 0;
int vtkTimerLog::MaxEntries = 100;
int vtkTimerLog::NextEntry = 0;
int vtkTimerLog::WrapFlag = 0;
vtkTimerLogEntry *vtkTimerLog::TimerLog = NULL;
#ifdef CLK_TCK
int vtkTimerLog::TicksPerSecond = CLK_TCK;
#else
int vtkTimerLog::TicksPerSecond = 60;
#endif
#ifndef CLOCKS_PER_SEC
#define CLOCKS_PER_SEC (vtkTimerLog::TicksPerSecond)
#endif
#ifdef _WIN32
#ifndef _WIN32_WCE
timeb vtkTimerLog::FirstWallTime;
timeb vtkTimerLog::CurrentWallTime;
#else
FILETIME vtkTimerLog::FirstWallTime;
FILETIME vtkTimerLog::CurrentWallTime;
#endif
#else
timeval vtkTimerLog::FirstWallTime;
timeval vtkTimerLog::CurrentWallTime;
tms vtkTimerLog::FirstCpuTicks;
tms vtkTimerLog::CurrentCpuTicks;
#endif
//----------------------------------------------------------------------------
// Allocate timing table with MaxEntries elements.
void vtkTimerLog::AllocateLog()
{
delete [] vtkTimerLog::TimerLog;
vtkTimerLog::TimerLog = new vtkTimerLogEntry[vtkTimerLog::MaxEntries];
}
//----------------------------------------------------------------------------
// Remove timer log.
void vtkTimerLog::CleanupLog()
{
if ( !vtkTimerLog::TimerLog )
{
return;
}
delete [] vtkTimerLog::TimerLog;
vtkTimerLog::TimerLog = 0;
}
//----------------------------------------------------------------------------
// Clear the timing table. walltime and cputime will also be set
// to zero when the first new event is recorded.
void vtkTimerLog::ResetLog()
{
vtkTimerLog::WrapFlag = 0;
vtkTimerLog::NextEntry = 0;
// may want to free TimerLog to force realloc so
// that user can resize the table by changing MaxEntries.
}
//----------------------------------------------------------------------------
// Record a timing event. The event is represented by a formatted
// string.
void vtkTimerLog::FormatAndMarkEvent(const char *format, ...)
{
if (! vtkTimerLog::Logging)
{
return;
}
static char event[4096];
va_list var_args;
va_start(var_args, format);
vsprintf(event, format, var_args);
va_end(var_args);
vtkTimerLog::MarkEvent(event);
}
//----------------------------------------------------------------------------
// Record a timing event and capture walltime and cputicks.
void vtkTimerLog::MarkEvent(const char *event)
{
if (! vtkTimerLog::Logging)
{
return;
}
int strsize;
double time_diff;
int ticks_diff;
strsize = (strlen(event)) > VTK_LOG_EVENT_LENGTH - 1
? VTK_LOG_EVENT_LENGTH-1 : static_cast<int>(strlen(event));
// If this the first event we're recording, allocate the
// internal timing table and initialize WallTime and CpuTicks
// for this first event to zero.
if (vtkTimerLog::NextEntry == 0 && ! vtkTimerLog::WrapFlag)
{
if (vtkTimerLog::TimerLog == NULL)
{
vtkTimerLog::AllocateLog();
}
#ifdef _WIN32
#ifdef _WIN32_WCE
SYSTEMTIME st;
GetLocalTime(&st);
SystemTimeToFileTime(&st, &(vtkTimerLog::FirstWallTime));
#else
::ftime( &(vtkTimerLog::FirstWallTime) );
#endif
#else
gettimeofday( &(vtkTimerLog::FirstWallTime), NULL );
times(&FirstCpuTicks);
#endif
vtkTimerLog::TimerLog[0].Indent = vtkTimerLog::Indent;
vtkTimerLog::TimerLog[0].WallTime = 0.0;
vtkTimerLog::TimerLog[0].CpuTicks = 0;
strncpy(vtkTimerLog::TimerLog[0].Event, event, strsize);
vtkTimerLog::TimerLog[0].Event[strsize] = '\0';
vtkTimerLog::NextEntry = 1;
return;
}
#ifdef _WIN32
#ifdef _WIN32_WCE
SYSTEMTIME st;
GetLocalTime(&st);
SystemTimeToFileTime(&st, &(vtkTimerLog::CurrentWallTime));
time_diff = (vtkTimerLog::CurrentWallTime.dwHighDateTime -
vtkTimerLog::FirstWallTime.dwHighDateTime);
time_diff = time_diff * 429.4967296;
time_diff = time_diff + ((vtkTimerLog::CurrentWallTime.dwLowDateTime -
vtkTimerLog::FirstWallTime.dwLowDateTime) / 10000000.0);
#else
static double scale = 1.0/1000.0;
::ftime( &(vtkTimerLog::CurrentWallTime) );
time_diff =
vtkTimerLog::CurrentWallTime.time - vtkTimerLog::FirstWallTime.time;
time_diff +=
(vtkTimerLog::CurrentWallTime.millitm
- vtkTimerLog::FirstWallTime.millitm) * scale;
#endif
ticks_diff = 0;
#else
static double scale = 1.0/1000000.0;
gettimeofday( &(vtkTimerLog::CurrentWallTime), NULL );
time_diff = vtkTimerLog::CurrentWallTime.tv_sec
- vtkTimerLog::FirstWallTime.tv_sec;
time_diff +=
(vtkTimerLog::CurrentWallTime.tv_usec
- vtkTimerLog::FirstWallTime.tv_usec) * scale;
times(&CurrentCpuTicks);
ticks_diff = (CurrentCpuTicks.tms_utime + CurrentCpuTicks.tms_stime) -
(FirstCpuTicks.tms_utime + FirstCpuTicks.tms_stime);
#endif
vtkTimerLog::TimerLog[vtkTimerLog::NextEntry].Indent = vtkTimerLog::Indent;
vtkTimerLog::TimerLog[vtkTimerLog::NextEntry].WallTime =
static_cast<double>(time_diff);
vtkTimerLog::TimerLog[vtkTimerLog::NextEntry].CpuTicks = ticks_diff;
strncpy(vtkTimerLog::TimerLog[vtkTimerLog::NextEntry].Event, event, strsize);
vtkTimerLog::TimerLog[vtkTimerLog::NextEntry].Event[strsize] = '\0';
vtkTimerLog::NextEntry++;
if (vtkTimerLog::NextEntry == vtkTimerLog::MaxEntries)
{
vtkTimerLog::NextEntry = 0;
vtkTimerLog::WrapFlag = 1;
}
}
//----------------------------------------------------------------------------
// Record a timing event and capture walltime and cputicks.
// Increments indent after mark.
void vtkTimerLog::MarkStartEvent(const char *event)
{
if (! vtkTimerLog::Logging)
{ // Maybe we should still change the Indent ...
return;
}
vtkTimerLog::MarkEvent(event);
++vtkTimerLog::Indent;
}
//----------------------------------------------------------------------------
// Record a timing event and capture walltime and cputicks.
// Decrements indent after mark.
void vtkTimerLog::MarkEndEvent(const char *event)
{
if (! vtkTimerLog::Logging)
{ // Maybe we should still change the Indent ...
return;
}
vtkTimerLog::MarkEvent(event);
--vtkTimerLog::Indent;
}
//----------------------------------------------------------------------------
// Record a timing event and capture walltime and cputicks.
int vtkTimerLog::GetNumberOfEvents()
{
if (vtkTimerLog::WrapFlag)
{
return vtkTimerLog::MaxEntries;
}
else
{
return vtkTimerLog::NextEntry;
}
}
//----------------------------------------------------------------------------
vtkTimerLogEntry *vtkTimerLog::GetEvent(int idx)
{
int num = vtkTimerLog::GetNumberOfEvents();
int start = 0;
if (vtkTimerLog::WrapFlag)
{
start = vtkTimerLog::NextEntry;
}
if (idx < 0 || idx >= num)
{
cerr << "Bad entry index.";
return NULL;
}
idx = (idx + start) % vtkTimerLog::MaxEntries;
return vtkTimerLog::TimerLog + idx;
}
//----------------------------------------------------------------------------
int vtkTimerLog::GetEventIndent(int idx)
{
vtkTimerLogEntry *tmp = vtkTimerLog::GetEvent(idx);
if (tmp)
{
return tmp->Indent;
}
else
{
return 0;
}
}
//----------------------------------------------------------------------------
double vtkTimerLog::GetEventWallTime(int idx)
{
vtkTimerLogEntry *tmp = vtkTimerLog::GetEvent(idx);
if (tmp)
{
return tmp->WallTime;
}
else
{
return 0.0;
}
}
//----------------------------------------------------------------------------
const char* vtkTimerLog::GetEventString(int idx)
{
vtkTimerLogEntry *tmp = vtkTimerLog::GetEvent(idx);
if (tmp)
{
return tmp->Event;
}
else
{
return NULL;
}
}
//----------------------------------------------------------------------------
// Write the timing table out to a file. Calculate some helpful
// statistics (deltas and percentages) in the process.
void vtkTimerLog::DumpLogWithIndents(ostream *os, double threshold)
{
#ifndef _WIN32_WCE
int num;
int i1, i2, j;
int indent1;
int nextIndent;
double dtime;
num = vtkTimerLog::GetNumberOfEvents();
for (i1=0; i1 < num; i1++)
{
indent1 = vtkTimerLog::GetEventIndent(i1);
// Search for an end event.
i2 = i1 + 1;
while (i2 < num && vtkTimerLog::GetEventIndent(i2) > indent1)
{ // This was a start event.
++i2;
}
// If the next indent is smaller, then the event should be an end event.
if (i2 == num)
{
nextIndent = vtkTimerLog::Indent;
}
else
{
nextIndent = vtkTimerLog::GetEventIndent(i2);
}
// Backup one to get the end event.
--i2;
// Simple events and end events will have dtime of 0.
dtime = vtkTimerLog::GetEventWallTime(i2) - vtkTimerLog::GetEventWallTime(i1);
if (nextIndent == indent1)
{ // not an end event
if (dtime >= threshold || i2 == i1)
{ // start event past threshold or singleton event.
// Print the indent.
j = indent1;
while (j-- > 0)
{
*os << " ";
}
*os << vtkTimerLog::GetEventString(i1);
if (i2 > i1)
{ // Start event.
*os << ", " << dtime << " seconds\n";
}
else
{ // Singlton event.
*os << endl;
}
}
}
}
#endif
}
//----------------------------------------------------------------------------
// Write the timing table out to a file. Calculate some helpful
// statistics (deltas and percentages) in the process.
void vtkTimerLog::DumpLog(const char *filename)
{
#ifndef _WIN32_WCE
ofstream os_with_warning_C4701(filename);
int i;
if ( vtkTimerLog::WrapFlag )
{
vtkTimerLog::DumpEntry(os_with_warning_C4701, 0,
vtkTimerLog::TimerLog[vtkTimerLog::NextEntry].WallTime, 0,
vtkTimerLog::TimerLog[vtkTimerLog::NextEntry].CpuTicks, 0,
vtkTimerLog::TimerLog[vtkTimerLog::NextEntry].Event);
for (i=vtkTimerLog::NextEntry+1; i<vtkTimerLog::MaxEntries; i++)
{
vtkTimerLog::DumpEntry(os_with_warning_C4701,
i-vtkTimerLog::NextEntry, vtkTimerLog::TimerLog[i].WallTime,
vtkTimerLog::TimerLog[i].WallTime
- vtkTimerLog::TimerLog[i-1].WallTime,
vtkTimerLog::TimerLog[i].CpuTicks,
vtkTimerLog::TimerLog[i].CpuTicks
- vtkTimerLog::TimerLog[i-1].CpuTicks,
vtkTimerLog::TimerLog[i].Event);
}
vtkTimerLog::DumpEntry(os_with_warning_C4701, vtkTimerLog::MaxEntries-vtkTimerLog::NextEntry,
vtkTimerLog::TimerLog[0].WallTime,
vtkTimerLog::TimerLog[0].WallTime
-vtkTimerLog::TimerLog[vtkTimerLog::MaxEntries-1].WallTime,
vtkTimerLog::TimerLog[0].CpuTicks,
vtkTimerLog::TimerLog[0].CpuTicks
-vtkTimerLog::TimerLog[vtkTimerLog::MaxEntries-1].CpuTicks,
vtkTimerLog::TimerLog[0].Event);
for (i=1; i<vtkTimerLog::NextEntry; i++)
{
vtkTimerLog::DumpEntry(os_with_warning_C4701, vtkTimerLog::MaxEntries-vtkTimerLog::NextEntry+i,
vtkTimerLog::TimerLog[i].WallTime,
vtkTimerLog::TimerLog[i].WallTime
- vtkTimerLog::TimerLog[i-1].WallTime,
vtkTimerLog::TimerLog[i].CpuTicks,
vtkTimerLog::TimerLog[i].CpuTicks
- vtkTimerLog::TimerLog[i-1].CpuTicks,
vtkTimerLog::TimerLog[i].Event);
}
}
else
{
vtkTimerLog::DumpEntry(os_with_warning_C4701, 0, vtkTimerLog::TimerLog[0].WallTime, 0,
vtkTimerLog::TimerLog[0].CpuTicks, 0,
vtkTimerLog::TimerLog[0].Event);
for (i=1; i<vtkTimerLog::NextEntry; i++)
{
vtkTimerLog::DumpEntry(os_with_warning_C4701, i, vtkTimerLog::TimerLog[i].WallTime,
vtkTimerLog::TimerLog[i].WallTime
- vtkTimerLog::TimerLog[i-1].WallTime,
vtkTimerLog::TimerLog[i].CpuTicks,
vtkTimerLog::TimerLog[i].CpuTicks
- vtkTimerLog::TimerLog[i-1].CpuTicks,
vtkTimerLog::TimerLog[i].Event);
}
}
os_with_warning_C4701.close();
#endif
}
//----------------------------------------------------------------------------
// Print method for vtkTimerLog.
void vtkTimerLog::PrintSelf(ostream& os, vtkIndent indent)
{
this->Superclass::PrintSelf(os, indent);
int i;
os << indent << "MaxEntries: " << vtkTimerLog::MaxEntries << "\n";
os << indent << "NextEntry: " << vtkTimerLog::NextEntry << "\n";
os << indent << "WrapFlag: " << vtkTimerLog::WrapFlag << "\n";
os << indent << "TicksPerSecond: " << vtkTimerLog::TicksPerSecond << "\n";
os << "\n";
os << indent << "Entry \tWall Time\tCpuTicks\tEvent\n";
os << indent << "----------------------------------------------\n";
if ( vtkTimerLog::WrapFlag )
{
for (i=vtkTimerLog::NextEntry; i<vtkTimerLog::MaxEntries; i++)
{
os << indent << i << "\t\t" << TimerLog[i].WallTime << "\t\t" <<
TimerLog[i].CpuTicks << "\t\t" << TimerLog[i].Event << "\n";
}
}
for (i=0; i<vtkTimerLog::NextEntry; i++)
{
os << indent << i << "\t\t" << TimerLog[i].WallTime << "\t\t" <<
TimerLog[i].CpuTicks << "\t\t" << TimerLog[i].Event << "\n";
}
os << "\n" << indent << "StartTime: " << this->StartTime << "\n";
os << indent << "WrapFlag: " << vtkTimerLog::WrapFlag << "\n";
}
// Methods to support simple timer functionality, separate from
// timer table logging.
//----------------------------------------------------------------------------
// Returns the elapsed number of seconds since January 1, 1970. This
// is also called Universal Coordinated Time.
double vtkTimerLog::GetUniversalTime()
{
double currentTimeInSeconds;
#ifdef _WIN32
#ifdef _WIN32_WCE
FILETIME CurrentTime;
SYSTEMTIME st;
GetLocalTime(&st);
SystemTimeToFileTime(&st, &CurrentTime);
currentTimeInSeconds = CurrentTime.dwHighDateTime;
currentTimeInSeconds *= 429.4967296;
currentTimeInSeconds = currentTimeInSeconds +
CurrentTime.dwLowDateTime / 10000000.0;
#else
timeb CurrentTime;
static double scale = 1.0/1000.0;
::ftime( &CurrentTime );
currentTimeInSeconds = CurrentTime.time + scale * CurrentTime.millitm;
#endif
#else
timeval CurrentTime;
static double scale = 1.0/1000000.0;
gettimeofday( &CurrentTime, NULL );
currentTimeInSeconds = CurrentTime.tv_sec + scale * CurrentTime.tv_usec;
#endif
return currentTimeInSeconds;
}
//----------------------------------------------------------------------------
double vtkTimerLog::GetCPUTime()
{
double currentCPUTime = 1.0;
#ifndef _WIN32_WCE
currentCPUTime = static_cast<double>(clock()) /static_cast<double>(CLOCKS_PER_SEC);
#endif
return currentCPUTime;
}
//----------------------------------------------------------------------------
// Set the StartTime to the current time. Used with GetElapsedTime().
void vtkTimerLog::StartTimer()
{
this->StartTime = vtkTimerLog::GetUniversalTime();
}
//----------------------------------------------------------------------------
// Sets EndTime to the current time. Used with GetElapsedTime().
void vtkTimerLog::StopTimer()
{
this->EndTime = vtkTimerLog::GetUniversalTime();
}
//----------------------------------------------------------------------------
// Returns the difference between StartTime and EndTime as
// a floating point value indicating the elapsed time in seconds.
double vtkTimerLog::GetElapsedTime()
{
return (this->EndTime - this->StartTime);
}
//----------------------------------------------------------------------------
void vtkTimerLog::DumpEntry(ostream& os, int index, double ttime,
double deltatime,
int tick, int deltatick, const char *event)
{
os << index << " "
<< ttime << " "
<< deltatime << " "
<< static_cast<double>(tick)/vtkTimerLog::TicksPerSecond << " "
<< static_cast<double>(deltatick)/vtkTimerLog::TicksPerSecond << " ";
if (deltatime == 0.0)
{
os << "0.0 ";
}
else
{
os << 100.0*deltatick/vtkTimerLog::TicksPerSecond/deltatime << " ";
}
os << event << "\n";
}
//----------------------------------------------------------------------------
void vtkTimerLog::SetMaxEntries(int a)
{
int num, i, offset;
vtkTimerLogEntry *newLog, *tmp;
if (vtkTimerLog::MaxEntries == a)
{
return;
}
newLog = new vtkTimerLogEntry[a];
if (vtkTimerLog::TimerLog == NULL)
{
vtkTimerLog::MaxEntries = a;
vtkTimerLog::TimerLog = newLog;
return;
}
// Copy the old log to the new.
num = vtkTimerLog::GetNumberOfEvents();
offset = 0;
if (a < num)
{
offset = num - a;
num = a;
}
for (i = 0; i < num; ++i)
{
tmp = vtkTimerLog::GetEvent(i+offset);
newLog[i] = *tmp;
}
delete [] vtkTimerLog::TimerLog;
vtkTimerLog::MaxEntries = a;
vtkTimerLog::TimerLog = newLog;
vtkTimerLog::WrapFlag = 0;
vtkTimerLog::NextEntry = num;
}
//----------------------------------------------------------------------------
int vtkTimerLog::GetMaxEntries()
{
return vtkTimerLog::MaxEntries;
}
|