@@ -106,6 +106,11 @@ static bool privsAlreadyElevated()
106106 return isElevated;
107107}
108108
109+ QStringList PerfRecord::offCpuProfilingOptions ()
110+ {
111+ return {QStringLiteral (" --switch-events" ), QStringLiteral (" --event" ), QStringLiteral (" sched:sched_switch" )};
112+ }
113+
109114void PerfRecord::startRecording (bool elevatePrivileges, const QStringList& perfOptions, const QString& outputPath,
110115 const QStringList& recordOptions, const QString& workingDirectory)
111116{
@@ -115,7 +120,7 @@ void PerfRecord::startRecording(bool elevatePrivileges, const QStringList& perfO
115120 // then parse its output and once we get the "waiting..." line the privileges got elevated
116121 // in that case, we can continue to start perf and quit the elevate_perf_privileges.sh script
117122 // once perf has started
118- const auto sudoBinary = sudoUtil ();
123+ const auto sudoBinary = Util:: sudoUtil ();
119124 if (sudoBinary.isEmpty ()) {
120125 emit recordingFailed (tr (" No sudo utility found. Please install pkexec, kdesudo or kdesu." ));
121126 return ;
@@ -395,107 +400,3 @@ void PerfRecord::sendInput(const QByteArray& input)
395400 Q_ASSERT (m_perfRecordProcess);
396401 m_perfRecordProcess->write (input);
397402}
398-
399- QString PerfRecord::sudoUtil ()
400- {
401- const auto commands = {
402- QStringLiteral (" pkexec" ), QStringLiteral (" kdesudo" ), QStringLiteral (" kdesu" ),
403- // gksudo / gksu seem to close stdin and thus the elevate script doesn't wait on read
404- };
405- for (const auto & cmd : commands) {
406- QString util = QStandardPaths::findExecutable (cmd);
407- if (!util.isEmpty ()) {
408- return util;
409- }
410- }
411- return {};
412- }
413-
414- QString PerfRecord::currentUsername ()
415- {
416- return KUser ().loginName ();
417- }
418-
419- bool PerfRecord::canTrace (const QString& path)
420- {
421- QFileInfo info (QLatin1String (" /sys/kernel/debug/tracing/" ) + path);
422- if (!info.isDir () || !info.isReadable ()) {
423- return false ;
424- }
425- QFile paranoid (QStringLiteral (" /proc/sys/kernel/perf_event_paranoid" ));
426- return paranoid.open (QIODevice::ReadOnly) && paranoid.readAll ().trimmed () == " -1" ;
427- }
428-
429- static QByteArray perfOutput (const QStringList& arguments)
430- {
431- QProcess process;
432-
433- auto reportError = [&]() {
434- qWarning () << " Failed to run perf" << process.arguments () << process.error () << process.errorString ()
435- << process.readAllStandardError ();
436- };
437-
438- QProcessEnvironment env = QProcessEnvironment::systemEnvironment ();
439- env.insert (QStringLiteral (" LANG" ), QStringLiteral (" C" ));
440- process.setProcessEnvironment (env);
441-
442- QObject::connect (&process, &QProcess::errorOccurred, &process, reportError);
443- process.start (QStringLiteral (" perf" ), arguments);
444- if (!process.waitForFinished (1000 ) || process.exitCode () != 0 )
445- reportError ();
446- return process.readAllStandardOutput ();
447- }
448-
449- static QByteArray perfRecordHelp ()
450- {
451- static const QByteArray recordHelp = []() {
452- static QByteArray help = perfOutput ({QStringLiteral (" record" ), QStringLiteral (" --help" )});
453- if (help.isEmpty ()) {
454- // no man page installed, assume the best
455- help = " --sample-cpu --switch-events" ;
456- }
457- return help;
458- }();
459- return recordHelp;
460- }
461-
462- static QByteArray perfBuildOptions ()
463- {
464- static const QByteArray buildOptions = perfOutput ({QStringLiteral (" version" ), QStringLiteral (" --build-options" )});
465- return buildOptions;
466- }
467-
468- bool PerfRecord::canProfileOffCpu ()
469- {
470- return canTrace (QStringLiteral (" events/sched/sched_switch" ));
471- }
472-
473- QStringList PerfRecord::offCpuProfilingOptions ()
474- {
475- return {QStringLiteral (" --switch-events" ), QStringLiteral (" --event" ), QStringLiteral (" sched:sched_switch" )};
476- }
477-
478- bool PerfRecord::canSampleCpu ()
479- {
480- return perfRecordHelp ().contains (" --sample-cpu" );
481- }
482-
483- bool PerfRecord::canSwitchEvents ()
484- {
485- return perfRecordHelp ().contains (" --switch-events" );
486- }
487-
488- bool PerfRecord::canUseAio ()
489- {
490- return perfBuildOptions ().contains (" aio: [ on ]" );
491- }
492-
493- bool PerfRecord::canCompress ()
494- {
495- return Zstd_FOUND && perfBuildOptions ().contains (" zstd: [ on ]" );
496- }
497-
498- bool PerfRecord::isPerfInstalled ()
499- {
500- return !QStandardPaths::findExecutable (QStringLiteral (" perf" )).isEmpty ();
501- }
0 commit comments