index.js 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. .on('gc',(req,res,e) => {
  29. gc();
  30. });
  31. // listen from command from ourselves
  32. /*context.on('core-shell:cmd:stat',(req,res,e) => {
  33. this.showStat(res); // Should pass stdout
  34. }).on('core-shell:cmd:gc',(req,res,e) => {
  35. gc();
  36. })
  37. context.on('core-shell:cmd:test',(req,res,e) => {
  38. });/**/
  39. },
  40. showStat(res) {
  41. var context = this.context;
  42. var obj = process.memoryUsage();
  43. if(res == undefined) res = process.stdout;
  44. res.write("Memory Rss: " + ntokb(obj.rss)
  45. +" Total: " + ntokb(obj.heapTotal)
  46. +" Used: " + ntokb(obj.heapUsed) +"\n");
  47. // Sum events
  48. var nevents = 0;
  49. context.manager.eventual._events.ctx.forEach((c) => {
  50. nevents += Object.keys(c.listeners).length;
  51. });
  52. res.write("Events: " + nevents + "\n");
  53. // Weird?
  54. /*res.write("Modules registered: " + context.manager.stat.modulesRegistered
  55. +" Modules UnRegistered: " + context.manager.stat.modulesUnRegistered
  56. +" events emitted: " + context.manager.events.stat.emitted
  57. +" events called: " + context.manager.events.stat.called
  58. +" events tried: " + context.manager.events.stat.tried + "\n");*/
  59. },
  60. // Human readable stat
  61. getStat() {
  62. var obj = {};
  63. var mem = process.memoryUsage();
  64. obj.mem = {
  65. rss: ntokb(mem.rss),
  66. heapTotal: ntokb(mem.heapTotal),
  67. heapUsed: ntokb(mem.heapUsed),
  68. }
  69. return obj;
  70. }
  71. };
  72. module.exports = monitor;