@@ -88,11 +88,11 @@ static void call_qzstd_once(void) {
88
88
* Represents a unique QAT session for specific compression parameters.
89
89
* This structure is used to manage a QAT session with associated metadata.
90
90
*/
91
- typedef struct QzWrapper_T {
91
+ typedef struct QzSessionHandle_T {
92
92
int32_t qz_key ; /**< Unique identifier for session parameters */
93
93
int32_t reference_count ; /**< Number of active references to this session */
94
94
QzSession_T * qz_session ; /**< Pointer to the QAT session object */
95
- } QzWrapper_T ;
95
+ } QzSessionHandle_T ;
96
96
97
97
/**
98
98
* Defines the maximum number of unique QAT sessions allowed per thread.
@@ -109,9 +109,9 @@ typedef struct QzWrapper_T {
109
109
* Thread-local cache of QAT session objects, indexed by unique session keys.
110
110
* Stores up to MAX_SESSIONS_PER_THREAD active sessions per thread to optimize
111
111
* 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.
113
113
*/
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 ];
115
115
116
116
/**
117
117
* Thread-local counter tracking the number of active QAT sessions in
@@ -135,12 +135,12 @@ static _Thread_local int g_session_counter;
135
135
* internally).
136
136
* @return A 32-bit key uniquely representing the session parameters.
137
137
*/
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 ) {
144
144
int32_t qz_key = 0 ;
145
145
146
146
// Bit-field allocation: 4 bits each for algorithm, level, sw_backup,
@@ -253,48 +253,33 @@ static int setup_lz4_session(QzSession_T *qz_session,
253
253
}
254
254
255
255
/**
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 .
260
260
*
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)
278
270
*
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) .
283
275
*
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 .
289
281
*/
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 ) {
298
283
if (g_session_counter == MAX_SESSIONS_PER_THREAD ) {
299
284
(* env )-> ThrowNew (env , (* env )-> FindClass (env , "java/lang/RuntimeException" ),
300
285
"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) {
308
293
int data_format = (qz_key >> 13 ) & 0xF ;
309
294
int hw_buff_sz = ((qz_key >> 17 ) & 0xFFF ) << 10 ;
310
295
311
- QzWrapper_T * sess_ptr = & g_session_cache [g_session_counter ++ ];
296
+ QzSessionHandle_T * sess_ptr = & g_session_cache [g_session_counter ++ ];
312
297
sess_ptr -> qz_key = qz_key ;
313
298
sess_ptr -> qz_session = (QzSession_T * )calloc (1 , sizeof (QzSession_T ));
314
299
@@ -350,6 +335,47 @@ static QzWrapper_T *get_or_create_session(JNIEnv *env, int32_t qz_key) {
350
335
return sess_ptr ;
351
336
}
352
337
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
+
353
379
/**
354
380
* Compresses a buffer pointed to by the given source pointer and writes it to
355
381
* 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) {
362
388
* @param src_len The size of the source buffer.
363
389
* @param dst_ptr The destination buffer.
364
390
* @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.
367
395
* @param retry_count The number of compression retries before we give up.
368
396
* @return QZ_OK (0) if successful, non-zero otherwise.
369
397
*/
@@ -470,7 +498,8 @@ JNIEXPORT void JNICALL Java_com_intel_qat_InternalJNI_initFieldIDs(JNIEnv *env,
470
498
(* env )-> GetFieldID (env , byte_buffer_class , "position" , "I" );
471
499
472
500
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" );
474
503
}
475
504
476
505
/**
@@ -481,13 +510,15 @@ JNIEXPORT void JNICALL Java_com_intel_qat_InternalJNI_initFieldIDs(JNIEnv *env,
481
510
* @param env JNI environment pointer. Must not be NULL.
482
511
* @param clz Java class object (unused).
483
512
* @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).
485
515
* @param level Compression level (1 to COMP_LVL_MAXIMUM).
486
516
* @param sw_backup Flag to enable (1) or disable (0) software fallback.
487
517
* @param polling_mode Polling mode for QAT hardware.
488
518
* @param data_format Data format for compression.
489
519
* @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
491
522
*/
492
523
JNIEXPORT jint JNICALL Java_com_intel_qat_InternalJNI_setup (JNIEnv * env ,
493
524
jclass clz ,
@@ -525,17 +556,20 @@ JNIEXPORT jint JNICALL Java_com_intel_qat_InternalJNI_setup(JNIEnv *env,
525
556
return QZ_OK ;
526
557
}
527
558
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 ++ ;
531
568
532
569
jclass qz_clz = (* env )-> FindClass (env , "com/intel/qat/QatZipper" );
533
570
jfieldID qz_session_field = (* env )-> GetFieldID (env , qz_clz , "qzKey" , "I" );
534
571
(* env )-> SetLongField (env , qz_obj , qz_session_field , (jint )sess_ptr -> qz_key );
535
572
536
- // Update reference count
537
- sess_ptr -> reference_count ++ ;
538
-
539
573
return QZ_OK ;
540
574
}
541
575
@@ -1501,16 +1535,16 @@ Java_com_intel_qat_InternalJNI_zstdFreeSeqProdState(JNIEnv *env,
1501
1535
* @param env JNI environment pointer. Must not be NULL.
1502
1536
* @param clz Java class object (unused).
1503
1537
* @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).
1506
1540
*/
1507
1541
JNIEXPORT jint JNICALL Java_com_intel_qat_InternalJNI_teardown (JNIEnv * env ,
1508
1542
jclass clz ,
1509
1543
jint qz_key ) {
1510
1544
(void )clz ;
1511
1545
1512
1546
// Validate session pointer
1513
- QzWrapper_T * sess_ptr = get_session (qz_key );
1547
+ QzSessionHandle_T * sess_ptr = get_session (qz_key );
1514
1548
if (!sess_ptr || !sess_ptr -> qz_session ) {
1515
1549
return QZ_OK ;
1516
1550
}
0 commit comments