@@ -114,7 +114,9 @@ namespace Monitors
114114 DUNE::Tasks::Task (name, ctx),
115115 m_tstamp (0 )
116116 {
117- paramActive (Tasks::Parameter::SCOPE_GLOBAL, Tasks::Parameter::VISIBILITY_USER);
117+ paramActive (Tasks::Parameter::SCOPE_GLOBAL,
118+ Tasks::Parameter::VISIBILITY_USER);
119+
118120 m_num_cpus = sysconf (_SC_NPROCESSORS_ONLN);
119121 if (m_num_cpus <= 0 || m_num_cpus > c_max_cpu)
120122 {
@@ -130,6 +132,7 @@ namespace Monitors
130132 setEntityState (IMC::EntityState::ESTA_NORMAL, Status::CODE_IDLE);
131133 }
132134
135+ // ! Reserve entity identifiers.
133136 void
134137 onEntityReservation (void )
135138 {
@@ -493,20 +496,117 @@ namespace Monitors
493496 {
494497 while (!stopping ())
495498 {
496- try
497- {
498- waitForMessages (0.01 );
499- }
500- catch (...)
501- {
502- war (" waitForMessages error" );
503- }
499+ waitForMessages (0.01 );
500+
504501 if (!isActive ())
505502 continue ;
506503
507- if (m_reads_check.overflow ())
504+ if (m_ram_check.overflow ())
505+ {
506+ m_ram_check.reset ();
507+ m_tstamp = Clock::get ();
508+ uint8_t cpu = 0 ;
509+ try
510+ {
511+ cpu = (uint8_t )getDUNECPUUsage ();
512+ if (m_num_cpus > 1 && cpu > 0 )
513+ cpu /= m_num_cpus;
514+ }
515+ catch (std::exception& e)
516+ {
517+ war (" Failed to get CPU usage: %s" , e.what ());
518+ cpu = 0 ;
519+ }
520+ catch (...)
521+ {
522+ war (" Failed to get CPU usage: unknown error" );
523+ cpu = 0 ;
524+ }
525+
526+ double ram = 0.0 ;
527+ try
528+ {
529+ ram = getDUNERAMUsage ();
530+ }
531+ catch (std::exception& e)
532+ {
533+ war (" Failed to get RAM usage: %s" , e.what ());
534+ ram = 0 ;
535+ }
536+ catch (...)
537+ {
538+ war (" Failed to get RAM usage: unknown error" );
539+ ram = 0 ;
540+ }
541+
542+ double swap = 0.0 ;
543+ try
544+ {
545+ swap = getDUNESwapUsage ();
546+ }
547+ catch (std::exception& e)
548+ {
549+ war (" Failed to get Swap usage: %s" , e.what ());
550+ swap = 0 ;
551+ }
552+ catch (...)
553+ {
554+ war (" Failed to get Swap usage: unknown error" );
555+ swap = 0 ;
556+ }
557+
558+ // dispatch CPU and RAM usage
559+ try {
560+ m_dune_cpu_usage[m_num_cpus].value = cpu;
561+ m_dune_cpu_usage[m_num_cpus].setTimeStamp (m_tstamp);
562+ dispatch (m_dune_cpu_usage[m_num_cpus]);
563+ }
564+ catch (std::exception& e)
565+ {
566+ war (" Failed to dispatch CPU usage: %s" , e.what ());
567+ }
568+ catch (...)
569+ {
570+ war (" Failed to dispatch CPU usage: unknown error" );
571+ }
572+ #if IMC_RAM_USAGE_MESSAGE_EXISTS
573+ try {
574+ m_dune_ram_usage[0 ].value = ram * 1024 .0f ; // KB -> MB
575+ m_dune_ram_usage[0 ].setTimeStamp (m_tstamp);
576+ dispatch (m_dune_ram_usage[0 ]);
577+ m_dune_ram_usage[1 ].value = swap * 1024 .0f ; // KB -> MB
578+ m_dune_ram_usage[1 ].setTimeStamp (m_tstamp);
579+ dispatch (m_dune_ram_usage[1 ]);
580+ }
581+ catch (std::exception& e)
582+ {
583+ war (" Failed to dispatch RAM usage: %s" , e.what ());
584+ }
585+ catch (...)
586+ {
587+ war (" Failed to dispatch RAM usage: unknown error" );
588+ }
589+ #endif
590+ try {
591+ // check if m_buffer_cpu_entity is empty
592+ const char * entity = m_buffer_cpu_entity.empty () ? " unknown" : m_buffer_cpu_entity.c_str ();
593+ // set entity state with CPU, RAM and Swap usage
594+ trace (" DUNE Process: CPU: %d%%, RAM: %.1fMB, Swap: %.1fMB | %s" , cpu, ram, swap, entity);
595+ std::string msg = String::str (" %s | DUNE (C:%d%%, R:%.1fMB, S:%.1fMB)" , entity, cpu, ram, swap);
596+ setEntityState (IMC::EntityState::ESTA_NORMAL, msg);
597+ }
598+ catch (std::exception& e)
599+ {
600+ war (" Failed to set entity state: %s" , e.what ());
601+ }
602+ catch (...)
603+ {
604+ war (" Failed to set entity state: unknown error" );
605+ }
606+ }
607+ else if (m_cpu_check.overflow ())
508608 {
509- m_reads_check .reset ();
609+ m_cpu_check .reset ();
510610 m_tstamp = Clock::getSinceEpoch ();
511611 getDuneUsageResources (m_tstamp);
512612 getUsageOfCPU (m_tstamp);
0 commit comments