Skip to main content

Comparing bash and /bin/sh on z/OS

A few weeks ago, @wizardofzos tweeted about a unix shell script that showed a bug on z/OS.

Here is the script:

#!/bin/bash mkdir -p broken c=1 while [ $c -le 4000 ] do f=$RANDOM touch broken/$f setfacl -m "u:IBMUSER:rwx" broken/$f clear echo "Testing cut" | cut -c1 echo "Done for $c" let c=c+1 done

I was curious, so I tried to reproduce it on my system. The original script runs under bash. I tried it under both bash and /bin/sh but was unable to reproduce the bug, so I couldn’t do any further investigation.

What I did notice, however, was that the script was much, much faster under /bin/sh. That was interesting, so I had a closer look at the SMF data using EasySMF. I ran the 2 jobs for 1000 iterations of the loop. 1000 iterations created approximately 5000 unix tasks. Due to the various type 30 subtypes, the /bin/sh job produced about 20,000 type 30 records and the bash job about 28,000.

Here is part of the Job Completions report for the 2 jobs:

ANDREWRA is the job running bash, ANDREWRB runs /bin/sh.

The bash job took 2 minutes elapsed versus 1 minute for /bin/sh, but more interesting is the CPU time: more than 1 minute for bash, less than 5 seconds for /bin/sh. Those CPU times are the totals for all the descendants grouped under the collapsed top level jobs in the report.

The Unix Work report shows this in more detail. It works from the Step End SMF records and shows substep information as well. This is part of the report for ANDREWRA running bash:

We can see some interesting stuff here:

  • Very little of the CPU time is charged back to the owning job JOB06732. Most of it is in OMVS sub tasks.
  • The top level bash step uses the most CPU time.
  • We can see what looks like the fork/exec pattern described in the SMF manual, where the parent forks and then execs another program creating a sub-step in SMF.
  • Forking the bash task also seems to use a relatively large amount of CPU time.
  • Although the Job Number STC06734 is the same for all these tasks, they are actually separate tasks reusing the same OMVS initiator.

The same report for ANDREWRB running /bin/sh:

Interesting differences:

  • The top level shell uses a lot less CPU.
  • It does not show the same sub-step pattern. The commands themselves exist as top level steps in the OMVS initiator. Overall it uses a lot less CPU.

Conclusions

This isn’t meant to be a criticism of the bash port. I imagine IBM has a lot more ability to get into the operating system internals and optimize /bin/sh. But it is interesting to see the difference in resource usage.

The obvious conclusion would be to avoid bash for shell scripts with significant loops, or (probably) scripts that spawn many subcommands e.g. find. Use bash if you really need it’s functionality, e.g. for login shells.

Much of my description of what is going on is speculative based on what I can see in SMF, if you know more please feel free to comment.

User Key Common Flags in EasySMF

As you have probably heard, user key common storage will not be allowed in z/OS 2.4.

For more information on z/OS 2.4 User Key Common removal see Marna Walle’s article: Reminder to take a look: z/OS V2.4 user key common removal

IBM added some flags in the type 30 SMF record to audit usage of user key common storage. The flags allow you to see which jobs used user key common storage. The flags are:

  • SMF30_UserKeyCsaUsage
  • SMF30_UserKeyCadsUsage
  • SMF30_UserKeyChangKeyUsage

There is another flag for Restricted Use Common Service Area:

  • SMF30_UserKeyRuCsaUsage

Restricted use CSA is a relatively new function not going away in 2.4, but IBM discourage its use and it is becoming a priced feature.

EasySMF provides reports to show these flags and help you find any jobs or address spaces using user key common storage. The common storage flags are shown in the following reports:

  • Job Memory Information – Shows information from jobs and address spaces after they have ended.
  • Step Completions – Shows information from ended steps. This report shows the program name.
  • Running Jobs – Shows information including jobs that are still running (from SMF type 30 interval records).

You will need to scroll right to find the User Key Common columns.

EasySMF User Key Common Report

  • User Key Audit indicates whether the SMF30_UserKeyCommonAuditEnabled flag is set. This must be on, otherwise the information in the other fields is not valid.
  • User Key CSA shows the value of SMF30_UserKeyCsaUsage.
  • User Key CADS shows the value of SMF30_UserKeyCadsUsage
  • User Key CHANGKEY shows the value of SMF30_UserKeyChangKeyUsage
  • Restricted Use CSA shows the value of SMF30_UserKeyRuCsaUsage

Click the column headers to sort the values and find any jobs where the flags are set (click twice to sort descending).

User Key Common reports on z/OS using Java

If you don’t want to download SMF data to a PC, you can run a report on z/OS using Java. EasySMF:JE (a set of Java classes to map SMF records) provides a sample report to show User Key Common information.

Quickstart installation information for EasySMF:JE can be found here: EasySMF:JE Java Quickstart

Change the class name in IVP4 from com/blackhillsoftware/samples/RecordCount to com/blackhillsoftware/samples/UserKeyCommon to run the User Key Common sample report.

30 Day Trial

Both EasySMF and EasySMF:JE can be downloaded for 30 day trials.

Information about the trial is available here.

Understanding z/OS Unix Work with EasySMF

Unix work running under z/OS can be difficult to track.

A Unix process can create thousands of child processes, each running in another address space. They may only exist for fractions of a second and produce no job output, so you don’t see them in SDSF. Child processes create their own SMF type 30 records for job accounting, so their resource usage doesn’t appear in the parent’s accounting records. The child processes may have a different jobname and can even run in different service and report classes to the parent.

I have seen a job running BPXBATCH where the job itself used virtually no CPU time, but it spawned over 10,000 sub tasks. More than 99% of the CPU time used by that batch job appeared in child process SMF records with different job names, service and report classes.

EasySMF has reports to help you understand your z/OS Unix work.

EasySMF uses the Unix process and parent process id information from the type 30 SMF records to build a tree view of your Unix work.

  • You can see the parent – child relationships between different address spaces.
  • You can see whether the child service and report classes are the same as the original job.
  • When you collapse the tree, usage information from all the children is rolled up into the parent job. When you expand the tree, usage information is shown for the individual entries.

The expanded view of Unix work, showing the relationship between work running in different address spaces:

The collapsed view of the same work. Resources like CPU time show the total for all the related address spaces:

EasySMF can also help you find where work came from. If Related Processes is selected, EasySMF will search for and show parent and child tasks that do not match the main selection criteria. Here we can see a task with the job name ANDREWR, and Related Processes shows that particular task came in through SSH, and it had a number of Unix sub tasks.

Related Processes can help you find out where a Unix process came from.

More Detailed Information about Unix Work

The Unix Work report shows you more detail about work that uses z/OS Unix. As the name suggests, it only shows work that had a Unix component – other batch jobs and started tasks are ignored. This report shows information from the step and substep end records, which include some information about the program that was executed. This give clues to the processing taking place, although the information included in SMF is limited and may not show every program.

The Unix Work report shows information from Step End records for work with a Unix component, including the program information recorded in SMF.

Unix processes don’t always run in a different address space under z/OS. Sometimes processes will share an address space. This gives multiple Unix Process sections in SMF, with most of the statistics reported at the shared address space level. You can see this in the Unix Process report where you have processes that do not report the address space level information.

When multiple processes share an address space only one will show address space level information.

You can filter the Unix Work report to find a particular process ID – in this case the selected process was one of multiple processes that shared one address space.

Detailed Unix information is included in EasySMF since version 3.2.

Upgrades are free for existing customers. If you are not a customer, a 30 day trial is available.

Java mapping for CICS SMF records

The EasySMF Java API now has experimental support for CICS records.

Experimental, because I want to get some feedback from CICS users about class names, usage etc. before locking down the design. In particular:

  • Do the class names and organization make sense to a CICS person? Would other names or a different organization make more sense?
  • Are the examples of how to process data clear and useful?
  • Are there areas where terminology is used incorrectly?

The complete Javadoc is here with an overview of the CICS functionality here.

If you have any comments, you can leave feedback in the comments box below, send it to support@blackhillsoftware.com, or give feedback in person at booth 323 at SHARE in Providence, Rhode Island.

You can try out the API using the 30 day trial available here: 30 Day Trial.

Installation information is available here: EasySMF:JE Java Quickstart

Using the API

EasySMF:JE aims to provide a consistent interface across different SMF record types and sections, and converts values to standard Java types for simple programming.

Dates and Times

Dates and times are converted to java.time classes. Java.time can represent dates and times with a precision of 1 nanosecond.

Times representing a duration e.g. CPU or elapsed time are converted to Duration.
Dates and times of day are converted to LocalDate, LocalTime, LocalDateTime or ZonedDateTime depending on exactly what information is in the field. Typically, times based on UTC(GMT) are converted to ZonedDateTime with ZoneOffset.UTC. Other dates and times are converted to LocalDate/Times.
Java has time zone rules so it is possible to apply a ZoneId to a LocalDateTime and perform date aware conversions between time zones.

Numeric Values

1, 2 and 3 byte integer values and 4 byte signed integer values are converted to int (32 bit signed) values.
4-7 byte integer values and 8 byte signed values are converted to long (64 bit signed).

8 byte unsigned values are available as both long (64 bit signed) and as a BigInteger. The long value may provide better performance if the value will not exceed the maximum value for a long. If a value does exceed the maximum value (i.e. the high order bit is set) an exception will be thrown. If the field value might exceed the maximum value for a long, use the BigInteger version.

Integer values greater than 8 bytes are converted to BigInteger.

Floating point values are converted to Java double.

String Values

EBCDIC and UTF8 string/character values are converted to String. Java uses Unicode internally – values are converted from EBCDIC or UTF8.

Flags

Flag bits within a byte are converted to a boolean value indicating whether the bit is set.

CICS Statistics

Reading CICS statistics is very done the same way as reading sections from other records using the API. Sections of a specific type are returned in a List<E> of that type. If there are no sections of the type in the record an empty List is returned. This allows you to iterate over the sections without explicitly checking whether the sections exist in the record – an empty list will iterate 0 times.

Example

The following code reads all FileControlStatistics sections from type 110 SMF records from the DD INPUT.

try (SmfRecordReader reader =
SmfRecordReader
.fromDD("INPUT")
.include(110, Smf110Record.SMFSTSTY))
{
for (SmfRecord record : reader)
{
Smf100Record r110 = new Smf110Record(record);
for (FileControlStatistics fc :
r110.fileControlStatistics())
{
//... process FileControlStatistics sections here
}
}
}

CICS Performance Monitoring

Accessing data from CICS monitoring performance records is slightly different to other SMF records because the data needs to be accessed using a Dictionary.

Dictionary records are handled automatically, however you cannot access the data from a record before a related dictionary record has been seen. You can check whether a dictionary record is available using Smf110Record.haveDictionary() or simply concatenate all required dictionary records ahead of the data records in the input data.

Specific fields are defined by name and type. Then Performance records are read from the SMF record, and specific fields accessed using getField(…) methods or variations.

Example

ByteStringField transactionField = ByteStringField.define("DFHTASK","C001");
TimestampField startField = TimestampField.define("DFHCICS","T005");
TimestampField stopField = TimestampField.define("DFHCICS","T006");
ClockField dispatchField = ClockField.define("DFHTASK","S007");

try (SmfRecordReader reader =
SmfRecordReader
.fromDD("INPUT")
.include(110, Smf110Record.SMFMNSTY))
{
for (SmfRecord record : reader)
{
Smf100Record r110 = new Smf110Record(record);
if (r110.haveDictionary())
{
for (PerformanceRecord perfdata :
r110.performanceRecords())
{
String txName = perfdata.getField(transactionField);
ZonedDateTime start = perfdata.getField(startField);
ZonedDateTime stop = perfdata.getField(stopField);
double dispatch = perfdata.getFieldTimerSeconds(dispatchField);

//... process data
}
}
}
}

Complete CICS Statistics reporting sample

These samples are designed to show how to use the API, not to suggest items that you should specifically be reporting. However comments about their relevance are welcome.

import java.io.*;
import java.util.*;
import static java.util.Comparator.comparing;

import com.blackhillsoftware.smf.*;
import com.blackhillsoftware.smf.cics.*;
import com.blackhillsoftware.smf.cics.statistics.FileControlStatistics;

public class CicsFileStatistics
{
public static void main(String[] args) throws IOException
{
Map<String, Map<String, FileData>> applids =
new HashMap<String, Map<String, FileData>>();

try (SmfRecordReader reader =
args.length == 0 ?
SmfRecordReader.fromDD("INPUT") :
SmfRecordReader.fromStream(new FileInputStream(args[0])))
{
reader.include(110, Smf110Record.SMFSTSTY);
for (SmfRecord record : reader)
{
Smf110Record r110 = new Smf110Record(record);

Map<String, FileData> applidFiles =
applids.computeIfAbsent(r110.stProductSection().smfstprn(),
files -> new HashMap<String, FileData>());

for (FileControlStatistics fileStats : r110.fileControlStatistics())
{
String entryName = fileStats.a17fnam();
applidFiles.computeIfAbsent(entryName,
x -> new FileData(entryName)).add(fileStats);
}
}
}
writeReport(applids);
}

private static void writeReport(Map<String, Map<String, FileData>> applidFiles)
{

applidFiles.entrySet().stream()
.filter(applid -> !applid.getValue().isEmpty())
.sorted((a, b) -> a.getKey().compareTo(b.getKey()))
.forEachOrdered(applid ->
{
// Headings
System.out.format("%n%-8s", applid.getKey());

System.out.format("%n%-8s %12s %12s %12s %12s %12s %12s %12s %12s%n%n",
"ID",
"Gets",
"Get Upd",
"Browse",
"Adds",
"Updates",
"Deletes",
"Data EXCP",
"Index EXCP");

applid.getValue().entrySet().stream()
.map(x -> x.getValue())
.sorted(comparing(FileData::getTotalExcps)
.reversed())
.forEachOrdered(fileInfo ->
{
// write detail line
System.out.format("%-8s %12d %12d %12d %12d %12d %12d %12d %12d%n",
fileInfo.getId(),
fileInfo.getGets(),
fileInfo.getGetUpd(),
fileInfo.getBrowse(),
fileInfo.getAdds(),
fileInfo.getUpdates(),
fileInfo.getDeletes(),
fileInfo.getDataExcps(),
fileInfo.getIndexExcps());
});
});

}

private static class FileData
{
public FileData(String fileId)
{
this.id = fileId;
}

public void add(FileControlStatistics fileStatistics)
{
gets += fileStatistics.a17dsrd();
getupd += fileStatistics.a17dsgu();
browse += fileStatistics.a17dsbr();
add = fileStatistics.a17dswra();
update = fileStatistics.a17dswru();
delete = fileStatistics.a17dsdel();
dataexcp = fileStatistics.a17dsxcp();
indexexcp = fileStatistics.a17dsixp();
totalexcp += fileStatistics.a17dsxcp()
+ fileStatistics.a17dsixp();
}

public String getId()
{
return id;
}

public long getGets()
{
return gets;
}

public long getGetUpd()
{
return getupd;
}

public long getBrowse()
{
return browse;
}

public long getAdds()
{
return add;
}

public long getUpdates()
{
return update;
}

public long getDeletes()
{
return delete;
}

public long getDataExcps()
{
return dataexcp;
}

public long getIndexExcps()
{
return indexexcp;
}

public long getTotalExcps()
{
return totalexcp;
}

private String id;
private long gets = 0;
private long getupd = 0;
private long browse = 0;
private long add = 0;
private long update = 0;
private long delete = 0;
private long dataexcp = 0;
private long indexexcp = 0;
private long totalexcp = 0;
}
}

Complete CICS Transaction Monitoring reporting sample

import java.io.*;
import java.time.*;
import java.util.*;
import static java.util.Collections.reverseOrder;
import static java.util.Comparator.comparing;

import com.blackhillsoftware.smf.*;
import com.blackhillsoftware.smf.cics.*;
import com.blackhillsoftware.smf.cics.monitoring.*;
import com.blackhillsoftware.smf.cics.monitoring.fields.*;

public class CicsTransactionSummary
{

public static void main(String[] args) throws IOException
{
Map<String, Map<String, TransactionData>> applids =
new HashMap<String, Map<String, TransactionData>>();

ByteStringField transaction = ByteStringField.define("DFHTASK", "C001");

int noDictionary = 0;

try (SmfRecordReader reader =
args.length == 0 ?
SmfRecordReader.fromDD("INPUT") :
SmfRecordReader.fromStream(new FileInputStream(args[0])))
{
reader.include(110, Smf110Record.SMFMNSTY);
for (SmfRecord record : reader)
{
Smf110Record r110 = new Smf110Record(record);

if (r110.haveDictionary())
{
Map<String, TransactionData> applidTransactions =
applids.computeIfAbsent(
r110.mnProductSection().smfmnprn(),
transactions -> new HashMap<String, TransactionData>());

for (PerformanceRecord mn : r110.performanceRecords())
{
String txName = mn.getField(transaction);
applidTransactions.computeIfAbsent(
txName,
x -> new TransactionData(txName)).add(mn);
}
} else
{
noDictionary++;
}
}
}

writeReport(applids);

if (noDictionary > 0)
{
System.out.format(
"%n%nSkipped %s records because no applicable dictionary was found.",
noDictionary);
}

}

private static void writeReport(Map<String, Map<String, TransactionData>> transactions)
{
transactions.entrySet().stream()
.sorted((a, b) -> a.getKey().compareTo(b.getKey()))
.forEachOrdered(applid ->
{
// Headings
System.out.format("%n%-8s", applid.getKey());

System.out.format("%n%-4s %15s %15s %15s %15s %15s %15s %15s %15s %15s%n%n",
"Name",
"Count",
"Elapsed",
"Avg Elapsed",
"CPU",
"Avg CPU",
"Dispatch",
"Avg Disp.",
"Disp Wait", ""
+ "Avg Disp Wait");

applid.getValue().entrySet().stream()
.map(x -> x.getValue())
.sorted(comparing(TransactionData::getCpu, reverseOrder())
.thenComparing(TransactionData::getCount, reverseOrder()))
.forEachOrdered(txInfo ->
{
// write detail line
System.out.format("%-4s %15d %15f %15f %15f %15f %15f %15f %15f %15f%n",
txInfo.getName(),
txInfo.getCount(),
txInfo.getElapsed(),
txInfo.getAvgElapsed(),
txInfo.getCpu(),
txInfo.getAvgCpu(),
txInfo.getDispatch(),
txInfo.getAvgDispatch(),
txInfo.getDispatchWait(),
txInfo.getAvgDispatchWait());

});
});

}

private static class TransactionData
{
public TransactionData(String name)
{
this.name = name;
}

public void add(PerformanceRecord perfdata)
{
count++;
elapsed += Utils.ToSeconds(
Duration.between(perfdata.getField(start), perfdata.getField(stop)));
dispatch += perfdata.getFieldTimerSeconds(dispatchField);
dispatchWait += perfdata.getFieldTimerSeconds(dispatchWaitField);
cpu += perfdata.getFieldTimerSeconds(cpuField);
}

public String getName()
{
return name;
}

public int getCount()
{
return count;
}

public double getElapsed()
{
return elapsed;
}

public double getDispatch()
{
return dispatch;
}

public double getDispatchWait()
{
return dispatchWait;
}

public double getCpu()
{
return cpu;
}

public Double getAvgElapsed()
{
return count != 0 ? elapsed / count : null;
}

public Double getAvgDispatch()
{
return count != 0 ? dispatch / count : null;
}

public Double getAvgDispatchWait()
{
return count != 0 ? dispatchWait / count : null;
}

public Double getAvgCpu()
{
return count != 0 ? cpu / count : null;
}

static TimestampField start = TimestampField.define("DFHCICS", "T005");
static TimestampField stop = TimestampField.define("DFHCICS", "T006");
static ClockField dispatchField = ClockField.define("DFHTASK", "S007");
static ClockField dispatchWaitField = ClockField.define("DFHTASK", "S102");
static ClockField cpuField = ClockField.define("DFHTASK", "S008");

private String name;
private int count = 0;
private double elapsed = 0;
private double dispatch = 0;
private double dispatchWait = 0;
private double cpu = 0;
}
}

Sending Email from z/OS using Java

How do you send email from your z/OS system?

Java on z/OS makes this very easy. Java on z/OS can use Java libraries developed for other platforms which means there are powerful, free libraries available and plenty of documentation available via Google.

Instead of setting up a SMTP server on z/OS, you can define a user (or more than one) for z/OS in your corporate mail system, and create batch jobs that log in and send mail just like any other email user.

This example uses the JavaMail package (part of Java EE and available for download for Java SE) to send email from z/OS. It uses the JZOS Batch Launcher so you can define the input using JCL DD statements.

You can send an email using JCL like this:

//JAVAG EXEC PROC=JAVAG,
// JAVACLS='''ZMail''',
// CLASPATH='''java/lib/javax.mail.jar'''
//TO DD *
someone@example.com
//SUBJECT DD *
Test Message from batch job
//MESSAGE DD *
Message line 1
Message line 2
...
Message Line n
//

The JCL uses the JAVAG JCL PROC described in this post: Systems Programmer Friendly Java

The following code is the complete Java program. The sample uses Gmail as an example to show how to use TLS and SMTP authentication. Customize the Properties used to create the Session to suit your own mail server.

To, Subject and the Message Body are read from DD statements defined in the JCL. You could use the same pattern to allow other information to be input from DD statements, e.g. CC, BCC, From, or even the properties used to log in to the mail server.

import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;

import com.ibm.jzos.ZFile;
import com.ibm.jzos.ZFileException;

public class ZMail
{
public static void main(String[] args)
throws IOException, MessagingException
{
List<String> messagelines = readLinesFromDD("MESSAGE");
String subject = readLinesFromDD("SUBJECT").get(0);
List<String> recipients = readLinesFromDD("TO");

StringBuilder messagetext = new StringBuilder();
for (String line : messagelines)
{
messagetext.append(line);
messagetext.append(System.lineSeparator());
}

final String username = "smtp-username";
final String password = "smtp-password";

Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");
props.put("mail.ssl.checkserveridentity", "true");

Session session = Session.getInstance(props,
new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(username, password);
}
});

Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("me@example.com"));
for (String recipient : recipients)
{
message.addRecipients(Message.RecipientType.TO,
InternetAddress.parse(recipient));

}
message.setSubject(subject);
message.setText(messagetext.toString());

Transport.send(message);
}

private static List<String> readLinesFromDD(String dd)
throws ZFileException, IOException
{
List<String> lines = new ArrayList<String>();
ZFile input = null;
try
{
input = new ZFile("//DD:" + dd, "rt");
BufferedReader reader =
new BufferedReader(
new InputStreamReader(input.getInputStream()));

String line;
while ((line = reader.readLine()) != null)
{
lines.add(line);
}

}
finally
{
if (input != null)
{
input.close();
}
}
return lines;
}
}

Java SMF API Version 1.3.0

Verison 1.3.0 of the EasySMF:JE Java API is now available.

Major Changes

EZSMFKEY DD

The program will now look for the key in the EZSMFKEY DD statement as well as the EASYSMFKEY environment variable. The EASYSMFKEY environment variable is no longer required for z/OS batch jobs.

API messages

API messages are now issued via the SLF4J logging framework instead of to STDOUT. This allows the destination to be customized, and avoids mixing them with program output written to STDOUT. The default destination for messages is STDERR.

SMF Section Constructors marked private

The SMF section class constructors were always documented as “intended for internal use only”. Now they are private. This allows more flexibilty in construction, which in turn allows more transparent handling of changes to sections.

Sample JCL changes

The sample JCL procedures have been modified to eliminate the extra step to concatenate user additions and changes to the environment variables. This is now done in the main program execution step.

QWHCTOKN

The DB2 Accounting Token QWHCTOKN in the Qwhc section now has its own class. It was initially unclear what type of data appeared in the QWHCTOKN. While it was frequently readable characters, it also often contained binary data. Since the purpose is presumably to match data by token, a string representation was no good because binary data might result in different tokens converting to the same String. So initially a BigInteger was chosen as a class that could uniquely represent 22 bytes of data, and be compared and used as a key etc.

However, since there is character data there, people might reasonably want to see it as a string. BigInteger doesn’t make for a simple EBCDIC to UTF8 conversion. So I created a specific class for the QWHCTOKN. Now they can be reliably compared and used as a key, and there is a custom toString() implementation that does the EBCDIC to UTF8 conversion on the character portion.

Reminder: the DB2 SMF record support is still in experimental status so there is a greater likelihood of changes to the API. In particular, while the status is experimental, improving the API is likely to take precedence over mainitaining compatibility with existing programs.

Java SMF API Version 1.2.1

Version 1.2.1 of the EasySMF:JE Java API is now available.

Major Changes

  • Add SMF Type 121 (Java Statistics) record.

    This record has some interesting information, but has one major drawback. The ID running the Java job needs access to write SMF records. This is probably a security issue in many shops, because in general you don’t want to allow unauthorized users to write SMF records. It would be much nicer if this information was written by the JVM, without requiring special access for the user.

  • Modify samples to demonstrate use of Java 8 Streams functions. Streams are very nice for processing SMF data, and in many cases allow you to greatly simplify your code. It is unclear at the moment what the performance implications are. There are suggestions in some places that the code generated by Streams is not as well optimized as loops and iterators. That may be the case, but I don’t know yet whether the difference is detectable.

Java API for DB2 SMF Records

Version 1.2.0 of the EasySMF:JE Java API is now available.

Major Changes

  • SMF Type 89 Support
  • SMF type 42 Support
  • DB2 SMF type 100 and 101 support

Java API for DB2 SMF Records

This support is currently experimental while we learn more about the specifics of DB2 SMF data. Breaking changes to the API are more likely while it is in experimental status.

Using the API

Typical steps to use the package are:

  1. Read SMF records using the SmfRecordReader class
  2. Check the record type and other relevant attributes of each SmfRecord to see whether it is one you are interested in
  3. Create the specific record type from the SmfRecord e.g. new Smf100Record(record)
  4. Process information from the specific record as required
DB2 Record Compression

Records compressed with CSRCESRV will be automatically expanded when the SmfDb2Record is constructed.

IFCID

The IFCID of the record can be determined using the SmfDb2Record.ifcid() method.

Accessing Record Data

DB2 records are typically made up of a varying number of sections of different types. Sections of a specific type are returned in a List<E> of that type. If there are no sections of the type in the record an empty List is returned. This allows you to iterate over the sections without explicitly checking whether the sections exist in the record – an empty list will iterate 0 times. However, you may need to check the IFCID if the section is present in multiple IFCIDs but you want data from a specific IFCID.

Example

The following code reads all Qwos sections from type 100 SMF records from the DD INPUT.

try (SmfRecordReader reader =
SmfRecordReader.fromDD("INPUT").include(100)) // include only type 100 records
{
for (SmfRecord record : reader) // read each record
{
Smf100Record r100 = new Smf100Record(record); // construct a Smf100record
for (Qwos qwos : r100.qwos()) // process 0 or more Qwos sections
{
//... // process Qwos sections here
}
}
} // reader automatically closed at end
// of try with resources block.

Of course the inner loop can be replaced with whatever processing you need to do with the record.

Data Conversion

The API aims to provide a consistent interface across different types and sections, and converts values to standard Java types for simple programming. Conversions are the same as for other SMF record types.

Dates and Times

Dates and times are converted to java.time classes. Java.time can represent dates and times with a precision of 1 nanosecond.

  • Times representing a duration e.g. CPU or elapsed time are converted to Duration.
  • Dates and times of day are converted to LocalDate, LocalTime, LocalDateTime or ZonedDateTime depending on exactly what information is in the field. Typically, times based on UTC(GMT) are converted to ZonedDateTime with ZoneOffset.UTC. Other dates and times are converted to LocalDate/Times.
    Java has time zone rules so it is possible to apply a ZoneId to a LocalDateTime and perform date aware conversions between time zones.

The raw numeric value of date and time fields is also provided.

Numeric Values
  • 1, 2 and 3 byte integer values and 4 byte signed integer values are converted to int (32 bit signed) values.
  • 4-7 byte integer values and 8 byte signed values are converted to long (64 bit signed).
  • 8 byte unsigned values are available as both long (64 bit signed) and as a BigInteger. The long value may provide better performance if the value will not exceed the maximum value for a long. If a value does exceed the maximum value (i.e. the high order bit is set) an exception will be thrown.
    If the field value might exceed the maximum value for a long, use the BigInteger version.
  • Integer values greater than 8 bytes are converted to BigInteger.
  • Floating point values are converted to Java double.

Systems Programmer Friendly Java

This article describes how to set up a simple environment for running Java batch reports or utilities, including JCL PROCs similar to the compile and go PROCs that come with other languages.

Java on z/OS is great. Fast and powerful, you can perform complex functions in very few lines of code. In the past, I have used Rexx for small utilities, but now Java is my first choice.

Java is an advanced language with many features that simplify programming problems. For example, the Collections framework provides various List, Map, Set etc. classes that allow you to quickly and efficiently create, search and compare groups of data items.

The JZOS Toolkit comes with Java on z/OS and provides interfaces for working with regular z/OS datasets.

Java should be in every systems programmer’s toolbox.

The problem is there is not much guidance on how to create a simple Java batch environment on z/OS. Most of what is out there is written for large Java applications rather than simple batch jobs and as a result is unnecessarily complex.

The Objective

To create a set of procs for batch Java Compile, Go, and Compile and Go (there is no Link step for Java).

Compiling and running a Java class should be as simple as:

//ANDREWRG JOB CLASS=A,
// MSGCLASS=H,
// NOTIFY=&SYSUID
//JAVACG EXEC JAVACG,
// JAVACLS='''MyClass'''
//G.INPUT DD DISP=SHR,DSN=...

Optionally, you need to be able to override the locations for Java source, Java class files, class path etc. and the various Java options that can be specified.

The compile and link steps should be consistent so that you can switch between JAVAC, JAVAG and JAVACG without making other changes to the JCL.

Prerequisites

First, obviously, if Java is not already installed you need to install it. Refer to IBM 31-bit SDK for z/OS, Java Technology Edition, Version 8 and/or IBM 64-bit SDK for z/OS, Java Technology Edition, Version 8 for Java installation instructions.

JZOS Batch Launcher

You also need to install the JZOS Batch Launcher. This provides the functions that make Java useful in a z/OS batch environment.

Installation is straightforward. There is one load module to copy to a PDS/E, a JCL procedure and a sample job. The Readme suggests copying the load module to SYS1.SIEALNKE, however this is a SMP/E maintained dataset so if you are installing outside of SMP/E you should use a different location – create a new PDS/E if necessary.

JZOS documentation can be found here:

z/OS 2.1

These PROCs use symbol substitution in instream data which was introduced with z/OS 2.1, so z/OS 2.1 is a prerequisite. The JCL procedure delivered with JZOS works with earlier versions, but in that case the JCL required in the calling job is more complicated.

JCL Procedures

Most languages on z/OS come with a set of compile, link and go JCL procedures. These allow you to compile and run programs using standard options without dealing with the gory details every time.

We can create the same thing for Java, which greatly simplifies the process of compiling and running Java batch programs. I will be calling the PROCs JAVAC, JAVAG and JAVACG.

Design Considerations

Default Locations

Java source and class files need to be in the unix filesystem.

A standard directory structure will mean that we can set a default Java CLASSPATH etc. in the PROC, to simplify the user’s JCL.

We will use:

  • ~/java/src for the Java source
  • ~/java/target for the output class files

(~ is shorthand for the user’s home directory)

These can be overridden by the SRCPATH and TGTPATH parameters of the PROCs.

Line length

Instream data in the proc is limited to 80 bytes. If the symbol substitution extends the line beyond 80 bytes, the step gets a S001 abend. This is most likely if multiple symbols are used on one line.

To get around the problem, where we need multiple substitutions on 1 line we can assign the values to shell variables, and then use the shell variables. They are substituted later by the Unix shell which doesn’t have the same restrictions.

Java Classpath

The Java classpath is potentially lengthy with multiple entries. To allow for longer classpaths multiple CLASPAT* parameters are provided on the PROC.

To make life more complicated, the classpath syntax for the JZOS Batch Launcher is different from the Java compiler classpath syntax. The Java compiler allows wildcards but JZOS does not. We want to use the same classpath parameters in the JCL for the compile and go steps, so we need to expand wildcards in the JZOS script. To make sure the compile and go steps use the same classpath, we do the same expansion for the compile step.

Why triple quotes?

The triple quotes (apostrophes) are a quirk of JCL, which seems to be required for lowercase parameters. The enclosing quotes are stripped off when the parameters are set. The result still needs to be quoted otherwise the lowercase data causes a JCL error.

JAVAC

JAVAC is the Java compile PROC. It invokes BPXBATCH to run the Java compiler.

//JAVAC PROC JAVACLS=,
// REGSIZE='0M',
//* Parameters for instream data need triple quotes
//* to preserve quotes in the SET statement
// SRCPATH='''java/src''',
// TGTPATH='''java/target''',
// CLASPATH='''''',
//* Additional CLASPAT* entries allow CLASSPATH to
//* be extended with less JCL continuation issues.
// CLASPAT2='''''',
// CLASPAT3='''''',
// CLASPAT4='''''',
// CLASPAT5='''''',
//* Options to the java compiler
// JAVACOPT=''''''
//*
//SYMBOLS EXPORT SYMLIST=(JAVACOPT,
// SRCPATH,
// TGTPATH,
// CLASPATH,
// CLASPAT2,
// CLASPAT3,
// CLASPAT4,
// CLASPAT5,
// JAVACLS)
//*
// SET JAVACLS=&JAVACLS
// SET SRCPATH=&SRCPATH
// SET TGTPATH=&TGTPATH
// SET CLASPATH=&CLASPATH
// SET CLASPAT2=&CLASPAT2
// SET CLASPAT3=&CLASPAT3
// SET CLASPAT4=&CLASPAT4
// SET CLASPAT5=&CLASPAT5
// SET JAVACOPT=&JAVACOPT
//*
//C EXEC PGM=BPXBATCH,REGION=&REGSIZE
//* STDPARM is multiple commands chained into one with
//* semicolons as an argument to "SH"
//* IFS splits CLASSPATH based on ":"
//* Then wildcards are expanded and recombined
//STDPARM DD *,SYMBOLS=JCLONLY
SH CLASSPATH=&TGTPATH;
IFS=":";
for i in &CLASPATH; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${i}";
done;
done;
for i in &CLASPAT2; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${i}";
done;
done;
for i in &CLASPAT3; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${i}";
done;
done;
for i in &CLASPAT4; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${i}";
done;
done;
for i in &CLASPAT5; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${i}";
done;
done;
export CLASSPATH="${CLASSPATH}";
SRCPATH=&SRCPATH;
TGTPATH=&TGTPATH;
JAVACLS=&JAVACLS;
/usr/lpp/java/J8.0/bin/javac
&JAVACOPT
-sourcepath
${SRCPATH}
-d
${TGTPATH}
${SRCPATH}/${JAVACLS}.java
//STDENV DD DDNAME=ENV
// DD DDNAME=ADDENV
//ADDENV DD DISP=SHR,DSN=NULLFILE
//ENV DD *
HJV_JZOS_JVM_SMF_LOGGING=true
HJV_JZOS_JVM_SMF_LOGGING_INTERVAL=10
HJV_JZOS_JVM_SMF_THREADS=true
//STDOUT DD SYSOUT=*
//STDERR DD SYSOUT=*
//*
// PEND

JAVAC Examples

//ANDREWRG JOB CLASS=A,
// MSGCLASS=H,
// NOTIFY=&SYSUID
//JAVACG EXEC PROC=JAVAC,
// JAVACLS='''helloworld'''
//ANDREWRC JOB CLASS=A,
// MSGCLASS=H,
// NOTIFY=&SYSUID
//*
//JAVAC EXEC JAVAC,
// JAVACLS='''com/blackhillsoftware/samples/RecordCount''',
// SRCPATH='''java/easysmf-je-1-5-2/samples/source''',
// JAVACOPT='''-Xlint -verbose''',
// CLASPATH='''java/easysmf-je-1-5-2/jar/*'''
STDENV, ENV and ADDENV DD Statements

The STDENV DD statement allows you to specify enviroment variables for the BPXBATCH step.

STDENV concatenates 2 other DD statements:

  • ENV has default environment variables you want to set for all users of the proc.
  • ADDENV is a null file which can be overridden by the calling job to add or modify the default environment
//C.ADDENV DD *
MYVARIABLE=ABCD
CLASSPATH

CLASSPATH can be set using wildcards or explicit entries. You can have multiple entries on one line separated by “:”, e.g.

// CLASPATH='''java/easysmf-je-1-5-2/jar/*''',
// CLASPAT2='''java/lib/javax.mail.jar:java/lib/jsoup-1.10.2.jar'''

The shell script sets the field separator (IFS) to “:” so it can split multiple entries in the class path and expand any wildcards, before recombining all the entries.

JAVAG

JAVAG is based on the JVMPRC80 and JVMJCL80 samples delivered with the JZOS Batch Launcher.

The embedded script has been modified to set the classpath using the same syntax as the JAVAC proc.

//JAVAG PROC JAVACLS=,
// ARGS=,
// LIBRARY='PDSE.CONTAINING.JVMLDM80',
// LOGLVL='',
// REGSIZE='0M',
// LEPARM='',
//* Parameters for instream data need triple quotes
//* to preserve quotes in the SET statement
// SRCPATH='''java/src''',
// TGTPATH='''java/target''',
// CLASPATH='''''',
//* Additional CLASPAT* entries allow CLASSPATH to
//* be extended with less JCL continuation issues.
// CLASPAT2='''''',
// CLASPAT3='''''',
// CLASPAT4='''''',
// CLASPAT5=''''''
//*
//SYMBOLS EXPORT SYMLIST=(JAVACOPT,
// SRCPATH,
// TGTPATH,
// CLASPATH,
// CLASPAT2,
// CLASPAT3,
// CLASPAT4,
// CLASPAT5,
// JAVACLS)
//*
// SET JAVACLS=&JAVACLS
// SET SRCPATH=&SRCPATH
// SET TGTPATH=&TGTPATH
// SET CLASPATH=&CLASPATH
// SET CLASPAT2=&CLASPAT2
// SET CLASPAT3=&CLASPAT3
// SET CLASPAT4=&CLASPAT4
// SET CLASPAT5=&CLASPAT5
//*
//G EXEC PGM=JVMLDM80,REGION=&REGSIZE,
// PARM='&LEPARM/&LOGLVL &JAVACLS &ARGS'
//*
//STEPLIB DD DSN=&LIBRARY,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//STDOUT DD SYSOUT=*
//STDERR DD SYSOUT=*
//CEEDUMP DD SYSOUT=*
//ABNLIGNR DD DUMMY
//STDENV DD DDNAME=ENV
// DD DDNAME=ADDENV
//ADDENV DD DISP=SHR,DSN=NULLFILE
//ENV DD *,SYMBOLS=JCLONLY
. /etc/profile
export JAVA_HOME=/usr/lpp/java/J8.0
export PATH=/bin:"${JAVA_HOME}"/bin

LIBPATH=/lib:/usr/lib:"${JAVA_HOME}"/bin
LIBPATH="$LIBPATH":"${JAVA_HOME}"/lib/s390
LIBPATH="$LIBPATH":"${JAVA_HOME}"/lib/s390/j9vm
LIBPATH="$LIBPATH":"${JAVA_HOME}"/bin/classic
export LIBPATH="$LIBPATH":

APP_HOME=&TGTPATH
CLASSPATH="${APP_HOME}"
IFS=':'
for i in &CLASPATH; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${j}"
done
done
for i in &CLASPAT2; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${j}"
done
done
for i in &CLASPAT3; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${j}"
done
done
for i in &CLASPAT4; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${j}"
done
done
for i in &CLASPAT5; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${j}"
done
done
export CLASSPATH="${CLASSPATH}"

IJO="-Xms16m -Xmx128m"
export IBM_JAVA_OPTIONS="$IJO "

export HJV_JZOS_JVM_SMF_LOGGING=true
export HJV_JZOS_JVM_SMF_LOGGING_INTERVAL=10
export HJV_JZOS_JVM_SMF_THREADS=true
// PEND

JAVAG Examples

//JAVAG EXEC PROC=JAVAG,
// JAVACLS='''helloworld'''
//*JAVACLS='''com/blackhillsoftware/ivp'''
//*JAVACLS='''-jar /u/ajr/java/helloworld.jar'''
//G.INPUT DD DISP=SHR,DSN=ANDREWR.SMF.SAMPLE
//G.ADDENV DD *
IJO="$IJO -verbose:class"
export IBM_JAVA_OPTIONS="$IJO "

This sample JCL shows 3 different types of Java class that can be invoked:

  1. A class which is not part of a package.
  2. A class (ivp) which is a member of package com.blackhillsoftware.
  3. A runnable jar.

//G.INPUT is an example of a DDNAME used by a particular Java program, not a DD required by the PROC.

STDENV, ENV and ADDENV DD Statements

The STDENV DD statement provides a script that is run before the Java program is invoked. It is used to set environment variables.

Unfortunately, the syntax is different to STDENV for BPXBATCH in the compile step. BPXBATCH STDENV sets the environment variables directly, while JZOS requires a script. The JZOS approach adds some flexibility, but means you need to explicitly “export” the variables.

As with the compile step, STDENV concatenates 2 other DD statements:

  • ENV has the standard script inline in the PROC.
  • ADDENV is a null file which can be overridden by the calling job to add or modify the default environment.
//G.ADDENV DD *
IJO="$IJO -verbose:class"
export IBM_JAVA_OPTIONS="$IJO "
CLASSPATH

CLASSPATH is set using the same syntax as the compile PROC.

JAVACG

JAVACG combines the C and G PROCs, with a condition code check after the BPXBATCH compile step.

//JAVACG PROC JAVACLS=,
// ARGS=,
// LIBRARY='PDSE.CONTAINING.JVMLDM80',
// LOGLVL='',
// REGSIZE='0M',
// LEPARM='',
//* Parameters for instream data need triple quotes
//* to preserve quotes in the SET statement
// SRCPATH='''java/src''',
// TGTPATH='''java/target''',
// CLASPATH='''''',
//* Additional CLASPAT* entries allow CLASSPATH to
//* be extended with less JCL continuation issues.
// CLASPAT2='''''',
// CLASPAT3='''''',
// CLASPAT4='''''',
// CLASPAT5='''''',
//* Options to the java compiler
// JAVACOPT=''''''
//*
//SYMBOLS EXPORT SYMLIST=(JAVACOPT,
// SRCPATH,
// TGTPATH,
// CLASPATH,
// CLASPAT2,
// CLASPAT3,
// CLASPAT4,
// CLASPAT5,
// JAVACLS)
//*
// SET JAVACLS=&JAVACLS
// SET SRCPATH=&SRCPATH
// SET TGTPATH=&TGTPATH
// SET CLASPATH=&CLASPATH
// SET CLASPAT2=&CLASPAT2
// SET CLASPAT3=&CLASPAT3
// SET CLASPAT4=&CLASPAT4
// SET CLASPAT5=&CLASPAT5
// SET JAVACOPT=&JAVACOPT
//*
//C EXEC PGM=BPXBATCH,REGION=&REGSIZE
//* STDPARM is multiple commands chained into one with
//* semicolons as an argument to "SH"
//* IFS splits CLASSPATH based on ":"
//* Then wildcards are expanded and recombined
//STDPARM DD *,SYMBOLS=JCLONLY
SH CLASSPATH=&TGTPATH;
IFS=":";
for i in &CLASPATH; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${i}";
done;
done;
for i in &CLASPAT2; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${i}";
done;
done;
for i in &CLASPAT3; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${i}";
done;
done;
for i in &CLASPAT4; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${i}";
done;
done;
for i in &CLASPAT5; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${i}";
done;
done;
export CLASSPATH="${CLASSPATH}";
SRCPATH=&SRCPATH;
TGTPATH=&TGTPATH;
JAVACLS=&JAVACLS;
/usr/lpp/java/J8.0/bin/javac
&JAVACOPT
-sourcepath
${SRCPATH}
-d
${TGTPATH}
${SRCPATH}/${JAVACLS}.java
//STDENV DD DDNAME=ENV
// DD DDNAME=ADDENV
//ADDENV DD DISP=SHR,DSN=NULLFILE
//ENV DD *
HJV_JZOS_JVM_SMF_LOGGING=true
HJV_JZOS_JVM_SMF_LOGGING_INTERVAL=10
HJV_JZOS_JVM_SMF_THREADS=true
//STDOUT DD SYSOUT=*
//STDERR DD SYSOUT=*
//*
//G EXEC PGM=JVMLDM80,REGION=&REGSIZE,
// PARM='&LEPARM/&LOGLVL &JAVACLS &ARGS',
// COND=(0,NE,C)
//*
//STEPLIB DD DSN=&LIBRARY,DISP=SHR
//SYSPRINT DD SYSOUT=*
//SYSOUT DD SYSOUT=*
//STDOUT DD SYSOUT=*
//STDERR DD SYSOUT=*
//CEEDUMP DD SYSOUT=*
//ABNLIGNR DD DUMMY
//STDENV DD DDNAME=ENV
// DD DDNAME=ADDENV
//ADDENV DD DISP=SHR,DSN=NULLFILE
//ENV DD *,SYMBOLS=JCLONLY
. /etc/profile
export JAVA_HOME=/usr/lpp/java/J8.0
export PATH=/bin:"${JAVA_HOME}"/bin

LIBPATH=/lib:/usr/lib:"${JAVA_HOME}"/bin
LIBPATH="$LIBPATH":"${JAVA_HOME}"/lib/s390
LIBPATH="$LIBPATH":"${JAVA_HOME}"/lib/s390/j9vm
LIBPATH="$LIBPATH":"${JAVA_HOME}"/bin/classic
export LIBPATH="$LIBPATH":

APP_HOME=&TGTPATH
CLASSPATH="${APP_HOME}"
IFS=':'
for i in &CLASPATH; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${j}"
done
done
for i in &CLASPAT2; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${j}"
done
done
for i in &CLASPAT3; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${j}"
done
done
for i in &CLASPAT4; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${j}"
done
done
for i in &CLASPAT5; do
for j in ${i}; do
CLASSPATH="${CLASSPATH}":"${j}"
done
done
export CLASSPATH="${CLASSPATH}"

IJO="-Xms16m -Xmx128m"
export IBM_JAVA_OPTIONS="$IJO "

export HJV_JZOS_JVM_SMF_LOGGING=true
export HJV_JZOS_JVM_SMF_LOGGING_INTERVAL=10
export HJV_JZOS_JVM_SMF_THREADS=true
// PEND

JCL to invoke it looks like:

//JAVAC EXEC PROC=JAVA8UCG,
// JAVACLS='smf/joblist'
//*
//* DD statement for the Java program
//G.INPUT DD DISP=SHR,DSN=SMF.DATA
//* Optional DD statements to demonstrate
//* modifing compile/go environments
//G.ADDENV DD *
IJO="$IJO -verbose:class"
export IBM_JAVA_OPTIONS="$IJO "

Writing your Java program

Most examples suggest using an IDE like Eclipse to write and compile your program, transferring the class files to z/OS to run.

Eclipse is a very productive environment for writing Java and if you are used to using Eclipse, writing Java without it is painful. However, writing Java is no different to writing in any other language on z/OS and the simplicity of working with a single platform and familiar tools has a lot to recommend it.

I suggest getting a simple batch environment working on z/OS, and only then expanding the environment to use Eclipse, build and deploy scripts etc. if required. It is much easier to setup and debug more complex functions if you start with a simple, working batch environment.

Java programs must live in Unix directories due to the way Java uses the directory structure. ISPF option 3.17 provides a nice environment for editing z/OS unix files.

> Tip: ISPF highlighting doesn’t have a Java option, but setting the language to C works pretty well for matching braces, showing comments etc.

Those pesky class not found errors

First efforts to run Java on z/OS often result in mystifying class not found errors. You look at the CLASSPATH, you can see the Java class file there, why can’t Java find it?

The answer lies in how Java classes are organized into packages. Packages avoid naming conflicts between classes, but Java requires that the classes are located in subdirectories that reflect the package name.

z/OS (and Windows/Unix for that matter), searches the exact locations specified (LINKLIST, STEPLIB, path etc) for executable programs. Java searches for classes in locations relative to the CLASSPATH directory, based on the class package name.

If we have a simple Java program:

public class helloworld {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}

This program doesn’t specify a package, so the source and class files must be in the directories specified in the SOURCEPATH and CLASSPATH.

However, if we decide to create a “helloworld” package and a “testhello” package (perhaps to share the function with other programs):

package testhello;
// This class doesn't do anything
// until called from another class
public class hello {
public static void sayHello() {
System.out.println("Hello World!");
}
}

helloworld becomes:

package helloworld;
import testhello.hello;
// This class has a main(...) method
// which java calls to invoke the program
public class helloworld {
public static void main(String[] args) {
hello.sayHello();
}
}

The helloworld class is in the helloworld package, so the PROCs specify:

// JAVACLS='helloworld/helloworld'

If the runtime CLASSPATH is set to
/u/andrew/java Java will expect to find /u/andrew/java/helloworld/helloworld.class
and /u/andrew/java/testhello/hello.class.

If your CLASSPATH is set to /u/andrew/java and the hello.class and helloworld.class files are located in that directory, Java will say they cannot be found… which can be puzzling to say the least. The answer is that the location relative to the CLASSPATH must match the package name.

Java source files are also required to use the same hierarchy. A class in package mypackage MUST be in the mypackage/ subdirectory or you will get a compilation error.

> Actually, that’s not completely true… if you point the compiler at a Java file that specifies a package, that class will compile OK. It will also find other classes in the same package if they are in the same directory. However, it will locate other classes by going up the directory tree to what it considers the package root, and then looking for subdirectories according to package names.
> The class files produced will be placed in subdirectories of the output directory.

This means that Java source code on z/OS must be in HFS directories – unfortunately, you can’t use regular z/OS datasets for your source code.

Jar files follow the same principle, except that the directory hierarchy is embedded in the jar file itself. You can see this if you extract a jar file with a zip program.

Naming packages

To avoid name clashes there is a naming convention for Java packages. According to convention, the package name begins with your internet domain name reversed, i.e. Black Hill Software uses com.blackhillsoftware. Subsequent components are based on your own naming conventions.

You might choose not to use the package naming conventions for your own programs, however if usage becomes more formalized e.g. you develop a library of commonly used functions it is a good idea to use names that adhere to the standard.

Either way, packages provide a good way to organize your programs inside the ~/java/src etc. directory structure. You can separate programs into packages, which allows you to organize the programs into subdirectories based on package name.

Conclusion

A set of JCL procedures for compiling and running Java programs makes Java much more useful on z/OS.

Once you can easily compile and run Java on z/OS it becomes an excellent language for writing small batch utilities and productivity tools.

What Next?

Why not try using Java for SMF reporting : EasySMF:JE Java Quickstart

z/OS Productivity Tools using Java

This article examines how Java can be a powerful productivity tool for a Systems Programmer, using the task of synchronizing Master Catalogs as an example.

Systems programmers are often dismissive of Java. They think it is memory hungry, has poor performance and is not useful for real systems programming work.

In fact, the reality is that Java performance on z/OS is good, and being improved all the time.

More importantly, it is a powerful language and when combined with the functions provided by the JZOS Toolkit can be very useful for writing System Programmer utilities.

Example: Synchronizing Master Catalogs

Comparing or synchronizing master catalogs is a common task. You may have several systems with separate master catalogs, or need to merge a master catalog from a Serverpac installation with a live master catalog.

This Java example generates IDCAMS commands to synchronize catalogs. It could be adapted to define new entries only, work with specific HLQs etc.

The power of Java Collections

Java includes a Collections framework. As its name implies, the Collections framework provides functions for working with collections of data – Lists, Maps, Sets etc.

One of the most useful Collections classes is the Map, which maps keys to values. I usually use the HashMap specialization, which uses a hash table to map the values and generally has excellent performance.

Using a Map, the logic for comparing catalogs is simple:

  1. Add entries from the first catalog to a map using the name as a key
  2. Get all entries from the second catalog and:
    1. If there is a matching name in the map – remove the entry from the map and compare the attributes.
    2. Otherwise, record that the entry is in the second catalog only.
  3. The remaining entries in the map are in the first catalog only.

Sample code showing the logic:

Map<String, CatalogEntry> cat1Entries
= new HashMap<String, CatalogEntry>();
for (CatalogEntry entry :
getCatalogEntries(catalog1, "**", null))
{
cat1Entries.put(entry.entryName, entry);
}

List<CatalogEntry> cat2Only
= new ArrayList<CatalogEntry>();

for (CatalogEntry cat2Entry :
getCatalogEntries(catalog2, "**", null))
{
CatalogEntry cat1Entry
= cat1Entries.remove(cat2Entry.entryName);
if (cat1Entry != null) // found entry from catalog1
{
if (!cat2Entry.equals(cat1Entry))
{
// same name, different attributes
}
// else entries match
}
else
{
// entry is in catalog 2 only
}
}
// Remaining entries in map are in catalog 1 only
List<CatalogEntry> cat1Only
= new ArrayList<CatalogEntry>(
cat1Entries.values());

Catalog comparison logic

CatalogSync Implementation

The program consists of a CatalogEntry class, which provides the functions to work with catalog entries, and the main program with the comparison logic.

CatalogEntry Class

CatalogEntry instances are created from entries returned from the JZOS CatalogSearch function. The CatalogEntry constructor takes the JZOS CatalogSearch.Entry and extracts the information we are interested in.

import java.util.*;
import com.ibm.jzos.CatalogSearch;

public class CatalogEntry {
public CatalogEntry(CatalogSearch.Entry entry)
{
entryName =
entry.getField("ENTNAME").getFString();
entryType =
entry.getField("ENTYPE").getFString();
relatedNames =
entry.getField("NAME").getFStringArray(44);
if (relatedNames == null)
relatedNames = new String[] {};
volumes =
entry.getField("VOLSER").getFStringArray(6);
if (volumes == null)
volumes = new String[] {};
deviceTypes =
entry.getField("DEVTYP").getIntArray(4);
if (deviceTypes == null)
deviceTypes = new int[] {};
}

public String entryName;
public String entryType;
public String[] relatedNames;
public String[] volumes;
public int[] deviceTypes;
}

CatalogEntry.java class with constructor

The NAME, VOLSER and DEVTYP from CatalogSearch.Entry can be null if the attribute does not exist for that entry. In that case they are set to empty arrays, because that simplifies comparison processing.

Our CatalogEntry class has the following additional methods:

CatalogEntry.getCatalogEntries

getCatalogEntries reads entries from the specified catalog.

It is a static method which is used by the main program to retrieve the list of entries.

getCatalogEntries uses the JZOS CatalogSearch class which provides a Java interface to the z/OS Catalog Search Interface (CSI).

public static List<CatalogEntry> getCatalogEntries(
String catalogname,
String search,
String entryTypes)
{
List<CatalogEntry> result =
new ArrayList<CatalogEntry>();
CatalogSearch catSearch =
new CatalogSearch(search);
catSearch.setCatalogName(catalogname);
catSearch.setEntryTypes(entryTypes);
catSearch.setCatalogName(catalogname);
catSearch.setSingleCatalog(true);
catSearch.addFieldName("ENTYPE");
catSearch.addFieldName("ENTNAME");
catSearch.addFieldName("NAME");
catSearch.addFieldName("DEVTYP");
catSearch.addFieldName("VOLSER");
catSearch.search();
while (catSearch.hasNext())
{
CatalogSearch.Entry entry =
(CatalogSearch.Entry)catSearch.next();
if (entry.isDatasetEntry())
{
result.add(new CatalogEntry(entry));
}
}
return result;
}

CatalogEntry.getCatalogEntries method

getCatalogEntries sets various CSI parameters then invokes the search, returning the result as a List.

Note: The line catSearch.setSingleCatalog(true) says that only the specified catalog will be searched. Otherwise entries from multiple catalogs (e.g. the current master catalog) can be returned. This gives false results when trying to compare 2 catalogs.

For more information on the CatalogSearch function refer to the JZOS CatalogSearch documentation, and the Catalog Search Interface documentation in the z/OS Managing Catalogs manual.

CatalogEntry.equals and CatalogEntry.hashcode

CatalogEntry.equals tests whether 2 catalog entries are equal.

CatalogEntry.hashcode is not used in this program, but is included because when you override the equals function it is good practice to also override the hashcode function. Equals and hashcode are used when the class is used as a key in a HashMap. The rule is that if 2 instances are equal they must have the same hashcode.

This class simply returns the hashcode from the entry name as the CatalogEntry.hashcode.1

@Override
public boolean equals(Object o)
{
if (o == this) return true; // same object
if (!(o instanceof CatalogEntry)) return false;
CatalogEntry ce = (CatalogEntry) o;
return (Objects.equals(
this.entryName, ce.entryName)
&& Objects.equals(
this.entryType, ce.entryType)
&& Arrays.equals(
this.relatedNames, ce.relatedNames)
&& Arrays.equals(
this.volumes, ce.volumes)
&& Arrays.equals(
this.deviceTypes, ce.deviceTypes)
);
}

@Override
public int hashCode()
{
return entryName.hashCode();
}

CatalogEntry.equals and hashcode methods

CatalogEntry.deviceType

This is a utility function to translate the device type from the catalog to a device type for the IDCAMS command.

String deviceType(int devtype)
{
switch (devtype)
{
case 0:
return "0000";
case 0x3010200E:
return "3380";
case 0x3010200F:
return "3390";
default:
return String.format(
"Device type %8X not implemented",
devtype);
}
}

CatalogEntry. deviceType method

CatalogEntry.deleteCommand, defineCommand

The deleteCommand and defineCommand methods generate the IDCAMS commands used to synchronize the catalogs.

The methods use String formatting to insert the values into the commands.

public String deleteCommand(String catalog)
{
switch (entryType)
{
case "A": //NONVSAM
return String.format(
" DELETE -%n" +
" %s -%n" +
" NOSCRATCH -%n"+
" CATALOG(%s)%n",
entryName,
catalog);
case "X": //ALIAS
return String.format(
" DELETE -%n" +
" %s -%n" +
" ALIAS -%n"+
" CATALOG(%s)%n",
entryName,
catalog);
default:
return String.format(
"Delete entry type %s " +
"not implemented, %s%n",
entryType, entryName);
}
}

CatalogEntry.deleteCommand method

public String defineCommand(String catalog)
{
switch (entryType)
{
case "A": //NONVSAM
String volList = volumes[0];
for (int i=1; i < volumes.length; i++)
{
volList += " " + volumes[i];
}
String devList =
deviceType(deviceTypes[0]);
for (int i=1;
i < deviceTypes.length; i++)
{
devList +=
" " + deviceType(deviceTypes[i]);
}
return String.format(
" DEFINE NONVSAM -%n" +
" (NAME(%s) -%n" +
" DEVICETYPES(%s) -%n"+
" VOLUMES(%s) )-%n"+
" CATALOG(%s)%n",
entryName,
devList,
volList,
catalog);
case "X": //ALIAS
return String.format(
" DEFINE ALIAS -%n" +
" (NAME(%s) -%n" +
" RELATE(%s) ) -%n" +
" CATALOG(%s)%n",
entryName,
relatedNames[0],
catalog);
default:
return String.format(
"Define entry type %s " +
"not implemented, %s%n",
entryType, entryName);
}
}

CatalogEntry.defineCommand method

defineCommand handles the case where there are multiple volumes in the entry by building strings with the volumes and device types before inserting them into the command.

The CatalogSync program

This is the main program to generate the catalog synchronization commands.


DANGER

Automatically generated commands can severely damage your system if they contain errors. Before running the commands generated by this program, please make sure that:

  • The generated commands are correct, and operating on the correct catalogs.
  • All required continuation characters are present so that the statement specifying the target catalog is part of the statement.
  • You understand what the commands are doing.
  • You have a plan for recovery if an error occurs.

The catalog names are passed as arguments to the program, and the delete/define commands are written to the DD COMMANDS.

This program only deals with NONVSAM and ALIAS entries, so the call to getCatalogEntries specifies “AX” as the entry types. (See the CSI documentation in the IBM Managing Catalogs manual.)

import java.io.*;
import java.util.*;
import com.ibm.jzos.*;

public class CatalogSync
{
public static void main(String[] args)
throws IOException
{
if (args.length != 2)
{
System.out.println(
"Usage: CatalogSync catalog1 catalog2");
return;
}
String cat1 = args[0];
String cat2 = args[1];

try (PrintWriter writer
= new PrintWriter(
FileFactory.newOutputStream(
"//DD:COMMANDS")))
{
Map<String, CatalogEntry> cat1Entries
= new HashMap<String, CatalogEntry>();
for (CatalogEntry entry :
CatalogEntry.getCatalogEntries(
cat1, "**", "AX"))
{
cat1Entries.put(
entry.entryName, entry);
}

for (CatalogEntry cat2entry :
CatalogEntry.getCatalogEntries(
cat2, "**", "AX"))
{
CatalogEntry cat1entry = cat1Entries
.remove(cat2entry.entryName);
if (cat1entry != null)
{
if (!cat2entry.equals(cat1entry))
{
writer.print(
cat2entry.deleteCommand(
cat2));
writer.print(
cat1entry.defineCommand(
cat2));
}
// else entries match
} else
{
writer.print(
cat2entry.deleteCommand(
cat2));
}
}

List<CatalogEntry> cat1Only
= new ArrayList<CatalogEntry>(
cat1Entries.values());
Collections.sort(cat1Only,
new Comparator<CatalogEntry>()
{
public int compare(
CatalogEntry s1, CatalogEntry s2)
{
return
s1.entryName.compareTo(
s2.entryName);
}
});

for (CatalogEntry entry : cat1Only)
{
writer.print(
entry.defineCommand(cat2));
}
}
}
}

CatalogSync.java class

Compiling and Running the Program

This JCL uses the PROCs described in the article Systems Programmer Friendly Java to compile and run the complete program:

//JAVAG EXEC PROC=JAVA8UCG,
// JAVACLS='CatalogSync'
//G.MAINARGS DD *
CATALOG.MASTER
CATALOG.MASTER.COPY
//G.COMMANDS DD DISP=SHR,DSN=USERID.JCL.CNTL(GENCMDS)

Compile and Execution JCL

Conclusion

Java is a powerful language and a useful addition to z/OS. Hopefully this article has given you some ideas about how it could make your job easier.


Footnote 1

1: Hash collisions can cause performance problems when using hash tables, so it is recommended that all the members that are used to determine equality are used when calculating a hashcode. In this case I am using the lazy assumption that CatalogEntries used as keys in a hashtable will normally have different entry names.