C++ optional computational-weight profiling
Computational-weight profiling shall be selected by nonzero PPL_PROFILE_ADD_WEIGHT together with NDEBUG. Assertion builds shall retain ordinary weight instrumentation even when the profiling flag is nonzero; profiling-disabled release builds shall retain 🔗 5. Computational-weight instrumentation outside profiling mode. Profiler implementation definitions and automatic calibration shall be present only in the selected profiling configuration. The profiler declaration shall be included after Weightwatch_Traits is complete, with the time declarations required by its interface.
In profiling mode, WEIGHT_BEGIN() shall call Weight_Profiler::begin(). Each expansion of WEIGHT_ADD(d) or WEIGHT_ADD_MUL(d, f) shall own a function-static profiling point identified by that expansion's file and line. The first execution shall evaluate and capture d as the point's Delta; later executions of that expansion shall reuse it. WEIGHT_ADD shall end a one-iteration sample; WEIGHT_ADD_MUL shall evaluate f once per execution, convert it to unsigned int, and end that many iterations. Expressions and their conversions shall be defined. Deltas intended to vary between calls shall use distinct explicit profiling points instead.
The profiler shall borrow its file-name string through its last output or destruction. Calls using the shared clock boundary, calibration and statistics shall be serialized, including in a thread-safe library build. A successful POSIX calling-thread CPU clock shall be available. Timing calls shall occur in the same thread over each interval. Admitted statistics shall remain representable in their unsigned int sample/iteration counters; timing differences shall remain representable in the clock arithmetic. No numerical-library initialization prerequisite shall be imposed by the profiler itself. The ordinary weight counter shall retain 🔗 1. Computational-weight value and current-state binding.
Optional measurement needs a distinct configuration and a stable per-site weight without changing ordinary instrumentation. Shared clock and stream state require explicit serialization.
Defining C++ headers: globals_defs.hh and Weight_Profiler_defs.hh. Names belong to Parma_Polyhedra_Library unless a macro is shown. Member excerpts apply to Weight_Profiler.
// Selected only for NDEBUG && PPL_PROFILE_ADD_WEIGHT: #define WEIGHT_BEGIN() /* begin the shared timing interval */ #define WEIGHT_ADD(delta) /* end one iteration at this point */ #define WEIGHT_ADD_MUL(delta, factor) /* end factor iterations */
Compile and run enabled release, enabled assertion and disabled release variants. Check absence of clock calls and calibration outside the selected configuration. Exercise two calls at one macro expansion with different delta expressions, single evaluation of factors, and distinct one-iteration sites. Inspect borrowed-string lifetime, shared-state restrictions and counter bounds.
src/globals_defs.hh; src/Weight_Profiler_defs.hh; src/Weight_Profiler.cc; tests/weightprofilerspec1.cc; tests/weightprofilerimpl1.cc; tests/weightprofilertestclock.hh.
begin() shall replace the shared timestamp with the calling thread CPU time. For end(n) with n > 0, the profiler shall add δn to Weightwatch_Traits::weight using that unsigned counter's modular arithmetic, read a new timestamp, and use the preceding shared boundary as the interval start. A nanosecond-field rollover shall retain the elapsed time across the seconds boundary. After subtracting the calibrated overhead a, it shall supply the elapsed nanoseconds e and per-iteration value x = e ⁄ n to 🔗 3. Profiler sample classification and weighted statistics. Timing arithmetic shall use double and its rounding; adjusted times need not be nonnegative.
For end(0), the profiler shall refresh the shared timestamp without changing the weight, sample counts, iteration counts or accumulated statistics. It shall perform no division by zero or statistical arithmetic for that call. The next sample shall start at the refreshed boundary, so an interval with no iterations shall not be attributed to subsequent work. All calls shall satisfy 🔗 1. Optional profiler selection and instrumentation protocol.
Per-iteration time is defined only for a positive iteration count. A zero count adds zero computational weight but must still separate timing intervals.
Defining C++ headers: globals_defs.hh and Weight_Profiler_defs.hh. Names belong to Parma_Polyhedra_Library unless a macro is shown. Member excerpts apply to Weight_Profiler.
class Weight_Profiler { public: static void begin(); void end(unsigned int factor = 1); };
Use a controlled clock to check direct and borrowed-nanosecond differences, multiple seconds, modular weight wrap, positive counts and default count one. Check zero-count calls before and after samples, their clock refresh, unchanged output/weight, and absence of FE_DIVBYZERO and FE_INVALID. Check that a later positive sample excludes the zero-count interval.
src/globals_defs.hh; src/Weight_Profiler_defs.hh; src/Weight_Profiler.cc; tests/weightprofilerspec1.cc; tests/weightprofilerimpl1.cc; tests/weightprofilertestclock.hh.
Construction shall capture the source location, delta and timing thresholds, and initialize both the valid and discarded statistical classes to zero. It shall leave the shared timing boundary and computational weight unchanged. For each positive-count sample from 🔗 2. Profiler clock boundaries and zero-iteration weight accounting, let x = e ⁄ n. The sample shall be discarded exactly when x < m or (M > 0 ∧ x > M), where m and M are the minimum and maximum thresholds. Equality with either active threshold shall be valid; a nonpositive maximum threshold shall disable the upper test.
In the selected class, the sample count shall increase by one and the iteration count N by n. The elapsed-time sum S shall increase by e, and the weighted square sum Q by ex. The first sample shall set both extrema to x; later samples shall retain the minimum and maximum per-iteration values. The other class shall remain unchanged. Classification shall not suppress the computational-weight increment. Statistics shall use double arithmetic with its rounding. The output mean shall be S ⁄ N, and valid-sample population variance Q ⁄ N − (S ⁄ N)2, the iteration-weighted statistics. Floating rounding can make the computed variance negative; no exact-time or nonnegative rounded variance guarantee shall be imposed. 🔗 1. Optional profiler selection and instrumentation protocol shall apply.
Samples can contain different iteration counts. Weighting the first and second moments by iterations preserves their mathematical interpretation; thresholds separate admitted and discarded timing observations.
Defining C++ headers: globals_defs.hh and Weight_Profiler_defs.hh. Names belong to Parma_Polyhedra_Library unless a macro is shown. Member excerpts apply to Weight_Profiler.
class Weight_Profiler { public: Weight_Profiler(const char* file, int line, Weightwatch_Traits::Delta delta, double min_threshold = 0, double max_threshold = 0); void end(unsigned int factor = 1); };
Check independent weighted sums and moments, initial/extreme/interior samples, both threshold equalities, lower and upper rejection, a disabled upper bound, and valid-only/discarded-only/mixed classes. Verify that discarded observations still add weight, and that zero-count calls preserve an existing report. Check finite exactly representable timing fixtures; inspect rounding limits.
src/globals_defs.hh; src/Weight_Profiler_defs.hh; src/Weight_Profiler.cc; tests/weightprofilerspec1.cc; tests/weightprofilerimpl1.cc; tests/weightprofilertestclock.hh.
output_stats() shall write to std::cout without clearing the stored statistics. Its prefix shall identify the borrowed file and line followed by Weight_Profiler. An unvisited point shall report never reached.. A nonempty valid class shall report samples, iterations, per-iteration extrema, mean, variance and square root of variance. A nonempty discarded class shall report thresholds, samples, iterations, extrema and mean. Both classes shall be reported when nonempty, valid first, with a terminating flushed newline. Numerical formatting shall follow the stream's current state. Stream failures shall follow standard stream behavior; destruction shall call this output operation subject to ordinary nonthrowing-destructor rules.
Automatic initialization in the selected configuration shall set the shared adjustment to tune_adjustment()'s return value. That internal calibration shall begin a shared interval, set the adjustment to zero, and perform 1000 one-iteration measurements at a function-static point with zero delta and zero thresholds. It shall return that point's stored valid minimum. Its statistics shall remain available for the point's destruction report. Calibration shall not change the computational weight. Direct subsequent calls shall again zero the adjustment, append 1000 measurements to that same point, and return its retained minimum; they shall not install the returned adjustment themselves. Calls shall satisfy 🔗 1. Optional profiler selection and instrumentation protocol.
Per-point output supplies the observations used to assess computational weights. Startup calibration estimates the measurement infrastructure cost without charging it as algorithmic weight.
Defining C++ headers: globals_defs.hh and Weight_Profiler_defs.hh. Names belong to Parma_Polyhedra_Library unless a macro is shown. Member excerpts apply to Weight_Profiler.
class Weight_Profiler { public: void output_stats(); ~Weight_Profiler(); static double tune_adjustment(); };
Check exact report fields against independently calculated values and check unvisited, valid-only, discarded-only and mixed output. Check repeatable output without reset, borrowed label identity, destructor reporting and newline flush. Use a constant-step clock to check the 1001 startup clock reads, zero calibration weight, adjustment subtraction, and the retained calibration report. Inspect stream exception and destructor behavior and exercise explicit recalibration.
src/globals_defs.hh; src/Weight_Profiler_defs.hh; src/Weight_Profiler.cc; tests/weightprofilerspec1.cc; tests/weightprofilerimpl1.cc; tests/weightprofilertestclock.hh.