| 1 | /* $Id: prfcore.h.h 3609 2007-10-29 01:11:39Z bird $ */
|
|---|
| 2 | /** @file
|
|---|
| 3 | * kProfiler Mark 2 - Core Header Template.
|
|---|
| 4 | */
|
|---|
| 5 |
|
|---|
| 6 | /*
|
|---|
| 7 | * Copyright (c) 2006-2007 knut st. osmundsen <bird-src-spam@anduin.net>
|
|---|
| 8 | *
|
|---|
| 9 | * This file is part of kProfiler.
|
|---|
| 10 | *
|
|---|
| 11 | * kProfiler is free software; you can redistribute it and/or
|
|---|
| 12 | * modify it under the terms of the GNU Lesser General Public
|
|---|
| 13 | * License as published by the Free Software Foundation; either
|
|---|
| 14 | * version 2.1 of the License, or (at your option) any later version.
|
|---|
| 15 | *
|
|---|
| 16 | * kProfiler is distributed in the hope that it will be useful,
|
|---|
| 17 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|---|
| 18 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|---|
| 19 | * Lesser General Public License for more details.
|
|---|
| 20 | *
|
|---|
| 21 | * You should have received a copy of the GNU Lesser General Public
|
|---|
| 22 | * License along with kProfiler; if not, write to the Free Software
|
|---|
| 23 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
|---|
| 24 | *
|
|---|
| 25 | */
|
|---|
| 26 |
|
|---|
| 27 |
|
|---|
| 28 | /** @def KPRF_NAME
|
|---|
| 29 | * Mixed case name macro.
|
|---|
| 30 | */
|
|---|
| 31 | #ifndef KPRF_NAME
|
|---|
| 32 | # define KPRF_NAME(Name) Name
|
|---|
| 33 | #endif
|
|---|
| 34 |
|
|---|
| 35 | /** @def KPRF_TYPE
|
|---|
| 36 | * Upper case type name macro.
|
|---|
| 37 | */
|
|---|
| 38 | #ifndef KPRF_TYPE
|
|---|
| 39 | # define KPRF_TYPE(Prefix,Name) Prefix##Name
|
|---|
| 40 | #endif
|
|---|
| 41 |
|
|---|
| 42 | /** @type KPRF_DECL_FUNC
|
|---|
| 43 | * The calling convention used.
|
|---|
| 44 | */
|
|---|
| 45 | #ifndef KPRF_DECL_FUNC
|
|---|
| 46 | # define KPRF_DECL_FUNC(type, name) type name
|
|---|
| 47 | #endif
|
|---|
| 48 |
|
|---|
| 49 | /** @def KPRF_BITS
|
|---|
| 50 | * The bitsize of the format.
|
|---|
| 51 | */
|
|---|
| 52 | #ifndef KPRF_BITS
|
|---|
| 53 | # define KPRF_BITS 32
|
|---|
| 54 | #endif
|
|---|
| 55 |
|
|---|
| 56 | /** @type UPTR
|
|---|
| 57 | * The basic unsigned interger pointer type.
|
|---|
| 58 | */
|
|---|
| 59 | /** @type IPTR
|
|---|
| 60 | * The basic signed interger pointer type.
|
|---|
| 61 | */
|
|---|
| 62 | #if KPRF_BITS == 16
|
|---|
| 63 | typedef KU16 KPRF_TYPE(,UPTR);
|
|---|
| 64 | typedef KI16 KPRF_TYPE(,IPTR);
|
|---|
| 65 | #elif KPRF_BITS == 32
|
|---|
| 66 | typedef KU32 KPRF_TYPE(,UPTR);
|
|---|
| 67 | typedef KI32 KPRF_TYPE(,IPTR);
|
|---|
| 68 | #elif KPRF_BITS == 64
|
|---|
| 69 | typedef KU64 KPRF_TYPE(,UPTR);
|
|---|
| 70 | typedef KI64 KPRF_TYPE(,IPTR);
|
|---|
| 71 | #else
|
|---|
| 72 | # error "KPRF_BITS has an invalid value. Supported values are 16, 32 and 64."
|
|---|
| 73 | #endif
|
|---|
| 74 | /** @type KPRF_TYPE(P,UPTR)
|
|---|
| 75 | * Pointer to the basic pointer type.
|
|---|
| 76 | */
|
|---|
| 77 | typedef KPRF_TYPE(,UPTR) *KPRF_TYPE(P,UPTR);
|
|---|
| 78 |
|
|---|
| 79 |
|
|---|
| 80 | /**
|
|---|
| 81 | * Various constants.
|
|---|
| 82 | */
|
|---|
| 83 | enum KPRF_TYPE(,CONSTANTS)
|
|---|
| 84 | {
|
|---|
| 85 | /** Magic for the profiler header. (Unix Epoc) */
|
|---|
| 86 | KPRF_TYPE(,HDR_MAGIC) = 0x19700101
|
|---|
| 87 | };
|
|---|
| 88 |
|
|---|
| 89 |
|
|---|
| 90 | /**
|
|---|
| 91 | * The profile data header.
|
|---|
| 92 | */
|
|---|
| 93 | typedef struct KPRF_TYPE(,HDR)
|
|---|
| 94 | {
|
|---|
| 95 | /** [0] The magic number for file data. (KPRF_TYPE(,HDR_MAGIC)) */
|
|---|
| 96 | KU32 u32Magic;
|
|---|
| 97 | /** [4] KPRF_BITS. */
|
|---|
| 98 | KU32 cFormatBits;
|
|---|
| 99 | /** [8] The base address which all pointers should be relative to. */
|
|---|
| 100 | KPRF_TYPE(,UPTR) uBasePtr;
|
|---|
| 101 | #if KPRF_BITS <= 16
|
|---|
| 102 | /** [a] Reserved. */
|
|---|
| 103 | KU16 u16Reserved;
|
|---|
| 104 | #endif
|
|---|
| 105 | #if KPRF_BITS <= 32
|
|---|
| 106 | /** [c] Reserved. */
|
|---|
| 107 | KU32 u32Reserved;
|
|---|
| 108 | #endif
|
|---|
| 109 | /** [10] The size of this data set. */
|
|---|
| 110 | KU32 cb;
|
|---|
| 111 | /** [10] The allocated data set size. */
|
|---|
| 112 | KU32 cbAllocated;
|
|---|
| 113 |
|
|---|
| 114 | /** [18] The max number of functions the function table can hold. */
|
|---|
| 115 | KU32 cMaxFunctions;
|
|---|
| 116 | /** [1c] The current number of functions in the function table. */
|
|---|
| 117 | KU32 cFunctions;
|
|---|
| 118 | /** [20] The offset of the function table (relative to this header). */
|
|---|
| 119 | KU32 offFunctions;
|
|---|
| 120 | /** [24] The size of a function entry. */
|
|---|
| 121 | KU32 cbFunction;
|
|---|
| 122 |
|
|---|
| 123 | /** [28] The max number of bytes the module segments can occupy. */
|
|---|
| 124 | KU32 cbMaxModSegs;
|
|---|
| 125 | /** [2c] The current size of the module segment records. */
|
|---|
| 126 | KU32 cbModSegs;
|
|---|
| 127 | /** [30] The offset of the module segment records (relative to this header). */
|
|---|
| 128 | KU32 offModSegs;
|
|---|
| 129 |
|
|---|
| 130 | /** [34] The max number of threads the thread table can contain. */
|
|---|
| 131 | KU32 cMaxThreads;
|
|---|
| 132 | /** [38] The current number of threads in the thread table. */
|
|---|
| 133 | KU32 cThreads;
|
|---|
| 134 | /** [3c] The offset of the thread table (relative to this header). */
|
|---|
| 135 | KU32 offThreads;
|
|---|
| 136 | /** [40] The size of a thread entry. */
|
|---|
| 137 | KU32 cbThread;
|
|---|
| 138 |
|
|---|
| 139 | /** [44] The max number of stacks the stack table can contain. */
|
|---|
| 140 | KU32 cMaxStacks;
|
|---|
| 141 | /** [48] The max number of stacks.
|
|---|
| 142 | * Unlike the other members, the stacks can be reused. It follows that
|
|---|
| 143 | * this count doesn't specify the number of used slots from the start. */
|
|---|
| 144 | KU32 cStacks;
|
|---|
| 145 | /** [4c] The offset of the thread table (relative to this header).
|
|---|
| 146 | * This is usually 0 in a stored data set. */
|
|---|
| 147 | KU32 offStacks;
|
|---|
| 148 | /** [50] The size of a stack. */
|
|---|
| 149 | KU32 cbStack;
|
|---|
| 150 | /** [54] The maxium stack depth. */
|
|---|
| 151 | KU32 cMaxStackFrames;
|
|---|
| 152 |
|
|---|
| 153 | /** [58] The process commandline.
|
|---|
| 154 | * Might not always apply is will be 0 in those cases. This is normally written
|
|---|
| 155 | * where the stacks used to be.
|
|---|
| 156 | */
|
|---|
| 157 | KU32 offCommandLine;
|
|---|
| 158 | /** [5c] The length of the command line. (excludes the terminator). */
|
|---|
| 159 | KU32 cchCommandLine;
|
|---|
| 160 |
|
|---|
| 161 | /** [60] The function lookup table (it contains indexes).
|
|---|
| 162 | * This is sorted by address so that a binary search can be performed.
|
|---|
| 163 | * Access to this table is managed externally, but generally a read/write lock is employed. */
|
|---|
| 164 | KU32 aiFunctions[1];
|
|---|
| 165 | } KPRF_TYPE(,HDR);
|
|---|
| 166 | /** Pointer to a profiler data header. */
|
|---|
| 167 | typedef KPRF_TYPE(,HDR) *KPRF_TYPE(P,HDR);
|
|---|
| 168 | /** Pointer to a const profiler data header. */
|
|---|
| 169 | typedef const KPRF_TYPE(,HDR) *KPRF_TYPE(PC,HDR);
|
|---|
| 170 |
|
|---|
| 171 |
|
|---|
| 172 | /**
|
|---|
| 173 | * Time statistics.
|
|---|
| 174 | */
|
|---|
| 175 | typedef struct KPRF_TYPE(,TIMESTAT) /** @todo bad names and descriptions! */
|
|---|
| 176 | {
|
|---|
| 177 | /** The minimum period */
|
|---|
| 178 | KU64 volatile MinTicks;
|
|---|
| 179 | /** The maximum period */
|
|---|
| 180 | KU64 volatile MaxTicks;
|
|---|
| 181 | /** The sum of all periods. */
|
|---|
| 182 | KU64 volatile SumTicks;
|
|---|
| 183 | } KPRF_TYPE(,TIMESTAT);
|
|---|
| 184 | /** Pointer to time statistics. */
|
|---|
| 185 | typedef KPRF_TYPE(,TIMESTAT) *KPRF_TYPE(P,TIMESTAT);
|
|---|
| 186 | /** Pointer to const time statistics. */
|
|---|
| 187 | typedef const KPRF_TYPE(,TIMESTAT) *KPRF_TYPE(PC,TIMESTAT);
|
|---|
| 188 |
|
|---|
| 189 |
|
|---|
| 190 | /**
|
|---|
| 191 | * A Module Segment.
|
|---|
| 192 | */
|
|---|
| 193 | typedef struct KPRF_TYPE(,MODSEG)
|
|---|
| 194 | {
|
|---|
| 195 | /** The address of the segment. (relative address) */
|
|---|
| 196 | KPRF_TYPE(,UPTR) uBasePtr;
|
|---|
| 197 | /** The size of the segment minus one (so the entire address space can be covered). */
|
|---|
| 198 | KPRF_TYPE(,UPTR) cbSegmentMinusOne;
|
|---|
| 199 | /** The segment number. (0 based) */
|
|---|
| 200 | KU32 iSegment;
|
|---|
| 201 | /** Flag indicating whether this segment is loaded or not.
|
|---|
| 202 | * (A 16-bit value was choosen out of convenience, all that's stored is 0 or 1 anyway.) */
|
|---|
| 203 | KU16 fLoaded;
|
|---|
| 204 | /** The length of the path.
|
|---|
| 205 | * This is used to calculate the length of the record: offsetof(MODSEG, szPath) + cchPath + 1 */
|
|---|
| 206 | KU16 cchPath;
|
|---|
| 207 | /** The module name. */
|
|---|
| 208 | char szPath[1];
|
|---|
| 209 | } KPRF_TYPE(,MODSEG);
|
|---|
| 210 | /** Pointer to a module segment. */
|
|---|
| 211 | typedef KPRF_TYPE(,MODSEG) *KPRF_TYPE(P,MODSEG);
|
|---|
| 212 | /** Pointer to a const module segment. */
|
|---|
| 213 | typedef const KPRF_TYPE(,MODSEG) *KPRF_TYPE(PC,MODSEG);
|
|---|
| 214 |
|
|---|
| 215 |
|
|---|
| 216 | /**
|
|---|
| 217 | * The profiler data for a function.
|
|---|
| 218 | */
|
|---|
| 219 | typedef struct KPRF_TYPE(,FUNC)
|
|---|
| 220 | {
|
|---|
| 221 | /** The entry address of the function. (relative address)
|
|---|
| 222 | * This is the return address of the entry hook (_mcount, _penter, _ProfileHook32, ...). */
|
|---|
| 223 | KPRF_TYPE(,UPTR) uEntryPtr;
|
|---|
| 224 | /** Offset (relative to the profiler header) of the module segment to which this function belongs. */
|
|---|
| 225 | KU32 offModSeg;
|
|---|
| 226 |
|
|---|
| 227 | /** The number times on the stack. */
|
|---|
| 228 | KU64 volatile cOnStack;
|
|---|
| 229 | /** The number of calls made from this function. */
|
|---|
| 230 | KU64 volatile cCalls;
|
|---|
| 231 |
|
|---|
| 232 | /** Time on stack. */
|
|---|
| 233 | KPRF_TYPE(,TIMESTAT) OnStack;
|
|---|
| 234 | /** Time on top of the stack, i.e. executing. */
|
|---|
| 235 | KPRF_TYPE(,TIMESTAT) OnTopOfStack;
|
|---|
| 236 |
|
|---|
| 237 | /** @todo recursion */
|
|---|
| 238 |
|
|---|
| 239 | } KPRF_TYPE(,FUNC);
|
|---|
| 240 | /** Pointer to the profiler data for a function. */
|
|---|
| 241 | typedef KPRF_TYPE(,FUNC) *KPRF_TYPE(P,FUNC);
|
|---|
| 242 | /** Pointer to the const profiler data for a function. */
|
|---|
| 243 | typedef const KPRF_TYPE(,FUNC) *KPRF_TYPE(PC,FUNC);
|
|---|
| 244 |
|
|---|
| 245 |
|
|---|
| 246 | /**
|
|---|
| 247 | * Stack frame.
|
|---|
| 248 | */
|
|---|
| 249 | typedef struct KPRF_TYPE(,FRAME)
|
|---|
| 250 | {
|
|---|
| 251 | /** The accumulated overhead.
|
|---|
| 252 | * Over head is accumulated by the parent frame when a child is poped off the stack. */
|
|---|
| 253 | KU64 OverheadTicks;
|
|---|
| 254 | /** The current (top of stack) overhead. */
|
|---|
| 255 | KU64 CurOverheadTicks;
|
|---|
| 256 | /** The accumulated sleep ticks.
|
|---|
| 257 | * It's possible to notify the profiler that the thread is being put into a wait/sleep/yield
|
|---|
| 258 | * state. The time spent sleeping is transfered to the parent frame when poping of a child one. */
|
|---|
| 259 | KU64 SleepTicks;
|
|---|
| 260 | /** The start of the on-stack period. */
|
|---|
| 261 | KU64 OnStackStart;
|
|---|
| 262 | /** The accumulated time on top (excludes overhead (sleep doesn't apply here obviously)). */
|
|---|
| 263 | KU64 OnTopOfStackTicks;
|
|---|
| 264 | /** The start of the current on-top-of-stack period.
|
|---|
| 265 | * This is also to mark the start of a sleeping period, the ResumeThread function will always
|
|---|
| 266 | * treat it as the start of the suspend period. */
|
|---|
| 267 | KU64 OnTopOfStackStart;
|
|---|
| 268 | /** The number of calls made from this stack frame. */
|
|---|
| 269 | KU64 cCalls;
|
|---|
| 270 | /** Stack address of this frame.
|
|---|
| 271 | * This is used to detect throw and longjmp, and is also used to deal with overflow. (relative address) */
|
|---|
| 272 | KPRF_TYPE(,UPTR) uFramePtr;
|
|---|
| 273 | /** Offset (relative to the profiler header) to the function record.
|
|---|
| 274 | * This is 0 if we're out of function space. */
|
|---|
| 275 | KU32 offFunction;
|
|---|
| 276 | } KPRF_TYPE(,FRAME);
|
|---|
| 277 | /** Pointer to a stack frame. */
|
|---|
| 278 | typedef KPRF_TYPE(,FRAME) *KPRF_TYPE(P,FRAME);
|
|---|
| 279 | /** Pointer to a const stack frame. */
|
|---|
| 280 | typedef const KPRF_TYPE(,FRAME) *KPRF_TYPE(PC,FRAME);
|
|---|
| 281 |
|
|---|
| 282 |
|
|---|
| 283 | /**
|
|---|
| 284 | * Stack.
|
|---|
| 285 | */
|
|---|
| 286 | typedef struct KPRF_TYPE(,STACK)
|
|---|
| 287 | {
|
|---|
| 288 | /** The offset (relative to the profiler header) of the thread owning the stack.
|
|---|
| 289 | * This is zero if not in use, and non-zero if in use. */
|
|---|
| 290 | KU32 offThread;
|
|---|
| 291 | /** The number of active stack frames. */
|
|---|
| 292 | KU32 cFrames;
|
|---|
| 293 | /** The stack frames.
|
|---|
| 294 | * The actual size of this array is specified in the header. */
|
|---|
| 295 | KPRF_TYPE(,FRAME) aFrames[1];
|
|---|
| 296 | } KPRF_TYPE(,STACK);
|
|---|
| 297 | /** Pointer to a stack. */
|
|---|
| 298 | typedef KPRF_TYPE(,STACK) *KPRF_TYPE(P,STACK);
|
|---|
| 299 | /** Pointer to a const stack. */
|
|---|
| 300 | typedef const KPRF_TYPE(,STACK) *KPRF_TYPE(PC,STACK);
|
|---|
| 301 |
|
|---|
| 302 |
|
|---|
| 303 | /**
|
|---|
| 304 | * The thread state.
|
|---|
| 305 | */
|
|---|
| 306 | typedef enum KPRF_TYPE(,THREADSTATE)
|
|---|
| 307 | {
|
|---|
| 308 | /** The thread hasn't been used yet. */
|
|---|
| 309 | KPRF_TYPE(,THREADSTATE_UNUSED) = 0,
|
|---|
| 310 | /** The thread is activly being profiled.
|
|---|
| 311 | * A thread is added in the suspended state and then activated when
|
|---|
| 312 | * starting to execute the first function.
|
|---|
| 313 | */
|
|---|
| 314 | KPRF_TYPE(,THREADSTATE_ACTIVE),
|
|---|
| 315 | /** The thread is currently suspended from profiling.
|
|---|
| 316 | * Upon entering profiler code the thread is suspended, it's reactivated
|
|---|
| 317 | * upon normal return.
|
|---|
| 318 | */
|
|---|
| 319 | KPRF_TYPE(,THREADSTATE_SUSPENDED),
|
|---|
| 320 | /** The thread is currently suspended due of stack overflow.
|
|---|
| 321 | * When we overflow the stack frame array, the thread enter the overflow state. In this
|
|---|
| 322 | * state nothing is profiled but we keep looking for the exit of the top frame. */
|
|---|
| 323 | KPRF_TYPE(,THREADSTATE_OVERFLOWED),
|
|---|
| 324 | /** The thread is terminated.
|
|---|
| 325 | * When we received a thread termination notification the thread is unwinded, statistics
|
|---|
| 326 | * updated and the state changed to terminated. A terminated thread cannot be revivied. */
|
|---|
| 327 | KPRF_TYPE(,THREADSTATE_TERMINATED),
|
|---|
| 328 |
|
|---|
| 329 | /** Ensure 32-bit size. */
|
|---|
| 330 | KPRF_TYPE(,THREADSTATE_32BIT_HACK) = 0x7fffffff
|
|---|
| 331 | } KPRF_TYPE(,THREADSTATE);
|
|---|
| 332 |
|
|---|
| 333 |
|
|---|
| 334 | /**
|
|---|
| 335 | * Thread statistics and stack.
|
|---|
| 336 | */
|
|---|
| 337 | typedef struct KPRF_TYPE(,THREAD)
|
|---|
| 338 | {
|
|---|
| 339 | /** The native thread id. */
|
|---|
| 340 | KU64 ThreadId;
|
|---|
| 341 | /** The thread name. (optional) */
|
|---|
| 342 | char szName[32];
|
|---|
| 343 | /** The thread current thread state. */
|
|---|
| 344 | KPRF_TYPE(,THREADSTATE) enmState;
|
|---|
| 345 | /** Alignment. */
|
|---|
| 346 | KPRF_TYPE(,THREADSTATE) Reserved0;
|
|---|
| 347 | /** The base pointer of the thread stack. (relative address) */
|
|---|
| 348 | KPRF_TYPE(,UPTR) uStackBasePtr;
|
|---|
| 349 | /** The maximum depth of the thread stack (bytes). */
|
|---|
| 350 | KPRF_TYPE(,UPTR) cbMaxStack;
|
|---|
| 351 | /** The number of calls done by this thread. */
|
|---|
| 352 | KU64 cCalls;
|
|---|
| 353 | /** The number of times the stack overflowed. */
|
|---|
| 354 | KU64 cOverflows;
|
|---|
| 355 | /** The number of times stack entries has been rejected because of a stack switch. */
|
|---|
| 356 | KU64 cStackSwitchRejects;
|
|---|
| 357 | /** The number of times the stack has been unwinded more than one frame. */
|
|---|
| 358 | KU64 cUnwinds;
|
|---|
| 359 |
|
|---|
| 360 | /** The profiled ticks. (This does not include sleep or overhead ticks.)
|
|---|
| 361 | * This is the accumulated on-stack values for the final stack frames. */
|
|---|
| 362 | KU64 ProfiledTicks;
|
|---|
| 363 | /** The accumulated overhead of this thread. */
|
|---|
| 364 | KU64 OverheadTicks;
|
|---|
| 365 | /** The accumulated sleep ticks for this thread.
|
|---|
| 366 | * See KPRF_TYPE(,FRAME)::SleepTicks for details. */
|
|---|
| 367 | KU64 SleepTicks;
|
|---|
| 368 |
|
|---|
| 369 | /** The offset of the stack. */
|
|---|
| 370 | KU32 offStack;
|
|---|
| 371 | } KPRF_TYPE(,THREAD);
|
|---|
| 372 | /** Pointer to a thread. */
|
|---|
| 373 | typedef KPRF_TYPE(,THREAD) *KPRF_TYPE(P,THREAD);
|
|---|
| 374 | /** Pointer to a const thread. */
|
|---|
| 375 | typedef const KPRF_TYPE(,THREAD) *KPRF_TYPE(PC,THREAD);
|
|---|
| 376 |
|
|---|
| 377 |
|
|---|