├── binding.gyp ├── child.coffee ├── .gitignore ├── bench.coffee ├── LICENSE ├── spawn.coffee └── shm_addon.cpp /binding.gyp: -------------------------------------------------------------------------------- 1 | { 2 | "targets": [ 3 | { 4 | "target_name": "shm_addon", 5 | "sources": ["shm_addon.cpp"] 6 | } 7 | ] 8 | } 9 | -------------------------------------------------------------------------------- /child.coffee: -------------------------------------------------------------------------------- 1 | shm = require './build/Release/shm_addon' 2 | 3 | a = new Int32Array shm.createSHM() 4 | 5 | start = -> 6 | c = 0 7 | k = 0 8 | while k < 1000 9 | t = 0 10 | i = 0 11 | while i < a.length 12 | if a[i] is k 13 | ++t 14 | ++i 15 | 16 | if t isnt 200 17 | throw new Error 'Counting error' 18 | c += t 19 | ++k 20 | 21 | console.log c 22 | process.exit() 23 | 24 | 25 | process.stdin.on 'data', (msg) -> 26 | start() 27 | 28 | 29 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Compiled source # 2 | ################### 3 | *.com 4 | *.class 5 | *.dll 6 | *.exe 7 | *.o 8 | *.so 9 | 10 | #swp 11 | .*.swp 12 | 13 | # Packages # 14 | ############ 15 | # it's better to unpack these files and commit the raw source 16 | # git has its own built in compression methods 17 | *.7z 18 | *.dmg 19 | *.gz 20 | *.iso 21 | *.jar 22 | *.rar 23 | *.tar 24 | *.zip 25 | 26 | # Logs and databases # 27 | ###################### 28 | *.log 29 | *.sql 30 | *.sqlite 31 | 32 | # OS generated files # 33 | ###################### 34 | .DS_Store* 35 | ehthumbs.db 36 | Icon? 37 | Thumbs.db 38 | node_modules 39 | build 40 | -------------------------------------------------------------------------------- /bench.coffee: -------------------------------------------------------------------------------- 1 | CHILDREN = 16 2 | data = null 3 | 4 | prepare = -> 5 | data = new Int32Array 200000 6 | N = 1000 7 | i = 0 8 | n = 0 9 | while i < data.length 10 | n = 0 if n is N 11 | data[i] = n 12 | ++i 13 | ++n 14 | 15 | count = -> 16 | c = 0 17 | k = 0 18 | while k < 1000 19 | t = 0 20 | i = 0 21 | while i < data.length 22 | if data[i] is k 23 | ++t 24 | ++i 25 | 26 | if t isnt 200 27 | throw new Error 'Counting error' 28 | c += t 29 | k++ 30 | 31 | console.log c 32 | 33 | prepare() 34 | 35 | console.log 'start' 36 | console.time 'count' 37 | for j in [0...CHILDREN] 38 | count() 39 | 40 | console.timeEnd 'count' 41 | 42 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2015 Varuna Jayasiri 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | 23 | -------------------------------------------------------------------------------- /spawn.coffee: -------------------------------------------------------------------------------- 1 | ps = require 'child_process' 2 | shm = require './build/Release/shm_addon' 3 | data = null 4 | workers = [] 5 | 6 | #Number of child processes 7 | CHILDREN = 64 8 | running = CHILDREN 9 | 10 | #Prepare data 11 | prepare = -> 12 | data = new Int32Array shm.createSHM() 13 | N = 1000 14 | i = 0 15 | n = 0 16 | while i < data.length 17 | n = 0 if n is N 18 | data[i] = n 19 | ++i 20 | ++n 21 | 22 | #Start processes 23 | start = -> 24 | console.log 'start' 25 | console.time 'count' 26 | for w in workers 27 | w.stdin.write 'start' 28 | 29 | #Finished process 30 | done = -> 31 | running-- 32 | if running is 0 33 | console.timeEnd 'count' 34 | 35 | #Spawn process 36 | spawn = (n) -> 37 | w = ps.spawn 'coffee', ['child.coffee'] 38 | 39 | w.stdout.on 'data', (msg) -> 40 | console.log "#{n}: #{msg}" 41 | done() 42 | 43 | w.stderr.on 'data', (msg) -> 44 | console.log "ERR: #{n}: #{msg}" 45 | 46 | w.on 'close', (status) -> 47 | #console.log "EXIT: #{n}: #{status}" 48 | 49 | workers.push w 50 | 51 | ############# 52 | 53 | prepare() 54 | for n in [0...CHILDREN] 55 | spawn n 56 | 57 | #Wait for processes to initialize 58 | setTimeout start, 2000 59 | -------------------------------------------------------------------------------- /shm_addon.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include 3 | #include 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | using namespace v8; 10 | using namespace std; 11 | 12 | char* data = NULL; 13 | const int MEM = 800000; /* Two hundred thousand 32-bit integers */ 14 | 15 | void create() { 16 | int key = 6562; 17 | int shmid; 18 | 19 | if( (shmid = shmget( key, MEM, IPC_CREAT | 0666 )) < 0 ) 20 | cout << "shmget failed" << endl; 21 | 22 | if( (data = (char *)shmat( shmid, NULL, 0 )) < 0 ) 23 | cout << "shmat failed." << endl; 24 | } 25 | 26 | void CreateSHM(const FunctionCallbackInfo& args) { 27 | Isolate* isolate = Isolate::GetCurrent(); 28 | HandleScope scope(isolate); 29 | 30 | /* If this is called by same node instance do not attach again */ 31 | if(!data) { 32 | create(); 33 | } 34 | 35 | /* Create a ArrayBuffer */ 36 | Local buffer = ArrayBuffer::New(isolate, (void *)data, MEM); 37 | 38 | /* Return buffer */ 39 | args.GetReturnValue().Set(buffer); 40 | } 41 | 42 | void init(Handle exports) { 43 | NODE_SET_METHOD(exports, "createSHM", CreateSHM); 44 | } 45 | 46 | NODE_MODULE(shm_addon, init) 47 | --------------------------------------------------------------------------------