-
Notifications
You must be signed in to change notification settings - Fork 17
Account for CPU used by child processes #378
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
sawenzel
wants to merge
1
commit into
AliceO2Group:dev
Choose a base branch
from
sawenzel:fix/o2-7096-child-cpu-accounting
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+58
−11
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -60,6 +60,7 @@ ProcessMonitor::ProcessMonitor() | |
| mPid = static_cast<unsigned int>(::getpid()); | ||
| mTimeLastRun = std::chrono::high_resolution_clock::now(); | ||
| getrusage(RUSAGE_SELF, &mPreviousGetrUsage); | ||
| getrusage(RUSAGE_CHILDREN, &mPreviousGetrUsageChildren); | ||
| #ifdef O2_MONITORING_OS_LINUX | ||
| setTotalMemory(); | ||
| #endif | ||
|
|
@@ -99,6 +100,13 @@ void ProcessMonitor::init() | |
| { | ||
| mTimeLastRun = std::chrono::high_resolution_clock::now(); | ||
| getrusage(RUSAGE_SELF, &mPreviousGetrUsage); | ||
| getrusage(RUSAGE_CHILDREN, &mPreviousGetrUsageChildren); | ||
| // The aggregates describe one monitoring period: monitoring that is stopped | ||
| // and started again reports the new period, not both blended together. | ||
| mCpuPerctange.clear(); | ||
| mCpuMicroSeconds.clear(); | ||
| mVmSizeMeasurements.clear(); | ||
| mVmRssMeasurements.clear(); | ||
| } | ||
|
|
||
| void ProcessMonitor::enable(PmMeasurement measurement) | ||
|
|
@@ -167,27 +175,41 @@ std::vector<Metric> ProcessMonitor::getSmaps() | |
| return {{pssTotal, metricsNames[PSS]}, {cleanTotal, metricsNames[PRIVATE_CLEAN]}, {dirtyTotal, metricsNames[PRIVATE_DIRTY]}}; | ||
| } | ||
|
|
||
| std::vector<Metric> ProcessMonitor::getCpuAndContexts() | ||
| std::vector<Metric> ProcessMonitor::getCpuAndContexts(bool force) | ||
| { | ||
| std::vector<Metric> metrics; | ||
| struct rusage currentUsage; | ||
| struct rusage currentUsageChildren; | ||
| getrusage(RUSAGE_SELF, ¤tUsage); | ||
| // CPU of reaped children (e.g. an external event generator forked by o2-sim) | ||
| // is spent outside this process and is invisible to RUSAGE_SELF | ||
| getrusage(RUSAGE_CHILDREN, ¤tUsageChildren); | ||
| auto timeNow = std::chrono::high_resolution_clock::now(); | ||
| double timePassed = std::chrono::duration_cast<std::chrono::microseconds>(timeNow - mTimeLastRun).count(); | ||
| if (timePassed < 950) { | ||
| if (timePassed < 950 && !force) { | ||
| MonLogger::Get(Severity::Warn) << "Do not invoke Process Monitor more frequent then every 1s" << MonLogger::End(); | ||
| metrics.emplace_back("processPerformance"); | ||
| return metrics; | ||
| } | ||
|
|
||
| uint64_t cpuUsedInMicroSeconds = currentUsage.ru_utime.tv_sec * 1000000.0 + currentUsage.ru_utime.tv_usec - (mPreviousGetrUsage.ru_utime.tv_sec * 1000000.0 + mPreviousGetrUsage.ru_utime.tv_usec) + currentUsage.ru_stime.tv_sec * 1000000.0 + currentUsage.ru_stime.tv_usec - (mPreviousGetrUsage.ru_stime.tv_sec * 1000000.0 + mPreviousGetrUsage.ru_stime.tv_usec); | ||
| auto micros = [](const timeval& t) { return t.tv_sec * 1000000.0 + t.tv_usec; }; | ||
| auto cpuDelta = [µs](const struct rusage& now, const struct rusage& before) { | ||
| return micros(now.ru_utime) - micros(before.ru_utime) + micros(now.ru_stime) - micros(before.ru_stime); | ||
| }; | ||
| uint64_t cpuUsedInMicroSeconds = cpuDelta(currentUsage, mPreviousGetrUsage) + | ||
| cpuDelta(currentUsageChildren, mPreviousGetrUsageChildren); | ||
| double fractionCpuUsed = cpuUsedInMicroSeconds / timePassed; | ||
|
|
||
| double cpuUsedPerctange = std::round(fractionCpuUsed * 100.0 * 100.0) / 100.0; | ||
| mCpuPerctange.push_back(cpuUsedPerctange); | ||
| mCpuMicroSeconds.push_back(cpuUsedInMicroSeconds); | ||
|
|
||
| metrics.emplace_back(Metric{cpuUsedPerctange, metricsNames[CPU_USED_PERCENTAGE]}); | ||
| // A forced measurement may report CPU accumulated over the whole run but only | ||
| // made visible at once (children become visible on reap), for which an | ||
| // instantaneous rate is meaningless: report it as absolute time only. | ||
| if (!force) { | ||
| mCpuPerctange.push_back(cpuUsedPerctange); | ||
| metrics.emplace_back(Metric{cpuUsedPerctange, metricsNames[CPU_USED_PERCENTAGE]}); | ||
| } | ||
| metrics.emplace_back(Metric{ | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Notice that the context switches will also be wrong. |
||
| static_cast<uint64_t>(currentUsage.ru_nivcsw - mPreviousGetrUsage.ru_nivcsw), metricsNames[INVOLUNTARY_CONTEXT_SWITCHES]}); | ||
| metrics.emplace_back(Metric{ | ||
|
|
@@ -212,6 +234,7 @@ std::vector<Metric> ProcessMonitor::getCpuAndContexts() | |
|
|
||
| mTimeLastRun = timeNow; | ||
| mPreviousGetrUsage = currentUsage; | ||
| mPreviousGetrUsageChildren = currentUsageChildren; | ||
| return metrics; | ||
| } | ||
|
|
||
|
|
@@ -262,14 +285,21 @@ std::vector<Metric> ProcessMonitor::makeLastMeasurementAndGetMetrics() | |
| } | ||
| #endif | ||
| if (mEnabledMeasurements.at(static_cast<short>(PmMeasurement::Cpu))) { | ||
| getCpuAndContexts(); | ||
| // forced: no later call will pick up a delta discarded here | ||
| auto lastCpuMetrics = getCpuAndContexts(true); | ||
| std::move(lastCpuMetrics.begin(), lastCpuMetrics.end(), std::back_inserter(metrics)); | ||
|
|
||
| auto avgCpuUsage = std::accumulate(mCpuPerctange.begin(), mCpuPerctange.end(), 0.0) / | ||
| mCpuPerctange.size(); | ||
| uint64_t accumulationOfCpuTimeConsumption = std::accumulate(mCpuMicroSeconds.begin(), | ||
| mCpuMicroSeconds.end(), 0UL); | ||
|
|
||
| metrics.emplace_back(avgCpuUsage, metricsNames[AVG_CPU_USED_PERCENTAGE]); | ||
| // Only forced measurements contribute no percentage, so a process that | ||
| // ends before the first periodic sample has none at all - report no | ||
| // average rather than a NaN one. | ||
| if (!mCpuPerctange.empty()) { | ||
| auto avgCpuUsage = std::accumulate(mCpuPerctange.begin(), mCpuPerctange.end(), 0.0) / | ||
| mCpuPerctange.size(); | ||
| metrics.emplace_back(avgCpuUsage, metricsNames[AVG_CPU_USED_PERCENTAGE]); | ||
| } | ||
| metrics.emplace_back(accumulationOfCpuTimeConsumption, metricsNames[ACCUMULATED_CPU_TIME]); | ||
| } | ||
| return metrics; | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Small remark. This is not thread safe. You probably want a:
It should not matter for DPL (where the .stop will always be called on the main thread) however I do not know if there are other usages of this elsewhere. Given at some point it was advertised that Monitoring is thread safe, I do not know if that's an hard-requirement.