-
Notifications
You must be signed in to change notification settings - Fork 293
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b174715
commit ed007cf
Showing
7 changed files
with
149 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
48 changes: 48 additions & 0 deletions
48
...gent-profiling/profiling-utils/src/main/java/com/datadog/profiling/utils/Timestamper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.datadog.profiling.utils; | ||
|
||
import java.util.concurrent.atomic.AtomicReferenceFieldUpdater; | ||
|
||
public interface Timestamper { | ||
|
||
Timestamper DEFAULT = new Timestamper() {}; | ||
|
||
default long timestamp() { | ||
return System.nanoTime(); | ||
} | ||
|
||
default double toNanosConversionFactor() { | ||
return 1D; | ||
} | ||
|
||
final class Registration { | ||
volatile Timestamper pending = Timestamper.DEFAULT; | ||
private static final AtomicReferenceFieldUpdater<Registration, Timestamper> UPDATER = | ||
AtomicReferenceFieldUpdater.newUpdater(Registration.class, Timestamper.class, "pending"); | ||
|
||
private static final Registration INSTANCE = new Registration(); | ||
} | ||
|
||
/** | ||
* One shot chance to override the timestamper, which allows delayed initialisation of the | ||
* timestamp source. | ||
* | ||
* @return whether override was successful | ||
*/ | ||
static boolean override(Timestamper timestamper) { | ||
return Registration.UPDATER.compareAndSet( | ||
Registration.INSTANCE, Timestamper.DEFAULT, timestamper); | ||
} | ||
|
||
final class Singleton { | ||
// | ||
static final Timestamper TIMESTAMPER = Registration.INSTANCE.pending; | ||
} | ||
|
||
/** | ||
* Gets the registered timestamper (e.g. using JFR) if one has been registered, otherwise uses the | ||
* default timer. | ||
*/ | ||
static Timestamper timestamper() { | ||
return Singleton.TIMESTAMPER; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters