// Shared instrumentation logger for the windowing feature demos (defined by
// the F01 spec, ../../../features/f01-first-pixel.md): one line per event on
// stderr in the format
//
// <monotonic_us> <DEMO> <EVENT_KIND> key=value ...
//
// where <monotonic_us> is microseconds since `initInstrument` was called
// (core.time.MonoTime, so the clock is monotonic and immune to wall-clock
// jumps). Logging goes to stderr so a demo's stdout stays reserved for the
// `SKIP:` capability-gating contract.
module (module) instrumentinstrument;
import (package) corecore.(package) core.stdcstdc.(module) core.stdc.stdargD header file for C99.
pubs.opengroup.org/onlinepubs/009695399/basedefs/stdarg.h.html, stdarg.h
Source
core/stdc/stdarg.d
stdarg : (alias template) instrument.va_end = core.stdc.stdarg.va_end()(va_list ap)End use of ap.
va_end, va_list, (alias template) instrument.va_start = core.stdc.stdarg.va_start(T)(out va_list ap, ref T parmn)Initialize ap.
parmn should be the last named parameter.
va_start;
import (package) corecore.(package) core.stdcstdc.(module) core.stdc.stdioD header file for C99 <stdio.h>
pubs.opengroup.org/onlinepubs/009695399/basedefs/stdio.h.html, stdio.h
Source
core/stdc/stdio.d
stdio : (alias) instrument.fprintf = int core.stdc.stdio.fprintf(shared(core.stdc.stdio._IO_FILE)* stream, scope const(char*) format, scope const ...) nothrow @nogcfprintf, (alias shared global) instrument.stderr = shared(core.stdc.stdio._IO_FILE*) core.stdc.stdio.stderrstderr, (alias) instrument.vfprintf = int core.stdc.stdio.vfprintf(shared(core.stdc.stdio._IO_FILE)* stream, scope const(char*) format, core.internal.vararg.sysv_x64.__va_list_tag* arg) nothrow @nogcvfprintf;
import (package) corecore.(module) core.timeModule containing core time functionality, such as Duration (which
represents a duration of time) or MonoTime (which represents a
timestamp of the system's monotonic clock).
Various functions take a string (or strings) to represent a unit of time
(e.g. convert!("days", "hours")(numDays)). The valid strings to use
with such functions are "years", "months", "weeks", "days", "hours",
"minutes", "seconds", "msecs" (milliseconds), "usecs" (microseconds),
"hnsecs" (hecto-nanoseconds - i.e. 100 ns) or some subset thereof. There
are a few functions that also allow "nsecs", but very little actually
has precision greater than hnsecs.
Symbol Description Types Duration Represents a duration of time of weeks or less (kept internally as hnsecs). (e.g. 22 days or 700 seconds). TickDuration DEPRECATED Represents a duration of time in system clock ticks, using the highest precision that the system provides. MonoTime Represents a monotonic timestamp in system clock ticks, using the highest precision that the system provides. Functions convert Generic way of converting between two time units. dur Allows constructing a Duration from the given time units with the given length. weeks days hours
minutes seconds msecs
usecs hnsecs nsecs |
Convenience aliases for dur. |
| abs | Returns the absolute value of a duration. |
From Duration
From TickDuration
From units
To Duration tickDuration.to, std,conv!Duration() dur!"msecs"(5) or 5.msecs()
| To TickDuration |
duration.to, std,conv!TickDuration() |
|
TickDuration.from!"msecs"(msecs) |
| To units |
duration.total!"days" |
tickDuration.msecs |
convert!("days", "msecs")(msecs) |
Source
core/time.d
time : (struct) core.time.MonoTimeImpl!(ClockType.normal)MonoTime;
private __gshared (struct) core.time.MonoTimeImpl!(ClockType.normal)MonoTime (__gshared global) core.time.MonoTimeImpl!(ClockType.normal) instrument.g_t0g_t0;
private __gshared const(char)* (__gshared global) const(char)* instrument.g_demog_demo = "demo";
/// Names the demo, starts the monotonic clock, and emits `init_start`.
/// Call this first, before any platform API call.
void void instrument.initInstrument(const(char)* demoName) nothrow @nogcNames the demo, starts the monotonic clock, and emits init_start.
Call this first, before any platform API call.
initInstrument(const(char)* (parameter) const(char)* demoNamedemoName) @nogc nothrow
{
(__gshared global) const(char)* instrument.g_demog_demo = (parameter) const(char)* demoNamedemoName;
(__gshared global) core.time.MonoTimeImpl!(ClockType.normal) instrument.g_t0g_t0 = (struct) core.time.MonoTimeImpl!(ClockType.normal)MonoTime.core.time.MonoTimeImpl!(ClockType.normal) core.time.MonoTimeImpl!(ClockType.normal).currTime() nothrow @nogc @property @trustedThe current time of the system's monotonic clock. This has no relation
to the wall clock time, as the wall clock time can be adjusted (e.g.
by NTP), whereas the monotonic clock always moves forward. The source
of the monotonic time is system-specific.
On Windows, QueryPerformanceCounter is used. On Mac OS X,
mach_absolute_time is used, while on other POSIX systems,
clock_gettime is used.
Warning: On some systems, the monotonic clock may stop counting
when the computer goes to sleep or hibernates. So, the
monotonic clock may indicate less time than has actually
passed if that occurs. This is known to happen on
Mac OS X. It has not been tested whether it occurs on
either Windows or Linux.
currTime;
void instrument.emit(scope const(char)* kind) nothrow @nogcEmit an event with no key=value payload.
emit("init_start");
}
/// Microseconds elapsed since `initInstrument`.
long long instrument.nowUs() nothrow @nogcMicroseconds elapsed since initInstrument.
nowUs() @nogc nothrow
{
return ((struct) core.time.MonoTimeImpl!(ClockType.normal)MonoTime.core.time.MonoTimeImpl!(ClockType.normal) core.time.MonoTimeImpl!(ClockType.normal).currTime() nothrow @nogc @property @trustedThe current time of the system's monotonic clock. This has no relation
to the wall clock time, as the wall clock time can be adjusted (e.g.
by NTP), whereas the monotonic clock always moves forward. The source
of the monotonic time is system-specific.
On Windows, QueryPerformanceCounter is used. On Mac OS X,
mach_absolute_time is used, while on other POSIX systems,
clock_gettime is used.
Warning: On some systems, the monotonic clock may stop counting
when the computer goes to sleep or hibernates. So, the
monotonic clock may indicate less time than has actually
passed if that occurs. This is known to happen on
Mac OS X. It has not been tested whether it occurs on
either Windows or Linux.
currTime - core.time.Duration core.time.MonoTimeImpl!(ClockType.normal).opBinary!"-"(core.time.MonoTimeImpl!(ClockType.normal) rhs) const pure nothrow @nogc @safeSubtracting two MonoTimes results in a Duration representing
the amount of time which elapsed between them.
The primary way that programs should time how long something takes is to
do
MonoTime before = MonoTime.currTime;
// do stuff
MonoTime after = MonoTime.currTime;
// How long it took.
Duration timeElapsed = after - before;
or to use a wrapper (such as a stop watch type) which does that.
Warning:
Because Duration is in hnsecs, whereas MonoTime is in system
ticks, it's usually the case that this assertion will fail
auto before = MonoTime.currTime;
// do stuff
auto after = MonoTime.currTime;
auto timeElapsed = after - before;
assert(before + timeElapsed == after);
This is generally fine, and by its very nature, converting from
system ticks to any type of seconds (hnsecs, nsecs, etc.) will
introduce rounding errors, but if code needs to avoid any of the
small rounding errors introduced by conversion, then it needs to use
MonoTime's ticks property and keep all calculations in ticks
rather than using Duration.
g_t0).long core.time.Duration.total!"usecs"() const pure nothrow @nogc @property @safeReturns the total number of the given units in this Duration.
So, unlike split, it does not strip out the larger units.
Examples
assert(dur!"weeks"(12).total!"weeks" == 12);
assert(dur!"weeks"(12).total!"days" == 84);
assert(dur!"days"(13).total!"weeks" == 1);
assert(dur!"days"(13).total!"days" == 13);
assert(dur!"hours"(49).total!"days" == 2);
assert(dur!"hours"(49).total!"hours" == 49);
assert(dur!"nsecs"(2007).total!"hnsecs" == 20);
assert(dur!"nsecs"(2007).total!"nsecs" == 2000);
total!"usecs";
}
/// Emit an event with no key=value payload.
void void instrument.emit(scope const(char)* kind) nothrow @nogcEmit an event with no key=value payload.
emit(scope const(char)* (parameter) const(char)* kindkind) @nogc nothrow
{
int core.stdc.stdio.fprintf(shared(core.stdc.stdio._IO_FILE)* stream, scope const(char*) format, scope const ...) nothrow @nogcfprintf((shared global) shared(core.stdc.stdio._IO_FILE*) core.stdc.stdio.stderrstderr, "%lld %s %s\n", long instrument.nowUs() nothrow @nogcMicroseconds elapsed since initInstrument.
nowUs(), (__gshared global) const(char)* instrument.g_demog_demo, (parameter) const(char)* kindkind);
}
/// Emit an event with a printf-formatted `key=value ...` payload.
extern (C) void void instrument.emitf(scope const(char)* kind, scope const(char)* fmt, ...) nothrow @nogcEmit an event with a printf-formatted key=value ... payload.
emitf(scope const(char)* (parameter) const(char)* kindkind, scope const(char)* (parameter) const(char)* fmtfmt, ...) @nogc nothrow
{
int core.stdc.stdio.fprintf(shared(core.stdc.stdio._IO_FILE)* stream, scope const(char*) format, scope const ...) nothrow @nogcfprintf((shared global) shared(core.stdc.stdio._IO_FILE*) core.stdc.stdio.stderrstderr, "%lld %s %s ", long instrument.nowUs() nothrow @nogcMicroseconds elapsed since initInstrument.
nowUs(), (__gshared global) const(char)* instrument.g_demog_demo, (parameter) const(char)* kindkind);
va_list (local variable) core.internal.vararg.sysv_x64.__va_list_tag* apap;
void core.stdc.stdarg.va_start!(const(char)*)(out core.internal.vararg.sysv_x64.__va_list_tag* ap, ref const(char)* parmn) nothrow @nogcInitialize ap.
parmn should be the last named parameter.
va_start((local variable) core.internal.vararg.sysv_x64.__va_list_tag* apap, (parameter) const(char)* fmtfmt);
int core.stdc.stdio.vfprintf(shared(core.stdc.stdio._IO_FILE)* stream, scope const(char*) format, core.internal.vararg.sysv_x64.__va_list_tag* arg) nothrow @nogcvfprintf((shared global) shared(core.stdc.stdio._IO_FILE*) core.stdc.stdio.stderrstderr, (parameter) const(char)* fmtfmt, (local variable) core.internal.vararg.sysv_x64.__va_list_tag* apap);
void core.stdc.stdarg.va_end!()(core.internal.vararg.sysv_x64.__va_list_tag* ap) pure nothrow @nogc @safeEnd use of ap.
va_end((local variable) core.internal.vararg.sysv_x64.__va_list_tag* apap);
int core.stdc.stdio.fprintf(shared(core.stdc.stdio._IO_FILE)* stream, scope const(char*) format, scope const ...) nothrow @nogcfprintf((shared global) shared(core.stdc.stdio._IO_FILE*) core.stdc.stdio.stderrstderr, "\n");
}