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
|
/*
Title: Profiling
Author: Dave Matthews, Cambridge University Computer Laboratory
Copyright (c) 2000-7
Cambridge University Technical Services Limited
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#ifdef WIN32
#include "winconfig.h"
#else
#include "config.h"
#endif
#ifdef HAVE_STDIO_H
#include <stdio.h>
#endif
#ifdef HAVE_ASSERT_H
#include <assert.h>
#define ASSERT(x) assert(x)
#else
#define ASSERT(x) 0
#endif
#ifdef HAVE_SYS_PARAM_H
#include <sys/param.h>
#endif
#ifdef HAVE_SYS_TIME_H
#include <sys/time.h>
#endif
#ifdef HAVE_SIGNAL_H
#include <signal.h>
#endif
#ifdef HAVE_ERRNO_H
#include <errno.h>
#endif
#ifdef HAVE_FCNTL_H
#include <fcntl.h>
#endif
#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif
#ifdef HAVE_STDLIB_H
#include <stdlib.h>
#endif
#ifdef HAVE_STRING_H
#include <string.h>
#endif
#ifdef HAVE_LIMITS_H
#include <limits.h>
#endif
/************************************************************************
*
* Include runtime headers
*
************************************************************************/
#include "globals.h"
#include "sighandler.h"
#include "arb.h"
#include "machine_dep.h"
#include "foreign.h"
#include "diagnostics.h"
#include "processes.h"
#include "polystring.h"
#include "profiling.h"
#include "save_vec.h"
#include "rts_module.h"
#include "gc.h" // For gc_phase
#include "memmgr.h"
#include "scanaddrs.h"
#include "locking.h"
/******************************************************************************/
/* */
/* PROFILING SHARED DATA */
/* */
/******************************************************************************/
static POLYUNSIGNED gc_count1 = 0;
static POLYUNSIGNED gc_count2 = 0;
static POLYUNSIGNED gc_count3 = 0;
static POLYUNSIGNED total_count = 0;
static POLYUNSIGNED unknown_count = 0;
ProfileMode profileMode;
/* If Accumulated is added to one of the above profiling is only turned off
by the next call with PROF_ACCUMULATED+PROF_OFF. This allows us to profile
the compiler. */
#define PROF_ACCUMULATED 4
/******************************************************************************/
/* */
/* PROFILING FUNCTIONS */
/* */
/******************************************************************************/
typedef struct
{
POLYUNSIGNED count;
PolyWord functionName;
} PROFENTRY, *PPROFENTRY;
/* Initial vector size. */
#define INITSIZE 40
/* This structure is used to retain count information.
Whenever counting is turned off we go through the store
checking for non-zero counts and build a table of the count
with the name of the function. That is then sorted and printed. */
static struct
{
POLYUNSIGNED total;
PPROFENTRY pTab;
int size;
int used;
} P;
// Lock to serialise updates of counts. Only used during update.
// Not required when we print the counts since there's only one thread
// running then.
static PLock countLock;
// Adds incr to the profile count for the function pointed at by
// pc or by one of its callers.
// This is called from a signal handler in the case of time profiling.
void add_count(TaskData *taskData, POLYCODEPTR fpc, PolyWord *sp, int incr)
{
/* The first PC may be valid even if it's not a code pointer */
bool is_code = true;
PolyWord pc = PolyWord::FromCodePtr(fpc);
StackObject *stack = taskData->stack;
PolyWord *endStack = stack->Offset(stack->Length());
/* First try the pc value we have been given - if that fails search down
the stack to see if there is a return address we can use. */
for (;;)
{
/* Get the address of the code segment from the code pointer */
if (pc.IsCodePtr() || is_code)
{
is_code = false;
// Check that the pc value is within the heap. It could be
// in the assembly code.
MemSpace *space = gMem.SpaceForAddress(pc.AsAddress());
if (space != 0)
{
PolyObject *ptr = ObjCodePtrToPtr(pc.AsCodePtr());
ASSERT(ptr->IsCodeObject());
PolyWord *consts = ptr->ConstPtrForCode();
if (consts[0] != TAGGED(0)) // Skip anonymous code.
{
PLocker locker(&countLock);
((POLYUNSIGNED*)consts)[-1] += incr;
total_count += incr;
return;
}
}
/* else just fall through and try next candidate address */
} /* OBJ_IS_CODEPTR(pc) */
/* Find next candidate address */
if (sp < endStack)
pc = *sp++;
else /* Reached bottom of stack without finding valid code pointer */
{
PLocker locker(&countLock);
unknown_count += incr;
total_count += incr;
return;
}
} /* loop "forever" */
/*NOTREACHED*/
}
/* newProfileEntry - return the address of an entry in the profile table,
extending it if necessary. */
static PPROFENTRY newProfileEntry(void)
{
if (P.used == P.size)
{
if (P.size == 0) P.size = INITSIZE;
else P.size += P.size/2;
P.pTab = (PPROFENTRY)realloc(P.pTab, P.size*sizeof(PROFENTRY));
}
return &P.pTab[P.used++];
}
/* qsortTab - sort the profiling results by count. It could use the
name as a secondary sort but that's probably unnecessary. I'm not
sure that Quicksort is the best way of sorting the table since
the values tend to be very unevenly spread, with a few large
values and large numbers of very small values. */
static void qsortTab(int nStart, int nEnd)
{
int startPos, endPos;
if (nEnd - nStart <= 1) return;
if (nEnd - nStart == 2)
{
if (P.pTab[nStart].count > P.pTab[nStart+1].count)
{
/* Swap entries. */
PROFENTRY saved = P.pTab[nStart];
P.pTab[nStart] = P.pTab[nStart+1];
P.pTab[nStart+1] = saved;
}
return;
}
/* Three or more entries. */
startPos = nStart;
endPos = nEnd-1;
PROFENTRY median = P.pTab[startPos]; /* Use the first entry as the median. */
while (1)
{
while (endPos > startPos && P.pTab[endPos].count > median.count) endPos--;
if (endPos == startPos) break;
P.pTab[startPos++] = P.pTab[endPos];
if (endPos == startPos) break;
while (endPos > startPos && P.pTab[startPos].count <= median.count) startPos++;
if (endPos == startPos) break;
P.pTab[endPos--] = P.pTab[startPos];
if (endPos == startPos) break;
}
P.pTab[startPos] = median;
qsortTab(nStart, startPos);
qsortTab(startPos+1, nEnd);
}
/* writeProfileResults - print out the saved results. */
static void writeProfileResults(void)
{
if (P.used != 0)
{
int i;
/* Sort the table. */
qsortTab(0, P.used);
/* Print it out. */
for (i = 0; i < P.used; i++)
{
PPROFENTRY pEnt = &P.pTab[i];
fprintf(stdout, "%10lu ", pEnt->count);
print_string(pEnt->functionName);
putc ('\n', stdout);
}
}
free(P.pTab);
P.pTab = 0;
P.size = 0;
P.used = 0;
}
// We don't use ScanAddress here because we're only interested in the
// objects themselves not the addresses in them.
static void PrintProfileCounts(PolyWord *bottom, PolyWord *top)
{
PolyWord *ptr = bottom;
while (ptr < top)
{
ptr++; // Skip the length word
PolyObject *obj = (PolyObject*)ptr;
if (obj->ContainsForwardingPtr())
{
// It's a forwarding pointer - get the length from the new location
while (obj->ContainsForwardingPtr())
obj = obj->GetForwardingPtr();
ASSERT(obj->ContainsNormalLengthWord());
ptr += obj->Length();
}
else
{
ASSERT(obj->ContainsNormalLengthWord());
if (obj->IsCodeObject())
{
PolyWord *firstConstant = obj->ConstPtrForCode();
PolyWord name = firstConstant[0];
// The word before is an untagged count
POLYUNSIGNED count = firstConstant[-1].AsUnsigned();
if (count != 0)
{
if (name != TAGGED(0))
{
PPROFENTRY pEnt = newProfileEntry();
pEnt->count = count;
// This should be a valid string which may be a single character.
ASSERT(name.IsTagged() || name.AsObjPtr()->IsByteObject());
pEnt->functionName = name;
}
firstConstant[-1] = PolyWord::FromUnsigned(0);
P.total += count;
} /* count != 0 */
} /* code object */
ptr += obj->Length();
} /* else */
} /* while */
}
/* This type is coercible to PolyString *. We can use static "pstrings"
since we are not going to garbage collect while we are printing out
the profiling information. */
static struct {
POLYUNSIGNED length; char chars[40];
} psMarkPhase, psCopyPhase, psUpdatePhase, psGCTotal, psUnknown;
static void printprofile(void)
/* Print profiling information and reset profile counts. */
/* Profile counts are also reset by commit so that objects */
/* written to the persistent store always have zero counts. */
{
PPROFENTRY pEnt;
/* flush old user output before printing profile SPF 30/9/94 */
fflush (stdout);
P.total = 0;
P.used = 0;
if (total_count != 0)
{
unsigned j;
for (j = 0; j < gMem.npSpaces; j++)
{
MemSpace *space = gMem.pSpaces[j];
// Permanent areas are filled with objects from the bottom.
PrintProfileCounts(space->bottom, space->top); // Bottom to top
}
for (j = 0; j < gMem.nlSpaces; j++)
{
LocalMemSpace *space = gMem.lSpaces[j];
// Local areas only have objects from the allocation pointer to the top.
PrintProfileCounts(space->pointer, space->top);
}
} // else if we haven't actually had an interrupt avoid expensive scan of memory.
if (gc_count1 || gc_count2 || gc_count3)
{
int gc_count = gc_count1 + gc_count2 + gc_count3;
P.total += gc_count;
total_count += gc_count;
pEnt = newProfileEntry();
strcpy(psMarkPhase.chars, "GARBAGE COLLECTION (mark phase)");
psMarkPhase.length = strlen(psMarkPhase.chars);
pEnt->count = gc_count1;
pEnt->functionName = PolyWord::FromUnsigned((POLYUNSIGNED)&psMarkPhase);
pEnt = newProfileEntry();
strcpy(psCopyPhase.chars, "GARBAGE COLLECTION (copy phase)");
psCopyPhase.length = strlen(psCopyPhase.chars);
pEnt->count = gc_count2;
pEnt->functionName = PolyWord::FromUnsigned((POLYUNSIGNED)&psCopyPhase);
pEnt = newProfileEntry();
strcpy(psUpdatePhase.chars, "GARBAGE COLLECTION (update phase)");
psUpdatePhase.length = strlen(psUpdatePhase.chars);
pEnt->count = gc_count3;
pEnt->functionName = PolyWord::FromUnsigned((POLYUNSIGNED)&psUpdatePhase);
pEnt = newProfileEntry();
strcpy(psGCTotal.chars, "GARBAGE COLLECTION (total)");
psGCTotal.length = strlen(psGCTotal.chars);
pEnt->count = gc_count;
pEnt->functionName = PolyWord::FromUnsigned((POLYUNSIGNED)&psGCTotal);
gc_count1 = 0;
gc_count2 = 0;
gc_count3 = 0;
}
if (unknown_count)
{
total_count += unknown_count;
P.total += unknown_count;
pEnt = newProfileEntry();
strcpy(psUnknown.chars, "UNKNOWN");
psUnknown.length = strlen(psUnknown.chars);
pEnt->count = unknown_count;
pEnt->functionName = PolyWord::FromUnsigned((POLYUNSIGNED)&psUnknown);
unknown_count = 0;
}
writeProfileResults();
if (total_count)
{
printf("\nTotal: %lu; Counted: %lu; Uncounted: %lu",
total_count, P.total, total_count - P.total);
total_count = 0;
putc('\n', stdout);
}
/* flush profile before printing next user output SPF 30/9/94 */
fflush (stdout);
}
void handleProfileTrap(TaskData *taskData, SIGNALCONTEXT *context)
{
/* If we are in the garbage-collector add the count to "gc_count"
otherwise try to find out where we are. */
switch (gc_phase)
{
case 0 :
{
if (taskData)
{
PolyWord *sp;
POLYCODEPTR pc;
if (machineDependent->GetPCandSPFromContext(taskData, context, sp, pc))
add_count(taskData, pc, sp, 1);
else unknown_count++;
}
else unknown_count++;
// On Mac OS X all virtual timer interrupts seem to be directed to the root thread
// so all the counts will be "unknown".
}
break;
case 1 :
gc_count1++;
break;
case 2 :
gc_count2++;
break;
case 3 :
gc_count3++;
break;
default :
unknown_count++;
break;
}
}
class ProfileRequest: public MainThreadRequest
{
public:
ProfileRequest(unsigned prof): mode(prof) {}
virtual void Perform();
unsigned mode;
};
// To speed up testing whether profiling is off already we
// maintain a variable that contains the global state.
static unsigned profile_mode = 0;
static PLock profLock;
// Called from ML to control profiling.
Handle profilerc(TaskData *taskData, Handle mode_handle)
/* Profiler - generates statistical profiles of the code.
The parameter is an integer which determines the value to be profiled.
When profiler is called it always resets the profiling and prints out any
values which have been accumulated.
If the parameter is 0 this is all it does,
if the parameter is 1 then it produces time profiling,
if the parameter is 2 it produces store profiling.
3 - arbitrary precision emulation traps. */
{
unsigned mode = get_C_ulong(taskData, DEREFWORDHANDLE(mode_handle));
{
PLocker locker(&profLock);
if (mode == profile_mode) // No change in mode = no-op
return taskData->saveVec.push(TAGGED(0));
/* profiling 0 turns off profiling and prints results,
but is ignored for profile_modes > 4. To get the
accumulated results of these, set profiling to 4. */
if (mode == 0 && profile_mode > PROF_ACCUMULATED)
return taskData->saveVec.push(TAGGED(0));
profile_mode = mode;
}
// All these actions are performed by the root thread. Only profile
// printing needs to be performed with all the threads stopped but it's
// simpler to serialise all requests.
ProfileRequest request(mode);
processes->MakeRootRequest(taskData, &request);
return taskData->saveVec.push(TAGGED(0));
}
// This is called from the root thread when all the ML threads have been paused.
void ProfileRequest::Perform()
{
switch (mode & ~PROF_ACCUMULATED)
{
case kProfileOff:
// Turn off old profiling mechanism and print out accumulated results
profileMode = kProfileOff;
processes->StopProfiling();
printprofile();
break;
case kProfileTime:
profileMode = kProfileTime;
processes->StartProfiling();
break;
case kProfileStoreAllocation:
profileMode = kProfileStoreAllocation;
break;
case kProfileEmulation:
profileMode = kProfileEmulation;
break;
default: /* do nothing */
break;
}
} /* profilerc */
class Profiling: public RtsModule
{
public:
virtual void Init(void);
};
// Declare this. It will be automatically added to the table.
static Profiling profileModule;
void Profiling::Init(void)
{
// Reset profiling counts.
profileMode = kProfileOff;
gc_count1 = 0;
gc_count2 = 0;
gc_count3 = 0;
total_count = 0;
unknown_count = 0;
}
|