12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- var log = require('hlogger').createLogger("monitor");
- function ntokb(n) {
- var suffix = ["B","KB","MB","GB"];
- var si = 0;
- while( n> 1024 && si< suffix.length) {
- n/=1024;
- si++;
- }
- return n.toFixed(2) + " " +suffix[si];
- }
- var monitor = {
-
- bundleStart(context) {
- this.context = context;
- log.info("Bundle monitor started");
- this.registerCommands(context);
- context.on('hci-config', (config) => {
- context.on(config,"start",function() {
- config.test();
- });
- });
- },
- registerCommands(context) {
- context
- .channel('core-shell:cmd')
- .on('stat',(req,res,e) => {
- this.showStat(res); // Should pass stdout
- })
- .on('gc',(req,res,e) => {
- gc();
- });
-
- // listen from command from ourselves
- /*context.on('core-shell:cmd:stat',(req,res,e) => {
- this.showStat(res); // Should pass stdout
- }).on('core-shell:cmd:gc',(req,res,e) => {
- gc();
- })
- context.on('core-shell:cmd:test',(req,res,e) => {
- });/**/
- },
- showStat(res) {
- var context = this.context;
- var obj = process.memoryUsage();
- if(res == undefined) res = process.stdout;
- res.write("Memory Rss: " + ntokb(obj.rss)
- +" Total: " + ntokb(obj.heapTotal)
- +" Used: " + ntokb(obj.heapUsed) +"\n");
- // Sum events
- var nevents = 0;
- context.manager.eventual._events.ctx.forEach((c) => {
- nevents += Object.keys(c.listeners).length;
- });
- res.write("Events: " + nevents + "\n");
- // Weird?
- /*res.write("Modules registered: " + context.manager.stat.modulesRegistered
- +" Modules UnRegistered: " + context.manager.stat.modulesUnRegistered
- +" events emitted: " + context.manager.events.stat.emitted
- +" events called: " + context.manager.events.stat.called
- +" events tried: " + context.manager.events.stat.tried + "\n");*/
- },
- // Human readable stat
- getStat() {
- var obj = {};
- var mem = process.memoryUsage();
- obj.mem = {
- rss: ntokb(mem.rss),
- heapTotal: ntokb(mem.heapTotal),
- heapUsed: ntokb(mem.heapUsed),
- }
- return obj;
- }
- };
- module.exports = monitor;
|