kezhenxu94 commented on a change in pull request #4220: sniffer processing 
profile task and report status and snapshot
URL: https://github.com/apache/skywalking/pull/4220#discussion_r368204871
 
 

 ##########
 File path: 
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ProfileTaskExecutionContext.java
 ##########
 @@ -30,20 +38,129 @@
     // task data
     private final ProfileTask task;
 
-    // task real start time
-    private final long startTime;
+    // record current profiling count, use this to check has available profile 
slot
+    private final AtomicInteger currentProfilingCount = new AtomicInteger(0);
+
+    // profiling segment slot
+    private volatile AtomicReferenceArray<ThreadProfiler> 
profilingSegmentSlots;
+
+    // current profiling execution future
+    private volatile Future profilingFuture;
 
-    public ProfileTaskExecutionContext(ProfileTask task, long startTime) {
+    // total started profiling tracing context count
+    private final AtomicInteger totalStartedProfilingCount = new 
AtomicInteger(0);
+
+    public ProfileTaskExecutionContext(ProfileTask task) {
         this.task = task;
-        this.startTime = startTime;
+        profilingSegmentSlots = new 
AtomicReferenceArray<>(Config.Profile.MAX_PARALLEL);
+    }
+
+    /**
+     * start profiling this task
+     *
+     * @param executorService start profiling to appoint thread pool
+     */
+    public void startProfiling(ExecutorService executorService) {
+        profilingFuture = executorService.submit(new ProfileThread(this));
+    }
+
+    /**
+     * stop profiling
+     */
+    public void stopProfiling() {
+        if (profilingFuture != null) {
+            profilingFuture.cancel(true);
+        }
+    }
+
+    /**
+     * check have available slot to profile and add it
+     *
+     * @param tracingContext need to profiling trace
+     * @param traceSegmentId current trace segment id
+     * @param firstSpanOPName first span operation name
+     * @return is add profile success
+     */
+    public boolean attemptProfiling(TracingContext tracingContext, ID 
traceSegmentId, String firstSpanOPName) {
+        // check has available slot
+        final int usingSlotCount = currentProfilingCount.get();
+        if (usingSlotCount >= Config.Profile.MAX_PARALLEL) {
+            return false;
+        }
+
+        // check first operation name matches
+        if (!Objects.equals(task.getFistSpanOPName(), firstSpanOPName)) {
+            return false;
+        }
+
+        // if out limit started profiling count then stop add profiling
+        if (totalStartedProfilingCount.get() > task.getMaxSamplingCount()) {
+            return false;
+        }
+
+        // try to occupy slot
+        if (!currentProfilingCount.compareAndSet(usingSlotCount, 
usingSlotCount + 1)) {
+            return false;
+        }
+
+        final ThreadProfiler threadProfiler = new 
ThreadProfiler(tracingContext, traceSegmentId, Thread.currentThread(), this);
+        int slotLength = profilingSegmentSlots.length();
+        for (int slot = 0; slot < slotLength; slot++) {
+            if (profilingSegmentSlots.compareAndSet(slot, null, 
threadProfiler)) {
+                break;
+            }
+        }
+        return true;
+    }
+
+
+    /**
+     * profiling recheck
+     *
+     * @return is recheck to add profile success
+     */
+    public boolean profilingRecheck(TracingContext tracingContext, ID 
traceSegmentId, String firstSpanOPName) {
+        // if started, keep profiling
+        if (tracingContext.isProfiling()) {
+            return true;
+        }
+
+        return attemptProfiling(tracingContext, traceSegmentId, 
firstSpanOPName);
+    }
+
+    /**
+     * find tracing context and clear on slot
+     *
+     * @param tracingContext stop profiling appoint trace
+     * @return current profiler is already start profiling
 
 Review comment:
   You don't `return` anything at all

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to