Skip to content

Commit fd30ae6

Browse files
committed
Upgrade to version 2.3.2.
- Also simplify complicated functions. Signed-off-by: Mulugeta Mammo <mulugeta.mammo@intel.com>
1 parent 556e1fb commit fd30ae6

File tree

1 file changed

+97
-63
lines changed

1 file changed

+97
-63
lines changed

src/main/jni/com_intel_qat_InternalJNI.c

Lines changed: 97 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,11 @@ static void call_qzstd_once(void) {
8888
* Represents a unique QAT session for specific compression parameters.
8989
* This structure is used to manage a QAT session with associated metadata.
9090
*/
91-
typedef struct QzWrapper_T {
91+
typedef struct QzSessionHandle_T {
9292
int32_t qz_key; /**< Unique identifier for session parameters */
9393
int32_t reference_count; /**< Number of active references to this session */
9494
QzSession_T *qz_session; /**< Pointer to the QAT session object */
95-
} QzWrapper_T;
95+
} QzSessionHandle_T;
9696

9797
/**
9898
* Defines the maximum number of unique QAT sessions allowed per thread.
@@ -109,9 +109,9 @@ typedef struct QzWrapper_T {
109109
* Thread-local cache of QAT session objects, indexed by unique session keys.
110110
* Stores up to MAX_SESSIONS_PER_THREAD active sessions per thread to optimize
111111
* session reuse for distinct compression configurations. Each session holds a
112-
* QzWrapper_T struct with a QAT session handle and metadata.
112+
* QzSessionHandle_T struct with a QAT session handle and metadata.
113113
*/
114-
static _Thread_local QzWrapper_T g_session_cache[MAX_SESSIONS_PER_THREAD];
114+
static _Thread_local QzSessionHandle_T g_session_cache[MAX_SESSIONS_PER_THREAD];
115115

116116
/**
117117
* Thread-local counter tracking the number of active QAT sessions in
@@ -135,12 +135,12 @@ static _Thread_local int g_session_counter;
135135
* internally).
136136
* @return A 32-bit key uniquely representing the session parameters.
137137
*/
138-
static int32_t generate_key_for_session(int32_t algorithm,
139-
int32_t level,
140-
int32_t sw_backup,
141-
int32_t polling_mode,
142-
int32_t data_format,
143-
int32_t hw_buff_sz) {
138+
static int32_t gen_session_key(int32_t algorithm,
139+
int32_t level,
140+
int32_t sw_backup,
141+
int32_t polling_mode,
142+
int32_t data_format,
143+
int32_t hw_buff_sz) {
144144
int32_t qz_key = 0;
145145

146146
// Bit-field allocation: 4 bits each for algorithm, level, sw_backup,
@@ -253,48 +253,33 @@ static int setup_lz4_session(QzSession_T *qz_session,
253253
}
254254

255255
/**
256-
* Retrieves a cached QAT session matching the specified key from the
257-
* thread-local session cache. Searches the g_session_cache array up to
258-
* g_session_cache_counter for a session with a matching key. Returns a pointer
259-
* to the found QzWrapper_T or NULL if no match is found.
256+
* Creates and initializes a new QAT session based on the provided
257+
* configuration key. The session is stored in a global cache, and the function
258+
* handles initialization and setup for either DEFLATE or LZ4 compression
259+
* algorithms, depending on the configuration.
260260
*
261-
* @param qz_key The unique session key generated from compression parameters (e.g., algorithm, level).
262-
* @return A pointer to the matching QzWrapper_T in g_session_cache, or NULL if no session matches.
263-
*/
264-
static QzWrapper_T *get_session(int32_t qz_key) {
265-
for (int i = 0; i < g_session_counter; ++i) {
266-
if (g_session_cache[i].qz_session && g_session_cache[i].qz_key == qz_key) {
267-
return &g_session_cache[i];
268-
}
269-
}
270-
return NULL;
271-
}
272-
273-
/*
274-
* Retrieves or creates a session based on the provided key.
275-
* Iterates through the global session cache to find an existing session
276-
* matching the key. If found, returns the cached session. Otherwise, creates a
277-
* new session if the cache limit (MAX_SESSIONS_PER_THREAD) is not exceeded.
261+
* Parameters:
262+
* env - JNI environment pointer for Java interaction.
263+
* qz_key - Configuration key encoding the following:
264+
* - Compression algorithm (bits 0-3)
265+
* - Compression level (bits 4-7)
266+
* - Software backup flag (bit 8)
267+
* - Polling mode (bits 9-12)
268+
* - Data format (bits 13-16)
269+
* - Hardware buffer size (bits 17-29, shifted left by 10)
278270
*
279-
* Extracts session parameters (compression algorithm, level, software backup,
280-
* polling mode, data format, and hardware buffer size) from the key using bit
281-
* manipulation. Initializes a QAT session and sets up the session based on the
282-
* compression algorithm.
271+
* Returns:
272+
* A pointer to the initialized QzSessionHandle_T session from the global
273+
* cache, or NULL if an error occurs (e.g., session limit reached,
274+
* initialization failure, or setup failure).
283275
*
284-
* Throws Java exceptions for errors such as exceeding session limits, QAT
285-
* initialization failures, or session setup failures.
286-
*
287-
* Increments the session's reference count upon successful creation.
288-
* Returns a pointer to the session or NULL on failure.
276+
* Throws:
277+
* - java.lang.RuntimeException: If the number of sessions exceeds the
278+
* thread's limit (MAX_SESSIONS_PER_THREAD).
279+
* - java.lang.IllegalStateException: If QAT initialization or session setup
280+
* fails.
289281
*/
290-
static QzWrapper_T *get_or_create_session(JNIEnv *env, int32_t qz_key) {
291-
for (int i = 0; i < g_session_counter; ++i) {
292-
if (g_session_cache[i].qz_session && g_session_cache[i].qz_key == qz_key) {
293-
return &g_session_cache[i];
294-
}
295-
}
296-
297-
// No cached session for the key in this thread. Create one now!
282+
static QzSessionHandle_T *create_session(JNIEnv *env, int32_t qz_key) {
298283
if (g_session_counter == MAX_SESSIONS_PER_THREAD) {
299284
(*env)->ThrowNew(env, (*env)->FindClass(env, "java/lang/RuntimeException"),
300285
"Number of sessions exceeded the limit for this thread");
@@ -308,7 +293,7 @@ static QzWrapper_T *get_or_create_session(JNIEnv *env, int32_t qz_key) {
308293
int data_format = (qz_key >> 13) & 0xF;
309294
int hw_buff_sz = ((qz_key >> 17) & 0xFFF) << 10;
310295

311-
QzWrapper_T *sess_ptr = &g_session_cache[g_session_counter++];
296+
QzSessionHandle_T *sess_ptr = &g_session_cache[g_session_counter++];
312297
sess_ptr->qz_key = qz_key;
313298
sess_ptr->qz_session = (QzSession_T *)calloc(1, sizeof(QzSession_T));
314299

@@ -350,6 +335,47 @@ static QzWrapper_T *get_or_create_session(JNIEnv *env, int32_t qz_key) {
350335
return sess_ptr;
351336
}
352337

338+
/**
339+
* Retrieves a cached QAT session matching the specified key from the
340+
* thread-local session cache. Searches the g_session_cache array up to
341+
* g_session_cache_counter for a session with a matching key. Returns a pointer
342+
* to the found QzSessionHandle_T or NULL if no match is found.
343+
*
344+
* @param qz_key The unique session key generated from compression parameters
345+
* (e.g., algorithm, level).
346+
* @return A pointer to the matching QzSessionHandle_T in
347+
* g_session_cache, or NULL if no session matches.
348+
*/
349+
static QzSessionHandle_T *get_session(int32_t qz_key) {
350+
for (int i = 0; i < g_session_counter; ++i) {
351+
if (g_session_cache[i].qz_session && g_session_cache[i].qz_key == qz_key) {
352+
return &g_session_cache[i];
353+
}
354+
}
355+
return NULL;
356+
}
357+
358+
/*
359+
* Retrieves or creates a session based on the provided key.
360+
* Iterates through the global thread-local session cache to find an existing
361+
* session matching the key. If found, returns the cached session. Otherwise,
362+
* creates a a new session.
363+
*
364+
* Returns a pointer to the session or NULL on failure.
365+
*/
366+
static QzSessionHandle_T *get_or_create_session(JNIEnv *env, int32_t qz_key) {
367+
QzSessionHandle_T *sess_ptr = get_session(qz_key);
368+
if (sess_ptr) {
369+
return sess_ptr;
370+
}
371+
372+
// Create a new session and update the reference count
373+
sess_ptr = create_session(env, qz_key);
374+
sess_ptr->reference_count++;
375+
376+
return sess_ptr;
377+
}
378+
353379
/**
354380
* Compresses a buffer pointed to by the given source pointer and writes it to
355381
* the destination buffer pointed to by the destination pointer. The read and
@@ -362,8 +388,10 @@ static QzWrapper_T *get_or_create_session(JNIEnv *env, int32_t qz_key) {
362388
* @param src_len The size of the source buffer.
363389
* @param dst_ptr The destination buffer.
364390
* @param dst_len The size of the destination buffer.
365-
* @param bytes_read An out parameter that stores the bytes read from the source buffer.
366-
* @param bytes_written An out parameter that stores the bytes written to the destination buffer.
391+
* @param bytes_read An out parameter that stores the bytes read from the
392+
* source buffer.
393+
* @param bytes_written An out parameter that stores the bytes written to the
394+
* destination buffer.
367395
* @param retry_count The number of compression retries before we give up.
368396
* @return QZ_OK (0) if successful, non-zero otherwise.
369397
*/
@@ -470,7 +498,8 @@ JNIEXPORT void JNICALL Java_com_intel_qat_InternalJNI_initFieldIDs(JNIEnv *env,
470498
(*env)->GetFieldID(env, byte_buffer_class, "position", "I");
471499

472500
jclass qz_obj_class = (*env)->FindClass(env, "com/intel/qat/QatZipper");
473-
g_qzip_bytes_read_id = (*env)->GetFieldID(env, qz_obj_class, "bytesRead", "I");
501+
g_qzip_bytes_read_id =
502+
(*env)->GetFieldID(env, qz_obj_class, "bytesRead", "I");
474503
}
475504

476505
/**
@@ -481,13 +510,15 @@ JNIEXPORT void JNICALL Java_com_intel_qat_InternalJNI_initFieldIDs(JNIEnv *env,
481510
* @param env JNI environment pointer. Must not be NULL.
482511
* @param clz Java class object (unused).
483512
* @param qz_obj QatZipper Java object to store session key.
484-
* @param comp_algo Compression algorithm identifier (e.g., ZSTD_ALGORITHM).
513+
* @param comp_algo Compression algorithm identifier (e.g.,
514+
* ZSTD_ALGORITHM).
485515
* @param level Compression level (1 to COMP_LVL_MAXIMUM).
486516
* @param sw_backup Flag to enable (1) or disable (0) software fallback.
487517
* @param polling_mode Polling mode for QAT hardware.
488518
* @param data_format Data format for compression.
489519
* @param hw_buff_sz Hardware buffer size.
490-
* @return QZ_OK on success, QZ_FAIL on invalid input, QZ_NO_HW if hardware is
520+
* @return QZ_OK on success, QZ_FAIL on invalid input, QZ_NO_HW
521+
* if hardware is
491522
*/
492523
JNIEXPORT jint JNICALL Java_com_intel_qat_InternalJNI_setup(JNIEnv *env,
493524
jclass clz,
@@ -525,17 +556,20 @@ JNIEXPORT jint JNICALL Java_com_intel_qat_InternalJNI_setup(JNIEnv *env,
525556
return QZ_OK;
526557
}
527558

528-
int qz_key = generate_key_for_session(comp_algo, level, sw_backup,
529-
polling_mode, data_format, hw_buff_sz);
530-
QzWrapper_T *sess_ptr = get_or_create_session(env, qz_key);
559+
int qz_key = gen_session_key(comp_algo, level, sw_backup, polling_mode,
560+
data_format, hw_buff_sz);
561+
562+
QzSessionHandle_T *sess_ptr = get_session(qz_key);
563+
if (!sess_ptr) {
564+
sess_ptr = create_session(env, qz_key);
565+
}
566+
// Update the reference count
567+
sess_ptr->reference_count++;
531568

532569
jclass qz_clz = (*env)->FindClass(env, "com/intel/qat/QatZipper");
533570
jfieldID qz_session_field = (*env)->GetFieldID(env, qz_clz, "qzKey", "I");
534571
(*env)->SetLongField(env, qz_obj, qz_session_field, (jint)sess_ptr->qz_key);
535572

536-
// Update reference count
537-
sess_ptr->reference_count++;
538-
539573
return QZ_OK;
540574
}
541575

@@ -1501,16 +1535,16 @@ Java_com_intel_qat_InternalJNI_zstdFreeSeqProdState(JNIEnv *env,
15011535
* @param env JNI environment pointer. Must not be NULL.
15021536
* @param clz Java class object (unused).
15031537
* @param qz_key Value representing unique compression params
1504-
* @return QZ_OK on success, or 0 if an error occurs (with a Java exception
1505-
* thrown).
1538+
* @return QZ_OK on success, or 0 if an error occurs (with a Java
1539+
* exception thrown).
15061540
*/
15071541
JNIEXPORT jint JNICALL Java_com_intel_qat_InternalJNI_teardown(JNIEnv *env,
15081542
jclass clz,
15091543
jint qz_key) {
15101544
(void)clz;
15111545

15121546
// Validate session pointer
1513-
QzWrapper_T *sess_ptr = get_session(qz_key);
1547+
QzSessionHandle_T *sess_ptr = get_session(qz_key);
15141548
if (!sess_ptr || !sess_ptr->qz_session) {
15151549
return QZ_OK;
15161550
}

0 commit comments

Comments
 (0)