Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit eca9f7b

Browse files
committed
Handle threads in PerfListener
1 parent 2a79f46 commit eca9f7b

File tree

5 files changed

+33
-9
lines changed

5 files changed

+33
-9
lines changed

src/perf/PerfListener.cc

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,12 @@ namespace perf {
3232

3333
const Feature PerfListener::feature = Feature::PERF;
3434

35-
PerfListener::PerfListener(uint64_t instructionCountLimit)
36-
: perfFd_(-1), instructionCountLimit_(instructionCountLimit) {}
35+
PerfListener::PerfListener(
36+
uint64_t instructionCountLimit,
37+
uint64_t samplingFactor)
38+
: instructionCountLimit_(instructionCountLimit)
39+
, samplingFactor_{std::max<uint64_t>(1ULL, samplingFactor)}
40+
, perfFd_{-1} {}
3741

3842
PerfListener::~PerfListener() {
3943
// TODO: handle closing perfFd in move assignement / constructor as well
@@ -72,7 +76,6 @@ void PerfListener::onPostForkParent(pid_t childPid) {
7276
TRACE();
7377

7478
childPid_ = childPid;
75-
// TODO: fix this // What fix?
7679
struct perf_event_attr attrs {};
7780
memset(&attrs, 0, sizeof(attrs));
7881
attrs.type = PERF_TYPE_HARDWARE;
@@ -83,8 +86,9 @@ void PerfListener::onPostForkParent(pid_t childPid) {
8386
attrs.exclude_hv = 1;
8487
attrs.disabled = 1;
8588
attrs.enable_on_exec = 1;
89+
attrs.inherit = 1;
8690
if (instructionCountLimit_ != 0) {
87-
attrs.sample_period = instructionCountLimit_;
91+
attrs.sample_period = instructionCountLimit_ / samplingFactor_;
8892
attrs.wakeup_events = 1;
8993
}
9094
// Apparently older (3.13) kernel versions doesn't support

src/perf/PerfListener.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ class PerfListener
1111
: public executor::ExecuteEventListener
1212
, public printer::OutputSource {
1313
public:
14-
PerfListener(uint64_t instructionCountLimit);
14+
PerfListener(uint64_t instructionCountLimit, uint64_t samplingFactor);
1515
~PerfListener();
1616

1717
void onPreFork() override;
@@ -25,8 +25,9 @@ class PerfListener
2525
private:
2626
uint64_t getInstructionsUsed();
2727

28+
const uint64_t instructionCountLimit_;
29+
const uint64_t samplingFactor_;
2830
int perfFd_;
29-
uint64_t instructionCountLimit_;
3031
pid_t childPid_{};
3132

3233
// Barrier used for synchronization

src/s2japp/Application.cc

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,15 @@ Application::ExitCode Application::handleRun() {
7272
settings_.threadsLimit >= 0);
7373

7474
auto traceExecutor = createListener<tracer::TraceExecutor>();
75-
auto perfListener =
76-
createListener<perf::PerfListener>(settings_.instructionCountLimit);
75+
76+
77+
uint64_t perfSamplingFactor = settings_.perfOversamplingFactor;
78+
if (settings_.threadsLimit > 0) {
79+
perfSamplingFactor *= static_cast<uint64_t>(settings_.threadsLimit);
80+
}
81+
82+
auto perfListener = createListener<perf::PerfListener>(
83+
settings_.instructionCountLimit, perfSamplingFactor);
7784
auto userNsListener = createListener<ns::UserNamespaceListener>();
7885
auto utsNsListener = createListener<ns::UTSNamespaceListener>();
7986
auto ipcNsListener = createListener<ns::IPCNamespaceListener>();

src/s2japp/ApplicationSettings.cc

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
#include "ApplicationSettings.h"
21
#include "ApplicationArguments.h"
32
#include "ApplicationException.h"
3+
#include "ApplicationSettings.h"
44

55
#include "common/Utils.h"
66
#include "printer/AugmentedOIOutputBuilder.h"
@@ -312,6 +312,16 @@ ApplicationSettings::ApplicationSettings(int argc, const char* argv[])
312312
"count",
313313
cmd);
314314

315+
TCLAP::ValueArg<uint32_t> argPerfOversamplingFactor(
316+
"w",
317+
"perf-oversampling-factor",
318+
"Additional number of perf wakupus during each base period "
319+
"(wakeups number equals threads limit * oversampling factor)",
320+
false,
321+
2,
322+
"factor",
323+
cmd);
324+
315325

316326
TCLAP::UnlabeledValueArg<std::string> argProgramName(
317327
"path", "Name of program to run", true, "", "path", cmd);
@@ -397,6 +407,7 @@ ApplicationSettings::ApplicationSettings(int argc, const char* argv[])
397407
suppressStderr = !argShowStderr.getValue();
398408
resultsFD = argResultsFD.getValue();
399409
threadsLimit = argThreadsLimit.getValue();
410+
perfOversamplingFactor = argPerfOversamplingFactor.getValue();
400411
}
401412
catch (const TCLAP::ArgException& ex) {
402413
outputGenerator.failure(ex);

src/s2japp/ApplicationSettings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ struct ApplicationSettings : public ns::MountNamespaceListener::Settings {
4545

4646
int resultsFD{};
4747
int threadsLimit{};
48+
uint32_t perfOversamplingFactor{};
4849

4950
std::string parsingError;
5051
std::string helpMessage;

0 commit comments

Comments
 (0)