It's easy with the Materialize Machine Attributes MI instruction.
While RPG application developers can easily access job-related information, such as the current job date (special words such as UDATE or *DATE) and the current user of the program (offset 358 through 367 of the PSDS), there are (admittedly infrequent) times when the developer also needs specific pieces of system-related information. Items that rapidly come to mind are the serial number of the system and the name of the system, though I've also seen questions about the serial number of the LPAR the application is running on, how many processors there are on the system, and so on.
There are many ways to access information such as the system serial number and/or the system name. In the case of the system serial number, the RPG developer might elect to call a CL program that runs the Retrieve System Value (RTVSYSVAL) command and then returns the system value QSRLNBR. Alternatively, the developer might choose to call the Retrieve System Values (QWCRSVAL) API directly to access the system value QSRLNBR. Along the same lines, access to the system name could be accomplished by using the Retrieve Network Attributes (RTVNETA) command within a called CL program or calling the Retrieve Network Attributes (QWCRNETA) API.
For accessing these specific pieces of system information, there's a third method that can be used—one that does not involve needing to call another program to do it. If you look at the various API categories that are documented in the Information Center, you'll notice a category named Machine Interface (MI) Instructions. Many of these instructions provide for bound program access, meaning they can be imbedded directly within your ILE RPG application program and appear to the developer as a procedure. The MI instruction of interest to us today is Materialize Machine Attributes (MATMATR).
The MATMATR instruction materializes (or retrieves to use common API terminology) information about the machine (or more typically, the partition of the machine) the instruction is running on. The interface to the instruction is defined as shown below.
Bound Program Access |
Built-in number for MATMATR1 is 92. MATMATR1 ( materialization : address machine_attributes : address (of just a selector value) ) |
A few items to point out about the interface documentation as provided in the Information Center:
-
The line "Bound program access" indicates that the instruction can be directly imbedded into an ILE application program (in our case, ILE RPG but it could just as well be used in ILE CL).
-
The RPG prototype will define the MI built-in as an external procedure using the documented name (MATMATR1) preceded by an underscore (_).
The term "address" indicates that the parameter is passed by reference (an address or pointer to the actual parameter values).
Reading the instruction documentation a bit further, we also learn that:
-
The first parameter (referred to in the documentation as operand 1) identifies where the information is to be returned. This loosely corresponds to the Receiver variable of the system Retrieve APIs you might be familiar with. One significant difference, however, is that the first four bytes of this parameter are defined as a 4-byte integer input representing the length of the parameter. When calling most system Retrieve-type APIs, this length of the receiver variable would be a separate parameter and the first 4 bytes of the receiver variable would be defined as an output value representing the number of bytes returned by the API in the receiver variable.
-
The second parameter (operand 2) is a 2-byte character input value identifying the information to be returned. This loosely corresponds to the Format parameter of many system Retrieve-type APIs.
With this information, here is a program that retrieves the serial number of the machine and then DSPLYs it.
h dftactgrp(*no)
d MatMAtr pr extproc('_MATMATR1')
d RcvVar 1a options(*varsize)
d Format 2a const
d MchInfo ds qualified
d BytPrv 10i 0 inz(%size(MchInfo))
d BytAvl 10i 0
d SrlNbr 8a
/free
MatMatr(MchInfo :x'0004');
dsply ('Serial number . . . ' + MchInfo.SrlNbr);
*inlr = *on;
return;
/end-free
The program prototypes the MATMATR instruction as discussed earlier and then defines the data structure MchInfo. Reviewing the MI instruction documentation, we see that a Selection value of x'0004' corresponds to the physical machine serial number identification and that this serial number is returned as an 8-byte character value immediately following the 4-byte integer field "Number of bytes provided by the user" and the 4-byte integer value "Number of bytes available for materialization" of the receiver variable parameter. The MchInfo data structure is our receiver variable which mirrors this definition. Note that the definition of MchInfo also initializes MchInfo.BytPrv to the size of the MchInfo data structure.
The program then runs the MATMATR instruction and DSPLYs the returned serial number. Not much to using this MI instruction is there?
Assuming the previous source is stored in member GETSRLNBR of QRPGLESRC, you can compile it using CRTBNDRPG PGM(GETSRLNBR) and call it with CALL PGM(GETSRLNBR). After calling the program, you should see a message similar to this:
DSPLY Serial number . . . 01AB23R
Here's another program demonstrating how you might access other types of system-related information and some of the flexibility you have when running the MATMATR instruction.
h dftactgrp(*no)
d MatMAtr pr extproc('_MATMATR1')
d RcvVar 1a options(*varsize)
d Format 2a const
d Various ds qualified
d BytPrv 10i 0 inz(%size(Various))
d BytAvl 10i 0
d SrlNbr 8a
d SysNam 8a overlay(SrlNbr)
d NbrCPUs 5u 0 overlay(SrlNbr)
d LPARInfo ds qualified
d BytPrv 10i 0 inz(%size(LPARInfo))
d BytAvl 10i 0
d OldNbrLPARs 1a
d OldCurLPARID 3u 0
d OldPriLPARID 1a
d OldSvcLPARID 1a
d FirmwareLvl 1a
d 3a
d LPARSrlNbr 10a
d NewNbrLPARs 97 98u 0
d NewCurLPARID 99 100u 0
d NewPriLPARID 101 102u 0
d NewSvcLPARID 103 104u 0
/free
MatMatr(Various :x'0130');
dsply ('System name . . . . ' + Various.SysNam);
MatMatr(Various :x'0004');
dsply ('Serial number . . . ' + Various.SrlNbr);
MatMAtr(LPARInfo :x'01E0');
dsply ('LPAR serial number . ' + LPARInfo.LPARSrlNbr);
dsply ('LPAR ID (new) . . . ' + %char(LPARInfo.NewCurLPARID));
dsply ('LPAR ID (old) . . . ' + %char(LPARInfo.OldCurLPARID));
dsply ('Number of LPARs . . ' + %char(LPARInfo.NewNbrLPARs));
MatMatr(Various :x'01DC');
dsply ('Number of CPUs . . . ' + %char(Various.NbrCPUs));
*inlr = *on;
return;
/end-free
Our first program, GETSRLNBR, used the data structure MchInfo, which was defined with only the system serial number in mind. In the new program, we use the data structure Various to return various types of information—the system serial number, the system name, and the number of processors installed on the system. For other accesses to system information, we use another data structure—LPARInfo.
The first use of MATMATR uses a format (selection value) of x'0130'. This value indicates that we want to materialize/retrieve network attribute information. While the amount of network information available is 190 bytes in length, we're interested only in the system name, which is returned as an 8-byte character value immediately following the Bytes provided (BytPrv) and Bytes available (BytAvl) fields discussed when accessing the serial number. As Various.BytPrv is initialized to a value of 16, we will materialize only the system name.
The program then reuses the Various data structure to access and DSPLY the system serial number.
Following this, the program uses the data structure LPARInfo and format x'01E0'. A selection value of x'01E0' indicates that we want to materialize partitioning information. I would like to point out a couple of items about the LPARInfo data structure:
-
Field "Current partition identifier (legacy)" is defined as being a Char(1) that represents a binary value. In addition, if the ID value is greater than or equal to 255, then the value x'FF' will be returned with the actual value being returned in "Current partition identifier." While the field is documented as Char(1), LPARInfo defines it as a 1-byte unsigned integer value. This definition makes it much easier to work with when subsequently DSPLYing the value.
-
Field "Current partition identifier" is defined as being a UBin(2), which corresponds to an RPG 2-byte unsigned integer value. As LPARInfo does not define all of the fields returned with selector value x'01E0' but we do want to access the Current partition identifier value, LPARInfo defines this field using absolute from and to location values. These values are 1 greater than those found in the MATMATR documentation as the documentation is using a base of 0 while RPG data structures are defined using a base of 1.
After DSPLYing the current partition's serial number (which is not the same as the physical serial number), the LPAR ID (where both the legacy and non-legacy values will be the same, assuming you have less than 255 LPAR IDs assigned), and the number of LPARs, the program then uses selector value x'01DC' to access the number of installed processors for the physical machine. This information is returned as a 2-byte unsigned integer. When accessing the installed processor count, the program is using the Various data structure with a BytPrv value of 16 while there are only 10 bytes to actually materialize. As with our earlier access to network attributes, where BytPrv was smaller than the data that could be materialized, it's also not a problem to provide a BytPrv value that is larger than the data that can be materialized.
In the case of x'01DC' (processor count), Various.BytAvl will be returned as a value of 10—the number of bytes actually available. In the earlier case of x'0130' (network attributes), Various.BytAvl will be returned with a value of 198—the number of bytes available if a larger receiver variable was provided (with, of course, an appropriately set Various.BytPrv value).
Assuming the previous source is stored in member GETMCHINFO of QRPGLESRC, you can compile it using CRTBNDRPG PGM(GETMCHINFO) and call it with CALL PGM(GETMCHINFO). After calling GETMCHINFO, you should see messages similar to this:
DSPLY System name . . . . S0123456
DSPLY Serial number . . . 01AB23R
DSPLY LPAR serial number . 01AB23RH
DSPLY LPAR ID (new) . . . 17
DSPLY LPAR ID (old) . . . 17
DSPLY Number of LPARs . . 26
DSPLY Number of CPUs . . . 24
Today, we've looked at another method that can be used to access some system-level information. Using the MATMATR MI instruction is different from using approaches such as CL commands and callable system APIs, but you'll find that this MI instruction can "run circles" around these other approaches and, as we've seen, is pretty easy to use.
As usual, if you have any API questions, send them to me at
This email address is being protected from spambots. You need JavaScript enabled to view it. . I'll see what I can do about answering your burning questions in future columns.
LATEST COMMENTS
MC Press Online