C Code to Lookup MQ Reason Code

Starting with the release of IBM MQ v8.0, IBM included a new header file called: cmqstrc.h. The file description says:

This file provides mappings between MQI constant values and string versions of their definitions.

In the cmqstrc.h file, from IBM MQ v9.2, there are 342 subroutines for looking up the various integer values and returning the MQ constant (string) value. Each subroutine uses switch/case instructions for looking up the integer value.

Using switch/case instructions is fine for 1 off usage in your code (i.e. for a failure situation) but if your code is calling a lookup subroutine many times a second then you are creating an unnecessary bottleneck.

Switch/case instructions are performing a linear/sequential search algorithm. A linear search is extremely inefficient. It would be far, far better to use a binary search algorithm.

Algorithm Worst-case
Performance
Best-case
Performance
Average
Performance
Linear Search O(n) O(1) O(n/2)
Binary Search O(log n) O(1) O(log n)

There are 561 MQ Reason codes. So, based on the above table, the code would make the following comparisons for average performance:

– Linear search is O(n/2) which is O(561/2) equals 280.5
– Binary search is O(log n) which is O(log 561) equals 9.13

The difference in the number of comparisons between Linear search and Binary search is staggering (orders of magnitude different).

As I said earlier, if your code only needs to output the MQ constant for an integer value in a 1 off situation (error condition) then using a Linear search algorithm is fine but if your code is repetitively looking up MQ constants then you should switch to using a Binary search algorithm.

I have created a simple C header file (called mqlookup.h) that will provide lookup subroutines for MQ Completion Codes and Reason Codes. Note: The Lookup_MQRC subroutine uses a Binary search whereas Lookup_MQCC subroutine uses switch/case because there are only 4 values for Completion Codes.

You simply include the mqlookup.h file in your program then you can do:

#include “cmqc.h”                   /* Include for MQI Constants */
#include “mqlookup.h”

char tempBuf[25];
...

MQCONN(QMName, &hConn, &CompCode, &Reason);

if (MQRC_NONE != Reason)
{
  printf("MQCONN to %.48s failed with MQCC=%d (%s) : MQRC=%d (%s)\n",
         QMName,
         CompCode,
         Lookup_MQCC(CompCode, tempBuf),
         Reason,
         Lookup_MQRC(Reason, tempBuf));
}

The Lookup_MQ* routines require 2 parameters:
– the integer value to look up
– a buffer to be used in case the integer value is not found. The integer will be converted to a string and returned to caller.

You can download the source code from here.

Regards,
Roger Lacroix
Capitalware Inc.

This entry was posted in C, Capitalware, HPE NonStop, IBM i (OS/400), IBM MQ, Linux, macOS (Mac OS X), Open Source, Programming, Raspberry Pi, Unix, Windows, z/OS.

Comments are closed.