summaryrefslogtreecommitdiff
path: root/vendor/open-telemetry/sdk/Common/Time
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/open-telemetry/sdk/Common/Time')
-rw-r--r--vendor/open-telemetry/sdk/Common/Time/ClockFactory.php30
-rw-r--r--vendor/open-telemetry/sdk/Common/Time/ClockFactoryInterface.php16
-rw-r--r--vendor/open-telemetry/sdk/Common/Time/ClockInterface.php19
-rw-r--r--vendor/open-telemetry/sdk/Common/Time/StopWatch.php119
-rw-r--r--vendor/open-telemetry/sdk/Common/Time/StopWatchFactory.php44
-rw-r--r--vendor/open-telemetry/sdk/Common/Time/StopWatchFactoryInterface.php18
-rw-r--r--vendor/open-telemetry/sdk/Common/Time/StopWatchInterface.php20
-rw-r--r--vendor/open-telemetry/sdk/Common/Time/SystemClock.php49
-rw-r--r--vendor/open-telemetry/sdk/Common/Time/Util.php32
9 files changed, 347 insertions, 0 deletions
diff --git a/vendor/open-telemetry/sdk/Common/Time/ClockFactory.php b/vendor/open-telemetry/sdk/Common/Time/ClockFactory.php
new file mode 100644
index 000000000..33f4364f6
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Time/ClockFactory.php
@@ -0,0 +1,30 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Time;
+
+final class ClockFactory implements ClockFactoryInterface
+{
+ private static ?ClockInterface $default = null;
+
+ public static function create(): self
+ {
+ return new self();
+ }
+
+ public function build(): ClockInterface
+ {
+ return new SystemClock();
+ }
+
+ public static function getDefault(): ClockInterface
+ {
+ return self::$default ?? self::$default = self::create()->build();
+ }
+
+ public static function setDefault(?ClockInterface $clock): void
+ {
+ self::$default = $clock;
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Common/Time/ClockFactoryInterface.php b/vendor/open-telemetry/sdk/Common/Time/ClockFactoryInterface.php
new file mode 100644
index 000000000..6d9afde91
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Time/ClockFactoryInterface.php
@@ -0,0 +1,16 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Time;
+
+interface ClockFactoryInterface
+{
+ public static function create(): self;
+
+ public function build(): ClockInterface;
+
+ public static function getDefault(): ClockInterface;
+
+ public static function setDefault(?ClockInterface $clock): void;
+}
diff --git a/vendor/open-telemetry/sdk/Common/Time/ClockInterface.php b/vendor/open-telemetry/sdk/Common/Time/ClockInterface.php
new file mode 100644
index 000000000..8f3170185
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Time/ClockInterface.php
@@ -0,0 +1,19 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Time;
+
+interface ClockInterface
+{
+ public const MILLIS_PER_SECOND = 1_000;
+ public const MICROS_PER_SECOND = 1_000_000;
+ public const NANOS_PER_SECOND = 1_000_000_000;
+ public const NANOS_PER_MILLISECOND = 1_000_000;
+ public const NANOS_PER_MICROSECOND = 1_000;
+
+ /**
+ * Returns the current epoch wall-clock timestamp in nanoseconds
+ */
+ public function now(): int;
+}
diff --git a/vendor/open-telemetry/sdk/Common/Time/StopWatch.php b/vendor/open-telemetry/sdk/Common/Time/StopWatch.php
new file mode 100644
index 000000000..b2abdabae
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Time/StopWatch.php
@@ -0,0 +1,119 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Time;
+
+final class StopWatch implements StopWatchInterface
+{
+ private const INITIAL_ELAPSED_TIME = 0;
+
+ private ClockInterface $clock;
+ private bool $running = false;
+ private ?int $initialStartTime;
+ private ?int $startTime = null;
+ private ?int $stopTime = null;
+
+ public function __construct(ClockInterface $clock, ?int $initialStartTime = null)
+ {
+ $this->clock = $clock;
+ $this->initialStartTime = $initialStartTime;
+ }
+
+ public function isRunning(): bool
+ {
+ return $this->running;
+ }
+
+ public function start(): void
+ {
+ // resolve start time as early as possible
+ $startTime = $this->time();
+
+ if ($this->isRunning()) {
+ return;
+ }
+
+ $this->startTime = $startTime;
+ if (!$this->hasBeenStarted()) {
+ $this->initialStartTime = $startTime;
+ }
+ $this->running = true;
+ }
+
+ public function stop(): void
+ {
+ if (!$this->isRunning()) {
+ return;
+ }
+
+ $this->stopTime = $this->time();
+ $this->running = false;
+ }
+
+ public function reset(): void
+ {
+ $this->startTime = $this->initialStartTime = $this->isRunning() ? $this->time() : null;
+ }
+
+ public function getElapsedTime(): int
+ {
+ if (!$this->hasBeenStarted()) {
+ return self::INITIAL_ELAPSED_TIME;
+ }
+
+ return $this->calculateElapsedTime();
+ }
+
+ public function getLastElapsedTime(): int
+ {
+ if (!$this->hasBeenStarted()) {
+ return self::INITIAL_ELAPSED_TIME;
+ }
+
+ return $this->calculateLastElapsedTime();
+ }
+
+ private function time(): int
+ {
+ return $this->clock->now();
+ }
+
+ private function hasBeenStarted(): bool
+ {
+ return $this->initialStartTime !== null;
+ }
+
+ private function calculateElapsedTime(): int
+ {
+ $referenceTime = $this->isRunning()
+ ? $this->time()
+ : $this->getStopTime();
+
+ return $referenceTime - $this->getInitialStartTime();
+ }
+
+ private function calculateLastElapsedTime(): int
+ {
+ $referenceTime = $this->isRunning()
+ ? $this->time()
+ : $this->getStopTime();
+
+ return $referenceTime - $this->getStartTime();
+ }
+
+ private function getInitialStartTime(): ?int
+ {
+ return $this->initialStartTime;
+ }
+
+ private function getStartTime(): ?int
+ {
+ return $this->startTime;
+ }
+
+ private function getStopTime(): ?int
+ {
+ return $this->stopTime;
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Common/Time/StopWatchFactory.php b/vendor/open-telemetry/sdk/Common/Time/StopWatchFactory.php
new file mode 100644
index 000000000..f60c377fc
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Time/StopWatchFactory.php
@@ -0,0 +1,44 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Time;
+
+final class StopWatchFactory implements StopWatchFactoryInterface
+{
+ private static ?StopWatchInterface $default = null;
+
+ private ClockInterface $clock;
+ private ?int $initialStartTime;
+
+ public function __construct(?ClockInterface $clock = null, ?int $initialStartTime = null)
+ {
+ $this->clock = $clock ?? ClockFactory::getDefault();
+ $this->initialStartTime = $initialStartTime;
+ }
+
+ public static function create(?ClockInterface $clock = null, ?int $initialStartTime = null): self
+ {
+ return new self($clock, $initialStartTime);
+ }
+
+ public static function fromClockFactory(ClockFactoryInterface $factory, ?int $initialStartTime = null): self
+ {
+ return self::create($factory->build(), $initialStartTime);
+ }
+
+ public function build(): StopWatch
+ {
+ return new StopWatch($this->clock, $this->initialStartTime);
+ }
+
+ public static function getDefault(): StopWatchInterface
+ {
+ return self::$default ?? self::$default = self::create()->build();
+ }
+
+ public static function setDefault(?StopWatchInterface $default): void
+ {
+ self::$default = $default;
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Common/Time/StopWatchFactoryInterface.php b/vendor/open-telemetry/sdk/Common/Time/StopWatchFactoryInterface.php
new file mode 100644
index 000000000..9750f5769
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Time/StopWatchFactoryInterface.php
@@ -0,0 +1,18 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Time;
+
+interface StopWatchFactoryInterface
+{
+ public static function create(?ClockInterface $clock = null, ?int $initialStartTime = null): self;
+
+ public static function fromClockFactory(ClockFactoryInterface $factory, ?int $initialStartTime = null): self;
+
+ public function build(): StopWatchInterface;
+
+ public static function getDefault(): StopWatchInterface;
+
+ public static function setDefault(?StopWatchInterface $default): void;
+}
diff --git a/vendor/open-telemetry/sdk/Common/Time/StopWatchInterface.php b/vendor/open-telemetry/sdk/Common/Time/StopWatchInterface.php
new file mode 100644
index 000000000..69a03b75e
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Time/StopWatchInterface.php
@@ -0,0 +1,20 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Time;
+
+interface StopWatchInterface
+{
+ public function isRunning(): bool;
+
+ public function start(): void;
+
+ public function stop(): void;
+
+ public function reset(): void;
+
+ public function getElapsedTime(): int;
+
+ public function getLastElapsedTime(): int;
+}
diff --git a/vendor/open-telemetry/sdk/Common/Time/SystemClock.php b/vendor/open-telemetry/sdk/Common/Time/SystemClock.php
new file mode 100644
index 000000000..f57e98490
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Time/SystemClock.php
@@ -0,0 +1,49 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Time;
+
+use function hrtime;
+use function microtime;
+
+final class SystemClock implements ClockInterface
+{
+ private static int $referenceTime = 0;
+
+ public function __construct()
+ {
+ self::init();
+ }
+
+ public static function create(): self
+ {
+ return new self();
+ }
+
+ /** @inheritDoc */
+ public function now(): int
+ {
+ return self::$referenceTime + hrtime(true);
+ }
+
+ private static function init(): void
+ {
+ if (self::$referenceTime > 0) {
+ return;
+ }
+
+ self::$referenceTime = self::calculateReferenceTime(
+ microtime(true),
+ hrtime(true)
+ );
+ }
+
+ /**
+ * Calculates the reference time which is later used to calculate the current wall clock time in nanoseconds by adding the current uptime.
+ */
+ private static function calculateReferenceTime(float $wallClockMicroTime, int $upTime): int
+ {
+ return ((int) ($wallClockMicroTime * ClockInterface::NANOS_PER_SECOND)) - $upTime;
+ }
+}
diff --git a/vendor/open-telemetry/sdk/Common/Time/Util.php b/vendor/open-telemetry/sdk/Common/Time/Util.php
new file mode 100644
index 000000000..e1be1f750
--- /dev/null
+++ b/vendor/open-telemetry/sdk/Common/Time/Util.php
@@ -0,0 +1,32 @@
+<?php
+
+declare(strict_types=1);
+
+namespace OpenTelemetry\SDK\Common\Time;
+
+class Util
+{
+ /** @psalm-pure */
+ public static function nanosToMicros(int $nanoseconds): int
+ {
+ return intdiv($nanoseconds, ClockInterface::NANOS_PER_MICROSECOND);
+ }
+
+ /** @psalm-pure */
+ public static function nanosToMillis(int $nanoseconds): int
+ {
+ return intdiv($nanoseconds, ClockInterface::NANOS_PER_MILLISECOND);
+ }
+
+ /** @psalm-pure */
+ public static function secondsToNanos(int $seconds): int
+ {
+ return $seconds * ClockInterface::NANOS_PER_SECOND;
+ }
+
+ /** @psalm-pure */
+ public static function millisToNanos(int $milliSeconds): int
+ {
+ return $milliSeconds * ClockInterface::NANOS_PER_MILLISECOND;
+ }
+}