JSPM

  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 72
  • Score
    100M100P100Q80740F
  • License MIT

IPC shared memory for NodeJs. Use as Buffer or TypedArray.

Package Exports

  • shm-typed-array

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (shm-typed-array) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

IPC shared memory for NodeJs. Use as Buffer or TypedArray.

Install

$ npm install shm-typed-array
$ npm test

Manual build:

node-gyp configure
node-gyp build
node example.js

Tested on Ubuntu 16, Node v6.9.1

API

shm.create (count, typeKey)

Create shared memory segment.
Count - number of elements (not bytes), typeKey - type of elements, see list below.
Returns shared memory Buffer or descendant of TypedArray object, class depends on param "typeKey"
Return object has property 'key' - integer key of created shared memory segment, to use in shm.get().

shm.get (key, typeKey)

Get created shared memory segment.

shm.detach (key)

Detach shared memory segment.
If there are no other attaches for this segment, it will be destroyed.

shm.detachAll ()

Detach all shared memory segments.
Will be automatically called on process exit/termination.

Types:

shm.BufferType = {
    'Buffer': shm.SHMBT_BUFFER,
    'Int8Array': shm.SHMBT_INT8,
    'Uint8Array': shm.SHMBT_UINT8,
    'Uint8ClampedArray': shm.SHMBT_UINT8CLAMPED,
    'Int16Array': shm.SHMBT_INT16,
    'Uint16Array': shm.SHMBT_UINT16,
    'Int32Array': shm.SHMBT_INT32,
    'Uint32Array': shm.SHMBT_UINT32,
    'Float32Array': shm.SHMBT_FLOAT32, 
    'Float64Array': shm.SHMBT_FLOAT64,
};
shm.BufferTypeSizeof = {
    'Buffer': 1,
    'Int8Array': 1,
    'Uint8Array': 1,
    'Uint8ClampedArray': 1,
    'Int16Array': 2,
    'Uint16Array': 2,
    'Int32Array': 4,
    'Uint32Array': 4,
    'Float32Array': 4, 
    'Float64Array': 8,
};

shm.getTotalSize()

Get total size of all shared segments in bytes.

shm.SizeMax

Max size of shared memory segment in bytes
2GB (0x7fffffff) for 64bit, 1GB for 32bit

Usage

See example.js

const cluster = require('cluster');
const shm = require('./index.js');

var buf, arr;
if (cluster.isMaster) {
    buf = shm.create(4096); //4KB
    arr = shm.create(1000000*100, 'Float32Array'); //100M floats
    buf[0] = 1;
    arr[0] = 10.0;
    console.log('[Master] Typeof buf:', buf.constructor.name, 
        'Typeof arr:', arr.constructor.name);
    
    var worker = cluster.fork();
    worker.on('online', function() {
        this.send({ msg: 'shm', bufKey: buf.key, arrKey: arr.key });
        var i = 0;
        setInterval(function() {
            buf[0] += 1;
            arr[0] /= 2;
            console.log(i + ' [Master] Set buf[0]=', buf[0], 
                ' arr[0]=', arr ? arr[0] : null);
            i++;
            if (i == 5) {
                groupSuicide();
            }
        }, 500);
    });	
} else {
    process.on('message', function(data) {
        var msg = data.msg;
        if (msg == 'shm') {
            buf = shm.get(data.bufKey);
            arr = shm.get(data.arrKey, 'Float32Array');
            console.log('[Worker] Typeof buf:', buf.constructor.name, 
                'Typeof arr:', arr.constructor.name);
            var i = 0;
            setInterval(function() {
                console.log(i + ' [Worker] Get buf[0]=', buf[0], 
                    ' arr[0]=', arr ? arr[0] : null);
                i++;
                if (i == 2) {
                    shm.detach(data.arrKey);
                    arr = null; //otherwise process will drop
                }
            }, 500);
        } else if (msg == 'exit') {
            process.exit();
        }
    });
}

function groupSuicide() {
    if (cluster.isMaster) {
        for (var id in cluster.workers) {
            cluster.workers[id].send({ msg: 'exit'});
            cluster.workers[id].destroy();
        }
        process.exit();
    }
}

Output:

[Master] Typeof buf: Buffer Typeof arr: Float32Array
[Worker] Typeof buf: Buffer Typeof arr: Float32Array
0 [Master] Set buf[0]= 2  arr[0]= 5
0 [Worker] Get buf[0]= 2  arr[0]= 5
1 [Master] Set buf[0]= 3  arr[0]= 2.5
1 [Worker] Get buf[0]= 3  arr[0]= 2.5
2 [Master] Set buf[0]= 4  arr[0]= 1.25
2 [Worker] Get buf[0]= 4  arr[0]= null
3 [Master] Set buf[0]= 5  arr[0]= 0.625
3 [Worker] Get buf[0]= 5  arr[0]= null
4 [Master] Set buf[0]= 6  arr[0]= 0.3125
shm segments destroyed: 2