1 // Copyright (c) 2011-present, Facebook, Inc.  All rights reserved.
2 //  This source code is licensed under both the GPLv2 (found in the
3 //  COPYING file in the root directory) and Apache 2.0 License
4 //  (found in the LICENSE.Apache file in the root directory).
5 
6 package org.rocksdb;
7 
8 import java.util.Map;
9 
10 public class ThreadStatus {
11   private final long threadId;
12   private final ThreadType threadType;
13   private final String dbName;
14   private final String cfName;
15   private final OperationType operationType;
16   private final long operationElapsedTime; // microseconds
17   private final OperationStage operationStage;
18   private final long  operationProperties[];
19   private final StateType stateType;
20 
21   /**
22    * Invoked from C++ via JNI
23    */
ThreadStatus(final long threadId, final byte threadTypeValue, final String dbName, final String cfName, final byte operationTypeValue, final long operationElapsedTime, final byte operationStageValue, final long[] operationProperties, final byte stateTypeValue)24   private ThreadStatus(final long threadId,
25                        final byte threadTypeValue,
26                        final String dbName,
27                        final String cfName,
28                        final byte operationTypeValue,
29                        final long operationElapsedTime,
30                        final byte operationStageValue,
31                        final long[] operationProperties,
32                        final byte stateTypeValue) {
33     this.threadId = threadId;
34     this.threadType = ThreadType.fromValue(threadTypeValue);
35     this.dbName = dbName;
36     this.cfName = cfName;
37     this.operationType = OperationType.fromValue(operationTypeValue);
38     this.operationElapsedTime = operationElapsedTime;
39     this.operationStage = OperationStage.fromValue(operationStageValue);
40     this.operationProperties = operationProperties;
41     this.stateType = StateType.fromValue(stateTypeValue);
42   }
43 
44   /**
45    * Get the unique ID of the thread.
46    *
47    * @return the thread id
48    */
getThreadId()49   public long getThreadId() {
50     return threadId;
51   }
52 
53   /**
54    * Get the type of the thread.
55    *
56    * @return the type of the thread.
57    */
getThreadType()58   public ThreadType getThreadType() {
59     return threadType;
60   }
61 
62   /**
63    * The name of the DB instance that the thread is currently
64    * involved with.
65    *
66    * @return the name of the db, or null if the thread is not involved
67    *     in any DB operation.
68    */
getDbName()69   /* @Nullable */ public String getDbName() {
70     return dbName;
71   }
72 
73   /**
74    * The name of the Column Family that the thread is currently
75    * involved with.
76    *
77    * @return the name of the db, or null if the thread is not involved
78    *     in any column Family operation.
79    */
getCfName()80   /* @Nullable */ public String getCfName() {
81     return cfName;
82   }
83 
84   /**
85    * Get the operation (high-level action) that the current thread is involved
86    * with.
87    *
88    * @return the operation
89    */
getOperationType()90   public OperationType getOperationType() {
91     return operationType;
92   }
93 
94   /**
95    * Get the elapsed time of the current thread operation in microseconds.
96    *
97    * @return the elapsed time
98    */
getOperationElapsedTime()99   public long getOperationElapsedTime() {
100     return operationElapsedTime;
101   }
102 
103   /**
104    * Get the current stage where the thread is involved in the current
105    * operation.
106    *
107    * @return the current stage of the current operation
108    */
getOperationStage()109   public OperationStage getOperationStage() {
110     return operationStage;
111   }
112 
113   /**
114    * Get the list of properties that describe some details about the current
115    * operation.
116    *
117    * Each field in might have different meanings for different operations.
118    *
119    * @return the properties
120    */
getOperationProperties()121   public long[] getOperationProperties() {
122     return operationProperties;
123   }
124 
125   /**
126    * Get the state (lower-level action) that the current thread is involved
127    * with.
128    *
129    * @return the state
130    */
getStateType()131   public StateType getStateType() {
132     return stateType;
133   }
134 
135   /**
136    * Get the name of the thread type.
137    *
138    * @param threadType the thread type
139    *
140    * @return the name of the thread type.
141    */
getThreadTypeName(final ThreadType threadType)142   public static String getThreadTypeName(final ThreadType threadType) {
143     return getThreadTypeName(threadType.getValue());
144   }
145 
146   /**
147    * Get the name of an operation given its type.
148    *
149    * @param operationType the type of operation.
150    *
151    * @return the name of the operation.
152    */
getOperationName(final OperationType operationType)153   public static String getOperationName(final OperationType operationType) {
154     return getOperationName(operationType.getValue());
155   }
156 
microsToString(final long operationElapsedTime)157   public static String microsToString(final long operationElapsedTime) {
158     return microsToStringNative(operationElapsedTime);
159   }
160 
161   /**
162    * Obtain a human-readable string describing the specified operation stage.
163    *
164    * @param operationStage the stage of the operation.
165    *
166    * @return the description of the operation stage.
167    */
getOperationStageName( final OperationStage operationStage)168   public static String getOperationStageName(
169       final OperationStage operationStage) {
170     return getOperationStageName(operationStage.getValue());
171   }
172 
173   /**
174    * Obtain the name of the "i"th operation property of the
175    * specified operation.
176    *
177    * @param operationType the operation type.
178    * @param i the index of the operation property.
179    *
180    * @return the name of the operation property
181    */
getOperationPropertyName( final OperationType operationType, final int i)182   public static String getOperationPropertyName(
183       final OperationType operationType, final int i) {
184     return getOperationPropertyName(operationType.getValue(), i);
185   }
186 
187   /**
188    * Translate the "i"th property of the specified operation given
189    * a property value.
190    *
191    * @param operationType the operation type.
192    * @param operationProperties the operation properties.
193    *
194    * @return the property values.
195    */
interpretOperationProperties( final OperationType operationType, final long[] operationProperties)196   public static Map<String, Long> interpretOperationProperties(
197       final OperationType operationType, final long[] operationProperties) {
198     return interpretOperationProperties(operationType.getValue(),
199         operationProperties);
200   }
201 
202   /**
203    * Obtain the name of a state given its type.
204    *
205    * @param stateType the state type.
206    *
207    * @return the name of the state.
208    */
getStateName(final StateType stateType)209   public static String getStateName(final StateType stateType) {
210     return getStateName(stateType.getValue());
211   }
212 
getThreadTypeName(final byte threadTypeValue)213   private static native String getThreadTypeName(final byte threadTypeValue);
getOperationName(final byte operationTypeValue)214   private static native String getOperationName(final byte operationTypeValue);
microsToStringNative( final long operationElapsedTime)215   private static native String microsToStringNative(
216       final long operationElapsedTime);
getOperationStageName( final byte operationStageTypeValue)217   private static native String getOperationStageName(
218       final byte operationStageTypeValue);
getOperationPropertyName( final byte operationTypeValue, final int i)219   private static native String getOperationPropertyName(
220       final byte operationTypeValue, final int i);
interpretOperationProperties( final byte operationTypeValue, final long[] operationProperties)221   private static native Map<String, Long>interpretOperationProperties(
222       final byte operationTypeValue, final long[] operationProperties);
getStateName(final byte stateTypeValue)223   private static native String getStateName(final byte stateTypeValue);
224 }
225