Skip to content

Commit 846c4dc

Browse files
authored
perf(core): Read the clock once per performance collection round (#5934)
* perf(core): Read the clock once per performance collection round While a transaction is running, performance data is collected every 100ms. CompositeData.addDataAndCheckTimeout read the clock to check for the 30 second collection timeout once per in-flight transaction, allocating a SentryNanotimeDate each time, even though the enclosing timer task had just read it for the sample it was distributing. Pass that reading in, so a collection round reads the clock once instead of once per transaction. * changelog
1 parent b79be83 commit 846c4dc

2 files changed

Lines changed: 7 additions & 3 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
### Performance
1818

19+
- Read the clock once per performance collection round instead of once per in-flight transaction ([#5934](https://github.com/getsentry/sentry-java/pull/5934))
1920
- Reduce allocations while collecting cpu usage during transactions by reading the process cpu time via `Process.getElapsedCpuTime()` instead of parsing `/proc/self/stat` (33.6kB to 16 bytes per sample on a Pixel 3) ([#5926](https://github.com/getsentry/sentry-java/pull/5926))
2021

2122
### Dependencies

‎sentry/src/main/java/io/sentry/DefaultCompositePerformanceCollector.java‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ public void run() {
124124
// Add the enriched tempData to all transactions/profiles/objects that collect data.
125125
// Then Check if that object timed out.
126126
for (CompositeData data : compositeDataMap.values()) {
127-
if (data.addDataAndCheckTimeout(tempData)) {
127+
if (data.addDataAndCheckTimeout(tempData, tempData.getNanoTimestamp())) {
128128
// timed out
129129
if (data.transaction != null) {
130130
timedOutTransactions.add(data.transaction);
@@ -224,16 +224,19 @@ private CompositeData(final @Nullable ITransaction transaction) {
224224
* Adds the data to the internal list of PerformanceCollectionData. Then it checks if data
225225
* collection timed out (for transactions only).
226226
*
227+
* @param nowNanos the timestamp of the current collection, passed in so a single clock reading
228+
* is shared by every transaction in this collection round.
227229
* @return true if data collection timed out (for transactions only).
228230
*/
229-
boolean addDataAndCheckTimeout(final @NotNull PerformanceCollectionData data) {
231+
boolean addDataAndCheckTimeout(
232+
final @NotNull PerformanceCollectionData data, final long nowNanos) {
230233
// stop() hands dataList out while this timer thread may still be writing to it, so consumers
231234
// synchronize on the list while iterating. We must hold the same monitor here.
232235
synchronized (dataList) {
233236
dataList.add(data);
234237
}
235238
return transaction != null
236-
&& options.getDateProvider().now().nanoTimestamp()
239+
&& nowNanos
237240
> startTimestamp
238241
+ TimeUnit.MILLISECONDS.toNanos(TRANSACTION_COLLECTION_TIMEOUT_MILLIS);
239242
}

0 commit comments

Comments
 (0)