index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. var log = require('hlogger').createLogger("monitor");
  2. function ntokb(n) {
  3. var suffix = ["B","KB","MB","GB"];
  4. var si = 0;
  5. while( n> 1024 && si< suffix.length) {
  6. n/=1024;
  7. si++;
  8. }
  9. return n.toFixed(2) + " " +suffix[si];
  10. }
  11. var monitor = {
  12. bundleStart(context) {
  13. this.context = context;
  14. log.info("Bundle monitor started");
  15. this.registerCommands(context);
  16. context.on('hci-config', (config) => {
  17. context.on(config,"start",function() {
  18. config.test();
  19. });
  20. });
  21. },
  22. registerCommands(context) {
  23. context
  24. .channel('core-shell:cmd')
  25. .on('stat',(req,res,e) => {
  26. this.showStat(res); // Should pass stdout
  27. });
  28. // listen from command from ourselves
  29. /*context.on('core-shell:cmd:stat',(req,res,e) => {
  30. this.showStat(res); // Should pass stdout
  31. }).on('core-shell:cmd:gc',(req,res,e) => {
  32. gc();
  33. })
  34. context.on('core-shell:cmd:test',(req,res,e) => {
  35. });/**/
  36. },
  37. showStat(res) {
  38. var context = this.context;
  39. var obj = process.memoryUsage();
  40. if(res == undefined) res = process.stdout;
  41. res.write("Memory Rss: " + ntokb(obj.rss)
  42. +" Total: " + ntokb(obj.heapTotal)
  43. +" Used: " + ntokb(obj.heapUsed) +"\n");
  44. // Weird?
  45. /*res.write("Modules registered: " + context.manager.stat.modulesRegistered
  46. +" Modules UnRegistered: " + context.manager.stat.modulesUnRegistered
  47. +" events emitted: " + context.manager.events.stat.emitted
  48. +" events called: " + context.manager.events.stat.called
  49. +" events tried: " + context.manager.events.stat.tried + "\n");*/
  50. },
  51. // Human readable stat
  52. getStat() {
  53. var obj = {};
  54. var mem = process.memoryUsage();
  55. obj.mem = {
  56. rss: ntokb(mem.rss),
  57. heapTotal: ntokb(mem.heapTotal),
  58. heapUsed: ntokb(mem.heapUsed),
  59. }
  60. return obj;
  61. }
  62. };
  63. module.exports = monitor;