|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.datafusion; |
| 21 | + |
| 22 | +/** |
| 23 | + * Snapshot of the underlying Tokio runtime from {@link SessionContext#runtimeStats()}. Mirrors the |
| 24 | + * subset of {@code tokio_metrics::RuntimeMetrics} fields that node-level dashboards typically |
| 25 | + * surface (worker count, busy time, queue depth, etc.). |
| 26 | + * |
| 27 | + * <p>Instances are immutable. The snapshot is process-wide rather than per-session because the JNI |
| 28 | + * library drives a single shared multi-threaded Tokio runtime; gating on the {@code SessionContext} |
| 29 | + * only ensures the caller still has a live handle. |
| 30 | + * |
| 31 | + * <p>Requires the {@code runtime-metrics} Cargo feature on the native crate, plus {@code |
| 32 | + * RUSTFLAGS="--cfg tokio_unstable"} at build time. |
| 33 | + */ |
| 34 | +public final class RuntimeStats { |
| 35 | + private final int numWorkers; |
| 36 | + private final long liveTasksCount; |
| 37 | + private final long globalQueueDepth; |
| 38 | + private final long elapsedNanos; |
| 39 | + private final long totalBusyNanos; |
| 40 | + private final long totalParkCount; |
| 41 | + private final long totalPollsCount; |
| 42 | + private final long totalNoopCount; |
| 43 | + private final long totalStealCount; |
| 44 | + private final long totalLocalScheduleCount; |
| 45 | + private final long totalOverflowCount; |
| 46 | + |
| 47 | + public RuntimeStats( |
| 48 | + int numWorkers, |
| 49 | + long liveTasksCount, |
| 50 | + long globalQueueDepth, |
| 51 | + long elapsedNanos, |
| 52 | + long totalBusyNanos, |
| 53 | + long totalParkCount, |
| 54 | + long totalPollsCount, |
| 55 | + long totalNoopCount, |
| 56 | + long totalStealCount, |
| 57 | + long totalLocalScheduleCount, |
| 58 | + long totalOverflowCount) { |
| 59 | + this.numWorkers = numWorkers; |
| 60 | + this.liveTasksCount = liveTasksCount; |
| 61 | + this.globalQueueDepth = globalQueueDepth; |
| 62 | + this.elapsedNanos = elapsedNanos; |
| 63 | + this.totalBusyNanos = totalBusyNanos; |
| 64 | + this.totalParkCount = totalParkCount; |
| 65 | + this.totalPollsCount = totalPollsCount; |
| 66 | + this.totalNoopCount = totalNoopCount; |
| 67 | + this.totalStealCount = totalStealCount; |
| 68 | + this.totalLocalScheduleCount = totalLocalScheduleCount; |
| 69 | + this.totalOverflowCount = totalOverflowCount; |
| 70 | + } |
| 71 | + |
| 72 | + /** Number of OS worker threads driving the multi-threaded Tokio runtime. */ |
| 73 | + public int numWorkers() { |
| 74 | + return numWorkers; |
| 75 | + } |
| 76 | + |
| 77 | + /** Number of tasks currently scheduled (alive) on the runtime. */ |
| 78 | + public long liveTasksCount() { |
| 79 | + return liveTasksCount; |
| 80 | + } |
| 81 | + |
| 82 | + /** Tasks waiting in the global injection queue, not yet picked up by a worker. */ |
| 83 | + public long globalQueueDepth() { |
| 84 | + return globalQueueDepth; |
| 85 | + } |
| 86 | + |
| 87 | + /** Wall-clock nanoseconds covered by this snapshot's interval (since runtime start). */ |
| 88 | + public long elapsedNanos() { |
| 89 | + return elapsedNanos; |
| 90 | + } |
| 91 | + |
| 92 | + /** Total nanoseconds workers spent doing work (sum across workers). */ |
| 93 | + public long totalBusyNanos() { |
| 94 | + return totalBusyNanos; |
| 95 | + } |
| 96 | + |
| 97 | + /** Times a worker has parked itself (gone idle waiting for work). */ |
| 98 | + public long totalParkCount() { |
| 99 | + return totalParkCount; |
| 100 | + } |
| 101 | + |
| 102 | + /** Total task polls completed across workers. */ |
| 103 | + public long totalPollsCount() { |
| 104 | + return totalPollsCount; |
| 105 | + } |
| 106 | + |
| 107 | + /** Times a worker unparked but found no work (false wakeup). */ |
| 108 | + public long totalNoopCount() { |
| 109 | + return totalNoopCount; |
| 110 | + } |
| 111 | + |
| 112 | + /** Times a worker successfully stole tasks from another worker. */ |
| 113 | + public long totalStealCount() { |
| 114 | + return totalStealCount; |
| 115 | + } |
| 116 | + |
| 117 | + /** Tasks scheduled into a worker's local queue. */ |
| 118 | + public long totalLocalScheduleCount() { |
| 119 | + return totalLocalScheduleCount; |
| 120 | + } |
| 121 | + |
| 122 | + /** Times a worker's local queue overflowed and pushed tasks to the injector. */ |
| 123 | + public long totalOverflowCount() { |
| 124 | + return totalOverflowCount; |
| 125 | + } |
| 126 | + |
| 127 | + @Override |
| 128 | + public String toString() { |
| 129 | + return "RuntimeStats{numWorkers=" |
| 130 | + + numWorkers |
| 131 | + + ", liveTasksCount=" |
| 132 | + + liveTasksCount |
| 133 | + + ", globalQueueDepth=" |
| 134 | + + globalQueueDepth |
| 135 | + + ", elapsedNanos=" |
| 136 | + + elapsedNanos |
| 137 | + + ", totalBusyNanos=" |
| 138 | + + totalBusyNanos |
| 139 | + + ", totalParkCount=" |
| 140 | + + totalParkCount |
| 141 | + + ", totalPollsCount=" |
| 142 | + + totalPollsCount |
| 143 | + + ", totalNoopCount=" |
| 144 | + + totalNoopCount |
| 145 | + + ", totalStealCount=" |
| 146 | + + totalStealCount |
| 147 | + + ", totalLocalScheduleCount=" |
| 148 | + + totalLocalScheduleCount |
| 149 | + + ", totalOverflowCount=" |
| 150 | + + totalOverflowCount |
| 151 | + + "}"; |
| 152 | + } |
| 153 | +} |
0 commit comments