1 /*
2  * Copyright @ 2018 - present 8x8, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package org.jitsi.videobridge.util;
18 
19 import org.jitsi.nlj.util.*;
20 import org.jitsi.utils.logging2.*;
21 import org.json.simple.*;
22 
23 import java.util.concurrent.*;
24 
25 public class TaskPools
26 {
27     private static final Logger classLogger = new LoggerImpl(TaskPools.class.getName());
28     /**
29      * A global executor service which can be used for non-CPU-intensive tasks.
30      */
31     public static final ExecutorService IO_POOL =
32             Executors.newCachedThreadPool(new NameableThreadFactory("Global IO pool"));
33 
34     /**
35      * An executor to be used for CPU-intensive tasks.  NOTE that tasks which block should
36      * NOT use this pool!
37      */
38     public static final ExecutorService CPU_POOL =
39             Executors.newFixedThreadPool(
40                     Runtime.getRuntime().availableProcessors(),
41                     new NameableThreadFactory("Global CPU pool")
42             );
43 
44     public static final ScheduledExecutorService SCHEDULED_POOL =
45             Executors.newSingleThreadScheduledExecutor(new NameableThreadFactory("Global scheduled pool"));
46 
47     @SuppressWarnings("unchecked")
getStatsJson(ExecutorService es)48     public static JSONObject getStatsJson(ExecutorService es)
49     {
50         JSONObject debugState = new JSONObject();
51         debugState.put("executor_class", es.getClass().getSimpleName());
52 
53         if (es instanceof ThreadPoolExecutor)
54         {
55             ThreadPoolExecutor ex = (ThreadPoolExecutor)es;
56             debugState.put("pool_size", ex.getPoolSize());
57             debugState.put("active_task_count", ex.getActiveCount());
58             debugState.put("completed_task_count", ex.getCompletedTaskCount());
59             debugState.put("core_pool_size", ex.getCorePoolSize());
60             debugState.put("maximum_pool_size", ex.getMaximumPoolSize());
61             debugState.put("largest_pool_size", ex.getLargestPoolSize());
62             debugState.put("queue_class", ex.getQueue().getClass().getSimpleName());
63             debugState.put("pending_task_count", ex.getQueue().size());
64         }
65 
66         return debugState;
67     }
68 
69     @SuppressWarnings("unchecked")
getStatsJson()70     public static JSONObject getStatsJson()
71     {
72         JSONObject debugState = new JSONObject();
73 
74         debugState.put("IO_POOL", getStatsJson(IO_POOL));
75         debugState.put("CPU_POOL", getStatsJson(CPU_POOL));
76 
77         return debugState;
78     }
79 
80     static {
81         classLogger.info("TaskPools detected " + Runtime.getRuntime().availableProcessors() +
82                 " processors, creating the CPU pool with that many threads");
83 
84     }
85 }
86