Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Note
titleContent Freeze

As of July 25th, 2023, there is a content freeze on this page.

Node.js by default is using a single core of a processor for code execution. Therefore, Node.js introduced a cluster module to spawn processes. “Cluster” was introduced to scale an application execution on multiple processor cores by creating worker processes. Worker processes share a single port, therefore, requests are routed through a shared port.

An alternate approach for scaling an application is to set up multiple Profound.js instances and use a load-balancer. The load-balancer method will allow you to scale to multiple servers and supports the setup of stateless and stateful apps.

This page shows the changes required to use clustering with Profound.js.

You can read more about that cluster module on the Node.js website https://nodejs.org/api/cluster.html

Create a new config file for the cluster module in your Profound.js installation directory (/profoundjs by default) called cluster.json

...

profoundjs/cluster.json
Code Block
languagejs
{                    
"workers": 4,    
"min_workers": 4     
}                    

Modify the start.js file to include the cluster module

 

Pre-Profoundjs 6.0 - profoundjs/start.js
Code Block
languagejavascripttitleprofoundjs/start.js
#!/usr/bin/env node                              
                                                 
var cluster = require("cluster");                
                                                 
if (cluster.isMaster) {                          
   var conf = require("./cluster.json");         
                                                 
   // Figure out number of workers.              
   var numWorkers = conf.workers;                
   if (numWorkers == 0) {                        
      var numCPUs = require("os").cpus().length; 
      if (numCPUs < conf.min_workers) {          
         numWorkers = conf.min_workers;          
      } else {                                   
         numWorkers = numCPUs;                   
      }                                          
   }                                             
 
   // Fork workers.                                       
   //console.log("Starting " + numWorkers + " workers."); 
   for (var i = 0; i < numWorkers; i++) {                 
       cluster.fork();                                    
   }                                                      
                                                          
   cluster.on('exit', (worker, code, signal) => { });     
                                                          
} else {                                                  
                                                          
   // Load Profound.js                                    
   var profoundjs = require("profoundjs");                
                                                          
   // Apply configuration                                 
   var config = require("./config.js");                   
   profoundjs.applyConfig(config);                        
                                                          
   // Start Profound.js server                            
   var isWorker = profoundjs.server.listen();                              
   if (isWorker) {                                                         
                                                                           
      // This is the top-level Express Application.                        
      // Custom Express coding can be added here.                          
      var express = profoundjs.server.express;                             
      var app = profoundjs.server.app;                                     
      app.use(express.json());  // default to use JSON-encoded post data   
                                                                           
   }                                                                       
}                                                                          

Profoundjs 6.0 and above - profoundjs/start.js
Code Block
languagejs
#!/usr/bin/env node

const cluster = require("cluster");
const os = require("os");

async function startPJS() {
  const profoundjs = require("profoundjs");
  const config = require("./config.js");

  await profoundjs.applyConfig(config);
  const isWorker = await profoundjs.server.listen();

  if (isWorker) {
    const express = profoundjs.server.express;
    const app = profoundjs.server.app;
    app.use(express.json()); // default to use JSON-encoded post data
  }
}

if (cluster.isMaster) {
  const conf = require("./cluster.json");

  // Determine the number of workers
  let numWorkers = conf.workers;
  if (numWorkers === 0) {
    const numCPUs = os.cpus().length;
    numWorkers = Math.max(numCPUs, conf.min_workers);
  }

  // Fork workers
  for (let i = 0; i < numWorkers; i++) {
    cluster.fork();
  }

  cluster.on('exit', (worker, code, signal) => {
    console.log(`Worker ${worker.process.pid} died. Code: ${code}, Signal: ${signal}`);
    // Optionally restart worker or handle exit
  });

} else {
  startPJS();
}

Understanding CPU Utilization on IBM i

Yes, an IBM i system can report CPU usage that exceeds 100% when using multiple processors. This happens because the CPU usage percentage is based on the utilization across all available processors. Here's how it works:

Understanding CPU Utilization on IBM i

  • Single Processor System: On a single-processor system, 100% CPU usage means the single processor is fully utilized.

  • Multi-Processor System: On a multi-processor system, the CPU utilization is calculated as a percentage of the total processing power available. For example:

    • If you have 4 processors and each is fully utilized, the system might report 400% CPU usage.

    • If 2 processors are fully utilized and the other 2 are idle, the system might report 200% CPU usage.

IBM i Specifics

  • Work Management: IBM i uses a sophisticated work management system that distributes workloads across multiple processors. The system monitors and reports CPU usage for each processor and aggregates the results.

  • System Performance Tools: Tools like WRKACTJOB (Work with Active Jobs) and WRKSYSSTS (Work with System Status) can show you CPU usage across all active jobs and the system as a whole. If your system has more than one CPU, it's normal to see CPU percentages exceeding 100%.

Practical Example

  • 4 CPUs Example: On a system with 4 CPUs, if each CPU is running at full capacity, the total CPU usage reported might be 400%. This reflects the total workload being processed by the system.

Summary

  • On IBM i, CPU usage over 100% indicates that the system is utilizing more than one processor.

  • This is a normal and expected behavior in multi-processor systems, and it helps provide a better understanding of how much of the total available processing power is being used.

If you see CPU usage above 100%, it's a sign that your IBM i system is efficiently distributing workloads across multiple processors.