@@ -414,6 +414,27 @@ void ProcessMonitor::getHighMemoryProcesses(const std::string & output)
414
414
getTopratedProcesses (&memory_tasks_, &p2);
415
415
}
416
416
417
+ bool ProcessMonitor::getCommandLineFromPiD (const std::string & pid, std::string & command)
418
+ {
419
+ std::string commandLineFilePath = " /proc/" + pid + " /cmdline" ;
420
+ std::ifstream commandFile (commandLineFilePath, std::ios::in | std::ios::binary);
421
+
422
+ if (commandFile.is_open ()) {
423
+ std::vector<uint8_t > buffer;
424
+ std::copy (
425
+ std::istream_iterator<uint8_t >(commandFile), std::istream_iterator<uint8_t >(),
426
+ std::back_inserter (buffer));
427
+ commandFile.close ();
428
+ std::replace (
429
+ buffer.begin (), buffer.end (), ' \0 ' ,
430
+ ' ' ); // 0x00 is used as delimiter in /cmdline instead of 0x20 (space)
431
+ command = std::string (buffer.begin (), buffer.end ());
432
+ return (buffer.size () > 0 ) ? true : false ; // cmdline is empty if it is kernel process
433
+ } else {
434
+ return false ;
435
+ }
436
+ }
437
+
417
438
void ProcessMonitor::getTopratedProcesses (
418
439
std::vector<std::shared_ptr<DiagTask>> * tasks, bp::pipe * p)
419
440
{
@@ -462,7 +483,14 @@ void ProcessMonitor::getTopratedProcesses(
462
483
info.virtualImage >> info.residentSize >> info.sharedMemSize >> info.processStatus >>
463
484
info.cpuUsage >> info.memoryUsage >> info.cpuTime ;
464
485
465
- std::getline (stream, info.commandName );
486
+ std::string program_name;
487
+ std::getline (stream, program_name);
488
+
489
+ bool flag_find_command_line = getCommandLineFromPiD (info.processId , info.commandName );
490
+
491
+ if (!flag_find_command_line) {
492
+ info.commandName = program_name; // if command line is not found, use program name instead
493
+ }
466
494
467
495
tasks->at (index )->setDiagnosticsStatus (DiagStatus::OK, " OK" );
468
496
tasks->at (index )->setProcessInformation (info);
@@ -515,7 +543,7 @@ void ProcessMonitor::onTimer()
515
543
std::ostringstream os;
516
544
517
545
// Get processes
518
- bp::child c (" top -bcn1 -o %CPU -w 256 " , bp::std_out > is_out, bp::std_err > is_err);
546
+ bp::child c (" top -bn1 -o %CPU -w 128 " , bp::std_out > is_out, bp::std_err > is_err);
519
547
c.wait ();
520
548
521
549
if (c.exit_code () != 0 ) {
0 commit comments