IBM Support

Using C API show jobs with specified "app"

How To


Summary

How to use LSF C API to list the jobs same as the command of “bjobs -app <app_name>"?

Steps

LSF provides some C API examples under LSF cluster directory "<LSF_TOP>/10.1/misc/examples".  "<LSF_TOP>/10.1/misc/examples/simbjobs.c" is an example that could show job list submitted with "-app" option.
Submit 3 jobs for test. The last 2 jobs are for "app2".
$ bsub -app app1 sleep 99999
Job <61342> is submitted to default queue <normal>.
$ bsub -app app2 sleep 99999
Job <61343> is submitted to default queue <normal>.
$ bsub -app app2 sleep 99999
Job <61344> is submitted to default queue <normal>.
Edit simbjobs.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <lsf/lsbatch.h>

#ifndef MAXLINELEN
#define MAXLINELEN  512
#endif

extern void add_pair2submit_ext();

int
main (argc, argv)
    int argc;
    char *argv[];
{
    struct jobInfoEnt *job;                   /*detail job info*/
    struct jobInfoHeadExt *jInfoHExt = NULL;  /*brief job Information head extent*/
    struct jobInfoReq  req;                    /*job request info*/
    struct affinityHostInfo *hostAffinity;
    char buf[MAXLINELEN];
    int more;                                 /*number of remaining jobs unread*/

.......

    memset(&req, 0, sizeof(struct jobInfoReq));
    req.app = "app2";                            /* Specify app2 */

    /*add "affinity" string field*/
    add_pair2submit_ext(&req.submitExt, JDATA_EXT_AFFINITYINFO, "affinity");
    jInfoHExt = lsb_openjobinfo_req(&req);
    if (jInfoHExt == NULL) {
        lsb_perror("simbjobs: lsb_openjobinfo_req() failed");
        exit (-1);
    }

    for (;;)
    {
        job = lsb_readjobinfo_cond(&more, jInfoHExt);
        if (job == NULL) {
            lsb_perror ("simbjobs: lsb_readjobinfo_req() failed");
            exit (-1);
        }
        printf ("Job <%s>, ", lsb_jobid2str (job->jobId));
        printf ("User <%s>, ", job->user);
        printf ("Queue <%s>, ", job->submit.queue);
        printf ("app <%s>, ", job->submit.app);                 /* show app name */
        printf ("Submitted from host <%s>, ",job->fromHost);
        int numHost= job->numhostAffinity;
        printf ("Total number of affinity host is <%d>.\n\n", numHost);

.......
    }
    lsb_closejobinfo ();
    exit (0);
}


Add the following 2 lines in simbjobs.c:
    req.app = "app2";
........
    printf ("app <%s>, ", job->submit.app);
Compile and build simbjobs.c
gcc -g -o simbjobs -I $LSF_ENVDIR/../10.1/include/ -ldl -lnsl -lm $LSF_LIBDIR/libbat.so $LSF_LIBDIR/liblsf.so -lpthread -lrt simbjobs.c
"simbjobs" is generated which can report the jobs that submitted with "-app app2".
$ bjobs -o "jobid app"
JOBID APPLICATION
61342 app1
61343 app2
61344 app2

$ bjobs -app app2
JOBID   USER    STAT  QUEUE      FROM_HOST   EXEC_HOST   JOB_NAME   SUBMIT_TIME
61343   jxiu    RUN   normal     bjhd07      bjhc01      *eep 99999 May 20 13:27
61344   jxiu    RUN   normal     bjhd07      bjhc01      *eep 99999 May 20 13:33

$ ./simbjobs_app
Job <61343>, User <jxiu>, Queue <normal>, app <app2>, Submitted from host <bjhd07>, Total number of affinity host is <-1>.
Job <61344>, User <jxiu>, Queue <normal>, app <app2>, Submitted from host <bjhd07>, Total number of affinity host is <-1>.

Document Location

Worldwide

[{"Type":"SW","Line of Business":{"code":"LOB10","label":"Data and AI"},"Business Unit":{"code":"BU059","label":"IBM Software w\/o TPS"},"Product":{"code":"SSWRJV","label":"IBM Spectrum LSF"},"ARM Category":[{"code":"a8m50000000CeJVAA0","label":"API"}],"ARM Case Number":"TS005662816","Platform":[{"code":"PF025","label":"Platform Independent"}],"Version":"10.1.0"}]

Document Information

Modified date:
25 November 2021

UID

ibm16454797