wu-sheng commented on a change in pull request #4220: [WIP] sniffer processing 
profile task and report status and snapshot
URL: https://github.com/apache/skywalking/pull/4220#discussion_r366337976
 
 

 ##########
 File path: 
apm-sniffer/apm-agent-core/src/main/java/org/apache/skywalking/apm/agent/core/profile/ProfileThread.java
 ##########
 @@ -0,0 +1,296 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ */
+
+package org.apache.skywalking.apm.agent.core.profile;
+
+import com.google.common.base.Objects;
+import org.apache.skywalking.apm.agent.core.boot.ServiceManager;
+import org.apache.skywalking.apm.agent.core.conf.Config;
+import org.apache.skywalking.apm.agent.core.context.trace.TraceSegment;
+import org.apache.skywalking.apm.agent.core.logging.api.ILog;
+import org.apache.skywalking.apm.agent.core.logging.api.LogManager;
+
+import java.util.ArrayList;
+import java.util.concurrent.LinkedBlockingQueue;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicInteger;
+
+/**
+ * Profile task process thread, dump segment executing thread stack.
+ *
+ * @author MrPro
+ */
+public class ProfileThread extends Thread {
+
+    private static final ILog logger = 
LogManager.getLogger(ProfileThread.class);
+
+    // per segment max profiling time (millisecond)
+    private static final long MAX_PROFILING_TIME_MILLS = 
TimeUnit.MINUTES.toMillis(Config.Profile.MAX_DURATION);
+
+    // current thread running status
+    private volatile boolean running = true;
+
+    // wait and notify has new profile task
+    private final LinkedBlockingQueue<ProfileTaskExecutionContext> 
executionContextListener = new LinkedBlockingQueue<>();
+
+    private final ProfileTaskExecutionService profileTaskExecutionService;
+    private final ProfileTaskChannelService profileTaskChannelService;
+
+    public ProfileThread() {
+        profileTaskExecutionService = 
ServiceManager.INSTANCE.findService(ProfileTaskExecutionService.class);
+        profileTaskChannelService = 
ServiceManager.INSTANCE.findService(ProfileTaskChannelService.class);
+    }
+
+    @Override
+    public void run() {
+
+        while (running) {
+            // waiting new profile task
+            ProfileTaskExecutionContext taskExecutionContext = null;
+            try {
+                taskExecutionContext = executionContextListener.take();
+            } catch (InterruptedException e) {
+                continue;
+            }
+
+            try {
+                profiling(taskExecutionContext);
+            } catch (InterruptedException e) {
+                // ignore interrupted
+                continue;
+            } catch (Exception e) {
+                logger.error(e, "Profiling task fail. taskId:{}", 
taskExecutionContext.getTask().getTaskId());
+            } finally {
+                // finally stop current profiling task, tell execution service 
task has stop
+                
profileTaskExecutionService.stopCurrentProfileTask(taskExecutionContext);
+            }
+        }
+    }
+
+    /**
+     * notify have new task need to process
+     */
+    void processNewProfileTask(ProfileTaskExecutionContext context) {
+        executionContextListener.add(context);
+    }
+
+    /**
+     * check have available slot to profile and add it
+     * @param segment
+     * @return
+     */
+    public ProfilingSegmentContext checkAndAddSegmentContext(TraceSegment 
segment, ProfileTaskExecutionContext taskExecutionContext) {
+        // check has available slot
+        AtomicInteger currentProfilingCount = 
taskExecutionContext.getCurrentProfilingCount();
+        final int usingSlotCount = currentProfilingCount.get();
+        if (usingSlotCount >= Config.Profile.MAX_PARALLEL) {
+            return null;
+        }
+
+        // try to occupy slot
+        if (!currentProfilingCount.compareAndSet(usingSlotCount, 
usingSlotCount + 1)) {
+            return null;
+        }
+
+        ProfilingSegmentContext[] profilingSegmentSlot = 
taskExecutionContext.getProfilingSegmentSlot();
+        final ProfilingSegmentContext segmentContext = new 
ProfilingSegmentContext(segment, Thread.currentThread(), taskExecutionContext);
+        for (int slot = 0; slot < profilingSegmentSlot.length; slot++) {
+            if (profilingSegmentSlot[slot] == null) {
+                profilingSegmentSlot[slot] = segmentContext;
+                break;
+            }
+        }
+        return segmentContext;
+    }
+
+    /**
+     * find segment and clear on slot
+     *
+     * @param segment
+     */
+    public void stopSegmentProfile(TraceSegment segment) {
+        ProfileTaskExecutionContext currentExecutionContext = 
profileTaskExecutionService.getCurrentTaskExecutionContext();
+        if (currentExecutionContext == null) {
+            return;
+        }
+
+        // find current segment and clear it
+        boolean find = false;
+        ProfilingSegmentContext[] profilingSegmentSlot = 
currentExecutionContext.getProfilingSegmentSlot();
+        for (int slot = 0; slot < profilingSegmentSlot.length; slot++) {
+            ProfilingSegmentContext currentSlotSegment = 
profilingSegmentSlot[slot];
+            if (currentSlotSegment != null && 
Objects.equal(profilingSegmentSlot[slot].getSegment().getTraceSegmentId(), 
segment.getTraceSegmentId())) {
+                profilingSegmentSlot[slot] = null;
+
+                // setting stop running
+                currentSlotSegment.setSegmentIsRunning(false);
 
 Review comment:
   What is `segmentIsRunning=false`?

----------------------------------------------------------------
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:
us...@infra.apache.org


With regards,
Apache Git Services

Reply via email to